@@ -24,7 +24,8 @@ import {
2424 TimingEvents ,
2525 InitiatedRequest ,
2626 RawHeaders ,
27- Destination
27+ Destination ,
28+ InitiatedResponse
2829} from "../types" ;
2930
3031import {
@@ -354,7 +355,10 @@ export function trackResponse(
354355 response : http . ServerResponse ,
355356 timingEvents : TimingEvents ,
356357 tags : string [ ] ,
357- options : { maxSize : number }
358+ options : {
359+ maxSize : number ,
360+ onWriteHead : ( ) => void
361+ }
358362) : OngoingResponse {
359363 let trackedResponse = < OngoingResponse > response ;
360364
@@ -378,6 +382,8 @@ export function trackResponse(
378382 trackedResponse . writeHead = function ( this : typeof trackedResponse , ...args : any ) {
379383 if ( ! timingEvents . headersSentTimestamp ) {
380384 timingEvents . headersSentTimestamp = now ( ) ;
385+ // Notify listeners that the head is being written:
386+ options . onWriteHead ( ) ;
381387 }
382388
383389 // HTTP/2 responses shouldn't have a status message:
@@ -466,41 +472,47 @@ export function trackResponse(
466472}
467473
468474/**
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 .
471477 */
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 ( [
482480 'id' ,
483481 'statusCode' ,
484482 'timingEvents' ,
485483 'tags'
486484 ] ) . assign ( {
487485 statusMessage : '' ,
488-
489486 headers : response . getHeaders ( ) ,
490487 rawHeaders : response . getRawHeaders ( ) ,
491-
492- body : body ,
493-
494- rawTrailers : response . getRawTrailers ( ) ,
495- trailers : rawHeadersToObject ( response . getRawTrailers ( ) )
496488 } ) . valueOf ( ) ;
497489
498490 if ( ! ( response instanceof http2 . Http2ServerResponse ) ) {
499491 // H2 has no status messages, and generates a warning if you look for one
500- completedResponse . statusMessage = response . statusMessage ;
492+ initiatedResponse . statusMessage = response . statusMessage ;
501493 }
502494
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+ } ) ;
504516}
505517
506518// Take raw HTTP request bytes received, have a go at parsing something useful out of them.
0 commit comments