watch.ts

The watch host module: changes beneath an fs handle, as an async iterator of events.

watch.dir(dir, options?) watches the directory a handle is on and resolves once the watch is in place, reporting no entry that was already there. Each event's path is relative to the handle and /-separated, the handle's own directory '', so a path from an event goes straight back to the handle's ops. The handle is the grant: a read-only one is enough, and nothing beyond it is watched or reported.

Events are add, update, and delete, each with is_directory, and rescan, which says changes beneath a path may have been missed and its state is to be read again: the events not yet read overflowed max_pending, or a directory couldn't be watched. They're hints for a caller to act on, not a log to replay:

  • A symlink is an entry like a file, is_directory: false, never followed, as fs_search treats one: what it points at is never watched, so a change there reports nothing, and replacing the link reports the link itself.
  • A rename is a delete of the old path and an add of the new, unpaired, and a file replaced by a rename over it (an atomic save) may be an update or a delete then an add.
  • Events for one directory arrive in the order they happened; across directories there's no order.
  • A directory created or moved in is an add, then an add for each entry found in it once its own watch is in place. An entry created and removed before then is never seen, and one created as the watch lands may be reported twice.
  • A directory removed or moved out is a delete, and its entries may or may not have deletes of their own.
  • update is a change to a non-directory's contents or metadata; a directory's own changes show as its entries' events.
  • Debouncing is the caller's.

Events not yet read are buffered up to max_pending, by default 4096. Past it they're dropped for one rescan of '', as a kernel's event queue overflows, and the events after it are dropped until that rescan is read, since reading the tree then sees them.

A watcher ends when it's closed (close(), await using, or leaving its for await), when its signal aborts, or when the watched directory is removed, moved, or replaced. Closing or aborting ends the iteration and drops the events not yet read; a watched directory that's gone delivers what was queued, then the iteration throws FsError not_found. An open watcher keeps the process alive, as a listening server does, and once it has ended nothing of it does.

Watchers and their iterators are frozen and null-prototype, and hold their state in private fields, so a method called on any other object throws TypeError. Events and iterator results are frozen null-prototype records, so a polluted Object.prototype.then is never consulted on the way to the reader.

This is the Node implementation, which runs on the Node Gro, over chokidar's per-directory native backend, symlinks never followed. Its gaps against the runtime's builtin, which it documents rather than the API weakening to match:

  • A real handle is told by a registry in internal/fs_handles.ts, which the package doesn't export, but code importing that file by its path can register any object, and the watch is on that object's path. The runtime's builtin tells a handle by the brand the host gave it.
  • The watch is bound to the directory's path, not its inode, as an fs handle is, so a moved watched directory ends the watch as a removed one does, where the runtime's keeps watching the directory it opened.
  • Chokidar checks an entry with lstat and then watches and reads it by path, so a directory swapped for a symlink in between is watched through the link. The watcher checks each path before chokidar descends into it and before reporting it, the root still the directory the watch started on and each directory on the way still a directory, a check then a use as fs's walk is: a swap between the two escapes, and chokidar can be left watching the directory a swapped-in link points at, never beneath it, and the directories beneath one renamed away for the link, their events dropped. The runtime's builtin watches by descriptor, so no path is resolved after the scan.
  • The root is told apart by device and inode, which a filesystem can reuse: on ext4 a watched directory removed and another made at once at its path usually gets its inode, and the watch then goes on in the new one, the directory the handle's path names, before the watch is in place or after. The runtime's builtin ends on the removal itself.
  • An entry replaced at once by one of another type can go unreported, with no rescan: a file replaced by a directory reports nothing, not even the directory's entries later, and a directory replaced by a symlink is an update of the link.
  • Names are decoded as UTF-8 by Node, so a name that isn't UTF-8 may go unreported, or arrive with U+FFFD in place of its bytes, naming no entry the handle reaches, and listing its directory through the handle fails with io_error.
  • Node exposes no kernel overflow, so a rescan comes only from max_pending and a directory chokidar couldn't watch, never from events the kernel dropped. A directory that can't be read when the watch starts stays unwatched after it's readable.
  • Any module can import node:fs and call watch on any path, so a handle bounds only code that uses it, a review signal, not a boundary.
view source

Declarations
#

7 declarations

watch
#

watch.ts view source

Watch import {watch} from '@fuzdev/gro/watch.js';

The watch host module's root. Import it where authority enters (a task, a script, an entry module), with the fs handle it watches.

Watch
#

watch.ts view source

Watch import type {Watch} from '@fuzdev/gro/watch.js';

The watch host module's root.

dir

Watches the directory a handle is on, resolving once the watch is in place, with no event for what was already there.

type (dir: FsReadDir, options?: WatchDirOptions | undefined): Promise<Watcher>

dir

the handle to watch, read-only or not

options?

optional
returns Promise<Watcher>

throws

  • FsError - `not_found`, `not_a_directory`, `permission_denied`, `outside_root`, or `io_error`, as listing the handle's directory throws, and for a directory removed or replaced before the watch is in place
  • TypeError - for a `dir` that isn't a handle this copy of `fs` made, a forged object or another copy's handle, or an invalid option
  • the - `signal`'s reason, when it aborts before the watch is in place

WatchChange
#

watch.ts view source

WatchChange import type {WatchChange} from '@fuzdev/gro/watch.js';

A change to an entry beneath a watched directory.

kind

add for an entry that appeared, update for a non-directory whose contents or metadata changed, delete for one that's gone.

type "add" | "update" | "delete"

readonly

path

The entry's path relative to the watched handle, /-separated.

type string

readonly

is_directory

Whether the entry is a directory; a symlink is not, whatever it points at.

type boolean

readonly

WatchDirOptions
#

watch.ts view source

WatchDirOptions import type {WatchDirOptions} from '@fuzdev/gro/watch.js';

The options of watch.dir.

recursive?

Whether the directories beneath are watched too, defaults to false, which reports the directory's own entries, a subdirectory's add and delete included.

type boolean

signal?

Ends the watch when it aborts. Before watch.dir resolves, the call rejects with its reason; after, the iteration ends.

type AbortSignal

max_pending?

How many events not yet read are buffered before they're dropped for one rescan of '', a positive safe integer, defaults to 4096.

type number

Watcher
#

watch.ts view source

Watcher import type {Watcher} from '@fuzdev/gro/watch.js';

A running watch: an async iterable of its events, read once.

inheritance

extends: AsyncIterable<WatchEvent>,AsyncDisposable

close

Ends the watch, dropping the events not yet read. Resolves once nothing of it is left running.

type (): Promise<void>

returns Promise<void>

WatchEvent
#

WatchRescan
#

watch.ts view source

WatchRescan import type {WatchRescan} from '@fuzdev/gro/watch.js';

Changes beneath path may have been missed, so its state is to be read again.

kind

type "rescan"

readonly

path

The directory to read again, relative to the watched handle, '' for all of it.

type string

readonly

Depends on
#