openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
3 lines • 3.29 kB
JavaScript
import{t as nodeRuntimePathEntries}from"./node-runtime-env-BKb2iRg8.mjs";import{f as isSupportedOpenClawNodeVersion}from"./node-version-EaZf6kHy.mjs";import{n as createRuntimeProcessScriptFileAsync,t as createRuntimeProcessScriptFile}from"./runtime-process-scripts.generated-eKTqHwuf.mjs";import{fileURLToPath}from"node:url";import path from"node:path";import{execFile,execFileSync}from"node:child_process";import{promisify}from"node:util";
//!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}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)}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)}export{resolveNodeRuntimeExecutableAsync as n,resolveNodeRuntimeExecutable as t};