openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
1 lines • 2.57 kB
JavaScript
import{n as e}from"./openclaw-state-read-file-hash-CQQXXBNG.mjs";import{t}from"./openclaw-state-read-subsystem-DSi0tJn8.mjs";import n,{closeSync as r,openSync as i}from"node:fs";import a from"node:path";import{hash as o}from"node:crypto";function sha256Base64Url(e){return o(`sha256`,e,`base64url`)}function sha256FileSync(t){let n=i(t,`r`);try{return e(n).digest}finally{r(n)}}const s=[``,`-wal`,`-shm`,`-journal`],c=s.slice(1);Buffer.from([0,5,22,7]);const l=t(`state/sqlite`);var SqliteOrphanedSidecarsError=class extends Error{constructor(e,t,n){super(`SQLite database is missing at ${e}, and orphaned sidecars could not be copied: ${t.join(`, `)}. Refusing to open because SQLite could delete orphan WAL or journal state. Preserve the sidecar bytes, restore the main database, and pair it with the matching sidecar before retrying.`,{cause:n}),this.name=`SqliteOrphanedSidecarsError`}};function resolveSqliteDatabaseFilePaths(e){return s.map(t=>`${e}${t}`)}function findMatchingOrphanedSidecarCopy(e,t){let r=a.dirname(e),i=`${a.basename(e)}.orphaned-`,o=n.readdirSync(r,{withFileTypes:!0}).filter(e=>e.isFile()&&e.name.startsWith(i)).map(e=>a.join(r,e.name)).filter(e=>n.statSync(e).size===t);if(o.length===0)return;let s=sha256FileSync(e);for(let e of o)if(sha256FileSync(e)===s)return e}function copyOrphanedSidecar(e,t){let r=`${e}.orphaned-${t}`;for(let t=0;;t+=1){let i=t===0?r:`${r}-${t}`;try{return n.copyFileSync(e,i,n.constants.COPYFILE_EXCL),i}catch(e){if(e.code!==`EEXIST`)throw e}}}function quarantineOrphanedSqliteSidecars(e){if(n.existsSync(e))return;let t=[{path:`${e}-wal`,minimumBytes:32},{path:`${e}-journal`,minimumBytes:0}].flatMap(e=>{let t=n.statSync(e.path,{throwIfNoEntry:!1});return t?.isFile()===!0&&t.size>e.minimumBytes?[{path:e.path,size:t.size}]:[]});if(t.length===0)return;let r=Date.now(),i=[];try{for(let e of t){if(findMatchingOrphanedSidecarCopy(e.path,e.size))continue;let t=copyOrphanedSidecar(e.path,r);i.push({quarantinePath:t,sourcePath:e.path})}}catch(n){throw new SqliteOrphanedSidecarsError(e,t.map(e=>e.path),n)}if(i.length===0)return;let a=i.map(({sourcePath:e,quarantinePath:t})=>`${e} -> ${t}`);l.warn(`SQLite database is missing at ${e}; copied orphaned sidecars: ${a.join(`, `)}. Committed frames could not be applied because the main database is missing. The bytes are preserved. Recovery requires restoring the main database and pairing it with the quarantined file.`,{databasePath:e,copiedSidecars:i})}export{sha256Base64Url as i,quarantineOrphanedSqliteSidecars as n,resolveSqliteDatabaseFilePaths as r,c as t};