From c9c64d3d794a4331ccded7fb02c8288d9129c7ed Mon Sep 17 00:00:00 2001 From: Robert DeLuca Date: Thu, 16 Jan 2020 11:56:40 -0600 Subject: [PATCH 1/2] fix: Skip requests that will never be saved We shouldn't bother waiting for a request to finish during asset discovery if we're not going to save it --- src/services/asset-discovery-service.ts | 28 +++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/services/asset-discovery-service.ts b/src/services/asset-discovery-service.ts index e6234006..2505b55b 100644 --- a/src/services/asset-discovery-service.ts +++ b/src/services/asset-discovery-service.ts @@ -4,6 +4,7 @@ import * as puppeteer from 'puppeteer' import { AssetDiscoveryConfiguration } from '../configuration/asset-discovery-configuration' import { DEFAULT_CONFIGURATION } from '../configuration/configuration' import { SnapshotOptions } from '../percy-agent-client/snapshot-options' +import domainMatch from '../utils/domain-match' import { addLogDate, logError, profile } from '../utils/logger' import { cacheResponse, getResponseCache } from '../utils/response-cache' import waitForNetworkIdle from '../utils/wait-for-network-idle' @@ -168,6 +169,25 @@ export class AssetDiscoveryService extends PercyClientService { await this.closeBrowser() } + // We shouldn't bother passing on requests that will never be saved + shouldProcessRequest(resourceUrl: string, rootResourceUrl: string): boolean { + const parsedRootResourceUrl = new URL(rootResourceUrl) + const rootUrl = `${parsedRootResourceUrl.protocol}//${parsedRootResourceUrl.host}` + + // Process if the resourceUrl has a hostname in the allowedHostnames + if (this.configuration['allowed-hostnames'].some((hostname) => domainMatch(hostname, resourceUrl))) { + return true + } + + // Capture if the resourceUrl is the same as the rootUrL + if (resourceUrl.startsWith(rootUrl)) { + return true + } + + // We won't be capturing this asset, no need to wait for it to respond + return false + } + private async resourcesForWidth( pool: pool.Pool, width: number, @@ -207,6 +227,12 @@ export class AssetDiscoveryService extends PercyClientService { return } + if (!this.shouldProcessRequest(requestUrl, rootResourceUrl)) { + logger.debug(addLogDate(`Aborting ${requestUrl} -- will never be saved`)) + await request.abort() + return + } + if (this.configuration['cache-responses'] === true && getResponseCache(requestUrl)) { logger.debug(addLogDate(`Asset cache hit for ${requestUrl}`)) await request.respond(getResponseCache(requestUrl)) @@ -214,6 +240,8 @@ export class AssetDiscoveryService extends PercyClientService { return } + logger.debug(addLogDate(`Starting processing for: ${requestUrl}`)) + await request.continue() } catch (error) { logError(error) From 710f1ac8b61d1595fda7f10886e14a1481f9fb50 Mon Sep 17 00:00:00 2001 From: Robert DeLuca Date: Thu, 16 Jan 2020 13:28:52 -0600 Subject: [PATCH 2/2] fix: import URL Somehow this worked in node 10? --- src/services/asset-discovery-service.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/services/asset-discovery-service.ts b/src/services/asset-discovery-service.ts index 2505b55b..e6d73491 100644 --- a/src/services/asset-discovery-service.ts +++ b/src/services/asset-discovery-service.ts @@ -1,6 +1,7 @@ import * as merge from 'deepmerge' import * as pool from 'generic-pool' import * as puppeteer from 'puppeteer' +import { URL } from 'url' import { AssetDiscoveryConfiguration } from '../configuration/asset-discovery-configuration' import { DEFAULT_CONFIGURATION } from '../configuration/configuration' import { SnapshotOptions } from '../percy-agent-client/snapshot-options'