Skip to content
VaelJS Blog

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"
Since v0.0.0-alpha.0 vjs (V8) vjs-lite (QuickJS)
Cargo feature: Always enabled in vaeljs-core; vael-fs (default) in vaeljs-lite-core
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 });
API Symbols
Functions (14)
functionSince v0.0.0-alpha.0vjs & vjs-lite
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.

Parameters
NameTypeDescription
pathstringFilesystem path to read.
options optionalReadOptionsOptional read bounds ({ maxBytes }).
Returns
Uint8Array — File contents as a Uint8Array.
Exceptions
  • TypeError — on wrong argument types.
  • Error — with code: FsErrorCode on filesystem or validation failure.
import { readFile } from "vael:fs";
const bytes = readFile("README.md", { maxBytes: 1024 * 1024 });
console.log(bytes.byteLength);
functionSince v0.0.0-alpha.0vjs & vjs-lite
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).

Parameters
NameTypeDescription
pathstringFilesystem path to read.
options optionalReadOptionsOptional read bounds ({ maxBytes }).
Returns
string — Decoded strict UTF-8 string.
Exceptions
  • TypeError — on wrong argument types.
  • Error — with code: FsErrorCode on filesystem, UTF-8, or validation failure.
import { readTextFile } from "vael:fs";
const content = readTextFile("README.md", { maxBytes: 65_536 });
console.log(content.length);
functionSince v0.0.0-alpha.0vjs & vjs-lite
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.

Parameters
NameTypeDescription
pathstringTarget filesystem path.
bytesUint8ArrayBytes to write.
options optionalWriteOptionsOptional { create, createNew, append } flags.
Returns
void
Exceptions
  • TypeError — on wrong argument types.
  • Error — with code: FsErrorCode (VAEL_FS_INVALID_ARGUMENT if both createNew and append are true).
import { writeFile } from "vael:fs";
writeFile("/tmp/data.bin", new Uint8Array([1, 2, 3, 4]));
functionSince v0.0.0-alpha.0vjs & vjs-lite
export function writeTextFile(path: string, text: string, options?: WriteOptions): void;

Synchronously write UTF-8 text to path (non-atomic; creates and truncates by default).

Parameters
NameTypeDescription
pathstringTarget filesystem path.
textstringUTF-8 string to write.
options optionalWriteOptionsOptional { create, createNew, append } flags.
Returns
void
Exceptions
  • TypeError — on wrong argument types.
  • Error — with code: FsErrorCode.
import { writeTextFile } from "vael:fs";
writeTextFile("/tmp/greeting.txt", "hello\n");
functionSince v0.0.0-alpha.0vjs & vjs-lite
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.

Parameters
NameTypeDescription
pathstringTarget filesystem path.
bytesUint8ArrayBytes to write atomically.
options optionalAtomicWriteOptionsOptional { sync, overwrite } flags.
Returns
void
Exceptions
  • TypeError — on wrong argument types.
  • Error — with code: FsErrorCode.
import { writeFileAtomic } from "vael:fs";
writeFileAtomic("/tmp/state.bin", new Uint8Array([7, 8, 9]), { sync: true });
functionSince v0.0.0-alpha.0vjs & vjs-lite
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.

Parameters
NameTypeDescription
pathstringFilesystem path to probe.
Returns
boolean — true if path resolves to an existing entry.
Exceptions
  • TypeError — on wrong argument types.
  • Error — with code: FsErrorCode.
import { exists } from "vael:fs";
console.log(exists("README.md"));
functionSince v0.0.0-alpha.0vjs & vjs-lite
export function stat(path: string): FileInfo;

Synchronously return metadata for path, following symbolic links.

Parameters
NameTypeDescription
pathstringFilesystem path to inspect.
Returns
FileInfo — Snapshot FileInfo metadata.
Exceptions
  • TypeError — on wrong argument types.
  • Error — with code: FsErrorCode (VAEL_FS_NOT_FOUND if missing or dangling symlink).
functionSince v0.0.0-alpha.0vjs & vjs-lite
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.

Parameters
NameTypeDescription
pathstringFilesystem path to inspect.
Returns
FileInfo — Snapshot FileInfo metadata for the entry itself.
Exceptions
  • TypeError — on wrong argument types.
  • Error — with code: FsErrorCode.
functionSince v0.0.0-alpha.0vjs & vjs-lite
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.

Parameters
NameTypeDescription
pathstringDirectory path to list.
Returns
DirEntry[] — Sorted array of DirEntry records.
Exceptions
  • TypeError — on wrong argument types.
  • Error — with code: FsErrorCode.
import { readDir } from "vael:fs";
for (const entry of readDir(".")) {
console.log(entry.name, entry.isDirectory);
}
functionSince v0.0.0-alpha.0vjs & vjs-lite
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.

Parameters
NameTypeDescription
pathstringDirectory path to create.
options optionalMkdirOptionsOptional { recursive } flag.
Returns
void
Exceptions
  • TypeError — on wrong argument types.
  • Error — with code: FsErrorCode.
functionSince v0.0.0-alpha.0vjs & vjs-lite
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.

