openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
124 lines (123 loc) • 3.92 kB
JavaScript
import { o as racePromiseWithAbortSignal } from "./src-BPmQBLJp.mjs";
import "./abort-signal-D4Q6SEw7.mjs";
import { n as resolvePreferredOpenClawTmpDir } from "./tmp-openclaw-dir-Bf0oU1Cn.mjs";
import { t as createSubsystemLogger } from "./subsystem-BHJiy01V.mjs";
import { n as runCommandWithTimeout } from "./exec-runner-Br73vG-X.mjs";
import "./exec-aRIvS9s4.mjs";
import { n as MAX_WORKER_BUNDLE_ARCHIVE_BYTES } from "./worker-bundle-limits-Bj1ih83T.mjs";
import { t as resolveNpmCommand } from "./npm-command-kcBVU3OP.mjs";
import { t as prepareNodeArtifactInWorker } from "./node-bootstrap-artifact-worker-BR0nDaYP.mjs";
import path from "node:path";
import { createHash } from "node:crypto";
import fs from "node:fs/promises";
//#region src/gateway/worker-environments/node-bootstrap-install-lock.ts
const log = createSubsystemLogger("gateway/worker-environments");
/** A fresh project keeps npm's optional platform variants independent of the Gateway's OS. */
async function prepareNodeBootstrapInstallLock(e) {
const t = path.dirname(e.tarballPath);
const n = path.join(t, "install-project");
try {
await fs.mkdir(n);
await fs.writeFile(path.join(n, "package.json"), JSON.stringify({
private: true,
dependencies: { openclaw: "file:../node-runtime.tgz" }
}));
const e = {
...Object.fromEntries([
"PATH",
"Path",
"SystemRoot",
"COMSPEC",
"PATHEXT"
].map((e) => [e, process.env[e]])),
HOME: n,
USERPROFILE: n,
npm_config_cache: path.join(n, "cache"),
npm_config_userconfig: path.join(n, ".npmrc")
};
await fs.writeFile(e.npm_config_userconfig, "");
const r = await runCommandWithTimeout(resolveNpmCommand([
"install",
"--package-lock-only",
"--omit=dev",
"--ignore-scripts",
"--no-audit",
"--no-fund"
]), {
cwd: n,
baseEnv: e,
timeoutMs: 12e4,
maxOutputBytes: 65536
});
if (r.code !== 0 || r.termination !== "exit") {
const e = /npm (?:error|ERR!) code ([A-Z0-9_]+)/u.exec(r.stderr)?.[1];
throw new Error(`${e ?? r.termination} (exit ${r.code ?? "unknown"})`);
}
const i = await fs.readFile(path.join(n, "package-lock.json"));
if (i.length === 0 || i.length > 536870912) throw new Error("Generated runtime lock is empty or exceeds the transfer limit");
const a = path.join(t, "package-lock.json");
await fs.rename(path.join(n, "package-lock.json"), a);
return {
tarballPath: a,
tarballBytes: i.length,
tarballSha256: createHash("sha256").update(i).digest("hex")
};
} catch (e) {
log.warn(`Cloud worker runtime lock generation failed; using unlocked installs: ${String(e)}`);
return;
} finally {
await fs.rm(n, {
recursive: true,
force: true
}).catch(() => void 0);
}
}
//#endregion
//#region src/gateway/worker-environments/node-bootstrap-artifact.ts
/** Owns one immutable deployment artifact for this Gateway process, never the live installation. */
function createNodeBootstrapArtifactProvider(e) {
let t;
let n;
let r = false;
return {
async prepare(i) {
i?.throwIfAborted();
if (r) throw new Error("Node bootstrap artifact provider is closed");
t ??= Promise.resolve().then(async () => {
try {
n = await fs.mkdtemp(path.join(resolvePreferredOpenClawTmpDir(), "openclaw-node-runtime-"));
const t = await prepareNodeArtifactInWorker("bootstrap", {
options: e,
temporaryRoot: n
});
return {
...t,
installLock: await prepareNodeBootstrapInstallLock(t)
};
} catch (e) {
if (n) await fs.rm(n, {
recursive: true,
force: true
});
n = void 0;
t = void 0;
throw e;
}
});
return racePromiseWithAbortSignal(t, i);
},
async close() {
r = true;
await t?.catch(() => void 0);
if (n) {
await fs.rm(n, {
recursive: true,
force: true
});
n = void 0;
}
}
};
}
//#endregion
export { createNodeBootstrapArtifactProvider };