fs.ts

The fs host module: filesystem access through directory handles.

Code reaches the filesystem only through an FsDir (or the read-only FsReadDir), a handle on one directory that resolves relative paths beneath it, the shape of cap-std and WASI preopens. The import is the capability, and handing a narrower handle (dir.dir('sub'), dir.readonly()) down the call graph is how authority narrows. fs.project is the project directory, fs.open_dir opens any other directory, fs.real_path resolves any absolute path's symlinks, and fs.temp_dir makes a fresh directory that removes itself on await using. Only fs and other handles make handles, and handles and their prototypes are frozen, so a handle's constructor makes nothing and no holder can patch another's ops. They and fs are null-prototype, so awaiting one adopts no polluted Object.prototype.then. Each op reads its handle's state from private fields, never path, so an op called on any other object throws TypeError.

Paths beneath a handle are relative and /-separated, and resolve as openat2 with RESOLVE_BENEATH does. An absolute path, or one whose .. segments climb above the handle, fails with outside_root. So does an absolute symlink, and a relative one whose target climbs above the handle, even when it would land back inside (../root/real). The path's own .. segments resolve lexically, before any symlink, so link/.. is the handle's own directory whatever link points at; the runtime's native op resolves them the same way before calling the kernel. A trailing / means a directory, as in the kernel: stat('a.txt/') and read('a.txt/') fail with not_a_directory, write('new.txt/') fails, and remove and rename refuse a symlink named with a trailing / (not_a_directory), never acting on its target. remove and rename refuse the handle's own directory with io_error. Symlink targets, real paths, and listed names are read as bytes, and one that isn't UTF-8 fails with io_error, since no string names what the kernel would reach. A symlink's target is data: symlink stores any target verbatim and read_link returns it, and following a link is what's confined, by whichever handle reaches it.

Errors are typed: each op throws an FsError (fs_error.ts) whose kind names the failure, and is_fs_error recognizes one from any copy of Gro. Each op's one expected failure returns a value instead of throwing: stat returns null on not_found, exists returns false, read_if_exists and read_text_if_exists return null, remove({missing_ok}) and mkdir({exists_ok}) return normally, and write({create_new}) and symlink return 'exists'. Every other kind still throws, so a permission or I/O error is never mistaken for a missing file.

This is the Node implementation, which runs on the Node Gro. Its gaps against the runtime's builtin, which it documents rather than the API weakening to match:

  • Confinement is a check, then an open: the path is walked beneath the handle's directory one component at a time, reading each symlink, then the op runs on the path the walk found, read_link, set_times, set_mode, and symlink included. A symlink swapped in between escapes, since Node has no openat2. The twin is a review signal, not a boundary.
  • Intrinsics stay pollutable: code that patches String.prototype, Array.prototype, or Promise after this module loads runs inside its ops. The walk refuses a segment holding a /, so a forged segment can't climb out, and the Node functions it calls are copied at load, so syncBuiltinESMExports can't rebind them.
  • A handle opened on /proc follows its magic links, which the kernel resolves without a path the walk can check.
  • fs.open_dir and fs.real_path take any absolute path, checked against no grant.
  • Any module can import node:fs, so a narrowed handle bounds only code that uses the handles.
  • fs.project is the working directory when this module loads, the same source as Gro's paths.root; getcwd returns a path with symlinks resolved. Importing the module throws when the working directory has been removed, or when its path isn't UTF-8, which Node would report lossily as a different path.
view source

Declarations
#

13 declarations

DirEntry
#

fs.ts view source

DirEntry import type {DirEntry} from '@fuzdev/gro/fs.js';

One entry of a directory listing, its kind read without following a symlink.

name

type string

kind

type FileKind

FileInfo
#

fs.ts view source

FileInfo import type {FileInfo} from '@fuzdev/gro/fs.js';

What stat reports about an entry.

kind

type FileKind

size

Size in bytes.

type number

mtime_ms

Last modification time in milliseconds since the epoch.

type number

mode

The permission bits, like 0o644, without the file type bits.

type number

FileKind
#

fs.ts view source

FileKind

type "file" | "directory" | "symlink" | "other"

import type {FileKind} from '@fuzdev/gro/fs.js';

The kind of a filesystem entry. stat with follow (the default) never returns 'symlink'; stat without it and list do.

fs
#

fs.ts view source

Fs import {fs} from '@fuzdev/gro/fs.js';

The fs host module's root handle. Import it where authority enters (a task, a script, an entry module) and hand narrower handles down.

Fs
#

fs.ts view source

Fs import type {Fs} from '@fuzdev/gro/fs.js';

The fs host module's root: the project directory and the ways to reach others.

project

The project directory.

type FsDir

readonly

open_dir

Opens a directory by absolute path.

type (path: string): Promise<FsDir>

path

type string
returns Promise<FsDir>

throws

  • TypeError - for a relative path, or one holding a NUL byte or a lone surrogate
  • FsError - `not_found`, `not_a_directory`; `not_granted` on `fgro` for a path outside the run's roots

real_path

