UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

1 lines • 3.9 kB
import{i as e}from"./fs-safe-copy.worker-global-singleton-DIJgobpU.mjs";import{i as t,t as n}from"./fs-safe-copy.worker-fs-safe-BFxtX2RT.mjs";import{d as r}from"./fs-safe-copy.worker-redact-BQxLxSX2.mjs";import{r as i}from"./fs-safe-copy.worker-store.shared-Bv7dKXsQ.mjs";import{t as a}from"./fs-safe-copy.worker-process-exit-cleanup-BJBelOoF.mjs";import{n as o}from"./fs-safe-copy.worker-directory-durability-CoFeWTxf.mjs";import s from"node:fs";import c from"node:fs/promises";import l from"node:path";import{assertDirectoryIdentitySync as u,buildRandomTempFilePath as d,readDirectoryIdentity as f,sameFileIdentity as p}from"@openclaw/fs-safe/advanced";const m=e(Symbol.for(`openclaw.readMediaExitCleanup`),()=>{let e=/* @__PURE__ */ new Set;return a(()=>{for(let t of e)try{t()}catch(e){console.error(`Channel read output cleanup failed at exit: ${r(String(e))}`)}}),e});function isUnownedMediaPath(e){return e instanceof n&&[`not-found`,`not-file`,`path-mismatch`,`symlink`,`hardlink`].includes(e.code)||e instanceof Error&&`code`in e&&e.code===`ENOENT`}async function captureDirectoryGuard(e){let t=await f(e);return r=>{if(u(e,t),r&&!p(s.fstatSync(r.fd,{bigint:!0}),t))throw new n(`path-mismatch`,`Media output directory identity changed`)}}async function writeReadScopeMedia(e){let assertCurrent=()=>{e.scope?.assertCurrent(),e.assertCommitAllowed?.()};assertCurrent();let r=await captureDirectoryGuard(e.dir),a=await t(e.dir),u=await captureDirectoryGuard(a.rootReal);if(process.platform!==`win32`){let e=await c.open(a.rootReal,s.constants.O_RDONLY|s.constants.O_DIRECTORY|s.constants.O_NOFOLLOW|s.constants.O_NONBLOCK);try{assertCurrent(),r(),u(e),await e.chmod(448).catch(()=>void 0)}finally{await i({key:a.rootReal,settle:async()=>await e.close()},!0)}}let f=d({rootDir:a.rootReal,prefix:e.tempPrefix,extension:`.tmp`}),p=l.basename(f),h,g,_=!1,v,y,cleanupAtExit,assertCustody,b={get key(){return g?l.join(e.dir,g):f},onDelegated:e=>{let t=assertCustody;assertCustody=()=>{t?.(),e?.()},cleanupAtExit&&m.delete(cleanupAtExit)},assertCurrent:()=>{v||(r(),u(),g&&y?.(l.join(a.rootReal,g)))},settle:e=>v??=(async()=>{e&&cleanupAtExit&&m.delete(cleanupAtExit);let t=[];try{if(!e&&h){if(!y)throw new n(`path-mismatch`,`Cannot verify created media for cleanup`);for(let e of[g,p])if(e)try{await a.remove(e,{assertBeforeMutation:()=>{assertCustody?.(),u(),y?.(l.join(a.rootReal,e))}})}catch(e){isUnownedMediaPath(e)||t.push(e)}}}catch(e){t.push(e)}cleanupAtExit&&m.delete(cleanupAtExit);try{await h?.close()}catch(e){t.push(e)}if(t.length>0)throw AggregateError(t,`Failed to release channel read output`)})()};try{assertCurrent(),h=await c.open(f,`wx`,420);let t=h,d=s.fstatSync(t.fd,{bigint:!0}),assertCreatedFile=e=>{let r=s.fstatSync(t.fd,{bigint:!0}),i=s.lstatSync(e,{bigint:!0});if(!r.isFile()||!i.isFile()||i.isSymbolicLink()||r.nlink!==1n||i.nlink!==1n||r.dev!==d.dev||r.ino!==d.ino||i.dev!==d.dev||i.ino!==d.ino||process.platform===`win32`&&(d.dev===0n||d.ino===0n))throw new n(`path-mismatch`,`Media output no longer names the created file`)};y=assertCreatedFile;let assertPublicationAllowed=()=>{assertCurrent(),r(),u(),assertCreatedFile(f)};cleanupAtExit=()=>{for(let e of[g,p])if(e)try{let t=l.join(a.rootReal,e);u(),y?.(t),s.unlinkSync(t)}catch(e){if(!isUnownedMediaPath(e))throw e}},m.add(cleanupAtExit),assertCurrent();let v=await e.write(t);assertPublicationAllowed();try{await t.chmod(420)}catch(e){if(process.platform!==`win32`&&(s.fstatSync(t.fd).mode&292)!=292)throw e}return e.durable&&(assertPublicationAllowed(),await t.sync()),assertPublicationAllowed(),g=v.id,await a.move(p,g,{overwrite:!1,assertBeforeMutation:assertPublicationAllowed}),b.assertCurrent(),e.durable&&(assertCurrent(),await o(a.rootReal),assertCurrent(),b.assertCurrent()),assertCurrent(),e.scope?e.scope.registerResource(b):await i(b,!0),_=!0,v}finally{_||await i(b,!1)}}export{writeReadScopeMedia};