Skip to content

Commit

Permalink
core(fr): convert source-maps gatherer (#12467)
Browse files Browse the repository at this point in the history
  • Loading branch information
adamraine authored and paulirish committed May 12, 2021
1 parent 3aeb5c6 commit f4c3b8b
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 27 deletions.
3 changes: 3 additions & 0 deletions lighthouse-core/fraggle-rock/config/default-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const artifacts = {
NetworkUserAgent: '',
PasswordInputsWithPreventedPaste: '',
RobotsTxt: '',
SourceMaps: '',
Stacks: '',
TapTargets: '',
TraceElements: '',
Expand Down Expand Up @@ -71,6 +72,7 @@ const defaultConfig = {
{id: artifacts.NetworkUserAgent, gatherer: 'network-user-agent'},
{id: artifacts.PasswordInputsWithPreventedPaste, gatherer: 'dobetterweb/password-inputs-with-prevented-paste'},
{id: artifacts.RobotsTxt, gatherer: 'seo/robots-txt'},
{id: artifacts.SourceMaps, gatherer: 'source-maps'},
{id: artifacts.Stacks, gatherer: 'stacks'},
{id: artifacts.TapTargets, gatherer: 'seo/tap-targets'},
{id: artifacts.TraceElements, gatherer: 'trace-elements'},
Expand Down Expand Up @@ -109,6 +111,7 @@ const defaultConfig = {
artifacts.NetworkUserAgent,
artifacts.PasswordInputsWithPreventedPaste,
artifacts.RobotsTxt,
artifacts.SourceMaps,
artifacts.Stacks,
artifacts.TapTargets,
artifacts.TraceElements,
Expand Down
53 changes: 30 additions & 23 deletions lighthouse-core/gather/gatherers/source-maps.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,18 @@
*/
'use strict';

/** @typedef {import('../driver.js')} Driver */

const Gatherer = require('./gatherer.js');
const FRGatherer = require('../../fraggle-rock/gather/base-gatherer.js');
const URL = require('../../lib/url-shim.js');

/**
* @fileoverview Gets JavaScript source maps.
*/
class SourceMaps extends Gatherer {
class SourceMaps extends FRGatherer {
/** @type {LH.Gatherer.GathererMeta} */
meta = {
supportedModes: ['timespan', 'navigation'],
}

constructor() {
super();
/** @type {LH.Crdp.Debugger.ScriptParsedEvent[]} */
Expand All @@ -22,7 +25,7 @@ class SourceMaps extends Gatherer {
}

/**
* @param {Driver} driver
* @param {LH.Gatherer.FRTransitionalDriver} driver
* @param {string} sourceMapUrl
* @return {Promise<LH.Artifacts.RawSourceMap>}
*/
Expand Down Expand Up @@ -52,15 +55,6 @@ class SourceMaps extends Gatherer {
}
}

/**
* @param {LH.Gatherer.PassContext} passContext
*/
async beforePass(passContext) {
const driver = passContext.driver;
driver.on('Debugger.scriptParsed', this.onScriptParsed);
await driver.sendCommand('Debugger.enable');
}

/**
* @param {string} url
* @param {string} base
Expand All @@ -75,7 +69,7 @@ class SourceMaps extends Gatherer {
}

/**
* @param {Driver} driver
* @param {LH.Gatherer.FRTransitionalDriver} driver
* @param {LH.Crdp.Debugger.ScriptParsedEvent} event
* @return {Promise<LH.Artifacts.SourceMap>}
*/
Expand Down Expand Up @@ -124,18 +118,31 @@ class SourceMaps extends Gatherer {
}

/**
* @param {LH.Gatherer.PassContext} passContext
* @return {Promise<LH.Artifacts['SourceMaps']>}
* @param {LH.Gatherer.FRTransitionalContext} context
*/
async afterPass(passContext) {
const driver = passContext.driver;
async startSensitiveInstrumentation(context) {
const session = context.driver.defaultSession;
session.on('Debugger.scriptParsed', this.onScriptParsed);
await session.sendCommand('Debugger.enable');
}

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

await driver.fetcher.enable();
/**
* @param {LH.Gatherer.FRTransitionalContext} context
* @return {Promise<LH.Artifacts['SourceMaps']>}
*/
async getArtifact(context) {
await context.driver.fetcher.enable();
const eventProcessPromises = this._scriptParsedEvents
.map((event) => this._retrieveMapFromScriptParsedEvent(driver, event));
.map((event) => this._retrieveMapFromScriptParsedEvent(context.driver, event));
return Promise.all(eventProcessPromises);
}
}
Expand Down
14 changes: 11 additions & 3 deletions lighthouse-core/test/gather/gatherers/source-maps-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const Driver = require('../../../gather/driver.js');
const Connection = require('../../../gather/connections/connection.js');
const SourceMaps = require('../../../gather/gatherers/source-maps.js');
const {createMockSendCommandFn, createMockOnFn} = require('../mock-commands.js');
const {flushAllTimersAndMicrotasks} = require('../../test-utils.js');

const mapJson = JSON.stringify({
version: 3,
Expand Down Expand Up @@ -89,10 +90,17 @@ describe('SourceMaps gatherer', () => {
driver.fetcher.fetchResource = fetchMock;

const sourceMaps = new SourceMaps();
await sourceMaps.beforePass({driver});

await sourceMaps.startInstrumentation({driver});
await sourceMaps.startSensitiveInstrumentation({driver});

// Needed for protocol events to emit.
jest.advanceTimersByTime(1);
return sourceMaps.afterPass({driver});
await flushAllTimersAndMicrotasks(1);

await sourceMaps.stopSensitiveInstrumentation({driver});
await sourceMaps.stopInstrumentation({driver});

return sourceMaps.getArtifact({driver});
}

function makeJsonDataUrl(data) {
Expand Down
1 change: 0 additions & 1 deletion types/artifacts.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ declare global {
| 'ResponseCompression'
| 'ScriptElements'
| 'ServiceWorker'
| 'SourceMaps'
| 'TagsBlockingFirstPaint'
| keyof FRBaseArtifacts
>;
Expand Down

0 comments on commit f4c3b8b

Please sign in to comment.