The real path of an absolute path, every symlink followed, with open_dir's reach: on Node any absolute path, checked against no grant; on fgro a path inside the run's roots, its real path one inside them too. Beneath a handle a real path is the handle's path and the confined walk, so handles have no such op. A real path that isn't UTF-8 fails with io_error, since no string spells it.

type (path: string): Promise<string>

path

type string
returns Promise<string>

throws

  • TypeError - for a relative path, or one holding a NUL byte or a lone surrogate
  • FsError - `not_found`, also for a dangling symlink; `not_a_directory` for a path through a file; `permission_denied`; `not_granted` on `fgro` for a path outside the run's roots; `io_error` for a symlink loop or any other failure

temp_dir

Creates a fresh directory under the OS temp directory, removed with everything in it when disposed (await using).

type (prefix?: string | undefined): Promise<FsDir & AsyncDisposable>

prefix?

the start of the directory's name, defaults to 'gro-'. It names no other directory: a non-string, a /, or a NUL byte throws TypeError, and .. is only the name's start (.. then six random characters).

type string
optional
returns Promise<FsDir & AsyncDisposable>

throws

  • TypeError - for a prefix that isn't a string or holds a `/` or a NUL byte
  • FsError - `not_granted` on `fgro` for a run started without `--allow-temp` or `-A`

FsCopyOptions
#

fs.ts view source

FsCopyOptions import type {FsCopyOptions} from '@fuzdev/gro/fs.js';

recursive?

Copy a directory and everything in it, symlinks copied as links.

type boolean

FsDir
#

fs.ts view source

FsDir import type {FsDir} from '@fuzdev/gro/fs.js';

A read-write handle on a directory. Every path is relative to it.

inheritance

extends: FsReadDir

dir

type (path: string): FsDir

path

type string
returns FsDir

readonly

A handle on the same directory without the write ops, at runtime as well as in types.

type (): FsReadDir

returns FsReadDir

write

Writes a file, replacing one that exists, or with create_new only creating one. data is a string, written as UTF-8, or a Uint8Array, else a TypeError.

type (path: string, data: string | Uint8Array<ArrayBufferLike>, options: FsWriteOptions & { create_new: true; }): Promise<"created" | "exists">

path

type string

data

type Uint8Array<ArrayBufferLike> | string

options

type FsWriteOptions & { create_new: true; }
returns Promise<"created" | "exists">

with create_new, 'created', or 'exists' when an entry was already there

mkdir

type (path: string, options?: FsMkdirOptions | undefined): Promise<void>

path

type string

options?

optional
returns Promise<void>

remove

Removes an entry. A final symlink is removed, not its target, and with a trailing / refused as not_a_directory. The handle's own directory ('') is refused with io_error.

type (path: string, options?: FsRemoveOptions | undefined): Promise<void>

path

type string

options?

optional
returns Promise<void>

rename

Renames an entry, replacing what's at to as the OS does. Symlinks are moved, not followed. The handle's own directory at either end is refused with io_error.

type (from: string, to: string): Promise<void>

from

type string

to

type string
returns Promise<void>

copy

Copies a file, replacing what's at to, or with recursive a directory tree.

type (from: string, to: string, options?: FsCopyOptions | undefined): Promise<void>

from

type string

to

type string

options?

optional
returns Promise<void>

set_times

Sets an entry's modification and access times, atime_ms defaulting to mtime_ms. A final symlink is followed, its target confined as any path is, unless follow is false, which sets the link's own times. The runtime's run box, as designed, refuses follow: false with permission_denied, since no form of it can be bounded there. The current time is the caller's Date.now().

type (path: string, times: FsTimes, options?: FsStatOptions | undefined): Promise<void>

path

type string

times

type FsTimes

options?

optional
returns Promise<void>

throws

  • TypeError - for a time that isn't a finite number from `0` to `8.64e15`
  • FsError - `not_found`, `not_a_directory`, `permission_denied`, `outside_root`

set_mode

Sets an entry's permission bits, like 0o755, following a final symlink, whose target is confined as any path is (Linux has no mode of its own for a link). The mode is absolute, never symbolic, and holds no setuid, setgid, or sticky bit.

type (path: string, mode: number): Promise<void>

path

type string

mode

type number
returns Promise<void>

throws

  • TypeError - for a mode that isn't an integer from `0` to `0o777`
  • FsError - `not_found`, `not_a_directory`, `permission_denied`, `outside_root`

symlink

Creates a symlink at path pointing at target, path first like every handle op. The target is data, stored verbatim and never resolved, so it may be relative, absolute, or dangling: following a link is what's confined, by every handle that reaches it. The final component of path is never followed, and its parent is confined.

type (path: string, target: string): Promise<"created" | "exists">

path

type string

target

type string
returns Promise<"created" | "exists">

'created', or 'exists' when any entry was already at path, a dangling symlink included, which is left untouched

