Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
resolve();
return;
}
const virtualConsole = new VirtualConsole();
virtualConsole.on('error', (err: Error) => {
debug(`Console: ${err}`);
});
virtualConsole.on('jsdomError', (err) => {
debug(`Console: ${err}`);
});
const initialDocument = createHTMLDocument(fetchEnd.response.body.content, this.finalHref);
this._resourceLoader = new CustomResourceLoader(this, initialDocument);
const jsdom = new JSDOM(this._targetNetworkData.response.body.content, {
beforeParse: beforeParse(this.finalHref),
// includeNodeLocations: true, // TODO: re-enable once locations can be copied from snapshot.
pretendToBeVisual: true,
resources: this._resourceLoader,
runScripts: 'dangerously',
url: this.finalHref,
virtualConsole
});
const window = jsdom.window;
this._window = window;
setTimeout(async () => {
const event: Event = { resource: this.finalHref };
debug(`${this.finalHref} loaded, traversing`);
try {
// TODO: Use a DOM snapshot and copy node locations insead of serializing.
const html = this._window.document.documentElement.outerHTML;
const htmlDocument = createHTMLDocument(html, this.finalHref, this._originalDocument);
this._document = htmlDocument;
const evaluateEvent = {
document: htmlDocument,
resource: this.finalHref
};
await this.server.emitAsync('can-evaluate::script', evaluateEvent);
await traverse(htmlDocument, this.server, this.finalHref);
// We download only the first favicon found
await this.getFavicon(window.document.querySelector('link[rel~="icon"]'));
/*
private async processTarget() {
await this.waitForTarget();
// QUESTION: Even if the content is blank we will receive a minimum HTML with this. Are we OK with the behavior?
const html = await this._page.content();
this._dom = createHTMLDocument(html, this._finalHref, this._originalDocument);
// Process pending requests now that the dom is ready
while (this._pendingRequests.length > 0) {
const pendingRequest = this._pendingRequests.shift()!;
await pendingRequest();
}
if (this._options.headless) {
// TODO: Check if browser downloads favicon even if there's no content
await getFavicon(this._dom, this.fetchContent.bind(this), this._engine);
}
if (this._targetBody) {
await traverse(this._dom, this._engine, this._page.url());
const event = (await onResponseHandler(response, this.fetchContent.bind(this), this._dom));
if (!event) {
this._pendingRequests.push(this.onResponse.bind(this, response));
return;
}
const { name, payload } = event;
if (isTarget) {
this._targetBody = payload.response.body.content;
this._targetNetworkData = payload;
if (name === 'fetch::end::html') {
this._originalDocument = createHTMLDocument(this._targetBody!, resource);
}
}
await this._engine.emitAsync(name, payload);
// The `fetch::end` of the target needs to be processed before notifying
/* istanbul ignore if */
if (isTarget && this._targetReady) {
this._targetReady();
}
}
private async onFetchEnd(fetchEnd: FetchEnd) {
const resource = fetchEnd.resource;
await this.engine.emitAsync(`parse::start::html`, { resource });
const html = fetchEnd.response.body.content;
const document = createHTMLDocument(html, fetchEnd.resource);
await this.engine.emitAsync('parse::end::html', { document, html, resource });
}
}