openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
121 lines (120 loc) • 3.99 kB
JavaScript
import { t as isBlockedObjectKey } from "./prototype-keys-CLGi5xDj.mjs";
import { i as getOrCreatePromise } from "./lazy-promise-DiCvjz9w.mjs";
import { t as isOsRequirementSatisfied } from "./requirements-cQci8f0k.mjs";
import fs from "node:fs";
import path from "node:path";
//#region src/shared/config-eval.ts
/** Checks a config path with fallback defaults only when the path is unresolved. */
function isConfigPathTruthyWithDefaults(e, t, n) {
const r = t.split(".").filter(Boolean);
let i = e;
for (const e of r) {
if (typeof i !== "object" || i === null || isBlockedObjectKey(e)) {
i = void 0;
break;
}
i = i[e];
}
if (i === void 0 && !r.some((e) => isBlockedObjectKey(e)) && Object.hasOwn(n, t)) return n[t] ?? false;
return typeof i === "string" ? i.trim().length > 0 : i !== void 0 && i !== null && i !== false && i !== 0;
}
/** Enforces OS compatibility before allowing `always` to bypass runtime requirements. */
function evaluateRuntimeEligibility(e) {
if (!isOsRequirementSatisfied(e.os ?? [], [e.platform ?? process.platform, ...e.remotePlatforms ?? []])) return false;
const t = e.requires;
if (e.always === true || !t) return true;
for (const n of t.env ?? []) if (!e.hasEnv(n)) return false;
for (const n of t.config ?? []) if (!e.isConfigPathTruthy(n)) return false;
for (const n of t.bins ?? []) if (!e.hasBin(n) && !e.hasRemoteBin?.(n)) return false;
const n = t.anyBins ?? [];
return n.length === 0 || n.some((t) => e.hasBin(t)) || Boolean(e.hasAnyRemoteBin?.(n));
}
function windowsPathExtensions(e) {
return ["", ...(e !== void 0 ? e.split(";").map((e) => e.trim()) : [
".EXE",
".CMD",
".BAT",
".COM"
]).filter(Boolean)];
}
const pendingBinaryAccess = /* @__PURE__ */ new Map();
let binaryCache;
function resolveBinaryCache() {
const e = process.platform === "win32";
const t = process.env.PATH ?? "";
const n = e ? process.env.PATHEXT ?? "" : "";
if (binaryCache?.path !== t || binaryCache.pathExt !== n) binaryCache = {
path: t,
pathExt: n,
hits: /* @__PURE__ */ new Set()
};
return binaryCache;
}
function resolveBinarySearch(e = resolveBinaryCache()) {
const t = process.platform === "win32";
return {
cache: e,
parts: e.path.split(path.delimiter).filter(Boolean),
extensions: t ? windowsPathExtensions(process.env.PATHEXT) : [""]
};
}
function* binaryCandidates(e, t) {
for (const n of e.parts) for (const r of e.extensions) yield path.join(n, t + r);
}
function hasBinary(e) {
const t = resolveBinaryCache();
if (t.hits.has(e)) return true;
const n = resolveBinarySearch(t);
for (const t of binaryCandidates(n, e)) try {
if (!fs.statSync(t, { throwIfNoEntry: false })?.isFile()) continue;
fs.accessSync(t, fs.constants.X_OK);
n.cache.hits.add(e);
return true;
} catch {}
return false;
}
/** Binary facts belong to one preparation; probe installations and removals again next time. */
async function prepareBinaryAvailability(e, t) {
const n = resolveBinarySearch();
const r = process.cwd();
const i = new Set(e).values();
const a = /* @__PURE__ */ new Set();
let o;
const probe = async (e) => {
for (const i of binaryCandidates(n, e)) {
if (o) return;
t?.();
try {
const e = path.resolve(r, i);
if (!await getOrCreatePromise(pendingBinaryAccess, e, async () => {
await fs.promises.access(e, fs.constants.X_OK);
return (await fs.promises.stat(e)).isFile();
}, { evictOnSettled: true })) continue;
} catch {
continue;
}
t?.();
a.add(e);
return;
}
};
const worker = async () => {
try {
for (;;) {
if (o) return;
t?.();
const e = i.next();
if (e.done) return;
await probe(e.value);
}
} catch (e) {
o ??= { error: e };
}
};
await Promise.all(Array.from({ length: 4 }, worker));
if (o) throw o.error;
t?.();
return { hasBinary: (e) => a.has(e) };
}
//#endregion
export { prepareBinaryAvailability as i, hasBinary as n, isConfigPathTruthyWithDefaults as r, evaluateRuntimeEligibility as t };