throws

  • TypeError - for a `path` ending in `/`, `/.`, or `/..`, which names a directory, unless it resolves to the handle's own directory (`''`, `'.'`, `'d/..'`), an existing entry and so `'exists'`; or a `target` that is empty, holds a NUL byte, or isn't well-formed Unicode
  • FsError - `not_found` for a missing parent, `not_a_directory`, `permission_denied`, `outside_root`

FsMkdirOptions
#

fs.ts view source

FsMkdirOptions import type {FsMkdirOptions} from '@fuzdev/gro/fs.js';

recursive?

Create missing parents too; an existing directory is then not an error.

type boolean

exists_ok?

Return normally when the directory exists. An existing non-directory still throws.

type boolean

FsReadDir
#

fs.ts view source

FsReadDir import type {FsReadDir} from '@fuzdev/gro/fs.js';

A read-only handle on a directory. Every path is relative to it.

path

The directory's absolute real path, for display and as a spawned process's cwd. It carries no authority: ops take relative paths on the handle.

type string

readonly

dir

Narrows to a subdirectory, which must exist.

type (path: string): FsReadDir

path

type string
returns FsReadDir

throws

  • FsError - `not_found`, `not_a_directory`, `outside_root`

stat

type (path: string, options?: FsStatOptions | undefined): Promise<FileInfo | null>

path

type string

options?

optional
returns Promise<FileInfo | null>

the entry's info, or null when nothing is at path (a path through a file throws not_a_directory)

exists

Whether an entry is at path, following a final symlink.

type (path: string): Promise<boolean>

path

type string
returns Promise<boolean>

read

type (path: string): Promise<Uint8Array<ArrayBufferLike>>

path

type string
returns Promise<Uint8Array<ArrayBufferLike>>

read_text

Reads a file as UTF-8.

type (path: string): Promise<string>

path

type string
returns Promise<string>

read_if_exists

type (path: string): Promise<Uint8Array<ArrayBufferLike> | null>

path

type string
returns Promise<Uint8Array<ArrayBufferLike> | null>

the file's bytes, or null when nothing is at path

read_text_if_exists

type (path: string): Promise<string | null>

path

type string
returns Promise<string | null>

the file's UTF-8 text, or null when nothing is at path

list

Lists a directory's entries sorted by name, comparing the names' UTF-8 bytes, which is code point order, never a locale's or UTF-16's, so a listing is in one order on every filesystem and runtime. The whole directory is read before the first entry is yielded. An entry whose name isn't UTF-8 fails the listing with io_error, before any entry, since no string names it.

type (path?: string | undefined): AsyncIterable<DirEntry>

path?

defaults to the handle's own directory

type string
optional
returns AsyncIterable<DirEntry, any, any>

read_link

Reads a symlink's target as stored, never following the link, so the target may be absolute or lead outside the handle: reading it reaches nothing. A target that isn't UTF-8 fails with io_error, since no string spells it.

type (path: string): Promise<string>

path

type string
returns Promise<string>

throws

  • FsError - `not_found`; `io_error` when the entry isn't a symlink

stat_sync

type (path: string, options?: FsStatOptions | undefined): FileInfo | null

path

type string

options?

optional
returns FileInfo | null

exists_sync

type (path: string): boolean

path

type string
returns boolean

read_text_sync

type (path: string): string

path

type string
returns string

FsRemoveOptions
#

fs.ts view source

FsRemoveOptions import type {FsRemoveOptions} from '@fuzdev/gro/fs.js';

recursive?

Remove a directory and everything in it. Without it, only a file, a symlink, or an empty directory is removed.

type boolean

missing_ok?

Return normally when nothing is at the path.

type boolean

FsStatOptions
#

fs.ts view source

FsStatOptions import type {FsStatOptions} from '@fuzdev/gro/fs.js';

follow?

Whether a final symlink is followed to its target. Defaults to true.

type boolean

FsTimes
#

fs.ts view source

FsTimes import type {FsTimes} from '@fuzdev/gro/fs.js';

The times set_times gives an entry, in milliseconds since the epoch, as FileInfo.mtime_ms. Each is a finite number from 0 to 8.64e15, the range of a Date, else a TypeError, and the filesystem may clamp a time beyond its own range. The Node implementation passes seconds as a double that libuv truncates, to a microsecond on libuv 1.51 (Node 24.14) and to a nanosecond on 1.52.1 (Node 26.13): on the microsecond step an integer is stored exactly through 2242, and on the nanosecond step within half the double's spacing (119ns from 2004 to 2038), so stat can show an integer one step of its own double off where that spacing is the coarser, as from 2038-01-19 to 2039-09-07.

mtime_ms

The last modification time.

type number

atime_ms?

The last access time. Defaults to mtime_ms.

type number

FsWriteOptions
#

fs.ts view source

FsWriteOptions import type {FsWriteOptions} from '@fuzdev/gro/fs.js';

create_new?

Fail with 'exists' rather than replace an existing entry.

type boolean

mode?

The permission bits of a newly created file, before the umask, masked to 0o777, so no setuid, setgid, or sticky bit. An integer from 0 to 0o7777, else a TypeError.

type number

Depends on
#

Imported by
#