zena:fs
import {…} from 'zena:fs';
Filesystem Standard Library - WASI Preview 1 Implementation
Provides file and directory operations using wasi_snapshot_preview1 interfaces. See docs/design/filesystem.md for the full design.
WASI Capability-Based Security ​
WASI uses a capability-based security model for filesystem access. Unlike traditional operating systems where processes can access any file path they have permission to, WASI programs have NO filesystem access by default.
Access is granted through "preopened directories" - directories that the host environment explicitly passes to the WASM module at startup. The module can only access files within these preopened directories.
For example, when running with wasmtime:
wasmtime run --dir /data::/data --dir /tmp::/tmp myprogram.wasm
This grants access to /data and /tmp on the host, mapped to the same
paths in the guest. Without these flags, the program cannot access the
filesystem at all.
The high-level functions (readFile, writeFile, etc.) use the first
preopened directory as the root. For more control, use getPreopens() to
access all available directories, or work with Descriptor objects directly.
Classes
FsError
class FsError extends Error
new(errno: i32, message: String)
Descriptor
class Descriptor
A handle for an open file or directory.
Descriptors are obtained either from getPreopens() (for preopened
directories) or by opening files/directories relative to an existing
descriptor using openAt() or createAt().
In WASI's capability model, a Descriptor acts as a capability token - you can only access files that are reachable from a Descriptor you already have.
Always call close() when done to release the underlying resource.
new(fd: i32)
fd: i32 { get; }
readAllBytes(): ByteArray
readString(): String
writeBytes(data: ByteArray): void
writeString(data: String): void
openAt(path: String, flags: DescriptorFlags): Descriptor
createAt(path: String): Descriptor
stat(): FileStat
statAt(path: String): FileStat
readDir(): Array<DirEntry>
mkdirAt(path: String): void
unlinkAt(path: String): void
rmdirAt(path: String): void
close(): void
Enums
FsErrorCode
enum FsErrorCode
FileType
enum FileType
OpenFlags
DescriptorFlags
Type aliases
FileStat
type FileStat = {fileType: FileType, size: i64}
DirEntry
type DirEntry = {name: String, fileType: FileType}
Functions
getPreopens
function getPreopens(): Array<(Descriptor, String)>
Returns all preopened directories granted to this WASM module.
Preopened directories are the ONLY filesystem locations this program can
access. They are granted by the host environment at startup (e.g., via
wasmtime's --dir flag).
Each entry is a tuple of [Descriptor, path] where:
Descriptoris a handle for filesystem operations within that directorypathis the guest path (e.g., "/tmp", "/data", or "." for current dir)
Returns an empty array if no directories were preopened (the program has no filesystem access).
getRootDir
function getRootDir(): Descriptor
Returns the first preopened directory, used as the "root" for high-level file operations.
This is typically the current working directory (".") or the first --dir
flag passed to the WASI runtime.
readFile
function readFile(path: String): String
readFileBytes
function readFileBytes(path: String): ByteArray
Reads the entire contents of a file as raw bytes.
The path is relative to the first preopened directory.
writeFile
function writeFile(path: String, content: String): void
Writes a string to a file as UTF-8, creating or truncating the file.
The path is relative to the first preopened directory.
writeFileBytes
function writeFileBytes(path: String, content: ByteArray): void
Writes raw bytes to a file, creating or truncating the file.
The path is relative to the first preopened directory.
listDir
function listDir(path: String): Array<DirEntry>
Lists the contents of a directory.
The path is relative to the first preopened directory. Use "." to list the root preopened directory itself.
exists
function exists(path: String): boolean
Checks if a path exists (file or directory).
The path is relative to the first preopened directory.
isFile
function isFile(path: String): boolean
Checks if a path is a regular file.
isDirectory
function isDirectory(path: String): boolean
Checks if a path is a directory.
mkdir
function mkdir(path: String): void
Creates a directory.
The path is relative to the first preopened directory. Parent directories must already exist.
unlink
function unlink(path: String): void
Deletes a file.
The path is relative to the first preopened directory.
rmdir
function rmdir(path: String): void
Deletes an empty directory.
The path is relative to the first preopened directory. The directory must be empty.