Skip to content
This repository has been archived by the owner on Jan 3, 2023. It is now read-only.
/ easy-vm Public archive

Running scripts using Node's VM made easy

License

Notifications You must be signed in to change notification settings

sentialx/easy-vm

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

39 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

easy-vm

easy-vm is a simple Node.js library which helps with running securely untrusted code with whitelisted Node modules.

Installation

$ npm install easy-vm

Quick example

const EasyVM = require('easy-vm');

const vm = new EasyVM({
  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 EasyVM

An EasyVM can be used to create a sandbox.

new EasyVM(options)

  • options VMOptions
    • console boolean - Whether to enable console in the sandbox or not.
    • sandbox 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

EasyVM.run(code, filename)

  • code string
  • filename string (optional) - Path to which Node's require() relates.

Releases

No releases published

Packages

No packages published