UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

5 lines • 3.5 kB
import{f as readEngineAnchor,t as withSqliteSourceReadDatabase}from"./sqlite-source-handle-94Qt8Oy_.mjs";import{_ as sqlitePrimaryResultCode,c as isSqliteLockError}from"./sql-connection-D2qSU3Um.mjs";import{t as _usingCtx}from"./usingCtx-CUVyQ_FT.mjs";import{a as SqliteSnapshotCleanupError,l as runWithSqliteCleanup}from"./sqlite-lifecycle-errors-BKA8AtC6.mjs";import{t as openNodeSqliteDatabase}from"./node-sqlite-CX_zY7OF.mjs";import{c as prepareSqliteReadOnlyCopyInProcess,d as readSourceSidecars,o as SqliteSourceChangedError,s as isSqliteReadOnlyError,u as readSourceJournalMode}from"./sqlite-handle-lifecycle-CnxVxCkX.mjs";import{a as StateDatabaseOfflineMaintenanceError,r as StateDatabaseAdmissionPendingError}from"./gateway-state-owner-errors-Cb7kma8W.mjs";import fsSync from"node:fs"; //!src/infra/sqlite-readonly-inspection.ts function tryInspectSqliteReadOnlyInProcess(e,t,n={}){const r=fsSync.realpathSync.native(e);let i;try{i=readSourceJournalMode(r)}catch(e){if(e instanceof SqliteSourceChangedError)return;throw e}const a=readSourceSidecars(r);const o=n.preserveSourceArtifacts===true;const s=!o&&n.allowClosedWal&&!a.wal&&!a.shm&&!a.journal;if(i===`empty`||i===`wal`&&!(a.wal&&a.shm)&&!s||o&&(i!==`wal`||a.journal))return;const read=e=>{try{e.exec(`PRAGMA busy_timeout = 30000; PRAGMA trusted_schema = OFF; BEGIN;`);e.prepare(`PRAGMA schema_version;`).get()}catch(e){let t;try{t=readSourceJournalMode(r)}catch(e){if(e instanceof SqliteSourceChangedError)return;throw e}const n=readSourceSidecars(r);if(t===`wal`&&!(n.wal&&n.shm)||t===`rollback`&&n.journal&&isSqliteReadOnlyError(e)||o&&isSqliteReadOnlyError(e))return;throw e}const n=t(e);e.exec(`ROLLBACK;`);return{value:n}};try{return withSqliteSourceReadDatabase(r,`source`,read,{readOnlyShm:o})}catch(e){if(i===`wal`&&s&&n.snapshotAfterClosedWalFailure)return;if(o&&isSqliteCantOpenError(e))return;throw e}}const SQLITE_CANTOPEN_RESULT_CODE=14;function isSqliteCantOpenError(e){return sqlitePrimaryResultCode(e)===SQLITE_CANTOPEN_RESULT_CODE} //!src/infra/database-engine/physical-store-inspection.ts function readBackupEngineImage(e,t){try{var n=_usingCtx();return readEngineAnchorIdentity(n.u(openNodeSqliteDatabase(e,{readOnly:true})),t)}catch(e){n.e=e}finally{n.d()}}function readEngineAnchorIdentity(e,t){const n=readEngineAnchor(e);if(!n)return{engine:`sqlite`};const r=n[0];if(n.length!==1||r?.engine!==`postgres`||typeof r.store_id!==`string`||typeof r.schema!==`string`||!/^[a-zA-Z_][a-zA-Z0-9_-]*$/.test(r.schema))throw new Error(`Invalid PostgreSQL anchor: ${t}. Restore its matched backup.`);return{engine:`postgres`,storeId:r.store_id,namespace:r.schema}}async function readBackupEngineInProcess(e,t=false){return inspectPhysicalStoreInProcess(e,t,t=>readEngineAnchorIdentity(t,e))}async function inspectPhysicalStoreInProcess(e,t,n){if(!t)try{const t=tryInspectSqliteReadOnlyInProcess(e,n);if(t)return t.value}catch(e){if(!(e instanceof StateDatabaseAdmissionPendingError)&&!(e instanceof StateDatabaseOfflineMaintenanceError)&&!isSqliteLockError(e))throw e}const r=await prepareSqliteReadOnlyCopyInProcess(e);return runWithSqliteCleanup({release(){if(!r.cleanup())throw new SqliteSnapshotCleanupError(`SQLite engine inspection cleanup failed: ${e}`)}},`SQLite engine inspection`,()=>{try{var e=_usingCtx();return n(e.u(openNodeSqliteDatabase(r.location,{readOnly:true})))}catch(t){e.e=t}finally{e.d()}})}export{inspectPhysicalStoreInProcess,readBackupEngineImage,readBackupEngineInProcess,readEngineAnchorIdentity};