You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 7-8.RSVP
Socket
Socket
Sign inDemoInstall

flagged-respawn

Package Overview
Dependencies
Maintainers
3
Versions
10
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

flagged-respawn

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


Version published
Maintainers
3
Created

Package description

What is flagged-respawn?

The flagged-respawn npm package is designed to facilitate the handling of specific flags in Node.js command-line applications. It allows developers to automatically respawn their node process with selected V8 flags or custom flags that are initially passed, making it easier to work with features or behaviors that require such flags to be enabled.

What are flagged-respawn's main functionalities?

Respawning with V8 flags

This feature allows the application to respawn with specified V8 flags if they are not already enabled. The code sample demonstrates how to use flagged-respawn to check for the '--harmony' flag and respawn the process with it if necessary.

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

const v8Flags = ['--harmony'];
flaggedRespawn(v8Flags, process.argv, function (ready, child) {
  if (ready) {
    // your app code here
  } else if (child) {
    // this is the child process, do something with it if needed
  }
});

Custom flag handling

This feature supports respawning the process with custom flags. The example shows how to ensure a process respawns with a '--custom-flag', allowing developers to implement custom behavior based on the presence of this flag.

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

const customFlags = ['--custom-flag'];
flaggedRespawn(customFlags, process.argv, function (ready) {
  if (ready) {
    // your app code here, knowing that --custom-flag is being handled
  }
});

Other packages similar to flagged-respawn

Changelog

Source

2.0.0 (2021-11-21)

⚠ BREAKING CHANGES

  • Drop support of flags separated by underscores
  • Normalize repository, dropping node <10.13 support (#16)

Miscellaneous Chores

  • Drop support of flags separated by underscores (cbd39e6)
  • Normalize repository, dropping node <10.13 support (#16) (cbd39e6)

Readme

Source

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:
ParameterTypeDescription
flagsArrayAn array of node flags and V8 flags which are available when present in argv.
argvArrayCommand line arguments to respawn.
forcedFlagsArray or StringAn array of node flags or a string of a single flag and V8 flags for respawning forcely.
callbackfunctionA 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:

    ParameterTypeDescription
    readybooleanTrue, if not respawning and is ready to execute main function.
    procobjectChild process object if respawned, otherwise current process object.
    argvArrayAn array of command line arguments.

License

MIT

Keywords

FAQs

Package last updated on 21 Nov 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc