1.2.1 • Published 5 years ago

tiny-vm v1.2.1

Weekly downloads
1
License
MIT
Repository
-
Last release
5 years ago

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 VMOptions
    • console boolean - Whether to enable console in the sandbox or not.
    • sanbox object - A global object in VM
    • require 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 string
  • filename string (optional) - Path to which Node's require() relates.