Skip to content

Commit dcfdb88

Browse files
committed
fixup! length
1 parent b8eb6ac commit dcfdb88

File tree

1 file changed

+9
-14
lines changed

1 file changed

+9
-14
lines changed

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

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,13 @@ export class OpenNextNodeResponse extends Transform implements ServerResponse {
2020
statusCode!: number;
2121
statusMessage = "";
2222
headers: OutgoingHttpHeaders = {};
23-
private _cookies: string[] = [];
24-
private responseStream?: Writable;
2523
headersSent = false;
2624
_chunks: Buffer[] = [];
2725

26+
private _cookies: string[] = [];
27+
private responseStream?: Writable;
28+
private bodyLength = 0;
29+
2830
// To comply with the ServerResponse interface :
2931
strictContentLength = false;
3032
assignSocket(_socket: Socket): void {
@@ -282,18 +284,12 @@ export class OpenNextNodeResponse extends Transform implements ServerResponse {
282284
return Buffer.concat(this._chunks);
283285
}
284286

285-
getBodyLength(): number {
286-
let size = 0;
287-
for (const chunk of this._chunks) {
288-
size += chunk.length;
289-
}
290-
return size;
291-
}
292-
293287
private _internalWrite(chunk: any, encoding: BufferEncoding) {
288+
const buffer = Buffer.from(chunk, encoding);
289+
this.bodyLength += buffer.length;
294290
if (!this.streamCreator) {
295291
// Do not keep chunks around for streamed responses
296-
this._chunks.push(Buffer.from(chunk, encoding));
292+
this._chunks.push(buffer);
297293
}
298294
this.push(chunk, encoding);
299295
this.streamCreator?.onWrite?.();
@@ -321,8 +317,7 @@ export class OpenNextNodeResponse extends Transform implements ServerResponse {
321317
globalThis.__openNextAls
322318
?.getStore()
323319
?.pendingPromiseRunner.add(this.onEnd(this.headers));
324-
const bodyLength = this.getBodyLength();
325-
this.streamCreator?.onFinish?.(bodyLength);
320+
this.streamCreator?.onFinish?.(this.bodyLength);
326321

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

0 commit comments

Comments
 (0)