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(fr): convert source-maps gatherer #12467

Merged
merged 3 commits into from
May 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -30,6 +30,7 @@ const artifacts = {
NetworkUserAgent: '',
PasswordInputsWithPreventedPaste: '',
RobotsTxt: '',
SourceMaps: '',
Stacks: '',
TapTargets: '',
TraceElements: '',
Expand Down Expand Up @@ -69,6 +70,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 @@ -106,6 +108,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');
Copy link
Collaborator

Choose a reason for hiding this comment

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

long-term WDYT about splitting out the script parsed bit as a dependency of this and JsUsage? (I think we took a similar note when we did JsUsage)

Copy link
Member Author

@adamraine adamraine May 11, 2021

Choose a reason for hiding this comment

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

Yeah that makes sense. This might be necessary if we want to run these gatherers in snapshot mode. If the events are sent after Debugger.enable, I'm not sure if we can get a set of events for each gatherer.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm not sure if we can get a set of events for each gatherer.

We'll need the dedicated session creation piece of #11313 for that to work, but we'll be making it work :)

}

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 @@ -34,7 +34,6 @@ declare global {
| 'ResponseCompression'
| 'ScriptElements'
| 'ServiceWorker'
| 'SourceMaps'
| 'TagsBlockingFirstPaint'
| keyof FRBaseArtifacts
>;
Expand Down