UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

1 lines • 4.78 kB
import{i as e,o as t}from"./transcript-path-guards-u0N7Z9_x.mjs";import n from"node:path";import{fstatSync as r,readdirSync as i,realpathSync as a,statSync as o}from"node:fs";import{realpath as s,stat as c}from"node:fs/promises";const l=process.platform!==`linux`&&process.platform!==`android`;function databaseFileIdentityKey(e){return`${e.dev}:${e.ino}`}function readDatabaseIdentityBirthtime(e){return l?e.birthtimeNs.toString():`0`}function normalizeDatabasePath(t){let n=process.platform===`win32`?e(t):t;return process.platform===`win32`&&!/^(?:[a-z]:[\\/]|[\\/]{2})/iu.test(n)?t:n}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 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 Error(`SQLite database file identity changed before existing-only open`)}function existingIdentity(e,t,n){if(!e.isFile())throw Error(`SQLite worker database path must identify a regular file`);if(e.dev!==t.dev||e.ino!==t.ino||readDatabaseIdentityBirthtime(e)!==readDatabaseIdentityBirthtime(t))throw Error(`SQLite database pathname changed during admission`);return{key:`file:${databaseFileIdentityKey(e)}`,canonicalPath:normalizeDatabasePath(n),birthtime:readDatabaseIdentityBirthtime(e)}}function missingAncestorParent(e,r,i){if(!t(i,`ENOENT`))throw i;r.unshift(n.basename(e));let a=n.dirname(e);if(a===e)throw i;return a}function resolveDatabasePathKey(e){let t=n.resolve(e),r=[];for(;;)try{return normalizeDatabasePath(n.join(a.native(t),...r))}catch(e){t=missingAncestorParent(t,r,e)}}function inspectDatabasePathIdentitySync(e){let r=n.resolve(e),i;try{i=o(r,{bigint:!0})}catch(e){if(!t(e,`ENOENT`))throw e}if(i){if(!i.isFile())return;let e=resolveDatabasePathKey(r);return existingIdentity(i,o(e,{bigint:!0}),e)}let a=resolveDatabasePathKey(r);return{key:`path:${a}`,canonicalPath:a}}function readDatabasePathIdentitySync(e){let t=inspectDatabasePathIdentitySync(e);if(!t)throw 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){let t=await inspectDatabasePathIdentity(e);if(!t)throw Error(`SQLite worker database path must identify a regular file`);return t}async function inspectDatabasePathIdentity(e){let n=await c(e,{bigint:!0}).catch(e=>{if(!t(e,`ENOENT`))throw e});if(n){if(!n.isFile())return;try{let t=await s(e);return existingIdentity(n,await c(t,{bigint:!0}),t)}catch(e){throw t(e,`ENOENT`)?Error(`SQLite database pathname changed during admission`,{cause:e}):e}}let r=await resolveDatabasePathKeyAsync(e);return{key:`path:${r}`,canonicalPath:r}}async function resolveDatabasePathKeyAsync(e){let t=[],r=n.resolve(e);for(;;)try{return normalizeDatabasePath(n.join(await s(r),...t))}catch(e){r=missingAncestorParent(r,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 Error(`SQLite database path identity changed: ${e}`)}async function assertDatabasePathIdentityAsync(e,t){if(t.key.startsWith(`file:`))assertDatabaseFileIdentity(await c(e,{bigint:!0}),t);else if((await readDatabasePathIdentity(e)).key!==t.key)throw Error(`SQLite database path identity changed: ${e}`)}function assertExistingDatabaseIdentity(e,t,n){assertDatabaseFileIdentity(o(e,{bigint:!0}),{key:t,birthtime:n})}function isSoleDatabaseFileDescriptor(e,n,a){if(process.platform!==`linux`)return!1;try{for(let o of i(`/proc/self/fd`))if(/^\d+$/u.test(o)&&Number(o)!==e&&!a?.has(Number(o)))try{let e=r(Number(o),{bigint:!0});if(e.dev===n.dev&&e.ino===n.ino)return!1}catch(e){if(!t(e,`EBADF`))return!1}return!0}catch{return!1}}export{databaseFileIdentityKey as a,inspectDatabasePathIdentitySync as c,readDatabaseFileIdentity as d,readDatabaseIdentityBirthtime as f,resolveDatabasePathKeyAsync as g,resolveDatabasePathKey as h,assertExistingDatabaseIdentity as i,isSoleDatabaseFileDescriptor as l,readDatabasePathIdentitySync as m,assertDatabasePathIdentity as n,findChangedDatabasePaths as o,readDatabasePathIdentity as p,assertDatabasePathIdentityAsync as r,inspectDatabasePathIdentity as s,assertDatabaseFileIdentity as t,normalizeDatabasePath as u};