tiny-vm
v1.2.1
Published
Run securely untrusted code with whitelisted Node modules
Downloads
7
Readme
tiny-vm
tiny-vm
is a simple Node.js library which helps with running securely untrusted code with whitelisted Node modules.
Installation
$ npm install tiny-vm
Quick example
const TinyVM = require('tiny-vm');
const vm = new TinyVM({
console: true,
sandbox: {
test: 'A test variable'
},
require: {
builtin: ['fs'],
mock: {
fs: {
readFile: (path: string) => {
console.log("Nice try!");
}
}
}
}
});
vm.run(`
const fs = require('fs');
fs.readFile(''); // Outputs: Nice try!
console.log(test); // Outputs: A test variable
`);
Documentation
Class TinyVM
An TinyVM
can be used to create a sandbox.
new TinyVM(options)
options
VMOptionsconsole
boolean - Whether to enable console in the sandbox or not.sanbox
object - A global object in VMrequire
VMRequireOptions | false - False to disable require or object to enable require with options.builtin
string[] - Array of allowed builtin modules, Use['*']
to accept all.mock
object - Collection of mocked Node modules.
Methods
TinyVM.run(code, filename)
code
stringfilename
string (optional) - Path to which Node'srequire()
relates.