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

piping

v1.0.0-rc.4

Published

Keep your code piping hot! Live code reloading without additional binaries

Downloads

8,395

Readme

Piping

There are already node "wrappers" that handle watching for file changes and restarting your application (such as node-supervisor), as well as reloading on crash, but I wasn't fond of having that. Piping adds "hot reloading" functionality to node, watching all your project files and reloading when anything changes, without requiring a "wrapper" binary.

Piping uses the node cluster API to spawn your application in a thread and then kill/reload it when necessary. Piping should not be used in production.

Installation

npm install piping

Usage

Piping is super simple to integrate and should not change the way you run your application:

require("piping")();
// Application logic here
express = require("express");
app = express();
app.listen(3000);

With the default settings, this will cause a second instance of your application to be launched, which is monitored by the first. Piping then does a trick with uncaughtException handling to avoid running any of your code on the first process.

The function returned by piping also accepts an options object. The following options are supported:

  • main (string): The path to the "top" file of your application. Defaults to process.argv[1], which should be sufficient provided you launch your application via "node yourapp.js". Other launch methods may require this to be set manually. If your app doesn't reload/reloads when it shouldn't, try changing this.
  • args (string[]): Arguments to pass to your application. Defaults to the current arguments.
  • hook (boolean): Whether to hook into node's "require" function and only watch required files. Defaults to false, which means piping will watch all the files in the folder in which main resides. The require hook can only detect files required after invoking this module!
  • includeModules (boolean): Whether to include required files than reside in node_modules folders. Defaults to false. Only has an effect when hook is true. For ignoring node_modules when hook is false, please use ignore.
  • ignore (regex): Files/paths matching this regex will not be watched. Defaults to /(\/\.|~$)/
  • respawnOnExit (boolean) : Default false. Whether the application should respawn after exiting. If you experience problems with infinite loops, try setting this to false.
  • throw (boolean): Use the trick with exceptions to avoid running your code in the first instance. Defaults to true. If set to false, you will need to use an if statement around the piping call to tell if your code should run. Piping returns a truthy value in this case.
  • quiet (boolean): Suppress piping output. Defaults to false.
  • usePolling (boolean) : From chokidar. Default false. Whether to use fs.watchFile (backed by polling), or fs.watch. It is typically necessary to set this to true to successfully watch files over a network.
  • interval (integer) : From chokidar. Polling specific. Interval of file system polling (default 100).
  • binaryInterval (integer) : From chokidar. Polling specific. Interval of file system polling for binary files.

Example:

require("piping")({main: "./app/server.js", hook:true});
// App logic

Piping can also be used just by passing a string. In this case, the string is taken to be the "main" option:

require("piping")("./app/server.js");
// App logic

Events

Piping emits lifecycle events on both the supervisor and your application process, allowing for custom behavior. Most events provide the reloaders status, in the form:

{
  exiting: boolean, // True if the process is about to reload
  firstRun: boolean, // True on the first run of the application
  exitReason: string? // Reason for last exit, one of "exited", "errored", "killed", "requested"
  fileChanged: string? // Name of file which changed and triggered the last reload.
}
  • exited: The last application process exited of its own accord, with 0 error code.
  • errored: The last application process exited with a non zero exit code.
  • killed: The last application process has to be killed in order to reload.
  • requested: The last application process reloaded cleanly as requested.

Supervisor

You can receive lifecycle events by passing in a function as the second parameter to the piping call, or the first if you are not specifying any options. This function will be called once everything is ready, and will be passed an EventEmitter. See the example below for supported events:

require("piping")(function(reloader){
  reloader.on("started", function(status) {
    // called on the first run of the application.
  });
  reloader.on("watch", function(file) {
    // called when a file is added to the watch list by the require hook.
  });
  reloader.on("reloading", function(status) {
    // called when the application is about to reload.
  });
  reloader.on("waiting", function(status) {
    // called when the application has indicated it will reload.
  });
  reloader.on("reloaded", function(status) {
    // called when the application has completed a reload.
  });
  reloader.on("exited", function(status) {
    // called when the application exits unprompted.
  });
});

Application

On the application process, the piping call will return an EventEmitter. See the example below for supported events:

const reloader = require("piping")();
reloader.on("reload", function(done) {
  // called when a reload is requested by the supervisor.
  // You can use this to do any clean up required.
  // If this event is handled by your application you need to call the done function to indicate your application can be restarted now.
});
reloader.on("reloaded", function(status) {
  // called after a successful reload on the new application instance.
});