🚨 Shai-Hulud Strikes Again:834 Packages Compromised.Technical Analysis →
Socket
Book a DemoInstallSign in
Socket

forkme

Package Overview
Dependencies
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

forkme

Spawn a process without a module

latest
npmnpm
Version
2.0.1
Version published
Maintainers
1
Created
Source

forkme

Travis branch

Spawn a process without a module

(does NOT write any temporary files)

Installation

yarn add forkme

Usage

let forkme = require('forkme');

Basic Use

var child = forkme(function () {
    console.log("running in a child process!");
});

Context

A fake copy of the calling module is created in the child process, so as much as possible the closure should behave as if it's still in the module that called forkme().

One possible way in which the calling module and the closure module might differ, is that the closure module will be the main module of the child process.

forkme(function () {
	console.log(require.main === module); // true
});

require
module.require

Modules required from the closure should resolve the same way they would if they had been required in the calling module.

Given:

  • Calling module "/path/to/calling-module.js"
  • Other module "/path/to/node_modules/foo/index.js"
  • Other module "/path/to/bar.js"

Then:

forkme(function () {
	console.log(require.resolve('foo')); // "/path/to/node_modules/foo/index.js"
	console.log(require.resolve('./bar')); // "/path/to/bar.js"
});

__filename
__dirname
module.filename

These variables have the same values in the closure as they did in the calling module.

module.id

This will be "." because the closure is the main module of the child process.

Arguments

An optional array of values can be passed as the first argument. The values will be passed to the closure when it's called in the child process.

forkme(['foo', 'bar'], function(a, b) {
    console.log(a + " " + b); // "foo bar"
});

All values in the arguments array must be JSON serializable.

Options

Environment variables, working directory, input and output streams, etc., can be modified by passing an options object. The options are passed directly to child_process.fork().

var child = forkme({
    cwd: "/some/path",
    env: { foo: 'bar' },
    silent: true // Pipe stdin, stdout, and stderr to parent process.
}, function () {
    console.log(process.cwd() + ", " + process.env.foo);
});

child.stdout.setEncoding('utf8');
child.stdout.on('data', function (data) {
	process.stdout.write(data); // "/some/path, bar";
});

Return

If the closure returns a defined value, an IPC message will be sent back to the parent process with the returned value. The return value should be JSON serializable.

var child = forkme(function () {
	return 'foo';
});

child.on('message', function (message) {
	console.log(message); // "foo"
});

API

forkme(closure)
forkme(args, closure)
forkme(options, closure)
forkme(args, options, closure)

  • args Array (optional)
  • options Object (optional)
  • closure Function
  • returns ChildProcess

Spawns a child process which calls the closure, passing it all values in the args array.

The options argument is passed directly to child_process.fork(). See the documentation for child_process.fork() for more information.

A standard Node.js ChildProcess instance is returned.

Keywords

fork

FAQs

Package last updated on 17 Nov 2017

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