UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

55 lines (54 loc) • 1.91 kB
import fs from "node:fs"; import path from "node:path"; //#region src/infra/disk-space.ts const LOW_DISK_SPACE_WARNING_THRESHOLD_BYTES = 1073741824; function finiteNonNegativeNumber(e) { const t = Number(e); return Number.isFinite(t) && t >= 0 ? t : null; } function findExistingDiskSpacePath(e) { let t = path.resolve(e); while (true) try { return fs.statSync(t).isDirectory() ? t : path.dirname(t); } catch { const e = path.dirname(t); if (e === t) return null; t = e; } } /** Reads available bytes for the volume containing a target path. */ function tryReadDiskSpace(e) { const t = findExistingDiskSpacePath(e); if (!t) return null; try { const n = fs.statfsSync(t); const r = finiteNonNegativeNumber(n.bsize); const i = finiteNonNegativeNumber(n.bavail); if (r === null || i === null) return null; const a = finiteNonNegativeNumber(n.blocks); return { targetPath: e, checkedPath: t, availableBytes: r * i, totalBytes: a === null ? null : r * a }; } catch { return null; } } /** Formats byte counts for compact operator-facing disk-space warnings. */ function formatDiskSpaceBytes(e) { const t = e / 1048576; const n = Math.max(0, Math.round(t)); if (n < 1024) return `${n} MiB`; const r = t / 1024; return `${r.toFixed(r < 10 ? 1 : 0)} GiB`; } /** Builds a soft low-disk warning for setup/update flows without failing the operation. */ function createLowDiskSpaceWarning(e) { const t = tryReadDiskSpace(e.targetPath); if (!t || t.availableBytes >= LOW_DISK_SPACE_WARNING_THRESHOLD_BYTES) return null; return `Low disk space near ${path.resolve(t.targetPath) === path.resolve(t.checkedPath) ? t.checkedPath : `${t.targetPath} (volume checked at ${t.checkedPath})`}: ${formatDiskSpaceBytes(t.availableBytes)} available; ${e.purpose} may fail.`; } //#endregion export { formatDiskSpaceBytes as n, tryReadDiskSpace as r, createLowDiskSpaceWarning as t };