openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
3 lines • 4.88 kB
JavaScript
import{t as hasErrnoCode}from"./errno-DUBt6SQQ.mjs";import{u as normalizeWindowsPathPreservingCase}from"./state-dir-C22DK7p0.mjs";import{fstatSync,readdirSync,realpathSync,statSync}from"node:fs";import path from"node:path";import{realpath,stat}from"node:fs/promises";
//!src/infra/sqlite-worker-identity.ts
const useDatabaseBirthtime=process.platform!==`linux`&&process.platform!==`android`;function databaseFileIdentityKey(e){return`${e.dev}:${e.ino}`}function readDatabaseIdentityBirthtime(e){return useDatabaseBirthtime?e.birthtimeNs.toString():`0`}function normalizeDatabasePath(e){const t=process.platform===`win32`?normalizeWindowsPathPreservingCase(e):e;return process.platform===`win32`&&!/^(?:[a-z]:[\\/]|[\\/]{2})/iu.test(t)?e:t}function readDatabaseFileIdentity(e){if(!e||typeof e!==`object`||Array.isArray(e)||!(`key`in e)||typeof e.key!==`string`||!/^file:(?:0|[1-9]\d{0,19}):(?:0|[1-9]\d{0,19})$/.test(e.key)||`birthtime`in e&&e.birthtime!==void 0&&(typeof e.birthtime!==`string`||!/^-?(?:0|[1-9]\d{0,29})$/.test(e.birthtime)))throw new Error(`SQLite snapshot requires a captured physical file identity`);return{key:e.key,...`birthtime`in e&&typeof e.birthtime===`string`?{birthtime:e.birthtime}:{}}}function assertDatabaseFileIdentity(e,t){if(!e.isFile()||`file:${databaseFileIdentityKey(e)}`!==t.key||t.birthtime!==void 0&&readDatabaseIdentityBirthtime(e)!==t.birthtime)throw new Error(`SQLite database file identity changed before existing-only open`)}function existingIdentity(e,t,n){if(!e.isFile())throw new Error(`SQLite worker database path must identify a regular file`);if(e.dev!==t.dev||e.ino!==t.ino||readDatabaseIdentityBirthtime(e)!==readDatabaseIdentityBirthtime(t))throw new Error(`SQLite database pathname changed during admission`);return{key:`file:${databaseFileIdentityKey(e)}`,canonicalPath:normalizeDatabasePath(n),birthtime:readDatabaseIdentityBirthtime(e)}}function missingAncestorParent(e,t,n){if(!hasErrnoCode(n,`ENOENT`))throw n;t.unshift(path.basename(e));const r=path.dirname(e);if(r===e)throw n;return r}function resolveDatabasePathKey(e){let t=path.resolve(e);const n=[];while(true)try{return normalizeDatabasePath(path.join(realpathSync.native(t),...n))}catch(e){t=missingAncestorParent(t,n,e)}}function inspectDatabasePathIdentitySync(e){const t=path.resolve(e);let n;try{n=statSync(t,{bigint:true})}catch(e){if(!hasErrnoCode(e,`ENOENT`))throw e}if(n){if(!n.isFile())return;const e=resolveDatabasePathKey(t);return existingIdentity(n,statSync(e,{bigint:true}),e)}const r=resolveDatabasePathKey(t);return{key:`path:${r}`,canonicalPath:r}}function readDatabasePathIdentitySync(e){const t=inspectDatabasePathIdentitySync(e);if(!t)throw new Error(`SQLite worker database path must identify a regular file`);return t}function findChangedDatabasePaths(e,t){return[...e].filter(e=>inspectDatabasePathIdentitySync(e)?.key!==t.key)}async function readDatabasePathIdentity(e){const t=await inspectDatabasePathIdentity(e);if(!t)throw new Error(`SQLite worker database path must identify a regular file`);return t}async function inspectDatabasePathIdentity(e){const t=await stat(e,{bigint:true}).catch(e=>{if(hasErrnoCode(e,`ENOENT`))return;throw e});if(t){if(!t.isFile())return;try{const n=await realpath(e);return existingIdentity(t,await stat(n,{bigint:true}),n)}catch(e){if(hasErrnoCode(e,`ENOENT`))throw new Error(`SQLite database pathname changed during admission`,{cause:e});throw e}}const n=await resolveDatabasePathKeyAsync(e);return{key:`path:${n}`,canonicalPath:n}}async function resolveDatabasePathKeyAsync(e){const t=[];let n=path.resolve(e);while(true)try{return normalizeDatabasePath(path.join(await realpath(n),...t))}catch(e){n=missingAncestorParent(n,t,e)}}function assertDatabasePathIdentity(e,t){if(t.key.startsWith(`file:`))assertExistingDatabaseIdentity(e,t.key,t.birthtime);else if(readDatabasePathIdentitySync(e).key!==t.key)throw new Error(`SQLite database path identity changed: ${e}`)}function assertExistingDatabaseIdentity(e,t,n){assertDatabaseFileIdentity(statSync(e,{bigint:true}),{key:t,birthtime:n})}function isSoleDatabaseFileDescriptor(e,t){if(process.platform!==`linux`)return false;try{for(const n of readdirSync(`/proc/self/fd`)){if(!/^\d+$/u.test(n)||Number(n)===e)continue;try{const e=fstatSync(Number(n),{bigint:true});if(e.dev===t.dev&&e.ino===t.ino)return false}catch(e){if(!hasErrnoCode(e,`EBADF`))return false}}return true}catch{return false}}export{inspectDatabasePathIdentity as a,normalizeDatabasePath as c,readDatabasePathIdentity as d,readDatabasePathIdentitySync as f,findChangedDatabasePaths as i,readDatabaseFileIdentity as l,resolveDatabasePathKeyAsync as m,assertDatabasePathIdentity as n,inspectDatabasePathIdentitySync as o,resolveDatabasePathKey as p,assertExistingDatabaseIdentity as r,isSoleDatabaseFileDescriptor as s,assertDatabaseFileIdentity as t,readDatabaseIdentityBirthtime as u};