UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

1 lines • 5.28 kB
import{P as e}from"./git-operation.worker-subsystem-DibpW_lB.mjs";import{t}from"./git-operation.worker-path-guards-B3dt-SRh.mjs";import{ot as n}from"./git-operation.worker.js";import{d as r,m as i,v as a}from"./git-operation.worker-schemas-OtWwONNf.mjs";import{c as o,d as s,f as c,i as l,p as u}from"./git-operation.worker-workspace-manifest-Zk1wvpHZ.mjs";import{t as d}from"./git-operation.worker-workspace-actual-manifest-CCtoh4gL.mjs";import f from"node:fs/promises";import p from"node:path";import{createReadStream as m,lstatSync as h,readlinkSync as g}from"node:fs";const workspaceInventoryError=e=>Object.assign(Error(e),{code:`invalid_state`,name:`WorkerWorkspacePreflightError`});function createInventoryPathWriter(){let e=[],t=0,flush=async()=>{e.length!==0&&(await n({type:`workspace.inventory.write`,input:{bytes:Uint8Array.from(Buffer.from(e.join(``)))}}),e=[],t=0)};return{flush,append(n){let r=`${n}\0`;if(e.push(r),t+=Buffer.byteLength(r),t>=65536)return flush()}}}function validateGitRelativePath(e){if(!e||p.posix.isAbsolute(e)||p.posix.normalize(e)!==e||e===`..`||e.startsWith(`../`))throw Error(`Worker workspace git file list contains an unsafe path`);return e}async function*readBoundedGitPathCandidates(e){let t=Buffer.alloc(0),n=0,r=0;for await(let i of m(e)){let e=Buffer.isBuffer(i)?i:Buffer.from(i);if(r+=e.byteLength,r>67108864)throw workspaceInventoryError(`Cloud workspace Git path metadata exceeds the 64 MiB limit`);let a=t.length===0?e:Buffer.concat([t,e]),o=0;for(;;){let e=a.indexOf(0,o);if(e<0)break;if(n+=1,n>1e6)throw workspaceInventoryError(`Cloud workspace Git path candidates exceed the ${c} limit`);yield validateGitRelativePath(a.subarray(o,e).toString(`utf8`)),o=e+1}t=Buffer.from(a.subarray(o))}if(t.length>0)throw Error(`Worker workspace git file list is not NUL terminated`)}async function executeWorkspaceInventoryComputation(n){switch(n.type){case`workspace.inventory.staged-directories`:{let e=n.input.rootDir,t=await a(e),o=await t.stat(`media/inbound`).catch(()=>void 0);if(!o?.isDirectory||o.isSymbolicLink)return[];let s=r(t),c=[],l=0;for await(let t of await f.opendir(p.join(e,`media/inbound`))){if(++l>25e4)throw workspaceInventoryError(`Cloud workspace has too many entries`);let e=i(`media/inbound/${t.name}`);t.isDirectory()&&e&&await s(e)&&c.push(e)}return c.toSorted()}case`workspace.inventory.select`:{let i=n.input,c=await f.realpath(i.gitRoot),m=r(await a(c)),_=/* @__PURE__ */ new Set,v=Buffer.byteLength(JSON.stringify({version:1,baseCommit:`0`.repeat(64),entries:[]})),y=0,b=0,x=0,S=0,assertBudget=()=>{let e=_.size;if(e>25e4)throw workspaceInventoryError(`Cloud workspace inventory exceeds ${u} manifest entries; reduce eligible files or narrow .worktreeinclude`);if(y>67108864)throw workspaceInventoryError(`Cloud workspace manifest paths exceed the 64 MiB metadata limit; reduce eligible files or shorten their paths`);if(b>67108864)throw workspaceInventoryError(`Cloud workspace eligible paths exceed the 64 MiB metadata limit; reduce eligible files or narrow .worktreeinclude`);if(v+x+Math.max(0,e-1)>67108864)throw workspaceInventoryError(`Cloud workspace manifest exceeds the 64 MiB limit; reduce eligible files or shorten their paths`);if(S>4294967296)throw workspaceInventoryError(`Cloud workspace eligible content exceeds the 4 GiB limit; remove large eligible files or ignore them`)},addEntry=e=>{_.has(e.path)||(_.add(e.path),y+=Buffer.byteLength(e.path),S+=e.type===`file`?e.size:e.type===`symlink`?Buffer.byteLength(e.target):0,x+=Buffer.byteLength(JSON.stringify(e)),assertBudget())},C=/* @__PURE__ */ new Set,w=createInventoryPathWriter(),inspectFile=async n=>{if(o(n,await m(n))||C.has(n))return;let r=p.join(c,n),i=(()=>{try{return h(r)}catch(e){if(t(e,`ENOENT`)||t(e,`ENOTDIR`))return;throw e}})();if(i&&(i.isFile()||i.isSymbolicLink())){if(i.isSymbolicLink()){let t=g(r);if(!d(c,n,t))throw workspaceInventoryError(`Cloud workspace symlink is not portable or escapes the sync root: ${e(n,0,160)}`);return{path:n,type:`symlink`,mode:511,target:t}}return{path:n,type:`file`,mode:l(i.mode),size:i.size,sha256:`0`.repeat(64)}}};async function*candidates(){yield*readBoundedGitPathCandidates(i.eligiblePath);let e=readBoundedGitPathCandidates(i.selectedPath);try{let t=await e.next();for await(let n of readBoundedGitPathCandidates(i.ignoredPath)){for(;!t.done&&Buffer.compare(Buffer.from(t.value),Buffer.from(n))<0;)t=await e.next();(await m(n)||!t.done&&t.value===n)&&(yield n)}for(;!t.done;)t=await e.next()}finally{await e.return(void 0)}}for await(let e of candidates()){let t=await inspectFile(e);if(!t)continue;C.add(e);for(let t of s(e))addEntry({path:t,type:`directory`,mode:448});addEntry(t),b+=Buffer.byteLength(e)+1,assertBudget();let n=w.append(e);n&&await n}await w.flush();return}case`workspace.inventory.existing`:{let e=n.input,r=createInventoryPathWriter();for await(let n of readBoundedGitPathCandidates(e.preparedListPath)){let i;try{i=h(p.join(e.gitRoot,n))}catch(e){if(!t(e,`ENOENT`))throw e}if(i?.isFile()||i?.isSymbolicLink()){let e=r.append(n);e&&await e}}await r.flush();return}case`workspace.inventory.paths`:{let e=/* @__PURE__ */ new Set;for await(let t of readBoundedGitPathCandidates(n.input.filePath))e.add(t);return e}}}export{executeWorkspaceInventoryComputation};