UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

130 lines (129 loc) • 4.46 kB
import { a as resolveGlobalSingleton } from "./global-singleton-D7yf05nR.mjs"; import { n as isPostgresSqlConnection, r as sqlPublicationAnchor } from "./sql-connection-Beiyg5Dt.mjs"; import { i as assertExistingDatabaseIdentity, m as readDatabasePathIdentitySync } from "./sqlite-worker-identity-BH8ZkJU9.mjs"; import { y as prepareSqliteDatabaseCleanClose } from "./sqlite-database-admission-DQIqro3d.mjs"; import { _ as withSqliteNativeOpen } from "./sqlite-error-diagnostics-6_hREUnq.mjs"; import { t as _usingCtx } from "./usingCtx-rhpPzaNq.mjs"; import { n as clearNodeSqliteKyselyCacheForDatabase, u as registerNodeSqliteDisposeCallback } from "./kysely-sync-cache-state-Bb1gOqQO.mjs"; import { a as setSqliteBusyTimeout } from "./sqlite-busy-timeout-BEDmnGR_.mjs"; import { n as openNodeSqliteDatabase, r as resolveExistingSqliteFileUri } from "./node-sqlite-CU4puofF.mjs"; import { r as isSqliteWalWorkerMaintained } from "./sqlite-wal-write-admission-BDhnPj3y.mjs"; import { r as assertStateDatabaseAccessAllowed } from "./gateway-state-owner-Bnv5owvG.mjs"; //#region src/state/openclaw-state-db-handle.ts const identities = resolveGlobalSingleton(Symbol.for("openclaw.stateNativeIdentities"), () => /* @__PURE__ */ new WeakMap()); /** Physical identity admitted by the native opener, never a later pathname observation. */ function readTrackedStateDatabaseIdentity(e) { return identities.get(sqlPublicationAnchor(e)); } function openTrackedStateDatabase(e, t) { const n = openTrackedStateDatabaseResult(e, t); if (n.status === "unavailable") throw n.error; return n.database; } /** Native open failure is an ordinary read failure; admitted handles retain their own cleanup. */ function openTrackedStateDatabaseResult(e, t) { assertStateDatabaseAccessAllowed(e); try { if (t?.expectedIdentity !== void 0) assertExistingDatabaseIdentity(e, t.expectedIdentity); const n = t?.existingOnly || t?.expectedIdentity !== void 0 ? resolveExistingSqliteFileUri(e) : e; const r = t?.readOnly ? { readOnly: true, timeout: t.timeout } : { enableForeignKeyConstraints: t?.enableForeignKeyConstraints }; const i = readDatabasePathIdentitySync(e); const a = withSqliteNativeOpen(() => openNodeSqliteDatabase(n, r)); try { assertStateDatabaseAccessAllowed(e); if (i.key.startsWith("file:")) assertExistingDatabaseIdentity(e, i.key, i.birthtime); const t = i.key.startsWith("file:") ? i : readDatabasePathIdentitySync(e); if (t.key.startsWith("file:")) { identities.set(a, t); const e = registerNodeSqliteDisposeCallback(a, () => { identities.delete(a); e(); }); } } catch (e) { a.close(); throw e; } return { status: "available", database: a }; } catch (e) { return { status: "unavailable", error: e }; } } function closeTrackedStateDatabase(e) { if (e.isOpen) e.close(); } /** Finish all native close stages even when a previous stage failed. */ function closeStateDatabaseHandle(e, t) { try { var n = _usingCtx(); const r = []; if (isPostgresSqlConnection(e.db)) { for (const t of [e.db, e.db.anchor]) { if (t === e.db.anchor && e.db.isOpen) break; try { if (t.isOpen) t.close(); } catch (e) { r.push(e); } } let t = e.db.isOpen || e.db.anchor.isOpen; if (!t) try { e.afterClose?.(); } catch (e) { r.push(e); t = true; } return { errors: r, cleanupPending: t }; } const i = n.u(prepareSqliteDatabaseCleanClose(e.db)); const a = isSqliteWalWorkerMaintained(e.db); const o = e.walMaintenance?.health?.state === "error"; let s = false; try { if (e.db.isOpen && !a) setSqliteBusyTimeout(e.db, 0); s = e.walMaintenance?.close(t) === true && t?.checkpointMode !== "PASSIVE"; } catch (e) { r.push(e); } try { clearNodeSqliteKyselyCacheForDatabase(e.db); } catch (e) { r.push(e); } try { closeTrackedStateDatabase(e.db); } catch (e) { r.push(e); } let c = false; if (!e.db.isOpen) try { e.afterClose?.(); } catch (e) { r.push(e); c = true; } if (r.length === 0 && !c && t?.checkpointMode !== "PASSIVE" && (s || a && !o)) i.publish(); return { errors: r, cleanupPending: c }; } catch (e) { n.e = e; } finally { n.d(); } } //#endregion export { readTrackedStateDatabaseIdentity as a, openTrackedStateDatabaseResult as i, closeTrackedStateDatabase as n, openTrackedStateDatabase as r, closeStateDatabaseHandle as t };