@@ -24,7 +24,8 @@ import {
24
24
TimingEvents ,
25
25
InitiatedRequest ,
26
26
RawHeaders ,
27
- Destination
27
+ Destination ,
28
+ InitiatedResponse
28
29
} from "../types" ;
29
30
30
31
import {
@@ -354,7 +355,10 @@ export function trackResponse(
354
355
response : http . ServerResponse ,
355
356
timingEvents : TimingEvents ,
356
357
tags : string [ ] ,
357
- options : { maxSize : number }
358
+ options : {
359
+ maxSize : number ,
360
+ onWriteHead : ( ) => void
361
+ }
358
362
) : OngoingResponse {
359
363
let trackedResponse = < OngoingResponse > response ;
360
364
@@ -378,6 +382,8 @@ export function trackResponse(
378
382
trackedResponse . writeHead = function ( this : typeof trackedResponse , ...args : any ) {
379
383
if ( ! timingEvents . headersSentTimestamp ) {
380
384
timingEvents . headersSentTimestamp = now ( ) ;
385
+ // Notify listeners that the head is being written:
386
+ options . onWriteHead ( ) ;
381
387
}
382
388
383
389
// HTTP/2 responses shouldn't have a status message:
@@ -466,41 +472,47 @@ export function trackResponse(
466
472
}
467
473
468
474
/**
469
- * Build a completed response: the external representation of a response
470
- * that's been completely written out and sent back to the client .
475
+ * Build an initiated response: the external representation of a response
476
+ * that's just started .
471
477
*/
472
- export async function waitForCompletedResponse (
473
- response : OngoingResponse | CompletedResponse
474
- ) : Promise < CompletedResponse > {
475
- // Ongoing response has 'getHeaders' - completed has 'headers'.
476
- if ( 'headers' in response ) return response ;
477
-
478
- const body = await waitForBody ( response . body , response . getHeaders ( ) ) ;
479
- response . timingEvents . responseSentTimestamp = response . timingEvents . responseSentTimestamp || now ( ) ;
480
-
481
- const completedResponse : CompletedResponse = _ ( response ) . pick ( [
478
+ export function buildInitiatedResponse ( response : OngoingResponse ) : InitiatedResponse {
479
+ const initiatedResponse : InitiatedResponse = _ ( response ) . pick ( [
482
480
'id' ,
483
481
'statusCode' ,
484
482
'timingEvents' ,
485
483
'tags'
486
484
] ) . assign ( {
487
485
statusMessage : '' ,
488
-
489
486
headers : response . getHeaders ( ) ,
490
487
rawHeaders : response . getRawHeaders ( ) ,
491
-
492
- body : body ,
493
-
494
- rawTrailers : response . getRawTrailers ( ) ,
495
- trailers : rawHeadersToObject ( response . getRawTrailers ( ) )
496
488
} ) . valueOf ( ) ;
497
489
498
490
if ( ! ( response instanceof http2 . Http2ServerResponse ) ) {
499
491
// H2 has no status messages, and generates a warning if you look for one
500
- completedResponse . statusMessage = response . statusMessage ;
492
+ initiatedResponse . statusMessage = response . statusMessage ;
501
493
}
502
494
503
- return completedResponse ;
495
+ return initiatedResponse ;
496
+ }
497
+
498
+ /**
499
+ * Build a completed response: the external representation of a response
500
+ * that's been completely written out and sent back to the client.
501
+ */
502
+ export async function waitForCompletedResponse (
503
+ response : OngoingResponse | CompletedResponse
504
+ ) : Promise < CompletedResponse > {
505
+ // Ongoing response has 'getHeaders' - completed has 'headers'.
506
+ if ( 'headers' in response ) return response ;
507
+
508
+ const body = await waitForBody ( response . body , response . getHeaders ( ) ) ;
509
+ response . timingEvents . responseSentTimestamp = response . timingEvents . responseSentTimestamp || now ( ) ;
510
+
511
+ return Object . assign ( buildInitiatedResponse ( response ) , {
512
+ body : body ,
513
+ rawTrailers : response . getRawTrailers ( ) ,
514
+ trailers : rawHeadersToObject ( response . getRawTrailers ( ) )
515
+ } ) ;
504
516
}
505
517
506
518
// Take raw HTTP request bytes received, have a go at parsing something useful out of them.
0 commit comments