vael:fs
Bounded synchronous filesystem operations and atomic file writes.
Available in both vjs (vaeljs) and vjs-lite (vaeljs-lite) runtimes.
All operations are synchronous, validate NUL-free UTF-8 paths, and report
machine-readable VAEL_FS_* error codes on Error.code.
import ... from "vael:fs"
vaeljs-core; vael-fs (default) in vaeljs-lite-coreExample
Section titled “Example”import { exists, mkdir, readTextFile, remove, writeTextFile } from "vael:fs";
mkdir("/tmp/vael-fs-example", { recursive: true });writeTextFile("/tmp/vael-fs-example/hello.txt", "hello vael\n");if (exists("/tmp/vael-fs-example/hello.txt")) { console.log(readTextFile("/tmp/vael-fs-example/hello.txt"));}remove("/tmp/vael-fs-example", { recursive: true });readFilev0.0.0-alpha.0path into a Uint8Array, following symbolic links.readTextFilev0.0.0-alpha.0path as strict UTF-8 text, following symbolic links.writeFilev0.0.0-alpha.0bytes to path (non-atomic; creates and truncates by default).writeTextFilev0.0.0-alpha.0text to path (non-atomic; creates and truncates by default).writeFileAtomicv0.0.0-alpha.0bytes to path via a unique sibling temporary file (0o600 on Unix) and atomic rename/link commit.existsv0.0.0-alpha.0path resolves to an existing entry, following symbolic links.statv0.0.0-alpha.0path, following symbolic links.lstatv0.0.0-alpha.0path without following symbolic links.readDirv0.0.0-alpha.0path, sorted lexicographically by name.mkdirv0.0.0-alpha.0path.removev0.0.0-alpha.0path without following symbolic links.renamev0.0.0-alpha.0from to to.copyFilev0.0.0-alpha.0from to to.realpathv0.0.0-alpha.0path, following all symbolic links and normalizing . and .. compo...ReadOptionsv0.0.0-alpha.0readFile and readTextFile.WriteOptionsv0.0.0-alpha.0writeFile and writeTextFile.AtomicWriteOptionsv0.0.0-alpha.0writeFileAtomic.MkdirOptionsv0.0.0-alpha.0mkdir.RemoveOptionsv0.0.0-alpha.0remove.CopyOptionsv0.0.0-alpha.0copyFile.FileInfov0.0.0-alpha.0stat and lstat.DirEntryv0.0.0-alpha.0readDir.Functions
Section titled “Functions”readFile
Section titled “readFile”export function readFile(path: string, options?: ReadOptions): Uint8Array;
Synchronously read the entire file at path into a Uint8Array,
following symbolic links.
Rejects empty paths and paths containing NUL (\0) bytes with
VAEL_FS_INVALID_PATH, directories with VAEL_FS_IS_A_DIRECTORY, and files
exceeding options.maxBytes with VAEL_FS_TOO_LARGE.
| Name | Type | Description |
|---|---|---|
path | string | Filesystem path to read. |
options optional | ReadOptions | Optional read bounds ({ maxBytes }). |
Uint8Array — File contents as a Uint8Array.TypeError— on wrong argument types.Error— withcode: FsErrorCodeon filesystem or validation failure.
import { readFile } from "vael:fs";
const bytes = readFile("README.md", { maxBytes: 1024 * 1024 });console.log(bytes.byteLength);readTextFile
Section titled “readTextFile”export function readTextFile(path: string, options?: ReadOptions): string;
Synchronously read the entire file at path as strict UTF-8 text,
following symbolic links.
Fails closed with VAEL_FS_INVALID_UTF8 on non-UTF-8 byte sequences (no
lossy replacement). Also enforces options.maxBytes (VAEL_FS_TOO_LARGE).
| Name | Type | Description |
|---|---|---|
path | string | Filesystem path to read. |
options optional | ReadOptions | Optional read bounds ({ maxBytes }). |
string — Decoded strict UTF-8 string.TypeError— on wrong argument types.Error— withcode: FsErrorCodeon filesystem, UTF-8, or validation failure.
import { readTextFile } from "vael:fs";
const content = readTextFile("README.md", { maxBytes: 65_536 });console.log(content.length);writeFile
Section titled “writeFile”export function writeFile(path: string, bytes: Uint8Array, options?: WriteOptions): void;
Synchronously write bytes to path (non-atomic; creates and truncates by
default).
An I/O failure mid-write can leave a partial file; use writeFileAtomic
when all-or-nothing replacement is required.
| Name | Type | Description |
|---|---|---|
path | string | Target filesystem path. |
bytes | Uint8Array | Bytes to write. |
options optional | WriteOptions | Optional { create, createNew, append } flags. |
voidTypeError— on wrong argument types.Error— withcode: FsErrorCode(VAEL_FS_INVALID_ARGUMENTif bothcreateNewandappendaretrue).
import { writeFile } from "vael:fs";
writeFile("/tmp/data.bin", new Uint8Array([1, 2, 3, 4]));writeTextFile
Section titled “writeTextFile”export function writeTextFile(path: string, text: string, options?: WriteOptions): void;
Synchronously write UTF-8 text to path (non-atomic; creates and
truncates by default).
| Name | Type | Description |
|---|---|---|
path | string | Target filesystem path. |
text | string | UTF-8 string to write. |
options optional | WriteOptions | Optional { create, createNew, append } flags. |
voidTypeError— on wrong argument types.Error— withcode: FsErrorCode.
import { writeTextFile } from "vael:fs";
writeTextFile("/tmp/greeting.txt", "hello\n");writeFileAtomic
Section titled “writeFileAtomic”export function writeFileAtomic(
path: string,
bytes: Uint8Array,
options?: AtomicWriteOptions,
): void;
Synchronously and atomically write bytes to path via a unique sibling
temporary file (0o600 on Unix) and atomic rename/link commit.
Defaults: sync: true, overwrite: true. Rejects trailing separators,
., and trailing /. segments with VAEL_FS_INVALID_PATH. Temporary files
are cleaned up on failure without masking the original error.
| Name | Type | Description |
|---|---|---|
path | string | Target filesystem path. |
bytes | Uint8Array | Bytes to write atomically. |
options optional | AtomicWriteOptions | Optional { sync, overwrite } flags. |
voidTypeError— on wrong argument types.Error— withcode: FsErrorCode.
import { writeFileAtomic } from "vael:fs";
writeFileAtomic("/tmp/state.bin", new Uint8Array([7, 8, 9]), { sync: true });exists
Section titled “exists”export function exists(path: string): boolean;
Synchronously check whether path resolves to an existing entry, following
symbolic links.
Returns false only when the OS reports NotFound; permission errors and
invalid paths throw rather than returning false.
| Name | Type | Description |
|---|---|---|
path | string | Filesystem path to probe. |
boolean — true if path resolves to an existing entry.TypeError— on wrong argument types.Error— withcode: FsErrorCode.
import { exists } from "vael:fs";
console.log(exists("README.md"));export function stat(path: string): FileInfo;
Synchronously return metadata for path, following symbolic links.
| Name | Type | Description |
|---|---|---|
path | string | Filesystem path to inspect. |
FileInfo — Snapshot FileInfo metadata.TypeError— on wrong argument types.Error— withcode: FsErrorCode(VAEL_FS_NOT_FOUNDif missing or dangling symlink).
export function lstat(path: string): FileInfo;
Synchronously return metadata for path without following symbolic links.
Trailing separators on non-root paths are stripped so symlinks to
directories are inspected directly; trailing /. segments fail with
VAEL_FS_INVALID_PATH.
| Name | Type | Description |
|---|---|---|
path | string | Filesystem path to inspect. |
FileInfo — Snapshot FileInfo metadata for the entry itself.TypeError— on wrong argument types.Error— withcode: FsErrorCode.
readDir
Section titled “readDir”export function readDir(path: string): DirEntry[];
Synchronously list direct entries in directory path, sorted
lexicographically by name.
Excludes . and .. and does not follow symbolic links. Fails closed with
VAEL_FS_INVALID_UTF8 if any entry name is not valid UTF-8.
| Name | Type | Description |
|---|---|---|
path | string | Directory path to list. |
DirEntry[] — Sorted array of DirEntry records.TypeError— on wrong argument types.Error— withcode: FsErrorCode.
import { readDir } from "vael:fs";
for (const entry of readDir(".")) { console.log(entry.name, entry.isDirectory);}export function mkdir(path: string, options?: MkdirOptions): void;
Synchronously create a directory at path.
Default recursive: false fails with VAEL_FS_ALREADY_EXISTS if present or
VAEL_FS_NOT_FOUND if parent is missing.
| Name | Type | Description |
|---|---|---|
path | string | Directory path to create. |
options optional | MkdirOptions | Optional { recursive } flag. |
voidTypeError— on wrong argument types.Error— withcode: FsErrorCode.
remove
Section titled “remove”export function remove(path: string, options?: RemoveOptions): void;
Synchronously remove a file, symbolic link, or directory at path without
following symbolic links.
Default recursive: false fails with VAEL_FS_DIRECTORY_NOT_EMPTY on
non-empty directories. Trailing /. segments fail with VAEL_FS_INVALID_PATH.
| Name | Type | Description |
|---|---|---|
path | string | Filesystem entry path to remove. |
options optional | RemoveOptions | Optional { recursive } flag. |
voidTypeError— on wrong argument types.Error— withcode: FsErrorCode.
rename
Section titled “rename”export function rename(from: string, to: string): void;
Synchronously rename or move a file or directory from from to to.
| Name | Type | Description |
|---|---|---|
from | string | Source path. |
to | string | Destination path. |
voidTypeError— on wrong argument types.Error— withcode: FsErrorCode.
copyFile
Section titled “copyFile”export function copyFile(from: string, to: string, options?: CopyOptions): void;
Synchronously copy a regular file from from to to.
Default overwrite: false creates to exclusively (VAEL_FS_ALREADY_EXISTS
if occupied) and cleans up partial targets on error. When overwrite: true
and from/to resolve to the same underlying file, returns without truncating.
| Name | Type | Description |
|---|---|---|
from | string | Source regular file path. |
to | string | Destination file path. |
options optional | CopyOptions | Optional { overwrite } flag. |
voidTypeError— on wrong argument types.Error— withcode: FsErrorCode(VAEL_FS_IS_A_DIRECTORYiffromis a directory).
realpath
Section titled “realpath”export function realpath(path: string): string;
Synchronously resolve the canonical, absolute filesystem path for path,
following all symbolic links and normalizing . and .. components.
| Name | Type | Description |
|---|---|---|
path | string | Filesystem path to canonicalize. |
string — Canonical absolute path string.TypeError— on wrong argument types.Error— withcode: FsErrorCode.
Interfaces
Section titled “Interfaces”ReadOptions
Section titled “ReadOptions”export interface ReadOptions {
maxBytes?: number;
}
Options controlling readFile and readTextFile.
| Property | Type | Description | Since |
|---|---|---|---|
maxBytes optional | number | Maximum number of bytes allowed when reading a file (default: unlimited). Files larger than maxBytes fail with VAEL_FS_TOO_LARGE without unbounded allocation. | v0.0.0-alpha.0 |
WriteOptions
Section titled “WriteOptions”export interface WriteOptions {
create?: boolean;
createNew?: boolean;
append?: boolean;
}
Options controlling writeFile and writeTextFile.
| Property | Type | Description | Since |
|---|---|---|---|
create optional | boolean | Create the file if it does not already exist (default: true). | v0.0.0-alpha.0 |
createNew optional | boolean | Require exclusive creation and fail with VAEL_FS_ALREADY_EXISTS if the file already exists (default: false). | v0.0.0-alpha.0 |
append optional | boolean | Append writes to the end of the file instead of truncating it (default: false). Combining createNew: true and append: true fails with VAEL_FS_INVALID_ARGUMENT. | v0.0.0-alpha.0 |
AtomicWriteOptions
Section titled “AtomicWriteOptions”export interface AtomicWriteOptions {
sync?: boolean;
overwrite?: boolean;
}
Options controlling writeFileAtomic.
| Property | Type | Description | Since |
|---|---|---|---|
sync optional | boolean | Call sync_all on the temporary file (and best-effort on its parent directory on Unix) before replacing the target (default: true). | v0.0.0-alpha.0 |
overwrite optional | boolean | Replace an existing target entry if present (default: true). When false, any existing entry (including a symlink) fails with VAEL_FS_ALREADY_EXISTS without clobbering. | v0.0.0-alpha.0 |
MkdirOptions
Section titled “MkdirOptions”export interface MkdirOptions {
recursive?: boolean;
}
Options controlling mkdir.
| Property | Type | Description | Since |
|---|---|---|---|
recursive optional | boolean | Create missing parent directories and succeed if the directory already exists (default: false). | v0.0.0-alpha.0 |
RemoveOptions
Section titled “RemoveOptions”export interface RemoveOptions {
recursive?: boolean;
}
Options controlling remove.
| Property | Type | Description | Since |
|---|---|---|---|
recursive optional | boolean | Recursively remove directory contents without following symbolic links (default: false). | v0.0.0-alpha.0 |
CopyOptions
Section titled “CopyOptions”export interface CopyOptions {
overwrite?: boolean;
}
Options controlling copyFile.
| Property | Type | Description | Since |
|---|---|---|---|
overwrite optional | boolean | Overwrite the destination file if it already exists (default: false). When false, existing targets fail with VAEL_FS_ALREADY_EXISTS without TOCTOU clobbering. | v0.0.0-alpha.0 |
FileInfo
Section titled “FileInfo”export interface FileInfo {
isFile: boolean;
isDirectory: boolean;
isSymlink: boolean;
size: number;
modifiedMs: number | null;
readonly: boolean;
}
Snapshot metadata returned by stat and lstat.
| Property | Type | Description | Since |
|---|---|---|---|
isFile | boolean | Whether the entry is a regular file. | v0.0.0-alpha.0 |
isDirectory | boolean | Whether the entry is a directory. | v0.0.0-alpha.0 |
isSymlink | boolean | Whether the entry is a symbolic link (always false for stat). | v0.0.0-alpha.0 |
size | number | Size of the entry in bytes. | v0.0.0-alpha.0 |
modifiedMs | number | null | Last modification time in whole milliseconds since the Unix epoch, or null if unavailable from the filesystem or outside signed 64-bit range. | v0.0.0-alpha.0 |
readonly | boolean | Whether the entry's permissions mark it as read-only. | v0.0.0-alpha.0 |
DirEntry
Section titled “DirEntry”export interface DirEntry {
name: string;
isFile: boolean;
isDirectory: boolean;
isSymlink: boolean;
}
Snapshot directory entry returned in sorted order by readDir.
| Property | Type | Description | Since |
|---|---|---|---|
name | string | Single path component name (valid UTF-8). | v0.0.0-alpha.0 |
isFile | boolean | Whether the entry is a regular file (does not follow symlinks). | v0.0.0-alpha.0 |
isDirectory | boolean | Whether the entry is a directory (does not follow symlinks). | v0.0.0-alpha.0 |
isSymlink | boolean | Whether the entry is a symbolic link. | v0.0.0-alpha.0 |
Type Aliases
Section titled “Type Aliases”FsErrorCode
Section titled “FsErrorCode”export type FsErrorCode =
| "VAEL_FS_NOT_FOUND"
| "VAEL_FS_ALREADY_EXISTS"
| "VAEL_FS_PERMISSION_DENIED"
| "VAEL_FS_IS_A_DIRECTORY"
| "VAEL_FS_NOT_A_DIRECTORY"
| "VAEL_FS_DIRECTORY_NOT_EMPTY"
| "VAEL_FS_INVALID_PATH"
| "VAEL_FS_INVALID_ARGUMENT"
| "VAEL_FS_INVALID_UTF8"
| "VAEL_FS_TOO_LARGE"
| "VAEL_FS_IO";
Stable machine-readable VAEL_FS_* error codes exposed on Error.code.
