lighthouse 9.2.0-dev.20220105 → 9.2.0-dev.20220109

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -232,6 +232,10 @@ async function runLighthouse(url, flags, config) {
232
232
  let launchedChrome;
233
233
 
234
234
  try {
235
+ if (url && flags.auditMode && !flags.gatherMode) {
236
+ log.warn('CLI', 'URL parameter is ignored if -A flag is used without -G flag');
237
+ }
238
+
235
239
  const shouldGather = flags.gatherMode || flags.gatherMode === flags.auditMode;
236
240
  const shouldUseLocalChrome = URL.isLikeLocalhost(flags.hostname);
237
241
  if (shouldGather && shouldUseLocalChrome) {
@@ -22,6 +22,7 @@ const {initializeConfig} = require('../config/config.js');
22
22
  const {getBaseArtifacts, finalizeArtifacts} = require('./base-artifacts.js');
23
23
  const format = require('../../../shared/localization/format.js');
24
24
  const LighthouseError = require('../../lib/lh-error.js');
25
+ const URL = require('../../lib/url-shim.js');
25
26
  const {getPageLoadError} = require('../../lib/navigation-error.js');
26
27
  const Trace = require('../../gather/gatherers/trace.js');
27
28
  const DevtoolsLog = require('../../gather/gatherers/devtools-log.js');
@@ -279,7 +280,7 @@ async function _cleanup({requestedUrl, driver, config}) {
279
280
  * @return {Promise<LH.RunnerResult|undefined>}
280
281
  */
281
282
  async function navigation(options) {
282
- const {url: requestedUrl, page, configContext = {}} = options;
283
+ const {url, page, configContext = {}} = options;
283
284
  const {config} = initializeConfig(options.config, {...configContext, gatherMode: 'navigation'});
284
285
  const computedCache = new Map();
285
286
  const internalOptions = {
@@ -289,6 +290,7 @@ async function navigation(options) {
289
290
  return Runner.run(
290
291
  async () => {
291
292
  const driver = new Driver(page);
293
+ const requestedUrl = URL.normalizeUrl(url);
292
294
  const context = {driver, config, requestedUrl, options: internalOptions};
293
295
  const {baseArtifacts} = await _setup(context);
294
296
  const {artifacts} = await _navigations({...context, baseArtifacts, computedCache});
@@ -297,7 +299,6 @@ async function navigation(options) {
297
299
  return finalizeArtifacts(baseArtifacts, artifacts);
298
300
  },
299
301
  {
300
- url: requestedUrl,
301
302
  config,
302
303
  computedCache: new Map(),
303
304
  }
@@ -51,7 +51,6 @@ async function snapshot(options) {
51
51
  return finalizeArtifacts(baseArtifacts, artifacts);
52
52
  },
53
53
  {
54
- url,
55
54
  config,
56
55
  computedCache,
57
56
  }
@@ -64,7 +64,6 @@ async function startTimespan(options) {
64
64
  return finalizeArtifacts(baseArtifacts, artifacts);
65
65
  },
66
66
  {
67
- url: finalUrl,
68
67
  config,
69
68
  computedCache,
70
69
  }
@@ -9,6 +9,7 @@ const Runner = require('./runner.js');
9
9
  const log = require('lighthouse-logger');
10
10
  const ChromeProtocol = require('./gather/connections/cri.js');
11
11
  const Config = require('./config/config.js');
12
+ const URL = require('./lib/url-shim.js');
12
13
 
13
14
  /** @typedef {import('./gather/connections/connection.js')} Connection */
14
15
 
@@ -42,12 +43,12 @@ async function lighthouse(url, flags = {}, configJSON, userConnection) {
42
43
 
43
44
  const config = generateConfig(configJSON, flags);
44
45
  const computedCache = new Map();
45
- const options = {url, config, computedCache};
46
+ const options = {config, computedCache};
46
47
  const connection = userConnection || new ChromeProtocol(flags.port, flags.hostname);
47
48
 
48
49
  // kick off a lighthouse run
49
- /** @param {{requestedUrl: string}} runnerData */
50
- const gatherFn = ({requestedUrl}) => {
50
+ const gatherFn = () => {
51
+ const requestedUrl = URL.normalizeUrl(url);
51
52
  return Runner._gatherArtifactsFromBrowser(requestedUrl, options, connection);
52
53
  };
53
54
  return Runner.run(gatherFn, options);
@@ -10,6 +10,7 @@
10
10
  */
11
11
 
12
12
  const {Util} = require('../util-commonjs.js');
13
+ const LHError = require('../lib/lh-error.js');
13
14
 
14
15
  /** @typedef {import('./network-request.js')} NetworkRequest */
15
16
 
@@ -248,6 +249,20 @@ class URLShim extends URL {
248
249
  if (ext === 'jpg') return 'image/jpeg';
249
250
  return `image/${ext}`;
250
251
  }
252
+
253
+ /**
254
+ * @param {string|undefined} url
255
+ * @return {string}
256
+ */
257
+ static normalizeUrl(url) {
258
+ // Verify the url is valid and that protocol is allowed
259
+ if (url && this.isValid(url) && this.isProtocolAllowed(url)) {
260
+ // Use canonicalized URL (with trailing slashes and such)
261
+ return new URL(url).href;
262
+ } else {
263
+ throw new LHError(LHError.errors.INVALID_URL);
264
+ }
265
+ }
251
266
  }
252
267
 
253
268
  URLShim.URL = URL;
@@ -16,7 +16,6 @@ const stackPacks = require('./lib/stack-packs.js');
16
16
  const assetSaver = require('./lib/asset-saver.js');
17
17
  const fs = require('fs');
18
18
  const path = require('path');
19
- const URL = require('./lib/url-shim.js');
20
19
  const Sentry = require('./lib/sentry.js');
21
20
  const generateReport = require('../report/generator/report-generator.js').generateReport;
22
21
  const LHError = require('./lib/lh-error.js');
@@ -29,8 +28,8 @@ const {version: lighthouseVersion} = require('../package.json');
29
28
  class Runner {
30
29
  /**
31
30
  * @template {LH.Config.Config | LH.Config.FRConfig} TConfig
32
- * @param {(runnerData: {requestedUrl: string, config: TConfig, driverMock?: Driver}) => Promise<LH.Artifacts>} gatherFn
33
- * @param {{config: TConfig, computedCache: Map<string, ArbitraryEqualityMap>, url?: string, driverMock?: Driver}} runOpts
31
+ * @param {(runnerData: {config: TConfig, driverMock?: Driver}) => Promise<LH.Artifacts>} gatherFn
32
+ * @param {{config: TConfig, computedCache: Map<string, ArbitraryEqualityMap>, driverMock?: Driver}} runOpts
34
33
  * @return {Promise<LH.RunnerResult|undefined>}
35
34
  */
36
35
  static async run(gatherFn, runOpts) {
@@ -52,47 +51,7 @@ class Runner {
52
51
  data: sentryContext?.extra,
53
52
  });
54
53
 
55
- // User can run -G solo, -A solo, or -GA together
56
- // -G and -A will run partial lighthouse pipelines,
57
- // and -GA will run everything plus save artifacts and lhr to disk.
58
-
59
- // Gather phase
60
- // Either load saved artifacts off disk or from the browser
61
- let artifacts;
62
- let requestedUrl;
63
- if (settings.auditMode && !settings.gatherMode) {
64
- // No browser required, just load the artifacts from disk.
65
- const path = Runner._getDataSavePath(settings);
66
- artifacts = assetSaver.loadArtifacts(path);
67
- requestedUrl = artifacts.URL.requestedUrl;
68
-
69
- if (!requestedUrl) {
70
- throw new Error('Cannot run audit mode on empty URL');
71
- }
72
- if (runOpts.url && !URL.equalWithExcludedFragments(runOpts.url, requestedUrl)) {
73
- throw new Error('Cannot run audit mode on different URL');
74
- }
75
- } else {
76
- // verify the url is valid and that protocol is allowed
77
- if (runOpts.url && URL.isValid(runOpts.url) && URL.isProtocolAllowed(runOpts.url)) {
78
- // Use canonicalized URL (with trailing slashes and such)
79
- requestedUrl = new URL(runOpts.url).href;
80
- } else {
81
- throw new LHError(LHError.errors.INVALID_URL);
82
- }
83
-
84
- artifacts = await gatherFn({
85
- requestedUrl,
86
- config: runOpts.config,
87
- driverMock: runOpts.driverMock,
88
- });
89
-
90
- // -G means save these to ./latest-run, etc.
91
- if (settings.gatherMode) {
92
- const path = Runner._getDataSavePath(settings);
93
- await assetSaver.saveArtifacts(artifacts, path);
94
- }
95
- }
54
+ const artifacts = await this.gatherAndManageArtifacts(gatherFn, runOpts);
96
55
 
97
56
  // Potentially quit early
98
57
  if (settings.gatherMode && !settings.auditMode) return;
@@ -133,7 +92,7 @@ class Runner {
133
92
  /** @type {LH.RawIcu<LH.Result>} */
134
93
  const i18nLhr = {
135
94
  lighthouseVersion,
136
- requestedUrl,
95
+ requestedUrl: artifacts.URL.requestedUrl,
137
96
  finalUrl: artifacts.URL.finalUrl,
138
97
  fetchTime: artifacts.fetchTime,
139
98
  gatherMode: artifacts.GatherContext.gatherMode,
@@ -184,6 +143,47 @@ class Runner {
184
143
  }
185
144
  }
186
145
 
146
+ /**
147
+ * User can run -G solo, -A solo, or -GA together
148
+ * -G and -A will run partial lighthouse pipelines,
149
+ * and -GA will run everything plus save artifacts and lhr to disk.
150
+ *
151
+ * @template {LH.Config.Config | LH.Config.FRConfig} TConfig
152
+ * @param {(runnerData: {config: TConfig, driverMock?: Driver}) => Promise<LH.Artifacts>} gatherFn
153
+ * @param {{config: TConfig, driverMock?: Driver}} options
154
+ * @return {Promise<LH.Artifacts>}
155
+ */
156
+ static async gatherAndManageArtifacts(gatherFn, options) {
157
+ const settings = options.config.settings;
158
+
159
+ // Gather phase
160
+ // Either load saved artifacts from disk or from the browser.
161
+ let artifacts;
162
+ if (settings.auditMode && !settings.gatherMode) {
163
+ // No browser required, just load the artifacts from disk.
164
+ const path = this._getDataSavePath(settings);
165
+ artifacts = assetSaver.loadArtifacts(path);
166
+ const requestedUrl = artifacts.URL.requestedUrl;
167
+
168
+ if (!requestedUrl) {
169
+ throw new Error('Cannot run audit mode on empty URL');
170
+ }
171
+ } else {
172
+ artifacts = await gatherFn({
173
+ config: options.config,
174
+ driverMock: options.driverMock,
175
+ });
176
+
177
+ // -G means save these to disk (e.g. ./latest-run).
178
+ if (settings.gatherMode) {
179
+ const path = this._getDataSavePath(settings);
180
+ await assetSaver.saveArtifacts(artifacts, path);
181
+ }
182
+ }
183
+
184
+ return artifacts;
185
+ }
186
+
187
187
  /**
188
188
  * This handles both the auditMode case where gatherer entries need to be merged in and
189
189
  * the gather/audit case where timingEntriesFromRunner contains all entries from this run,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "lighthouse",
3
- "version": "9.2.0-dev.20220105",
3
+ "version": "9.2.0-dev.20220109",
4
4
  "description": "Automated auditing, performance metrics, and best practices for the web.",
5
5
  "main": "./lighthouse-core/index.js",
6
6
  "bin": {
@@ -170,7 +170,7 @@
170
170
  "resolve": "^1.20.0",
171
171
  "rollup": "^2.52.7",
172
172
  "rollup-plugin-node-resolve": "^5.2.0",
173
- "rollup-plugin-polyfill-node": "^0.7.0",
173
+ "rollup-plugin-polyfill-node": "^0.8.0",
174
174
  "rollup-plugin-replace": "^2.2.0",
175
175
  "rollup-plugin-shim": "^1.0.0",
176
176
  "rollup-plugin-terser": "^7.0.2",