UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

1 lines • 9.89 kB
import{n as e}from"./sqlite-readonly-rolldown-runtime-DJBNY8Aw.mjs";import{Vt as t,tt as n}from"./sqlite-readonly-subsystem-C-VJT-xk.mjs";import{fn as r}from"./sqlite-readonly-runtime-process-manifest-CLhsN5hb.mjs";import{H as i,c as a}from"./sqlite-readonly-sqlite-readonly-location-CKoGOncu.mjs";import{c as o}from"./sqlite-readonly-sqlite-wal-CjNCU3qS.mjs";import{In as s,Jn as c,Wn as l,h as u,jn as d,r as f,t as p}from"./sqlite-readonly-node-sqlite-u3RAlsBE.mjs";import{D as m,M as h,O as g,j as _,k as v}from"./sqlite-readonly-sqlite-snapshot-capacity-error-Dx-DuFsZ.mjs";import{p as y}from"./sqlite-readonly-location.worker.js";import b from"node:path";import x from"node:fs";function readSqliteIntegrityFileIdentity(e,t){let n=x.statSync(e,{bigint:!0});if(!n.isFile()||t&&!r(t,n))throw Error(`SQLite source changed during integrity admission: ${e}`);return{dev:n.dev,ino:n.ino,size:n.size}}function canRetainDigest(e,t){let n=t-2000000000n;return typeof e.dev==`bigint`&&typeof e.ino==`bigint`&&typeof e.size==`bigint`&&e.dev!==0n&&e.ino!==0n&&typeof e.birthtimeNs==`bigint`&&typeof e.mtimeNs==`bigint`&&typeof e.ctimeNs==`bigint`&&e.mtimeNs>0n&&e.ctimeNs>0n&&e.mtimeNs<n&&e.ctimeNs<n}function timestampNs(e,t){return e??(typeof t==`bigint`?t*1000000n:BigInt(Math.trunc(t*1e6)))}function fingerprintFile(e,t){let n=BigInt(Date.now())*1000000n,r=x.openSync(e,`r`);try{let i=x.fstatSync(r,{bigint:!0});if(!i.isFile())throw Error(`SQLite generation target must be a regular file`);let a=t.get(e);if(a&&canRetainDigest(i,n)&&h(a,i)){let t=x.fstatSync(r,{bigint:!0}),n=x.statSync(e,{bigint:!0});if(n.isFile()&&h(i,t)&&h(t,n))return a}let{sha256:o}=g(x.realpathSync.native(e),i),s=x.fstatSync(r,{bigint:!0}),c=x.statSync(e,{bigint:!0});if(!v(i,s)||!v(s,c))throw Error(`SQLite generation target changed while hashing: ${e}`);let l={birthtimeNs:timestampNs(s.birthtimeNs,s.birthtimeMs),ctimeNs:timestampNs(s.ctimeNs,s.ctimeMs),dev:s.dev,ino:s.ino,mtimeNs:timestampNs(s.mtimeNs,s.mtimeMs),sha256:o,size:s.size};return canRetainDigest(i,n)&&h(i,s)&&h(s,c)?t.set(e,l):t.delete(e),l}finally{x.closeSync(r)}}function readOptionalFile(e,t){try{return fingerprintFile(e,t)}catch(n){if(n.code===`ENOENT`){t.delete(e);return}throw n}}function createSqliteFileGenerationReader(){let e=/* @__PURE__ */ new Map;return t=>{let n=fingerprintFile(t,e),r=readOptionalFile(`${t}-journal`,e),i=readOptionalFile(`${t}-wal`,e);return{database:n,...r?{journal:r}:{},...i?{wal:i}:{}}}}function readStableSqliteFileGeneration(e,t=createSqliteFileGenerationReader()){let n=t(e),r=t(e);if(!sameSqliteFileGeneration(n,r))throw Error(`SQLite file generation changed while reading: ${e}`);return r}function sameFileFingerprint(e,t){return e.dev===t.dev&&e.ino===t.ino&&v(e,t)&&e.sha256===t.sha256}function sameOptionalFileFingerprint(e,t){return e===void 0?t===void 0:t!==void 0&&sameFileFingerprint(e,t)}function sameSqliteFileGeneration(e,t){return sameFileFingerprint(e.database,t.database)&&sameOptionalFileFingerprint(e.journal,t.journal)&&sameOptionalFileFingerprint(e.wal?.size===0n?void 0:e.wal,t.wal?.size===0n?void 0:t.wal)}function serializeFileFingerprint(e){return{birthtimeNs:e.birthtimeNs.toString(),ctimeNs:e.ctimeNs.toString(),dev:e.dev.toString(),ino:e.ino.toString(),mtimeNs:e.mtimeNs.toString(),sha256:e.sha256,size:e.size.toString()}}function serializeSqliteFileGeneration(e){return JSON.stringify({database:serializeFileFingerprint(e.database),...e.journal?{journal:serializeFileFingerprint(e.journal)}:{},...e.wal?{wal:serializeFileFingerprint(e.wal)}:{}})}function parseFileFingerprint(e){if(!t(e))throw Error(`SQLite file fingerprint must be an object`);for(let t of[`birthtimeNs`,`ctimeNs`,`dev`,`ino`,`mtimeNs`,`size`])if(typeof e[t]!=`string`||!/^-?\d+$/u.test(e[t]))throw Error(`SQLite file fingerprint ${t} is invalid`);if(typeof e.sha256!=`string`||!/^[a-f0-9]{64}$/u.test(e.sha256))throw Error(`SQLite file fingerprint sha256 is invalid`);return{birthtimeNs:BigInt(e.birthtimeNs),ctimeNs:BigInt(e.ctimeNs),dev:BigInt(e.dev),ino:BigInt(e.ino),mtimeNs:BigInt(e.mtimeNs),sha256:e.sha256,size:BigInt(e.size)}}function parseSqliteFileGeneration(e){let n=JSON.parse(e);if(!t(n))throw Error(`SQLite file generation must be an object`);return{database:parseFileFingerprint(n.database),...n.journal===void 0?{}:{journal:parseFileFingerprint(n.journal)},...n.wal===void 0?{}:{wal:parseFileFingerprint(n.wal)}}}var S=/* @__PURE__ */ e({readMatchingUpdateDatabaseGeneration:()=>readMatchingUpdateDatabaseGeneration,readUpdateDatabaseGeneration:()=>readUpdateDatabaseGeneration,readUpdateDatabaseGenerationPair:()=>readUpdateDatabaseGenerationPair,readUpdateDatabaseGenerations:()=>readUpdateDatabaseGenerations});function readCommittedContent(e,t,n,r){let i=p(t.location);c({release:()=>i.close()},`Database generation checkpoint`,()=>o(i,t.location)),n?.(t.location,e);let a=r?.(),s=a?y(t.location,t.cleanupRoot??b.dirname(t.location)):void 0;if(a&&!s)throw Error(`Database generation private image cannot be retained`);let l=t.journalFreeContent;if(l)return m(t.location,l.identity,l),{sha256:l.sha256,family:s};let u=x.openSync(t.location,`r`);return{sha256:c({release:()=>x.closeSync(u)},`Database generation digest`,()=>_(u).sha256),family:s}}function inspectDrainedSource(e,t){let n=f(e);if(t){t(n,e);return}let r=p(n,{readOnly:!0});c({release:()=>r.close()},`Database generation admission`,()=>u(r))}function readWalIndexHeader(e){let t=`${e}-shm`,r;try{r=x.openSync(t,`r`)}catch(e){return n(e)??null}try{let n=x.fstatSync(r,{bigint:!0}),i=Buffer.alloc(96),a=x.readSync(r,i,0,48,0),o=x.readSync(r,i,48,48,48),s=x.fstatSync(r,{bigint:!0}),c=x.lstatSync(t,{bigint:!0});if(!n.isFile()||!c.isFile()||[s,c].some(e=>n.dev!==e.dev||n.ino!==e.ino)||a!==48||o!==48||!i.subarray(0,48).equals(i.subarray(48,96)))throw Error(`SQLite WAL commit header is unavailable or changing: ${e}`);return i}finally{x.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=>x.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,n){let r=readGenerationFamily(e);if([n.database,n.journal,n.wal?.size===0n?void 0:n.wal].some((e,n)=>[t[n],r[n]].some(t=>e===void 0?t!==void 0:!t||!v(t,e))))throw Error(`SQLite file generation changed while reading: ${e}`)}function serializePhysicalGeneration(e,t){return i(JSON.stringify([serializeSqliteFileGeneration(e.wal?.size===0n?{...e,wal:void 0}:e),t?.toString(`hex`)??null]))}function readUpdateDatabaseGeneration(e,t,n){let r=readGenerationFamily(e),o=r[0];if(!o)return{generation:null,physical:null};let u=readWalIndexHeader(e),f=createSqliteFileGenerationReader(),p=f(e),observe=(t,n)=>{let a=readStableSqliteFileGeneration(e,n?f:void 0),c=readWalIndexHeader(e);if(verifyGenerationFamily(e,r,a),!sameSqliteFileGeneration(p,a)||!sameWalCommitPublication(u,c,a)||p.wal&&p.wal.size>0n&&c?.[12]!==1)throw Error(`SQLite WAL commit publication could not be verified: ${e}`);let l=[p.database.dev.toString(),p.database.ino.toString(),s(o),t.sha256],d=c?.subarray(8,12).toString(`hex`)??null,m=c?.subarray(24,32).toString(`hex`)??null,fingerprint=(e,t)=>i(JSON.stringify([...l,e,t])),h=!p.journal&&(p.wal?.size??0n)===0n&&(c===null||d===`00000000`&&m===`0000000000000000`),g;if(t.family&&n){let r=y(n.location,n.cleanupRoot??b.dirname(n.location));if(r?.main!==t.family.main||r?.wal!==t.family.wal)throw Error(`Database generation private image changed before retention`);g={sourcePath:e,prepared:n,family:t.family}}return{retained:g!==void 0,observation:{generation:fingerprint(d,m),physical:serializePhysicalGeneration(a,c),...c!==null&&(p.wal?.size??0n)!==0n&&d!==`00000000`?{rebuiltIndexAlternate:fingerprint(`00000000`,m)}:{},...h?{emptyReaderAlternate:c===null?fingerprint(`00000000`,`0000000000000000`):fingerprint(null,null)}:{},...g?{preparedSource:g}:{}}}},m=!p.journal&&(p.wal?.size??0n)===0n;if(m&&!n)try{return inspectDrainedSource(e,t),observe({sha256:p.database.sha256}).observation}catch{}let h=a(e,n?.stagingRoot,{key:`file:${d(p.database)}`},m?{sha256:p.database.sha256,sizeBytes:Number(p.database.size)}:void 0),g=!1;return c({release:()=>{if(!g){if(!n)h.cleanup();else try{if(!h.cleanup())throw Error(`Database generation snapshot cleanup did not settle`)}catch(e){throw new l(`Database generation snapshot cleanup failed`,{cause:e})}}}},`Database generation snapshot`,()=>{let r=readCommittedContent(e,h,t,n?.accept),i=observe(r,h);return g=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}}function readMatchingUpdateDatabaseGeneration(e,t){let{generation:n,emptyReaderAlternate:r}=readUpdateDatabaseGeneration(e);return n!==null&&t.includes(n)||r!==void 0&&t.includes(r)?n??void 0:void 0}export{sameSqliteFileGeneration as a,readSqliteIntegrityFileIdentity as i,S as n,parseSqliteFileGeneration as r,readMatchingUpdateDatabaseGeneration as t};