Finish flushes any buffered partial SSE data at the end of a stream.
()
| 256 | |
| 257 | // Finish flushes any buffered partial SSE data at the end of a stream. |
| 258 | func (r *StreamRewriter) Finish() []byte { |
| 259 | if len(r.pendingBuf) == 0 { |
| 260 | return nil |
| 261 | } |
| 262 | buf := make([]byte, len(r.pendingBuf)+2) |
| 263 | copy(buf, r.pendingBuf) |
| 264 | buf[len(r.pendingBuf)] = '\n' |
| 265 | buf[len(r.pendingBuf)+1] = '\n' |
| 266 | buf = normalizeGluedSSEEvents(buf) |
| 267 | r.pendingBuf = nil |
| 268 | out := r.RewriteChunk(buf) |
| 269 | if len(r.pendingBuf) > 0 { |
| 270 | tail := rewriteSSEPayloadLines(r.pendingBuf, r.options.RewriteModel) |
| 271 | r.pendingBuf = nil |
| 272 | if len(tail) > 0 { |
| 273 | if len(out) > 0 { |
| 274 | out = append(out, tail...) |
| 275 | } else { |
| 276 | out = tail |
| 277 | } |
| 278 | } |
| 279 | } |
| 280 | return out |
| 281 | } |