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
7 changes: 2 additions & 5 deletions .changeset/honest-lamps-smell.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
---
"@opennextjs/aws": minor
"@opennextjs/aws": patch
---

perf(OpenNextResponse): do not store the chunks for streamed responses

There is no need to store the chunks for streamed responses.
Not storing the chunks allows saving memory.

BREAKING CHANGE: Note that `OpenNextHandler` will now return an empty body if your wrapper provides a `StreamCreator`
This could break custom converters.
Not storing the chunks allows saving memory.
5 changes: 5 additions & 0 deletions .changeset/warm-rats-crash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@opennextjs/aws": patch
---

refactor(StreamCreator): allow opting-out of retaining the chunks
4 changes: 2 additions & 2 deletions packages/open-next/src/http/openNextResponse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -287,8 +287,8 @@ export class OpenNextNodeResponse extends Transform implements ServerResponse {
private _internalWrite(chunk: any, encoding: BufferEncoding) {
const buffer = Buffer.from(chunk, encoding);
this.bodyLength += buffer.length;
if (!this.streamCreator) {
// Do not keep chunks around for streamed responses
if (this.streamCreator?.retainChunks !== false) {
// Avoid keeping chunks around when the `StreamCreator` supports it to save memory
this._chunks.push(buffer);
}
this.push(chunk, encoding);
Expand Down
2 changes: 2 additions & 0 deletions packages/open-next/src/overrides/wrappers/cloudflare-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ const handler: WrapperHandler<InternalEvent, InternalResult> =
// Ensures that the response we pass to NextServer is aborted if the request is aborted
// By doing this `request.signal.onabort` will work in route handlers
abortSignal: abortSignal,
// There is no need to retain the chunks that were pushed to the response stream.
retainChunks: false,
};

ctx.waitUntil(
Expand Down
12 changes: 12 additions & 0 deletions packages/open-next/src/types/open-next.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,18 @@ export interface StreamCreator {
onWrite?: () => void;
onFinish?: (length: number) => void;
abortSignal?: AbortSignal;
/**
* Normally there is no need to retain the chunks that have been pushed to the response stream.
*
* However some implementations use a fake `StreamCreator` and expect the chunks to be retained.
* When your stream controller implementation doesn't need to retain the chunk, you can set this
* to `false` to reduce memory usage.
*
* @see https://github.com/opennextjs/opennextjs-aws/blob/main/packages/open-next/src/overrides/wrappers/aws-lambda.ts
*
* @default true for backward compatibility.
*/
retainChunks?: boolean;
}

export type WaitUntil = (promise: Promise<void>) => void;
Expand Down