UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

1 lines • 5.34 kB
import{a as e}from"./git-operation.worker-fs-safe-2XqdJR5K.mjs";import{t}from"./git-operation.worker-path-guards-B73RPnzx.mjs";import{y as n}from"./git-operation.worker-redact-CGAANVVf.mjs";import{Ut as r}from"./git-operation.worker.js";import{a as i,n as a}from"./git-operation.worker-staged-inputs-DZiTgX91.mjs";import{_ as o,d as s,g as c,o as l,v as u}from"./git-operation.worker-workspace-manifest-DsRMcvMP.mjs";import{t as d}from"./git-operation.worker-workspace-actual-manifest-Y3A72WaR.mjs";import{createReadStream as f,lstatSync as p,readlinkSync as m}from"node:fs";import h from"node:fs/promises";import g from"node:path";const workspaceInventoryError=e=>Object.assign(Error(e),{code:`invalid_state`,name:`WorkerWorkspacePreflightError`});function createInventoryPathWriter(){let e=[],t=0,flush=async()=>{e.length!==0&&(await r({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||g.posix.isAbsolute(e)||g.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 f(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]),s=0;for(;;){let e=a.indexOf(0,s);if(e<0)break;if(n+=1,n>1e6)throw workspaceInventoryError(`Cloud workspace Git path candidates exceed the ${o} limit`);yield validateGitRelativePath(a.subarray(s,e).toString(`utf8`)),s=e+1}t=Buffer.from(a.subarray(s))}if(t.length>0)throw Error(`Worker workspace git file list is not NUL terminated`)}async function executeWorkspaceInventoryComputation(r){switch(r.type){case`workspace.inventory.staged-directories`:{let t=r.input.rootDir,n=await e(t),o=await n.stat(`media/inbound`).catch(()=>void 0);if(!o?.isDirectory||o.isSymbolicLink)return[];let s=a(n),c=[],l=0;for await(let e of await h.opendir(g.join(t,`media/inbound`))){if(++l>25e4)throw workspaceInventoryError(`Cloud workspace has too many entries`);let t=i(`media/inbound/${e.name}`);e.isDirectory()&&t&&await s(t)&&c.push(t)}return c.toSorted()}case`workspace.inventory.select`:{let i=r.input,o=await h.realpath(i.gitRoot),f=a(await e(o)),_=/* @__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 e=>{if(s(e,await f(e))||C.has(e))return;let r=g.join(o,e),i=(()=>{try{return p(r)}catch(e){if(t(e,`ENOENT`)||t(e,`ENOTDIR`))return;throw e}})();if(i&&(i.isFile()||i.isSymbolicLink())){if(i.isSymbolicLink()){let t=m(r);if(!d(o,e,t))throw workspaceInventoryError(`Cloud workspace symlink is not portable or escapes the sync root: ${n(e,0,160)}`);return{path:e,type:`symlink`,mode:511,target:t}}return{path:e,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 f(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 c(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=r.input,n=createInventoryPathWriter();for await(let r of readBoundedGitPathCandidates(e.preparedListPath)){let i;try{i=p(g.join(e.gitRoot,r))}catch(e){if(!t(e,`ENOENT`))throw e}if(i?.isFile()||i?.isSymbolicLink()){let e=n.append(r);e&&await e}}await n.flush();return}case`workspace.inventory.paths`:{let e=/* @__PURE__ */ new Set;for await(let t of readBoundedGitPathCandidates(r.input.filePath))e.add(t);return e}}}export{executeWorkspaceInventoryComputation};