Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
10 changes: 10 additions & 0 deletions .changeset/honest-lamps-smell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
"@opennextjs/aws": minor
---

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.

Note that `OpenNextHandler` will now return an empty body for streamed responses.
28 changes: 13 additions & 15 deletions packages/open-next/src/http/openNextResponse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@ export class OpenNextNodeResponse extends Transform implements ServerResponse {
statusCode!: number;
statusMessage = "";
headers: OutgoingHttpHeaders = {};
private _cookies: string[] = [];
private responseStream?: Writable;
headersSent = false;
_chunks: Buffer[] = [];

private _cookies: string[] = [];
private responseStream?: Writable;
private bodyLength = 0;

// To comply with the ServerResponse interface :
strictContentLength = false;
assignSocket(_socket: Socket): void {
Expand Down Expand Up @@ -282,16 +284,13 @@ export class OpenNextNodeResponse extends Transform implements ServerResponse {
return Buffer.concat(this._chunks);
}

getBodyLength(): number {
let size = 0;
for (const chunk of this._chunks) {
size += chunk.length;
}
return size;
}

private _internalWrite(chunk: any, encoding: BufferEncoding) {
this._chunks.push(Buffer.from(chunk, encoding));
const buffer = Buffer.from(chunk, encoding);
this.bodyLength += buffer.length;
if (!this.streamCreator) {
// Do not keep chunks around for streamed responses
this._chunks.push(buffer);
}
this.push(chunk, encoding);
this.streamCreator?.onWrite?.();
}
Expand All @@ -314,12 +313,11 @@ export class OpenNextNodeResponse extends Transform implements ServerResponse {
this.flushHeaders();
}
// In some cases we might not have a store i.e. for example in the image optimization function
// We may want to reconsider this in the future, it might be intersting to have access to this store everywhere
// We may want to reconsider this in the future, it might be interesting to have access to this store everywhere
globalThis.__openNextAls
?.getStore()
?.pendingPromiseRunner.add(this.onEnd(this.headers));
const bodyLength = this.getBodyLength();
this.streamCreator?.onFinish?.(bodyLength);
this.streamCreator?.onFinish?.(this.bodyLength);

//This is only here because of aws broken streaming implementation.
//Hopefully one day they will be able to give us a working streaming implementation in lambda for everyone
Expand All @@ -328,7 +326,7 @@ export class OpenNextNodeResponse extends Transform implements ServerResponse {
//BE CAREFUL: Aws keeps rolling out broken streaming implementations even on accounts that had working ones before
//This is not dependent on the node runtime used
if (
bodyLength === 0 &&
this.bodyLength === 0 &&
// We use an env variable here because not all aws account have the same behavior
// On some aws accounts the response will hang if the body is empty
// We are modifying the response body here, this is not a good practice
Expand Down