UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

72 lines (71 loc) • 2.8 kB
import { t as SqliteWorkerError } from "./sqlite-worker-error-DEIJgRIz.mjs"; import "./context-DE59wyBs.mjs"; import { r as getPostgresPool } from "./pool-ClFRe4Wg.mjs"; import "./sqlite-worker-contract--dxXkOty.mjs"; import { c as observeDatabaseRequest } from "./adapters-D8O-uubH.mjs"; import { a as isSqliteWorkerStoreAvailable, c as openSqliteWorkerStore, d as runSqliteWorkerStoreOperation, f as runSqliteWorkerStoreWrite } from "./sqlite-worker-store-D51tD0Vp.mjs"; import "./config-1gpCgiFx.mjs"; import { deserialize, serialize } from "node:v8"; //#region src/infra/database-engine/worker-store.ts var EngineWorkerStore = class { constructor(e, t, n) { this.options = e; this.input = t; this.store = n; this.closed = false; } async current() { if (this.closed) throw new SqliteWorkerError("Database engine store is closed", "closed"); if (!isSqliteWorkerStoreAvailable(this.store)) { this.reopening ??= (async () => { await this.store.close(); this.store = await observeDatabaseRequest(this.input ? "postgres" : "sqlite", () => openSqliteWorkerStore(this.options)); })().finally(() => { this.reopening = void 0; }); await this.reopening; if (this.closed) throw new SqliteWorkerError("Database engine store is closed", "closed"); } return this.store; } async execute(e, t) { let n = this.store; let r = e; if (this.closed || !isSqliteWorkerStoreAvailable(n)) { const t = serialize(e); n = await this.current(); r = deserialize(t); } return observeDatabaseRequest(this.input ? "postgres" : "sqlite", () => n.execute(r, t)); } close() { this.closed = true; this.closing ??= (async () => { await this.reopening?.catch(() => void 0); await observeDatabaseRequest(this.input ? "postgres" : "sqlite", () => this.store.close()); })().catch((e) => { this.closing = void 0; throw e; }); return this.closing; } }; async function openEngineWorkerStore(e, t) { if (t) getPostgresPool(); const n = { ...e, moduleUrl: new URL(e.moduleUrl), input: deserialize(serialize(e.input)) }; const r = await observeDatabaseRequest(t ? "postgres" : "sqlite", () => openSqliteWorkerStore(n)); return new EngineWorkerStore(n, t, r); } /** The selected adapter owns the host admission envelope, not a store facade. */ async function runEngineStoreOperation(e, t, n, r, i, a) { const o = e instanceof EngineWorkerStore ? await e.current() : e; const observed = (e) => r({ execute: (n, r) => observeDatabaseRequest(t ? "postgres" : "sqlite", () => e.execute(n, r)) }); if (t && n) return runSqliteWorkerStoreWrite(o, observed, i ?? (() => {}), [a]); return i ? runSqliteWorkerStoreOperation(o, observed, void 0, i) : observed(o); } //#endregion export { runEngineStoreOperation as n, openEngineWorkerStore as t };