|
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 | 7 | const reader = stream.getReader();
|
9 | 8 | const chunks: Uint8Array[] = [];
|
| 9 | + let totalLength = 0; |
10 | 10 |
|
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); |
| 11 | + try { |
| 12 | + while (true) { |
| 13 | + const { done, value } = await reader.read(); |
| 14 | + if (done) break; |
| 15 | + chunks.push(value); |
| 16 | + totalLength += value.length; |
24 | 17 | }
|
25 |
| - pump(); |
26 |
| - }); |
| 18 | + |
| 19 | + if (chunks.length === 0) { |
| 20 | + return ""; |
| 21 | + } |
| 22 | + |
| 23 | + if (chunks.length === 1) { |
| 24 | + return Buffer.from(chunks[0]).toString(base64 ? "base64" : "utf8"); |
| 25 | + } |
| 26 | + |
| 27 | + // Pre-allocate buffer with exact size to avoid reallocation |
| 28 | + const buffer = Buffer.allocUnsafe(totalLength); |
| 29 | + let offset = 0; |
| 30 | + for (const chunk of chunks) { |
| 31 | + buffer.set(chunk, offset); |
| 32 | + offset += chunk.length; |
| 33 | + } |
| 34 | + |
| 35 | + return buffer.toString(base64 ? "base64" : "utf8"); |
| 36 | + } finally { |
| 37 | + reader.releaseLock(); |
| 38 | + } |
27 | 39 | }
|
28 | 40 |
|
29 | 41 | export function toReadableStream(
|
30 | 42 | value: string,
|
31 | 43 | isBase64?: boolean,
|
32 | 44 | ): ReadableStream {
|
33 |
| - return Readable.toWeb( |
34 |
| - Readable.from(Buffer.from(value, isBase64 ? "base64" : "utf8")), |
35 |
| - ); |
| 45 | + const buffer = Buffer.from(value, isBase64 ? "base64" : "utf8"); |
| 46 | + |
| 47 | + return new ReadableStream({ |
| 48 | + start(controller) { |
| 49 | + controller.enqueue(buffer); |
| 50 | + controller.close(); |
| 51 | + }, |
| 52 | + }); |
36 | 53 | }
|
37 | 54 |
|
| 55 | +let maybeSomethingBuffer: Buffer | undefined; |
| 56 | + |
38 | 57 | export function emptyReadableStream(): ReadableStream {
|
39 | 58 | if (process.env.OPEN_NEXT_FORCE_NON_EMPTY_RESPONSE === "true") {
|
40 |
| - return Readable.toWeb(Readable.from([Buffer.from("SOMETHING")])); |
| 59 | + return new ReadableStream({ |
| 60 | + start(controller) { |
| 61 | + maybeSomethingBuffer ??= Buffer.from("SOMETHING"); |
| 62 | + controller.enqueue(maybeSomethingBuffer); |
| 63 | + controller.close(); |
| 64 | + }, |
| 65 | + }); |
41 | 66 | }
|
42 |
| - return Readable.toWeb(Readable.from([])); |
| 67 | + return new ReadableStream({ |
| 68 | + start(controller) { |
| 69 | + controller.close(); |
| 70 | + }, |
| 71 | + }); |
43 | 72 | }
|
0 commit comments