-
Notifications
You must be signed in to change notification settings - Fork 9.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
core: add source-maps gatherer (#9101)
- Loading branch information
1 parent
f065def
commit cce65ee
Showing
6 changed files
with
563 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
/** | ||
* @license Copyright 2019 Google Inc. All Rights Reserved. | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. | ||
*/ | ||
'use strict'; | ||
|
||
/** @typedef {import('../driver.js')} Driver */ | ||
|
||
const Gatherer = require('./gatherer.js'); | ||
const URL = require('../../lib/url-shim.js'); | ||
|
||
/** | ||
* This function fetches source maps; it is careful not to parse the response as JSON, as it will | ||
* just need to be serialized again over the protocol, and source maps can | ||
* be huge. | ||
* | ||
* @param {string} url | ||
* @return {Promise<string>} | ||
*/ | ||
/* istanbul ignore next */ | ||
async function fetchSourceMap(url) { | ||
// eslint-disable-next-line no-undef | ||
const response = await fetch(url); | ||
if (response.ok) { | ||
return response.text(); | ||
} else { | ||
throw new Error(`Received status code ${response.status} for ${url}`); | ||
} | ||
} | ||
|
||
/** | ||
* @fileoverview Gets JavaScript source maps. | ||
*/ | ||
class SourceMaps extends Gatherer { | ||
constructor() { | ||
super(); | ||
/** @type {LH.Crdp.Debugger.ScriptParsedEvent[]} */ | ||
this._scriptParsedEvents = []; | ||
this.onScriptParsed = this.onScriptParsed.bind(this); | ||
} | ||
|
||
/** | ||
* @param {Driver} driver | ||
* @param {string} sourceMapUrl | ||
* @return {Promise<LH.Artifacts.RawSourceMap>} | ||
*/ | ||
async fetchSourceMapInPage(driver, sourceMapUrl) { | ||
driver.setNextProtocolTimeout(1500); | ||
/** @type {string} */ | ||
const sourceMapJson = | ||
await driver.evaluateAsync(`(${fetchSourceMap})(${JSON.stringify(sourceMapUrl)})`); | ||
return JSON.parse(sourceMapJson); | ||
} | ||
|
||
/** | ||
* @param {string} sourceMapURL | ||
* @return {LH.Artifacts.RawSourceMap} | ||
*/ | ||
parseSourceMapFromDataUrl(sourceMapURL) { | ||
const buffer = Buffer.from(sourceMapURL.split(',')[1], 'base64'); | ||
return JSON.parse(buffer.toString()); | ||
} | ||
|
||
/** | ||
* @param {LH.Crdp.Debugger.ScriptParsedEvent} event | ||
*/ | ||
onScriptParsed(event) { | ||
if (event.sourceMapURL) { | ||
this._scriptParsedEvents.push(event); | ||
} | ||
} | ||
|
||
/** | ||
* @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 | ||
* @return {string|undefined} | ||
*/ | ||
_resolveUrl(url, base) { | ||
try { | ||
return new URL(url, base).href; | ||
} catch (e) { | ||
return; | ||
} | ||
} | ||
|
||
/** | ||
* @param {Driver} driver | ||
* @param {LH.Crdp.Debugger.ScriptParsedEvent} event | ||
* @return {Promise<LH.Artifacts.SourceMap>} | ||
*/ | ||
async _retrieveMapFromScriptParsedEvent(driver, event) { | ||
if (!event.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 rawSourceMapUrl = isSourceMapADataUri ? | ||
event.sourceMapURL : | ||
this._resolveUrl(event.sourceMapURL, event.url); | ||
|
||
if (!rawSourceMapUrl) { | ||
return { | ||
scriptUrl, | ||
errorMessage: `Could not resolve map url: ${event.sourceMapURL}`, | ||
}; | ||
} | ||
|
||
// sourceMapUrl isn't included in the the artifact if it was a data URL. | ||
const sourceMapUrl = isSourceMapADataUri ? undefined : rawSourceMapUrl; | ||
|
||
try { | ||
const map = isSourceMapADataUri ? | ||
this.parseSourceMapFromDataUrl(rawSourceMapUrl) : | ||
await this.fetchSourceMapInPage(driver, rawSourceMapUrl); | ||
return { | ||
scriptUrl, | ||
sourceMapUrl, | ||
map, | ||
}; | ||
} catch (err) { | ||
return { | ||
scriptUrl, | ||
sourceMapUrl, | ||
errorMessage: err.toString(), | ||
}; | ||
} | ||
} | ||
|
||
/** | ||
* @param {LH.Gatherer.PassContext} passContext | ||
* @return {Promise<LH.Artifacts['SourceMaps']>} | ||
*/ | ||
async afterPass(passContext) { | ||
const driver = passContext.driver; | ||
|
||
driver.off('Debugger.scriptParsed', this.onScriptParsed); | ||
await driver.sendCommand('Debugger.disable'); | ||
|
||
const eventProcessPromises = this._scriptParsedEvents | ||
.map((event) => this._retrieveMapFromScriptParsedEvent(driver, event)); | ||
|
||
return Promise.all(eventProcessPromises); | ||
} | ||
} | ||
|
||
module.exports = SourceMaps; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.