UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

50 lines (49 loc) • 2 kB
import { o as normalizeEnvVarKey } from "./host-env-security-BrTYS4pH.mjs"; import { n as resolvePreferredOpenClawTmpDir } from "./tmp-openclaw-dir-Bf0oU1Cn.mjs"; import { tempWorkspace } from "@openclaw/fs-safe/temp"; //#region src/infra/container-env-file.ts function getContainerEnvFileEntryIssue(e, t) { if (normalizeEnvVarKey(e, { portable: true }) !== e) return "invalid-name"; if (/[\r\n]/u.test(t)) return "line-break"; return t.includes("\0") ? "nul" : void 0; } /** Stages engine environment values outside process arguments until the caller-owned cleanup. */ async function createContainerEnvFile(e) { let t = ""; const n = Object.entries(e).toSorted(([e], [t]) => e.localeCompare(t)); for (const [e, r] of n) { const n = getContainerEnvFileEntryIssue(e, r); if (n === "invalid-name") throw new Error(`Invalid container environment variable name ${JSON.stringify(e)}; use letters, digits, and underscores without a leading digit.`); if (n === "line-break") throw new Error(`Container environment variable ${e} must have a single-line value because Docker and Podman --env-file entries are line-delimited.`); if (n === "nul") throw new Error(`Container environment variable ${e} must not contain NUL bytes.`); t += `${e}=${r}\n`; } const r = await tempWorkspace({ rootDir: resolvePreferredOpenClawTmpDir(), prefix: "openclaw-container-env-", dirMode: 448, mode: 384 }); try { return { path: await r.store.write("container.env", t, { durable: false }), cleanup: async () => { await r.cleanup(); } }; } catch (e) { await r.cleanup().catch(() => void 0); throw e; } } /** Keeps a private container environment file alive only while its engine operation runs. */ async function withContainerEnvFile(e, t) { const n = await createContainerEnvFile(e); try { return await t(n.path); } finally { await n.cleanup(); } } //#endregion export { getContainerEnvFileEntryIssue as n, withContainerEnvFile as r, createContainerEnvFile as t };