openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
52 lines (51 loc) • 2.54 kB
JavaScript
import { canUseRootFileOpen, matchRootFileOpenFailure, matchRootFileOpenFailure as matchRootFileOpenFailure$1, openRootFile, openRootFileSync as openRootFileSync$1, readFileDescriptorBounded, readFileDescriptorBoundedSync } from "@openclaw/fs-safe/advanced";
import { FsSafeError } from "@openclaw/fs-safe/errors";
//#region src/infra/boundary-file-read.ts
const MISSING_PATH_ERROR_CODES = /* @__PURE__ */ new Set(["ENOENT", "ENOTDIR"]);
function readFailureErrorCode(e) {
const t = e && typeof e === "object" ? e.code : void 0;
return typeof t === "string" && t ? t : void 0;
}
function isRootFileMissingFailure(e) {
return e.reason === "path" && MISSING_PATH_ERROR_CODES.has(readFailureErrorCode(e.error) ?? "");
}
/**
* Describes a root-scoped open failure without collapsing every cause into a
* containment violation. Only `validation` means the path failed the boundary or
* alias check; a missing artifact or an unreadable descriptor is an ordinary
* operational state, and reporting those as escapes sends operators hunting a
* security incident that never happened.
*/
function describeRootFileOpenFailure(e) {
const unreadable = (t) => `${e.subject} could not be read${t ? ` (${t})` : ""}: ${e.filePath}`;
return matchRootFileOpenFailure(e.failure, {
path: (t) => {
const n = readFailureErrorCode(t.error);
return isRootFileMissingFailure(t) ? `${e.subject} not found: ${e.filePath}` : unreadable(n);
},
validation: () => `${e.subject} escapes ${e.boundaryLabel} or fails alias checks: ${e.filePath}`,
fallback: (e) => unreadable(readFailureErrorCode(e.error))
});
}
function preserveOpenClawOverflowError(e, t) {
if (e instanceof FsSafeError && e.code === "too-large") throw new RangeError(`File exceeds ${t} bytes`, { cause: e });
throw e;
}
/** Read a pinned descriptor without changing OpenClaw's user-facing overflow error. */
async function readFileDescriptorBounded$1(e, t) {
try {
return await readFileDescriptorBounded(e, t);
} catch (e) {
return preserveOpenClawOverflowError(e, t);
}
}
/** Synchronous variant for callers that own a pinned descriptor. */
function readFileDescriptorBoundedSync$1(e, t) {
try {
return readFileDescriptorBoundedSync(e, t);
} catch (e) {
return preserveOpenClawOverflowError(e, t);
}
}
//#endregion
export { openRootFile as a, readFileDescriptorBoundedSync$1 as c, matchRootFileOpenFailure$1 as i, describeRootFileOpenFailure as n, openRootFileSync$1 as o, isRootFileMissingFailure as r, readFileDescriptorBounded$1 as s, canUseRootFileOpen as t };