JSPM

sandbox-runner

0.1.1
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • 0
  • Score
    100M100P100Q31007F
  • License MIT

Run javascript in a sandbox context

Package Exports

  • sandbox-runner

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (sandbox-runner) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

sandbox-runner NPM version Build Status Dependency Status

Eval a piece of javascript text in a sandbox environment Could be useful to test code generation or test javascript without proper module encapsulation.

Install

Install using npm.

$ npm install sandbox-runner

Usage

var fs = require('fs');
var sandbox = require('sandbox-runner');

var codeSnippet = fs.readFileSync('./templates.js', {encoding:'utf8'});

/* Content of templates.js
this.Templates = this.Templates != null ? this.Templates : {};
this.Templates["hello"] = function (name) {
            return 'hello ' + name + '!';
          };
this.Templates["foo"] = function () {
            return 'foo';
          };
*/

var context = sandbox.run(codeSnippet);

context.Templates.hello('world').should.equal('hello world !');

API

sanbox.run(script, context = {})

run the script snippet, a context is provided as this pointer.

  • script The script text to be executed
  • context The context that used in execution. An empty object will be provide if omitted.

** HINT ** Exception is thrown when error occurs in script. Remember to catch the exception in async code.

sandbox.moduleRun(script, filename = '')

run the script snippet as a node.js module

  • script The script text to be executed
  • filenae The fake file name that of the module in file system.

License

MIT

NPM downloads