openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
137 lines (136 loc) • 3.74 kB
JavaScript
//#region src/daemon/runtime-binary.ts
const NODE_VERSIONED_PATTERN = /^node(?:-\d+|\d+)(?:\.\d+)*(?:\.exe)?$/;
function normalizeRuntimeBasename(e) {
const t = e.trim().replace(/^["']|["']$/g, "");
const n = Math.max(t.lastIndexOf("/"), t.lastIndexOf("\\"));
return (n === -1 ? t : t.slice(n + 1)).trim().toLowerCase();
}
function isNodeRuntime(e) {
const t = normalizeRuntimeBasename(e);
return /^node(?:js)?(?:\.exe)?$/.test(t) || NODE_VERSIONED_PATTERN.test(t);
}
function isBunRuntime(e) {
const t = normalizeRuntimeBasename(e);
return t === "bun" || t === "bun.exe";
}
const RUNTIME_MODULE_OPTIONS = /* @__PURE__ */ new Set([
"-r",
"--preload",
"--require",
"--import",
"--loader",
"--experimental-loader",
"--test-reporter",
"--test-global-setup"
]);
const RUNTIME_VALUE_OPTIONS = /* @__PURE__ */ new Set([
...RUNTIME_MODULE_OPTIONS,
"-C",
"--env-file",
"--env-file-if-exists",
"--tsconfig",
"--cwd",
"--conditions",
"--icu-data-dir",
"--openssl-config",
"--title",
"--disable-warning",
"--disable-proto",
"--cpu-prof-name",
"--max-old-space-size",
"--max-semi-space-size"
]);
const RUNTIME_BOOLEAN_OPTIONS = /* @__PURE__ */ new Set([
"--inspect",
"--inspect-brk",
"--inspect-wait",
"--expose-gc",
"--jitless",
"--no-opt",
"--experimental-strip-types",
"--watch",
"--no-warnings",
"--trace-uncaught",
"--bun"
]);
/** One runtime walk locates the script, eval source, and possible artifact operands. */
function resolveRuntimeScriptPosition(e) {
const t = [];
const n = e[0] ?? "";
const r = n.replaceAll("\\", "/").trim().toLowerCase().split("/").at(-1);
const i = isBunRuntime(n);
const a = r === "tsx" || r === "tsx.cmd";
let o = i ? "run" : a ? "watch" : void 0;
let s;
const c = [];
if (!isNodeRuntime(n) && !i && !a) return {
position: { kind: "not-runtime" },
operands: t,
sourceIndices: c
};
for (let n = 1; n < e.length; n++) {
const r = e[n];
if (r === "--") {
if (!s) return {
position: c.length === 0 && e[n + 1] ? n + 1 : { kind: "other" },
operands: t,
sourceIndices: c
};
t.push(...e.slice(n + 1).map((e) => ({
value: e,
module: false
})));
break;
}
if (r === "-e" || r === "-p" || /^--(?:eval|print|run)(?:=|$)/.test(r)) {
if (!r.includes("=")) n++;
c.push(n);
continue;
}
const a = r.indexOf("=");
const l = a < 0 ? r : r.slice(0, a);
if (r.startsWith("-r") && !r.startsWith("--") && r.length > 2) t.push({
value: r.slice(2),
module: true
});
else if (RUNTIME_VALUE_OPTIONS.has(l)) {
const o = a < 0 ? e[++n] ?? "" : r.slice(a + 1);
const s = !i && l === "--test-reporter" && /^(?:dot|spec|tap)$/.test(o);
if (RUNTIME_MODULE_OPTIONS.has(l) && !s && (!i || l !== "--loader")) t.push({
value: o,
module: true
});
} else if (r.startsWith("-")) {
const e = `--no-${l.replace(/^--(?:no-)?/, "")}`;
const n = !i && process.allowedNodeEnvironmentFlags.has(l);
const o = RUNTIME_BOOLEAN_OPTIONS.has(l) || i && (l === "--hot" || l === "--no-install") || n && process.allowedNodeEnvironmentFlags.has(e);
if (c.length === 0 && !o && !/^--[^=]+=/.test(r)) s ??= {
kind: "unclassified",
reason: `unsupported runtime option ${r}`
};
if (a >= 0 && !o && !n) t.push({
value: r.slice(a + 1),
module: true
});
} else if (c.length === 0 && r === o) o = void 0;
else if (c.length === 0) {
if (!s) return {
position: n,
operands: t,
sourceIndices: c
};
o = void 0;
t.push({
value: r,
module: false
});
} else break;
}
return {
position: s ?? { kind: "other" },
operands: t,
sourceIndices: s ? [] : c
};
}
//#endregion
export { isNodeRuntime as n, resolveRuntimeScriptPosition as r, isBunRuntime as t };