forked from GoogleChrome/lighthouse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsource-maps.js
150 lines (134 loc) · 4.61 KB
/
source-maps.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/**
* @license Copyright 2019 The Lighthouse Authors. 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';
const FRGatherer = require('../../fraggle-rock/gather/base-gatherer.js');
const URL = require('../../lib/url-shim.js');
/**
* @fileoverview Gets JavaScript source maps.
*/
class SourceMaps extends FRGatherer {
/** @type {LH.Gatherer.GathererMeta} */
meta = {
supportedModes: ['timespan', 'navigation'],
}
constructor() {
super();
/** @type {LH.Crdp.Debugger.ScriptParsedEvent[]} */
this._scriptParsedEvents = [];
this.onScriptParsed = this.onScriptParsed.bind(this);
}
/**
* @param {LH.Gatherer.FRTransitionalDriver} driver
* @param {string} sourceMapUrl
* @return {Promise<LH.Artifacts.RawSourceMap>}
*/
async fetchSourceMap(driver, sourceMapUrl) {
const response = await driver.fetcher.fetchResource(sourceMapUrl, {timeout: 1500});
if (response.content === null) {
throw new Error(`Failed fetching source map (${response.status})`);
}
return JSON.parse(response.content);
}
/**
* @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 {string} url
* @param {string} base
* @return {string|undefined}
*/
_resolveUrl(url, base) {
try {
return new URL(url, base).href;
} catch (e) {
return;
}
}
/**
* @param {LH.Gatherer.FRTransitionalDriver} 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.fetchSourceMap(driver, rawSourceMapUrl);
if (map.sections) {
map.sections = map.sections.filter(section => section.map);
}
return {
scriptUrl,
sourceMapUrl,
map,
};
} catch (err) {
return {
scriptUrl,
sourceMapUrl,
errorMessage: err.toString(),
};
}
}
/**
* @param {LH.Gatherer.FRTransitionalContext} context
*/
async startSensitiveInstrumentation(context) {
const session = context.driver.defaultSession;
session.on('Debugger.scriptParsed', this.onScriptParsed);
await session.sendCommand('Debugger.enable');
}
/**
* @param {LH.Gatherer.FRTransitionalContext} context
*/
async stopSensitiveInstrumentation(context) {
const session = context.driver.defaultSession;
await session.sendCommand('Debugger.disable');
session.off('Debugger.scriptParsed', this.onScriptParsed);
}
/**
* @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(context.driver, event));
return Promise.all(eventProcessPromises);
}
}
module.exports = SourceMaps;