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/olive-cars-count.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@opennextjs/aws": patch
---

perf: avoid unnecessary buffer copy in internalWrite
9 changes: 7 additions & 2 deletions packages/open-next/src/http/openNextResponse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -285,13 +285,18 @@ export class OpenNextNodeResponse extends Transform implements ServerResponse {
}

private _internalWrite(chunk: any, encoding: BufferEncoding) {
const buffer = Buffer.from(chunk, encoding);
// When encoding === 'buffer', chunk is already a Buffer
// and does not need to be converted again.
// @ts-expect-error TS2367 'encoding' can be 'buffer', but it's not in the
// official type definition
const buffer = encoding === "buffer" ? chunk : Buffer.from(chunk, encoding);
this.bodyLength += buffer.length;
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);
// No need to pass the encoding for buffers
this.push(buffer);
this.streamCreator?.onWrite?.();
}

Expand Down
Loading