openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
11 lines • 6.97 kB
JavaScript
import{t as e}from"./fs-safe-copy.worker-errno-CsW5NI8i.mjs";import{n as t}from"./fs-safe-copy.worker-path-existence-DvAA8vT7.mjs";import{i as n,t as r}from"./fs-safe-copy.worker-fs-safe-BFxtX2RT.mjs";import{t as i}from"./fs-safe-copy.worker-usingCtx-03vOGQ3v.mjs";import a,{constants as o}from"node:fs";import s from"node:fs/promises";import c from"node:path";import l from"node:child_process";function createProvisionCopyCommand(t,n,r){let i=performance.now(),a=l.spawn(`/bin/sh`,[`-c`,`test -d "/proc/self/fd/3/$1" || exit 77
version=$(cp --version 2>/dev/null) || exit 78
case "$version" in 'cp (GNU coreutils)'*) ;; *) exit 78 ;; esac
printf R
while IFS= read -r target; do
IFS= read -r sources || exit 1
set --
for source in $sources; do set -- "$@" "/proc/self/fd/3/$source"; done
cp -L --reflink=auto --preserve=timestamps -t "/proc/self/fd/3/$target" "$@" || exit
printf '.'
done`,`provision-copy`,String(n)],{stdio:[`pipe`,`pipe`,`pipe`,t]});r&&(r.spawnCount++,r.spawnMs+=performance.now()-i);let o=a.stdin,s=a.stdout,c=a.stderr,u,d,f=``;c.on(`data`,e=>{f=(f+e.toString()).slice(0,8192)}),a.once(`error`,e=>{u=e}),o.on(`error`,e=>{u??=e,o.end()});let p=new Promise(e=>{a.once(`close`,t=>{d=t,t!==0&&(u??=/* @__PURE__ */ Error(`Provisioning copy failed (${t}): ${f}`)),e()})}),m=s[Symbol.asyncIterator](),receive=async e=>{try{let t=await m.next();if(!t.done&&t.value.toString()===e)return;throw Error(`Provisioning copy returned no batch receipt`)}catch(e){throw o.end(),await p,u??e}};return{async ready(){try{await receive(`R`);return}catch(t){if(e(t,`ENOENT`)||e(t,`EACCES`))return`helper-unavailable`;if(d===77)return`proc-descriptors-unavailable`;if(d===78)return`gnu-cp-unavailable`;throw t}},async copy(e,t){let n=performance.now();r&&r.batchCount++;try{o.write(`${e}\n${t.join(` `)}\n`),await receive(`.`)}finally{r&&(r.childMs+=performance.now()-n)}},async close(){o.end(),await p}}}function normalizeProvisionedRelativePath(e){if(!c.isAbsolute(e)&&!e.split(`/`).some(e=>!e||e===`.`||e===`..`))return e}function resolveGitPath(e,t){return c.join(e,...t.split(`/`))}async function hasSafeParentDirectories(e,n){let r=n.split(`/`),i=e;for(let e of r.slice(0,-1)){i=c.join(i,e);let n=await t(i,`ENOENT`);if(n&&(n.isSymbolicLink()||!n.isDirectory()))return!1}return!0}const selection=()=>({files:/* @__PURE__ */ new Set,directories:/* @__PURE__ */ new Map}),descriptorPath=e=>`/proc/self/fd/${e.fd}`,isAbsentOrSymlink=t=>e(t,`ENOENT`)||e(t,`ENOTDIR`)||e(t,`ELOOP`);async function openDirectory(e){try{return await s.open(e,o.O_RDONLY|o.O_DIRECTORY|o.O_NOFOLLOW)}catch(e){if(!isAbsentOrSymlink(e))throw e;return}}async function copyProvisionedPathsLinux(t,n,r,c){try{var l=i();if(process.platform!==`linux`){c.copyDetails&&(c.copyDetails.fallbackReason=`platform`);return}let u=l.a(await s.open(`/proc/self/fd`,o.O_RDONLY|o.O_DIRECTORY).catch(()=>void 0));if(!u){c.copyDetails&&(c.copyDetails.fallbackReason=`procfs-unavailable`);return}let d=selection();for(let e of r){let t=normalizeProvisionedRelativePath(e);if(!t)continue;let n=t.split(`/`),r=n.pop(),i=d;for(let e of n){let t=i.directories.get(e);t||(t=selection(),i.directories.set(e,t)),i=t}i.files.add(r)}let openRoot=async e=>{let t=await openDirectory(e.rootReal);if(!t)throw Error(`Provisioning root changed`);try{let[n,r]=await Promise.all([t.stat({bigint:!0}),e.stat(`.`)]);if(n.dev!==r.exactIdentity?.dev||n.ino!==r.exactIdentity?.ino)throw Error(`Provisioning root changed`);return t}catch(e){throw await t.close(),e}},f=l.a(await openRoot(t)),p=l.a(await openRoot(n)),m,h=[],g=[],_=/* @__PURE__ */ new Map,v=0,check=()=>c.signal?.throwIfAborted(),releaseBatch=()=>{for(let e of g)a.closeSync(e.fd);g.length=0;for(let e of _.values())a.closeSync(e);_.clear()},flush=async()=>{if(!g.length)return;let t;try{try{var n=i();check(),t=await s.mkdtemp(`${descriptorPath(p)}/.provision-`);let r=n.a(await s.open(t,o.O_RDONLY|o.O_DIRECTORY|o.O_NOFOLLOW));check(),c.assertCurrent?.(),check(),await m.copy(r.fd,g.map(e=>e.fd)),check(),c.assertCurrent?.(),check();for(let t of g){let n=`${descriptorPath(r)}/${t.fd}`;a.chmodSync(n,t.mode);try{a.linkSync(n,t.target)}catch(t){if(e(t,`EEXIST`))continue;throw t}h.push(t.path)}}catch(e){n.e=e}finally{await n.d()}}finally{releaseBatch(),t&&a.rmSync(t,{recursive:!0,force:!0})}check(),c.assertCurrent?.(),check()},visit=async(t,n,r,l)=>{check(),c.assertCurrent?.(),check();for(let e of r.files){if(a.lstatSync(`${descriptorPath(n)}/${e}`,{throwIfNoEntry:!1}))continue;let r;try{r=a.openSync(`${descriptorPath(t)}/${e}`,o.O_RDONLY|o.O_NOFOLLOW|o.O_NONBLOCK)}catch(e){if(isAbsentOrSymlink(e))continue;throw e}let i=!1;try{let t=a.fstatSync(r);if(t.isFile()){c.copyDetails&&(c.copyDetails.sourceBytes+=t.size);let s=_.get(l);s===void 0&&(s=a.openSync(`${descriptorPath(n)}/.`,o.O_RDONLY|o.O_DIRECTORY|o.O_NOFOLLOW),_.set(l,s)),g.push({path:l+e,target:`/proc/self/fd/${s}/${e}`,fd:r,mode:t.mode&4095}),i=!0,v++}}finally{i||a.closeSync(r)}g.length===128&&await flush()}for(let[a,o]of r.directories)try{var u=i();check();let r=u.a(await openDirectory(`${descriptorPath(t)}/${a}`));if(!r)continue;let c=`${descriptorPath(n)}/${a}`,d=!0;await s.mkdir(c).catch(t=>{if(!e(t,`EEXIST`))throw t;d=!1});let f=u.a(await openDirectory(c)),p=v;f&&await visit(r,f,o,`${l}${a}/`),d&&v===p&&await s.rmdir(c)}catch(e){u.e=e}finally{await u.d()}};try{check(),c.assertCurrent?.(),check(),m=createProvisionCopyCommand(u.fd,f.fd,c.copyDetails);let e=await m.ready();if(e){c.copyDetails&&(c.copyDetails.fallbackReason=e);return}return c.copyDetails&&(c.copyDetails.copier=`linux-batched`),await visit(f,p,d,``),await flush(),h.toSorted()}finally{await m?.close(),releaseBatch()}}catch(e){l.e=e}finally{await l.d()}}async function copyProvisionedFile(e){let n=e.relativePath;if(!await hasSafeParentDirectories(e.sourceRoot.rootReal,n)||!await hasSafeParentDirectories(e.destinationRoot.rootReal,n))return!1;let i=resolveGitPath(e.sourceRoot.rootReal,n),a=resolveGitPath(e.destinationRoot.rootReal,n),o=await s.lstat(i).catch(()=>void 0);if(!o?.isFile()||o.isSymbolicLink()||await t(a,`ENOENT`))return!1;try{await e.destinationRoot.copyIn(a,{root:e.sourceRoot,relativePath:i},{overwrite:!1,clone:`auto`,maxBytes:1/0,preserveSourceMode:!0,preserveMetadata:!0,sourceHardlinks:`allow`,mutationSymlinks:`reject`,durable:!1,assertBeforeMutation:e.assertCurrent,signal:e.signal})}catch(e){if(e instanceof r&&e.code===`already-exists`)return!1;throw e}return e.assertCurrent?.(),e.copyDetails&&(e.copyDetails.sourceBytes+=o.size),!0}async function copyProvisionedPaths(e,t,r,i){let[a,o]=await Promise.all([n(e),n(t)]);i.signal?.throwIfAborted();let s=await copyProvisionedPathsLinux(a,o,r,i);if(s)return s;let c=[];for(let e of r){let t=normalizeProvisionedRelativePath(e);t&&await copyProvisionedFile({sourceRoot:a,destinationRoot:o,relativePath:t,assertCurrent:i.assertCurrent,signal:i.signal,copyDetails:i.copyDetails})&&c.push(t)}return c.toSorted()}export{copyProvisionedPaths};