openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
7 lines • 3.63 kB
JavaScript
import{E as parseStrictNonNegativeInteger,d as asPositiveFiniteNumber}from"./number-coercion-B3CfI447.mjs";import{t as createPluginRuntimeProcessEntrypoint}from"./runtime-process-manifest-CUxW6j_V.mjs";import{a as resolveRuntimeWorkerUrl,n as resolveRuntimeWorkerArgv}from"./runtime-worker-url-Dqf-I1cs.mjs";import{t as runCommandBuffered}from"./exec-c5tqm8tb.mjs";import"./process-runtime-Co5MR2hI.mjs";import"./number-runtime-G0nhUVZu.mjs";import{i as resolveBoundReadDirectory,n as readAbsolutePath,o as statRequiredDirectory,t as createReadErrorClassifier}from"./path-errors-BePH4DjW.mjs";import{r as mimeFromExtension}from"./mime-CDIJ7RQm.mjs";import path from"node:path";
//!extensions/file-transfer/src/node-host/dir-list-worker-command.ts
const workerEntry=createPluginRuntimeProcessEntrypoint(`fileTransferDirList`,import.meta.url);function createCanonicalDirListCommand(e){return[process.execPath,...resolveRuntimeWorkerArgv(resolveRuntimeWorkerUrl(workerEntry)),e.directoryPath,e.expectedCanonicalPath,e.expectedDevice,e.expectedInode,String(e.offset),String(e.maxEntries)]}
//!extensions/file-transfer/src/node-host/dir-list-worker.ts
const CANONICAL_PATH_CHANGED_EXIT_CODE=78;const DIR_LIST_MAX_OUTPUT_BYTES=8388608;async function listCanonicalDirectory(e){const t=await runCommandBuffered(createCanonicalDirListCommand(e),{discardOutput:{stderr:true},maxOutputBytes:{stdout:DIR_LIST_MAX_OUTPUT_BYTES,stderr:65536},timeoutMs:6e4}).catch(()=>null);if(t?.termination===`exit`&&t.code===CANONICAL_PATH_CHANGED_EXIT_CODE)return{ok:false,code:`CANONICAL_PATH_CHANGED`};if(!t||t.termination!==`exit`||t.code!==0)return{ok:false,code:`READ_ERROR`};try{return{ok:true,...JSON.parse(t.stdout.toString(`utf8`))}}catch{return{ok:false,code:`READ_ERROR`}}}
//!extensions/file-transfer/src/node-host/dir-list.ts
const DIR_LIST_DEFAULT_MAX_ENTRIES=200;const DIR_LIST_HARD_MAX_ENTRIES=5e3;function parsePageOffset(e){if(typeof e!==`string`)return 0;return parseStrictNonNegativeInteger(e)??0}const classifyFsError=createReadErrorClassifier({ENOENT:`NOT_FOUND`,EACCES:`PERMISSION_DENIED`,EPERM:`PERMISSION_DENIED`});async function handleDirList(e){const t=readAbsolutePath(e.path);if(typeof t!==`string`)return t;const n=Math.min(Math.floor(asPositiveFiniteNumber(e.maxEntries)??DIR_LIST_DEFAULT_MAX_ENTRIES),DIR_LIST_HARD_MAX_ENTRIES);const r=parsePageOffset(e.pageToken);const i=e.followSymlinks===true;const a=await resolveBoundReadDirectory({requestedPath:t,followSymlinks:i,classifyError:classifyFsError,notFoundMessage:`path not found`,expectedCanonicalPath:e.expectedCanonicalPath,expectedBinding:e.expectedBinding});if(!a.ok)return a;const{canonicalPath:o,identity:s}=a;if(e.preflightOnly===true)return{ok:true,path:o,entries:[],truncated:false,preflight:true,binding:{kind:`existing`,...s}};const c=await listCanonicalDirectory({directoryPath:o,expectedCanonicalPath:o,expectedDevice:s.device,expectedInode:s.inode,maxEntries:n,offset:r});if(!c.ok){if(c.code===`CANONICAL_PATH_CHANGED`)return{ok:false,code:`CANONICAL_PATH_CHANGED`,message:`resolved path differs from the authorized target`,canonicalPath:o};const e=await statRequiredDirectory(o,classifyFsError);if(!e.ok)return e;return{ok:false,code:`READ_ERROR`,message:`list failed`,canonicalPath:o}}const l=r+n<c.total;const u=l?String(r+n):void 0;return{ok:true,path:o,entries:c.entries.map(e=>({name:e.name,path:path.join(o,e.name),size:e.isDirectory?0:e.size,mimeType:e.isDirectory?`inode/directory`:mimeFromExtension(e.name),isDir:e.isDirectory,isFile:e.isFile,mtime:e.mtimeMs})),nextPageToken:u,truncated:l,binding:{kind:`existing`,...s}}}export{handleDirList};