openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
133 lines (132 loc) • 4.06 kB
JavaScript
import { t as isPlainObject } from "./plain-object-CGY5w7fM.mjs";
import { t as appendConfigPathSegment } from "./dot-path-BJ4tyWKJ.mjs";
import { m as parseEnvTemplateSecretRef } from "./types.secrets-BmNLLnHp.mjs";
//#region src/config/env-substitution.ts
const ENV_VAR_NAME_PATTERN = /^[A-Z_][A-Z0-9_]*$/;
const DEFAULT_VALUE_OPERATOR = ":-";
/** Error thrown when a config value references a missing or empty environment variable. */
var MissingEnvVarError = class extends Error {
constructor(e, t) {
super(`Missing env var "${e}" referenced at config path: ${t}`);
this.varName = e;
this.configPath = t;
this.name = "MissingEnvVarError";
}
};
/** Rebuilds the authored placeholder text for a parsed token. */
function renderEnvTemplateToken(e) {
return e.defaultValue === void 0 ? `\${${e.name}}` : `\${${e.name}${DEFAULT_VALUE_OPERATOR}${e.defaultValue}}`;
}
/** Shares the substitution grammar with write-back preservation, in authoring order. */
function scanEnvTemplateTokens(e) {
return Array.from(iterateEnvTemplateTokens(e), (e) => ({
kind: e.kind,
name: e.name,
defaultValue: e.defaultValue
}));
}
function* iterateEnvTemplateTokens(e) {
for (let t = e.indexOf("$"); t !== -1; t = e.indexOf("$", t + 1)) {
const n = e[t + 1] === "$" && e[t + 2] === "{";
if (!n && e[t + 1] !== "{") continue;
const r = t + (n ? 3 : 2);
const i = e.indexOf("}", r);
if (i === -1) continue;
const a = e.slice(r, i);
const o = a.indexOf(DEFAULT_VALUE_OPERATOR);
const s = o === -1 ? a : a.slice(0, o);
const c = o === -1 ? void 0 : a.slice(o + 2);
if (!ENV_VAR_NAME_PATTERN.test(s) || c?.includes("$") || c?.includes("{")) continue;
yield {
kind: n ? "escaped" : "substitution",
name: s,
defaultValue: c,
start: t,
end: i
};
t = i;
}
}
function substituteString(e, t, n, r) {
if (!e.includes("$")) return e;
const i = parseEnvTemplateSecretRef(e);
if (i && !containsEnvVarReference(e)) r?.onPendingEnvSecretRef?.(i.id, n);
const a = [];
let o = 0;
for (const s of iterateEnvTemplateTokens(e)) {
a.push(e.slice(o, s.start));
o = s.end + 1;
if (s.kind === "escaped") {
a.push(renderEnvTemplateToken(s));
continue;
}
const c = t[s.name];
if (c === void 0 || c === "") {
if (s.defaultValue !== void 0) {
a.push(s.defaultValue);
continue;
}
if (r?.onMissing) {
r.onMissing({
varName: s.name,
configPath: n
});
if (i?.id === s.name) r.onPendingEnvSecretRef?.(s.name, n);
a.push(renderEnvTemplateToken(s));
continue;
}
throw new MissingEnvVarError(s.name, n);
}
if (i?.id === s.name) r?.onResolvedEnvSecretRef?.(s.name, n);
a.push(c);
}
a.push(e.slice(o));
return a.join("");
}
/** Detects unescaped `${VAR}` references without treating escaped `$${VAR}` as references. */
function containsEnvVarReference(e) {
for (const t of iterateEnvTemplateTokens(e)) if (t.kind === "substitution") return true;
return false;
}
/** Resolve config string templates, preserving unresolved text when onMissing is supplied. */
function resolveConfigEnvVars(e, t = process.env, n) {
const r = [];
const visit = (e, i) => {
if (typeof e === "string") return substituteString(e, t, i, n);
if (Array.isArray(e)) {
const t = e.length;
const n = [];
n.length = t;
let a = 0;
r.push(() => {
while (a < t) {
const t = a++;
if (t in e) {
n[t] = visit(e[t], `${i}[${t}]`);
return true;
}
}
return false;
});
return n;
}
if (isPlainObject(e)) {
const t = {};
const n = Object.entries(e)[Symbol.iterator]();
r.push(() => {
const e = n.next();
if (e.done) return false;
const [r, a] = e.value;
t[r] = visit(a, appendConfigPathSegment(i, r));
return true;
});
return t;
}
return e;
};
const i = visit(e, "");
for (let e = r.at(-1); e; e = r.at(-1)) if (!e()) r.pop();
return i;
}
//#endregion
export { scanEnvTemplateTokens as a, resolveConfigEnvVars as i, containsEnvVarReference as n, renderEnvTemplateToken as r, MissingEnvVarError as t };