Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Introducing Node Sentinel File Watcher

DZone's Guide to

Introducing Node Sentinel File Watcher

In this article we look at the new NSFW that’s entirely safe for work. Check out this new Node.js module for recursively watching directories.

· Web Dev Zone
Free Resource

Discover how to focus on operators for Reactive Programming and how they are essential to react to data in your application.  Brought to you in partnership with Wakanda

Node Whatnow?

Node Sentinel File Watcher (NSFW). It’s a file watching module built for Node.js. I built NSFW to overcome an obstacle we were encountering while developing GitKraken, and I released it as an ongoing open-source project. It’s actually entirely safe for work. Ideal for it, even.

NSFW is a native Node module, which means it is developed and written to run natively on an operating system without an interpreter (JavaScript uses an interpreter, whereas a language like C++ compiles to machine code).

NSFW has a file watching implementation for Linux, MacOS, and Windows, which are wrapped and bound for the node runtime to utilize. Thanks to its native implementation, NSFW is able to watch recursive directories on all operating systems in a very performant way.

What Problem Does NSFW Solve?

At this time, Node has pretty poor support for file watching. Every operating system has a different capacity to watch directories. Here’s what some celebs have been saying on Twitter:

NSFW solves the poor file watching experience on Node by utilizing 3 low-level file watching utilities written in C++ and targeted for the Linux, MacOS, and Windows operating systems.

NSFW does most of its work on a separate thread, giving it big performance gains over the built-in Node file system (FS) watcher API. NSFW queues file events on a separate thread and batches file change dispatches into a single callback. That callback can be throttled internally to prevent spamming the JavaScript/C++ bridge.

This means NSFW doesn’t slow down JavaScript applications, even when they’re under a load of large FS operations.

Linux, MacOS, and Windows each ship with their own file watching APIs. Since NSFW’s watch utility is targeted specifically at each of those APIs, it means the experience of using the module is consistent across all three OSs. NSFW fills in the gaps for each API so they’re all consistently feature-complete:

  • Linux: The Inotify file watching system does not perform recursive directory watching, so NSFW builds and maintains a recursive watch tree for you.
  • MacOS: FSEvents is known to produce inconsistent file events (the file event bitmask becomes corrupted if events occur too quickly), so NSFW stats and disambiguates file change events for you.
  • Windows: Supports all targeted needs out of the box. (RECORD SCRATCH) That’s right, Windows has the best native support. I said it.

Why This Was an Important Problem for the Axosoft Dev Team to Solve

GitKraken is currently the primary consumer of NSFW. A good Git client should not ship with a refresh button because it should automatically know when things change.


What could have been: Imagine if GitKraken needed a refresh button like this. Gross!

NSFW is essential to the smooth, cross-platform experience of GitKraken, as it helps the app respond quickly and accurately to changes in a repository it is viewing. NSFW is a quiet and humble, no-frills background process. It doesn’t make waves. It doesn’t talk through movies, chew loudly, or snore, but its transparency is its strength; if you notice it, it’s more than likely because something isn’t working. So cheers to watching files silently and effectively – something made easy by the NSFW module.

Development Hurdles

Oh yes.

After fiddling around with the project through the 0.x.x months (aka The Dark Times), I learned a lot about how each underlying file watcher API works, including their caveats, demands, and broken bits.

After spending a week putting together a complete system diagram, I scrapped the entire 0.x.x project and rebuilt the project to handle the differences of each operating system in a planned way. I also did away with the C++/CLI interface and opted for ReadDirectoryChangesW in Windows.

A Couple of Lessons Learned

  1. Predicting a system’s architecture without first dirtying your hands with the core features, means you’ll probably end up throwing away your project, dirtying your hands with the core features, and starting the project over. As a new developer, be prepared to throw away your prototypes.
  2. I’m not sure if C++/CLI and Node should ever be a thing. Ever.

Check out NSFW, and use it for your own projects.

Install NSFW

Learn how divergent branches can appear in your repository and how to better understand why they are called “branches".  Brought to you in partnership with Wakanda

Topics:
node.js ,module ,javascript ,webdev

Published at DZone with permission of Tyler Wanek, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}