openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
1 lines • 5.54 kB
JavaScript
import{rt as e}from"./sqlite-readonly-subsystem-Bfq7Sewc.mjs";import{E as t,O as n}from"./sqlite-readonly-sqlite-snapshot-capacity-error-CpnKAwJy.mjs";import{c as r}from"./sqlite-readonly-sqlite-readonly-location-CrsaHxKY.mjs";import{n as i}from"./sqlite-readonly-file-descriptor-XY1-VkqF.mjs";import{Tn as a,_n as o,fn as s,kn as c}from"./sqlite-readonly-sqlite-database-admission-Bj6bmeyX.mjs";import{h as l,r as u,t as d}from"./sqlite-readonly-node-sqlite-C-jA8uc4.mjs";import{ot as f,p}from"./sqlite-readonly-location.worker.js";import{a as m,i as h,o as g,t as _}from"./sqlite-readonly-sqlite-file-generation-BjHWanIP.mjs";import{n as v}from"./sqlite-readonly-sqlite-wal-checkpoint-Cr5sXmOh.mjs";import y from"node:path";import b from"node:fs";function readCommittedContent(e,n,r,a){let o=d(n.location);c({release:()=>o.close()},`Database generation checkpoint`,()=>v(o,n.location)),r?.(n.location,e);let s=a?.(),l=s?p(n.location,n.cleanupRoot??y.dirname(n.location)):void 0;if(s&&!l)throw Error(`Database generation private image cannot be retained`);let u=n.journalFreeContent;if(u)return t(n.location,u.identity,u),{sha256:u.sha256,family:l};let f=b.openSync(n.location,`r`);return{sha256:c({release:()=>b.closeSync(f)},`Database generation digest`,()=>i(f).sha256),family:l}}function inspectDrainedSource(e,t){let n=u(e);if(t){t(n,e);return}let r=d(n,{readOnly:!0});c({release:()=>r.close()},`Database generation admission`,()=>l(r))}function readWalIndexHeader(t){let n=`${t}-shm`,r;try{r=b.openSync(n,`r`)}catch(t){return e(t)??null}try{let e=b.fstatSync(r,{bigint:!0}),i=Buffer.alloc(96),a=b.readSync(r,i,0,48,0),o=b.readSync(r,i,48,48,48),s=b.fstatSync(r,{bigint:!0}),c=b.lstatSync(n,{bigint:!0});if(!e.isFile()||!c.isFile()||[s,c].some(t=>e.dev!==t.dev||e.ino!==t.ino)||a!==48||o!==48||!i.subarray(0,48).equals(i.subarray(48,96)))throw Error(`SQLite WAL commit header is unavailable or changing: ${t}`);return i}finally{b.closeSync(r)}}function isRebuiltWalIndex(e,t){return t.readUInt32LE(8)===0&&t.subarray(0,8).equals(e.subarray(0,8))&&t.subarray(12,40).equals(e.subarray(12,40))}function sameWalCommitPublication(e,t,n){return(e===null?t===null:t!==null&&(t.equals(e)||isRebuiltWalIndex(e,t)))?!0:n.journal||(n.wal?.size??0n)!==0n?!1:t===null||e===null&&t.readUInt32LE(8)===0&&t.readBigUInt64LE(24)===0n}function readGenerationFamily(e){let t=[``,`-journal`,`-wal`].map(t=>b.lstatSync(`${e}${t}`,{bigint:!0,throwIfNoEntry:!1}));if(!t[0]&&t.slice(1).some(Boolean))throw Error(`Database is absent but retained journal data exists: ${e}`);if(t.some(e=>e&&!e.isFile()))throw Error(`Database generation requires a regular file: ${e}`);return t.map((e,t)=>t===2&&e?.size===0n?void 0:e)}function verifyGenerationFamily(e,t,r){let i=readGenerationFamily(e);if([r.database,r.journal,r.wal?.size===0n?void 0:r.wal].some((e,r)=>[t[r],i[r]].some(t=>e===void 0?t!==void 0:!t||!n(t,e))))throw Error(`SQLite file generation changed while reading: ${e}`)}function serializePhysicalGeneration(e,t){return f(JSON.stringify([g(e.wal?.size===0n?{...e,wal:void 0}:e),t?.toString(`hex`)??null]))}function readUpdateDatabaseGeneration(e,t,n){let i=readGenerationFamily(e),l=i[0];if(!l)return{generation:null,physical:null};let u=readWalIndexHeader(e),d=_(),g=d(e),observe=(t,n)=>{let r=h(e,n?d:void 0),a=readWalIndexHeader(e);if(verifyGenerationFamily(e,i,r),!m(g,r)||!sameWalCommitPublication(u,a,r)||g.wal&&g.wal.size>0n&&a?.[12]!==1)throw Error(`SQLite WAL commit publication could not be verified: ${e}`);let s=[g.database.dev.toString(),g.database.ino.toString(),o(l),t.sha256],c=a?.subarray(8,12).toString(`hex`)??null,_=a?.subarray(24,32).toString(`hex`)??null,fingerprint=(e,t)=>f(JSON.stringify([...s,e,t])),v=!g.journal&&(g.wal?.size??0n)===0n&&(a===null||c===`00000000`&&_===`0000000000000000`),b;if(t.family&&n){let r=p(n.location,n.cleanupRoot??y.dirname(n.location));if(r?.main!==t.family.main||r?.wal!==t.family.wal)throw Error(`Database generation private image changed before retention`);b={sourcePath:e,prepared:n,family:t.family}}return{retained:b!==void 0,observation:{generation:fingerprint(c,_),physical:serializePhysicalGeneration(r,a),...a!==null&&!g.journal&&c!==`00000000`?{rebuiltIndexAlternate:fingerprint(`00000000`,(g.wal?.size??0n)===0n?`0000000000000000`:_)}:{},...v?{emptyReaderAlternate:a===null?fingerprint(`00000000`,`0000000000000000`):fingerprint(null,null)}:{},...b?{preparedSource:b}:{}}}},v=!g.journal&&(g.wal?.size??0n)===0n;if(v&&!n)try{return inspectDrainedSource(e,t),observe({sha256:g.database.sha256}).observation}catch{}let b=r(e,n?.stagingRoot,{key:`file:${s(g.database)}`},v?{sha256:g.database.sha256,sizeBytes:Number(g.database.size)}:void 0),x=!1;return c({release:()=>{if(!x){if(!n)b.cleanup();else try{if(!b.cleanup())throw Error(`Database generation snapshot cleanup did not settle`)}catch(e){throw new a(`Database generation snapshot cleanup failed`,{cause:e})}}}},`Database generation snapshot`,()=>{let r=readCommittedContent(e,b,t,n?.accept),i=observe(r,b);return x=i.retained,i.observation})}function readUpdateDatabaseGenerations(e,t){return readUpdateDatabaseGenerationPair(e,t).committed}function readUpdateDatabaseGenerationPair(e,t){let n={},r={},i={},a={};for(let o of e){let e=readUpdateDatabaseGeneration(o,t);n[o]=e.generation,r[o]=e.physical,e.emptyReaderAlternate&&(i[o]=e.emptyReaderAlternate),e.rebuiltIndexAlternate&&(a[o]=e.rebuiltIndexAlternate)}return{committed:n,physical:r,emptyReaderAlternates:i,rebuiltIndexAlternates:a}}export{readUpdateDatabaseGenerations};