openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
1 lines • 7.76 kB
JavaScript
import{q as e}from"./git-operation.worker-subsystem-DibpW_lB.mjs";import{t}from"./git-operation.worker-path-guards-B3dt-SRh.mjs";import{xt as n}from"./git-operation.worker.js";import{_ as r,d as i,g as a,h as o,v as s}from"./git-operation.worker-schemas-OtWwONNf.mjs";import{o as c,s as l,t as u}from"./git-operation.worker-workspace-hash-memo-B4uJ3zCg.mjs";import{c as d,i as f,l as p,o as m,s as h}from"./git-operation.worker-workspace-manifest-Zk1wvpHZ.mjs";import g from"node:fs/promises";import _ from"node:path";import{createHash as v}from"node:crypto";import{constants as y}from"node:fs";import{readFileWindowFully as b}from"@openclaw/fs-safe/advanced";import"@openclaw/fs-safe/errors";import{sha256File as x}from"@openclaw/fs-safe/durability";var S=/* @__PURE__ */ e({isPortableRootContainedSymlink:()=>isPortableRootContainedSymlink,readActualWorkspaceManifestImpl:()=>readActualWorkspaceManifestImpl,readWorkspaceFileContentsWithLimit:()=>readWorkspaceFileContentsWithLimit,readWorkspaceFileSnapshotWithLimit:()=>readWorkspaceFileSnapshotWithLimit,scanActualWorkspaceManifest:()=>scanActualWorkspaceManifest});function isPortableRootContainedSymlink(e,t,n){if(!n||n.includes(`\\`)||_.posix.isAbsolute(n)||_.win32.parse(n).root!==``)return!1;let r=_.resolve(_.dirname(_.join(e,t)),n);return r===e||r.startsWith(`${e}${_.sep}`)}async function readWorkspaceFileSnapshotWithLimit(e,t,n,r,i){return await readWorkspaceFile({expectedPath:e,maxBytes:t,root:n,signal:r,contents:!1,onIdentity:i})}async function readWorkspaceFileContentsWithLimit(e,t){return await readWorkspaceFile({expectedPath:e,maxBytes:t,contents:!0})}async function readWorkspaceFile(e){let{expectedPath:t,maxBytes:n,root:i,signal:s}=e,d=Date.now();s?.throwIfAborted();let p=await g.open(t,y.O_RDONLY|y.O_NOFOLLOW|y.O_NONBLOCK),m;try{s?.throwIfAborted();let{memo:h,metrics:g,owner:_=`gateway`}=u()??{},y=await p.stat({bigint:!0}),S=await r(p,t);if(!y.isFile()||i&&!a(i,S))throw Error(`Gateway workspace file changed while it was being read`);let C=typeof n==`number`?n:n(Number(y.size));if(y.size>BigInt(C))return{type:`unsupported`};let w=l(_,y),T=c(y.ctimeNs,d),E=e.contents||!T?void 0:h?.get(w),D=Number(y.size);if(E)g&&(g.memoHitCount+=1);else{if(e.contents){if(m=Buffer.allocUnsafe(Number(y.size)+1),D=await b(p,m,0,{signal:s}),D>C)return{type:`unsupported`};E=v(`sha256`).update(m.subarray(0,D)).digest(`hex`)}else try{({bytes:D,digest:E}=await x(p,{maxBytes:C,signal:s}))}catch(e){if(s?.throwIfAborted(),!(e instanceof o)||e.code!==`too-large`)throw e;if(typeof n!=`number`)throw Error(`Gateway workspace file changed while it was being read`,{cause:e});return{type:`unsupported`}}g&&(g.contentHashCount+=1)}let O=await p.stat({bigint:!0});if(s?.throwIfAborted(),O.size!==BigInt(D)||l(_,O)!==w)throw Error(`Gateway workspace file changed while it was being read`);T&&h?.set(w,E),e.onIdentity?.(w);let k={type:`file`,mode:f(Number(O.mode&511n)),size:D,sha256:E};return e.contents&&m?{...k,content:m.subarray(0,D)}:k}finally{await p.close()}}async function scanActualWorkspaceManifest(e){let r=Date.now();e.signal?.throwIfAborted();let a,o;try{a=await g.realpath(e.root),o=i(await s(a))}catch(t){throw e.signal?.throwIfAborted(),t}let m=h(e.buildCacheInputs),v=e.gitIgnore?.paths,isExcluded=async t=>{let n=m.has(t)||await o(t);return d(t,n,e.buildCacheInputs!==void 0,e.includeLegacyPnpmStore)?!0:!v?.size||n||e.gitIgnore?.retainedPaths.has(t)?!1:p(t,v)},y=0,b=0,x=0,S=0,C=0,addBytes=e=>{if(b+=e,b>4294967296)throw Error(`Gateway workspace manifest exceeds its eligible byte limit`)},addEntry=(t,n=0,r)=>{if(addBytes(n),x+=Buffer.byteLength(t.path),x>67108864)throw Error(`Gateway workspace manifest paths exceed their byte limit`);if(y+=1,y>25e4)throw Error(`Gateway workspace manifest has too many entries`);e.onEntry(t,r)},w=new AbortController,T=e.signal?AbortSignal.any([e.signal,w.signal]):w.signal,checkTraversal=e=>{if(T.throwIfAborted(),S+=1,C+=Buffer.byteLength(e),S>25e4)throw Error(`Gateway workspace manifest has too many entries`);if(C>67108864)throw Error(`Gateway workspace manifest paths exceed their byte limit`)},E=[],D=u(),O=!1,runScans=async(t,r,i)=>{let a=t,o=await n({tasks:Array.from({length:Math.min(4,r-t)},()=>async()=>{for(;a<r&&!T.aborted;)await i(a++)}),limit:4,errorMode:`stop`,onTaskError:t=>{w.signal.aborted||(O=!e.signal?.aborted),w.abort(t)}});if(o.hasError)throw O||e.signal?.throwIfAborted(),o.firstError;T.throwIfAborted()},addFile=async e=>{let t,n=await readWorkspaceFileSnapshotWithLimit(_.join(a,e),e=>(addBytes(e),e),a,T,e=>{t=e});if(n.type===`file`)addEntry({path:e,...n},0,t);else throw Error(`Gateway workspace manifest exceeds its eligible byte limit`)},addLeaf=async(t,n,i)=>{if(i.isSymbolicLink()){let e=await g.readlink(n);if(!isPortableRootContainedSymlink(a,t,e))return!1;addEntry({path:t,type:`symlink`,mode:511,target:e},Buffer.byteLength(e))}else if(i.isFile()){let a=l(D?.owner??`gateway`,i),o=e.reuseFile?.(t,a,i),s=o?.sha256??(c(i.ctimeNs,r)?D?.memo.get(a):void 0);if(s){if(await g.realpath(n)!==n)throw Error(`Gateway workspace file changed while it was being read`);addEntry(o??{path:t,type:`file`,mode:f(Number(i.mode&511n)),size:Number(i.size),sha256:s},Number(i.size),a),D?.metrics&&(D.metrics.memoHitCount+=1)}else E.push(t)}else return!1;return!0},addIncludedPath=async(n,r,i)=>{if(await isExcluded(n))return`derived-only`;checkTraversal(n);let o=_.join(a,n),s=await g.lstat(o,{bigint:!0}).catch(e=>{if(!(t(e,`ENOENT`)||t(e,`ENOTDIR`)))throw e});if(!s)return`absent`;if(s.isDirectory()&&!s.isSymbolicLink()){if(e.preserveDirectories?.has(n)||r.has(n))return addEntry({path:n,type:`directory`}),`included`;for await(let e of await g.opendir(o)){T.throwIfAborted();let t=`${n}/${e.name}`;if(await isExcluded(t)||i.has(t))return`derived-only`}return addEntry({path:n,type:`directory`}),`included`}return await addLeaf(n,o,s)?`included`:`absent`},walk=async t=>{let n=t?_.join(a,t):a,r=!1,i=!1,o=[],scanBatch=async()=>{let t=[];await runScans(0,o.length,async e=>{let n=o[e];if(await isExcluded(n)){r=!0;return}let s=_.join(a,n),c=await g.lstat(s,{bigint:!0});c.isDirectory()&&!c.isSymbolicLink()?t[e]=n:(i=!0,await addLeaf(n,s,c))});for(let n of t){if(n===void 0)continue;let t=await walk(n);t.included||e.preserveDirectories?.has(n)?(addEntry({path:n,type:`directory`}),i=!0):r||=t.hasDerivedEntry}o.length=0};for await(let e of await g.opendir(n)){let n=e.name,r=t?`${t}/${n}`:n;checkTraversal(r),(t||n!==`.git`)&&(o.push(r),o.length===32&&await scanBatch())}return o.length&&await scanBatch(),{hasDerivedEntry:r,included:i||!r}};if(e.includePaths){let t=/* @__PURE__ */ new Set,n=/* @__PURE__ */ new Set,r=Array.from(e.includePaths,e=>({relative:e,depth:e.split(`/`).length})).toSorted((e,t)=>t.depth-e.depth||e.relative.localeCompare(t.relative));for(let e=0;e<r.length;){let i=e+1;for(;i<r.length&&r[i].depth===r[e].depth;)i++;await runScans(e,i,async e=>{let{relative:i}=r[e],a=await addIncludedPath(i,t,n);a===`included`?t.add(_.posix.dirname(i)):a===`derived-only`&&n.add(i)}),e=i}}else await runScans(0,1,async()=>{await walk(``)});await runScans(0,E.length,e=>addFile(E[e])),T.throwIfAborted()}async function readActualWorkspaceManifestImpl(e){let t=[],n=[];await scanActualWorkspaceManifest({...e,onEntry(e){e.type===`directory`?n.push(e.path):t.push(e)}});let r={version:1,baseCommit:e.baseCommit,...e.buildCacheInputs===void 0?{}:{buildCacheInputs:[...h(e.buildCacheInputs)]},entries:t.toSorted((e,t)=>e.path.localeCompare(t.path)),directories:n.toSorted((e,t)=>e.localeCompare(t))},i=m(r);return{manifestRef:`sha256:${v(`sha256`).update(i).digest(`hex`)}`,manifest:r,rawManifest:i}}export{S as i,readWorkspaceFileContentsWithLimit as n,scanActualWorkspaceManifest as r,isPortableRootContainedSymlink as t};