()
| 369 | // but after SSR deserialization it becomes ReadableStream<Uint8Array>. |
| 370 | // We accept both types to handle the TypeScript mismatch. |
| 371 | export function createStreamConsumer() { |
| 372 | const decoder = new TextDecoder() |
| 373 | |
| 374 | return async function consumeStream( |
| 375 | stream: ReadableStream<Uint8Array> | RawStream, |
| 376 | ): Promise<string> { |
| 377 | // Handle both RawStream (from type system) and ReadableStream (runtime) |
| 378 | const actualStream = |
| 379 | stream instanceof RawStream |
| 380 | ? stream.stream |
| 381 | : (stream as ReadableStream<Uint8Array>) |
| 382 | const reader = actualStream.getReader() |
| 383 | const chunks: Array<string> = [] |
| 384 | |
| 385 | // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition |
| 386 | while (true) { |
| 387 | const { done, value } = await reader.read() |
| 388 | if (done) break |
| 389 | chunks.push(decoder.decode(value, { stream: true })) |
| 390 | } |
| 391 | |
| 392 | return chunks.join('') |
| 393 | } |
| 394 | } |
| 395 | |
| 396 | export async function consumeBinaryStream( |
| 397 | stream: ReadableStream<Uint8Array> | RawStream, |
no test coverage detected