import { NetworkRequest } from './NetworkRequest' ; import { Header } from 'har-format' ; export interface RequestExtraInfo { requestHeaders : Header [ ] ; } export interface ResponseExtraInfo { responseHeaders : Header [ ] ; responseHeadersText ? : string ; } export class ExtraInfoBuilder { private _hasExtraInfo : boolean = false ; private _finished : boolean = false ; private readonly _requests : NetworkRequest [ ] = [ ] ; private readonly _requestExtraInfo : RequestExtraInfo [ ] = [ ] ; private readonly _responseExtraInfo : ResponseExtraInfo [ ] = [ ] ; constructor ( private readonly deleteCallback ) { } public addRequest ( request ) { this . _requests . push ( request ) ; } public addRequestExtraInfo ( info ) { this . _requestExtraInfo . push ( info ) ; this . _hasExtraInfo = true ; this . sync ( ) ; } public addResponseExtraInfo ( info ) { this . _responseExtraInfo . push ( info ) ; this . sync ( ) ; } public finished ( ) { this . _finished = true ; this . deleteIfComplete ( ) ; } private deleteIfComplete ( ) { if ( ! this . _finished ) { return ; } if ( this . _hasExtraInfo ) { const lastRequest = this . getLastRequest ( ) ; if ( ! lastRequest ?. hasExtraResponseInfo ) { return ; } } this . deleteCallback ( ) ; } private getLastRequest ( ) { return this . _requests [ this . _requests . length - 1 ] ; } private getRequestIndex ( req ) { return this . _requests . indexOf ( req ) ; } private sync ( ) { const req = this . getLastRequest ( ) ; if ( ! req ) { return ; } const index = this . getRequestIndex ( req ) ; const requestExtraInfo = this . _requestExtraInfo [ index ] ; if ( requestExtraInfo ) { req . addExtraRequestInfo ( requestExtraInfo ) ; delete this . _requestExtraInfo [ index ] ; } const responseExtraInfo = this . _responseExtraInfo [ index ] ; if ( responseExtraInfo ) { req . addExtraResponseInfo ( responseExtraInfo ) ; delete this . _responseExtraInfo [ index ] ; } this . deleteIfComplete ( ) ; } }