@@ -20,11 +20,13 @@ export class OpenNextNodeResponse extends Transform implements ServerResponse {
20
20
statusCode ! : number ;
21
21
statusMessage = "" ;
22
22
headers : OutgoingHttpHeaders = { } ;
23
- private _cookies : string [ ] = [ ] ;
24
- private responseStream ?: Writable ;
25
23
headersSent = false ;
26
24
_chunks : Buffer [ ] = [ ] ;
27
25
26
+ private _cookies : string [ ] = [ ] ;
27
+ private responseStream ?: Writable ;
28
+ private bodyLength = 0 ;
29
+
28
30
// To comply with the ServerResponse interface :
29
31
strictContentLength = false ;
30
32
assignSocket ( _socket : Socket ) : void {
@@ -282,18 +284,12 @@ export class OpenNextNodeResponse extends Transform implements ServerResponse {
282
284
return Buffer . concat ( this . _chunks ) ;
283
285
}
284
286
285
- getBodyLength ( ) : number {
286
- let size = 0 ;
287
- for ( const chunk of this . _chunks ) {
288
- size += chunk . length ;
289
- }
290
- return size ;
291
- }
292
-
293
287
private _internalWrite ( chunk : any , encoding : BufferEncoding ) {
288
+ const buffer = Buffer . from ( chunk , encoding ) ;
289
+ this . bodyLength += buffer . length ;
294
290
if ( ! this . streamCreator ) {
295
291
// Do not keep chunks around for streamed responses
296
- this . _chunks . push ( Buffer . from ( chunk , encoding ) ) ;
292
+ this . _chunks . push ( buffer ) ;
297
293
}
298
294
this . push ( chunk , encoding ) ;
299
295
this . streamCreator ?. onWrite ?.( ) ;
@@ -321,8 +317,7 @@ export class OpenNextNodeResponse extends Transform implements ServerResponse {
321
317
globalThis . __openNextAls
322
318
?. getStore ( )
323
319
?. pendingPromiseRunner . add ( this . onEnd ( this . headers ) ) ;
324
- const bodyLength = this . getBodyLength ( ) ;
325
- this . streamCreator ?. onFinish ?.( bodyLength ) ;
320
+ this . streamCreator ?. onFinish ?.( this . bodyLength ) ;
326
321
327
322
//This is only here because of aws broken streaming implementation.
328
323
//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 {
331
326
//BE CAREFUL: Aws keeps rolling out broken streaming implementations even on accounts that had working ones before
332
327
//This is not dependent on the node runtime used
333
328
if (
334
- bodyLength === 0 &&
329
+ this . bodyLength === 0 &&
335
330
// We use an env variable here because not all aws account have the same behavior
336
331
// On some aws accounts the response will hang if the body is empty
337
332
// We are modifying the response body here, this is not a good practice
0 commit comments