-
Notifications
You must be signed in to change notification settings - Fork 9.4k
/
runner.js
477 lines (421 loc) · 18.9 KB
/
runner.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
/**
* @license Copyright 2016 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 isDeepEqual = require('lodash.isequal');
const Driver = require('./gather/driver.js');
const GatherRunner = require('./gather/gather-runner.js');
const ReportScoring = require('./scoring.js');
const Audit = require('./audits/audit.js');
const log = require('lighthouse-logger');
const i18n = require('./lib/i18n/i18n.js');
const stackPacks = require('./lib/stack-packs.js');
const assetSaver = require('./lib/asset-saver.js');
const fs = require('fs');
const path = require('path');
const URL = require('./lib/url-shim.js');
const Sentry = require('./lib/sentry.js');
const generateReport = require('./report/report-generator.js').generateReport;
const LHError = require('./lib/lh-error.js');
/** @typedef {import('./gather/connections/connection.js')} Connection */
/** @typedef {import('./lib/arbitrary-equality-map.js')} ArbitraryEqualityMap */
/** @typedef {LH.Config.Config} Config */
class Runner {
/**
* @template {LH.Config.Config | LH.Config.FRConfig} TConfig
* @param {(runnerData: {requestedUrl: string, config: TConfig, driverMock?: Driver}) => Promise<LH.Artifacts>} gatherFn
* @param {{config: TConfig, computedCache: Map<string, ArbitraryEqualityMap>, url?: string, driverMock?: Driver}} runOpts
* @return {Promise<LH.RunnerResult|undefined>}
*/
static async run(gatherFn, runOpts) {
const settings = runOpts.config.settings;
try {
const runnerStatus = {msg: 'Runner setup', id: 'lh:runner:run'};
log.time(runnerStatus, 'verbose');
/**
* List of top-level warnings for this Lighthouse run.
* @type {Array<string | LH.IcuMessage>}
*/
const lighthouseRunWarnings = [];
const sentryContext = Sentry.getContext();
Sentry.captureBreadcrumb({
message: 'Run started',
category: 'lifecycle',
data: sentryContext && sentryContext.extra,
});
// User can run -G solo, -A solo, or -GA together
// -G and -A will run partial lighthouse pipelines,
// and -GA will run everything plus save artifacts and lhr to disk.
// Gather phase
// Either load saved artifacts off disk or from the browser
let artifacts;
let requestedUrl;
if (settings.auditMode && !settings.gatherMode) {
// No browser required, just load the artifacts from disk.
const path = Runner._getDataSavePath(settings);
artifacts = assetSaver.loadArtifacts(path);
requestedUrl = artifacts.URL.requestedUrl;
if (!requestedUrl) {
throw new Error('Cannot run audit mode on empty URL');
}
if (runOpts.url && !URL.equalWithExcludedFragments(runOpts.url, requestedUrl)) {
throw new Error('Cannot run audit mode on different URL');
}
} else {
// verify the url is valid and that protocol is allowed
if (runOpts.url && URL.isValid(runOpts.url) && URL.isProtocolAllowed(runOpts.url)) {
// Use canonicalized URL (with trailing slashes and such)
requestedUrl = new URL(runOpts.url).href;
} else {
throw new LHError(LHError.errors.INVALID_URL);
}
artifacts = await gatherFn({
requestedUrl,
config: runOpts.config,
driverMock: runOpts.driverMock,
});
// -G means save these to ./latest-run, etc.
if (settings.gatherMode) {
const path = Runner._getDataSavePath(settings);
await assetSaver.saveArtifacts(artifacts, path);
}
}
// Potentially quit early
if (settings.gatherMode && !settings.auditMode) return;
// Audit phase
if (!runOpts.config.audits) {
throw new Error('No audits to evaluate.');
}
const auditResultsById = await Runner._runAudits(settings, runOpts.config.audits, artifacts,
lighthouseRunWarnings, runOpts.computedCache);
// LHR construction phase
const resultsStatus = {msg: 'Generating results...', id: 'lh:runner:generate'};
log.time(resultsStatus);
if (artifacts.LighthouseRunWarnings) {
lighthouseRunWarnings.push(...artifacts.LighthouseRunWarnings);
}
// Entering: conclusion of the lighthouse result object
const lighthouseVersion = require('../package.json').version;
// Use version from gathering stage.
// If accessibility gatherer didn't run or errored, it won't be in credits.
const axeVersion = artifacts.Accessibility && artifacts.Accessibility.version;
const credits = {
'axe-core': axeVersion,
};
/** @type {Record<string, LH.RawIcu<LH.Result.Category>>} */
let categories = {};
if (runOpts.config.categories) {
categories = ReportScoring.scoreAllCategories(runOpts.config.categories, auditResultsById);
}
log.timeEnd(resultsStatus);
log.timeEnd(runnerStatus);
/** @type {LH.RawIcu<LH.Result>} */
const i18nLhr = {
userAgent: artifacts.HostUserAgent,
environment: {
networkUserAgent: artifacts.NetworkUserAgent,
hostUserAgent: artifacts.HostUserAgent,
benchmarkIndex: artifacts.BenchmarkIndex,
credits,
},
lighthouseVersion,
fetchTime: artifacts.fetchTime,
requestedUrl: requestedUrl,
finalUrl: artifacts.URL.finalUrl,
runWarnings: lighthouseRunWarnings,
runtimeError: Runner.getArtifactRuntimeError(artifacts),
audits: auditResultsById,
configSettings: settings,
categories,
categoryGroups: runOpts.config.groups || undefined,
timing: this._getTiming(artifacts),
i18n: {
rendererFormattedStrings: i18n.getRendererFormattedStrings(settings.locale),
icuMessagePaths: {},
},
stackPacks: stackPacks.getStackPacks(artifacts.Stacks),
};
// Replace ICU message references with localized strings; save replaced paths in lhr.
i18nLhr.i18n.icuMessagePaths = i18n.replaceIcuMessages(i18nLhr, settings.locale);
// LHR has now been localized.
const lhr = /** @type {LH.Result} */ (i18nLhr);
// Save lhr to ./latest-run, but only if -GA is used.
if (settings.gatherMode && settings.auditMode) {
const path = Runner._getDataSavePath(settings);
assetSaver.saveLhr(lhr, path);
}
// Create the HTML, JSON, and/or CSV string
const report = generateReport(lhr, settings.output);
return {lhr, artifacts, report};
} catch (err) {
// i18n LighthouseError strings.
if (err.friendlyMessage) {
err.friendlyMessage = i18n.getFormatted(err.friendlyMessage, settings.locale);
}
await Sentry.captureException(err, {level: 'fatal'});
throw err;
}
}
/**
* This handles both the auditMode case where gatherer entries need to be merged in and
* the gather/audit case where timingEntriesFromRunner contains all entries from this run,
* including those also in timingEntriesFromArtifacts.
* @param {LH.Artifacts} artifacts
* @return {LH.Result.Timing}
*/
static _getTiming(artifacts) {
const timingEntriesFromArtifacts = artifacts.Timing || [];
const timingEntriesFromRunner = log.takeTimeEntries();
const timingEntriesKeyValues = [
...timingEntriesFromArtifacts,
...timingEntriesFromRunner,
// As entries can share a name, dedupe based on the startTime timestamp
].map(entry => /** @type {[number, PerformanceEntry]} */ ([entry.startTime, entry]));
const timingEntries = Array.from(new Map(timingEntriesKeyValues).values())
// Truncate timestamps to hundredths of a millisecond saves ~4KB. No need for microsecond
// resolution.
.map(entry => {
return {
// Don't spread entry because browser PerformanceEntries can't be spread.
// https://github.com/GoogleChrome/lighthouse/issues/8638
startTime: parseFloat(entry.startTime.toFixed(2)),
name: entry.name,
duration: parseFloat(entry.duration.toFixed(2)),
entryType: entry.entryType,
};
});
const runnerEntry = timingEntries.find(e => e.name === 'lh:runner:run');
return {entries: timingEntries, total: runnerEntry && runnerEntry.duration || 0};
}
/**
* Establish connection, load page and collect all required artifacts
* @param {string} requestedUrl
* @param {{config: Config, computedCache: Map<string, ArbitraryEqualityMap>, driverMock?: Driver}} runnerOpts
* @param {Connection} connection
* @return {Promise<LH.Artifacts>}
*/
static async _gatherArtifactsFromBrowser(requestedUrl, runnerOpts, connection) {
if (!runnerOpts.config.passes) {
throw new Error('No browser artifacts are either provided or requested.');
}
const driver = runnerOpts.driverMock || new Driver(connection);
const gatherOpts = {
driver,
requestedUrl,
settings: runnerOpts.config.settings,
computedCache: runnerOpts.computedCache,
};
const artifacts = await GatherRunner.run(runnerOpts.config.passes, gatherOpts);
return artifacts;
}
/**
* Run all audits with specified settings and artifacts.
* @param {LH.Config.Settings} settings
* @param {Array<LH.Config.AuditDefn>} audits
* @param {LH.Artifacts} artifacts
* @param {Array<string | LH.IcuMessage>} runWarnings
* @param {Map<string, ArbitraryEqualityMap>} computedCache
* @return {Promise<Record<string, LH.RawIcu<LH.Audit.Result>>>}
*/
static async _runAudits(settings, audits, artifacts, runWarnings, computedCache) {
const status = {msg: 'Analyzing and running audits...', id: 'lh:runner:auditing'};
log.time(status);
if (artifacts.settings) {
const overrides = {
locale: undefined,
gatherMode: undefined,
auditMode: undefined,
output: undefined,
channel: undefined,
budgets: undefined,
};
const normalizedGatherSettings = Object.assign({}, artifacts.settings, overrides);
const normalizedAuditSettings = Object.assign({}, settings, overrides);
if (!isDeepEqual(normalizedGatherSettings, normalizedAuditSettings)) {
throw new Error('Cannot change settings between gathering and auditing');
}
}
// Members of LH.Audit.Context that are shared across all audits.
const sharedAuditContext = {
settings,
computedCache,
};
// Run each audit sequentially
/** @type {Record<string, LH.RawIcu<LH.Audit.Result>>} */
const auditResultsById = {};
for (const auditDefn of audits) {
const auditId = auditDefn.implementation.meta.id;
const auditResult = await Runner._runAudit(auditDefn, artifacts, sharedAuditContext,
runWarnings);
auditResultsById[auditId] = auditResult;
}
log.timeEnd(status);
return auditResultsById;
}
/**
* Checks that the audit's required artifacts exist and runs the audit if so.
* Otherwise returns error audit result.
* @param {LH.Config.AuditDefn} auditDefn
* @param {LH.Artifacts} artifacts
* @param {Pick<LH.Audit.Context, 'settings'|'computedCache'>} sharedAuditContext
* @param {Array<string | LH.IcuMessage>} runWarnings
* @return {Promise<LH.RawIcu<LH.Audit.Result>>}
* @private
*/
static async _runAudit(auditDefn, artifacts, sharedAuditContext, runWarnings) {
const audit = auditDefn.implementation;
const status = {
msg: `Auditing: ${i18n.getFormatted(audit.meta.title, 'en-US')}`,
id: `lh:audit:${audit.meta.id}`,
};
log.time(status);
let auditResult;
try {
// Return an early error if an artifact required for the audit is missing or an error.
for (const artifactName of audit.meta.requiredArtifacts) {
const noArtifact = artifacts[artifactName] === undefined;
// If trace/devtoolsLog required, check that DEFAULT_PASS trace/devtoolsLog exists.
// NOTE: for now, not a pass-specific check of traces or devtoolsLogs.
const noRequiredTrace = artifactName === 'traces' && !artifacts.traces[Audit.DEFAULT_PASS];
const noRequiredDevtoolsLog = artifactName === 'devtoolsLogs' &&
!artifacts.devtoolsLogs[Audit.DEFAULT_PASS];
if (noArtifact || noRequiredTrace || noRequiredDevtoolsLog) {
log.warn('Runner',
`${artifactName} gatherer, required by audit ${audit.meta.id}, did not run.`);
throw new LHError(LHError.errors.MISSING_REQUIRED_ARTIFACT, {artifactName});
}
// If artifact was an error, output error result on behalf of audit.
if (artifacts[artifactName] instanceof Error) {
/** @type {Error} */
// @ts-expect-error An artifact *could* be an Error, but caught here, so ignore elsewhere.
const artifactError = artifacts[artifactName];
Sentry.captureException(artifactError, {
tags: {gatherer: artifactName},
level: 'error',
});
log.warn('Runner', `${artifactName} gatherer, required by audit ${audit.meta.id},` +
` encountered an error: ${artifactError.message}`);
// Create a friendlier display error and mark it as expected to avoid duplicates in Sentry
const error = new LHError(LHError.errors.ERRORED_REQUIRED_ARTIFACT,
{artifactName, errorMessage: artifactError.message});
// @ts-expect-error Non-standard property added to Error
error.expected = true;
throw error;
}
}
// all required artifacts are in good shape, so we proceed
const auditOptions = Object.assign({}, audit.defaultOptions, auditDefn.options);
const auditContext = {
options: auditOptions,
...sharedAuditContext,
};
// Only pass the declared required and optional artifacts to the audit
// The type is masquerading as `LH.Artifacts` but will only contain a subset of the keys
// to prevent consumers from unnecessary type assertions.
const requestedArtifacts = audit.meta.requiredArtifacts
.concat(audit.meta.__internalOptionalArtifacts || []);
const narrowedArtifacts = requestedArtifacts
.reduce((narrowedArtifacts, artifactName) => {
const requestedArtifact = artifacts[artifactName];
// @ts-expect-error tsc can't yet express that artifactName is only a single type in each iteration, not a union of types.
narrowedArtifacts[artifactName] = requestedArtifact;
return narrowedArtifacts;
}, /** @type {LH.Artifacts} */ ({}));
const product = await audit.audit(narrowedArtifacts, auditContext);
runWarnings.push(...product.runWarnings || []);
auditResult = Audit.generateAuditResult(audit, product);
} catch (err) {
// Log error if it hasn't already been logged above.
if (err.code !== 'MISSING_REQUIRED_ARTIFACT' && err.code !== 'ERRORED_REQUIRED_ARTIFACT') {
log.warn(audit.meta.id, `Caught exception: ${err.message}`);
}
Sentry.captureException(err, {tags: {audit: audit.meta.id}, level: 'error'});
// Errors become error audit result.
const errorMessage = err.friendlyMessage ? err.friendlyMessage : err.message;
auditResult = Audit.generateErrorAuditResult(audit, errorMessage);
}
log.timeEnd(status);
return auditResult;
}
/**
* Searches a pass's artifacts for any `lhrRuntimeError` error artifacts.
* Returns the first one found or `null` if none found.
* @param {LH.Artifacts} artifacts
* @return {LH.RawIcu<LH.Result['runtimeError']>|undefined}
*/
static getArtifactRuntimeError(artifacts) {
const possibleErrorArtifacts = [
artifacts.PageLoadError, // Preferentially use `PageLoadError`, if it exists.
...Object.values(artifacts), // Otherwise check amongst all artifacts.
];
for (const possibleErrorArtifact of possibleErrorArtifacts) {
if (possibleErrorArtifact instanceof LHError && possibleErrorArtifact.lhrRuntimeError) {
const errorMessage = possibleErrorArtifact.friendlyMessage || possibleErrorArtifact.message;
return {
code: possibleErrorArtifact.code,
message: errorMessage,
};
}
}
return undefined;
}
/**
* Returns list of audit names for external querying.
* @return {Array<string>}
*/
static getAuditList() {
const ignoredFiles = [
'audit.js',
'violation-audit.js',
'accessibility/axe-audit.js',
'multi-check-audit.js',
'byte-efficiency/byte-efficiency-audit.js',
'manual/manual-audit.js',
];
const fileList = [
...fs.readdirSync(path.join(__dirname, './audits')),
...fs.readdirSync(path.join(__dirname, './audits/dobetterweb')).map(f => `dobetterweb/${f}`),
...fs.readdirSync(path.join(__dirname, './audits/metrics')).map(f => `metrics/${f}`),
...fs.readdirSync(path.join(__dirname, './audits/seo')).map(f => `seo/${f}`),
...fs.readdirSync(path.join(__dirname, './audits/seo/manual')).map(f => `seo/manual/${f}`),
...fs.readdirSync(path.join(__dirname, './audits/accessibility'))
.map(f => `accessibility/${f}`),
...fs.readdirSync(path.join(__dirname, './audits/accessibility/manual'))
.map(f => `accessibility/manual/${f}`),
...fs.readdirSync(path.join(__dirname, './audits/byte-efficiency'))
.map(f => `byte-efficiency/${f}`),
...fs.readdirSync(path.join(__dirname, './audits/manual')).map(f => `manual/${f}`),
];
return fileList.filter(f => {
return /\.js$/.test(f) && !ignoredFiles.includes(f);
}).sort();
}
/**
* Returns list of gatherer names for external querying.
* @return {Array<string>}
*/
static getGathererList() {
const fileList = [
...fs.readdirSync(path.join(__dirname, './gather/gatherers')),
...fs.readdirSync(path.join(__dirname, './gather/gatherers/seo')).map(f => `seo/${f}`),
...fs.readdirSync(path.join(__dirname, './gather/gatherers/dobetterweb'))
.map(f => `dobetterweb/${f}`),
];
return fileList.filter(f => /\.js$/.test(f) && f !== 'gatherer.js').sort();
}
/**
* Get path to use for -G and -A modes. Defaults to $CWD/latest-run
* @param {LH.Config.Settings} settings
* @return {string}
*/
static _getDataSavePath(settings) {
const {auditMode, gatherMode} = settings;
// This enables usage like: -GA=./custom-folder
if (typeof auditMode === 'string') return path.resolve(process.cwd(), auditMode);
if (typeof gatherMode === 'string') return path.resolve(process.cwd(), gatherMode);
return path.join(process.cwd(), 'latest-run');
}
}
module.exports = Runner;