lighthouse 9.5.0-dev.20220608 → 9.5.0-dev.20220609

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.
package/esm-utils.mjs ADDED
@@ -0,0 +1,40 @@
1
+ /**
2
+ * @license Copyright 2021 The Lighthouse Authors. All Rights Reserved.
3
+ * 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
4
+ * 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.
5
+ */
6
+ 'use strict';
7
+ // TODO(esmodules): rename to .js when root is type: module
8
+
9
+ import module from 'module';
10
+ import url from 'url';
11
+ import path from 'path';
12
+
13
+ /**
14
+ * Commonjs equivalent of `require.resolve`.
15
+ * @param {string} packageName
16
+ */
17
+ function resolveModulePath(packageName) {
18
+ const require = module.createRequire(import.meta.url);
19
+ return require.resolve(packageName);
20
+ }
21
+
22
+ /**
23
+ * @param {ImportMeta} importMeta
24
+ */
25
+ function getModulePath(importMeta) {
26
+ return url.fileURLToPath(importMeta.url);
27
+ }
28
+
29
+ /**
30
+ * @param {ImportMeta} importMeta
31
+ */
32
+ function getModuleDirectory(importMeta) {
33
+ return path.dirname(getModulePath(importMeta));
34
+ }
35
+
36
+ export {
37
+ resolveModulePath,
38
+ getModulePath,
39
+ getModuleDirectory,
40
+ };
@@ -26,8 +26,6 @@ const throwingSession = {
26
26
  off: throwNotConnectedFn,
27
27
  addProtocolMessageListener: throwNotConnectedFn,
28
28
  removeProtocolMessageListener: throwNotConnectedFn,
29
- addSessionAttachedListener: throwNotConnectedFn,
30
- removeSessionAttachedListener: throwNotConnectedFn,
31
29
  sendCommand: throwNotConnectedFn,
32
30
  dispose: throwNotConnectedFn,
33
31
  };
