Skip to content

Commit ca06ec1

Browse files
committed
address pr reviews
1 parent e140a73 commit ca06ec1

File tree

3 files changed

+39
-42
lines changed

3 files changed

+39
-42
lines changed

packages/open-next/src/core/routing/util.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,15 +105,14 @@ export function convertRes(res: OpenNextNodeResponse): InternalResult {
105105
const isBase64Encoded =
106106
isBinaryContentType(headers["content-type"]) ||
107107
!!headers["content-encoding"];
108-
let index = 0;
109108
const body = new ReadableStream({
110109
pull(controller) {
111-
if (!res._chunks || index >= res._chunks.length) {
110+
if (!res._chunks || res._chunks.length === 0) {
112111
controller.close();
113112
return;
114113
}
115114

116-
controller.enqueue(res._chunks[index++]);
115+
controller.enqueue(res._chunks.shift());
117116
},
118117
});
119118
return {

packages/open-next/src/utils/binary.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,6 @@ const commonBinaryMimeTypes = new Set([
6262
export function isBinaryContentType(contentType?: string | null) {
6363
if (!contentType) return false;
6464

65-
const value = contentType.split(";").at(0);
65+
const value = contentType.split(";")[0];
6666
return commonBinaryMimeTypes.has(value);
6767
}

packages/open-next/src/utils/stream.ts

Lines changed: 36 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -4,65 +4,63 @@ export async function fromReadableStream(
44
stream: ReadableStream<Uint8Array>,
55
base64?: boolean,
66
): Promise<string> {
7-
const reader = stream.getReader();
87
const chunks: Uint8Array[] = [];
98
let totalLength = 0;
109

11-
try {
12-
while (true) {
13-
const { done, value } = await reader.read();
14-
if (done) break;
15-
chunks.push(value);
16-
totalLength += value.length;
17-
}
18-
19-
if (chunks.length === 0) {
20-
return "";
21-
}
10+
for await (const chunk of stream) {
11+
chunks.push(chunk);
12+
totalLength += chunk.length;
13+
}
2214

23-
if (chunks.length === 1) {
24-
return Buffer.from(chunks[0]).toString(base64 ? "base64" : "utf8");
25-
}
15+
if (chunks.length === 0) {
16+
return "";
17+
}
2618

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-
}
19+
if (chunks.length === 1) {
20+
return Buffer.from(chunks[0]).toString(base64 ? "base64" : "utf8");
21+
}
3422

35-
return buffer.toString(base64 ? "base64" : "utf8");
36-
} finally {
37-
reader.releaseLock();
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;
3829
}
30+
31+
return buffer.toString(base64 ? "base64" : "utf8");
3932
}
4033

4134
export function toReadableStream(
4235
value: string,
4336
isBase64?: boolean,
4437
): ReadableStream {
45-
const buffer = Buffer.from(value, isBase64 ? "base64" : "utf8");
46-
47-
return new ReadableStream({
48-
start(controller) {
49-
controller.enqueue(buffer);
50-
controller.close();
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+
},
5145
},
52-
});
46+
{ highWaterMark: 0 },
47+
);
5348
}
5449

5550
let maybeSomethingBuffer: Buffer | undefined;
5651

5752
export function emptyReadableStream(): ReadableStream {
5853
if (process.env.OPEN_NEXT_FORCE_NON_EMPTY_RESPONSE === "true") {
59-
return new ReadableStream({
60-
start(controller) {
61-
maybeSomethingBuffer ??= Buffer.from("SOMETHING");
62-
controller.enqueue(maybeSomethingBuffer);
63-
controller.close();
54+
return new ReadableStream(
55+
{
56+
pull(controller) {
57+
maybeSomethingBuffer ??= Buffer.from("SOMETHING");
58+
controller.enqueue(maybeSomethingBuffer);
59+
controller.close();
60+
},
6461
},
65-
});
62+
{ highWaterMark: 0 },
63+
);
6664
}
6765
return new ReadableStream({
6866
start(controller) {

0 commit comments

Comments
 (0)