UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

145 lines (144 loc) • 5.79 kB
import { c as hasErrnoCode } from "./paths-0xDagtub.mjs"; import { i as normalizeWindowsPathPreservingCase } from "./path-guards-quCEQx1i.mjs"; import { fstatSync, readdirSync, realpathSync, statSync } from "node:fs"; import path from "node:path"; import { realpath, stat } from "node:fs/promises"; //#region src/infra/sqlite-worker-identity.ts const useDatabaseBirthtime = process.platform !== "linux" && process.platform !== "android"; function databaseFileIdentityKey(e) { return `${e.dev}:${e.ino}`; } function readDatabaseIdentityBirthtime(e) { return useDatabaseBirthtime ? e.birthtimeNs.toString() : "0"; } /** Native SQLite namespaces are filesystem locators, not distinct database owners. */ function normalizeDatabasePath(e) { const t = process.platform === "win32" ? normalizeWindowsPathPreservingCase(e) : e; return process.platform === "win32" && !/^(?:[a-z]:[\\/]|[\\/]{2})/iu.test(t) ? e : t; } function readDatabaseFileIdentity(e) { if (!e || typeof e !== "object" || Array.isArray(e) || !("key" in e) || typeof e.key !== "string" || !/^file:(?:0|[1-9]\d{0,19}):(?:0|[1-9]\d{0,19})$/.test(e.key) || "birthtime" in e && e.birthtime !== void 0 && (typeof e.birthtime !== "string" || !/^-?(?:0|[1-9]\d{0,29})$/.test(e.birthtime))) throw new Error("SQLite snapshot requires a captured physical file identity"); return { key: e.key, ..."birthtime" in e && typeof e.birthtime === "string" ? { birthtime: e.birthtime } : {} }; } function assertDatabaseFileIdentity(e, t) { if (!e.isFile() || `file:${databaseFileIdentityKey(e)}` !== t.key || t.birthtime !== void 0 && readDatabaseIdentityBirthtime(e) !== t.birthtime) throw new Error("SQLite database file identity changed before existing-only open"); } function existingIdentity(e, t, n) { if (!e.isFile()) throw new Error("SQLite worker database path must identify a regular file"); if (e.dev !== t.dev || e.ino !== t.ino || readDatabaseIdentityBirthtime(e) !== readDatabaseIdentityBirthtime(t)) throw new Error("SQLite database pathname changed during admission"); return { key: `file:${databaseFileIdentityKey(e)}`, canonicalPath: normalizeDatabasePath(n), birthtime: readDatabaseIdentityBirthtime(e) }; } function missingAncestorParent(e, t, n) { if (!hasErrnoCode(n, "ENOENT")) throw n; t.unshift(path.basename(e)); const r = path.dirname(e); if (r === e) throw n; return r; } /** In-process owner key only; never rewrite a configured or persisted database locator. */ function resolveDatabasePathKey(e) { let t = path.resolve(e); const n = []; while (true) try { return normalizeDatabasePath(path.join(realpathSync.native(t), ...n)); } catch (e) { t = missingAncestorParent(t, n, e); } } /** Inspect a native-owner path without replacing its diagnostic for a non-file target. */ function inspectDatabasePathIdentitySync(e) { const t = path.resolve(e); let n; try { n = statSync(t, { bigint: true }); } catch (e) { if (!hasErrnoCode(e, "ENOENT")) throw e; } if (n) { if (!n.isFile()) return; const e = resolveDatabasePathKey(t); return existingIdentity(n, statSync(e, { bigint: true }), e); } const r = resolveDatabasePathKey(t); return { key: `path:${r}`, canonicalPath: r }; } /** Capture identity before yielding; worker admission requires a regular file or absent path. */ function readDatabasePathIdentitySync(e) { const t = inspectDatabasePathIdentitySync(e); if (!t) throw new Error("SQLite worker database path must identify a regular file"); return t; } /** Inspect retained aliases only while binding a newly observed database path. */ function findChangedDatabasePaths(e, t) { return [...e].filter((e) => inspectDatabasePathIdentitySync(e)?.key !== t.key); } async function readDatabasePathIdentity(e) { const t = await inspectDatabasePathIdentity(e); if (!t) throw new Error("SQLite worker database path must identify a regular file"); return t; } async function inspectDatabasePathIdentity(e) { const t = await stat(e, { bigint: true }).catch((e) => { if (hasErrnoCode(e, "ENOENT")) return; throw e; }); if (t) { if (!t.isFile()) return; try { const n = await realpath(e); return existingIdentity(t, await stat(n, { bigint: true }), n); } catch (e) { if (hasErrnoCode(e, "ENOENT")) throw new Error("SQLite database pathname changed during admission", { cause: e }); throw e; } } const n = await resolveDatabasePathKeyAsync(e); return { key: `path:${n}`, canonicalPath: n }; } async function resolveDatabasePathKeyAsync(e) { const t = []; let n = path.resolve(e); while (true) try { return normalizeDatabasePath(path.join(await realpath(n), ...t)); } catch (e) { n = missingAncestorParent(n, t, e); } } function assertExistingDatabaseIdentity(e, t, n) { assertDatabaseFileIdentity(statSync(e, { bigint: true }), { key: t, birthtime: n }); } function isSoleDatabaseFileDescriptor(e, t, n) { if (process.platform !== "linux") return false; try { for (const r of readdirSync("/proc/self/fd")) { if (!/^\d+$/u.test(r) || Number(r) === e || n?.has(Number(r))) continue; try { const e = fstatSync(Number(r), { bigint: true }); if (e.dev === t.dev && e.ino === t.ino) return false; } catch (e) { if (!hasErrnoCode(e, "EBADF")) return false; } } return true; } catch { return false; } } //#endregion export { inspectDatabasePathIdentity as a, normalizeDatabasePath as c, readDatabasePathIdentity as d, readDatabasePathIdentitySync as f, findChangedDatabasePaths as i, readDatabaseFileIdentity as l, resolveDatabasePathKeyAsync as m, assertExistingDatabaseIdentity as n, inspectDatabasePathIdentitySync as o, resolveDatabasePathKey as p, databaseFileIdentityKey as r, isSoleDatabaseFileDescriptor as s, assertDatabaseFileIdentity as t, readDatabaseIdentityBirthtime as u };