openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
7 lines • 7.45 kB
JavaScript
import{a as resolveRuntimeWorkerUrl}from"../runtime-worker-url-Dqf-I1cs.mjs";import{n as encodeNativeWorkerFailure}from"../worker-native-error-B1RuZbYc.mjs";import{t as readBackupEngine}from"../physical-store-Dt80uUi6.mjs";import{n as prepareManagedHandoffLeaseStore,r as resolveManagedUpdateLeaseDatabasePath}from"../update-managed-service-handoff-lease-DI0P-NxP.mjs";import{r as assertManagedUpdateLeaseDatabaseIdentity,u as prepareManagedHandoffLeaseDatabaseIdentity}from"../update-managed-service-handoff-database-H0kqm6Ol.mjs";import{n as serveWorkerTasks}from"../worker-task-server-BeWxX5OH.mjs";import{r as resolveLegacyGatewayManagedUpdateLeaseDatabasePath,t as assertLegacyGatewayCustodySettled}from"../update-managed-service-handoff-legacy-store-BBICUW5r.mjs";import{n as managedHandoffPostgresBridgeEntrypoint,r as managedHandoffRuntimeEntrypoint,t as MANAGED_HANDOFF_RUNTIME_ENTRY}from"../update-managed-service-handoff-runtime-assets-wjaWsW3J.mjs";import{createRequire}from"node:module";import fs,{existsSync}from"node:fs";import path from"node:path";
//!src/infra/update-managed-service-handoff-native.ts
function packageMetadata(e,t){const n=JSON.parse(fs.readFileSync(path.join(e,`package.json`),`utf8`));if(n.name!==t||typeof n.version!==`string`||!n.version)throw new Error(`Managed handoff FreeBSD native runtime has invalid package metadata`);return{version:n.version,optionalDependencies:n.optionalDependencies}}function loadedNativeFile(e,t){const n=path.join(t,`proc-safe-native.node`);if(!fs.lstatSync(n).isFile())throw new Error(`Managed handoff FreeBSD native runtime requires regular package files`);const r=fs.realpathSync(n);const i=e.cache[r];if(!i?.loaded||i.filename!==r)throw new Error(`Managed handoff could not identify the loaded FreeBSD native runtime`);return r}function runtimeFiles(e,t=`dist`){const n=path.join(e,t);if(!fs.lstatSync(n).isDirectory())throw new Error(`Managed handoff FreeBSD native runtime requires regular package directories`);return fs.readdirSync(n).toSorted().flatMap(n=>{const r=path.join(t,n);const i=fs.lstatSync(path.join(e,r));if(i.isDirectory())return runtimeFiles(e,r);if(!i.isFile())throw new Error(`Managed handoff FreeBSD native runtime requires regular package files`);return n.endsWith(`.js`)?[r]:[]})}function stageFreeBsdManagedHandoffNativeRuntime(e){if(process.platform!==`freebsd`)return[];if(process.arch!==`x64`&&process.arch!==`arm64`)throw new Error(`Managed handoff requires a supported FreeBSD native architecture`);if(process.resourcesPath!==void 0)throw new Error(`Managed handoff cannot use an external FreeBSD native resource path`);const t=createRequire(import.meta.url);const n=path.dirname(t.resolve(`@openclaw/proc-safe/package.json`));const r=t.resolve(`@openclaw/proc-safe/identity`);if(r!==path.join(n,`dist`,`identity.js`)||fs.realpathSync(r)!==r)throw new Error(`Managed handoff FreeBSD identity entry has an unexpected package path`);const i=[`package.json`,`LICENSE`,...runtimeFiles(n)];for(const e of i)if(!fs.lstatSync(path.join(n,e)).isFile())throw new Error(`Managed handoff FreeBSD native runtime requires regular package files`);const a=createRequire(r);const o=`@openclaw/proc-safe-freebsd-${process.arch}`;const s=a.resolve(o);const c=path.dirname(a.resolve(`${o}/package.json`));if(s!==path.join(c,`proc-safe-native.node`)||!fs.lstatSync(s).isFile())throw new Error(`Managed handoff FreeBSD native runtime has an unexpected package path`);const l=packageMetadata(n,`@openclaw/proc-safe`);const u=packageMetadata(c,o);if(l.optionalDependencies?.[o]!==u.version)throw new Error(`Managed handoff FreeBSD native runtime version does not match its package`);const d=a(r).readProcessIdentity(process.pid);if(d?.startTimeSinceBootMicros===void 0)throw new Error(`Managed handoff could not read its FreeBSD process identity`);loadedNativeFile(a,c);const f=path.resolve(e,`runtime`,`node_modules`);const p=[...i.map(e=>({source:path.join(n,e),destination:path.join(f,`@openclaw`,`proc-safe`,e)})),...[`package.json`,`proc-safe-native.node`].map(e=>({source:path.join(c,e),destination:path.join(f,o,e)}))].map(({source:e,destination:t})=>{if(!fs.lstatSync(e).isFile())throw new Error(`Managed handoff FreeBSD native runtime requires regular package files`);fs.mkdirSync(path.dirname(t),{recursive:true,mode:448});fs.writeFileSync(t,fs.readFileSync(e),{mode:384,flag:`wx`});return t});const m=path.join(f,`@openclaw`,`proc-safe`,`dist`,`identity.js`);const h=createRequire(m);if(h(m).readProcessIdentity(process.pid)?.startTimeSinceBootMicros!==d.startTimeSinceBootMicros||loadedNativeFile(h,path.join(f,o))!==fs.realpathSync(path.join(f,o,`proc-safe-native.node`)))throw new Error(`Managed handoff did not load its private FreeBSD native runtime`);return p}
//!src/infra/update-managed-service-handoff-runtime.ts
function stageManagedHandoffRuntime(e){const t=resolveRuntimeWorkerUrl(managedHandoffRuntimeEntrypoint);if(!t.pathname.endsWith(`.mjs`))throw new Error(`Managed handoff requires its sealed runtime; use the repository test runner or the dist-backed pnpm openclaw CLI.`);const n=path.join(e,`runtime`,MANAGED_HANDOFF_RUNTIME_ENTRY);fs.mkdirSync(path.dirname(n),{recursive:true,mode:448});fs.writeFileSync(n,fs.readFileSync(t),{mode:384,flag:`wx`});const r=path.join(e,`runtime`,managedHandoffPostgresBridgeEntrypoint.distWorkerPath);fs.writeFileSync(r,fs.readFileSync(resolveRuntimeWorkerUrl(managedHandoffPostgresBridgeEntrypoint)),{mode:384,flag:`wx`});const i=[n,r];if(process.platform===`win32`){const e=path.join(path.dirname(n),`windows-security-helper.exe`);fs.writeFileSync(e,fs.readFileSync(new URL(`./windows-security-helper.exe`,t)),{mode:448,flag:`wx`});i.push(e)}return[...i,...stageFreeBsdManagedHandoffNativeRuntime(e)]}
//!src/infra/update-managed-service-handoff-engine.worker.ts
let store;let databaseIdentity;let installRoot;let warnings=[];async function run(e){if(e.kind===`prepare`){const t=e.options.databasePath??resolveManagedUpdateLeaseDatabasePath();const n=e.options.existingIdentity??await prepareManagedHandoffLeaseDatabaseIdentity(t);store=await prepareManagedHandoffLeaseStore({...e.options,databasePath:n.databasePath,existingIdentity:n,onProcessIdentityWarning:(e,t)=>warnings.push({pid:e,message:t})});databaseIdentity=n;installRoot=e.options.installRoot;return{kind:`prepare`,value:n}}if(!store||!databaseIdentity||!installRoot)throw new Error(`Managed handoff lease owner is unavailable`);switch(e.kind){case`stageRuntime`:return{kind:e.kind,value:stageManagedHandoffRuntime(e.directory)};case`assertCurrent`:await assertLegacyGatewayCustodySettled(installRoot,resolveLegacyGatewayManagedUpdateLeaseDatabasePath(databaseIdentity.databasePath));assertManagedUpdateLeaseDatabaseIdentity(databaseIdentity);return{kind:e.kind};case`read`:return{kind:e.kind,value:store.read(...e.args)};case`release`:return{kind:e.kind,value:store.release(...e.args)};case`stopNative`:return{kind:e.kind,value:store.stopNative(...e.args)};case`processIdentity`:return{kind:e.kind,value:store.processIdentity(...e.args)};case`isProcessIdentityCurrent`:return{kind:e.kind,value:store.isProcessIdentityCurrent(...e.args)};default:return{kind:e.kind,value:store.inspectProcessIdentity(...e.args)}}}serveWorkerTasks(async e=>{if(typeof e===`string`)return existsSync(e)?readBackupEngine(e).engine:void 0;warnings=[];try{return{result:await run(e),warnings}}catch(e){return{failure:encodeNativeWorkerFailure(e),warnings}}});export{};