error-less 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +204 -0
- package/dist/index.d.mts +112 -0
- package/dist/index.d.ts +112 -0
- package/dist/index.js +854 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +802 -0
- package/dist/index.mjs.map +1 -0
- package/dist/register-BG47xgjJ.d.mts +160 -0
- package/dist/register-BG47xgjJ.d.ts +160 -0
- package/dist/register.d.mts +1 -0
- package/dist/register.d.ts +1 -0
- package/dist/register.js +763 -0
- package/dist/register.js.map +1 -0
- package/dist/register.mjs +737 -0
- package/dist/register.mjs.map +1 -0
- package/package.json +80 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,854 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var path = require('path');
|
|
6
|
+
var fs2 = require('fs');
|
|
7
|
+
var pc = require('picocolors');
|
|
8
|
+
|
|
9
|
+
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
|
10
|
+
|
|
11
|
+
function _interopNamespace(e) {
|
|
12
|
+
if (e && e.__esModule) return e;
|
|
13
|
+
var n = Object.create(null);
|
|
14
|
+
if (e) {
|
|
15
|
+
Object.keys(e).forEach(function (k) {
|
|
16
|
+
if (k !== 'default') {
|
|
17
|
+
var d = Object.getOwnPropertyDescriptor(e, k);
|
|
18
|
+
Object.defineProperty(n, k, d.get ? d : {
|
|
19
|
+
enumerable: true,
|
|
20
|
+
get: function () { return e[k]; }
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
n.default = e;
|
|
26
|
+
return Object.freeze(n);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
var path__namespace = /*#__PURE__*/_interopNamespace(path);
|
|
30
|
+
var fs2__namespace = /*#__PURE__*/_interopNamespace(fs2);
|
|
31
|
+
var pc__default = /*#__PURE__*/_interopDefault(pc);
|
|
32
|
+
|
|
33
|
+
// src/types.ts
|
|
34
|
+
var defaultConfig = {
|
|
35
|
+
contextLinesBefore: 2,
|
|
36
|
+
contextLinesAfter: 2,
|
|
37
|
+
enableSourceMaps: true,
|
|
38
|
+
filterNodeModules: true,
|
|
39
|
+
frameFilter: () => true,
|
|
40
|
+
showFullStack: false,
|
|
41
|
+
colors: true
|
|
42
|
+
};
|
|
43
|
+
var INTERNAL_PATTERNS = [
|
|
44
|
+
/^node:/,
|
|
45
|
+
/^internal\//,
|
|
46
|
+
/^events\.js$/,
|
|
47
|
+
/^_stream_/,
|
|
48
|
+
/^stream\.js$/,
|
|
49
|
+
/^module\.js$/,
|
|
50
|
+
/^loader\.js$/,
|
|
51
|
+
/^vm\.js$/
|
|
52
|
+
];
|
|
53
|
+
function isInternalPath(filePath) {
|
|
54
|
+
if (!filePath) return true;
|
|
55
|
+
if (filePath === "native") return true;
|
|
56
|
+
for (const pattern of INTERNAL_PATTERNS) {
|
|
57
|
+
if (pattern.test(filePath)) return true;
|
|
58
|
+
}
|
|
59
|
+
if (filePath.includes("internal/") || filePath.includes("internal\\")) {
|
|
60
|
+
return true;
|
|
61
|
+
}
|
|
62
|
+
return false;
|
|
63
|
+
}
|
|
64
|
+
function isNodeModulesPath(filePath) {
|
|
65
|
+
if (!filePath) return false;
|
|
66
|
+
const normalized = filePath.replace(/\\/g, "/");
|
|
67
|
+
return normalized.includes("/node_modules/") || normalized.includes("\\node_modules\\");
|
|
68
|
+
}
|
|
69
|
+
function getDisplayFunctionName(callSite) {
|
|
70
|
+
const functionName = callSite.getFunctionName();
|
|
71
|
+
const methodName = callSite.getMethodName();
|
|
72
|
+
const typeName = callSite.getTypeName();
|
|
73
|
+
if (functionName) {
|
|
74
|
+
if (typeName && functionName !== typeName) {
|
|
75
|
+
return `${typeName}.${functionName}`;
|
|
76
|
+
}
|
|
77
|
+
return functionName;
|
|
78
|
+
}
|
|
79
|
+
if (methodName) {
|
|
80
|
+
if (typeName) {
|
|
81
|
+
return `${typeName}.${methodName}`;
|
|
82
|
+
}
|
|
83
|
+
return methodName;
|
|
84
|
+
}
|
|
85
|
+
if (typeName) {
|
|
86
|
+
return `${typeName}.<anonymous>`;
|
|
87
|
+
}
|
|
88
|
+
return null;
|
|
89
|
+
}
|
|
90
|
+
function parseCallSite(callSite) {
|
|
91
|
+
try {
|
|
92
|
+
let fileName = callSite.getFileName() ?? callSite.getScriptNameOrSourceURL();
|
|
93
|
+
const lineNumber = callSite.getLineNumber();
|
|
94
|
+
const columnNumber = callSite.getColumnNumber();
|
|
95
|
+
const isNative = callSite.isNative();
|
|
96
|
+
if (!fileName && !isNative) {
|
|
97
|
+
return null;
|
|
98
|
+
}
|
|
99
|
+
if (fileName == null ? void 0 : fileName.startsWith("file:")) {
|
|
100
|
+
try {
|
|
101
|
+
fileName = new URL(fileName).pathname;
|
|
102
|
+
if (process.platform === "win32" && /^\/[A-Z]:/.test(fileName)) {
|
|
103
|
+
fileName = fileName.slice(1);
|
|
104
|
+
}
|
|
105
|
+
} catch {
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
let filePath = fileName ?? "native";
|
|
109
|
+
if (fileName && !path__namespace.isAbsolute(fileName) && !fileName.startsWith("node:")) {
|
|
110
|
+
try {
|
|
111
|
+
filePath = path__namespace.resolve(process.cwd(), fileName);
|
|
112
|
+
} catch {
|
|
113
|
+
filePath = fileName;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
const isInternal = isInternalPath(filePath);
|
|
117
|
+
const isNodeModules = isNodeModulesPath(filePath);
|
|
118
|
+
const isUserCode = !isInternal && !isNodeModules && !isNative;
|
|
119
|
+
return {
|
|
120
|
+
filePath,
|
|
121
|
+
lineNumber: lineNumber ?? 0,
|
|
122
|
+
columnNumber: columnNumber ?? 0,
|
|
123
|
+
functionName: callSite.getFunctionName(),
|
|
124
|
+
methodName: callSite.getMethodName(),
|
|
125
|
+
typeName: callSite.getTypeName(),
|
|
126
|
+
isNative,
|
|
127
|
+
isNodeModules,
|
|
128
|
+
isInternal,
|
|
129
|
+
isUserCode,
|
|
130
|
+
callSite
|
|
131
|
+
};
|
|
132
|
+
} catch {
|
|
133
|
+
return null;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
function parseStackTrace(structuredStack) {
|
|
137
|
+
const frames = [];
|
|
138
|
+
for (const callSite of structuredStack) {
|
|
139
|
+
const parsed = parseCallSite(callSite);
|
|
140
|
+
if (parsed) {
|
|
141
|
+
frames.push(parsed);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
return frames;
|
|
145
|
+
}
|
|
146
|
+
function findUserCodeFrame(frames) {
|
|
147
|
+
for (const frame of frames) {
|
|
148
|
+
if (frame.isUserCode && frame.lineNumber > 0) {
|
|
149
|
+
return frame;
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
for (const frame of frames) {
|
|
153
|
+
if (!frame.isInternal && !frame.isNative && frame.lineNumber > 0) {
|
|
154
|
+
return frame;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
for (const frame of frames) {
|
|
158
|
+
if (frame.lineNumber > 0) {
|
|
159
|
+
return frame;
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
return null;
|
|
163
|
+
}
|
|
164
|
+
function formatFrameAsStackLine(frame) {
|
|
165
|
+
const funcName = getDisplayFunctionName(frame.callSite) ?? "<anonymous>";
|
|
166
|
+
const location = frame.isNative ? "native" : `${frame.filePath}:${frame.lineNumber}:${frame.columnNumber}`;
|
|
167
|
+
return ` at ${funcName} (${location})`;
|
|
168
|
+
}
|
|
169
|
+
function getRelativePath(absolutePath) {
|
|
170
|
+
try {
|
|
171
|
+
const cwd = process.cwd();
|
|
172
|
+
if (absolutePath.startsWith(cwd)) {
|
|
173
|
+
const relative2 = path__namespace.relative(cwd, absolutePath);
|
|
174
|
+
if (!relative2.startsWith(".")) {
|
|
175
|
+
return "./" + relative2.replace(/\\/g, "/");
|
|
176
|
+
}
|
|
177
|
+
return relative2.replace(/\\/g, "/");
|
|
178
|
+
}
|
|
179
|
+
} catch {
|
|
180
|
+
}
|
|
181
|
+
return absolutePath.replace(/\\/g, "/");
|
|
182
|
+
}
|
|
183
|
+
var fileCache = /* @__PURE__ */ new Map();
|
|
184
|
+
var MAX_FILE_SIZE = 10 * 1024 * 1024;
|
|
185
|
+
var MAX_CACHED_FILES = 50;
|
|
186
|
+
function clearFileCache() {
|
|
187
|
+
fileCache.clear();
|
|
188
|
+
}
|
|
189
|
+
function readFileLines(filePath) {
|
|
190
|
+
const cached = fileCache.get(filePath);
|
|
191
|
+
if (cached) {
|
|
192
|
+
return cached;
|
|
193
|
+
}
|
|
194
|
+
try {
|
|
195
|
+
const stats = fs2__namespace.statSync(filePath);
|
|
196
|
+
if (stats.size > MAX_FILE_SIZE) {
|
|
197
|
+
return null;
|
|
198
|
+
}
|
|
199
|
+
const content = fs2__namespace.readFileSync(filePath, "utf-8");
|
|
200
|
+
const lines = content.split(/\r?\n/);
|
|
201
|
+
if (fileCache.size >= MAX_CACHED_FILES) {
|
|
202
|
+
const firstKey = fileCache.keys().next().value;
|
|
203
|
+
if (firstKey) {
|
|
204
|
+
fileCache.delete(firstKey);
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
fileCache.set(filePath, lines);
|
|
208
|
+
return lines;
|
|
209
|
+
} catch {
|
|
210
|
+
return null;
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
function getCodeContext(filePath, lineNumber, columnNumber, linesBefore = 2, linesAfter = 2) {
|
|
214
|
+
const lines = readFileLines(filePath);
|
|
215
|
+
if (!lines || lines.length === 0) {
|
|
216
|
+
return null;
|
|
217
|
+
}
|
|
218
|
+
const errorLineIndex = lineNumber - 1;
|
|
219
|
+
if (errorLineIndex < 0 || errorLineIndex >= lines.length) {
|
|
220
|
+
return null;
|
|
221
|
+
}
|
|
222
|
+
const startIndex = Math.max(0, errorLineIndex - linesBefore);
|
|
223
|
+
const endIndex = Math.min(lines.length - 1, errorLineIndex + linesAfter);
|
|
224
|
+
const contextLines = [];
|
|
225
|
+
for (let i = startIndex; i <= endIndex; i++) {
|
|
226
|
+
contextLines.push({
|
|
227
|
+
lineNumber: i + 1,
|
|
228
|
+
// Convert back to 1-indexed
|
|
229
|
+
content: lines[i] ?? "",
|
|
230
|
+
isErrorLine: i === errorLineIndex
|
|
231
|
+
});
|
|
232
|
+
}
|
|
233
|
+
const errorLineContent = lines[errorLineIndex] ?? "";
|
|
234
|
+
return {
|
|
235
|
+
lines: contextLines,
|
|
236
|
+
errorColumn: columnNumber,
|
|
237
|
+
errorLineContent
|
|
238
|
+
};
|
|
239
|
+
}
|
|
240
|
+
function getSourceContentFromMap(sourceContent, lineNumber, columnNumber, linesBefore = 2, linesAfter = 2) {
|
|
241
|
+
if (!sourceContent) {
|
|
242
|
+
return null;
|
|
243
|
+
}
|
|
244
|
+
const lines = sourceContent.split(/\r?\n/);
|
|
245
|
+
const errorLineIndex = lineNumber - 1;
|
|
246
|
+
if (errorLineIndex < 0 || errorLineIndex >= lines.length) {
|
|
247
|
+
return null;
|
|
248
|
+
}
|
|
249
|
+
const startIndex = Math.max(0, errorLineIndex - linesBefore);
|
|
250
|
+
const endIndex = Math.min(lines.length - 1, errorLineIndex + linesAfter);
|
|
251
|
+
const contextLines = [];
|
|
252
|
+
for (let i = startIndex; i <= endIndex; i++) {
|
|
253
|
+
contextLines.push({
|
|
254
|
+
lineNumber: i + 1,
|
|
255
|
+
content: lines[i] ?? "",
|
|
256
|
+
isErrorLine: i === errorLineIndex
|
|
257
|
+
});
|
|
258
|
+
}
|
|
259
|
+
return {
|
|
260
|
+
lines: contextLines,
|
|
261
|
+
errorColumn: columnNumber,
|
|
262
|
+
errorLineContent: lines[errorLineIndex] ?? ""
|
|
263
|
+
};
|
|
264
|
+
}
|
|
265
|
+
function getContextForFrame(frame, sourceMapResult, linesBefore = 2, linesAfter = 2) {
|
|
266
|
+
if (sourceMapResult == null ? void 0 : sourceMapResult.sourceContent) {
|
|
267
|
+
const context = getSourceContentFromMap(
|
|
268
|
+
sourceMapResult.sourceContent,
|
|
269
|
+
sourceMapResult.line,
|
|
270
|
+
sourceMapResult.column,
|
|
271
|
+
linesBefore,
|
|
272
|
+
linesAfter
|
|
273
|
+
);
|
|
274
|
+
if (context) {
|
|
275
|
+
return context;
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
if (sourceMapResult == null ? void 0 : sourceMapResult.source) {
|
|
279
|
+
const compiledDir = path__namespace.dirname(frame.filePath);
|
|
280
|
+
const sourcePath = path__namespace.resolve(compiledDir, sourceMapResult.source);
|
|
281
|
+
const context = getCodeContext(
|
|
282
|
+
sourcePath,
|
|
283
|
+
sourceMapResult.line,
|
|
284
|
+
sourceMapResult.column,
|
|
285
|
+
linesBefore,
|
|
286
|
+
linesAfter
|
|
287
|
+
);
|
|
288
|
+
if (context) {
|
|
289
|
+
return context;
|
|
290
|
+
}
|
|
291
|
+
if (path__namespace.isAbsolute(sourceMapResult.source)) {
|
|
292
|
+
const absContext = getCodeContext(
|
|
293
|
+
sourceMapResult.source,
|
|
294
|
+
sourceMapResult.line,
|
|
295
|
+
sourceMapResult.column,
|
|
296
|
+
linesBefore,
|
|
297
|
+
linesAfter
|
|
298
|
+
);
|
|
299
|
+
if (absContext) {
|
|
300
|
+
return absContext;
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
return getCodeContext(
|
|
305
|
+
frame.filePath,
|
|
306
|
+
frame.lineNumber,
|
|
307
|
+
frame.columnNumber,
|
|
308
|
+
linesBefore,
|
|
309
|
+
linesAfter
|
|
310
|
+
);
|
|
311
|
+
}
|
|
312
|
+
function fileExists(filePath) {
|
|
313
|
+
try {
|
|
314
|
+
fs2__namespace.accessSync(filePath, fs2__namespace.constants.R_OK);
|
|
315
|
+
return true;
|
|
316
|
+
} catch {
|
|
317
|
+
return false;
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
var VLQ_BASE64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
|
321
|
+
var VLQ_BASE64_DECODE = {};
|
|
322
|
+
for (let i = 0; i < VLQ_BASE64.length; i++) {
|
|
323
|
+
VLQ_BASE64_DECODE[VLQ_BASE64[i]] = i;
|
|
324
|
+
}
|
|
325
|
+
var sourceMapCache = /* @__PURE__ */ new Map();
|
|
326
|
+
function decodeVLQ(encoded) {
|
|
327
|
+
const result = [];
|
|
328
|
+
let shift = 0;
|
|
329
|
+
let value = 0;
|
|
330
|
+
for (const char of encoded) {
|
|
331
|
+
const digit = VLQ_BASE64_DECODE[char];
|
|
332
|
+
if (digit === void 0) continue;
|
|
333
|
+
const cont = (digit & 32) !== 0;
|
|
334
|
+
const digitValue = digit & 31;
|
|
335
|
+
value += digitValue << shift;
|
|
336
|
+
if (cont) {
|
|
337
|
+
shift += 5;
|
|
338
|
+
} else {
|
|
339
|
+
const shouldNegate = (value & 1) !== 0;
|
|
340
|
+
value >>= 1;
|
|
341
|
+
result.push(shouldNegate ? -value : value);
|
|
342
|
+
value = 0;
|
|
343
|
+
shift = 0;
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
return result;
|
|
347
|
+
}
|
|
348
|
+
function findOriginalPosition(map, generatedLine, generatedColumn) {
|
|
349
|
+
const mappingLines = map.mappings.split(";");
|
|
350
|
+
let sourceIndex = 0;
|
|
351
|
+
let sourceLine = 0;
|
|
352
|
+
let sourceColumn = 0;
|
|
353
|
+
let bestMatch = null;
|
|
354
|
+
let bestColumn = -1;
|
|
355
|
+
for (let lineIndex = 0; lineIndex < mappingLines.length; lineIndex++) {
|
|
356
|
+
const line = mappingLines[lineIndex];
|
|
357
|
+
if (!line) continue;
|
|
358
|
+
const segments = line.split(",");
|
|
359
|
+
let generatedCol = 0;
|
|
360
|
+
for (const segment of segments) {
|
|
361
|
+
const decoded = decodeVLQ(segment);
|
|
362
|
+
if (decoded.length === 0) continue;
|
|
363
|
+
generatedCol += decoded[0] ?? 0;
|
|
364
|
+
if (decoded.length >= 4) {
|
|
365
|
+
sourceIndex += decoded[1] ?? 0;
|
|
366
|
+
sourceLine += decoded[2] ?? 0;
|
|
367
|
+
sourceColumn += decoded[3] ?? 0;
|
|
368
|
+
}
|
|
369
|
+
if (lineIndex + 1 === generatedLine) {
|
|
370
|
+
if (generatedCol <= generatedColumn && generatedCol > bestColumn) {
|
|
371
|
+
bestMatch = {
|
|
372
|
+
sourceIndex,
|
|
373
|
+
line: sourceLine + 1,
|
|
374
|
+
// Convert to 1-indexed
|
|
375
|
+
column: sourceColumn + 1
|
|
376
|
+
// Convert to 1-indexed
|
|
377
|
+
};
|
|
378
|
+
bestColumn = generatedCol;
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
return bestMatch;
|
|
384
|
+
}
|
|
385
|
+
function readSourceMap(mapPath) {
|
|
386
|
+
try {
|
|
387
|
+
const content = fs2__namespace.readFileSync(mapPath, "utf-8");
|
|
388
|
+
return JSON.parse(content);
|
|
389
|
+
} catch {
|
|
390
|
+
return null;
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
function extractInlineSourceMap(filePath) {
|
|
394
|
+
try {
|
|
395
|
+
const content = fs2__namespace.readFileSync(filePath, "utf-8");
|
|
396
|
+
const inlineMatch = content.match(
|
|
397
|
+
/\/\/[#@]\s*sourceMappingURL=data:application\/json;(?:charset=utf-8;)?base64,([A-Za-z0-9+/=]+)\s*$/m
|
|
398
|
+
);
|
|
399
|
+
if (inlineMatch == null ? void 0 : inlineMatch[1]) {
|
|
400
|
+
const decoded = Buffer.from(inlineMatch[1], "base64").toString("utf-8");
|
|
401
|
+
return JSON.parse(decoded);
|
|
402
|
+
}
|
|
403
|
+
return null;
|
|
404
|
+
} catch {
|
|
405
|
+
return null;
|
|
406
|
+
}
|
|
407
|
+
}
|
|
408
|
+
function findExternalSourceMap(filePath) {
|
|
409
|
+
try {
|
|
410
|
+
const content = fs2__namespace.readFileSync(filePath, "utf-8");
|
|
411
|
+
const externalMatch = content.match(
|
|
412
|
+
/\/\/[#@]\s*sourceMappingURL=(?!data:)([^\s'"]+)\s*$/m
|
|
413
|
+
);
|
|
414
|
+
if (externalMatch == null ? void 0 : externalMatch[1]) {
|
|
415
|
+
const mapPath = path__namespace.resolve(path__namespace.dirname(filePath), externalMatch[1]);
|
|
416
|
+
if (fs2__namespace.existsSync(mapPath)) {
|
|
417
|
+
return mapPath;
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
const conventions = [
|
|
421
|
+
`${filePath}.map`,
|
|
422
|
+
filePath.replace(/\.js$/, ".js.map"),
|
|
423
|
+
filePath.replace(/\.mjs$/, ".mjs.map"),
|
|
424
|
+
filePath.replace(/\.cjs$/, ".cjs.map")
|
|
425
|
+
];
|
|
426
|
+
for (const mapPath of conventions) {
|
|
427
|
+
if (fs2__namespace.existsSync(mapPath)) {
|
|
428
|
+
return mapPath;
|
|
429
|
+
}
|
|
430
|
+
}
|
|
431
|
+
return null;
|
|
432
|
+
} catch {
|
|
433
|
+
return null;
|
|
434
|
+
}
|
|
435
|
+
}
|
|
436
|
+
function getSourceMapData(filePath) {
|
|
437
|
+
if (sourceMapCache.has(filePath)) {
|
|
438
|
+
return sourceMapCache.get(filePath) ?? null;
|
|
439
|
+
}
|
|
440
|
+
let sourceMapData = extractInlineSourceMap(filePath);
|
|
441
|
+
if (!sourceMapData) {
|
|
442
|
+
const mapPath = findExternalSourceMap(filePath);
|
|
443
|
+
if (mapPath) {
|
|
444
|
+
sourceMapData = readSourceMap(mapPath);
|
|
445
|
+
}
|
|
446
|
+
}
|
|
447
|
+
sourceMapCache.set(filePath, sourceMapData);
|
|
448
|
+
return sourceMapData;
|
|
449
|
+
}
|
|
450
|
+
function resolveSourceMap(frame) {
|
|
451
|
+
var _a;
|
|
452
|
+
try {
|
|
453
|
+
const map = getSourceMapData(frame.filePath);
|
|
454
|
+
if (!map) {
|
|
455
|
+
return null;
|
|
456
|
+
}
|
|
457
|
+
const position = findOriginalPosition(
|
|
458
|
+
map,
|
|
459
|
+
frame.lineNumber,
|
|
460
|
+
frame.columnNumber - 1
|
|
461
|
+
// Convert to 0-indexed for mapping
|
|
462
|
+
);
|
|
463
|
+
if (!position) {
|
|
464
|
+
return null;
|
|
465
|
+
}
|
|
466
|
+
const source = map.sources[position.sourceIndex];
|
|
467
|
+
if (!source) {
|
|
468
|
+
return null;
|
|
469
|
+
}
|
|
470
|
+
let sourceContent;
|
|
471
|
+
if ((_a = map.sourcesContent) == null ? void 0 : _a[position.sourceIndex]) {
|
|
472
|
+
sourceContent = map.sourcesContent[position.sourceIndex] ?? void 0;
|
|
473
|
+
}
|
|
474
|
+
return {
|
|
475
|
+
source,
|
|
476
|
+
line: position.line,
|
|
477
|
+
column: position.column,
|
|
478
|
+
sourceContent
|
|
479
|
+
};
|
|
480
|
+
} catch {
|
|
481
|
+
return null;
|
|
482
|
+
}
|
|
483
|
+
}
|
|
484
|
+
function clearSourceMapCache() {
|
|
485
|
+
sourceMapCache.clear();
|
|
486
|
+
}
|
|
487
|
+
async function preloadSourceMapModule() {
|
|
488
|
+
return true;
|
|
489
|
+
}
|
|
490
|
+
var BOX = {
|
|
491
|
+
vertical: "\u2502",
|
|
492
|
+
arrow: "\u2192",
|
|
493
|
+
pointer: "^",
|
|
494
|
+
lineMarker: ">"
|
|
495
|
+
};
|
|
496
|
+
var ERROR_COLORS = {
|
|
497
|
+
ReferenceError: pc__default.default.red,
|
|
498
|
+
TypeError: pc__default.default.red,
|
|
499
|
+
SyntaxError: pc__default.default.magenta,
|
|
500
|
+
RangeError: pc__default.default.yellow,
|
|
501
|
+
EvalError: pc__default.default.red,
|
|
502
|
+
URIError: pc__default.default.cyan,
|
|
503
|
+
Error: pc__default.default.red,
|
|
504
|
+
default: pc__default.default.red
|
|
505
|
+
};
|
|
506
|
+
function getErrorColor(errorName) {
|
|
507
|
+
return ERROR_COLORS[errorName] ?? ERROR_COLORS["default"] ?? pc__default.default.red;
|
|
508
|
+
}
|
|
509
|
+
function padNumber(num, width) {
|
|
510
|
+
return String(num).padStart(width, " ");
|
|
511
|
+
}
|
|
512
|
+
function visualWidth(str, tabWidth = 4) {
|
|
513
|
+
let width = 0;
|
|
514
|
+
for (const char of str) {
|
|
515
|
+
if (char === " ") {
|
|
516
|
+
width += tabWidth - width % tabWidth;
|
|
517
|
+
} else {
|
|
518
|
+
width += 1;
|
|
519
|
+
}
|
|
520
|
+
}
|
|
521
|
+
return width;
|
|
522
|
+
}
|
|
523
|
+
function expandTabs(str, tabWidth = 4) {
|
|
524
|
+
let result = "";
|
|
525
|
+
let column = 0;
|
|
526
|
+
for (const char of str) {
|
|
527
|
+
if (char === " ") {
|
|
528
|
+
const spaces = tabWidth - column % tabWidth;
|
|
529
|
+
result += " ".repeat(spaces);
|
|
530
|
+
column += spaces;
|
|
531
|
+
} else {
|
|
532
|
+
result += char;
|
|
533
|
+
column += 1;
|
|
534
|
+
}
|
|
535
|
+
}
|
|
536
|
+
return result;
|
|
537
|
+
}
|
|
538
|
+
function highlightSyntax(line, _isErrorLine) {
|
|
539
|
+
if (!line.trim()) {
|
|
540
|
+
return line;
|
|
541
|
+
}
|
|
542
|
+
let result = line;
|
|
543
|
+
result = result.replace(
|
|
544
|
+
/(["'`])(?:(?!\1)[^\\]|\\.)*\1/g,
|
|
545
|
+
(match) => pc__default.default.green(match)
|
|
546
|
+
);
|
|
547
|
+
result = result.replace(
|
|
548
|
+
/\b(\d+(?:\.\d+)?)\b/g,
|
|
549
|
+
(match) => pc__default.default.yellow(match)
|
|
550
|
+
);
|
|
551
|
+
const keywords = /\b(const|let|var|function|class|if|else|for|while|return|throw|new|async|await|import|export|from|try|catch|finally|default|switch|case|break|continue|typeof|instanceof|in|of|null|undefined|true|false|this|super)\b/g;
|
|
552
|
+
result = result.replace(keywords, (match) => pc__default.default.blue(match));
|
|
553
|
+
result = result.replace(
|
|
554
|
+
/(\/\/.*$|\/\*[\s\S]*?\*\/)/gm,
|
|
555
|
+
(match) => pc__default.default.dim(match)
|
|
556
|
+
);
|
|
557
|
+
return result;
|
|
558
|
+
}
|
|
559
|
+
function renderCodeFrame(context, _config) {
|
|
560
|
+
const lines = [];
|
|
561
|
+
const { lines: codeLines, errorColumn } = context;
|
|
562
|
+
const maxLineNumber = Math.max(...codeLines.map((l) => l.lineNumber));
|
|
563
|
+
const lineNumWidth = String(maxLineNumber).length;
|
|
564
|
+
const gutterWidth = lineNumWidth + 3;
|
|
565
|
+
for (const codeLine of codeLines) {
|
|
566
|
+
const expandedContent = expandTabs(codeLine.content);
|
|
567
|
+
const lineNumStr = padNumber(codeLine.lineNumber, lineNumWidth);
|
|
568
|
+
if (codeLine.isErrorLine) {
|
|
569
|
+
const marker = pc__default.default.red(pc__default.default.bold(BOX.lineMarker));
|
|
570
|
+
const lineNum = pc__default.default.red(pc__default.default.bold(lineNumStr));
|
|
571
|
+
const separator = pc__default.default.red(pc__default.default.bold(BOX.vertical));
|
|
572
|
+
const highlighted = highlightSyntax(expandedContent);
|
|
573
|
+
lines.push(` ${marker} ${lineNum} ${separator} ${highlighted}`);
|
|
574
|
+
if (errorColumn > 0) {
|
|
575
|
+
const beforeColumn = codeLine.content.slice(0, errorColumn - 1);
|
|
576
|
+
const visualColumn = visualWidth(beforeColumn);
|
|
577
|
+
const padding = " ".repeat(gutterWidth + visualColumn + 1);
|
|
578
|
+
const pointer = pc__default.default.red(pc__default.default.bold(BOX.pointer.repeat(Math.min(3, Math.max(1, codeLine.content.length - errorColumn + 1)))));
|
|
579
|
+
lines.push(`${padding}${pointer}`);
|
|
580
|
+
}
|
|
581
|
+
} else {
|
|
582
|
+
const lineNum = pc__default.default.dim(lineNumStr);
|
|
583
|
+
const separator = pc__default.default.dim(BOX.vertical);
|
|
584
|
+
const highlighted = highlightSyntax(expandedContent);
|
|
585
|
+
lines.push(` ${lineNum} ${separator} ${highlighted}`);
|
|
586
|
+
}
|
|
587
|
+
}
|
|
588
|
+
return lines;
|
|
589
|
+
}
|
|
590
|
+
function renderStackTrace(frames, topFrame, maxFrames = 5) {
|
|
591
|
+
const lines = [];
|
|
592
|
+
let shown = 0;
|
|
593
|
+
let hidden = 0;
|
|
594
|
+
for (const frame of frames) {
|
|
595
|
+
if (frame === topFrame) {
|
|
596
|
+
continue;
|
|
597
|
+
}
|
|
598
|
+
if (shown >= maxFrames) {
|
|
599
|
+
hidden++;
|
|
600
|
+
continue;
|
|
601
|
+
}
|
|
602
|
+
lines.push(pc__default.default.dim(formatFrameAsStackLine(frame)));
|
|
603
|
+
shown++;
|
|
604
|
+
}
|
|
605
|
+
if (hidden > 0) {
|
|
606
|
+
lines.push(pc__default.default.dim(` ... ${hidden} more frame${hidden > 1 ? "s" : ""}`));
|
|
607
|
+
}
|
|
608
|
+
return lines;
|
|
609
|
+
}
|
|
610
|
+
function renderHeader(error, filePath, lineNumber, columnNumber, sourceMapResult) {
|
|
611
|
+
const lines = [];
|
|
612
|
+
const errorColor = getErrorColor(error.name);
|
|
613
|
+
const errorType = pc__default.default.bold(errorColor(error.name));
|
|
614
|
+
const errorMessage = pc__default.default.white(error.message);
|
|
615
|
+
lines.push("");
|
|
616
|
+
lines.push(`${errorType}: ${errorMessage}`);
|
|
617
|
+
if (filePath) {
|
|
618
|
+
const displayPath = (sourceMapResult == null ? void 0 : sourceMapResult.source) ? getRelativePath(path__namespace.resolve(path__namespace.dirname(filePath), sourceMapResult.source)) : getRelativePath(filePath);
|
|
619
|
+
const line = (sourceMapResult == null ? void 0 : sourceMapResult.line) ?? lineNumber ?? 0;
|
|
620
|
+
const column = (sourceMapResult == null ? void 0 : sourceMapResult.column) ?? columnNumber ?? 0;
|
|
621
|
+
const location = `${displayPath}:${line}:${column}`;
|
|
622
|
+
lines.push(` ${pc__default.default.dim(BOX.arrow)} ${pc__default.default.cyan(location)}`);
|
|
623
|
+
}
|
|
624
|
+
lines.push("");
|
|
625
|
+
return lines;
|
|
626
|
+
}
|
|
627
|
+
function renderTip(error) {
|
|
628
|
+
const tips = {
|
|
629
|
+
ReferenceError: "Check that the variable is defined and in scope",
|
|
630
|
+
TypeError: "Verify the value type matches what the operation expects",
|
|
631
|
+
SyntaxError: "Look for missing brackets, commas, or typos",
|
|
632
|
+
RangeError: "Check array indices and numeric boundaries"
|
|
633
|
+
};
|
|
634
|
+
const tip = tips[error.name];
|
|
635
|
+
if (tip) {
|
|
636
|
+
return [
|
|
637
|
+
"",
|
|
638
|
+
pc__default.default.dim(` ${pc__default.default.yellow("tip")}: ${tip}`)
|
|
639
|
+
];
|
|
640
|
+
}
|
|
641
|
+
return [];
|
|
642
|
+
}
|
|
643
|
+
function renderError(error, topFrame, codeContext, allFrames, sourceMapResult, config = defaultConfig) {
|
|
644
|
+
const output = [];
|
|
645
|
+
output.push(...renderHeader(
|
|
646
|
+
error,
|
|
647
|
+
(topFrame == null ? void 0 : topFrame.filePath) ?? null,
|
|
648
|
+
(topFrame == null ? void 0 : topFrame.lineNumber) ?? null,
|
|
649
|
+
(topFrame == null ? void 0 : topFrame.columnNumber) ?? null,
|
|
650
|
+
sourceMapResult
|
|
651
|
+
));
|
|
652
|
+
if (codeContext && codeContext.lines.length > 0) {
|
|
653
|
+
output.push(...renderCodeFrame(codeContext));
|
|
654
|
+
}
|
|
655
|
+
output.push(...renderTip(error));
|
|
656
|
+
if (config.showFullStack && allFrames.length > 0) {
|
|
657
|
+
output.push("");
|
|
658
|
+
output.push(pc__default.default.dim(" Stack trace:"));
|
|
659
|
+
output.push(...renderStackTrace(allFrames, topFrame));
|
|
660
|
+
}
|
|
661
|
+
output.push("");
|
|
662
|
+
return output.join("\n");
|
|
663
|
+
}
|
|
664
|
+
function renderMinimalError(error, frames) {
|
|
665
|
+
const output = [];
|
|
666
|
+
const errorColor = getErrorColor(error.name);
|
|
667
|
+
output.push("");
|
|
668
|
+
output.push(`${pc__default.default.bold(errorColor(error.name))}: ${pc__default.default.white(error.message)}`);
|
|
669
|
+
output.push("");
|
|
670
|
+
const framesToShow = frames.slice(0, 5);
|
|
671
|
+
for (const frame of framesToShow) {
|
|
672
|
+
output.push(pc__default.default.dim(formatFrameAsStackLine(frame)));
|
|
673
|
+
}
|
|
674
|
+
if (frames.length > 5) {
|
|
675
|
+
output.push(pc__default.default.dim(` ... ${frames.length - 5} more frames`));
|
|
676
|
+
}
|
|
677
|
+
output.push("");
|
|
678
|
+
return output.join("\n");
|
|
679
|
+
}
|
|
680
|
+
function formatErrorString(error) {
|
|
681
|
+
return error.stack ?? String(error);
|
|
682
|
+
}
|
|
683
|
+
|
|
684
|
+
// src/interceptor.ts
|
|
685
|
+
var originalPrepareStackTrace;
|
|
686
|
+
var isInstalled = false;
|
|
687
|
+
var currentConfig = { ...defaultConfig };
|
|
688
|
+
var isProcessing = false;
|
|
689
|
+
function customPrepareStackTrace(error, structuredStack) {
|
|
690
|
+
if (isProcessing) {
|
|
691
|
+
return formatFallbackStack(error, structuredStack);
|
|
692
|
+
}
|
|
693
|
+
isProcessing = true;
|
|
694
|
+
try {
|
|
695
|
+
return generateBeautifulStack(error, structuredStack);
|
|
696
|
+
} catch {
|
|
697
|
+
return formatFallbackStack(error, structuredStack);
|
|
698
|
+
} finally {
|
|
699
|
+
isProcessing = false;
|
|
700
|
+
}
|
|
701
|
+
}
|
|
702
|
+
function generateBeautifulStack(error, structuredStack) {
|
|
703
|
+
const allFrames = parseStackTrace(structuredStack);
|
|
704
|
+
if (allFrames.length === 0) {
|
|
705
|
+
return formatFallbackStack(error, structuredStack);
|
|
706
|
+
}
|
|
707
|
+
let frames = allFrames;
|
|
708
|
+
if (currentConfig.filterNodeModules) {
|
|
709
|
+
const userFrames = frames.filter((f) => f.isUserCode);
|
|
710
|
+
if (userFrames.length > 0) {
|
|
711
|
+
frames = allFrames.filter((f) => f.isUserCode || !f.isNodeModules);
|
|
712
|
+
}
|
|
713
|
+
}
|
|
714
|
+
frames = frames.filter(currentConfig.frameFilter);
|
|
715
|
+
const topFrame = findUserCodeFrame(frames.length > 0 ? frames : allFrames);
|
|
716
|
+
if (!topFrame) {
|
|
717
|
+
return renderMinimalError(error, allFrames);
|
|
718
|
+
}
|
|
719
|
+
let sourceMapResult = null;
|
|
720
|
+
if (currentConfig.enableSourceMaps) {
|
|
721
|
+
sourceMapResult = resolveSourceMap(topFrame);
|
|
722
|
+
}
|
|
723
|
+
const codeContext = getContextForFrame(
|
|
724
|
+
topFrame,
|
|
725
|
+
sourceMapResult,
|
|
726
|
+
currentConfig.contextLinesBefore,
|
|
727
|
+
currentConfig.contextLinesAfter
|
|
728
|
+
);
|
|
729
|
+
if (!codeContext) {
|
|
730
|
+
return renderMinimalError(error, allFrames);
|
|
731
|
+
}
|
|
732
|
+
return renderError(
|
|
733
|
+
error,
|
|
734
|
+
topFrame,
|
|
735
|
+
codeContext,
|
|
736
|
+
allFrames,
|
|
737
|
+
sourceMapResult,
|
|
738
|
+
currentConfig
|
|
739
|
+
);
|
|
740
|
+
}
|
|
741
|
+
function formatFallbackStack(error, structuredStack) {
|
|
742
|
+
const lines = [];
|
|
743
|
+
lines.push(`${error.name}: ${error.message}`);
|
|
744
|
+
for (const callSite of structuredStack) {
|
|
745
|
+
try {
|
|
746
|
+
const fileName = callSite.getFileName() ?? callSite.getScriptNameOrSourceURL() ?? "<anonymous>";
|
|
747
|
+
const lineNumber = callSite.getLineNumber() ?? 0;
|
|
748
|
+
const columnNumber = callSite.getColumnNumber() ?? 0;
|
|
749
|
+
const functionName = callSite.getFunctionName() ?? "<anonymous>";
|
|
750
|
+
if (callSite.isNative()) {
|
|
751
|
+
lines.push(` at ${functionName} (native)`);
|
|
752
|
+
} else {
|
|
753
|
+
lines.push(` at ${functionName} (${fileName}:${lineNumber}:${columnNumber})`);
|
|
754
|
+
}
|
|
755
|
+
} catch {
|
|
756
|
+
lines.push(" at <unknown>");
|
|
757
|
+
}
|
|
758
|
+
}
|
|
759
|
+
return lines.join("\n");
|
|
760
|
+
}
|
|
761
|
+
function install(config = {}) {
|
|
762
|
+
if (isInstalled) {
|
|
763
|
+
currentConfig = { ...defaultConfig, ...config };
|
|
764
|
+
return;
|
|
765
|
+
}
|
|
766
|
+
originalPrepareStackTrace = Error.prepareStackTrace;
|
|
767
|
+
Error.prepareStackTrace = customPrepareStackTrace;
|
|
768
|
+
currentConfig = { ...defaultConfig, ...config };
|
|
769
|
+
isInstalled = true;
|
|
770
|
+
}
|
|
771
|
+
function uninstall() {
|
|
772
|
+
if (!isInstalled) {
|
|
773
|
+
return;
|
|
774
|
+
}
|
|
775
|
+
Error.prepareStackTrace = originalPrepareStackTrace;
|
|
776
|
+
originalPrepareStackTrace = void 0;
|
|
777
|
+
isInstalled = false;
|
|
778
|
+
}
|
|
779
|
+
function isEnabled() {
|
|
780
|
+
return isInstalled;
|
|
781
|
+
}
|
|
782
|
+
function configure(config) {
|
|
783
|
+
currentConfig = { ...currentConfig, ...config };
|
|
784
|
+
}
|
|
785
|
+
function getConfig() {
|
|
786
|
+
return { ...currentConfig };
|
|
787
|
+
}
|
|
788
|
+
function formatError(error) {
|
|
789
|
+
if (!isInstalled) {
|
|
790
|
+
const wasInstalled = isInstalled;
|
|
791
|
+
if (!wasInstalled) {
|
|
792
|
+
install();
|
|
793
|
+
}
|
|
794
|
+
try {
|
|
795
|
+
return error.stack ?? String(error);
|
|
796
|
+
} finally {
|
|
797
|
+
if (!wasInstalled) {
|
|
798
|
+
uninstall();
|
|
799
|
+
}
|
|
800
|
+
}
|
|
801
|
+
}
|
|
802
|
+
return error.stack ?? String(error);
|
|
803
|
+
}
|
|
804
|
+
function formatThrown(thrown) {
|
|
805
|
+
if (thrown instanceof Error) {
|
|
806
|
+
return formatError(thrown);
|
|
807
|
+
}
|
|
808
|
+
const wrapped = new Error(String(thrown));
|
|
809
|
+
wrapped.name = "UnknownError";
|
|
810
|
+
return formatError(wrapped);
|
|
811
|
+
}
|
|
812
|
+
function getErrorFrames(error) {
|
|
813
|
+
const tempStack = [];
|
|
814
|
+
const originalPST = Error.prepareStackTrace;
|
|
815
|
+
Error.prepareStackTrace = (_, stack) => {
|
|
816
|
+
tempStack.push(...stack);
|
|
817
|
+
return "";
|
|
818
|
+
};
|
|
819
|
+
Error.captureStackTrace(error);
|
|
820
|
+
error.stack;
|
|
821
|
+
Error.prepareStackTrace = originalPST;
|
|
822
|
+
return parseStackTrace(tempStack);
|
|
823
|
+
}
|
|
824
|
+
|
|
825
|
+
// src/index.ts
|
|
826
|
+
var src_default = install;
|
|
827
|
+
|
|
828
|
+
exports.clearFileCache = clearFileCache;
|
|
829
|
+
exports.clearSourceMapCache = clearSourceMapCache;
|
|
830
|
+
exports.configure = configure;
|
|
831
|
+
exports.default = src_default;
|
|
832
|
+
exports.defaultConfig = defaultConfig;
|
|
833
|
+
exports.fileExists = fileExists;
|
|
834
|
+
exports.findUserCodeFrame = findUserCodeFrame;
|
|
835
|
+
exports.formatError = formatError;
|
|
836
|
+
exports.formatErrorString = formatErrorString;
|
|
837
|
+
exports.formatFrameAsStackLine = formatFrameAsStackLine;
|
|
838
|
+
exports.formatThrown = formatThrown;
|
|
839
|
+
exports.getCodeContext = getCodeContext;
|
|
840
|
+
exports.getConfig = getConfig;
|
|
841
|
+
exports.getContextForFrame = getContextForFrame;
|
|
842
|
+
exports.getErrorFrames = getErrorFrames;
|
|
843
|
+
exports.getRelativePath = getRelativePath;
|
|
844
|
+
exports.install = install;
|
|
845
|
+
exports.isEnabled = isEnabled;
|
|
846
|
+
exports.parseCallSite = parseCallSite;
|
|
847
|
+
exports.parseStackTrace = parseStackTrace;
|
|
848
|
+
exports.preloadSourceMapModule = preloadSourceMapModule;
|
|
849
|
+
exports.renderError = renderError;
|
|
850
|
+
exports.renderMinimalError = renderMinimalError;
|
|
851
|
+
exports.resolveSourceMap = resolveSourceMap;
|
|
852
|
+
exports.uninstall = uninstall;
|
|
853
|
+
//# sourceMappingURL=index.js.map
|
|
854
|
+
//# sourceMappingURL=index.js.map
|