( response: Response, controller: AbortController, )
| 214 | * @throws Error if the response has no body. |
| 215 | */ |
| 216 | export async function* _iterSSEMessages( |
| 217 | response: Response, |
| 218 | controller: AbortController, |
| 219 | ): AsyncGenerator<ServerSentEvent, void, unknown> { |
| 220 | if (!response.body) { |
| 221 | controller.abort(); |
| 222 | throw new Error(`Attempted to iterate over a response with no body`); |
| 223 | } |
| 224 | |
| 225 | const sseDecoder = new SSEDecoder(); |
| 226 | const lineDecoder = new LineDecoder(); |
| 227 | |
| 228 | const iter = readableStreamAsyncIterable<Bytes>(response.body); |
| 229 | for await (const sseChunk of iterSSEChunks(iter)) { |
| 230 | for (const line of lineDecoder.decode(sseChunk)) { |
| 231 | const sse = sseDecoder.decode(line); |
| 232 | if (sse) yield sse; |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | for (const line of lineDecoder.flush()) { |
| 237 | const sse = sseDecoder.decode(line); |
| 238 | if (sse) yield sse; |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | /** |
| 243 | * Asynchronously iterates over SSE (Server-Sent Events) chunks and yields the data as Uint8Array. |
no test coverage detected