UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

5 lines • 3.49 kB
import{t as _usingCtx}from"./usingCtx-CUVyQ_FT.js";import{Nt as sqlitePrimaryResultCode,dn as runWithSqliteCleanup,sn as SqliteSnapshotCleanupError,wt as isSqliteLockError}from"./sqlite-worker-request-class-l9sdQxsj.js";import{H as openNodeSqliteDatabase}from"./openclaw-state-db-async-lifecycle-DFpGMybR.js";import{f as readEngineAnchor,t as withSqliteSourceReadDatabase}from"./sqlite-source-handle-gJa7vBkU.js";import{D as StateDatabaseOfflineMaintenanceError,T as StateDatabaseAdmissionPendingError}from"./sqlite-worker-operation-settlement-DUfe98Na.js";import{at as isSqliteReadOnlyError,ct as readSourceJournalMode,it as SqliteSourceChangedError,lt as readSourceSidecars,ot as prepareSqliteReadOnlyCopyInProcess}from"./sqlite-schema-issues-Dicad9Gx.js";import fs from"node:fs"; //!src/infra/sqlite-readonly-inspection.ts function tryInspectSqliteReadOnlyInProcess(e,t,n={}){const r=fs.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};