@@ -27,6 +27,7 @@ class ProtocolSession {
27
27
 
28
28
  sessionId() {
29
29
  return this._targetInfo && this._targetInfo.type === 'iframe' ?
30
+ // TODO: use this._session.id() for real session id.
30
31
  this._targetInfo.targetId :
31
32
  undefined;
32
33
  }
@@ -77,27 +78,6 @@ class ProtocolSession {
77
78
  this._cdpSession.once(eventName, /** @type {*} */ (callback));
78
79
  }
79
80
 
80
- /**
81
- * Bind to the puppeteer `sessionattached` listener and return an LH ProtocolSession.
82
- * @param {(session: ProtocolSession) => void} callback
83
- */
84
- addSessionAttachedListener(callback) {
85
- /** @param {LH.Puppeteer.CDPSession} session */
86
- const listener = session => callback(new ProtocolSession(session));
87
- this._callbackMap.set(callback, listener);
88
- this._getConnection().on('sessionattached', listener);
89
- }
90
-
91
- /**
92
- * Unbind to the puppeteer `sessionattached` listener.
93
- * @param {(session: ProtocolSession) => void} callback
94
- */
95
- removeSessionAttachedListener(callback) {
96
- const listener = this._callbackMap.get(callback);
97
- if (!listener) return;
98
- this._getConnection().off('sessionattached', listener);
99
- }
100
-
101
81
  /**
102
82
  * Bind to puppeteer's '*' event that fires for *any* protocol event,
103
83
  * and wrap it with data about the protocol message instead of just the event.
@@ -172,12 +152,6 @@ class ProtocolSession {
172
152
  this._cdpSession.removeAllListeners();
173
153
  await this._cdpSession.detach();
174
154
  }
175
-
176
- _getConnection() {
177
- const connection = this._cdpSession.connection();
178
- if (!connection) throw new Error('Connection has been closed.');
179
- return connection;
180
- }
181
155
  }
182
156
 
183
157
  module.exports = ProtocolSession;
@@ -90,7 +90,10 @@ class NetworkMonitor {
90
90
  this._frameNavigations = [];
91
91
  this._sessions = new Map();
92
92
  this._networkRecorder = new NetworkRecorder();
93
- this._targetManager = new TargetManager(this._session);
93
+ /** @type {LH.Puppeteer.CDPSession} */
94
+ // @ts-expect-error - temporarily reach in to get CDPSession
95
+ const rootCdpSession = this._session._cdpSession;
96
+ this._targetManager = new TargetManager(rootCdpSession);
94
97
 
95
98
  /**
96
99
  * Reemit the same network recorder events.
@@ -11,21 +11,23 @@
11
11
  */
12
12
 
13
13
  const log = require('lighthouse-logger');
14
+ const ProtocolSession = require('../../fraggle-rock/gather/session.js');
14
15
 
15
16
  /** @typedef {{target: LH.Crdp.Target.TargetInfo, session: LH.Gatherer.FRProtocolSession}} TargetWithSession */
16
17
 
17
18
  class TargetManager {
18
- /** @param {LH.Gatherer.FRProtocolSession} session */
19
- constructor(session) {
19
+ /** @param {LH.Puppeteer.CDPSession} cdpSession */
20
+ constructor(cdpSession) {
20
21
  this._enabled = false;
21
- this._session = session;
22
+ this._rootCdpSession = cdpSession;
23
+
22
24
  /** @type {Array<(targetWithSession: TargetWithSession) => Promise<void>|void>} */
23
25
  this._listeners = [];
24
26
 
25
27
  this._onSessionAttached = this._onSessionAttached.bind(this);
26
28
 
27
29
  /** @type {Map<string, TargetWithSession>} */
28
- this._targets = new Map();
30
+ this._targetIdToTargets = new Map();
29
31
 
30
32
  /** @param {LH.Crdp.Page.FrameNavigatedEvent} event */
31
33
  this._onFrameNavigated = async event => {
@@ -35,7 +37,7 @@ class TargetManager {
35
37
  // It's not entirely clear when this is necessary, but when the page switches processes on
36
38
  // navigating from about:blank to the `requestedUrl`, resetting `setAutoAttach` has been
37
39
  // necessary in the past.
38
- await this._session.sendCommand('Target.setAutoAttach', {
40
+ await this._rootCdpSession.send('Target.setAutoAttach', {
39
41
  autoAttach: true,
40
42
  flatten: true,
41
43
  waitForDebuggerOnStart: true,
@@ -44,9 +46,11 @@ class TargetManager {
44
46
  }
45
47
 
46
48
  /**
47
- * @param {LH.Gatherer.FRProtocolSession} session
49
+ * @param {LH.Puppeteer.CDPSession} cdpSession
48
50
  */
49
- async _onSessionAttached(session) {
51
+ async _onSessionAttached(cdpSession) {
52
+ const session = new ProtocolSession(cdpSession);
53
+
50
54
  try {
51
55
  const target = await session.sendCommand('Target.getTargetInfo').catch(() => null);
52
56
  const targetType = target?.targetInfo?.type;
@@ -55,13 +59,13 @@ class TargetManager {
55
59
 
56
60
  const targetId = target.targetInfo.targetId;
57
61
  session.setTargetInfo(target.targetInfo);
58
- if (this._targets.has(targetId)) return;
62
+ if (this._targetIdToTargets.has(targetId)) return;
59
63
 
60
64
  const targetName = target.targetInfo.url || target.targetInfo.targetId;
61
65
  log.verbose('target-manager', `target ${targetName} attached`);
62
66
 
63
67
  const targetWithSession = {target: target.targetInfo, session};
64
- this._targets.set(targetId, targetWithSession);
68
+ this._targetIdToTargets.set(targetId, targetWithSession);
65
69
  for (const listener of this._listeners) await listener(targetWithSession);
66
70
 
67
71
  await session.sendCommand('Target.setAutoAttach', {
@@ -87,24 +91,32 @@ class TargetManager {
87
91
  if (this._enabled) return;
88
92
 
89
93
  this._enabled = true;
90
- this._targets = new Map();
94
+ this._targetIdToTargets = new Map();
95
+
96
+ this._rootCdpSession.on('Page.frameNavigated', this._onFrameNavigated);
97
+
98
+ const rootConnection = this._rootCdpSession.connection();
99
+ if (!rootConnection) {
100
+ throw new Error('Connection has been closed.');
101
+ }
102
+ rootConnection.on('sessionattached', this._onSessionAttached);
91
103
 
92
- this._session.on('Page.frameNavigated', this._onFrameNavigated);
93
- this._session.addSessionAttachedListener(this._onSessionAttached);
104
+ await this._rootCdpSession.send('Page.enable');
94
105
 
95
- await this._session.sendCommand('Page.enable');
96
- await this._onSessionAttached(this._session);
106
+ // Start with the already attached root session.
107
+ await this._onSessionAttached(this._rootCdpSession);
97
108
  }
98
109
 
99
110
  /**
100
111
  * @return {Promise<void>}
101
112
  */
102
113
  async disable() {
103
- this._session.off('Page.frameNavigated', this._onFrameNavigated);
104
- this._session.removeSessionAttachedListener(this._onSessionAttached);
114
+ this._rootCdpSession.off('Page.frameNavigated', this._onFrameNavigated);
115
+ // No need to remove listener if connection is already closed.
116
+ this._rootCdpSession.connection()?.off('sessionattached', this._onSessionAttached);
105
117
 
106
118
  this._enabled = false;
107
- this._targets = new Map();
119
+ this._targetIdToTargets = new Map();
108
120
  }
109
121
 
110
122
  /**
@@ -12,7 +12,7 @@ const {fetchResponseBodyFromCache} = require('../gather/driver/network.js');
12
12
  const EventEmitter = require('events').EventEmitter;
13
13
 
14
14
  const log = require('lighthouse-logger');
15
- const DevtoolsLog = require('./devtools-log.js');
15
+ const DevtoolsMessageLog = require('./gatherers/devtools-log.js').DevtoolsMessageLog;
16
16
  const TraceGatherer = require('./gatherers/trace.js');
17
17
 
18
18
  // Pulled in for Connection type checking.
@@ -41,7 +41,7 @@ class Driver {
41
41
  * @private
42
42
  * Used to save network and lifecycle protocol traffic. Just Page and Network are needed.
43
43
  */
44
- _devtoolsLog = new DevtoolsLog(/^(Page|Network)\./);
44
+ _devtoolsLog = new DevtoolsMessageLog(/^(Page|Network)\./);
45
45
 
46
46
  /**
47
47
  * @private
@@ -208,16 +208,6 @@ class Driver {
208
208
  // OOPIF handling in legacy driver is implicit.
209
209
  }
210
210
 
211
- /** @param {(session: LH.Gatherer.FRProtocolSession) => void} callback */
212
- addSessionAttachedListener(callback) { // eslint-disable-line no-unused-vars
213
- // OOPIF handling in legacy driver is implicit.
214
- }
215
-
216
- /** @param {(session: LH.Gatherer.FRProtocolSession) => void} callback */
217
- removeSessionAttachedListener(callback) { // eslint-disable-line no-unused-vars
218
- // OOPIF handling in legacy driver is implicit.
219
- }
220
-
221
211
  /**
222
212
  * Debounce enabling or disabling domains to prevent driver users from
223
213
  * stomping on each other. Maintains an internal count of the times a domain
@@ -12,7 +12,6 @@
12
12
  */
13
13
 
14
14
  const NetworkMonitor = require('../driver/network-monitor.js');
15
- const MessageLog = require('../devtools-log.js');
16
15
  const FRGatherer = require('../../fraggle-rock/gather/base-gatherer.js');
17
16
 
18
17
  class DevtoolsLog extends FRGatherer {
@@ -30,7 +29,7 @@ class DevtoolsLog extends FRGatherer {
30
29
  /** @type {NetworkMonitor|undefined} */
31
30
  this._networkMonitor = undefined;
32
31
 
33
- this._messageLog = new MessageLog(/^(Page|Network|Target|Runtime)\./);
32
+ this._messageLog = new DevtoolsMessageLog(/^(Page|Network|Target|Runtime)\./);
34
33
 
35
34
  /** @param {LH.Protocol.RawEventMessage} e */
36
35
  this._onProtocolMessage = e => this._messageLog.record(e);
@@ -63,4 +62,59 @@ class DevtoolsLog extends FRGatherer {
63
62
  }
64
63
  }
65
64
 
65
+
66
+ /**
67
+ * This class saves all protocol messages whose method match a particular
68
+ * regex filter. Used when saving assets for later analysis by another tool such as
69
+ * Webpagetest.
70
+ */
71
+ class DevtoolsMessageLog {
72
+ /**
73
+ * @param {RegExp=} regexFilter
74
+ */
75
+ constructor(regexFilter) {
76
+ this._filter = regexFilter;
77
+
78
+ /** @type {LH.DevtoolsLog} */
79
+ this._messages = [];
80
+ this._isRecording = false;
81
+ }
82
+
83
+ /**
84
+ * @return {LH.DevtoolsLog}
85
+ */
86
+ get messages() {
87
+ return this._messages;
88
+ }
89
+
90
+ reset() {
91
+ this._messages = [];
92
+ }
93
+
94
+ beginRecording() {
95
+ this._isRecording = true;
96
+ }
97
+
98
+ endRecording() {
99
+ this._isRecording = false;
100
+ }
101
+
102
+ /**
103
+ * Records a message if method matches filter and recording has been started.
104
+ * @param {LH.Protocol.RawEventMessage} message
105
+ */
106
+ record(message) {
107
+ // We're not recording, skip the rest of the checks.
108
+ if (!this._isRecording) return;
109
+ // The event was likely an internal puppeteer method that uses Symbols.
110
+ if (typeof message.method !== 'string') return;
111
+ // The event didn't pass our filter, do not record it.
112
+ if (this._filter && !this._filter.test(message.method)) return;
113
+
114
+ // We passed all the checks, record the message.
115
+ this._messages.push(message);
116
+ }
117
+ }
118
+
66
119
  module.exports = DevtoolsLog;
120
+ module.exports.DevtoolsMessageLog = DevtoolsMessageLog;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "lighthouse",
3
- "version": "9.5.0-dev.20220608",
3
+ "version": "9.5.0-dev.20220609",
4
4
  "description": "Automated auditing, performance metrics, and best practices for the web.",
5
5
  "main": "./lighthouse-core/index.js",
6
6
  "bin": {
package/tsconfig.json CHANGED
@@ -30,6 +30,7 @@
30
30
  "third-party/snyk/snapshot.json",
31
31
  "lighthouse-core/audits/byte-efficiency/polyfill-graph-data.json",
32
32
  "shared/localization/locales/en-US.json",
33
+ "esm-utils.mjs",
33
34
  ],
34
35
  "exclude": [
35
36
  "lighthouse-core/test/audits/**/*.js",
@@ -46,11 +47,11 @@
46
47
  "lighthouse-core/test/config/config-test.js",
47
48
  "lighthouse-core/test/config/default-config-test.js",
48
49
  "lighthouse-core/test/create-test-trace.js",
49
- "lighthouse-core/test/gather/devtools-log-test.js",
50
50
  "lighthouse-core/test/gather/driver/wait-for-condition-test.js",
51
51
  "lighthouse-core/test/gather/gatherers/accessibility-test.js",
52
52
  "lighthouse-core/test/gather/gatherers/cache-contents-test.js",
53
53
  "lighthouse-core/test/gather/gatherers/console-messages-test.js",
54
+ "lighthouse-core/test/gather/gatherers/devtools-log-test.js",
54
55
  "lighthouse-core/test/gather/gatherers/dobetterweb/optimized-images-test.js",
55
56
  "lighthouse-core/test/gather/gatherers/dobetterweb/password-inputs-with-prevented-paste-test.js",
56
57
  "lighthouse-core/test/gather/gatherers/dobetterweb/response-compression-test.js",
@@ -30,8 +30,6 @@ declare module Gatherer {
30
30
  once<TEvent extends keyof LH.CrdpEvents>(event: TEvent, callback: (...args: LH.CrdpEvents[TEvent]) => void): void;
31
31
  addProtocolMessageListener(callback: (payload: Protocol.RawEventMessage) => void): void
32
32
  removeProtocolMessageListener(callback: (payload: Protocol.RawEventMessage) => void): void
33
- addSessionAttachedListener(callback: (session: FRProtocolSession) => void): void
34
- removeSessionAttachedListener(callback: (session: FRProtocolSession) => void): void
35
33
  off<TEvent extends keyof LH.CrdpEvents>(event: TEvent, callback: (...args: LH.CrdpEvents[TEvent]) => void): void;
36
34
  sendCommand<TMethod extends keyof LH.CrdpCommands>(method: TMethod, ...params: LH.CrdpCommands[TMethod]['paramsType']): Promise<LH.CrdpCommands[TMethod]['returnType']>;
37
35
  dispose(): Promise<void>;
@@ -1,61 +0,0 @@
1
- /**
2
- * @license Copyright 2017 The Lighthouse Authors. All Rights Reserved.
3
- * 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
4
- * 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.
5
- */
6
- 'use strict';
7
-
8
- /**
9
- * @fileoverview This class saves all protocol messages whose method match a particular
10
- * regex filter. Used when saving assets for later analysis by another tool such as
11
- * Webpagetest.
12
- */
13
- class DevtoolsLog {
14
- /**
15
- * @param {RegExp=} regexFilter
16
- */
17
- constructor(regexFilter) {
18
- this._filter = regexFilter;
19
-
20
- /** @type {LH.DevtoolsLog} */
21
- this._messages = [];
22
- this._isRecording = false;
23
- }
24
-
25
- /**
26
- * @return {LH.DevtoolsLog}
27
- */
28
- get messages() {
29
- return this._messages;
30
- }
31
-
32
- reset() {
33
- this._messages = [];
34
- }
35
-
36
- beginRecording() {
37
- this._isRecording = true;
38
- }
39
-
40
- endRecording() {
41
- this._isRecording = false;
42
- }
43
-
44
- /**
45
- * Records a message if method matches filter and recording has been started.
46
- * @param {LH.Protocol.RawEventMessage} message
47
- */
48
- record(message) {
49
- // We're not recording, skip the rest of the checks.
50
- if (!this._isRecording) return;
51
- // The event was likely an internal puppeteer method that uses Symbols.
52
- if (typeof message.method !== 'string') return;
53
- // The event didn't pass our filter, do not record it.
54
- if (this._filter && !this._filter.test(message.method)) return;
55
-
56
- // We passed all the checks, record the message.
57
- this._messages.push(message);
58
- }
59
- }
60
-
61
- module.exports = DevtoolsLog;