Skip to content

Abstract Class: BaseFileSystem\<TBackend\>

Base implementation for local, remote, and virtual filesystems.

Subclasses implement the low-level *At primitives. This class provides:

  • Memoized _init (so an explicit init call is optional)
  • Optional root creation via createRootDirectory
  • POSIX-only rooted path normalization and traversal protection
  • toBackendPath for host/separator conversion at the boundary
  • Text encoding and decoding
  • exists
  • Parent-directory creation on write / append / copy / move
  • Append / copy / move fallbacks (override try* for native ops)
  • Recursive mkdir, readdir, and rmdir
  • Extension filtering for readdir

Namespace paths always use /. Host adapters convert with posixPath.toPosix / posixPath.toHost in toBackendPath.

TBackend extends string = string

protected new BaseFileSystem<TBackend>(options): BaseFileSystem<TBackend>

BaseFileSystemOptions<TBackend>

BaseFileSystem<TBackend>

protected _init: () => Promise<void>

Memoized initialization. Every operation that needs a ready backend awaits this, so callers (e.g. Mastra) may call init every time or never; both are fine.

Promise<void>


readonly backend: TBackend

Identifier for the underlying implementation.

Examples: “disk”, “ftp”, “sftp”, “memory”, “s3”, or “dbfs”.

FileSystem.backend


protected readonly createRoot: boolean


readonly id: string

Unique identifier for this filesystem instance.

FileSystem.id


readonly readOnly: boolean

FileSystem.readOnly


readonly root: string

POSIX-normalized root (see posixPath.normalizeRoot).

FileSystem.root

appendFile(inputPath, content): Promise<void>

string

FileContent

Promise<void>

FileSystem.appendFile


protected assertWritable(operation): void

string

void


close(): Promise<void>

Release connections or other resources.

Promise<void>

FileSystem.close


copyFile(sourcePath, destinationPath, options?): Promise<void>

string

string

CopyOptions = {}

Promise<void>

FileSystem.copyFile


abstract protected createDirectoryAt(resolvedPath): Promise<void>

string

Promise<void>


protected createRootDirectory(): Promise<void>

Ensure root exists when createRoot is true.

Default is a no-op. Local adapters typically mkdir -p; remote adapters leave the default when the root is provisioned out of band.

Promise<void>


deleteFile(inputPath, options?): Promise<void>

string

RemoveOptions = {}

Promise<void>

FileSystem.deleteFile


abstract protected deleteFileAt(resolvedPath): Promise<void>

string

Promise<void>


protected ensureParentDirectory(inputPath): Promise<void>

Create parent directories for inputPath when it is nested.

string

Promise<void>


exists(inputPath): Promise<boolean>

string

Promise<boolean>

FileSystem.exists


init(): Promise<void>

Prepare, connect to, or validate the filesystem.

Promise<void>

FileSystem.init


protected isNotFoundError(err): boolean

Recognize the backend’s not-found error.

Default accepts FileSystemError NOT_FOUND plus common SDK / HTTP “not found” shapes via inferFileSystemErrorCode. Override for backend-specific codes (e.g. Node ENOENT) that do not carry a message.

unknown

boolean


protected joinNamespace(parent, child): string

string

string

string


abstract protected listDirectoryAt(resolvedPath): Promise<FileEntry[]>

Return only the direct children of a directory (name is the basename).

string

Promise<FileEntry[]>


protected mapError(err, filePath): FileSystemError

Normalize a backend failure into a FileSystemError.

Every *At / try* primitive is invoked through guard, so an adapter never writes its own try/catch and cannot forget to normalize. Override only to classify backend-specific codes (e.g. Node errno).

unknown

string

FileSystemError


mkdir(inputPath, options?): Promise<void>

string

MakeDirectoryOptions = {}

Promise<void>

FileSystem.mkdir


moveFile(sourcePath, destinationPath, options?): Promise<void>

