openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
74 lines (73 loc) • 2.88 kB
JavaScript
import path from "node:path";
//#region packages/media-core/src/inbound-path-policy.ts
const WILDCARD_SEGMENT = "*";
const WINDOWS_DRIVE_ABS_RE = /^[A-Za-z]:\//;
const WINDOWS_DRIVE_ROOT_RE = /^[A-Za-z]:$/;
function normalizePosixAbsolutePath(e) {
const t = e.trim();
if (!t || t.includes("\0")) return;
const n = path.posix.normalize(t.replaceAll("\\", "/"));
if (!(n.startsWith("/") || WINDOWS_DRIVE_ABS_RE.test(n)) || n === "/") return;
const r = n.endsWith("/") ? n.slice(0, -1) : n;
if (WINDOWS_DRIVE_ROOT_RE.test(r)) return;
return WINDOWS_DRIVE_ABS_RE.test(r) ? r.toLowerCase() : r;
}
function splitPathSegments(e) {
return e.split("/").filter(Boolean);
}
function joinAbsolutePathSegments(e, t) {
const n = t.join("/");
if (!WINDOWS_DRIVE_ABS_RE.test(e)) return `/${n}`;
return t.length === 1 ? `${n}/` : n;
}
function normalizeInboundPathRootPattern(e) {
const t = normalizePosixAbsolutePath(e);
if (!t) return;
const n = splitPathSegments(t);
return n.length > 0 && n.every((e) => e === WILDCARD_SEGMENT || !e.includes("*")) ? t : void 0;
}
/** Validates an absolute inbound root pattern with whole-segment wildcards only. */
function isValidInboundPathRootPattern(e) {
return normalizeInboundPathRootPattern(e) !== void 0;
}
/** Normalizes configured inbound attachment roots, dropping invalid or duplicate patterns. */
function normalizeInboundPathRoots(e) {
const t = [];
const n = /* @__PURE__ */ new Set();
for (const r of e ?? []) {
if (typeof r !== "string") continue;
const e = normalizeInboundPathRootPattern(r);
if (!e || n.has(e)) continue;
n.add(e);
t.push(e);
}
return t;
}
/** Merges inbound attachment root lists while preserving first-seen priority. */
function mergeInboundPathRoots(...e) {
return normalizeInboundPathRoots(e.flatMap((e) => e ?? []));
}
/** Resolves the concrete lexical root matched by an inbound path pattern. */
function resolveInboundPathRoot(e) {
const t = normalizePosixAbsolutePath(e.filePath);
if (!t) return;
const n = normalizeInboundPathRoots(e.roots);
const r = n.length > 0 ? n : normalizeInboundPathRoots(e.fallbackRoots ?? void 0);
const i = splitPathSegments(t);
for (const e of r) {
const n = splitPathSegments(e);
if (i.length < n.length || n.some((e, t) => e !== WILDCARD_SEGMENT && e !== i[t])) continue;
const r = i.slice(0, n.length);
const a = n.indexOf(WILDCARD_SEGMENT);
return {
anchorRoot: joinAbsolutePathSegments(t, a === -1 ? r : n.slice(0, a)),
matchedRoot: joinAbsolutePathSegments(t, r)
};
}
}
/** Checks whether a candidate inbound media path is covered by configured or fallback roots. */
function isInboundPathAllowed(e) {
return resolveInboundPathRoot(e) !== void 0;
}
//#endregion
export { resolveInboundPathRoot as a, normalizeInboundPathRoots as i, isValidInboundPathRootPattern as n, mergeInboundPathRoots as r, isInboundPathAllowed as t };