Skip to content

Commit e8b859b

Browse files
Joel Griffithelasticmachine
andauthored
[Reporting] Fix map tiles not loading by using Chrome's Remote Protocol (#55137)
* WIP Fixing map tiles and such * Small comment and importing map from dolash * Better destructuring and comments Co-authored-by: Elastic Machine <[email protected]>
1 parent 801302e commit e8b859b

File tree

2 files changed

+47
-9
lines changed

2 files changed

+47
-9
lines changed

x-pack/legacy/plugins/reporting/server/browsers/chromium/driver/chromium_driver.ts

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
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';
88
import open from 'opn';
99
import { parse as parseUrl } from 'url';
1010
import { 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
});

x-pack/legacy/plugins/reporting/types.d.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,4 +339,19 @@ export interface AbsoluteURLFactoryOptions {
339339
port: string | number;
340340
}
341341

342+
export interface InterceptedRequest {
343+
requestId: string;
344+
request: {
345+
url: string;
346+
method: string;
347+
headers: {
348+
[key: string]: string;
349+
};
350+
initialPriority: string;
351+
referrerPolicy: string;
352+
};
353+
frameId: string;
354+
resourceType: string;
355+
}
356+
342357
export { ServerFacade };

0 commit comments

Comments
 (0)