openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
3 lines • 2.32 kB
JavaScript
import{u as readChildRuntimeViability}from"./child-runtime-viability-1cqYEa1U.mjs";import{a as raiseChildOomScore,i as isWrapped,n as OOM_SCORE_WRAP_SCRIPT,r as OOM_SCORE_WRAP_SHELL,s as shouldAdjustChildOomScore,t as OOM_SCORE_RESTORE_EXEC_ENV_SCRIPT}from"./linux-oom-score-spawn-B1qsd3-X.mjs";import fs from"node:fs";
//!src/process/linux-oom-score.ts
const SHELL_INIT_ENV_CARRIERS=[[`BASH_ENV`,`OC_INTERNAL_OOM_EXEC_BASH_ENV`],[`ENV`,`OC_INTERNAL_OOM_EXEC_ENV`],[`CDPATH`,`OC_INTERNAL_OOM_EXEC_CDPATH`],[`PS4`,`OC_INTERNAL_OOM_EXEC_PS4`]];const NON_RESTORABLE_BASH_ENV_KEY=/^(?:SHELLOPTS|BASHOPTS|BASH_FUNC_.*)$/u;let cachedShellAvailable=null;function defaultShellAvailable(){if(cachedShellAvailable!==null)return cachedShellAvailable;try{cachedShellAvailable=fs.statSync(OOM_SCORE_WRAP_SHELL).isFile()}catch{cachedShellAvailable=false}return cachedShellAvailable}function hardenShellEnv(e){const t={...e??process.env};for(const[e]of SHELL_INIT_ENV_CARRIERS)delete t[e];for(const e of Object.keys(t))if(NON_RESTORABLE_BASH_ENV_KEY.test(e))delete t[e];return t}function prepareOomScoreAdjustedSpawnWithExecEnvPolicy(e,t=[],n,r=false){const i=[...t];const a={command:e,args:i,...n?.argv0===void 0?{}:{argv0:n.argv0},env:n?.env,wrapped:false};if(!e||e.startsWith(`-`)||!shouldAdjustChildOomScore(n)||e===process.execPath&&!readChildRuntimeViability().available)return a;if(n?.argv0!==void 0&&n.argv0!==e)return{...a,afterSpawn:raiseChildOomScore};if(!(n?.shellAvailable??defaultShellAvailable)())return a;if(!r&&isWrapped(e,i))return{command:e,args:i,env:hardenShellEnv(n?.env),wrapped:true};const o=n?.env??process.env;if(r&&Object.entries(o).some(([e,t])=>t!==void 0&&NON_RESTORABLE_BASH_ENV_KEY.test(e)))return a;const s=hardenShellEnv(o);if(r)for(const[e,t]of SHELL_INIT_ENV_CARRIERS){if(o[t]!==void 0)return a;delete s[t];if(o[e]!==void 0)s[t]=o[e]}return{command:OOM_SCORE_WRAP_SHELL,args:[`-c`,r?OOM_SCORE_RESTORE_EXEC_ENV_SCRIPT:OOM_SCORE_WRAP_SCRIPT,e,...i],env:s,wrapped:true}}function prepareOomScoreAdjustedSpawn(e,t=[],n){return prepareOomScoreAdjustedSpawnWithExecEnvPolicy(e,t,n)}function prepareOomScoreAdjustedSpawnPreservingExecEnv(e,t=[],n){return prepareOomScoreAdjustedSpawnWithExecEnvPolicy(e,t,n,true)}export{prepareOomScoreAdjustedSpawnPreservingExecEnv as n,prepareOomScoreAdjustedSpawn as t};