@remotex-labs/xmap
Version:
A library with a sourcemap parser and TypeScript code formatter for the CLI
105 lines (104 loc) • 4.33 kB
JavaScript
// src/components/parser.component.ts
var s = {
V8: {
GLOBAL: /at\s(?:async\s)?(.+):(\d+):(\d+)/,
STANDARD: /at\s(.*?)\((?:(.+?):(\d+):(\d+)|(native))\)/,
EVAL: /^at\s(.+?)\s\(eval\sat\s(.+?)\s?\((.*):(\d+):(\d+)\),\s(.+?):(\d+):(\d+)\)$/
},
SPIDERMONKEY: {
EVAL: /^(.*?)@(.+?):(\d+):(\d+),\s(.+?)@(.+?):(\d+):(\d+)$/,
STANDARD: /^(.*?)@(.*?)(?:(\[native code\])|:(\d+):(\d+))$/
},
JAVASCRIPT_CORE: {
STANDARD: /^(?:(global|eval)\s)?(.*?)@(.*?)(?::(\d+)(?::(\d+))?)?$/
}
}, m = /* @__PURE__ */ ((a) => (a[a.V8 = 0] = "V8", a[a.SPIDERMONKEY = 1] = "SPIDERMONKEY", a[a.JAVASCRIPT_CORE = 2] = "JAVASCRIPT_CORE", a[a.UNKNOWN = 3] = "UNKNOWN", a))(m || {});
function l(n) {
return n.startsWith(" at ") || n.startsWith("at ") ? 0 : n.includes("@") ? /(?:global|eval) code@/.test(n) ? 2 : 1 : 3;
}
function c(n) {
return n.startsWith("file://") ? n.startsWith("file:///") && /^file:\/\/\/[A-Za-z]:/.test(n) ? n.substring(8) : n.substring(7) : (n = n.replace(/\\/g, "/"), n);
}
function u(n) {
return {
source: n,
eval: !1,
async: !1,
native: !1,
constructor: !1
};
}
function i(n) {
return n && n.trim() !== "" ? parseInt(n, 10) : void 0;
}
function N(n) {
let e = u(n);
n.toLowerCase().includes("new") && (e.constructor = !0), n.toLowerCase().includes("async") && (e.async = !0);
let t = n.match(s.V8.EVAL), r = n.match(s.V8.GLOBAL), a = !t && n.match(s.V8.STANDARD);
return t ? (e.eval = !0, e.functionName = t[1] || void 0, e.evalOrigin = {
line: i(t[4]) ?? void 0,
column: i(t[5]) ?? void 0,
fileName: t[3] ? c(t[3]) : void 0,
functionName: t[2] || "<anonymous>"
}, e.line = i(t[7]) ?? void 0, e.column = i(t[8]) ?? void 0, e.fileName = t[6] ? c(t[6]) : void 0) : a ? (e.functionName = a[1] ? a[1].trim() : void 0, a[5] === "native" ? (e.native = !0, e.fileName = "[native code]") : (e.line = i(a[3]) ?? void 0, e.column = i(a[4]) ?? void 0, e.fileName = a[2] ? c(a[2]) : void 0, e.fileName?.includes("node:") && (e.native = !0))) : r && (e.fileName = r[1] ? c(r[1]) : void 0, e.line = i(r[2]) ?? void 0, e.column = i(r[3]) ?? void 0), e;
}
function S(n) {
let e = u(n), t = n.match(s.SPIDERMONKEY.EVAL);
if (t)
return e.eval = !0, e.functionName = t[1] ? t[1] : void 0, n.toLowerCase().includes("constructor") && (e.constructor = !0), n.toLowerCase().includes("async") && (e.async = !0), e.evalOrigin = {
line: i(t[7]) ?? void 0,
column: i(t[8]) ?? void 0,
fileName: c(t[6]),
functionName: t[5] ? t[5] : void 0
}, e.line = i(t[3]) ?? void 0, e.column = i(t[4]) ?? void 0, e;
let r = n.match(s.SPIDERMONKEY.STANDARD);
return r && (e.functionName = r[1] ? r[1] : void 0, e.fileName = c(r[2]), r[3] === "[native code]" ? e.fileName = "[native code]" : (e.line = i(r[4]) ?? void 0, e.column = i(r[5]) ?? void 0)), e;
}
function p(n) {
let e = u(n), t = n.match(s.JAVASCRIPT_CORE.STANDARD);
return t && (e.functionName = t[2], e.eval = t[1] === "eval" || t[3] === "eval", n.toLowerCase().includes("constructor") && (e.constructor = !0), n.toLowerCase().includes("async") && (e.async = !0), t[3] === "[native code]" ? (e.native = !0, e.fileName = "[native code]") : (e.line = i(t[4]) ?? void 0, e.column = i(t[5]) ?? void 0, e.fileName = c(t[3]))), e;
}
function g(n, e) {
switch (e) {
case 1:
return S(n);
case 2:
return p(n);
default:
return N(n);
}
}
function A(n) {
if (!n) return "";
let e = /^\s*at .+$|^.+@.+:\d+:\d+$|^@.+:\d+:\d+$|^.+@\[native code\]$/, t = n.split(`
`);
for (let r = 0; r < t.length; r++)
if (e.test(t[r]))
return t.slice(r).join(`
`);
return "";
}
function E(n) {
let e = typeof n == "string" ? { stack: n, message: n, name: "" } : n, t = e.name || "Error", r = A(e.stack || ""), a = e?.message || "", f = l(r), d = r.split(`
`).map((o) => o.trim()).filter((o) => o.trim() !== "");
return {
name: t,
message: a,
stack: d.map((o) => g(o, f)),
rawStack: r
};
}
export {
m as JSEngines,
u as createDefaultFrame,
l as detectJSEngine,
A as getStackWithoutMessage,
c as normalizePath,
E as parseErrorStack,
p as parseJavaScriptCoreStackLine,
S as parseSpiderMonkeyStackLine,
g as parseStackLine,
N as parseV8StackLine,
i as safeParseInt
};
//# sourceMappingURL=parser.component.js.map