forked from firefox-devtools/debugger
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnewSources.js
More file actions
380 lines (333 loc) · 10.6 KB
/
newSources.js
File metadata and controls
380 lines (333 loc) · 10.6 KB
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at <http://mozilla.org/MPL/2.0/>. */
// @flow
/**
* Redux actions for the sources state
* @module actions/sources
*/
import { generatedToOriginalId } from "devtools-source-map";
import { flatten } from "lodash";
import {
stringToSourceActorId,
type SourceActor
} from "../../reducers/source-actors";
import { insertSourceActors } from "../../actions/source-actors";
import { makeSourceId } from "../../client/firefox/create";
import { toggleBlackBox } from "./blackbox";
import { syncBreakpoint } from "../breakpoints";
import { loadSourceText } from "./loadSourceText";
import { togglePrettyPrint } from "./prettyPrint";
import { selectLocation, setBreakableLines } from "../sources";
import {
getRawSourceURL,
isPrettyURL,
isOriginal,
isUrlExtension,
isInlineScript
} from "../../utils/source";
import {
getBlackBoxList,
getSource,
getSourceFromId,
hasSourceActor,
getPendingSelectedLocation,
getPendingBreakpointsForSource,
getContext,
isSourceLoadingOrLoaded
} from "../../selectors";
import { prefs } from "../../utils/prefs";
import sourceQueue from "../../utils/source-queue";
import { validateNavigateContext, ContextError } from "../../utils/context";
import type {
Source,
SourceId,
Context,
OriginalSourceData,
GeneratedSourceData,
QueuedSourceData
} from "../../types";
import type { Action, ThunkArgs } from "../types";
function loadSourceMaps(cx: Context, sources: Source[]) {
return async function({
dispatch,
sourceMaps
}: ThunkArgs): Promise<?(Promise<Source>[])> {
try {
const sourceList = await Promise.all(
sources.map(async ({ id }) => {
const originalSources = await dispatch(loadSourceMap(cx, id));
sourceQueue.queueSources(
originalSources.map(data => ({
type: "original",
data
}))
);
return originalSources;
})
);
await sourceQueue.flush();
// We would like to sync breakpoints after we are done
// loading source maps as sometimes generated and original
// files share the same paths.
for (const source of sources) {
dispatch(checkPendingBreakpoints(cx, source.id));
}
return flatten(sourceList);
} catch (error) {
if (!(error instanceof ContextError)) {
throw error;
}
}
};
}
/**
* @memberof actions/sources
* @static
*/
function loadSourceMap(cx: Context, sourceId: SourceId) {
return async function({
dispatch,
getState,
sourceMaps
}: ThunkArgs): Promise<Source[]> {
const source = getSource(getState(), sourceId);
if (
!prefs.clientSourceMapsEnabled ||
!source ||
isOriginal(source) ||
!source.sourceMapURL
) {
return [];
}
let urls = null;
try {
// Unable to correctly type the result of a spread on a union type.
// See https://github.com/facebook/flow/pull/7298
const urlInfo: Source = { ...(source: any) };
if (!urlInfo.url && typeof urlInfo.introductionUrl === "string") {
// If the source was dynamically generated (via eval, dynamically
// created script elements, and so forth), it won't have a URL, so that
// it is not collapsed into other sources from the same place. The
// introduction URL will include the point it was constructed at,
// however, so use that for resolving any source maps in the source.
(urlInfo: any).url = urlInfo.introductionUrl;
}
urls = await sourceMaps.getOriginalURLs(urlInfo);
} catch (e) {
console.error(e);
}
if (!urls) {
// If this source doesn't have a sourcemap, enable it for pretty printing
dispatch(
({
type: "CLEAR_SOURCE_MAP_URL",
cx,
sourceId
}: Action)
);
return [];
}
validateNavigateContext(getState(), cx);
return urls.map(url => ({
id: generatedToOriginalId(source.id, url),
url
}));
};
}
// If a request has been made to show this source, go ahead and
// select it.
function checkSelectedSource(cx: Context, sourceId: string) {
return async ({ dispatch, getState }: ThunkArgs) => {
const source = getSource(getState(), sourceId);
const pendingLocation = getPendingSelectedLocation(getState());
if (!pendingLocation || !pendingLocation.url || !source || !source.url) {
return;
}
const pendingUrl = pendingLocation.url;
const rawPendingUrl = getRawSourceURL(pendingUrl);
if (rawPendingUrl === source.url) {
if (isPrettyURL(pendingUrl)) {
const prettySource = await dispatch(togglePrettyPrint(cx, source.id));
return dispatch(checkPendingBreakpoints(cx, prettySource.id));
}
await dispatch(
selectLocation(cx, {
sourceId: source.id,
line:
typeof pendingLocation.line === "number" ? pendingLocation.line : 0,
column: pendingLocation.column
})
);
}
};
}
function checkPendingBreakpoints(cx: Context, sourceId: string) {
return async ({ dispatch, getState }: ThunkArgs) => {
// source may have been modified by selectLocation
const source = getSource(getState(), sourceId);
if (!source) {
return;
}
const pendingBreakpoints = getPendingBreakpointsForSource(
getState(),
source
);
if (pendingBreakpoints.length === 0) {
return;
}
// load the source text if there is a pending breakpoint for it
await dispatch(loadSourceText({ cx, source }));
await Promise.all(
pendingBreakpoints.map(bp => {
return dispatch(syncBreakpoint(cx, sourceId, bp));
})
);
};
}
function restoreBlackBoxedSources(cx: Context, sources: Source[]) {
return async ({ dispatch }: ThunkArgs) => {
const tabs = getBlackBoxList();
if (tabs.length == 0) {
return;
}
for (const source of sources) {
if (tabs.includes(source.url) && !source.isBlackBoxed) {
dispatch(toggleBlackBox(cx, source));
}
}
};
}
export function newQueuedSources(sourceInfo: Array<QueuedSourceData>) {
return async ({ dispatch }: ThunkArgs) => {
const generated = [];
const original = [];
for (const source of sourceInfo) {
if (source.type === "generated") {
generated.push(source.data);
} else {
original.push(source.data);
}
}
if (generated.length > 0) {
await dispatch(newGeneratedSources(generated));
}
if (original.length > 0) {
await dispatch(newOriginalSources(original));
}
};
}
export function newOriginalSource(sourceInfo: OriginalSourceData) {
return async ({ dispatch }: ThunkArgs) => {
const sources = await dispatch(newOriginalSources([sourceInfo]));
return sources[0];
};
}
export function newOriginalSources(sourceInfo: Array<OriginalSourceData>) {
return async ({ dispatch, getState }: ThunkArgs) => {
const sources: Array<Source> = sourceInfo.map(({ id, url }) => ({
id,
url,
relativeUrl: url,
isPrettyPrinted: false,
isWasm: false,
isBlackBoxed: false,
introductionUrl: null,
introductionType: undefined,
isExtension: false
}));
const cx = getContext(getState());
dispatch(addSources(cx, sources));
await dispatch(checkNewSources(cx, sources));
return sources;
};
}
export function newGeneratedSource(sourceInfo: GeneratedSourceData) {
return async ({ dispatch }: ThunkArgs) => {
const sources = await dispatch(newGeneratedSources([sourceInfo]));
return sources[0];
};
}
export function newGeneratedSources(sourceInfo: Array<GeneratedSourceData>) {
return async ({
dispatch,
getState,
client
}: ThunkArgs): Promise<Array<Source>> => {
const supportsWasm = client.hasWasmSupport();
const resultIds = [];
const newSourcesObj = {};
const newSourceActors: Array<SourceActor> = [];
for (const { thread, source, id } of sourceInfo) {
const newId = id || makeSourceId(source);
if (!getSource(getState(), newId) && !newSourcesObj[newId]) {
newSourcesObj[newId] = ({
id: newId,
url: source.url,
relativeUrl: source.url,
isPrettyPrinted: false,
sourceMapURL: source.sourceMapURL,
introductionUrl: source.introductionUrl,
introductionType: source.introductionType,
isBlackBoxed: false,
isWasm: !!supportsWasm && source.introductionType === "wasm",
isExtension: (source.url && isUrlExtension(source.url)) || false
}: any);
}
const actorId = stringToSourceActorId(source.actor);
// We are sometimes notified about a new source multiple times if we
// request a new source list and also get a source event from the server.
if (!hasSourceActor(getState(), actorId)) {
newSourceActors.push({
id: actorId,
actor: source.actor,
thread,
source: newId,
isBlackBoxed: source.isBlackBoxed,
sourceMapURL: source.sourceMapURL,
url: source.url,
introductionUrl: source.introductionUrl,
introductionType: source.introductionType
});
}
resultIds.push(newId);
}
const newSources: Array<Source> = (Object.values(newSourcesObj): Array<
any
>);
const cx = getContext(getState());
dispatch(addSources(cx, newSources));
dispatch(insertSourceActors(newSourceActors));
for (const newSourceActor of newSourceActors) {
// Fetch breakable lines for new HTML scripts
// when the HTML file has started loading
if (
isInlineScript(newSourceActor) &&
isSourceLoadingOrLoaded(getState(), newSourceActor.source)
) {
dispatch(setBreakableLines(cx, newSourceActor.source)).catch(error => {
if (!(error instanceof ContextError)) {
throw error;
}
});
}
}
await dispatch(checkNewSources(cx, newSources));
return resultIds.map(id => getSourceFromId(getState(), id));
};
}
function addSources(cx, sources: Array<Source>) {
return ({ dispatch, getState }: ThunkArgs) => {
dispatch({ type: "ADD_SOURCES", cx, sources });
};
}
function checkNewSources(cx, sources: Source[]) {
return async ({ dispatch, getState }: ThunkArgs) => {
for (const source of sources) {
dispatch(checkSelectedSource(cx, source.id));
}
dispatch(restoreBlackBoxedSources(cx, sources));
dispatch(loadSourceMaps(cx, sources));
return sources;
};
}