# script-manager

> Manager for running foreign and potentionally dangerous scripts in the cluster

Latest version **0.10.2** (published 2020-09-07) · MIT license · 0 weekly downloads

## Install

```sh
npm install script-manager
pnpm add script-manager
yarn add script-manager
bun add script-manager
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.10.2 |
| Published | 2020-09-07 |
| First published | 2015-03-12 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 3 |
| Unpacked size | 44.9 KB |
| Known vulnerabilities | 0 (+26 in 2 direct dependencies) |
| Install scripts | no |
| GitHub stars | 11 |
| Author | Jan Blaha |
| Maintainers | pofider, bjrmatos |
| Keywords | custom, script, manager |

## Links

- npm: https://www.npmjs.com/package/script-manager
- Repository: https://github.com/pofider/node-script-manager
- Issues: https://github.com/pofider/node-script-manager/issues
- npm.io page: https://npm.io/package/script-manager

## Dependencies (3)

- [uuid](https://npm.io/package/uuid.md) 3.3.2
- [axios](https://npm.io/package/axios.md) 0.19.2
- [serializator](https://npm.io/package/serializator.md) 1.0.2

## Alternatives

- [@openai/codex-sdk](https://npm.io/package/@openai/codex-sdk.md) — 731.4K weekly downloads
- [babel-plugin-transform-react-jsx](https://npm.io/package/babel-plugin-transform-react-jsx.md) — 565.0K weekly downloads
- [babel-helper-remove-or-void](https://npm.io/package/babel-helper-remove-or-void.md) — 508.5K weekly downloads
- [@pnpm/store-controller-types](https://npm.io/package/@pnpm/store-controller-types.md) — 186.9K weekly downloads
- [react-native-signature-canvas](https://npm.io/package/react-native-signature-canvas.md) — 155.6K weekly downloads

## Recent versions

- 0.10.2 (latest) — 2020-09-07
- 0.10.1 — 2020-03-02
- 0.10.0 — 2020-02-20
- 0.9.1 — 2019-10-09
- 0.9.0 — 2019-08-27
- 0.8.6 — 2019-05-21
- 0.8.5 — 2019-04-16
- 0.8.4 — 2019-03-04
- 0.8.3 — 2019-01-17
- 0.8.2 — 2019-01-05
- 0.8.1 — 2018-12-07
- 0.8.0 — 2018-09-05
- 0.7.1 — 2018-08-02
- 0.7.0 — 2017-11-07
- 0.6.0 — 2017-09-27
- … 22 more at https://npm.io/package/script-manager/versions

## README

# script-manager
[![NPM Version](http://img.shields.io/npm/v/script-manager.svg?style=flat-square)](https://npmjs.com/package/script-manager)
[![License](http://img.shields.io/npm/l/script-manager.svg?style=flat-square)](http://opensource.org/licenses/MIT)
[![Build Status](https://travis-ci.org/pofider/node-script-manager.png?branch=master)](https://travis-ci.org/pofider/node-script-manager)

**node.js manager for running foreign and potentially dangerous scripts in the cluster**


## Basics

You can use node.js vm module for running a custom javascript code, but when the code is bad it can quickly get your node.js process into an endless loop. For this reason it is better to run users's custom code in a separate node process which you can recycle when the script reaches timeout. This can be achieved using node child_process module, but a simple implementation has limitations in performance and scale because running each script in a new node child process can quickly spawn whole system with node processes. This package solves the problem of running user's custom javascript code in a load balanced cluster of node processes which are reused over the requests and recycled when needed.

```js
var path = require('path')
var scriptManager = require("script-manager")({ numberOfWorkers: 2 });

scriptManager.ensureStarted(function(err) {

	/*send user's script including some other specific options into
	wrapper specified by execModulePath*/
	scriptManager.execute({
		script: "return 'Jan';"
	}, {
		execModulePath: path.join(__dirname, "script.js"),
	    timeout: 10
	}, function(err, res) {
		if (err) {
			return console.error('Error:', err)
		}

		console.log(res);
	});

});
```

```js
/*script.js
wrapper usually does some fancy thing and then runs the custom script using node.js vm module*/
module.exports = function(inputs, callback, done) {
	var result = require('vm').runInNewContext(inputs.script, {
		require: function() { throw new Error("Not supported"); }
	});
	done(null result);
};
```

## Callbacks
The executing script can also callback to the caller process. The callback is provided using `node.js` cross process messages so it has some limitations, but should work when transferring just common objects in parameters.

To provide caller callback you can add the `callback` property to the `execute` options:

```js
scriptManager.execute({
		script: "return 'Jan';"
	}, {
		execModulePath: path.join(__dirname, "script.js"),
	    callback: function(argA, argB, cb) {
		    cb(null, "foo");
	    }
	}, function(err, res) {
		console.log(res);
	});
```

Then in the wrapper you can for example offer a function `funcA` to the users script which uses callback parameter to contact the original caller.

```js
module.exports = function(inputs, callback, done) {
	var result = require('vm').runInNewContext(inputs.script, {
		require: function() { throw new Error("Not supported"); },
		funcA: function(argA, cb) {
			callback(argA, cb);
		}
	});
	done(result);
});
```

## Options

```js
var scriptManager = require("script-manager")({
 		/* number of worker node.js processes */
		numberOfWorkers: 2,
		/* set a custom hostname on which script execution server is started, useful is cloud environments where you need to set specific IP */
		host: '127.0.0.1',
		/* set a specific port range for script execution server */
		portLeftBoundary: 1000,
		portRightBoundary: 2000,
		/* maximum size of message sent/received from/to worker in http-server strategy, pass -1 to have no limit*/
		inputRequestLimit: 200e6,
		/* switch to use dedicated process for script evalution, this can help with
		some issues caused by corporate proxies */
		strategy: "http-server | dedicated-process | in-process",
		/* options passed to forked node worker process: { execArgv: ['�-max-old-space-size=128'] } */
		forkOptions: {}
	});
```


## License
See [license](https://github.com/pofider/node-script-manager/blob/master/LICENSE)

---
_Source: https://npm.io/package/script-manager · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
