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.
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:
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.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.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.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.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:
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.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.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.U+FFFD in place of its bytes, naming no entry the handle reaches, and listing its
directory through the handle fails with io_error.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.node:fs and call watch on any path, so a handle bounds only
code that uses it, a review signal, not a boundary.7 declarations
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 import type {Watch} from '@fuzdev/gro/watch.js'; The watch host module's root.
dirWatches 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>
dirthe handle to watch, read-only or not
options?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 placeTypeError - for a `dir` that isn't a handle this copy of `fs` made, a forged
object or another copy's handle, or an invalid optionthe - `signal`'s reason, when it aborts before the watch is in placeWatchChange import type {WatchChange} from '@fuzdev/gro/watch.js'; A change to an entry beneath a watched directory.
kindadd 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"
pathThe entry's path relative to the watched handle, /-separated.
type string
is_directoryWhether the entry is a directory; a symlink is not, whatever it points at.
type boolean
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 import type {Watcher} from '@fuzdev/gro/watch.js'; A running watch: an async iterable of its events, read once.
AsyncIterable<WatchEvent>,AsyncDisposablecloseEnds the watch, dropping the events not yet read. Resolves once nothing of it is left running.
type (): Promise<void>
Promise<void>WatchEvent type WatchChange | WatchRescan
import type {WatchEvent} from '@fuzdev/gro/watch.js'; What a Watcher yields.
WatchRescan import type {WatchRescan} from '@fuzdev/gro/watch.js'; Changes beneath path may have been missed, so its state is to be read again.
kindtype "rescan"
pathThe directory to read again, relative to the watched handle, '' for all of it.
type string