Skip to content

Commit

Permalink
core(source-maps): use Scripts as a dependency (#15293)
Browse files Browse the repository at this point in the history
  • Loading branch information
adamraine authored Jul 21, 2023
1 parent 42ffe53 commit f60f4e4
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 116 deletions.
8 changes: 3 additions & 5 deletions core/gather/gatherers/scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,11 @@ function isLighthouseRuntimeEvaluateScript(script) {
* @fileoverview Gets JavaScript file contents.
*/
class Scripts extends BaseGatherer {
static symbol = Symbol('Scripts');

/** @type {LH.Gatherer.GathererMeta} */
meta = {
symbol: Scripts.symbol,
supportedModes: ['timespan', 'navigation'],
};

Expand Down Expand Up @@ -91,11 +94,6 @@ class Scripts extends BaseGatherer {

session.off('Debugger.scriptParsed', this.onScriptParsed);

// Without this line the Debugger domain will be off in FR runner,
// because only the legacy gatherer has special handling for multiple,
// overlapped enabled/disable calls.
await session.sendCommand('Debugger.enable');

// If run on a mobile device, be sensitive to memory limitations and only
// request one at a time.
this._scriptContents = await runInSeriesOrParallel(
Expand Down
67 changes: 18 additions & 49 deletions core/gather/gatherers/source-maps.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,18 @@

import SDK from '../../lib/cdt/SDK.js';
import BaseGatherer from '../base-gatherer.js';
import Scripts from './scripts.js';

/**
* @fileoverview Gets JavaScript source maps.
*/
class SourceMaps extends BaseGatherer {
/** @type {LH.Gatherer.GathererMeta} */
/** @type {LH.Gatherer.GathererMeta<'Scripts'>} */
meta = {
supportedModes: ['timespan', 'navigation'],
dependencies: {Scripts: Scripts.symbol},
};

constructor() {
super();
/** @type {LH.Crdp.Debugger.ScriptParsedEvent[]} */
this._scriptParsedEvents = [];
this.onScriptParsed = this.onScriptParsed.bind(this);
}

/**
* @param {LH.Gatherer.Driver} driver
* @param {string} sourceMapUrl
Expand All @@ -45,15 +40,6 @@ class SourceMaps extends BaseGatherer {
return SDK.SourceMap.parseSourceMap(buffer.toString());
}

/**
* @param {LH.Crdp.Debugger.ScriptParsedEvent} event
*/
onScriptParsed(event) {
if (event.sourceMapURL) {
this._scriptParsedEvents.push(event);
}
}

/**
* @param {string} url
* @param {string} base
Expand All @@ -69,27 +55,27 @@ class SourceMaps extends BaseGatherer {

/**
* @param {LH.Gatherer.Driver} driver
* @param {LH.Crdp.Debugger.ScriptParsedEvent} event
* @param {LH.Artifacts.Script} script
* @return {Promise<LH.Artifacts.SourceMap>}
*/
async _retrieveMapFromScriptParsedEvent(driver, event) {
if (!event.sourceMapURL) {
async _retrieveMapFromScript(driver, script) {
if (!script.sourceMapURL) {
throw new Error('precondition failed: event.sourceMapURL should exist');
}

// `sourceMapURL` is simply the URL found in either a magic comment or an x-sourcemap header.
// It has not been resolved to a base url.
const isSourceMapADataUri = event.sourceMapURL.startsWith('data:');
const scriptUrl = event.url;
const isSourceMapADataUri = script.sourceMapURL.startsWith('data:');
const scriptUrl = script.name;
const rawSourceMapUrl = isSourceMapADataUri ?
event.sourceMapURL :
this._resolveUrl(event.sourceMapURL, event.url);
script.sourceMapURL :
this._resolveUrl(script.sourceMapURL, script.name);

if (!rawSourceMapUrl) {
return {
scriptId: event.scriptId,
scriptId: script.scriptId,
scriptUrl,
errorMessage: `Could not resolve map url: ${event.sourceMapURL}`,
errorMessage: `Could not resolve map url: ${script.sourceMapURL}`,
};
}

Expand All @@ -109,14 +95,14 @@ class SourceMaps extends BaseGatherer {
map.sections = map.sections.filter(section => section.map);
}
return {
scriptId: event.scriptId,
scriptId: script.scriptId,
scriptUrl,
sourceMapUrl,
map,
};
} catch (err) {
return {
scriptId: event.scriptId,
scriptId: script.scriptId,
scriptUrl,
sourceMapUrl,
errorMessage: err.toString(),
Expand All @@ -125,30 +111,13 @@ class SourceMaps extends BaseGatherer {
}

/**
* @param {LH.Gatherer.Context} context
*/
async startSensitiveInstrumentation(context) {
const session = context.driver.defaultSession;
session.on('Debugger.scriptParsed', this.onScriptParsed);
await session.sendCommand('Debugger.enable');
}

/**
* @param {LH.Gatherer.Context} context
*/
async stopSensitiveInstrumentation(context) {
const session = context.driver.defaultSession;
await session.sendCommand('Debugger.disable');
session.off('Debugger.scriptParsed', this.onScriptParsed);
}

/**
* @param {LH.Gatherer.Context} context
* @param {LH.Gatherer.Context<'Scripts'>} context
* @return {Promise<LH.Artifacts['SourceMaps']>}
*/
async getArtifact(context) {
const eventProcessPromises = this._scriptParsedEvents
.map((event) => this._retrieveMapFromScriptParsedEvent(context.driver, event));
const eventProcessPromises = context.dependencies.Scripts
.filter(script => script.sourceMapURL)
.map(script => this._retrieveMapFromScript(context.driver, script));
return Promise.all(eventProcessPromises);
}
}
Expand Down
Loading

0 comments on commit f60f4e4

Please sign in to comment.