Skip to content

Latest commit

 

History

History
45 lines (40 loc) · 1.51 KB

README.md

File metadata and controls

45 lines (40 loc) · 1.51 KB

node vm sandbox

Build Status Coverage Status Dependency Status

Runs a script string in a vm within a child process.

Example usage:

var sandbox = require('node_vm_sandbox');
sandbox.run({
  script: 'res = "Hello world!"'
}, function(err, res) {
  console.log('Script returned:', res);
});

Usage:

options = {
  script: 'res = "Hello world!"'; // String with JavaScript code. Set `res` with your results.
  env: path.join(__dirname, 'env.js'); // Optional, path to an environment file.
  data: 'abc'; // Optional, will be passed to the environment file.
  timeout: 1000; // Optional, time before the script is terminated, default: 1000.
};
callback = function(err, res) {
  // res will be 'Hello world!'
};
sandbox.run(options, callback);

To illustrate the intended usage of the environment file, this shows how to inject cheerio into the environment:

var cheerio = require('cheerio');

exports.init = function(data) {
  // 'data' is passed from the options
  // This example assumes that data is a HTML page
  return {
    $: cheerio.load(data)
  };
  // Now you will be able to use jQuery inside your script!
};