forked from facebook/react
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathsymbolicateSource.js
More file actions
160 lines (142 loc) · 4.72 KB
/
symbolicateSource.js
File metadata and controls
160 lines (142 loc) · 4.72 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
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/
import SourceMapConsumer from 'react-devtools-shared/src/hooks/SourceMapConsumer';
import type {ReactFunctionLocation} from 'shared/ReactTypes';
import type {FetchFileWithCaching} from 'react-devtools-shared/src/devtools/views/Components/FetchFileWithCachingContext';
const symbolicationCache: Map<
string,
Promise<SourceMappedLocation | null>,
> = new Map();
export type SourceMappedLocation = {
location: ReactFunctionLocation,
ignored: boolean, // Whether the file for this location was ignore listed
};
export function symbolicateSourceWithCache(
fetchFileWithCaching: FetchFileWithCaching,
sourceURL: string,
line: number, // 1-based
column: number, // 1-based
): Promise<SourceMappedLocation | null> {
const key = `${sourceURL}:${line}:${column}`;
const cachedPromise = symbolicationCache.get(key);
if (cachedPromise != null) {
return cachedPromise;
}
const promise = symbolicateSource(
fetchFileWithCaching,
sourceURL,
line,
column,
);
symbolicationCache.set(key, promise);
return promise;
}
const SOURCE_MAP_ANNOTATION_PREFIX = 'sourceMappingURL=';
export async function symbolicateSource(
fetchFileWithCaching: FetchFileWithCaching,
sourceURL: string,
lineNumber: number, // 1-based
columnNumber: number, // 1-based
): Promise<SourceMappedLocation | null> {
if (!sourceURL || sourceURL.startsWith('<anonymous')) {
return null;
}
const resource = await fetchFileWithCaching(sourceURL).catch(() => null);
if (resource == null) {
return null;
}
const resourceLines = resource.split(/[\r\n]+/);
for (let i = resourceLines.length - 1; i >= 0; --i) {
const resourceLine = resourceLines[i];
// In case there is empty last line
if (!resourceLine) continue;
// Not an annotation? Stop looking for a source mapping url.
if (!resourceLine.startsWith('//#')) break;
if (resourceLine.includes(SOURCE_MAP_ANNOTATION_PREFIX)) {
const sourceMapAnnotationStartIndex = resourceLine.indexOf(
SOURCE_MAP_ANNOTATION_PREFIX,
);
const sourceMapAt = resourceLine.slice(
sourceMapAnnotationStartIndex + SOURCE_MAP_ANNOTATION_PREFIX.length,
resourceLine.length,
);
// Compute the absolute source map URL. If the base URL is invalid, gracefully bail.
let sourceMapURL;
try {
sourceMapURL = new URL(sourceMapAt, sourceURL).toString();
} catch (e) {
// Fallback: try if sourceMapAt is already an absolute URL; otherwise give up.
try {
sourceMapURL = new URL(sourceMapAt).toString();
} catch (_e) {
return null;
}
}
const sourceMap = await fetchFileWithCaching(sourceMapURL).catch(
() => null,
);
if (sourceMap != null) {
try {
const parsedSourceMap = JSON.parse(sourceMap);
const consumer = SourceMapConsumer(parsedSourceMap);
const functionName = ''; // TODO: Parse function name from sourceContent.
const {
sourceURL: possiblyURL,
line,
column: columnZeroBased,
ignored,
} = consumer.originalPositionFor({
lineNumber, // 1-based
columnNumber, // 1-based
});
const column = columnZeroBased + 1;
if (possiblyURL === null) {
return null;
}
try {
// sourceMapURL = https://react.dev/script.js.map
void new URL(possiblyURL); // test if it is a valid URL
return {
location: [functionName, possiblyURL, line, column],
ignored,
};
} catch (e) {
// This is not valid URL
if (
// sourceMapURL = /file
possiblyURL.startsWith('/') ||
// sourceMapURL = C:\\...
possiblyURL.slice(1).startsWith(':\\\\')
) {
// This is an absolute path
return {
location: [functionName, possiblyURL, line, column],
ignored,
};
}
// This is a relative path
// possiblyURL = x.js.map, sourceMapURL = https://react.dev/script.js.map
const absoluteSourcePath = new URL(
possiblyURL,
sourceMapURL,
).toString();
return {
location: [functionName, absoluteSourcePath, line, column],
ignored,
};
}
} catch (e) {
return null;
}
}
return null;
}
}
return null;
}