@@ -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,16 +284,13 @@ 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 ) {
294
- this . _chunks . push ( Buffer . from ( chunk , encoding ) ) ;
288
+ const buffer = Buffer . from ( chunk , encoding ) ;
289
+ this . bodyLength += buffer . length ;
290
+ if ( ! this . streamCreator ) {
291
+ // Do not keep chunks around for streamed responses
292
+ this . _chunks . push ( buffer ) ;
293
+ }
295
294
this . push ( chunk , encoding ) ;
296
295
this . streamCreator ?. onWrite ?.( ) ;
297
296
}
@@ -314,12 +313,11 @@ export class OpenNextNodeResponse extends Transform implements ServerResponse {
314
313
this . flushHeaders ( ) ;
315
314
}
316
315
// In some cases we might not have a store i.e. for example in the image optimization function
317
- // We may want to reconsider this in the future, it might be intersting to have access to this store everywhere
316
+ // We may want to reconsider this in the future, it might be interesting to have access to this store everywhere
318
317
globalThis . __openNextAls
319
318
?. getStore ( )
320
319
?. pendingPromiseRunner . add ( this . onEnd ( this . headers ) ) ;
321
- const bodyLength = this . getBodyLength ( ) ;
322
- this . streamCreator ?. onFinish ?.( bodyLength ) ;
320
+ this . streamCreator ?. onFinish ?.( this . bodyLength ) ;
323
321
324
322
//This is only here because of aws broken streaming implementation.
325
323
//Hopefully one day they will be able to give us a working streaming implementation in lambda for everyone
@@ -328,7 +326,7 @@ export class OpenNextNodeResponse extends Transform implements ServerResponse {
328
326
//BE CAREFUL: Aws keeps rolling out broken streaming implementations even on accounts that had working ones before
329
327
//This is not dependent on the node runtime used
330
328
if (
331
- bodyLength === 0 &&
329
+ this . bodyLength === 0 &&
332
330
// We use an env variable here because not all aws account have the same behavior
333
331
// On some aws accounts the response will hang if the body is empty
334
332
// We are modifying the response body here, this is not a good practice
0 commit comments