Skip to content
velt/fs exports

velt/fs

API reference

File system access, with Node's names, behavior and error messages. Every operation has an async form, which runs on the runtime's blocking pool so other tasks keep running, and a *Sync form, which blocks the calling thread. For a walkthrough, read the guide.

import { appendFile, MkdirOptions } from "velt/fs";

Exports

appendFileAppends, creating the file if needed.appendFileSyncappendFile, blocking the calling thread.readFileReads a UTF-8 text file (EILSEQ if it is not valid UTF-8).readFileBytesReads a file's bytes.readFileBytesSyncreadFileBytes, blocking the calling thread.readFileSyncreadFile, blocking the calling thread.writeFileCreates or truncates the file.writeFileSyncwriteFile, blocking the calling thread.mkdirCreates the directory path (and with recursive its missing parents).mkdirSyncmkdir, blocking the calling thread.readDirEntry names (not paths), sorted.readDirEntriesEntries with their types, sorted by name: Node's readdir(path, { withFileTypes: true }).readDirEntriesSyncreadDirEntries, blocking the calling thread.readDirSyncreadDir, blocking the calling thread.copyFileCopies the file from to to, replacing a file at to.copyFileSynccopyFile, blocking the calling thread.existsNever throws: any failure to reach path reads as false.existsSyncexists, blocking the calling thread.removeRemoves a file, symlink or empty directory (with recursive, a directory and its contents).removeSyncremove, blocking the calling thread.renameRenames (moves) from to to, replacing a file at to.renameSyncrename, blocking the calling thread.lstatMetadata of path itself: a symlink is described, not followed.lstatSynclstat, blocking the calling thread.readlinkThe target of the symlink path, as stored in the link (EINVAL if it is not a link).readlinkSyncreadlink, blocking the calling thread.statMetadata of path, following symlinks.statSyncstat, blocking the calling thread.symlinkCreates a symlink at path pointing to target (relative to the link's directory).symlinkSyncsymlink, blocking the calling thread.openReadOpens path for chunked reading.openWriteCreates (or truncates, or with append extends) path for buffered writing.closeSyncCloses the file descriptor fd (Node's fs.closeSync).openSyncOpens path and returns its file descriptor (Node's fs.openSync).unlinkSyncRemoves the file path (Node's fs.unlinkSync; not a directory: EPERM or EISDIR).readSyncReads up to length bytes into buffer from byte offset on (Node's fs.readSync): at position in the file, or at the file's own position (which then advances) when position is left out, null or -1.writeSyncWrites length bytes of buffer from byte offset on (Node's fs.writeSync): at position in the file, or at the file's own position when position is left out, null or not a valid byte offset (at its end for a file opened to append).fdatasyncSyncFlushes the file's data to the disk (Node's fs.fdatasyncSync).fstatSyncThe metadata of the open file fd (Node's fs.fstatSync): its size, modification time and type, from the system's fstat.fsyncSyncFlushes the file's data and metadata to the disk (Node's fs.fsyncSync).ftruncateSyncSets the file's size to len bytes, cutting or extending it with zeros (Node's fs.ftruncateSync).MkdirOptionsThe options of mkdir.RemoveOptionsThe options of remove.DirentA directory entry from readDirEntries, as Node's fs.Dirent.StatsFile metadata, as Node's fs.Stats: from stat (which follows symlinks) or lstat (which describes a symlink itself).FileReaderA file open for reading.FileWriterA file open for writing, buffered (64 KiB).OpenWriteOptionsThe options of openWrite.
functionasync

appendFile

#std/fs/files.vlt:33

Re-exported from velt/fs/files

async function appendFile(path: string, data: string): Promise<void>

Appends, creating the file if needed.

function

appendFileSync

#std/fs/files.vlt:53

Re-exported from velt/fs/files

function appendFileSync(path: string, data: string)

appendFile, blocking the calling thread.

functionasync

readFile

#std/fs/files.vlt:18

Re-exported from velt/fs/files

async function readFile(path: string): Promise<string>

Reads a UTF-8 text file (EILSEQ if it is not valid UTF-8).

functionasync

readFileBytes

#std/fs/files.vlt:23

Re-exported from velt/fs/files

async function readFileBytes(path: string): Promise<u8[]>

Reads a file's bytes.

function

readFileBytesSync

#std/fs/files.vlt:43

Re-exported from velt/fs/files

function readFileBytesSync(path: string): u8[]

readFileBytes, blocking the calling thread.

function

readFileSync

#std/fs/files.vlt:38

Re-exported from velt/fs/files

function readFileSync(path: string): string

readFile, blocking the calling thread.

functionasync

writeFile

#std/fs/files.vlt:28

Re-exported from velt/fs/files

async function writeFile(path: string, data: string): Promise<void>

Creates or truncates the file.

function

writeFileSync

#std/fs/files.vlt:48

Re-exported from velt/fs/files

function writeFileSync(path: string, data: string)

writeFile, blocking the calling thread.

functionasync

mkdir

#std/fs/dirs.vlt:37

Re-exported from velt/fs/dirs

async function mkdir(path: string, opts: MkdirOptions = {}): Promise<void>

