Skip to content

FilesystemClient

Source: src/services/filesystem.ts

FilesystemClient(transport: Leap0Transport)

Performs filesystem operations inside a sandbox.

  • Leap0Error: If API calls or response validation fail.
async ls(sandbox: SandboxRef, path: string, params: { recursive?: boolean; exclude?: string[] } = {}, options: RequestOptions = {}): Promise<LsResult>

Lists directory contents, optionally recursively.

  • sandbox: Sandbox ID or sandbox-like object.
  • path: Directory path to list.
  • params: Listing options.
  • options: Optional request settings such as timeout and query params.
  • The directory listing result.
async stat(sandbox: SandboxRef, path: string, options: RequestOptions = {}): Promise<FileInfo>

Fetches metadata for a path.

  • sandbox: Sandbox ID or sandbox-like object.
  • path: Filesystem path to stat.
  • options: Optional request settings such as timeout and query params.
  • The path metadata.
async mkdir(sandbox: SandboxRef, path: string, params: { recursive?: boolean; permissions?: string } = {}, options: RequestOptions = {}): Promise<void>

Creates a directory. Set recursive to create parent directories.

  • sandbox: Sandbox ID or sandbox-like object.
  • path: Directory path to create.
  • params: Directory creation options.
  • options: Optional request settings such as timeout and query params.
async writeBytes(sandbox: SandboxRef, path: string, content: Uint8Array, params: { permissions?: string } = {}, options: RequestOptions = {}): Promise<void>

Writes raw bytes to a single file path.

  • sandbox: Sandbox ID or sandbox-like object.
  • path: File path to write.
  • content: File bytes to write.
  • params: File write options.
  • options: Optional request settings such as timeout and query params.
async writeFile(sandbox: SandboxRef, path: string, content: string, params: { permissions?: string } = {}, options: RequestOptions = {}): Promise<void>

Writes UTF-8 text to a single file path.

  • sandbox: Sandbox ID or sandbox-like object.
  • path: File path to write.
  • content: UTF-8 text content to write.
  • params: File write options.
  • options: Optional request settings such as timeout and query params.
async writeFilesBytes(sandbox: SandboxRef, files: Record<string, Uint8Array>, options: RequestOptions = {}): Promise<void>

Writes multiple files in a single request using multipart upload.

  • sandbox: Sandbox ID or sandbox-like object.
  • files: Files keyed by destination path.
  • options: Optional request settings such as timeout and query params.
async writeFiles(sandbox: SandboxRef, files: Record<string, string>, options: RequestOptions = {}): Promise<void>

Writes multiple UTF-8 text files in a single request.

  • sandbox: Sandbox ID or sandbox-like object.
  • files: Files keyed by destination path.
  • options: Optional request settings such as timeout and query params.
async readBytes(sandbox: SandboxRef, path: string, params: { offset?: number; limit?: number; head?: number; tail?: number } = {}, options: RequestOptions = {}): Promise<Uint8Array>

Reads a single file and returns its raw bytes.

  • sandbox: Sandbox ID or sandbox-like object.
  • path: File path to read.
  • params: Read options.
  • options: Optional request settings such as timeout and query params.
  • The file contents as bytes.
  • Error: If both head and tail are provided.
  • Leap0Error: If the read request fails.
async readFile(sandbox: SandboxRef, path: string, params: { offset?: number; limit?: number; head?: number; tail?: number } = {}, options: RequestOptions = {}): Promise<string>

Reads a single file and returns its content decoded as text.

  • sandbox: Sandbox ID or sandbox-like object.
  • path: File path to read.
  • params: Read options.
  • options: Optional request settings such as timeout and query params.
  • The file contents decoded as text.
  • Error: If both head and tail are provided.
  • Leap0Error: If the read request fails.
const readme = await sandbox.filesystem.readFile("/workspace/README.md");
async readFilesBytes(sandbox: SandboxRef, paths: string[], options: RequestOptions = {}): Promise<Record<string, Uint8Array>>

Reads multiple files and returns raw bytes keyed by path.

  • sandbox: Sandbox ID or sandbox-like object.
  • paths: File paths to read.
  • options: Optional request settings such as timeout and query params.
  • File contents keyed by path.
  • Error: If the multipart response is malformed.
  • Leap0Error: If the read request fails.
