openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
3 lines • 3.6 kB
JavaScript
import path from"node:path";import fs from"node:fs/promises";import{appendRegularFile,canonicalPathFromExistingAncestor as canonicalPathFromExistingAncestor$1,ensureDirectoryWithinRoot,findExistingAncestor,findExistingAncestor as findExistingAncestor$1,movePathToTrash,pathExistsSync,readLocalFileFromRoots,readRegularFile as readRegularFile$1,readRegularFileSync as readRegularFileSync$1,resolveAbsolutePathForRead as resolveAbsolutePathForRead$1,resolveAbsolutePathForWrite,resolveLocalPathFromRootsSync,sanitizeUntrustedFileName as sanitizeUntrustedFileName$1,statRegularFile,statRegularFileSync as statRegularFileSync$1,withTimeout as withTimeout$1}from"@openclaw/fs-safe/advanced";import{FsSafeError as FsSafeError$1}from"@openclaw/fs-safe/errors";import{isPathInside as isPathInside$1}from"@openclaw/fs-safe/path";import{writeExternalFileWithinRoot}from"@openclaw/fs-safe/output";import{openLocalFileSafely as openLocalFileSafely$1,readLocalFileSafely,resolveOpenedFileRealPathForHandle,root}from"@openclaw/fs-safe/root";import{readSecureFile}from"@openclaw/fs-safe/secure-file";import{walkDirectory as walkDirectory$1,walkDirectorySync}from"@openclaw/fs-safe/walk";
//!src/infra/fs-safe.ts
async function pathExists(e){try{let t=e;if(process.platform===`win32`&&/^[\\/]{2}[?.][\\/][A-Za-z]:[\\/]/.test(e)){if(e.length===7)t=`${e[4]}:\\`;else{const n=path.resolve(e);if(/^[\\/]{2}[?.][\\/][A-Za-z]:$/.test(n)&&!e.includes(`:`,6))t=`${n[4]}:\\`}}await fs.stat(t);return true}catch{return false}}const readLocalFileFromRoots$1=readLocalFileFromRoots;async function root$1(e,t){return await root(e,t)}async function ensureAbsoluteDirectory$1(e,t){const n=path.resolve(e);const r=t?.scopeLabel??`directory`;const i=await findExistingAncestor(n);if(i&&i!==n){const e=await ensureDirectoryWithinRoot({rootDir:i,requestedPath:path.relative(i,n),scopeLabel:r,mode:t?.mode});return e.ok?e:{ok:false,error:new Error(e.error)}}if(i)try{const e=await fs.lstat(n);if(!e.isSymbolicLink()&&e.isDirectory())return{ok:true,path:n}}catch{}return{ok:false,error:/* @__PURE__ */ new Error(`Invalid path: must stay within ${r}`)}}async function writeExternalFileWithinRoot$1(e){const t=path.resolve(e.rootDir,e.path);const n=await writeExternalFileWithinRoot({rootDir:e.rootDir,path:e.path,write:e.write,staging:`sibling`,producerIsolation:`private-directory`,fallbackFileName:e.fallbackFileName??e.tempPrefix});return{path:path.join(path.dirname(t),path.basename(n.path))}}async function readFileWithinRoot(e){return await(await root(e.rootDir)).read(e.relativePath,{hardlinks:e.rejectHardlinks===false?`allow`:`reject`,maxBytes:e.maxBytes,symlinks:e.allowSymlinkTargetWithinRoot===true?`follow-within-root`:`reject`})}async function writeFileWithinRoot(e){await(await root$1(e.rootDir)).write(e.relativePath,e.data,{encoding:e.encoding,mkdir:e.mkdir})}export{statRegularFile as C,withTimeout$1 as D,walkDirectorySync as E,writeExternalFileWithinRoot$1 as O,sanitizeUntrustedFileName$1 as S,walkDirectory$1 as T,resolveAbsolutePathForRead$1 as _,findExistingAncestor$1 as a,resolveOpenedFileRealPathForHandle as b,openLocalFileSafely$1 as c,readFileWithinRoot as d,readLocalFileFromRoots$1 as f,readSecureFile as g,readRegularFileSync$1 as h,ensureAbsoluteDirectory$1 as i,writeFileWithinRoot as k,pathExists as l,readRegularFile$1 as m,appendRegularFile as n,isPathInside$1 as o,readLocalFileSafely as p,canonicalPathFromExistingAncestor$1 as r,movePathToTrash as s,FsSafeError$1 as t,pathExistsSync as u,resolveAbsolutePathForWrite as v,statRegularFileSync$1 as w,root$1 as x,resolveLocalPathFromRootsSync as y};