openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
65 lines (64 loc) • 2.2 kB
JavaScript
import path from "node:path";
//#region node-runtime-env.mjs
function parseNodeOptionsEnvVar(e) {
const t = [];
let n = "";
let r = false;
const i = e ?? "";
for (let e = 0; e < i.length; e += 1) {
let a = i.charAt(e);
if (a === "\\" && r) {
e += 1;
if (e >= i.length) return null;
a = i.charAt(e);
} else if (a === " " && !r) {
if (n.length > 0) {
t.push(n);
n = "";
}
continue;
} else if (a === "\"") {
r = !r;
continue;
}
n += a;
}
if (r) return null;
if (n.length > 0) t.push(n);
return t;
}
/** Preloads can install module hooks and start an independent compiler/reaper. */
function isNodeStartupHook(e) {
return /^(?:--(?:import|require|(?:experimental-)?loader)(?:=|$)|-r)/u.test(e.replaceAll("_", "-"));
}
/** Dedicated process owners must not inherit the caller's instrumentation loaders. */
function stripNodeStartupHooks(e) {
const t = parseNodeOptionsEnvVar(e);
if (t === null) throw new Error("Invalid NODE_OPTIONS for process owner");
if (!t.some(isNodeStartupHook)) return e;
const n = [];
for (let e = 0; e < t.length; e += 1) {
const r = t[e];
if (isNodeStartupHook(r)) {
if (r === "-r" || r.startsWith("--") && !r.includes("=")) e += 1;
} else n.push("\"" + r.replaceAll("\\", "\\\\").replaceAll("\"", "\\\"") + "\"");
}
return n.join(" ");
}
function nodeRuntimePathEntries(e) {
const t = process.platform === "win32";
return (e ?? "").split(t ? ";" : ":").map((e) => t ? e.replace(/^"(.*)"$/, "$1") : e).filter(Boolean);
}
/** Keep lifecycle children on the explicitly selected runtime without changing npm config. */
function withNodeRuntimePath(e, t, n = process.platform) {
const r = n === "win32" ? path.win32 : path.posix;
const i = Object.keys(e).toSorted().filter((e) => n === "win32" ? e.toUpperCase() === "PATH" : e === "PATH");
const a = i[0] ?? "PATH";
const o = r.dirname(t);
const s = (e[a] ? e[a].split(r.delimiter) : []).filter((e) => e !== o);
const c = { ...e };
for (const e of i.length ? i : [a]) c[e] = [o, ...s].join(r.delimiter);
return c;
}
//#endregion
export { withNodeRuntimePath as i, parseNodeOptionsEnvVar as n, stripNodeStartupHooks as r, nodeRuntimePathEntries as t };