UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

5 lines • 4.87 kB
import{d as asPositiveFiniteNumber}from"./number-coercion-B3CfI447.mjs";import{t as createPluginRuntimeProcessEntrypoint}from"./runtime-process-manifest-CUxW6j_V.mjs";import{x as root}from"./fs-safe-Bxzu1j4h.mjs";import{t as formatErrorMessage}from"./errors-D37BJTfI.mjs";import{a as resolveRuntimeWorkerUrl,n as resolveRuntimeWorkerArgv}from"./runtime-worker-url-Dqf-I1cs.mjs";import{t as runCommandBuffered}from"./exec-c5tqm8tb.mjs";import{n as ArchiveLimitError}from"./archive-BAuHVLxp.mjs";import"./security-runtime-Y28JTB1K.mjs";import"./process-runtime-Co5MR2hI.mjs";import"./number-runtime-G0nhUVZu.mjs";import"./error-runtime-DW-sj0iq.mjs";import"./archive-CS1U7znq.mjs";import{a as DIR_FETCH_MAX_ENTRIES,i as DIR_FETCH_HARD_MAX_BYTES,n as inspectDirFetchArchive,r as DIR_FETCH_DEFAULT_MAX_BYTES}from"./dir-fetch-archive-Ddnnu07X.mjs";import{n as matchesFileIdentity}from"./path-binding-DEwRklUS.mjs";import{i as resolveBoundReadDirectory,n as readAbsolutePath,o as statRequiredDirectory,t as createReadErrorClassifier}from"./path-errors-BePH4DjW.mjs";import path from"node:path";import crypto from"node:crypto";import fs from"node:fs/promises"; //!extensions/file-transfer/src/node-host/dir-fetch-archive.ts const CANONICAL_PATH_CHANGED_EXIT_CODE=78;const workerEntry=createPluginRuntimeProcessEntrypoint(`fileTransferDirFetch`,import.meta.url);async function createTarArchive(e,t,n,r,i){const a=process.platform!==`win32`?`/usr/bin/tar`:`tar`;const o=6e4;const s=await runCommandBuffered([process.execPath,...resolveRuntimeWorkerArgv(resolveRuntimeWorkerUrl(workerEntry)),e,t,n,r,a],{discardOutput:{stderr:true},maxOutputBytes:{stdout:i,stderr:65536},timeoutMs:o}).catch(()=>null);if(!s)return`ERROR`;if(s.termination===`timeout`)return`TIMEOUT`;if(s.termination===`output-limit`&&s.outputLimitStream===`stdout`)return`TOO_LARGE`;if(s.termination===`exit`&&s.code===CANONICAL_PATH_CHANGED_EXIT_CODE)return`CANONICAL_PATH_CHANGED`;return s.termination===`exit`&&s.code===0?s.stdout:`ERROR`} //!extensions/file-transfer/src/node-host/dir-fetch.ts const classifyFsError=createReadErrorClassifier({ENOENT:`NOT_FOUND`});async function listTreeEntries(e,t,n){const r=[];const i=await root(e);const a=await fs.stat(i.rootReal,{bigint:true});if(!matchesFileIdentity(a,n))throw Object.assign(/* @__PURE__ */ new Error(`filesystem identity differs from the authorized target`),{code:`CANONICAL_PATH_CHANGED`});async function visit(e){const n=await i.list(e,{withFileTypes:true});for(const i of n.toSorted((e,t)=>e.name.localeCompare(t.name)||(e.name<t.name?-1:1))){const n=path.posix.join(e,i.name);r.push(n);if(r.length>t)return false;if(i.isDirectory&&!await visit(n))return false}return true}return await visit(`.`)?r:`TOO_MANY`}async function handleDirFetch(e){const t=readAbsolutePath(e.path);if(typeof t!==`string`)return t;const n=Math.min(Math.floor(asPositiveFiniteNumber(e.maxBytes)??8388608),DIR_FETCH_HARD_MAX_BYTES);const r=e.followSymlinks===true;const i=e.preflightOnly===true;const a=await resolveBoundReadDirectory({requestedPath:t,followSymlinks:r,classifyError:classifyFsError,notFoundMessage:`directory not found`,expectedCanonicalPath:e.expectedCanonicalPath,expectedBinding:e.expectedBinding});if(!a.ok)return a;const{canonicalPath:o,identity:s}=a;const failure=(e,t)=>({ok:false,code:e,message:t,canonicalPath:o});let c;if(i){let e;try{e=await listTreeEntries(o,DIR_FETCH_MAX_ENTRIES,s)}catch(e){return failure((e&&typeof e===`object`&&`code`in e?e.code:void 0)===`CANONICAL_PATH_CHANGED`?`CANONICAL_PATH_CHANGED`:classifyFsError(e),`preflight readdir failed: ${String(e)}`)}if(e===`TOO_MANY`)return failure(`TREE_TOO_LARGE`,`directory tree exceeds 5000 entries during preflight`);c=e}const l=await createTarArchive(o,o,s.device,s.inode,n);if(l===`TOO_LARGE`)return failure(`TREE_TOO_LARGE`,`tarball exceeded ${n} byte limit ${i?`during preflight`:`mid-stream`}`);if(l===`TIMEOUT`)return failure(`READ_ERROR`,`tar command exceeded 60s wall-clock timeout (slow filesystem or symlink loop?)`);if(l===`CANONICAL_PATH_CHANGED`)return failure(`CANONICAL_PATH_CHANGED`,`resolved path differs from the authorized target`);if(l===`ERROR`){if(i){const e=await statRequiredDirectory(o,classifyFsError);if(!e.ok)return e}return failure(`READ_ERROR`,`tar command failed`)}if(c)return{ok:true,path:o,tarBase64:``,tarBytes:0,sha256:``,fileCount:c.length,entries:c,preflightOnly:true,binding:{kind:`existing`,...s}};const u=crypto.createHash(`sha256`).update(l).digest(`hex`);const d=l.toString(`base64`);const f=l.byteLength;let p;try{p=await inspectDirFetchArchive(l,1e4)}catch(e){return failure(e instanceof ArchiveLimitError?`TREE_TOO_LARGE`:`READ_ERROR`,`archive inspection failed: ${formatErrorMessage(e)}`)}return{ok:true,path:o,tarBase64:d,tarBytes:f,sha256:u,fileCount:p.length,entries:p,binding:{kind:`existing`,...s}}}export{handleDirFetch as t};