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

sents

v0.3.2

Published

Sentinels file watcher

Downloads

12

Readme

Test

Filesystem watcher that uses fs.watch and friends (i.e., no polling). This is the library—check out sents-cli to use this on the command-line.

Features

  • Zero dependencies 🍩 and no native code
  • Supports macOS, Linux, Windows (and probably others) via the same API
  • Supports hard links (announces changes at all places inode is found)
  • Handles awkward renames on case-insensitive filesystems ("foo" => "FOO" is announced)

It does not support some features:

  • Following symlinks
  • Renames (instead announce delete/add—what do renames mean in a world of hard links?)
  • Watching across volumes
  • And it should not be used on network shares (they need polling, every watcher hates these)

Usage

Basic usage:

import buildWatcher from 'sents';

// Create a watcher by passing a directory name ("." for the current dir).
const watcher = buildWatcher('.');

// Type can be 'add', 'change', or 'remove'.
watcher.on('raw', (filename, type, ino) => {
  console.warn(type.toUpperCase(), filename, ino);
});

// If the watcher emits an error, it's no longer usable.
watcher.on('error', (e) => {
  console.warn('got error', e);
});

// Call .close() to shut down the watcher.
setTimeout(() => {
  watcher.close();
}, 60 * 1000);

Note that this watches a single directory, and directly watching files is not supported.

The only events emitted by sents are "ready", "raw", and "error".

Options

This also supports some options:

const watcher = buildWatcher('.', {
  dotfiles: false,
  filter: (rel) => true,
  delay: 1000,
});
  • dotfiles controls whether files starting with '.' are returned (default false)
  • filter allows filtering of the results (return true to include, default includes all)
    • When directories are passed, they'll end with path.sep (i.e., "/" most places); if you filter them, you'll never be asked or notified about their subdirectories
    • This should be a pure function—don't change results over time—otherwise, you're gonna have a bad time
  • delay allows you to delay and aggregate changes by ms (if unset, uses a microtask)

Notes

On macOS and Windows, watching a whole directory subtree is fairly cheap. On Linux and other platforms, this package installs a watcher on every subdirectory. Keep this in mind when building tools using sents—be sure to use filter to limit what you're watching.

Ready Check

If you don't want to get all initial updates (i.e., the first scan of files) then you do either of:

await watcher.ready;
watcher.once('ready', () => {...});

Glob Support

This doesn't have glob support or any built-in filtering aside the controls above. If you want to write a command-line tool or similar, you should build a filter function that supports globs. Check out sents-cli.

Files Only

Technically this package isn't a file watcher, it's a directory watcher (most files watchers are). It's not more efficient to watch single files than it is a whole directory (since it has to be watched in case the file is removed). If you just want to watch a small number of files for changes, be sure to allow them specifically in filter, while ignoring all other files or directories.