tiny-vm
TypeScript icon, indicating that this package has built-in type declarations

1.2.1 • Public • Published

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.

Readme

Keywords

Package Sidebar

Install

npm i tiny-vm

Weekly Downloads

1

Version

1.2.1

License

MIT

Unpacked Size

92.6 kB

Total Files

22

Last publish

Collaborators

  • ahmedmostafa15