npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

tasty-sandbox

v2.0.6

Published

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

Downloads

7

Readme

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.
  • sourceTransformers: An object of named functions to transform the source code of the sandboxed module's file (e.g. transpiler language, code coverage).
  • singleOnly: If false, modules that are required by the sandboxed module will not be sandboxed. By default all modules required by the sandboxedModule will be sandboxed using the same options that were used for the original sandboxed module.
  • sourceTransformersSingleOnly: If false, the source transformers will not be run against modules required by the sandboxed module. By default it will take the same value as singleOnly.

SandboxedModule.require(moduleId, [options])

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

SandboxedModule.configure(options)

Sets options globally across all uses of SandboxedModule.load() and SandboxedModule.require(). This way, a commonly needed require, global, local, or sourceTransformer can be specified once across all sandboxed modules.

SandboxedModule.registerBuiltInSourceTransformer(name)

Enables a built-in source transformer by name. Currently, SandboxedModule ships with two built in source transformers:

  • "coffee" - Compiles source with CoffeeScript [Enabled by default for backwards compatibility]. Be sure to run require('coffee-script').register() or require('coffee-script/register') as well.
  • "istanbul" - Instruments sources via istanbul when istanbul code coverage is running.

For example, if you'd like to use SandboxedModule in conjunction with istanbul, just run SandboxedModule.registerBuiltInSourceTransformer('istanbul').

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.sourceTransformers

An object of named functions which will transform the source code required with SandboxedModule.require. For example, CoffeeScript & istanbul support is implemented with built-in sourceTransformer functions (see #registerBuiltInSourceTransformer).

A source transformer receives the source (as it's been transformed thus far) and must return the transformed source (whether it's changed or unchanged).

An example source transformer to change all instances of the number "3" to "5" would look like this:

SandboxedModule.require('../fixture/baz', {
  sourceTransformers: {
    turn3sInto5s: function(source) {
      return source.replace(/3/g,'5');
    }
  }
})

License

sandboxed-module is licensed under the MIT license.