string

string

CopyOptions = {}

Promise<void>

FileSystem.moveFile


protected normalizePath(inputPath): string

Normalize an input path into an absolute POSIX path inside the virtual filesystem namespace (/a/b). Backslashes are converted; .. escaping the root throws FileSystemError PERMISSION_DENIED.

string

string


protected onClose(): Promise<void>

Override when the backend owns connections or other resources.

Promise<void>


protected onInit(): Promise<void>

Override when the backend requires connection or validation work.

Promise<void>


protected preparePath(resolvedPath, _options?): Promise<string>

Hook after lexical resolution. Override for realpath containment or similar backend-specific checks. Default is a no-op.

string

boolean

Promise<string>


abstract protected readBytesAt(resolvedPath): Promise<Uint8Array<ArrayBufferLike>>

string

Promise<Uint8Array<ArrayBufferLike>>


readdir(inputPath, options?): Promise<FileEntry[]>

string

ListOptions = {}

Promise<FileEntry[]>

FileSystem.readdir


readFile(inputPath): Promise<Uint8Array<ArrayBufferLike>>

Read a file as binary data.

string

Promise<Uint8Array<ArrayBufferLike>>

FileSystem.readFile

readFile(inputPath, options): Promise<string>

Read and decode a file as text.

string

ReadFileOptions & object

Promise<string>

FileSystem.readFile


abstract protected removeDirectoryAt(resolvedPath): Promise<void>

Remove an empty directory.

Recursive deletion is implemented by BaseFileSystem.

string

Promise<void>


protected resolveBackendPath(namespacePath): string

Convert a normalized namespace path (/a/b) into a backend path.

Joins root with the namespace using POSIX /, then applies toBackendPath. Override toBackendPath instead of this method unless the join itself must change.

string

string


protected resolveFor(inputPath, options?): Promise<string>

Resolve inputPath, ensure init, and run preparePath.

string

boolean

Promise<string>


resolvePath(inputPath): string

Resolve a filesystem-relative path into the path understood by the underlying backend.

string

string

FileSystem.resolvePath


rmdir(inputPath, options?): Promise<void>

string

RemoveOptions = {}

Promise<void>

FileSystem.rmdir


stat(inputPath): Promise<FileStat>

string

Promise<FileStat>

FileSystem.stat


abstract protected statAt(resolvedPath): Promise<Omit<FileStat, "path">>

string

Promise<Omit<FileStat, "path">>


protected toBackendPath(posixBackendPath): string

Convert a POSIX backend path (under root) into the form the underlying API expects.

Default is identity. Local disk overrides with posixPath.toHost. Databricks / object-store adapters usually leave the default.

string

string


protected toBytes(content): Uint8Array

FileContent

Uint8Array


protected toRelativePath(namespacePath): string

Namespace path without a leading slash (. for the root).

string

string


protected tryAppendFileAt(_resolvedPath, _content): Promise<boolean>

Override when the backend supports native append.

Parent directories are already created by appendFile. Return true when the operation was performed. The default causes BaseFileSystem to use read-concatenate-write.

string

Uint8Array

Promise<boolean>


protected tryCopyFileAt(_sourcePath, _destinationPath, _options): Promise<boolean>

Override when the backend supports native server-side copying.

Parent directories of the destination are already created by copyFile.

string

string

Required<CopyOptions>

Promise<boolean>


protected tryMoveFileAt(_sourcePath, _destinationPath, _options): Promise<boolean>

Override for native rename or move support.

Parent directories of the destination are already created by moveFile.

string

string

Required<CopyOptions>

Promise<boolean>


abstract protected writeBytesAt(resolvedPath, content, options): Promise<void>

string

Uint8Array

Required<WriteFileOptions>

Promise<void>


writeFile(inputPath, content, options?): Promise<void>

string

FileContent

WriteFileOptions = {}

Promise<void>

FileSystem.writeFile