-
Notifications
You must be signed in to change notification settings - Fork 9.4k
/
base-artifacts.js
92 lines (78 loc) · 2.67 KB
/
base-artifacts.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
/**
* @license
* Copyright 2021 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import log from 'lighthouse-logger';
import {isEqual} from 'lodash-es';
import {
getBrowserVersion, getBenchmarkIndex, getEnvironmentWarnings,
} from './driver/environment.js';
/**
* @param {LH.Config.ResolvedConfig} resolvedConfig
* @param {LH.Gatherer.Driver} driver
* @param {{gatherMode: LH.Gatherer.GatherMode}} context
* @return {Promise<LH.BaseArtifacts>}
*/
async function getBaseArtifacts(resolvedConfig, driver, context) {
const BenchmarkIndex = await getBenchmarkIndex(driver.executionContext);
const {userAgent, product} = await getBrowserVersion(driver.defaultSession);
return {
// Meta artifacts.
fetchTime: new Date().toJSON(),
Timing: [],
LighthouseRunWarnings: [],
settings: resolvedConfig.settings,
// Environment artifacts that can always be computed.
BenchmarkIndex,
HostUserAgent: userAgent,
HostFormFactor: userAgent.includes('Android') || userAgent.includes('Mobile') ?
'mobile' : 'desktop',
HostProduct: product,
// Contextual artifacts whose collection changes based on gather mode.
URL: {
finalDisplayedUrl: '',
},
PageLoadError: null,
GatherContext: context,
};
}
/**
* Deduplicates identical warnings.
* @param {Array<string | LH.IcuMessage>} warnings
* @return {Array<string | LH.IcuMessage>}
*/
function deduplicateWarnings(warnings) {
/** @type {Array<string | LH.IcuMessage>} */
const unique = [];
for (const warning of warnings) {
if (unique.some(existing => isEqual(warning, existing))) continue;
unique.push(warning);
}
return unique;
}
/**
* @param {LH.BaseArtifacts} baseArtifacts
* @param {Partial<LH.GathererArtifacts>} gathererArtifacts
* @return {LH.Artifacts}
*/
function finalizeArtifacts(baseArtifacts, gathererArtifacts) {
baseArtifacts.LighthouseRunWarnings.push(
...getEnvironmentWarnings({settings: baseArtifacts.settings, baseArtifacts})
);
// Cast to remove the partial from gathererArtifacts.
const artifacts = /** @type {LH.Artifacts} */ ({...baseArtifacts, ...gathererArtifacts});
// Set the post-run meta artifacts.
artifacts.Timing = log.getTimeEntries();
artifacts.LighthouseRunWarnings = deduplicateWarnings(baseArtifacts.LighthouseRunWarnings);
if (artifacts.PageLoadError && !artifacts.URL.finalDisplayedUrl) {
artifacts.URL.finalDisplayedUrl = artifacts.URL.requestedUrl || '';
}
// Check that the runner remembered to mutate the special-case URL artifact.
if (!artifacts.URL.finalDisplayedUrl) throw new Error('Runner did not set finalDisplayedUrl');
return artifacts;
}
export {
getBaseArtifacts,
finalizeArtifacts,
};