Creates the directory path (and with recursive its missing parents).

function

mkdirSync

#std/fs/dirs.vlt:52

Re-exported from velt/fs/dirs

function mkdirSync(path: string, opts: MkdirOptions = {})

mkdir, blocking the calling thread.

functionasync

readDir

#std/fs/dirs.vlt:24

Re-exported from velt/fs/dirs

async function readDir(path: string): Promise<string[]>

Entry names (not paths), sorted.

functionasync

readDirEntries

#std/fs/dirs.vlt:32

Re-exported from velt/fs/dirs

async function readDirEntries(path: string): Promise<Dirent[]>

Entries with their types, sorted by name: Node's readdir(path, { withFileTypes: true }). Faster than readDir plus a stat per entry, since the listing carries the types.

function

readDirEntriesSync

#std/fs/dirs.vlt:47

Re-exported from velt/fs/dirs

function readDirEntriesSync(path: string): Dirent[]

readDirEntries, blocking the calling thread.

function

readDirSync

#std/fs/dirs.vlt:42

Re-exported from velt/fs/dirs

function readDirSync(path: string): string[]

readDir, blocking the calling thread.

functionasync

copyFile

#std/fs/entries.vlt:33

Re-exported from velt/fs/entries

async function copyFile(from: string, to: string): Promise<void>

Copies the file from to to, replacing a file at to.

function

copyFileSync

#std/fs/entries.vlt:53

Re-exported from velt/fs/entries

function copyFileSync(from: string, to: string)

copyFile, blocking the calling thread.

functionasync

exists

#std/fs/entries.vlt:38

Re-exported from velt/fs/entries

async function exists(path: string): Promise<bool>

Never throws: any failure to reach path reads as false.

function

existsSync

#std/fs/entries.vlt:58

Re-exported from velt/fs/entries

function existsSync(path: string): bool

exists, blocking the calling thread.

functionasync

remove

#std/fs/entries.vlt:23

Re-exported from velt/fs/entries

async function remove(path: string, opts: RemoveOptions = {}): Promise<void>

Removes a file, symlink or empty directory (with recursive, a directory and its contents).

function

removeSync

#std/fs/entries.vlt:43

Re-exported from velt/fs/entries

function removeSync(path: string, opts: RemoveOptions = {})

remove, blocking the calling thread.

functionasync

rename

#std/fs/entries.vlt:28

Re-exported from velt/fs/entries

async function rename(from: string, to: string): Promise<void>

Renames (moves) from to to, replacing a file at to.

function

renameSync

#std/fs/entries.vlt:48

Re-exported from velt/fs/entries

function renameSync(from: string, to: string)

rename, blocking the calling thread.

functionasync

lstat

#std/fs/metadata.vlt:24

Re-exported from velt/fs/metadata

async function lstat(path: string): Promise<Stats>

Metadata of path itself: a symlink is described, not followed.

function

lstatSync

#std/fs/metadata.vlt:48

Re-exported from velt/fs/metadata

function lstatSync(path: string): Stats

lstat, blocking the calling thread.

function

readlinkSync

#std/fs/metadata.vlt:53

Re-exported from velt/fs/metadata

function readlinkSync(path: string): string

readlink, blocking the calling thread.

functionasync

stat

#std/fs/metadata.vlt:19

Re-exported from velt/fs/metadata

async function stat(path: string): Promise<Stats>

Metadata of path, following symlinks.

function

statSync

#std/fs/metadata.vlt:43

Re-exported from velt/fs/metadata

function statSync(path: string): Stats

stat, blocking the calling thread.

function

symlinkSync

#std/fs/metadata.vlt:58

Re-exported from velt/fs/metadata

function symlinkSync(target: string, path: string)

symlink, blocking the calling thread.

functionasync

openRead

#std/fs/stream.vlt:24

Re-exported from velt/fs/stream

async function openRead(path: string): Promise<FileReader>

Opens path for chunked reading.

functionasync

openWrite

#std/fs/stream.vlt:29

Re-exported from velt/fs/stream

async function openWrite(
  path: string,
  opts: OpenWriteOptions = {},
): Promise<FileWriter>

Creates (or truncates, or with append extends) path for buffered writing.

function

closeSync

#std/fs/fd/open.vlt:74

Re-exported from velt/fs/fd/open

function closeSync(fd: number)

