Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
launchChrome(headless = this.flags.headless) {
Log.log('Lighthouse runner:', 'Launching Chrome');
const opts = {
chromeFlags: [
'--window-position=40,100',
'--window-size=412,732', // Nexus 5x
headless ? '--headless' : ''
]
};
if (this.flags.chromePath) {
opts.chromePath = this.flags.chromePath;
}
return chromeLauncher.launch(opts);
}
// save traces
for (const [passName, trace] of Object.entries(traces)) {
await saveTrace(trace, `${basePath}/${passName}${traceSuffix}`);
}
// save devtools log
for (const [passName, devtoolsLog] of Object.entries(devtoolsLogs)) {
const log = JSON.stringify(devtoolsLog);
fs.writeFileSync(`${basePath}/${passName}${devtoolsLogSuffix}`, log, 'utf8');
}
// save everything else, using a replacer to serialize LHErrors in the artifacts.
const restArtifactsString = JSON.stringify(restArtifacts, stringifyReplacer, 2);
fs.writeFileSync(`${basePath}/${artifactsFilename}`, restArtifactsString, 'utf8');
log.log('Artifacts saved to disk in folder:', basePath);
log.timeEnd(status);
}
async afterPass(passContext, loadData) {
/** @type {Array} */
const networkEvents = this.devtoolsEvents_.slice();
log.log('Debug', 'Network: Get trace snapshot');
const har = chromeHar.harFromMessages(networkEvents);
const parsedUrls = har.log.entries.map((e) => new URL(e.request.url));
if (isDebugMode()) {
// These entries are present in our network artifacts, but are missing in
// the builtin loadData network records.
const missingEntries =
findMissingEntries(har.log.entries, loadData.networkRecords);
missingEntries.forEach((entry) => {
log.warn('Debug', 'Missing URL', entry.request.url.substr(0, 100));
});
}
return {har, networkEvents, parsedUrls};
}
}
static async run(requestedUrl, settings, audits, artifacts) {
log.log('status', 'Analyzing and running audits...');
if (!audits) throw new Error('No audits in config to evaluate');
if (requestedUrl && !URL.equalWithExcludedFragments(requestedUrl, artifacts.URL.requestedUrl)) {
throw new Error('Cannot run audit mode on different URL than gatherers were');
}
// Check that current settings are compatible with settings used to gather artifacts.
if (artifacts.settings) {
const overrides = {gatherMode: undefined, auditMode: undefined, output: undefined};
const normalizedGatherSettings = Object.assign({}, artifacts.settings, overrides);
const normalizedAuditSettings = Object.assign({}, settings, overrides);
// TODO(phulce): allow change of throttling method to `simulate`
if (!isDeepEqual(normalizedGatherSettings, normalizedAuditSettings)) {
throw new Error('Cannot change settings between gathering and auditing');
}
}
for (const outputType of flags.output) {
const extension = outputType;
const output = report[flags.output.indexOf(outputType)];
let outputPath = `${resolvedPath}.report.${extension}`;
// If there was only a single output and the user specified an outputPath, force usage of it.
if (flags.outputPath && flags.output.length === 1) outputPath = flags.outputPath;
await Printer.write(output, outputType, outputPath);
if (outputType === Printer.OutputMode[Printer.OutputMode.html]) {
if (flags.view) {
opn(outputPath, {wait: false});
} else {
// eslint-disable-next-line max-len
log.log('CLI', 'Protip: Run lighthouse with `--view` to immediately open the HTML report in your browser');
}
}
}
}
print(score) {
Log.log('LIGHTHOUSE SCORE:', score);
}
}
async function saveLanternDebugTraces(pathWithBasename) {
if (!process.env.LANTERN_DEBUG) return;
for (const [label, nodeTimings] of Simulator.ALL_NODE_TIMINGS) {
if (lanternTraceSaver.simulationNamesToIgnore.includes(label)) continue;
const traceFilename = `${pathWithBasename}-${label}${traceSuffix}`;
await saveTrace(lanternTraceSaver.convertNodeTimingsToTrace(nodeTimings), traceFilename);
log.log('saveAssets', `${label} lantern trace file streamed to disk: ${traceFilename}`);
}
}
return Printer.write(results.report, flags.output, outputPath).then(results => {
if (flags.output === Printer.OutputMode[Printer.OutputMode.html] ||
flags.output === Printer.OutputMode[Printer.OutputMode.domhtml]) {
if (flags.view) {
opn(outputPath, {wait: false});
} else {
log.log(
'CLI',
'Protip: Run lighthouse with `--view` to immediately open the HTML report in your browser');
}
}
return results;
});
}
const saveAll = allAssets.map(async (passAssets, index) => {
const devtoolsLogFilename = `${pathWithBasename}-${index}${devtoolsLogSuffix}`;
fs.writeFileSync(devtoolsLogFilename, JSON.stringify(passAssets.devtoolsLog, null, 2));
log.log('saveAssets', 'devtools log saved to disk: ' + devtoolsLogFilename);
const streamTraceFilename = `${pathWithBasename}-${index}${traceSuffix}`;
log.log('saveAssets', 'streaming trace file to disk: ' + streamTraceFilename);
await saveTrace(passAssets.traceData, streamTraceFilename);
log.log('saveAssets', 'trace file streamed to disk: ' + streamTraceFilename);
});