-
Notifications
You must be signed in to change notification settings - Fork 9.4k
/
runner.js
437 lines (381 loc) · 16.2 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
/**
* @license Copyright 2016 Google Inc. 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');
const ReportScoring = require('./scoring');
const Audit = require('./audits/audit');
const log = require('lighthouse-logger');
const i18n = require('./lib/i18n/i18n.js');
const assetSaver = require('./lib/asset-saver');
const fs = require('fs');
const path = require('path');
const URL = require('./lib/url-shim');
const Sentry = require('./lib/sentry');
const generateReport = require('./report/report-generator').generateReport;
const LHError = require('./lib/lh-error.js');
/** @typedef {import('./gather/connections/connection.js')} Connection */
/** @typedef {import('./config/config.js')} Config */
class Runner {
/**
* @param {Connection} connection
* @param {{config: Config, url?: string, driverMock?: Driver}} runOpts
* @return {Promise<LH.RunnerResult|undefined>}
*/
static async run(connection, runOpts) {
try {
const startTime = Date.now();
const settings = runOpts.config.settings;
/**
* List of top-level warnings for this Lighthouse run.
* @type {Array<string>}
*/
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 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._getArtifactsPath(settings);
artifacts = await 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 {
if (typeof runOpts.url !== 'string' || runOpts.url.length === 0) {
throw new Error(`You must provide a url to the runner. '${runOpts.url}' provided.`);
}
try {
// Use canonicalized URL (with trailing slashes and such)
requestedUrl = new URL(runOpts.url).href;
} catch (e) {
throw new Error('The url provided should have a proper protocol and hostname.');
}
artifacts = await Runner._gatherArtifactsFromBrowser(requestedUrl, runOpts, connection);
// -G means save these to ./latest-run, etc.
if (settings.gatherMode) {
const path = Runner._getArtifactsPath(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 auditResults = await Runner._runAudits(settings, runOpts.config.audits, artifacts,
lighthouseRunWarnings);
// LHR construction phase
log.log('status', 'Generating results...');
if (artifacts.LighthouseRunWarnings) {
lighthouseRunWarnings.push(...artifacts.LighthouseRunWarnings);
}
// Entering: conclusion of the lighthouse result object
const lighthouseVersion = require('../package.json').version;
/** @type {Object<string, LH.Audit.Result>} */
const resultsById = {};
for (const audit of auditResults) {
resultsById[audit.id] = audit;
}
/** @type {Object<string, LH.Result.Category>} */
let categories = {};
if (runOpts.config.categories) {
categories = ReportScoring.scoreAllCategories(runOpts.config.categories, resultsById);
}
/** @type {LH.Result} */
const lhr = {
userAgent: artifacts.HostUserAgent,
environment: {
networkUserAgent: artifacts.NetworkUserAgent,
hostUserAgent: artifacts.HostUserAgent,
benchmarkIndex: artifacts.BenchmarkIndex,
},
lighthouseVersion,
fetchTime: artifacts.fetchTime,
requestedUrl: requestedUrl,
finalUrl: artifacts.URL.finalUrl,
runWarnings: lighthouseRunWarnings,
runtimeError: Runner.getArtifactRuntimeError(artifacts),
audits: resultsById,
configSettings: settings,
categories,
categoryGroups: runOpts.config.groups || undefined,
timing: {total: Date.now() - startTime},
i18n: {
rendererFormattedStrings: i18n.getRendererFormattedStrings(settings.locale),
icuMessagePaths: {},
},
};
// Replace ICU message references with localized strings; save replaced paths in lhr.
lhr.i18n.icuMessagePaths = i18n.replaceIcuMessageInstanceIds(lhr, settings.locale);
const report = generateReport(lhr, settings.output);
return {lhr, artifacts, report};
} catch (err) {
await Sentry.captureException(err, {level: 'fatal'});
throw err;
}
}
/**
* Establish connection, load page and collect all required artifacts
* @param {string} requestedUrl
* @param {{config: Config, 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,
};
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>} runWarnings
* @return {Promise<Array<LH.Audit.Result>>}
*/
static async _runAudits(settings, audits, artifacts, runWarnings) {
log.log('status', 'Analyzing and running audits...');
if (artifacts.settings) {
const overrides = {
locale: undefined,
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');
}
}
// Members of LH.Audit.Context that are shared across all audits.
const sharedAuditContext = {
settings,
LighthouseRunWarnings: runWarnings,
computedCache: new Map(),
};
// Run each audit sequentially
const auditResults = [];
for (const auditDefn of audits) {
const auditResult = await Runner._runAudit(auditDefn, artifacts, sharedAuditContext);
auditResults.push(auditResult);
}
return auditResults;
}
/**
* 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'|'LighthouseRunWarnings'|'computedCache'>} sharedAuditContext
* @return {Promise<LH.Audit.Result>}
* @private
*/
static async _runAudit(auditDefn, artifacts, sharedAuditContext) {
const audit = auditDefn.implementation;
const status = `Evaluating: ${i18n.getFormatted(audit.meta.title, 'en-US')}`;
log.log('status', 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 required, check that DEFAULT_PASS trace exists.
// TODO: need pass-specific check of networkRecords and traces.
const noTrace = artifactName === 'traces' && !artifacts.traces[Audit.DEFAULT_PASS];
if (noArtifact || noTrace) {
log.warn('Runner',
`${artifactName} gatherer, required by audit ${audit.meta.id}, did not run.`);
throw new Error(`Required ${artifactName} gatherer did not run.`);
}
// If artifact was an error, it must be non-fatal (or gatherRunner would
// have thrown). Output error result on behalf of audit.
if (artifacts[artifactName] instanceof Error) {
/** @type {Error} */
// @ts-ignore 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 Error(
`Required ${artifactName} gatherer encountered an error: ${artifactError.message}`);
// @ts-ignore 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,
};
const product = await audit.audit(artifacts, auditContext);
auditResult = Audit.generateAuditResult(audit, product);
} catch (err) {
log.warn(audit.meta.id, `Caught exception: ${err.message}`);
if (err.fatal) {
throw err;
}
Sentry.captureException(err, {tags: {audit: audit.meta.id}, level: 'error'});
// Non-fatal error become error audit result.
const errorMessage = err.friendlyMessage ?
`${err.friendlyMessage} (${err.message})` :
`Audit error: ${err.message}`;
auditResult = Audit.generateErrorAuditResult(audit, errorMessage);
}
log.verbose('statusEnd', status);
return auditResult;
}
/**
* Returns first runtimeError found in artifacts.
* @param {LH.Artifacts} artifacts
* @return {LH.Result['runtimeError']}
*/
static getArtifactRuntimeError(artifacts) {
for (const possibleErrorArtifact of Object.values(artifacts)) {
if (possibleErrorArtifact instanceof LHError && possibleErrorArtifact.lhrRuntimeError) {
const errorMessage = possibleErrorArtifact.friendlyMessage || possibleErrorArtifact.message;
return {
code: possibleErrorArtifact.code,
message: errorMessage,
};
}
}
return {
code: LHError.NO_ERROR,
message: '',
};
}
/**
* 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();
}
/**
* Resolves the location of the specified plugin and returns an absolute
* string path to the file. Used for loading custom audits and gatherers.
* Throws an error if no plugin is found.
* @param {string} plugin
* @param {string=} configDir The absolute path to the directory of the config file, if there is one.
* @param {string=} category Optional plugin category (e.g. 'audit') for better error messages.
* @return {string}
* @throws {Error}
*/
static resolvePlugin(plugin, configDir, category) {
// First try straight `require()`. Unlikely to be specified relative to this
// file, but adds support for Lighthouse plugins in npm modules as
// `require()` walks up parent directories looking inside any node_modules/
// present. Also handles absolute paths.
try {
return require.resolve(plugin);
} catch (e) {}
// See if the plugin resolves relative to the current working directory.
// Most useful to handle the case of invoking Lighthouse as a module, since
// then the config is an object and so has no path.
const cwdPath = path.resolve(process.cwd(), plugin);
try {
return require.resolve(cwdPath);
} catch (e) {}
const errorString = 'Unable to locate ' +
(category ? `${category}: ` : '') +
`${plugin} (tried to require() from '${__dirname}' and load from '${cwdPath}'`;
if (!configDir) {
throw new Error(errorString + ')');
}
// Finally, try looking up relative to the config file path. Just like the
// relative path passed to `require()` is found relative to the file it's
// in, this allows plugin paths to be specified relative to the config file.
const relativePath = path.resolve(configDir, plugin);
try {
return require.resolve(relativePath);
} catch (requireError) {}
throw new Error(errorString + ` and '${relativePath}')`);
}
/**
* Get path to use for -G and -A modes. Defaults to $CWD/latest-run
* @param {LH.Config.Settings} settings
* @return {string}
*/
static _getArtifactsPath(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;