Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

core(driver): attach to worker targets #14212

Merged
merged 15 commits into from
Sep 11, 2023
16 changes: 7 additions & 9 deletions cli/test/smokehouse/test-definitions/oopif-scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,13 @@ const expectations = {
{url: 'http://localhost:10503/simple-script.js', resourceType: 'Fetch'},
{url: 'http://localhost:10200/simple-worker.js'},
{url: 'http://localhost:10503/simple-worker.js'},
// These requests are emitted in workers so Lighthouse doesn't capture them.
// For some reason, legacy navigations in DevTools still pick them up.
// https://github.com/GoogleChrome/lighthouse/issues/14211
{_legacyOnly: true, _runner: 'devtools', url: 'http://localhost:10200/simple-worker.mjs'},
{_legacyOnly: true, _runner: 'devtools', url: 'http://localhost:10503/simple-worker.mjs'},
{_legacyOnly: true, _runner: 'devtools', url: 'http://localhost:10200/simple-script.js?esm', resourceType: 'Script'},
{_legacyOnly: true, _runner: 'devtools', url: 'http://localhost:10503/simple-script.js?esm', resourceType: 'Script'},
{_legacyOnly: true, _runner: 'devtools', url: 'http://localhost:10200/simple-script.js?importScripts', resourceType: 'Other'},
{_legacyOnly: true, _runner: 'devtools', url: 'http://localhost:10503/simple-script.js?importScripts', resourceType: 'Other'},
// Requests from worker targets
{url: 'http://localhost:10200/simple-worker.mjs'},
{url: 'http://localhost:10503/simple-worker.mjs'},
{url: 'http://localhost:10200/simple-script.js?esm', resourceType: 'Script'},
{url: 'http://localhost:10503/simple-script.js?esm', resourceType: 'Script'},
{url: 'http://localhost:10200/simple-script.js?importScripts', resourceType: 'Other'},
{url: 'http://localhost:10503/simple-script.js?importScripts', resourceType: 'Other'},
],
// Ensure the above is exhaustive (except for favicon, which won't be fetched in devtools/LR).
_excludes: [
Expand Down
17 changes: 9 additions & 8 deletions core/gather/driver/target-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ import {ProtocolSession} from '../session.js';
/** @typedef {LH.Protocol.StrictEventEmitterClass<ProtocolEventMap>} ProtocolEventMessageEmitter */
const ProtocolEventEmitter = /** @type {ProtocolEventMessageEmitter} */ (EventEmitter);

const VALID_TARGET_TYPES = ['page', 'iframe', 'worker'];

/**
* Tracks targets (the page itself, its iframes, their iframes, etc) as they
* appear and allows listeners to the flattened protocol events from all targets.
Expand Down Expand Up @@ -104,19 +106,18 @@ class TargetManager extends ProtocolEventEmitter {
const newSession = new ProtocolSession(cdpSession);

try {
const target = await newSession.sendCommand('Target.getTargetInfo').catch(() => null);
const targetType = target?.targetInfo?.type;
const hasValidTargetType = targetType === 'page' || targetType === 'iframe';
const {targetInfo} = await newSession.sendCommand('Target.getTargetInfo');
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Before M110 this command would throw an error on worker targets. With this patch we would require a Chrome version >=M110 which is the current stable.


// TODO: should detach from target in this case?
// See pptr: https://github.com/puppeteer/puppeteer/blob/733cbecf487c71483bee8350e37030edb24bc021/src/common/Page.ts#L495-L526
if (!target || !hasValidTargetType) return;
if (!VALID_TARGET_TYPES.includes(targetInfo.type)) return;

// No need to continue if target has already been seen.
const targetId = target.targetInfo.targetId;
const targetId = targetInfo.targetId;
if (this._targetIdToTargets.has(targetId)) return;

newSession.setTargetInfo(target.targetInfo);
const targetName = target.targetInfo.url || target.targetInfo.targetId;
newSession.setTargetInfo(targetInfo);
const targetName = targetInfo.url || targetInfo.targetId;
log.verbose('target-manager', `target ${targetName} attached`);

const trueProtocolListener = this._getProtocolEventListener(newSession.id());
Expand All @@ -127,7 +128,7 @@ class TargetManager extends ProtocolEventEmitter {
cdpSession.on('sessionattached', this._onSessionAttached);

const targetWithSession = {
target: target.targetInfo,
target: targetInfo,
cdpSession,
session: newSession,
protocolListener,
Expand Down
6 changes: 3 additions & 3 deletions core/legacy/gather/driver.js
Original file line number Diff line number Diff line change
Expand Up @@ -292,9 +292,9 @@ class Driver {
* @param {LH.Crdp.Target.AttachedToTargetEvent} event
*/
async _handleTargetAttached(event) {
// We're only interested in network requests from iframes for now as those are "part of the page".
// If it's not an iframe, just resume it and move on.
if (event.targetInfo.type !== 'iframe') {
// We're only interested in network requests from iframes and workers for now as those are "part of the page".
// If it's not an iframe or worker, just resume it and move on.
if (event.targetInfo.type !== 'iframe' && event.targetInfo.type !== 'worker') {
// We suspended the target when we auto-attached, so make sure it goes back to being normal.
await this.sendCommandToSession('Runtime.runIfWaitingForDebugger', event.sessionId);
return;
Expand Down