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

flagged-respawn

v2.0.0

Published

A tool for respawning node binaries when special flags are present.

Downloads

12,301,029

Readme

flagged-respawn

NPM version Downloads Build Status Coveralls Status

A tool for respawning node binaries when special flags are present.

What is it?

Say you wrote a command line tool that runs arbitrary javascript (e.g. task runner, test framework, etc). For the sake of discussion, let's pretend it's a testing harness you've named testify.

Everything is going splendidly until one day you decide to test some code that relies on a feature behind a v8 flag in node (--harmony, for example). Without much thought, you run testify --harmony spec tests.js.

It doesn't work. After digging around for a bit, you realize this produces a process.argv of:

['node', '/usr/local/bin/test', '--harmony', 'spec', 'tests.js']

Crap. The --harmony flag is in the wrong place! It should be applied to the node command, not our binary. What we actually wanted was this:

['node', '--harmony', '/usr/local/bin/test', 'spec', 'tests.js']

Flagged-respawn fixes this problem and handles all the edge cases respawning creates, such as:

  • Providing a method to determine if a respawn is needed.
  • Piping stderr/stdout from the child into the parent.
  • Making the parent process exit with the same code as the child.
  • If the child is killed, making the parent exit with the same signal.

To see it in action, clone this repository and run npm install / npm run respawn / npm run nospawn.

Sample Usage

#!/usr/bin/env node

const flaggedRespawn = require('flagged-respawn');

// get a list of all possible v8 flags for the running version of node
const v8flags = require('v8flags').fetch();

flaggedRespawn(v8flags, process.argv, function (ready, child) {
  if (ready) {
    console.log('Running!');
    // your cli code here
  } else {
    console.log('Special flags found, respawning.');
  }
  if (process.pid !== child.pid) {
    console.log('Respawned to PID:', child.pid);
  }
});

API

flaggedRespawn(flags, argv, [ forcedFlags, ] callback) : Void

Respawns the script itself when argv has special flag contained in flags and/or forcedFlags is not empty. Because members of flags and forcedFlags are passed to node command, each of them needs to be a node flag or a V8 flag.

Forbid respawning

If --no-respawning flag is given in argv, this function does not respawned even if argv contains members of flags or forcedFlags is not empty. (This flag is also used internally to prevent from respawning more than once).

Parameter:

| Parameter | Type | Description | | :------------ | :-------------: | :--------------------------------------------------------------------------------------- | | flags | Array | An array of node flags and V8 flags which are available when present in argv. | | argv | Array | Command line arguments to respawn. | | forcedFlags | Array or String | An array of node flags or a string of a single flag and V8 flags for respawning forcely. | | callback | function | A called function when not respawning or after respawned. |

  • callback(ready, proc, argv) : Void

    callback function is called both when respawned or not, and it can be distinguished by callback's argument: ready. (ready indicates whether a process spawned its child process (false) or not (true), but it does not indicate whether a process is a spawned child process or not. ready for a spawned child process is true.)

    argv is an array of command line arguments which is respawned (when ready is false) or is passed current process except flags within flags and --no-respawning (when ready is true).

    Parameter:

    | Parameter | Type | Description | | :-------- | :-----: | :------------------------------------------------------------------- | | ready | boolean | True, if not respawning and is ready to execute main function. | | proc | object | Child process object if respawned, otherwise current process object. | | argv | Array | An array of command line arguments. |

License

MIT