Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
if (!dom && !isTarget) {
// DOM isn't loaded yet, no event to return
return null;
}
const payload = await createFetchEndPayload(response, fetchContent, dom);
/*
* If the target has a weird value like `application/x-httpd-php`
* (which translates into `unknown`) or is detected as `xml`.
* (e.g.: because it starts with
* `` even though it has
* `` declared after),
* we change the suffix to `html` so hints work properly.
*/
let suffix = getType(payload.response.mediaType);
const defaults = ['html', 'unknown', 'xml'];
if (isTarget && defaults.includes(suffix)) {
suffix = 'html';
}
const name = `fetch::end::${suffix}` as 'fetch::end::*';
return {
name,
payload
};
};
await this._connector.server.emitAsync('fetch::start', { resource: resourceUrl });
try {
const resourceNetworkData: NetworkData = await this._connector.fetchContent(resourceUrl);
debug(`resource ${resourceUrl} fetched`);
const fetchEndEvent: FetchEnd = {
element,
request: resourceNetworkData.request,
resource: resourceNetworkData.response.url,
response: resourceNetworkData.response
};
const { charset, mediaType } = getContentTypeData(element, fetchEndEvent.resource, fetchEndEvent.response.headers, fetchEndEvent.response.body.rawContent);
const type = mediaType ? getType(mediaType) : /* istanbul ignore next */ 'unknown';
fetchEndEvent.response.mediaType = mediaType!;
fetchEndEvent.response.charset = charset!;
/*
* TODO: Replace `null` with `resource` once it
* can be converted to `JSDOMAsyncHTMLElement`.
* Event is also emitted when status code in response is not 200.
*/
await this._connector.server.emitAsync(`fetch::end::${type}` as 'fetch::end::*', fetchEndEvent);
return resolve(resourceNetworkData.response.body.rawContent);
} catch (err) {
const hops: string[] = this._connector.request.getRedirects(err.uri);
const fetchError: FetchError = {
element: element!,
private async notifyFetch(event: FetchEnd) {
const type = getType(event.response.mediaType);
await this.engine.emitAsync(`fetch::end::${type}` as 'fetch::end::*', event);
}