UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

90 lines (89 loc) • 2.79 kB
import path from "node:path"; import fs from "node:fs/promises"; import { FsSafeError } from "@openclaw/fs-safe/errors"; import { isNotFoundPathError } from "@openclaw/fs-safe/path"; import { root } from "@openclaw/fs-safe/root"; //#region src/infra/fs-observation-mode.ts const FS_RECONCILE_MS = 3e5; /** Preserve operator polling overrides while letting fs-safe choose the native backend. */ function resolveFsObservationMode(e = process.env) { const t = e.CHOKIDAR_USEPOLLING?.toLowerCase(); return t && t !== "false" && t !== "0" ? "poll" : "auto"; } function resolveFsObservationIntervalMs(e = process.env, t = 100) { const n = Number.parseInt(e.CHOKIDAR_INTERVAL ?? "", 10); return Number.isFinite(n) && n > 0 ? Math.min(2147483647, Math.max(20, n)) : t; } //#endregion //#region src/infra/fs-observation-root.ts /** Probe a caller-selected prefix without following links or comparing entry names. */ async function observationPrefixKind(e, t, n) { const r = e.entries("./" + t, { symlinks: "reject", maxEntries: 1, signal: n }); try { await r.next(); return "directory"; } catch (e) { if (e instanceof FsSafeError) { if (e.code === "symlink") return "symlink"; if (e.code === "not-file") return "other"; if (e.code === "not-found") return "missing"; } throw e; } finally { await r.return?.(); } } /** * Admit an existing prefix of a caller-selected stable boundary, stopping before * a symbolic entry. Each subscription admits its own Root; callers separately * admit trusted canonical targets. This does not scan descendants. */ async function admitObservationRoot(e, t = "reject") { const n = path.resolve(e); let r = path.parse(n).root; for (const e of path.relative(r, n).split(path.sep).filter(Boolean)) { const t = path.join(r, e); try { const e = await fs.lstat(t); if (!e.isDirectory() || e.isSymbolicLink()) break; r = t; } catch (e) { if (!isNotFoundPathError(e)) throw e; break; } } return await root(r, { symlinks: "reject", hardlinks: t }); } //#endregion //#region src/infra/fs-observation-snapshot.ts var ObservationSampleCloseError = class extends Error { constructor(e) { super("File observation metadata handle cleanup failed", { cause: e }); } }; async function readObservationSnapshot(e, t) { let n; try { n = await e.open("./" + t, { symlinks: "reject" }); } catch { return; } try { await n[Symbol.asyncDispose](); } catch (e) { throw new ObservationSampleCloseError(e); } return { size: n.stat.size, mtimeMs: n.stat.mtimeMs }; } //#endregion export { FS_RECONCILE_MS as a, observationPrefixKind as i, readObservationSnapshot as n, resolveFsObservationIntervalMs as o, admitObservationRoot as r, resolveFsObservationMode as s, ObservationSampleCloseError as t };