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

spork

v0.3.2

Published

Stress-free node child process spawning and management

Downloads

22

Readme

spork

Stress-free node child process spawning and management. A small wrapper around forever-monitor with a simple interface.

npm package node version dependency status contributions welcome

Not everything has a node adapter. Often times, there is a need to spawn a child process to run another script (maybe even fork another node process).

Forking and managing child processes in node can prove challenging. If something fails in the pipe, you can be left over with phantom processes running in the background. Over time, these can accumulate and result in strange issues such as blocked ports, misbehaving debug tools, "random" exits, etc. Even a number of popular node modules, grunt plugins, etc. fail to exit cleanly.

Additionally, the child_process.spawn interface is not very desirable. It was written for flexibility, but the majority of the time it is used the same way. Managing all of this correctly across multiple processes and multiple scripts can be cumbersome and error-prone.

To defer this redudancy and avoid the risk, spork was created to try to make spawning and forking smoother -- hence the name.

Overview

Spork follows a very similar syntax to node core's child_process.spawn to keep the interface simple. It also takes advantage of the power of forever-monitor to add some much-needed robustness to the interface. The syntax should be straight-forward and familiar:

const spork = require('spork');
spork(command, args, options);

Besides spawning child processes, spork can do a few other things:

  • manage and kill processes that fail - clean exits using node-clean-exit
  • capture and/or act on stdio events
  • exit automatically on failure and/or success (configurable)
  • inherit parent stdio automatically, or suppress entirely
  • retain ANSI color character sequences through the pipe

Installation

$ npm install --save spork

The interface

Take me straight to the API

In its simplest form:

spork('command', {exit: true}); // process will automatically exit when the command fails or succeeds

Normally, all of this is required for just one process, in order to spawn it, act on data events, and exit cleanly:

const done = async();

let child = spawn('command', ['--arg1', '--arg2'], {
  env: _.extend(process.env, {
    WHATEVER: 'isNeeded'
  }),

  stdio: [process.stdin, 'pipe', 'pipe']
});

child.stdout.setEncoding('utf8');
child.stderr.setEncoding('utf8');

child.stdout.on('data', function(data) {
  // .. do something
  
  process.stdout.write(data);
});

let errorBuffer = '';
child.stderr.on('data', function(data) {
  errorBuffer += data;
});

child.on('close', function(code) {
  if (code) {
    customErrorHandler(errorBuffer, code);
  } else {
    done();
  }
});

child.on('error', function(err) {
  console.error(err);
  process.exit(99);
});

process.stdin.resume();

process.on('exit', exitHandler);
process.on('SIGINT', _.partial(exitHandler, {exitCode: 2}));
process.on('uncaughtException', _.partial(exitHandler, {exitCode: 99}));

function exitHandler(exitData, err) {
  if (err && err.stack) {
    console.error(err);
    process.exit(1);
  }
  
  child.kill();

  if (exitData && exitData.exit) {
    process.exit(exitData.exit);
  }
}

Instead, just do this, to produce the same result:

spork('command', ['--arg1', '--arg2'], {env: {WHATEVER: 'isNeeded'}})
    .on('stdout', function(data) {
      // .. do something
    })
    .on('exit:code', function(code) {
      // .. do something
      process.exit(code);
    });

Or, let spork handle everything:

spork('command', ['--arg1', '--arg2'], {exit: true});

spawn(command, [args|options], [options])

  • @param command {string} - The command to run.
  • @param [args] {mixed} - The arguments to pass to the command, as an {array}. Also can pass {object} [options] in this position and ignore [args].
  • @param [options] {object} - Additional options to be passed to forever-monitor, plus custom options below.
  • @returns {forever.monitor}
  • @see child_process.spawn
  • @see forever-monitor

Options

All options here, plus:

  • exit {mixed} - Close the child process on exit. Can be success, failure, always, true (alias for always), or false. Defaults to failure.
  • quiet {boolean} - Output nothing (suppress STDOUT and STDERR. Defaults to false.
  • stdio {array} - Identify whether or not to pipe STDIN, STDOUT, STDERR to the parent process. Defaults to ['inherit', 'inherit', 'inherit'].
  • verbose {mixed} - Output more. Can be a boolean or a number. The higher the number, the higher the verbosity. Defaults to false.

You can completely nix the built-in stdio inheritence using stdio: [null, null, null] and manage it all yourself.

Events

Here

Contributing

contributions welcome

Licence

The MIT License (MIT)

Copyright (c) 2016 Justin Helmer

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.