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 🙏

© 2025 – Pkg Stats / Ryan Hefner

@beyond-js/watchers

v1.0.7

Published

A powerful and flexible utility for monitoring files in Node.js applications. Unlike using libraries like `chokidar` directly, this tool operates with a client-server architecture, performing file monitoring in a separate process. This design ensures that

Readme

@beyond-js/watchers

A powerful and flexible utility for monitoring files in Node.js applications. Unlike using libraries like chokidar directly, this tool operates with a client-server architecture, performing file monitoring in a separate process. This design ensures that the file monitoring operation does not block your application's main thread.

Installation

npm install @beyond-js/watchers

Usage

The package is divided into a service and a client.

1. The Service

The watchers service is started with WatchersService. This service runs in a separate process and can manage multiple watchers, with each watcher dedicated to a specific folder.

import { WatchersService } from '@beyond-js/watchers/service';

const service = new WatchersService('name-of-watchers-service');

2. The Client: Creating and Managing Watchers

The client is the interface used to interact with the service. The WatcherClient is responsible for creating and managing the connection to a folder being watched. When a Watcher is configured to listen to a folder, it recursively monitors all files and subfolders within that path.

import { WatcherClient } from '@beyond-js/watchers/client';

const spec = {
	path: '/path/to/watch',
	excludes: ['node_modules'],
	is: 'name-of-the-watcher' // Used for debugging to help identify when multiple watchers are attached to the same path.
};

const watcher = new WatcherClient('name-of-watchers-service', spec);
await watcher.start();

3. Creating and Managing Listeners

After starting the watcher, you can create one or more listeners to filter and handle specific events from the monitored folder. This decoupled design allows you to have multiple listeners for the same folder, each with its own filtering rules, providing great flexibility. A listener configured with a filename will receive events for that specific file, regardless of which subfolder it is in.

Example: Listening for all JS files

const listener = watcher.listeners.create('/path/to/watch/src', {
	extname: ['.js']
});
await listener.listen();

listener.on('change', file => {
	console.log(`JS file changed: ${file}`);
});

Example: Listening for a specific file in any subfolder

const listener = watcher.listeners.create('/path/to/watch', {
	filename: 'config.json'
});
await listener.listen();

listener.on('change', file => {
	console.log(`Config file changed: ${file}`);
});

Stopping the Watcher and Listeners

When listeners are no longer needed, they can be stopped individually.

// Stop a single listener
await listener.stop();

// Once all listeners are stopped, you can stop the watcher.
await watcher.stop();

// Finally, close the service when all watchers are shut down.
service.kill();

API

WatchersService

  • constructor(name: string): Creates a new instance of WatchersService.

WatcherClient

  • constructor(serviceName: string, spec: WatcherSpec): Creates a new instance of WatcherClient. WatcherSpec is an object that must contain a path property for the folder to be monitored and an is property to identify the watcher.
  • start(): Promise<void>: Starts the watcher asynchronously.
  • stop(): Promise<void>: Stops the watcher asynchronously.
  • listeners: A property that contains the methods for managing the watcher's listeners.

Listener

  • create(path: string, filter?: IListenerFilter): Listener: Creates a new Listener instance for a specific watcher. It accepts a file or directory path and, optionally, a filter object (IListenerFilter).
  • listen(): Promise<UUID>: Registers the listener with the service process, enabling it to receive events.
  • stop(): Promise<void>: Stops the listener asynchronously.
  • on(event: string, callback: Function): void: Attaches an event handler. The available events are 'all', 'add', 'change', and 'unlink'.

Filter Object (IListenerFilter)

The Listener can be configured with a filter object to control which events are relevant.

  • includes?: string[]: A list of patterns to include files or directories.
  • excludes?: string[]: A list of patterns to exclude files or directories.
  • extname?: string[]: A list of file extensions to include.
  • filename?: string: A specific file name to include.

Listener Events

Once the listener has started, it will emit the following events via IPC:

  • 'all': Emitted on any event ('add', 'change', or 'unlink'). The callback function receives two arguments: the event type ('add', 'change', or 'unlink') and the file path.
  • 'add': Emitted when a new file or directory is added.
  • 'change': Emitted when an existing file is modified.
  • 'unlink': Emitted when a file or directory is deleted.

Features

  • Efficient file watching using chokidar.
  • Support for multiple watchers and listeners.
  • Configurable file filters (includes, excludes, extname).
  • Event-based architecture.
  • Inter-process communication (IPC) for cross-process management.

Implementation Details

  • Uses chokidar for file watching.
  • Implements a client-server architecture for distributed watching.
  • Supports chained exceptions for better error handling.
  • Utilizes Promises for asynchronous operations.

License

MIT © [BeyondJS]