From ec3775ae5af032b8f6738e58d5e107f474025b59 Mon Sep 17 00:00:00 2001 From: Paul Irish Date: Sun, 25 Feb 2024 13:09:19 -0800 Subject: [PATCH] core(gather): gatherFn refactor --- core/gather/navigation-runner.js | 75 ++++++++++++++++---------------- core/gather/snapshot-runner.js | 50 ++++++++++----------- core/gather/timespan-runner.js | 49 ++++++++++----------- 3 files changed, 86 insertions(+), 88 deletions(-) diff --git a/core/gather/navigation-runner.js b/core/gather/navigation-runner.js index 31c6e7c45d80..c15aa7418651 100644 --- a/core/gather/navigation-runner.js +++ b/core/gather/navigation-runner.js @@ -277,44 +277,43 @@ async function navigationGather(page, requestor, options = {}) { const isCallback = typeof requestor === 'function'; const runnerOptions = {resolvedConfig, computedCache}; - const artifacts = await Runner.gather( - async () => { - const normalizedRequestor = isCallback ? requestor : UrlUtils.normalizeUrl(requestor); - - /** @type {LH.Puppeteer.Browser|undefined} */ - let lhBrowser = undefined; - /** @type {LH.Puppeteer.Page|undefined} */ - let lhPage = undefined; - - // For navigation mode, we shouldn't connect to a browser in audit mode, - // therefore we connect to the browser in the gatherFn callback. - if (!page) { - const {hostname = DEFAULT_HOSTNAME, port = DEFAULT_PORT} = flags; - lhBrowser = await puppeteer.connect({browserURL: `http://${hostname}:${port}`, defaultViewport: null}); - lhPage = await lhBrowser.newPage(); - page = lhPage; - } - - const driver = new Driver(page); - const context = { - driver, - lhBrowser, - lhPage, - page, - resolvedConfig, - requestor: normalizedRequestor, - computedCache, - }; - const {baseArtifacts} = await _setup(context); - - const artifacts = await _navigation({...context, baseArtifacts}); - - await _cleanup(context); - - return finalizeArtifacts(baseArtifacts, artifacts); - }, - runnerOptions - ); + + const gatherFn = async () => { + const normalizedRequestor = isCallback ? requestor : UrlUtils.normalizeUrl(requestor); + + /** @type {LH.Puppeteer.Browser|undefined} */ + let lhBrowser = undefined; + /** @type {LH.Puppeteer.Page|undefined} */ + let lhPage = undefined; + + // For navigation mode, we shouldn't connect to a browser in audit mode, + // therefore we connect to the browser in the gatherFn callback. + if (!page) { + const {hostname = DEFAULT_HOSTNAME, port = DEFAULT_PORT} = flags; + lhBrowser = await puppeteer.connect({browserURL: `http://${hostname}:${port}`, defaultViewport: null}); + lhPage = await lhBrowser.newPage(); + page = lhPage; + } + + const driver = new Driver(page); + const context = { + driver, + lhBrowser, + lhPage, + page, + resolvedConfig, + requestor: normalizedRequestor, + computedCache, + }; + const {baseArtifacts} = await _setup(context); + + const artifacts = await _navigation({...context, baseArtifacts}); + + await _cleanup(context); + + return finalizeArtifacts(baseArtifacts, artifacts); + }; + const artifacts = await Runner.gather(gatherFn, runnerOptions); return {artifacts, runnerOptions}; } diff --git a/core/gather/snapshot-runner.js b/core/gather/snapshot-runner.js index 476a0039976e..e8f6c8b4c6b9 100644 --- a/core/gather/snapshot-runner.js +++ b/core/gather/snapshot-runner.js @@ -30,35 +30,35 @@ async function snapshotGather(page, options = {}) { const url = await driver.url(); const runnerOptions = {resolvedConfig, computedCache}; - const artifacts = await Runner.gather( - async () => { - const baseArtifacts = + + const gatherFn = async () => { + const baseArtifacts = await getBaseArtifacts(resolvedConfig, driver, {gatherMode: 'snapshot'}); - baseArtifacts.URL = { - finalDisplayedUrl: url, - }; + baseArtifacts.URL = { + finalDisplayedUrl: url, + }; + + const artifactDefinitions = resolvedConfig.artifacts || []; + const artifactState = getEmptyArtifactState(); + await collectPhaseArtifacts({ + phase: 'getArtifact', + gatherMode: 'snapshot', + driver, + page, + baseArtifacts, + artifactDefinitions, + artifactState, + computedCache, + settings: resolvedConfig.settings, + }); - const artifactDefinitions = resolvedConfig.artifacts || []; - const artifactState = getEmptyArtifactState(); - await collectPhaseArtifacts({ - phase: 'getArtifact', - gatherMode: 'snapshot', - driver, - page, - baseArtifacts, - artifactDefinitions, - artifactState, - computedCache, - settings: resolvedConfig.settings, - }); + await driver.disconnect(); - await driver.disconnect(); + const artifacts = await awaitArtifacts(artifactState); + return finalizeArtifacts(baseArtifacts, artifacts); + }; - const artifacts = await awaitArtifacts(artifactState); - return finalizeArtifacts(baseArtifacts, artifacts); - }, - runnerOptions - ); + const artifacts = await Runner.gather(gatherFn, runnerOptions); return {artifacts, runnerOptions}; } diff --git a/core/gather/timespan-runner.js b/core/gather/timespan-runner.js index 35859287b6f4..3b619d587477 100644 --- a/core/gather/timespan-runner.js +++ b/core/gather/timespan-runner.js @@ -72,31 +72,30 @@ async function startTimespanGather(page, options = {}) { const finalDisplayedUrl = await driver.url(); const runnerOptions = {resolvedConfig, computedCache}; - const artifacts = await Runner.gather( - async () => { - baseArtifacts.URL = {finalDisplayedUrl}; - - await collectPhaseArtifacts({phase: 'stopSensitiveInstrumentation', ...phaseOptions}); - await collectPhaseArtifacts({phase: 'stopInstrumentation', ...phaseOptions}); - - // bf-cache-failures can emit `Page.frameNavigated` at the end of the run. - // This can cause us to issue protocol commands after the target closes. - // We should disable our `Page.frameNavigated` handlers before that. - await disableAsyncStacks(); - - driver.defaultSession.off('Page.frameNavigated', onFrameNavigated); - if (pageNavigationDetected) { - baseArtifacts.LighthouseRunWarnings.push(str_(UIStrings.warningNavigationDetected)); - } - - await collectPhaseArtifacts({phase: 'getArtifact', ...phaseOptions}); - await driver.disconnect(); - - const artifacts = await awaitArtifacts(artifactState); - return finalizeArtifacts(baseArtifacts, artifacts); - }, - runnerOptions - ); + const gatherFn = async () => { + baseArtifacts.URL = {finalDisplayedUrl}; + + await collectPhaseArtifacts({phase: 'stopSensitiveInstrumentation', ...phaseOptions}); + await collectPhaseArtifacts({phase: 'stopInstrumentation', ...phaseOptions}); + + // bf-cache-failures can emit `Page.frameNavigated` at the end of the run. + // This can cause us to issue protocol commands after the target closes. + // We should disable our `Page.frameNavigated` handlers before that. + await disableAsyncStacks(); + + driver.defaultSession.off('Page.frameNavigated', onFrameNavigated); + if (pageNavigationDetected) { + baseArtifacts.LighthouseRunWarnings.push(str_(UIStrings.warningNavigationDetected)); + } + + await collectPhaseArtifacts({phase: 'getArtifact', ...phaseOptions}); + await driver.disconnect(); + + const artifacts = await awaitArtifacts(artifactState); + return finalizeArtifacts(baseArtifacts, artifacts); + }; + + const artifacts = await Runner.gather(gatherFn, runnerOptions); return {artifacts, runnerOptions}; }, };