-
Notifications
You must be signed in to change notification settings - Fork 9.4k
/
target-manager.js
284 lines (238 loc) · 9.95 KB
/
target-manager.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
/**
* @license
* Copyright 2021 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
/**
* @fileoverview This class tracks multiple targets (the page itself and its OOPIFs) and allows consumers to
* listen for protocol events before each target is resumed.
*/
import EventEmitter from 'events';
import log from 'lighthouse-logger';
import {ProtocolSession} from '../session.js';
/**
* @typedef {{
* target: LH.Crdp.Target.TargetInfo,
* cdpSession: LH.Puppeteer.CDPSession,
* session: LH.Gatherer.ProtocolSession,
* protocolListener: (event: unknown) => void,
* }} TargetWithSession
*/
// Add protocol event types to EventEmitter.
/** @typedef {{'protocolevent': [LH.Protocol.RawEventMessage]}} ProtocolEventMap */
/** @typedef {LH.Protocol.StrictEventEmitterClass<ProtocolEventMap>} ProtocolEventMessageEmitter */
const ProtocolEventEmitter = /** @type {ProtocolEventMessageEmitter} */ (EventEmitter);
/**
* Tracks targets (the page itself, its iframes, their iframes, etc) as they
* appear and allows listeners to the flattened protocol events from all targets.
*/
class TargetManager extends ProtocolEventEmitter {
/** @param {LH.Puppeteer.CDPSession} cdpSession */
constructor(cdpSession) {
super();
this._enabled = false;
this._rootCdpSession = cdpSession;
this._mainFrameId = '';
/**
* A map of target id to target/session information. Used to ensure unique
* attached targets.
* @type {Map<string, TargetWithSession>}
*/
this._targetIdToTargets = new Map();
/** @type {Map<string, LH.Crdp.Runtime.ExecutionContextDescription>} */
this._executionContextIdToDescriptions = new Map();
this._onSessionAttached = this._onSessionAttached.bind(this);
this._onFrameNavigated = this._onFrameNavigated.bind(this);
this._onExecutionContextCreated = this._onExecutionContextCreated.bind(this);
this._onExecutionContextDestroyed = this._onExecutionContextDestroyed.bind(this);
this._onExecutionContextsCleared = this._onExecutionContextsCleared.bind(this);
}
/**
* @param {LH.Crdp.Page.FrameNavigatedEvent} frameNavigatedEvent
*/
async _onFrameNavigated(frameNavigatedEvent) {
// Child frames are handled in `_onSessionAttached`.
if (frameNavigatedEvent.frame.parentId) return;
if (!this._enabled) return;
// It's not entirely clear when this is necessary, but when the page switches processes on
// navigating from about:blank to the `requestedUrl`, resetting `setAutoAttach` has been
// necessary in the past.
try {
await this._rootCdpSession.send('Target.setAutoAttach', {
autoAttach: true,
flatten: true,
waitForDebuggerOnStart: true,
});
} catch (err) {
// The page can be closed at the end of the run before this CDP function returns.
// In these cases, just ignore the error since we won't need the page anyway.
if (this._enabled) throw err;
}
}
/**
* @param {string} sessionId
* @return {LH.Gatherer.ProtocolSession}
*/
_findSession(sessionId) {
for (const {session, cdpSession} of this._targetIdToTargets.values()) {
if (cdpSession.id() === sessionId) return session;
}
throw new Error(`session ${sessionId} not found`);
}
/**
* @param {string} targetType
* @return {targetType is LH.Protocol.TargetType}
*/
_isAcceptedTargetType(targetType) {
return targetType === 'page' ||
targetType === 'iframe' ||
targetType === 'worker';
}
/**
* Returns the root session.
* @return {LH.Gatherer.ProtocolSession}
*/
rootSession() {
const rootSessionId = this._rootCdpSession.id();
return this._findSession(rootSessionId);
}
mainFrameExecutionContexts() {
return [...this._executionContextIdToDescriptions.values()].filter(executionContext => {
return executionContext.auxData.frameId === this._mainFrameId;
});
}
/**
* @param {LH.Puppeteer.CDPSession} cdpSession
*/
async _onSessionAttached(cdpSession) {
const newSession = new ProtocolSession(cdpSession);
let targetType;
try {
const {targetInfo} = await newSession.sendCommand('Target.getTargetInfo');
targetType = targetInfo.type;
// TODO: should detach from target in this case?
// See pptr: https://github.com/puppeteer/puppeteer/blob/733cbecf487c71483bee8350e37030edb24bc021/src/common/Page.ts#L495-L526
if (!this._isAcceptedTargetType(targetType)) return;
// No need to continue if target has already been seen.
const targetId = targetInfo.targetId;
if (this._targetIdToTargets.has(targetId)) return;
newSession.setTargetInfo(targetInfo);
const targetName = targetInfo.url || targetInfo.targetId;
log.verbose('target-manager', `target ${targetName} attached`);
const trueProtocolListener = this._getProtocolEventListener(targetType, newSession.id());
/** @type {(event: unknown) => void} */
// @ts-expect-error - pptr currently typed only for single arg emits.
const protocolListener = trueProtocolListener;
cdpSession.on('*', protocolListener);
cdpSession.on('sessionattached', this._onSessionAttached);
const targetWithSession = {
target: targetInfo,
cdpSession,
session: newSession,
protocolListener,
};
this._targetIdToTargets.set(targetId, targetWithSession);
// We want to receive information about network requests from iframes, so enable the Network domain.
await newSession.sendCommand('Network.enable');
// We also want to receive information about subtargets of subtargets, so make sure we autoattach recursively.
await newSession.sendCommand('Target.setAutoAttach', {
autoAttach: true,
flatten: true,
waitForDebuggerOnStart: true,
});
} catch (err) {
// Sometimes targets can be closed before we even have a chance to listen to their network activity.
if (/Target closed/.test(err.message)) return;
// `Target.getTargetInfo` is not implemented for certain target types.
// Lighthouse isn't interested in these targets anyway so we can just ignore them.
if (/'Target.getTargetInfo' wasn't found/.test(err)) return;
// Worker targets can be a bit fickle and we only enable them for diagnostic purposes.
// We shouldn't throw a fatal error if there were issues attaching to them.
if (targetType === 'worker') {
log.warn('target-manager', `Issue attaching to worker target: ${err}`);
return;
}
throw err;
} finally {
// Resume the target if it was paused, but if it's unnecessary, we don't care about the error.
await newSession.sendCommandAndIgnore('Runtime.runIfWaitingForDebugger');
}
}
/**
* @param {LH.Crdp.Runtime.ExecutionContextCreatedEvent} event
*/
_onExecutionContextCreated(event) {
if (event.context.name.match(/^__puppeteer_utility_world__/)) return;
if (event.context.name === 'lighthouse_isolated_context') return;
this._executionContextIdToDescriptions.set(event.context.uniqueId, event.context);
}
/**
* @param {LH.Crdp.Runtime.ExecutionContextDestroyedEvent} event
*/
_onExecutionContextDestroyed(event) {
this._executionContextIdToDescriptions.delete(event.executionContextUniqueId);
}
_onExecutionContextsCleared() {
this._executionContextIdToDescriptions.clear();
}
/**
* Returns a listener for all protocol events from session, and augments the
* event with the sessionId.
* @param {LH.Protocol.TargetType} targetType
* @param {string} sessionId
*/
_getProtocolEventListener(targetType, sessionId) {
/**
* @template {keyof LH.Protocol.RawEventMessageRecord} EventName
* @param {EventName} method
* @param {LH.Protocol.RawEventMessageRecord[EventName]['params']} params
*/
const onProtocolEvent = (method, params) => {
// Cast because tsc 4.7 still can't quite track the dependent parameters.
const payload = /** @type {LH.Protocol.RawEventMessage} */ (
{method, params, targetType, sessionId});
this.emit('protocolevent', payload);
};
return onProtocolEvent;
}
/**
* @return {Promise<void>}
*/
async enable() {
if (this._enabled) return;
this._enabled = true;
this._targetIdToTargets = new Map();
this._executionContextIdToDescriptions = new Map();
this._rootCdpSession.on('Page.frameNavigated', this._onFrameNavigated);
this._rootCdpSession.on('Runtime.executionContextCreated', this._onExecutionContextCreated);
this._rootCdpSession.on('Runtime.executionContextDestroyed', this._onExecutionContextDestroyed);
this._rootCdpSession.on('Runtime.executionContextsCleared', this._onExecutionContextsCleared);
await this._rootCdpSession.send('Page.enable');
await this._rootCdpSession.send('Runtime.enable');
this._mainFrameId = (await this._rootCdpSession.send('Page.getFrameTree')).frameTree.frame.id;
// Start with the already attached root session.
await this._onSessionAttached(this._rootCdpSession);
}
/**
* @return {Promise<void>}
*/
async disable() {
this._rootCdpSession.off('Page.frameNavigated', this._onFrameNavigated);
this._rootCdpSession.off('Runtime.executionContextCreated', this._onExecutionContextCreated);
this._rootCdpSession.off('Runtime.executionContextDestroyed',
this._onExecutionContextDestroyed);
this._rootCdpSession.off('Runtime.executionContextsCleared', this._onExecutionContextsCleared);
for (const {cdpSession, protocolListener} of this._targetIdToTargets.values()) {
cdpSession.off('*', protocolListener);
cdpSession.off('sessionattached', this._onSessionAttached);
}
// Ignore failures on these in case the tab has crashed.
await this._rootCdpSession.send('Page.disable').catch(_ => {});
await this._rootCdpSession.send('Runtime.disable').catch(_ => {});
this._enabled = false;
this._targetIdToTargets = new Map();
this._executionContextIdToDescriptions = new Map();
this._mainFrameId = '';
}
}
export {TargetManager};