async readFiles(sandbox: SandboxRef, paths: string[], options: RequestOptions = {}): Promise<Record<string, string>>

Reads multiple files and returns decoded text keyed by path.

  • sandbox: Sandbox ID or sandbox-like object.
  • paths: File paths to read.
  • options: Optional request settings such as timeout and query params.
  • Decoded file contents keyed by path.
async delete(sandbox: SandboxRef, path: string, recursive = false, options: RequestOptions = {}): Promise<void>

Deletes a file or directory. Set recursive for non-empty directories.

  • sandbox: Sandbox ID or sandbox-like object.
  • path: File or directory path to delete.
  • recursive: Whether to delete directories recursively.
  • options: Optional request settings such as timeout and query params.
async setPermissions(sandbox: SandboxRef, path: string, params: { mode?: string; owner?: string; group?: string } = {}, options: RequestOptions = {}): Promise<void>

Sets file mode and optionally changes owner and group.

  • sandbox: Sandbox ID or sandbox-like object.
  • path: File or directory path to update.
  • params: Permission update parameters.
  • options: Optional request settings such as timeout and query params.
  • Leap0Error: If params are invalid or the request fails.
async glob(sandbox: SandboxRef, path: string, pattern: string, params: { exclude?: string[] } = {}, options: RequestOptions = {}): Promise<string[]>

Finds file paths matching a glob pattern.

  • sandbox: Sandbox ID or sandbox-like object.
  • path: Base directory to search.
  • pattern: Glob pattern to match.
  • params: Glob options.
  • options: Optional request settings such as timeout and query params.
  • Matching file paths.
async grep(sandbox: SandboxRef, path: string, pattern: string, params: { include?: string; exclude?: string[] } = {}, options: RequestOptions = {}): Promise<SearchMatch[]>

Searches for a text pattern across files in a directory.

  • sandbox: Sandbox ID or sandbox-like object.
  • path: Base directory to search.
  • pattern: Text or regex pattern to search for.
  • params: Grep options.
  • options: Optional request settings such as timeout and query params.
  • Matching search results.
async editFile(sandbox: SandboxRef, path: string, edits: FileEdit[], options: RequestOptions = {}): Promise<EditFileResult>

Applies one or more find-and-replace edits to a single file.

  • sandbox: Sandbox ID or sandbox-like object.
  • path: File path to edit.
  • edits: Edit operations to apply.
  • options: Optional request settings such as timeout and query params.
  • The single-file edit result.
async editFiles(sandbox: SandboxRef, params: { paths: string[]; find: string; replace?: string }, options: RequestOptions = {}): Promise<EditFilesResult>

Replaces text across multiple files at once.

  • sandbox: Sandbox ID or sandbox-like object.
  • params: Multi-file edit parameters.
  • options: Optional request settings such as timeout and query params.
  • The multi-file edit result.
async move(sandbox: SandboxRef, srcPath: string, dstPath: string, overwrite = false, options: RequestOptions = {}): Promise<void>

Moves or renames a file or directory.

  • sandbox: Sandbox ID or sandbox-like object.
  • srcPath: Source path.
  • dstPath: Destination path.
  • overwrite: Whether to overwrite an existing destination.
  • options: Optional request settings such as timeout and query params.
async copy(sandbox: SandboxRef, srcPath: string, dstPath: string, params: { recursive?: boolean; overwrite?: boolean } = {}, options: RequestOptions = {}): Promise<void>

Copies a file or directory.

  • sandbox: Sandbox ID or sandbox-like object.
  • srcPath: Source path.
  • dstPath: Destination path.
  • params: Copy options.
  • options: Optional request settings such as timeout and query params.
async exists(sandbox: SandboxRef, path: string, options: RequestOptions = {}): Promise<boolean>

Checks whether a path exists in the sandbox.

  • sandbox: Sandbox ID or sandbox-like object.
  • path: File or directory path to check.
  • options: Optional request settings such as timeout and query params.
  • Whether the path exists.
async tree(sandbox: SandboxRef, path: string, params: { maxDepth?: number; exclude?: string[] } = {}, options: RequestOptions = {}): Promise<TreeResult>

Gets a recursive directory tree.

  • sandbox: Sandbox ID or sandbox-like object.
  • path: Root path for the tree.
  • params: Tree options.
  • options: Optional request settings such as timeout and query params.
  • The recursive tree result.