Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/shaggy-walls-sniff.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@opennextjs/aws": patch
---

Reduces allocations and copies of streams
3 changes: 1 addition & 2 deletions packages/open-next/src/core/routing/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,13 +217,12 @@ export function convertBodyToReadableStream(
) {
if (method === "GET" || method === "HEAD") return undefined;
if (!body) return undefined;
const readable = new ReadableStream({
return new ReadableStream({
start(controller) {
controller.enqueue(body);
controller.close();
},
});
return readable;
}

enum CommonHeaders {
Expand Down
2 changes: 1 addition & 1 deletion packages/open-next/src/utils/binary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,6 @@ const commonBinaryMimeTypes = new Set([
export function isBinaryContentType(contentType?: string | null) {
if (!contentType) return false;

const value = contentType?.split(";")[0] ?? "";
const value = contentType.split(";")[0];
return commonBinaryMimeTypes.has(value);
}
3 changes: 1 addition & 2 deletions packages/open-next/src/utils/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ export function customFetchClient(client: AwsClient) {
* This is necessary otherwise we get some error : SocketError: other side closed
* https://github.com/nodejs/undici/issues/583#issuecomment-855384858
*/
const clonedResponse = response.clone();
return clonedResponse;
return response.clone();
};
}
75 changes: 51 additions & 24 deletions packages/open-next/src/utils/stream.ts
Original file line number Diff line number Diff line change
@@ -1,43 +1,70 @@
import { Readable } from "node:stream";
import type { ReadableStream } from "node:stream/web";
import { ReadableStream } from "node:stream/web";

export function fromReadableStream(
export async function fromReadableStream(
stream: ReadableStream<Uint8Array>,
base64?: boolean,
): Promise<string> {
const reader = stream.getReader();
const chunks: Uint8Array[] = [];
let totalLength = 0;

return new Promise((resolve, reject) => {
function pump() {
reader
.read()
.then(({ done, value }) => {
if (done) {
resolve(Buffer.concat(chunks).toString(base64 ? "base64" : "utf8"));
return;
}
chunks.push(value);
pump();
})
.catch(reject);
}
pump();
});
for await (const chunk of stream) {
chunks.push(chunk);
totalLength += chunk.length;
}

if (chunks.length === 0) {
return "";
}

if (chunks.length === 1) {
return Buffer.from(chunks[0]).toString(base64 ? "base64" : "utf8");
}

// Pre-allocate buffer with exact size to avoid reallocation
const buffer = Buffer.alloc(totalLength);
let offset = 0;
for (const chunk of chunks) {
buffer.set(chunk, offset);
offset += chunk.length;
}

return buffer.toString(base64 ? "base64" : "utf8");
}

export function toReadableStream(
value: string,
isBase64?: boolean,
): ReadableStream {
return Readable.toWeb(
Readable.from(Buffer.from(value, isBase64 ? "base64" : "utf8")),
return new ReadableStream(
{
pull(controller) {
// Defer the Buffer.from conversion to when the stream is actually read.
controller.enqueue(Buffer.from(value, isBase64 ? "base64" : "utf8"));
controller.close();
},
},
{ highWaterMark: 0 },
);
}

let maybeSomethingBuffer: Buffer | undefined;

export function emptyReadableStream(): ReadableStream {
if (process.env.OPEN_NEXT_FORCE_NON_EMPTY_RESPONSE === "true") {
return Readable.toWeb(Readable.from([Buffer.from("SOMETHING")]));
return new ReadableStream(
{
pull(controller) {
maybeSomethingBuffer ??= Buffer.from("SOMETHING");
controller.enqueue(maybeSomethingBuffer);
controller.close();
},
},
{ highWaterMark: 0 },
);
}
return Readable.toWeb(Readable.from([]));
return new ReadableStream({
start(controller) {
controller.close();
},
});
}
Loading