Giter Club home page Giter Club logo

node-sandboxed-module's Introduction

sandboxed-module

Build Status

A sandboxed node.js module loader that lets you inject dependencies into your modules.

Installation

npm install sandboxed-module

Usage

var SandboxedModule = require('sandboxed-module');
var user = SandboxedModule.require('./user', {
  requires: {'mysql': {fake: 'mysql module'}},
  globals: {myGlobal: 'variable'},
  locals: {myLocal: 'other variable'},
});

What to do with this

This module is intended to ease dependency injection for unit testing. However, feel free to use it for whatever crimes you can think of.

API

SandboxedModule.load(moduleId, [options])

Returns a new SandboxedModule where moduleId is a regular module path / id as you would normally pass into require(). The new module will be loaded in its own v8 context, but otherwise have access to the normal node.js environment.

options is an optional object that can be used to inject any of the following:

  • requires: An object containing moduleIds and the values to inject for them when required by the sandboxed module. This does not affect children of the sandboxed module.
  • globals: An object of global variables to inject into the sandboxed module.
  • locals: An object of local variables to inject into the sandboxed module.

SandboxedModule.require(moduleId, [options])

Identical to SandboxedModule.load(), but returns sandboxedModule.exports directly.

sandboxedModule.filename

The full path to the module.

sandboxedModule.module

The underlaying node.js Module instance.

sandboxedModule.exports

A getter returning the sandboxedModule.module.exports object.

sandboxedModule.globals

The global object of the v8 context this module was loaded in. Modifications to this object will be reflected in the sandboxed module.

sandboxedModule.locals

The local variables injected into the sandboxed module using a closure. Modifying this object has no effect on the state of the sandbox.

sandboxedModule.required

An object holding a list of all module required by the sandboxed module itself. The keys are the moduleIds used for the require calls.

sandboxedModule.getGlobalLeaks()

Returns an array of variable names that have leaked into the global scope, except those those that are part of the node environment, or have been injected using the globals: option.

License

sandboxed-module is licensed under the MIT license.

node-sandboxed-module's People

Contributors

felixge avatar ctavan avatar

Stargazers

Niall O'Higgins avatar

Watchers

Niall O'Higgins avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.