Skip to content

About

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

Resources

Security policy

Stars

21 stars

Watchers

6 watching

Forks

Repository files navigation

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 ci / 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();

const { ready, child, argv } = flaggedRespawn(v8flags, process.argv);
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 ]) : { ready, child, argv }

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).

Parameters

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.

Returns

An object representing the state of respawn.

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

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).

Strict No LLM / No AI Policy

No LLMs for issues.

No LLMs for patches / pull requests.

No LLMs for comments on the bug tracker, including translation.

English is encouraged, but not required. You are welcome to post in your native language and rely on others to have their own translation tools of choice to interpret your words.

License

MIT

About

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

Resources

Security policy

Stars

21 stars

Watchers

6 watching

Forks

Releases

Sponsor this project

Packages

Used by

Contributors

Languages