Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
return async () => {
try {
if (this._errorWithPage) {
return callback(new Error(`Problem loading the website ${this._href}`));
}
// Sometimes we receive the `onLoadEvent` before the response of the target. See: https://github.com/webhintio/hint/issues/1158
await this._waitForTarget;
await delay(this._options.waitFor);
const { DOM } = this._client;
const event: Event = { resource: this._finalHref };
this._dom = new CDPAsyncHTMLDocument(DOM);
await this._dom.load();
await this.processPendingResponses();
/*
* If the target is not an HTML we don't need to
* traverse it.
*/
if (!isHTMLDocument(this._finalHref, this.headers)) {
await this._server.emitAsync('scan::end', event);
while (!finish) {
try {
/*
* We test if the process is still running or is a leftover:
* https://nodejs.org/api/process.html#process_process_kill_pid_signal
*/
process.kill(this._pid, 0);
maxTries--;
// Wait for 10 seconds to close the browser or continue.
if (maxTries === 0) {
finish = true;
} else {
await delay(50);
}
} catch (e) {
debug(`Process with ${this._pid} doesn't seem to be running`);
finish = true;
}
}
resolve();
});
}
private async createResponse(cdpResponse: Crdp.Network.ResponseReceivedEvent, element: IAsyncHTMLElement): Promise {
const resourceUrl: string = cdpResponse.response.url;
const hops: Array = this._redirects.calculate(resourceUrl);
const resourceHeaders: object = normalizeHeaders(cdpResponse.response.headers);
let { content, rawContent, rawResponse } = await this.getResponseBody(cdpResponse);
let retry = 3;
/*
* Sometimes, the content is empty at the beginning, but
* after few millisecons, it isn't.
*/
while (!content && (!rawContent || rawContent.length === 0) && retry > 0) {
await delay(250);
({ content, rawContent, rawResponse } = await this.getResponseBody(cdpResponse));
retry--;
}
if (retry === 0) {
debug(`${resourceUrl} is empty`);
}
const response: Response = {
body: {
content,
rawContent,
rawResponse
},
const loadCDP = async () => {
try {
const client = await cdp({ port, tab });
return client;
} catch (err) {
if (retries > 3) {
throw err;
}
await delay((retries * 250) + 500);
retries++;
return loadCDP();
}
};