-
Notifications
You must be signed in to change notification settings - Fork 9.4k
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(fr): add basic timespan support #11944
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/** | ||
* @license Copyright 2021 The Lighthouse Authors. All Rights Reserved. | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. | ||
*/ | ||
'use strict'; | ||
|
||
/** | ||
* @param {LH.Config.FRConfig} config | ||
* @return {LH.BaseArtifacts} | ||
*/ | ||
function getBaseArtifacts(config) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is just moved, nothing special here |
||
// TODO(FR-COMPAT): convert these to regular artifacts | ||
|
||
return { | ||
fetchTime: new Date().toJSON(), | ||
LighthouseRunWarnings: [], | ||
URL: {requestedUrl: '', finalUrl: ''}, | ||
Timing: [], | ||
Stacks: [], | ||
settings: config.settings, | ||
HostFormFactor: 'mobile', | ||
HostUserAgent: 'unknown', | ||
NetworkUserAgent: 'unknown', | ||
BenchmarkIndex: 0, | ||
InstallabilityErrors: {errors: []}, | ||
traces: {}, | ||
devtoolsLogs: {}, | ||
WebAppManifest: null, | ||
PageLoadError: null, | ||
}; | ||
} | ||
|
||
module.exports = {getBaseArtifacts}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/** | ||
* @license Copyright 2020 The Lighthouse Authors. All Rights Reserved. | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. | ||
*/ | ||
'use strict'; | ||
|
||
const Driver = require('./driver.js'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is just moved, no logic changes here |
||
const Runner = require('../../runner.js'); | ||
const {initializeConfig} = require('../config/config.js'); | ||
const {getBaseArtifacts} = require('./base-artifacts.js'); | ||
|
||
/** @param {{page: import('puppeteer').Page, config?: LH.Config.Json}} options */ | ||
async function snapshot(options) { | ||
const {config} = initializeConfig(options.config, {gatherMode: 'snapshot'}); | ||
const driver = new Driver(options.page); | ||
await driver.connect(); | ||
|
||
const url = await options.page.url(); | ||
|
||
return Runner.run( | ||
async () => { | ||
const baseArtifacts = getBaseArtifacts(config); | ||
baseArtifacts.URL.requestedUrl = url; | ||
baseArtifacts.URL.finalUrl = url; | ||
|
||
/** @type {Partial<LH.GathererArtifacts>} */ | ||
const artifacts = {}; | ||
|
||
for (const {id, gatherer} of config.artifacts || []) { | ||
const artifactName = /** @type {keyof LH.GathererArtifacts} */ (id); | ||
const artifact = await Promise.resolve() | ||
.then(() => gatherer.instance.snapshot({gatherMode: 'snapshot', driver})) | ||
.catch(err => err); | ||
|
||
artifacts[artifactName] = artifact; | ||
} | ||
|
||
return /** @type {LH.Artifacts} */ ({...baseArtifacts, ...artifacts}); // Cast to drop Partial<> | ||
}, | ||
{ | ||
url, | ||
config, | ||
} | ||
); | ||
} | ||
|
||
module.exports = { | ||
snapshot, | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/** | ||
* @license Copyright 2021 The Lighthouse Authors. All Rights Reserved. | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. | ||
*/ | ||
'use strict'; | ||
|
||
const Driver = require('./driver.js'); | ||
const Runner = require('../../runner.js'); | ||
const {initializeConfig} = require('../config/config.js'); | ||
const {getBaseArtifacts} = require('./base-artifacts.js'); | ||
|
||
/** | ||
* @param {{page: import('puppeteer').Page, config?: LH.Config.Json}} options | ||
* @return {Promise<{endTimespan(): Promise<LH.RunnerResult|undefined>}>} | ||
*/ | ||
async function startTimespan(options) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is new and the bulk of the logic to review |
||
const {config} = initializeConfig(options.config, {gatherMode: 'timespan'}); | ||
const driver = new Driver(options.page); | ||
await driver.connect(); | ||
|
||
const requestedUrl = await options.page.url(); | ||
|
||
/** @type {Record<string, Promise<Error|undefined>>} */ | ||
const artifactErrors = {}; | ||
|
||
for (const {id, gatherer} of config.artifacts || []) { | ||
artifactErrors[id] = await Promise.resolve() | ||
.then(() => gatherer.instance.beforeTimespan({gatherMode: 'timespan', driver})) | ||
.catch(err => err); | ||
} | ||
|
||
return { | ||
async endTimespan() { | ||
const finalUrl = await options.page.url(); | ||
return Runner.run( | ||
async () => { | ||
const baseArtifacts = getBaseArtifacts(config); | ||
baseArtifacts.URL.requestedUrl = requestedUrl; | ||
baseArtifacts.URL.finalUrl = finalUrl; | ||
|
||
/** @type {Partial<LH.GathererArtifacts>} */ | ||
const artifacts = {}; | ||
|
||
for (const {id, gatherer} of config.artifacts || []) { | ||
const artifactName = /** @type {keyof LH.GathererArtifacts} */ (id); | ||
const artifact = artifactErrors[id] | ||
? Promise.reject(artifactErrors[id]) | ||
: await Promise.resolve() | ||
.then(() => gatherer.instance.afterTimespan({gatherMode: 'timespan', driver})) | ||
.catch(err => err); | ||
|
||
artifacts[artifactName] = artifact; | ||
} | ||
|
||
return /** @type {LH.Artifacts} */ ({...baseArtifacts, ...artifacts}); // Cast to drop Partial<> | ||
}, | ||
{ | ||
url: finalUrl, | ||
config, | ||
} | ||
); | ||
}, | ||
}; | ||
} | ||
|
||
module.exports = { | ||
startTimespan, | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is just a move, no logic changes here