Parameters
NameTypeDescription
pathstringFilesystem entry path to remove.
options optionalRemoveOptionsOptional { recursive } flag.
Returns
void
Exceptions
  • TypeError — on wrong argument types.
  • Error — with code: FsErrorCode.
functionSince v0.0.0-alpha.0vjs & vjs-lite
export function rename(from: string, to: string): void;

Synchronously rename or move a file or directory from from to to.

Parameters
NameTypeDescription
fromstringSource path.
tostringDestination path.
Returns
void
Exceptions
  • TypeError — on wrong argument types.
  • Error — with code: FsErrorCode.
functionSince v0.0.0-alpha.0vjs & vjs-lite
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.

Parameters
NameTypeDescription
fromstringSource regular file path.
tostringDestination file path.
options optionalCopyOptionsOptional { overwrite } flag.
Returns
void
Exceptions
  • TypeError — on wrong argument types.
  • Error — with code: FsErrorCode (VAEL_FS_IS_A_DIRECTORY if from is a directory).
functionSince v0.0.0-alpha.0vjs & vjs-lite
export function realpath(path: string): string;

Synchronously resolve the canonical, absolute filesystem path for path, following all symbolic links and normalizing . and .. components.

Parameters
NameTypeDescription
pathstringFilesystem path to canonicalize.
Returns
string — Canonical absolute path string.
Exceptions
  • TypeError — on wrong argument types.
  • Error — with code: FsErrorCode.
interfaceSince v0.0.0-alpha.0vjs & vjs-lite
export interface ReadOptions {
  maxBytes?: number;
}

Options controlling readFile and readTextFile.

Properties
PropertyTypeDescriptionSince
maxBytes optionalnumberMaximum 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
interfaceSince v0.0.0-alpha.0vjs & vjs-lite
export interface WriteOptions {
  create?: boolean;
  createNew?: boolean;
  append?: boolean;
}

Options controlling writeFile and writeTextFile.

Properties
PropertyTypeDescriptionSince
create optionalbooleanCreate the file if it does not already exist (default: true).v0.0.0-alpha.0
createNew optionalbooleanRequire exclusive creation and fail with VAEL_FS_ALREADY_EXISTS if the file already exists (default: false).v0.0.0-alpha.0
append optionalbooleanAppend 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
interfaceSince v0.0.0-alpha.0vjs & vjs-lite
export interface AtomicWriteOptions {
  sync?: boolean;
  overwrite?: boolean;
}

Options controlling writeFileAtomic.

Properties
PropertyTypeDescriptionSince
sync optionalbooleanCall 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 optionalbooleanReplace 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
interfaceSince v0.0.0-alpha.0vjs & vjs-lite
export interface MkdirOptions {
  recursive?: boolean;
}

Options controlling mkdir.

Properties
PropertyTypeDescriptionSince
recursive optionalbooleanCreate missing parent directories and succeed if the directory already exists (default: false).v0.0.0-alpha.0
interfaceSince v0.0.0-alpha.0vjs & vjs-lite
export interface RemoveOptions {
  recursive?: boolean;
}

Options controlling remove.

Properties
PropertyTypeDescriptionSince
recursive optionalbooleanRecursively remove directory contents without following symbolic links (default: false).v0.0.0-alpha.0
interfaceSince v0.0.0-alpha.0vjs & vjs-lite
export interface CopyOptions {
  overwrite?: boolean;
}

Options controlling copyFile.

Properties
PropertyTypeDescriptionSince
overwrite optionalbooleanOverwrite 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
interfaceSince v0.0.0-alpha.0vjs & vjs-lite
export interface FileInfo {
  isFile: boolean;
  isDirectory: boolean;
  isSymlink: boolean;
  size: number;
  modifiedMs: number | null;
  readonly: boolean;
}

Snapshot metadata returned by stat and lstat.

Properties
PropertyTypeDescriptionSince
isFile booleanWhether the entry is a regular file.v0.0.0-alpha.0
isDirectory booleanWhether the entry is a directory.v0.0.0-alpha.0
isSymlink booleanWhether the entry is a symbolic link (always false for stat).v0.0.0-alpha.0
size numberSize of the entry in bytes.v0.0.0-alpha.0
modifiedMs number | nullLast 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 booleanWhether the entry's permissions mark it as read-only.v0.0.0-alpha.0
interfaceSince v0.0.0-alpha.0vjs & vjs-lite
export interface DirEntry {
  name: string;
  isFile: boolean;
  isDirectory: boolean;
  isSymlink: boolean;
}

Snapshot directory entry returned in sorted order by readDir.

Properties
PropertyTypeDescriptionSince
name stringSingle path component name (valid UTF-8).v0.0.0-alpha.0
isFile booleanWhether the entry is a regular file (does not follow symlinks).v0.0.0-alpha.0
isDirectory booleanWhether the entry is a directory (does not follow symlinks).v0.0.0-alpha.0
isSymlink booleanWhether the entry is a symbolic link.v0.0.0-alpha.0
typeSince v0.0.0-alpha.0vjs & vjs-lite
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.