UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

5 lines • 4.32 kB
import{c as clearFsSafeEnvFallback,u as normalizeFsSafeNativeEnv}from"./env-fLMEolk5.mjs";import{a as resolveRequiredHomeDir}from"./home-dir-DEN5DisQ.mjs";import{t as resolveConfigDir}from"./config-dir-DaqnOvHp.mjs";import{o as normalizeEnvVarKey}from"./host-env-security-CA3pVx5Z.mjs";import{t as createSubsystemLogger}from"./subsystem-DLCDFvCH.mjs";import fs from"node:fs";import path from"node:path";import os from"node:os";import{readRegularFile,readRegularFileSync}from"@openclaw/fs-safe/advanced";import{parse as parse$1}from"dotenv"; //!src/infra/dotenv-global-core.ts const MAX_DOTENV_FILE_BYTES=1048576;function parseDotEnvFile(e,t){const n=[];for(const[r,i]of Object.entries(parse$1(t))){const t=normalizeEnvVarKey(r,{portable:true});if(t&&(e.entryFilter?.(t,i)??true))n.push({key:t,value:i})}return{filePath:e.filePath,entries:n}}function readDotEnvFileCore(e){let t;try{const n=fs.realpathSync(e.filePath);const{buffer:r}=readRegularFileSync({filePath:n,maxBytes:MAX_DOTENV_FILE_BYTES});t=r}catch{return null}return parseDotEnvFile(e,t)}async function readDotEnvFileAsyncCore(e){let t;try{const n=await fs.promises.realpath(e.filePath);const{buffer:r}=await readRegularFile({filePath:n,maxBytes:MAX_DOTENV_FILE_BYTES});t=r}catch{return null}return parseDotEnvFile(e,t)}function loadParsedDotEnvFiles(e,t,n,r){clearFsSafeEnvFallback(t);const i=new Set(Object.keys(t));const canonicalizeKey=e=>normalizeEnvVarKey(e,{portable:true})?.toUpperCase()??null;const a=new Set([...n??[]].flatMap(e=>{const t=canonicalizeKey(e);return t?[t]:[]}));const o=/* @__PURE__ */ new Map;const s=/* @__PURE__ */ new Map;const c=/* @__PURE__ */ new Map;for(const n of e)for(const{key:e,value:r}of n.entries){const l=canonicalizeKey(e);const u=l!==null&&a.has(l);const d=u&&l?l:e;if(i.has(e)&&!u)continue;const f=s.get(d);if(f){if(f.value!==r){const t=`${f.filePath}\u0000${n.filePath}`;const r=o.get(t);if(r)r.keys.add(e);else o.set(t,{keptPath:f.filePath,ignoredPath:n.filePath,keys:/* @__PURE__ */ new Set([e])})}continue}s.set(d,{value:r,filePath:n.filePath});if(t[e]===void 0||u){if(u){for(const e of i)if(canonicalizeKey(e)===l)t[e]=r}t[e]=r;const a=c.get(n.filePath);if(a)a.push(e);else c.set(n.filePath,[e])}}normalizeFsSafeNativeEnv(t);for(const e of o.values()){const t=[...e.keys].toSorted();r?.(`Conflicting values in ${e.keptPath} and ${e.ignoredPath} for ${t.join(`, `)}; keeping ${e.keptPath}.`,{keptPath:e.keptPath,ignoredPath:e.ignoredPath,keys:t})}return c}function resolveGlobalDotEnvFiles(e,t){const n=e.stateEnvPath??path.join(resolveConfigDir(t),`.env`);const r=[.../* @__PURE__ */ new Set([n,...e.additionalEnvPaths??[]])];const i=resolveRequiredHomeDir(t,os.homedir);const a=path.join(i,`.openclaw`,`.env`);const o=t.OPENCLAW_STATE_DIR?.trim()!==void 0&&path.resolve(n)!==path.resolve(a);const s={entryFilter:e.entryFilter};return{globalFiles:r.map(e=>Object.assign({},s,{filePath:e})),gatewayFile:o?void 0:{...s,filePath:path.join(i,`.config`,`openclaw`,`gateway.env`)}}}function applyGlobalDotEnvFiles(e,t,n,r,i){const a=[...e,t].filter(e=>e!==null);const o=loadParsedDotEnvFiles(a,n,r,i);return{dotenvPresentKeys:[...new Set(a.flatMap(e=>e.entries.map(({key:e})=>e)))],stateEnvAppliedKeys:e.flatMap(e=>e?o.get(e.filePath)??[]:[]),gatewayEnvAppliedKeys:t?o.get(t.filePath)??[]:[]}}function loadGlobalRuntimeDotEnvFilesCore(e={}){const t=e.env??process.env;const{globalFiles:n,gatewayFile:r}=resolveGlobalDotEnvFiles(e,t);return applyGlobalDotEnvFiles(n.map(readDotEnvFileCore),r?readDotEnvFileCore(r):null,t,e.overrideKeys,e.onWarning)}async function loadGlobalRuntimeDotEnvFilesAsyncCore(e){const{env:t}=e;const{globalFiles:n,gatewayFile:r}=resolveGlobalDotEnvFiles(e,t);const i=[];for(const e of n)i.push(await readDotEnvFileAsyncCore(e));return applyGlobalDotEnvFiles(i,r?await readDotEnvFileAsyncCore(r):null,t,e.overrideKeys,e.onWarning)} //!src/infra/dotenv-global.ts const logger=createSubsystemLogger(`infra:dotenv`);function loadGlobalRuntimeDotEnvFiles(e={}){return loadGlobalRuntimeDotEnvFilesCore({...e,onWarning:logger.warn})}async function loadGlobalRuntimeDotEnvFilesAsync(e){return await loadGlobalRuntimeDotEnvFilesAsyncCore({...e,onWarning:logger.warn})}export{readDotEnvFileCore as i,loadGlobalRuntimeDotEnvFilesAsync as n,readDotEnvFileAsyncCore as r,loadGlobalRuntimeDotEnvFiles as t};