node-network-devtools
Version:
Inspecting Node.js's Network with Chrome DevTools
133 lines (132 loc) • 4.12 kB
JavaScript
import { fileURLToPath as d } from "url";
import { dirname as x } from "path";
class u {
constructor(e) {
typeof e == "string" && this.parse(e), e instanceof u && Object.assign(this, e);
}
parse(e) {
if (e.match(/^\s*[-]{4,}$/))
return this.fileName = e, this;
const n = /at (?:(.+?)\s+\()?(?:(.+?):(\d+)(?::(\d+))?|([^)]+))\)?/, t = e.match(n);
if (!t)
return this;
let r = null, o = null, i = null, m = null, c = null, f = t[5] === "native";
if (t[1]) {
i = t[1];
let a = i.lastIndexOf(".");
if (i[a - 1] == "." && a--, a > 0) {
r = i.substr(0, a), o = i.substr(a + 1);
const l = r.indexOf(".Module");
l > 0 && (i = i.substr(l + 1), r = r.substr(0, l));
}
}
o && (m = r, c = o), o === "<anonymous>" && (c = null, i = null);
const h = {
fileName: t[2] || null,
lineNumber: parseInt(t[3], 10) || null,
functionName: i,
typeName: m,
methodName: c,
columnNumber: parseInt(t[4], 10) || null,
native: f
};
return Object.assign(this, h), this;
}
valueOf() {
return {
fileName: this.fileName,
lineNumber: this.lineNumber,
functionName: this.functionName,
typeName: this.typeName,
methodName: this.methodName,
columnNumber: this.columnNumber,
native: this.native
};
}
toString() {
return JSON.stringify(this.valueOf());
}
}
const p = [
/* node:async_hooks */
/\((internal\/)?async_hooks\.js:/,
/* external */
/\(\//,
/* node_modules */
/node_modules/
];
function N(s) {
const e = /* @__PURE__ */ Object.create(null);
return s ? e.stack = s : (Error.stackTraceLimit = 1 / 0, Error.captureStackTrace(e)), e.stack.split(`
`).slice(1).map((r) => new u(r));
}
function b(s) {
return s.filter((n) => !p.some((t) => t.test(n.fileName || "")));
}
const T = console.log.bind(console, "\x1B[36m[node-network-debugger]:", "\x1B[32m"), _ = console.warn.bind(console, "\x1B[36m[node-network-debugger](warn):", "\x1B[33m"), k = (s, e) => s + Object.entries(e).map(([n, t]) => `${n}: ${String(t)}`).join(`\r
`), y = (s) => s.reduce(
(e, n, t, r) => t % 2 === 0 ? { ...e, [r[t]]: r[t + 1] } : e,
{}
), g = (s) => Object.keys(s).reduce((e, n) => {
const t = s[n];
return e[n] = typeof t == "object" && t !== null ? g(t) : String(t), e;
}, {}), w = () => (/* @__PURE__ */ new Date()).getTime() / 1e3;
function E() {
let s = (/* @__PURE__ */ new Date()).getTime();
return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function(n) {
const t = (s + Math.random() * 16) % 16 | 0;
return s = Math.floor(s / 16), (n === "x" ? t : t & 3 | 8).toString(16);
});
}
function v(s) {
let e = 0, n, t;
if (s.length === 0) return e.toString(36);
for (n = 0; n < s.length; n++)
t = s.charCodeAt(n), e = (e << 5) - e + t, e |= 0;
return e.toString(36);
}
class j {
constructor(e) {
e ? (this.id = e.id, this.responseInfo = e.responseInfo, Object.assign(this, e)) : (this.id = E(), this.responseInfo = {});
}
loadCallFrames(e) {
const t = b(N(e)).map((r) => {
const o = r.fileName || "";
return {
columnNumber: r.columnNumber || 0,
functionName: r.functionName || "",
lineNumber: r.lineNumber || 0,
url: o.startsWith("/") ? `file://${o}` : o
};
});
t.length > 0 && (this.initiator = {
type: "script",
stack: {
callFrames: t
}
});
}
isHiden() {
return this.isWebSocket() && ["http://localhost/", "ws://localhost/"].includes(this.url);
}
isWebSocket() {
return this.requestHeaders?.Upgrade === "websocket" || this.requestHeaders?.upgrade === "websocket";
}
}
const D = Number(process.env.NETWORK_PORT || 5270), I = Number(process.env.NETWORK_SERVER_PORT || 5271), M = Number(process.env.REMOTE_DEBUGGER_PORT || 9333), P = process.env.NETWORK_DEBUG_MODE === "true", B = "ready", O = d(import.meta.url), H = x(O);
export {
P as I,
D as P,
j as R,
I as S,
H as _,
B as a,
v as b,
M as c,
k as f,
w as g,
T as l,
y as p,
g as s,
_ as w
};