Security News
Nightmares on npm: How Two Malicious Packages Facilitate Data Theft and Destruction
Our threat research team breaks down two malicious npm packages designed to exploit developer trust, steal your data, and destroy data on your machine.
@parcel/watcher
Advanced tools
A native C++ Node module for querying and subscribing to filesystem events. Used by Parcel 2.
The @parcel/watcher package is a file system watcher that provides an efficient way to subscribe to changes in the file system. It is designed to be fast and works across multiple platforms. It can be used to watch for changes in files and directories and perform actions in response to those changes.
Subscribing to file changes
This feature allows you to subscribe to changes in a specific directory. The callback function is called with an array of events whenever a change occurs. Each event contains the type of change (e.g., 'create', 'update', 'delete') and the path to the affected file or directory.
const {subscribe} = require('@parcel/watcher');
async function run() {
await subscribe('/path/to/directory', (err, events) => {
if (err) {
// Handle the error
return;
}
// Handle file system change events
events.forEach(event => {
console.log(`Type: ${event.type}, Path: ${event.path}`);
});
});
}
run();
Unsubscribing from file changes
This feature allows you to unsubscribe from a previously established subscription. This is useful when you no longer need to watch for changes or when your application is shutting down.
const {subscribe, unsubscribe} = require('@parcel/watcher');
let subscription;
async function run() {
subscription = await subscribe('/path/to/directory', (err, events) => {
// Handle file system change events
});
}
async function stop() {
await unsubscribe(subscription);
}
run();
// Later on, when you want to stop watching
stop();
Chokidar is a popular file watching package that provides a high-level API to watch file system changes. It is known for its stability and compatibility with multiple platforms. Chokidar is often used because of its extensive feature set and ease of use, but it may be slower than @parcel/watcher for large directories or projects.
Watchpack is a wrapper around the file-watching functionality of Node.js and other file-watching libraries. It is used by webpack to detect changes in files. Watchpack offers a good balance of features and performance, but @parcel/watcher might provide better performance in certain scenarios due to its focus on speed and efficiency.
Node-watch is a simple and lightweight file watching library. It is easy to use and works across different platforms. While it may not have as many features as @parcel/watcher, it is suitable for simpler use cases where a straightforward file watching solution is needed.
A native C++ Node module for querying and subscribing to filesystem events. Used by Parcel 2.
git checkout
or npm install
).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);
@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.
@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.
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).
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.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(/* ... */);
MIT
FAQs
A native C++ Node module for querying and subscribing to filesystem events. Used by Parcel 2.
The npm package @parcel/watcher receives a total of 2,784,286 weekly downloads. As such, @parcel/watcher popularity was classified as popular.
We found that @parcel/watcher demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Our threat research team breaks down two malicious npm packages designed to exploit developer trust, steal your data, and destroy data on your machine.
Security News
A senior white house official is urging insurers to stop covering ransomware payments, indicating possible stricter regulations to deter cybercrime.
Security News
ESLint has added JSON and Markdown linting support with new officially-supported plugins, expanding its versatility beyond JavaScript.