openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
119 lines (118 loc) • 4.05 kB
JavaScript
import { t as nodeRuntimePathEntries } from "./node-runtime-env-ncloLK-i.mjs";
import { d as isSupportedOpenClawNodeVersion } from "./node-version-CYJ7qOPD.mjs";
import { n as createRuntimeProcessScriptFileAsync, t as createRuntimeProcessScriptFile } from "./runtime-process-scripts.generated-DGDjiipU.mjs";
import { fileURLToPath } from "node:url";
import path from "node:path";
import { promisify } from "node:util";
import { execFile, execFileSync } from "node:child_process";
//#region src/infra/node-runtime-executable.ts
const NODE_RUNTIME_PROBE_TIMEOUT_MS = 5e3;
const NODE_RUNTIME_CACHE_MAX_ENTRIES = 16;
const resolvedNodeRuntimeExecutables = /* @__PURE__ */ new Map();
const execFileAsync = promisify(execFile);
function buildProbeEnv(e) {
const t = {};
for (const n of [
"SystemRoot",
"SYSTEMROOT",
"WINDIR",
"TEMP",
"TMP",
"TMPDIR"
]) {
const r = e[n];
if (r) t[n] = r;
}
return t;
}
function buildNodeExecutableCandidates(e) {
const t = process.platform === "win32" ? (e.PATHEXT ?? ".EXE;.CMD;.BAT;.COM").split(";").filter(Boolean).map((e) => `node${e.toLowerCase()}`) : ["node"];
const n = nodeRuntimePathEntries(e.PATH).flatMap((e) => t.map((t) => path.join(e, t)));
if (process.platform === "darwin") n.push("/opt/homebrew/bin/node", "/opt/homebrew/opt/node/bin/node", "/usr/local/bin/node", "/usr/local/opt/node/bin/node", "/usr/bin/node");
else if (process.platform === "linux") n.push("/usr/local/bin/node", "/usr/bin/node");
return [...new Set(n)];
}
function prepareNodeRuntimeResolution(e) {
const t = e?.env ?? process.env;
const n = e?.requiredFlag;
if (!process.versions.bun && isSupportedOpenClawNodeVersion(process.versions.node) && (!n || process.allowedNodeEnvironmentFlags.has(n))) return { resolved: process.execPath };
const r = [
process.platform,
process.versions.bun ?? process.versions.node,
n ?? "",
t.PATH ?? "",
t.PATHEXT ?? ""
].join("\0");
const i = resolvedNodeRuntimeExecutables.get(r);
if (i !== void 0) return { resolved: i ?? void 0 };
return {
cacheKey: r,
requiredFlag: n,
candidates: buildNodeExecutableCandidates(t),
env: buildProbeEnv(t)
};
}
function recordNodeRuntimeResolution(e, t) {
resolvedNodeRuntimeExecutables.set(e, t ?? null);
while (resolvedNodeRuntimeExecutables.size > NODE_RUNTIME_CACHE_MAX_ENTRIES) {
const e = resolvedNodeRuntimeExecutables.keys().next().value;
if (e === void 0) break;
resolvedNodeRuntimeExecutables.delete(e);
}
return t;
}
/** Synchronous compatibility for CLI tooling and the published process-runtime SDK. */
function resolveNodeRuntimeExecutable(e) {
const t = prepareNodeRuntimeResolution(e);
if (!("cacheKey" in t)) return t.resolved;
const { cacheKey: n, requiredFlag: r, candidates: i, env: a } = t;
const o = createRuntimeProcessScriptFile("nodeRuntimeProbe");
let s;
try {
for (const e of i) try {
const t = execFileSync(e, [fileURLToPath(o.url), r ?? ""], {
encoding: "utf8",
env: a,
stdio: [
"ignore",
"pipe",
"ignore"
],
timeout: NODE_RUNTIME_PROBE_TIMEOUT_MS
}).trim();
if (t) {
s = t;
break;
}
} catch {}
} finally {
o.dispose();
}
return recordNodeRuntimeResolution(n, s);
}
/** Runtime requests must not create probe files or wait for child exit on the main thread. */
async function resolveNodeRuntimeExecutableAsync(e) {
const t = prepareNodeRuntimeResolution(e);
if (!("cacheKey" in t)) return t.resolved;
const { cacheKey: n, requiredFlag: r, candidates: i, env: a } = t;
const o = await createRuntimeProcessScriptFileAsync("nodeRuntimeProbe");
let s;
try {
for (const e of i) try {
const { stdout: t } = await execFileAsync(e, [fileURLToPath(o.url), r ?? ""], {
encoding: "utf8",
env: a,
timeout: NODE_RUNTIME_PROBE_TIMEOUT_MS
});
if (t.trim()) {
s = t.trim();
break;
}
} catch {}
} finally {
await o.dispose();
}
return recordNodeRuntimeResolution(n, s);
}
//#endregion
export { resolveNodeRuntimeExecutableAsync as n, resolveNodeRuntimeExecutable as t };