Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

core(gather): gatherFn refactor #15830

Merged
merged 2 commits into from
Feb 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 37 additions & 38 deletions core/gather/navigation-runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -265,44 +265,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};
}

Expand Down
50 changes: 25 additions & 25 deletions core/gather/snapshot-runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -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};
}

Expand Down
49 changes: 24 additions & 25 deletions core/gather/timespan-runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -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};
},
};
Expand Down
Loading