Skip to content

Commit 9c9bc1a

Browse files
authored
refactor(StreamCreator): allow opting-out of retaining the chunks (#994)
1 parent 6a96ac8 commit 9c9bc1a

File tree

5 files changed

+23
-7
lines changed

5 files changed

+23
-7
lines changed

.changeset/honest-lamps-smell.md

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
---
2-
"@opennextjs/aws": minor
2+
"@opennextjs/aws": patch
33
---
44

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

77
There is no need to store the chunks for streamed responses.
8-
Not storing the chunks allows saving memory.
9-
10-
BREAKING CHANGE: Note that `OpenNextHandler` will now return an empty body if your wrapper provides a `StreamCreator`
11-
This could break custom converters.
8+
Not storing the chunks allows saving memory.

.changeset/warm-rats-crash.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@opennextjs/aws": patch
3+
---
4+
5+
refactor(StreamCreator): allow opting-out of retaining the chunks

packages/open-next/src/http/openNextResponse.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -287,8 +287,8 @@ export class OpenNextNodeResponse extends Transform implements ServerResponse {
287287
private _internalWrite(chunk: any, encoding: BufferEncoding) {
288288
const buffer = Buffer.from(chunk, encoding);
289289
this.bodyLength += buffer.length;
290-
if (!this.streamCreator) {
291-
// Do not keep chunks around for streamed responses
290+
if (this.streamCreator?.retainChunks !== false) {
291+
// Avoid keeping chunks around when the `StreamCreator` supports it to save memory
292292
this._chunks.push(buffer);
293293
}
294294
this.push(chunk, encoding);

packages/open-next/src/overrides/wrappers/cloudflare-node.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ const handler: WrapperHandler<InternalEvent, InternalResult> =
7070
// Ensures that the response we pass to NextServer is aborted if the request is aborted
7171
// By doing this `request.signal.onabort` will work in route handlers
7272
abortSignal: abortSignal,
73+
// There is no need to retain the chunks that were pushed to the response stream.
74+
retainChunks: false,
7375
};
7476

7577
ctx.waitUntil(

packages/open-next/src/types/open-next.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,18 @@ export interface StreamCreator {
5656
onWrite?: () => void;
5757
onFinish?: (length: number) => void;
5858
abortSignal?: AbortSignal;
59+
/**
60+
* Normally there is no need to retain the chunks that have been pushed to the response stream.
61+
*
62+
* However some implementations use a fake `StreamCreator` and expect the chunks to be retained.
63+
* When your stream controller implementation doesn't need to retain the chunk, you can set this
64+
* to `false` to reduce memory usage.
65+
*
66+
* @see https://github.com/opennextjs/opennextjs-aws/blob/main/packages/open-next/src/overrides/wrappers/aws-lambda.ts
67+
*
68+
* @default true for backward compatibility.
69+
*/
70+
retainChunks?: boolean;
5971
}
6072

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

0 commit comments

Comments
 (0)