zena:fs
import {…} from 'zena:fs';
File system operations: reading, writing, querying metadata, and directory traversal.
In WebAssembly environments, filesystem access is restricted to preopened
directories provided by the host runtime. High-level functions such as
readFile, writeFile, and readDir operate relative to the default
preopened directory. getPreopens() and Descriptor allow explicit access
to multiple preopened directories.
Examples ​
import { readFile, writeFile, exists } from 'zena:fs';
writeFile('example.txt', 'Hello, Zena!');
if (exists('example.txt')) {
let content = readFile('example.txt');
}
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
GlobOptions
class GlobOptions
Options for directory globbing.
Enums
FsErrorCode
enum FsErrorCode
FileType
enum FileType
OpenFlags
DescriptorFlags
Type aliases
FileStat
type FileStat = {fileType: FileType, size: i64, modified: i64}
DirEntry
type DirEntry = {name: String, fileType: FileType}
When the contents last changed, in nanoseconds since the Unix epoch.
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.
rename
function rename(from: String, to: String): void
Renames a file or directory. When to is an existing file it is
replaced in one step, so a reader opening to sees either the old file
or the new one and never a partly written file. Writing to a temporary
name and renaming it into place relies on this.
stat
function stat(path: String): FileStat
The type, size and modification time of a file or 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.
glob
function glob(pattern: String, options: GlobOptions | null = null): Array<String>
Searches the filesystem for files and directories matching pattern.