44 * you may not use this file except in compliance with the Elastic License.
55 */
66
7- import { trunc } from 'lodash' ;
7+ import { trunc , map } from 'lodash' ;
88import open from 'opn' ;
99import { parse as parseUrl } from 'url' ;
1010import { Page , SerializableOrJSHandle , EvaluateFn } from 'puppeteer' ;
@@ -15,6 +15,7 @@ import {
1515 ConditionalHeaders ,
1616 ConditionalHeadersConditions ,
1717 ElementPosition ,
18+ InterceptedRequest ,
1819 NetworkPolicy ,
1920} from '../../../../types' ;
2021
@@ -59,35 +60,57 @@ export class HeadlessChromiumDriver {
5960 } : { conditionalHeaders : ConditionalHeaders ; waitForSelector : string } ,
6061 logger : LevelLogger
6162 ) {
62- await this . page . setRequestInterception ( true ) ;
6363 logger . info ( `opening url ${ url } ` ) ;
64+ // @ts -ignore
65+ const client = this . page . _client ;
6466 let interceptedCount = 0 ;
6567
66- this . page . on ( 'request' , interceptedRequest => {
67- const interceptedUrl = interceptedRequest . url ( ) ;
68+ await this . page . setRequestInterception ( true ) ;
69+
70+ // We have to reach into the Chrome Devtools Protocol to apply headers as using
71+ // puppeteer's API will cause map tile requests to hang indefinitely:
72+ // https://github.com/puppeteer/puppeteer/issues/5003
73+ // Docs on this client/protocol can be found here:
74+ // https://chromedevtools.github.io/devtools-protocol/tot/Fetch
75+ client . on ( 'Fetch.requestPaused' , ( interceptedRequest : InterceptedRequest ) => {
76+ const {
77+ requestId,
78+ request : { url : interceptedUrl } ,
79+ } = interceptedRequest ;
6880 const allowed = ! interceptedUrl . startsWith ( 'file://' ) ;
6981 const isData = interceptedUrl . startsWith ( 'data:' ) ;
7082
7183 // We should never ever let file protocol requests go through
7284 if ( ! allowed || ! this . allowRequest ( interceptedUrl ) ) {
7385 logger . error ( `Got bad URL: "${ interceptedUrl } ", closing browser.` ) ;
74- interceptedRequest . abort ( 'blockedbyclient' ) ;
86+ client . send ( 'Fetch.failRequest' , {
87+ errorReason : 'Aborted' ,
88+ requestId,
89+ } ) ;
7590 this . page . browser ( ) . close ( ) ;
7691 throw new Error ( `Received disallowed outgoing URL: "${ interceptedUrl } ", exiting` ) ;
7792 }
7893
7994 if ( this . _shouldUseCustomHeaders ( conditionalHeaders . conditions , interceptedUrl ) ) {
8095 logger . debug ( `Using custom headers for ${ interceptedUrl } ` ) ;
81- interceptedRequest . continue ( {
82- headers : {
83- ...interceptedRequest . headers ( ) ,
96+ const headers = map (
97+ {
98+ ...interceptedRequest . request . headers ,
8499 ...conditionalHeaders . headers ,
85100 } ,
101+ ( value , name ) => ( {
102+ name,
103+ value,
104+ } )
105+ ) ;
106+ client . send ( 'Fetch.continueRequest' , {
107+ requestId,
108+ headers,
86109 } ) ;
87110 } else {
88111 const loggedUrl = isData ? this . truncateUrl ( interceptedUrl ) : interceptedUrl ;
89112 logger . debug ( `No custom headers for ${ loggedUrl } ` ) ;
90- interceptedRequest . continue ( ) ;
113+ client . send ( 'Fetch.continueRequest' , { requestId } ) ;
91114 }
92115 interceptedCount = interceptedCount + ( isData ? 0 : 1 ) ;
93116 } ) ;
0 commit comments