-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
234 lines (211 loc) · 6.24 KB
/
index.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
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
'use strict';
// https://github.com/evanw/node-source-map-support/blob/master/source-map-support.js
const { readFileSync } = require('fs');
const { fileURLToPath } = require('url');
const path = require('path');
const { SourceMapConsumer } = require('./source_map');
function readFile(pathname) {
if (pathname.startsWith('file:')) {
pathname = fileURLToPath(pathname);
}
return readFileSync(pathname, 'utf8');
}
const cache = new Map();
function supportRelativeURL(file, url) {
if (!file) {
return url;
}
const dir = path.dirname(file);
const match = /^\w+:\/\/[^/]*/.exec(dir);
let protocol = match ? match[0] : '';
const startPath = dir.slice(protocol.length);
if (protocol && /^\/\w:/.test(startPath)) {
// handle file:///C:/ paths
protocol += '/';
return protocol + path.resolve(dir.slice(protocol.length), url).replace(/\\/g, '/');
}
return protocol + path.resolve(dir.slice(protocol.length), url);
}
function getSourceMap(pathname, source) {
if (cache.has(pathname)) {
return cache.get(pathname);
}
try {
if (!source) {
source = readFile(pathname);
}
const re = /(?:\/\/[@#][ \t]+sourceMappingURL=([^\s'"]+?)[ \t]*$)|(?:\/\*[@#][ \t]+sourceMappingURL=([^*]+?)[ \t]*(?:\*\/)[ \t]*$)/mg;
let lastMatch;
while (true) { // eslint-disable-line no-constant-condition
const match = re.exec(source);
if (!match) {
break;
}
lastMatch = match;
}
if (!lastMatch) {
cache.set(pathname, null);
return null;
}
const r = supportRelativeURL(pathname, lastMatch[1]);
const sourceMap = new SourceMapConsumer(readFile(r), r);
cache.set(pathname, sourceMap);
if (sourceMap.sourcesContent) {
sourceMap.sources.forEach((s, i) => {
const contents = sourceMap.sourcesContent[i];
if (contents) {
const url = supportRelativeURL(sourceMap.url, s);
getSourceMap(url, contents);
}
});
}
return sourceMap;
} catch {
cache.set(pathname, null);
return null;
}
}
function getSourceMapPosition(position) {
const sourceMap = getSourceMap(position.source);
if (sourceMap) {
const originalPosition = sourceMap.originalPositionFor(position);
if (originalPosition !== null) {
return originalPosition;
}
}
return position;
}
function getEvalOrigin(frame) {
const origin = frame.getEvalOrigin();
if (origin) {
let match = /^eval at ([^(]+) \((.+):(\d+):(\d+)\)$/.exec(origin);
if (match) {
const position = getSourceMapPosition({
source: match[2],
line: Number.parseInt(match[3], 10),
column: Number.parseInt(match[4], 10) - 1,
});
return `eval at ${match[1]} (${position.source}:${position.line}:${position.column + 1})`;
}
match = /^eval at ([^(]+) \((.+)\)$/.exec(origin);
if (match) {
return `eval at ${match[1]} (${getEvalOrigin(match[2])})`;
}
}
return null;
}
function frameToString(frame, position, evalOrigin) {
// JSStackFrame::ToString
const isTopLevel = frame.isToplevel();
const isAsync = frame.isAsync && frame.isAsync();
const isPromiseAll = frame.isPromiseAll && frame.isPromiseAll();
const isConstructor = frame.isConstructor();
const isMethodCall = !(isTopLevel || isConstructor);
const functionName = (position && position.name) || frame.getFunctionName();
// AppendFileLocation
const locationInfo = () => {
const fileName = position ? position.source : null;
let out = '';
if (!fileName && frame.isEval()) {
out += `${evalOrigin}, `;
}
if (fileName) {
out += fileName;
} else {
out += '<anonymous>';
}
const lineNumber = position ? position.line : frame.getLineNumber();
if (lineNumber !== -1) {
out += `:${lineNumber}`;
const columnNumber = position ? position.column : frame.getColumnNumber();
if (columnNumber !== -1) {
out += `:${columnNumber}`;
}
}
return out;
};
let string = isAsync ? 'async ' : '';
if (isPromiseAll) {
string += `Promise.all (index ${frame.getPromiseIndex()})`;
return string;
}
if (isMethodCall) {
// AppendMethodCall
const typeName = frame.getTypeName();
const methodName = frame.getMethodName();
if (functionName) {
if (typeName) {
const startsWithTypeName = functionName.startsWith(typeName);
if (!startsWithTypeName) {
string += `${typeName}.`;
}
}
string += functionName;
if (methodName) {
// StringEndsWithMethodName(functionName, methodName);
if (functionName !== methodName && !functionName.endsWith(`.${methodName}`)) {
string += ` [as ${methodName}]`;
}
}
} else {
if (typeName) {
string += `${typeName}.`;
}
if (methodName) {
string += methodName;
} else {
string += '<anonymous>';
}
}
} else if (isConstructor) {
string += 'new ';
if (functionName) {
string += functionName;
} else {
string += '<anonymous>';
}
} else if (functionName) {
string += functionName;
} else {
return `${string}${locationInfo()}`;
}
return `${string} (${locationInfo()})`;
}
function getMappedString(frame) {
if (frame.isNative()) {
return frame.toString();
}
const source = frame.getFileName() || frame.getScriptNameOrSourceURL();
if (source) {
const position = getSourceMapPosition({
source,
line: frame.getLineNumber(),
column: frame.getColumnNumber() - 1,
});
if (position) {
return frameToString(frame, position, null);
}
}
if (frame.isEval()) {
const evalOrigin = getEvalOrigin(frame);
if (evalOrigin !== null) {
return frameToString(frame, null, evalOrigin);
}
}
return frame.toString();
}
module.exports = () => {
const originalPrepareStackTrace = Error.prepareStackTrace;
Error.prepareStackTrace = (error, stack) => {
const errString = Error.prototype.toString.call(error);
const frames = stack.map((frame) => `\n at ${getMappedString(frame)}`);
return `${errString}${frames.join('')}`;
};
return () => {
if (originalPrepareStackTrace) {
Error.prepareStackTrace = originalPrepareStackTrace;
} else {
delete Error.prepareStackTrace;
}
};
};