Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/lousando/forkme
Spawn a process without a module
https://github.com/lousando/forkme
Last synced: about 1 month ago
JSON representation
Spawn a process without a module
- Host: GitHub
- URL: https://github.com/lousando/forkme
- Owner: lousando
- License: mit
- Created: 2017-11-17T03:29:16.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2017-11-17T05:44:47.000Z (about 7 years ago)
- Last Synced: 2024-08-10T02:52:04.902Z (5 months ago)
- Language: JavaScript
- Size: 7.81 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# forkme
[![Travis branch](https://img.shields.io/travis/lousando/forkme/master.svg?style=for-the-badge)](https://github.com/lousando/forkme)
Spawn a process without a module
_(does NOT write any temporary files)_
## Installation
```bash
yarn add forkme
```## Usage
```js
let forkme = require('forkme');
```### Basic Use
```js
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.```js
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:
```js
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.```js
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()`.```js
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.```js
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__ ChildProcessSpawns 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.