UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

3 lines • 5.09 kB
import{o as resolveGlobalSingleton}from"./global-singleton-DolL9xGt.mjs";import{r as registerListener}from"./listeners-awzYn4LE.mjs";import{n as isPostgresSqlConnection}from"./sql-connection-nx8lLUJM.mjs"; //!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;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)}function registerNodeSqliteDisposeCallback(e,t){const n=e;installStatementInvalidation(n);return registerListener(n[disposeCallbacksSymbol]??=/* @__PURE__ */ new Set,t)}function registerNodeSqliteDisposeGuard(e,t){const n=e;installStatementInvalidation(n);return registerListener(n[disposeGuardsSymbol]??=/* @__PURE__ */ new Set,t)}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)}function registerNodeSqliteKyselyQueryErrorHandler(e,t){queryErrorHandlerByDatabase.set(e,t)}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})}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)})}export{enableNodeSqliteKyselyStatementCache as a,kyselyByDatabase as c,registerNodeSqliteDisposeGuard as d,registerNodeSqliteKyselyQueryErrorHandler as f,disposeSqliteStatement as i,queryErrorHandlerByDatabase as l,clearNodeSqliteKyselyCacheForDatabase as n,executeWithCachedStatement as o,disposeNodeSqliteDependents as r,installStatementInvalidation as s,assertNodeSqliteDisposalAllowed as t,registerNodeSqliteDisposeCallback as u};