import { BrowserController , BrowserRequest , BrowserRequestHandler , BrowserRequestType , ResponseData , } from './BrowserController' ; import { getOrigin , tryJsonParse } from '../utils' ; import { parse } from 'url' ; export class PlaywrightController implements BrowserController { constructor ( private readonly page , private readonly onRequest ) { } public async startInterception ( ) { await this . page . route ( '**/*' , this . requestHandler ) ; } public async stopInterception ( ) { await this . page . unroute ( '**/*' , this . requestHandler ) ; } private requestHandler = ( route ) => { this . onRequest ( this . toBrowserRequest ( route . request ( ) ) , ( data ) => this . respond ( route , data ) , ( ) => this . skip ( route ) ) ; } ; private toBrowserRequest ( request ) { const { pathname , query , protocol , host } = parse ( request . url ( ) , true ) ; return { type : request . resourceType ( ) as BrowserRequestType , url : request . url ( ) , body : tryJsonParse ( request . postData ( ) ) , method : request . method ( ) , headers : ( request . headers ( ) as Record < string , string > ) || { } , path : pathname ?? '' , hostname : `${ protocol }//${ host }` , query : query , sourceOrigin : this . getRequestOrigin ( request ) , } ; } private async respond ( route , response ) { await route . fulfill ( { headers : response . headers || { } , status : response . status , body : response . body ? response . body : '' , contentType : response . headers ?. [ 'content-type' ] , } ) ; } private async skip ( route ) { await route . continue ( ) ; } /**
     * Obtain request origin url from originating frame url
     */ private getRequestOrigin ( request ) { return getOrigin ( request . frame ( ) . url ( ) ) ; } } /**
 * Mirror of playwright's Page interface
 */ export interface PlaywrightPage { route ( url , handler ) : Promise < void > ; unroute ( url , handler ) : Promise < void > ; } /**
 * Mirror of playwright's Route interface
 */ interface PlaywrightRoute { fulfill ( response ) : Promise < void > ; request ( ) : PlaywrightRequest ; continue ( ) : Promise < void > ; } /**
 * Mirror of playwright's Response interface
 */ interface PlaywrightRouteFulfillResponse { status ? : number ; headers ? : { [ key ] : string ; } ; contentType ? : string ; body ? : string | Buffer ; path ? : string ; } /**
 * Mirror of playwright's Request interface
 */ interface PlaywrightRequest { frame ( ) : { url ( ) : string ; } ; headers ( ) : { [ key ] : string ; } ; method ( ) : string ; postData ( ) : null | string ; resourceType ( ) : string ; url ( ) : string ; }