Skip to content

Class: MemoryFileSystem

FileSystem that stores files and directories in process memory.

const fs = new MemoryFileSystem();
await fs.writeFile("note.txt", "hi");
const text = await fs.readFile("note.txt", { encoding: "utf8" });

new MemoryFileSystem(options?): MemoryFileSystem

MemoryFileSystemOptions = {}

MemoryFileSystem

BaseFileSystem.constructor

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>

BaseFileSystem._init


readonly backend: "memory"

Identifier for the underlying implementation.

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

BaseFileSystem.backend


protected readonly createRoot: boolean

BaseFileSystem.createRoot


readonly id: string

Unique identifier for this filesystem instance.

BaseFileSystem.id


readonly readOnly: boolean

BaseFileSystem.readOnly


readonly root: string

POSIX-normalized root (see posixPath.normalizeRoot).

BaseFileSystem.root

appendFile(inputPath, content): Promise<void>

string

FileContent

Promise<void>

BaseFileSystem.appendFile


protected assertWritable(operation): void

string

void

BaseFileSystem.assertWritable


clear(): void

Drop every file and directory except the root.

void


close(): Promise<void>

Release connections or other resources.

Promise<void>

BaseFileSystem.close


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

string

string

CopyOptions = {}

Promise<void>

BaseFileSystem.copyFile


protected createDirectoryAt(resolvedPath): Promise<void>

string

Promise<void>

BaseFileSystem.createDirectoryAt


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>

BaseFileSystem.createRootDirectory


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

string

RemoveOptions = {}

Promise<void>

BaseFileSystem.deleteFile


protected deleteFileAt(resolvedPath): Promise<void>

string

Promise<void>

BaseFileSystem.deleteFileAt


protected ensureParentDirectory(inputPath): Promise<void>

Create parent directories for inputPath when it is nested.

string

Promise<void>

BaseFileSystem.ensureParentDirectory


exists(inputPath): Promise<boolean>

string

Promise<boolean>

BaseFileSystem.exists


init(): Promise<void>

Prepare, connect to, or validate the filesystem.

Promise<void>

BaseFileSystem.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

BaseFileSystem.isNotFoundError


protected joinNamespace(parent, child): string

string

string

string

BaseFileSystem.joinNamespace


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

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

string

Promise<FileEntry[]>

BaseFileSystem.listDirectoryAt


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

BaseFileSystem.mapError


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

string

MakeDirectoryOptions = {}

Promise<void>

BaseFileSystem.mkdir


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

string

string

CopyOptions = {}

Promise<void>

BaseFileSystem.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

BaseFileSystem.normalizePath


protected onClose(): Promise<void>

Override when the backend owns connections or other resources.

Promise<void>

BaseFileSystem.onClose


protected onInit(): Promise<void>

Override when the backend requires connection or validation work.

Promise<void>

BaseFileSystem.onInit


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>

BaseFileSystem.preparePath


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

string

Promise<Uint8Array<ArrayBufferLike>>

BaseFileSystem.readBytesAt


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

string

ListOptions = {}

Promise<FileEntry[]>

BaseFileSystem.readdir


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

Read a file as binary data.

string

Promise<Uint8Array<ArrayBufferLike>>

BaseFileSystem.readFile

readFile(inputPath, options): Promise<string>

Read and decode a file as text.

string

ReadFileOptions & object

Promise<string>

BaseFileSystem.readFile


protected removeDirectoryAt(resolvedPath): Promise<void>

Remove an empty directory.

Recursive deletion is implemented by BaseFileSystem.

string

Promise<void>

BaseFileSystem.removeDirectoryAt


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

BaseFileSystem.resolveBackendPath


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

Resolve inputPath, ensure init, and run preparePath.

string

boolean

Promise<string>

BaseFileSystem.resolveFor


resolvePath(inputPath): string

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

string

string

BaseFileSystem.resolvePath


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

string

RemoveOptions = {}

Promise<void>

BaseFileSystem.rmdir


stat(inputPath): Promise<FileStat>

string

Promise<FileStat>

BaseFileSystem.stat


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

string

Promise<Omit<FileStat, "path">>

BaseFileSystem.statAt


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

BaseFileSystem.toBackendPath


protected toBytes(content): Uint8Array

FileContent

Uint8Array

BaseFileSystem.toBytes


protected toRelativePath(namespacePath): string

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

string

string

BaseFileSystem.toRelativePath


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>

BaseFileSystem.tryAppendFileAt


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>

BaseFileSystem.tryCopyFileAt


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>

BaseFileSystem.tryMoveFileAt


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

string

Uint8Array

Required<WriteFileOptions>

Promise<void>

BaseFileSystem.writeBytesAt


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

string

FileContent

WriteFileOptions = {}

Promise<void>

BaseFileSystem.writeFile