npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@parcel/watcher

v2.5.0

Published

A native C++ Node module for querying and subscribing to filesystem events. Used by Parcel 2.

Downloads

27,353,134

Readme

@parcel/watcher

A native C++ Node module for querying and subscribing to filesystem events. Used by Parcel 2.

Features

  • Watch - subscribe to realtime recursive directory change notifications when files or directories are created, updated, or deleted.
  • Query - performantly query for historical change events in a directory, even when your program is not running.
  • Native - implemented in C++ for performance and low-level integration with the operating system.
  • Cross platform - includes backends for macOS, Linux, Windows, FreeBSD, and Watchman.
  • Performant - events are throttled in C++ so the JavaScript thread is not overwhelmed during large filesystem changes (e.g. git checkout or npm install).
  • Scalable - tens of thousands of files can be watched or queried at once with good performance.

Example

const watcher = require('@parcel/watcher');
const path = require('path');

// Subscribe to events
let subscription = await watcher.subscribe(process.cwd(), (err, events) => {
  console.log(events);
});

// later on...
await subscription.unsubscribe();

// Get events since some saved snapshot in the past
let snapshotPath = path.join(process.cwd(), 'snapshot.txt');
let events = await watcher.getEventsSince(process.cwd(), snapshotPath);

// Save a snapshot for later
await watcher.writeSnapshot(process.cwd(), snapshotPath);

Watching

@parcel/watcher supports subscribing to realtime notifications of changes in a directory. It works recursively, so changes in sub-directories will also be emitted.

Events are throttled and coalesced for performance during large changes like git checkout or npm install, and a single notification will be emitted with all of the events at the end.

Only one notification will be emitted per file. For example, if a file was both created and updated since the last event, you'll get only a create event. If a file is both created and deleted, you will not be notifed of that file. Renames cause two events: a delete for the old name, and a create for the new name.

let subscription = await watcher.subscribe(process.cwd(), (err, events) => {
  console.log(events);
});

Events have two properties:

  • type - the event type: create, update, or delete.
  • path - the absolute path to the file or directory.

To unsubscribe from change notifications, call the unsubscribe method on the returned subscription object.

await subscription.unsubscribe();

@parcel/watcher has the following watcher backends, listed in priority order:

You can specify the exact backend you wish to use by passing the backend option. If that backend is not available on the current platform, the default backend will be used instead. See below for the list of backend names that can be passed to the options.

Querying

@parcel/watcher also supports querying for historical changes made in a directory, even when your program is not running. This makes it easy to invalidate a cache and re-build only the files that have changed, for example. It can be significantly faster than traversing the entire filesystem to determine what files changed, depending on the platform.

In order to query for historical changes, you first need a previous snapshot to compare to. This can be saved to a file with the writeSnapshot function, e.g. just before your program exits.

await watcher.writeSnapshot(dirPath, snapshotPath);

When your program starts up, you can query for changes that have occurred since that snapshot using the getEventsSince function.

let events = await watcher.getEventsSince(dirPath, snapshotPath);

The events returned are exactly the same as the events that would be passed to the subscribe callback (see above).

@parcel/watcher has the following watcher backends, listed in priority order:

The FSEvents (macOS) and Watchman backends are significantly more performant than the brute force backends used by default on Linux and Windows, for example returning results in miliseconds instead of seconds for large directory trees. This is because a background daemon monitoring filesystem changes on those platforms allows us to query cached data rather than traversing the filesystem manually (brute force).

macOS has good performance with FSEvents by default. For the best performance on other platforms, install Watchman and it will be used by @parcel/watcher automatically.

You can specify the exact backend you wish to use by passing the backend option. If that backend is not available on the current platform, the default backend will be used instead. See below for the list of backend names that can be passed to the options.

Options

All of the APIs in @parcel/watcher support the following options, which are passed as an object as the last function argument.

  • ignore - an array of paths or glob patterns to ignore. uses is-glob to distinguish paths from globs. glob patterns are parsed with micromatch (see features).
    • paths can be relative or absolute and can either be files or directories. No events will be emitted about these files or directories or their children.
    • glob patterns match on relative paths from the root that is watched. No events will be emitted for matching paths.
  • backend - the name of an explicitly chosen backend to use. Allowed options are "fs-events", "watchman", "inotify", "kqueue", "windows", or "brute-force" (only for querying). If the specified backend is not available on the current platform, the default backend will be used instead.

WASM

The @parcel/watcher-wasm package can be used in place of @parcel/watcher on unsupported platforms. It relies on the Node fs module, so in non-Node environments such as browsers, an fs polyfill will be needed.

Note: the WASM implementation is significantly less efficient than the native implementations because it must crawl the file system to watch each directory individually. Use the native @parcel/watcher package wherever possible.

import {subscribe} from '@parcel/watcher-wasm';

// Use the module as documented above.
subscribe(/* ... */);

Who is using this?

License

MIT