UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

96 lines (95 loc) • 2.8 kB
import { fileURLToPath } from "node:url"; import path from "node:path"; import { spawn } from "node:child_process"; import { WindowsJob } from "@openclaw/proc-safe/windows-job"; //#region scripts/lib/direct-run.mjs /** * Return whether a direct-run path points at the current module path. * @internal Directly tested script implementation detail. * @param {string | undefined} directPath * @param {string | undefined} modulePath * @param {NodeJS.Platform} [platform] * @returns {boolean} */ function isDirectRunPath(e, t, n = process.platform) { if (!e || !t) return false; const r = n === "win32" ? path.win32 : path; /** @type {(value: string) => string} */ const i = n === "win32" ? (e) => r.resolve(e).toLowerCase() : (e) => r.resolve(e); return i(e) === i(t); } /** * Return whether a direct-run path points at the current module URL. * @param {string | undefined} directPath * @param {string} moduleUrl * @param {NodeJS.Platform} [platform] * @returns {boolean} */ function isDirectRunUrl(e, t, n = process.platform) { return isDirectRunPath(e, fileURLToPath(t), n); } //#endregion //#region scripts/lib/managed-windows-job-launcher.mts const name = process.argv[2]; const send = (e) => process.send?.({ job: name, ...e }); const fail = (e, t = "error") => { process.exitCode = 1; if (process.connected) process.send?.({ job: name, type: t, error: e instanceof Error ? e.message : String(e), ...e && typeof e === "object" && "code" in e ? { code: e.code } : {} }, () => process.disconnect?.()); }; if (isDirectRunUrl(process.argv[1], import.meta.url)) try { if (!name || !process.connected) throw new Error("Windows command Job handoff is missing"); const e = WindowsJob.open(name); if (!e) throw new Error("Windows command Job owner is gone"); try { e.assignCurrentProcess(); } finally { e.close(); } process.once("message", (e) => { try { const t = spawn(e.command, e.args, { ...e.options, stdio: e.stdio }); t.once("error", fail); t.once("spawn", () => { process.send?.({ job: name, type: "spawned", pid: t.pid }, (t) => { if (t) fail(t); else if (!e.stdio.includes("ipc")) process.disconnect?.(); }); }); t.once("exit", (e) => { process.exitCode = e ?? 1; }); if (e.stdio.includes("ipc")) { process.on("message", (e) => { if (e !== null && t.connected) t.send(e, (e) => e && fail(e)); }); t.on("message", (e) => { if (process.connected) process.send?.(e, (e) => e && fail(e)); }); process.once("disconnect", () => t.connected && t.disconnect()); t.once("disconnect", () => process.connected && process.disconnect?.()); } } catch (e) { fail(e); } }); send({ type: "ready" }); } catch (e) { fail(e, "job-error"); } //#endregion export {};