|
1 |
| -import { Readable } from "node:stream"; |
2 |
| -import type { ReadableStream } from "node:stream/web"; |
| 1 | +import { ReadableStream } from "node:stream/web"; |
3 | 2 |
|
4 |
| -export function fromReadableStream( |
| 3 | +export async function fromReadableStream( |
5 | 4 | stream: ReadableStream<Uint8Array>,
|
6 | 5 | base64?: boolean,
|
7 | 6 | ): Promise<string> {
|
8 |
| - const reader = stream.getReader(); |
9 | 7 | const chunks: Uint8Array[] = [];
|
| 8 | + let totalLength = 0; |
10 | 9 |
|
11 |
| - return new Promise((resolve, reject) => { |
12 |
| - function pump() { |
13 |
| - reader |
14 |
| - .read() |
15 |
| - .then(({ done, value }) => { |
16 |
| - if (done) { |
17 |
| - resolve(Buffer.concat(chunks).toString(base64 ? "base64" : "utf8")); |
18 |
| - return; |
19 |
| - } |
20 |
| - chunks.push(value); |
21 |
| - pump(); |
22 |
| - }) |
23 |
| - .catch(reject); |
24 |
| - } |
25 |
| - pump(); |
26 |
| - }); |
| 10 | + for await (const chunk of stream) { |
| 11 | + chunks.push(chunk); |
| 12 | + totalLength += chunk.length; |
| 13 | + } |
| 14 | + |
| 15 | + if (chunks.length === 0) { |
| 16 | + return ""; |
| 17 | + } |
| 18 | + |
| 19 | + if (chunks.length === 1) { |
| 20 | + return Buffer.from(chunks[0]).toString(base64 ? "base64" : "utf8"); |
| 21 | + } |
| 22 | + |
| 23 | + // Pre-allocate buffer with exact size to avoid reallocation |
| 24 | + const buffer = Buffer.alloc(totalLength); |
| 25 | + let offset = 0; |
| 26 | + for (const chunk of chunks) { |
| 27 | + buffer.set(chunk, offset); |
| 28 | + offset += chunk.length; |
| 29 | + } |
| 30 | + |
| 31 | + return buffer.toString(base64 ? "base64" : "utf8"); |
27 | 32 | }
|
28 | 33 |
|
29 | 34 | export function toReadableStream(
|
30 | 35 | value: string,
|
31 | 36 | isBase64?: boolean,
|
32 | 37 | ): ReadableStream {
|
33 |
| - return Readable.toWeb( |
34 |
| - Readable.from(Buffer.from(value, isBase64 ? "base64" : "utf8")), |
| 38 | + return new ReadableStream( |
| 39 | + { |
| 40 | + pull(controller) { |
| 41 | + // Defer the Buffer.from conversion to when the stream is actually read. |
| 42 | + controller.enqueue(Buffer.from(value, isBase64 ? "base64" : "utf8")); |
| 43 | + controller.close(); |
| 44 | + }, |
| 45 | + }, |
| 46 | + { highWaterMark: 0 }, |
35 | 47 | );
|
36 | 48 | }
|
37 | 49 |
|
| 50 | +let maybeSomethingBuffer: Buffer | undefined; |
| 51 | + |
38 | 52 | export function emptyReadableStream(): ReadableStream {
|
39 | 53 | if (process.env.OPEN_NEXT_FORCE_NON_EMPTY_RESPONSE === "true") {
|
40 |
| - return Readable.toWeb(Readable.from([Buffer.from("SOMETHING")])); |
| 54 | + return new ReadableStream( |
| 55 | + { |
| 56 | + pull(controller) { |
| 57 | + maybeSomethingBuffer ??= Buffer.from("SOMETHING"); |
| 58 | + controller.enqueue(maybeSomethingBuffer); |
| 59 | + controller.close(); |
| 60 | + }, |
| 61 | + }, |
| 62 | + { highWaterMark: 0 }, |
| 63 | + ); |
41 | 64 | }
|
42 |
| - return Readable.toWeb(Readable.from([])); |
| 65 | + return new ReadableStream({ |
| 66 | + start(controller) { |
| 67 | + controller.close(); |
| 68 | + }, |
| 69 | + }); |
43 | 70 | }
|
0 commit comments