UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

189 lines (188 loc) • 6.42 kB
import { W as resolveGlobalSingleton } from "./path-guards-quCEQx1i.mjs"; import { a as registerListener, n as isPostgresSqlConnection } from "./sql-connection-VmV9pnJI.mjs"; //#region src/infra/kysely-sync-cache-state.ts const { kyselyByDatabase, queryErrorHandlerByDatabase } = resolveGlobalSingleton(Symbol.for("openclaw.sqliteKyselyCacheState"), () => ({ kyselyByDatabase: /* @__PURE__ */ new WeakMap(), queryErrorHandlerByDatabase: /* @__PURE__ */ new WeakMap() })); const statementCacheSymbol = Symbol.for("openclaw.kyselySyncStatementCache"); const statementInvalidationSymbol = Symbol.for("openclaw.kyselySyncStatementInvalidation"); const statementCacheEnabledSymbol = Symbol.for("openclaw.kyselySyncStatementCacheEnabled"); const authorizerActiveSymbol = Symbol.for("openclaw.kyselySyncAuthorizerActive"); const disposeCallbacksSymbol = Symbol.for("openclaw.sqliteDisposeCallbacks"); const disposeGuardsSymbol = Symbol.for("openclaw.sqliteDisposeGuards"); const statementCacheCapacity = 64; const statementCacheEntryBytes = 65536; /** Node 26.8+ can release bindings without waiting for an unrelated V8 collection. */ function disposeSqliteStatement(e) { e[Symbol.dispose]?.(); } function clearStatementCache(e) { const t = e[statementCacheSymbol]; delete e[statementCacheSymbol]; if (!t) return; for (const e of t.statements.values()) if (!t.active.has(e)) disposeSqliteStatement(e); } /** Retire dependent native resources before this connection closes or is replaced. */ function registerNodeSqliteDisposeCallback(e, t) { const n = e; installStatementInvalidation(n); return registerListener(n[disposeCallbacksSymbol] ??= /* @__PURE__ */ new Set(), t); } /** Pending native work must settle before any dependent identity or lease is released. */ function registerNodeSqliteDisposeGuard(e, t) { const n = e; installStatementInvalidation(n); return registerListener(n[disposeGuardsSymbol] ??= /* @__PURE__ */ new Set(), t); } /** Lifecycle owners check before preparing cleanup that would revoke retained admission. */ function assertNodeSqliteDisposalAllowed(e) { for (const t of e[disposeGuardsSymbol] ?? []) t(); } function disposeNodeSqliteDependents(e, t = "close") { assertNodeSqliteDisposalAllowed(e); for (const n of e[disposeCallbacksSymbol] ?? []) n(t); } /** Register the lifecycle owner's handler for synchronous Kysely query failures. */ function registerNodeSqliteKyselyQueryErrorHandler(e, t) { queryErrorHandlerByDatabase.set(e, t); } /** Drop cached Kysely state for a DatabaseSync. */ function clearNodeSqliteKyselyCacheForDatabase(e) { clearStatementCache(e); kyselyByDatabase.delete(e); queryErrorHandlerByDatabase.delete(e); } function installStatementInvalidation(e) { if (e[statementInvalidationSymbol]) return; if (typeof e.setAuthorizer === "function") { const t = e.setAuthorizer.bind(e); Object.defineProperty(e, "setAuthorizer", { configurable: true, writable: true, value(e) { t(e); this[authorizerActiveSymbol] = e !== null; clearStatementCache(this); } }); } if (typeof e.deserialize === "function") { const t = e.deserialize.bind(e); Object.defineProperty(e, "deserialize", { configurable: true, writable: true, value(...e) { disposeNodeSqliteDependents(this, "replace"); try { t(...e); } finally { clearStatementCache(this); } } }); } for (const t of ["close", Symbol.dispose]) if (typeof e[t] === "function") { const n = e[t].bind(e); Object.defineProperty(e, t, { configurable: true, writable: true, value() { disposeNodeSqliteDependents(this); clearNodeSqliteKyselyCacheForDatabase(this); return n(); } }); } Object.defineProperty(e, statementInvalidationSymbol, { configurable: true, value: true }); } /** * Enable bounded statement caching for a lifecycle-owned database that has not * installed an authorizer before this call. */ function enableNodeSqliteKyselyStatementCache(e) { const t = e; installStatementInvalidation(t); t[statementCacheEnabledSymbol] = true; } function queryFitsStatementCache(e, t) { let n = Buffer.byteLength(e); if (n > statementCacheEntryBytes) return false; for (const e of t) { if (typeof e === "string") { if (e.length > statementCacheEntryBytes - n) return false; n += Buffer.byteLength(e); } else if (ArrayBuffer.isView(e)) n += e.byteLength; if (n > statementCacheEntryBytes) return false; } return true; } function executeWithCachedStatement(e, t, n, r, i) { if (isPostgresSqlConnection(e)) return r(e.prepare(t)); const a = e; const executeLeased = (e, t) => { if (i) { try { i(t); } catch (e) { t(); throw e; } return r(e); } try { return r(e); } finally { t(); } }; if (!a[statementCacheEnabledSymbol] || a[authorizerActiveSymbol] || !queryFitsStatementCache(t, n)) { const n = e.prepare(t); return executeLeased(n, () => disposeSqliteStatement(n)); } let o = a[statementCacheSymbol]; if (!o) { o = { statements: /* @__PURE__ */ new Map(), candidates: /* @__PURE__ */ new Set(), active: /* @__PURE__ */ new WeakSet() }; Object.defineProperty(a, statementCacheSymbol, { configurable: true, value: o }); } const s = o.statements.get(t); let c; if (s && !o.active.has(s)) { o.statements.delete(t); o.statements.set(t, s); c = s; } else { c = e.prepare(t); if (!s && o.candidates.delete(t)) { o.statements.set(t, c); if (o.statements.size > statementCacheCapacity) { const e = o.statements.entries().next().value; o.statements.delete(e[0]); if (!o.active.has(e[1])) disposeSqliteStatement(e[1]); } } else if (!s) { o.candidates.add(t); if (o.candidates.size > statementCacheCapacity) { const e = o.candidates.values().next().value; if (e !== void 0) o.candidates.delete(e); } } } o.active.add(c); return executeLeased(c, () => { o.active.delete(c); if (a[statementCacheSymbol] !== o || o.statements.get(t) !== c) disposeSqliteStatement(c); }); } //#endregion export { installStatementInvalidation as a, registerNodeSqliteDisposeCallback as c, executeWithCachedStatement as i, registerNodeSqliteDisposeGuard as l, disposeSqliteStatement as n, kyselyByDatabase as o, enableNodeSqliteKyselyStatementCache as r, queryErrorHandlerByDatabase as s, clearNodeSqliteKyselyCacheForDatabase as t, registerNodeSqliteKyselyQueryErrorHandler as u };