Closes the file descriptor fd (Node's fs.closeSync).

function

openSync

#std/fs/fd/open.vlt:44

Re-exported from velt/fs/fd/open

function openSync(
  path: string,
  flags: string | number = "r",
  mode: number = 0o666,
): number

Opens path and returns its file descriptor (Node's fs.openSync). flags is one of Node's strings ("r", "r+", "w", "wx", "w+", "a", "ax", "a+", ... and the s forms) or the system's O_* bits as a number; mode is the permission of a file it creates (before the umask).

function

unlinkSync

#std/fs/fd/open.vlt:64

Re-exported from velt/fs/fd/open

function unlinkSync(path: string)

Removes the file path (Node's fs.unlinkSync; not a directory: EPERM or EISDIR).

function

readSync

#std/fs/fd/read.vlt:18

Re-exported from velt/fs/fd/read

function readSync(
  fd: number,
  buffer: u8[] | ArrayBufferView,
  offset: number | null = 0,
  length?: number | null,
  position?: number | null,
): number

Reads up to length bytes into buffer from byte offset on (Node's fs.readSync): at position in the file, or at the file's own position (which then advances) when position is left out, null or -1. Returns the number of bytes read, 0 at the end of the file. offset defaults to 0 and length to the rest of the buffer; a number is coerced as Node does (length truncated to a 32-bit integer, position validated strictly). buffer is a u8[] (Node's Buffer), a typed array or a DataView; offset and length count bytes of it. Velt's typed API takes positional arguments, not Node's (fd, buffer, options) object.

function

writeSync

#std/fs/fd/write.vlt:18

Re-exported from velt/fs/fd/write

function writeSync(
  fd: number,
  buffer: u8[] | ArrayBufferView | string,
  offset: number | null = null,
  length?: number | null,
  position?: number | null,
): number

Writes length bytes of buffer from byte offset on (Node's fs.writeSync): at position in the file, or at the file's own position when position is left out, null or not a valid byte offset (at its end for a file opened to append). Returns the number of bytes written. offset defaults to 0 and length to the rest of the buffer. buffer is a u8[] (Node's Buffer), a typed array or a DataView; a string is written as UTF-8, and its third argument is then the position.

function

fdatasyncSync

#std/fs/fd/sync.vlt:35

Re-exported from velt/fs/fd/sync

function fdatasyncSync(fd: number)

Flushes the file's data to the disk (Node's fs.fdatasyncSync).

function

fstatSync

#std/fs/fd/sync.vlt:55

Re-exported from velt/fs/fd/sync

function fstatSync(fd: number): Stats

The metadata of the open file fd (Node's fs.fstatSync): its size, modification time and type, from the system's fstat.

function

fsyncSync

#std/fs/fd/sync.vlt:27

Re-exported from velt/fs/fd/sync

function fsyncSync(fd: number)

Flushes the file's data and metadata to the disk (Node's fs.fsyncSync).

function

ftruncateSync

#std/fs/fd/sync.vlt:43

Re-exported from velt/fs/fd/sync

function ftruncateSync(fd: number, len: number = 0)

Sets the file's size to len bytes, cutting or extending it with zeros (Node's fs.ftruncateSync).

struct

MkdirOptions

#std/fs/dirs.vlt:18

Re-exported from velt/fs/dirs

The options of mkdir.

FieldTypeDescription
recursive? boolCreate missing parents; an existing directory is then not an error.
struct

RemoveOptions

#std/fs/entries.vlt:17

Re-exported from velt/fs/entries

The options of remove.

FieldTypeDescription
recursive? boolRemove a directory and everything in it (otherwise only files, symlinks, empty dirs).

Re-exported from velt/fs/dirent

A directory entry from readDirEntries, as Node's fs.Dirent. Its type comes with the listing (no stat per entry) and describes the entry itself: a symlink is isSymbolicLink(), whatever it points to.

FieldTypeDescription
name stringThe entry's name (not a path).
isFile(): bool

True for a regular file.

isDirectory(): bool

True for a directory.

Re-exported from velt/fs/stats

File metadata, as Node's fs.Stats: from stat (which follows symlinks) or lstat (which describes a symlink itself). The field order is the runtime's VeltStat layout { u64 size; f64 mtime_ms; u8 is_file; u8 is_dir; u8 is_symlink }, so results are used without conversion.

FieldTypeDescription
size u64Size in bytes (of a symlink from lstat: the length of its target path).
mtimeMs f64Last modification, in milliseconds since the Unix epoch (0 if unavailable).
isFile(): bool

True for a regular file.

isDirectory(): bool

True for a directory.

Re-exported from velt/fs/file_reader

A file open for reading.

async read(max: u64 = 0): Promise<u8[]>

The next 1..max bytes; empty at end of file (max 0 = up to 64 KiB).

async readString(max: u64 = 0): Promise<string>

The next chunk as UTF-8 text ("" at end of file); a character split between chunks is completed by the next call.

async readLine(): Promise<string | null>

The next line without "\n" / "\r\n", or null at end of file. Don't mix with readString.

async * lines(): AsyncGenerator<string, IoError>

The remaining lines, as readLine reads them: for await (const line of r.lines()). Leaving the loop early keeps the reader open at the next line.

close()

Closes the file (reads in flight finish first).

Re-exported from velt/fs/file_writer

A file open for writing, buffered (64 KiB).

async write(data: string): Promise<void>

Writes data as UTF-8 (buffered).

async writeBytes(data: u8[]): Promise<void>

Writes data (buffered).

async flush(): Promise<void>

Pushes buffered data to the operating system.

async close(): Promise<void>

Flushes and closes the file, then releases the handle; throws if the final flush fails.

struct

OpenWriteOptions

#std/fs/stream.vlt:18

Re-exported from velt/fs/stream

The options of openWrite.

FieldTypeDescription
append? boolAppend to the file instead of truncating it.