-
Notifications
You must be signed in to change notification settings - Fork 496
/
Copy pathSourceMapManager.ts
223 lines (201 loc) · 8.17 KB
/
SourceMapManager.ts
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import * as Common from '../common/common.js';
import * as Platform from '../platform/platform.js';
import type {FrameAssociated} from './FrameAssociated.js';
import {PageResourceLoader, type PageResourceLoadInitiator} from './PageResourceLoader.js';
import {parseSourceMap, SourceMap, type SourceMapV3} from './SourceMap.js';
import {type Target, Type} from './Target.js';
export class SourceMapManager<T extends FrameAssociated> extends Common.ObjectWrapper.ObjectWrapper<EventTypes<T>> {
readonly #target: Target;
#isEnabled: boolean;
readonly #clientData: Map<T, ClientData>;
readonly #sourceMaps: Map<SourceMap, T>;
#attachingClient: T|null;
constructor(target: Target) {
super();
this.#target = target;
this.#isEnabled = true;
this.#attachingClient = null;
this.#clientData = new Map();
this.#sourceMaps = new Map();
}
setEnabled(isEnabled: boolean): void {
if (isEnabled === this.#isEnabled) {
return;
}
// We need this copy, because `this.#clientData` is getting modified
// in the loop body and trying to iterate over it at the same time
// leads to an infinite loop.
const clientData = [...this.#clientData.entries()];
for (const [client] of clientData) {
this.detachSourceMap(client);
}
this.#isEnabled = isEnabled;
for (const [client, {relativeSourceURL, relativeSourceMapURL}] of clientData) {
this.attachSourceMap(client, relativeSourceURL, relativeSourceMapURL);
}
}
private static getBaseUrl(target: Target|null): Platform.DevToolsPath.UrlString {
while (target && target.type() !== Type.FRAME) {
target = target.parentTarget();
}
return target?.inspectedURL() ?? Platform.DevToolsPath.EmptyUrlString;
}
static resolveRelativeSourceURL(target: Target|null, url: Platform.DevToolsPath.UrlString):
Platform.DevToolsPath.UrlString {
url = Common.ParsedURL.ParsedURL.completeURL(SourceMapManager.getBaseUrl(target), url) ?? url;
return url;
}
sourceMapForClient(client: T): SourceMap|undefined {
return this.#clientData.get(client)?.sourceMap;
}
// This method actively awaits the source map, if still loading.
sourceMapForClientPromise(client: T): Promise<SourceMap|undefined> {
const clientData = this.#clientData.get(client);
if (!clientData) {
return Promise.resolve(undefined);
}
return clientData.sourceMapPromise;
}
clientForSourceMap(sourceMap: SourceMap): T|undefined {
return this.#sourceMaps.get(sourceMap);
}
// TODO(bmeurer): We are lying about the type of |relativeSourceURL| here.
attachSourceMap(
client: T, relativeSourceURL: Platform.DevToolsPath.UrlString, relativeSourceMapURL: string|undefined): void {
if (this.#clientData.has(client)) {
throw new Error('SourceMap is already attached or being attached to client');
}
if (!relativeSourceMapURL) {
return;
}
let clientData: ClientData|null = {
relativeSourceURL,
relativeSourceMapURL,
sourceMap: undefined,
sourceMapPromise: Promise.resolve(undefined),
};
if (this.#isEnabled) {
// The `// #sourceURL=foo` can be a random string, but is generally an absolute path.
// Complete it to inspected page url for relative links.
const sourceURL = SourceMapManager.resolveRelativeSourceURL(this.#target, relativeSourceURL);
const sourceMapURL = Common.ParsedURL.ParsedURL.completeURL(sourceURL, relativeSourceMapURL);
if (sourceMapURL) {
if (this.#attachingClient) {
// This should not happen
console.error('Attaching source map may cancel previously attaching source map');
}
this.#attachingClient = client;
this.dispatchEventToListeners(Events.SourceMapWillAttach, {client});
if (this.#attachingClient === client) {
this.#attachingClient = null;
const initiator = client.createPageResourceLoadInitiator();
clientData.sourceMapPromise =
loadSourceMap(sourceURL, sourceMapURL, initiator)
.then(
({fetchedSourceContent, parsedSourceMap}) => {
const sourceMap = new SourceMap(sourceURL, sourceMapURL, parsedSourceMap, fetchedSourceContent);
if (this.#clientData.get(client) === clientData) {
clientData.sourceMap = sourceMap;
this.#sourceMaps.set(sourceMap, client);
this.dispatchEventToListeners(Events.SourceMapAttached, {client, sourceMap});
}
return sourceMap;
},
() => {
if (this.#clientData.get(client) === clientData) {
this.dispatchEventToListeners(Events.SourceMapFailedToAttach, {client});
}
return undefined;
});
} else {
// Assume cancelAttachSourceMap was called.
if (this.#attachingClient) {
// This should not happen
console.error('Cancelling source map attach because another source map is attaching');
}
clientData = null;
this.dispatchEventToListeners(Events.SourceMapFailedToAttach, {client});
}
}
}
if (clientData) {
this.#clientData.set(client, clientData);
}
}
cancelAttachSourceMap(client: T): void {
if (client === this.#attachingClient) {
this.#attachingClient = null;
} else {
// This should not happen.
if (this.#attachingClient) {
console.error('cancel attach source map requested but a different source map was being attached');
} else {
console.error('cancel attach source map requested but no source map was being attached');
}
}
}
detachSourceMap(client: T): void {
const clientData = this.#clientData.get(client);
if (!clientData) {
return;
}
this.#clientData.delete(client);
if (!this.#isEnabled) {
return;
}
const {sourceMap} = clientData;
if (sourceMap) {
this.#sourceMaps.delete(sourceMap);
this.dispatchEventToListeners(Events.SourceMapDetached, {client, sourceMap});
} else {
this.dispatchEventToListeners(Events.SourceMapFailedToAttach, {client});
}
}
}
async function loadSourceMap(
sourceURL: Platform.DevToolsPath.UrlString,
sourceMapURL: Platform.DevToolsPath.UrlString,
initiator: PageResourceLoadInitiator
): Promise<{fetchedSourceContent: string, parsedSourceMap: SourceMapV3}> {
try {
const [
{content: sourceContent},
{content: sourceMapContent}
] = await Promise.all([
PageResourceLoader.instance().loadResource(sourceURL, initiator),
PageResourceLoader.instance().loadResource(sourceMapURL, initiator)
]);
// TODO: error handling, caching.
return {
fetchedSourceContent: sourceContent,
parsedSourceMap: parseSourceMap(sourceMapContent),
};
} catch (cause) {
throw new Error(`Could not load content for ${sourceMapURL}: ${cause.message}`, {cause});
}
}
interface ClientData {
relativeSourceURL: Platform.DevToolsPath.UrlString;
// Stores the raw sourceMappingURL as provided by V8. These are not guaranteed to
// be valid URLs and will be checked and resolved once `attachSourceMap` is called.
relativeSourceMapURL: string;
sourceMap: SourceMap|undefined;
sourceMapPromise: Promise<SourceMap|undefined>;
}
export enum Events {
/* eslint-disable @typescript-eslint/naming-convention -- Used by web_tests. */
SourceMapWillAttach = 'SourceMapWillAttach',
SourceMapFailedToAttach = 'SourceMapFailedToAttach',
SourceMapAttached = 'SourceMapAttached',
SourceMapDetached = 'SourceMapDetached',
/* eslint-enable @typescript-eslint/naming-convention */
}
export interface EventTypes<T extends FrameAssociated> {
[Events.SourceMapWillAttach]: {client: T};
[Events.SourceMapFailedToAttach]: {client: T};
[Events.SourceMapAttached]: {client: T, sourceMap: SourceMap};
[Events.SourceMapDetached]: {client: T, sourceMap: SourceMap};
}