2.1.5 • Published 2 years ago

vm2-process v2.1.5

Weekly downloads
-
License
MIT
Repository
-
Last release
2 years ago

vm2-process

Run untrusted code via vm2, but inside a separate process which has additional restrictions: 1. Limit how much of a CPU can be used 2. Limit how much memory can be used 3. Limit how much time it can take (even if blocked by sync code)

Installation

npm install --save vm2-process

API

The createVm2Pool (default export) accepts the following options:

TitleKeyDefault
Min Threadsmin-
Max Threadsmax-
CPUcpu100 percent
Memorymemory2000 megabytes
Execution Timetime1000 milliseconds

It will return a run function that takes two arguments: run(code, scope)

code is a string of JavaScript code. scope is an object, of which will be globally accessible during execution.

Note: Communication is done via a unix socket, and therefore the scope, and result from the execution needs to be JSON serializable.

Usage

Simple usage with only code

import createVm2Pool from 'vm2-process';

const { run, drain } = createVm2Pool({ min: 1, max: 3 });
const result = await run('1 + 1');

console.log(result) // prints '2'

drain();

Simple usage with some scope

import createVm2Pool from 'vm2-process';

const { run, drain } = createVm2Pool({ min: 1, max: 3 });
const result = await run('1 + a', { a: 2 })

console.log(result) // prints '3'

drain();

Simple usage with some limits

import createVm2Pool from 'vm2-process';

const { run, drain } = createVm2Pool({
  min: 1, /* min threads in the pool */
  max: 3, /* max threads in the pool */
  cpu: 100, /* in percent */
  memory: 2000, /* in megabytes */
  time: 1000 /* in milliseconds */
});
const result = await run('while (true) {}', null);

// above throws as it either takes too long or exceeds the memory limit
drain();
2.1.2

2 years ago

2.1.4

2 years ago

2.1.3

2 years ago

2.1.5

2 years ago

2.1.1

3 years ago

2.1.0

3 years ago

2.0.0

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago