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

contextify

v1.0.0

Published

Turn an object into a persistent execution context.

Downloads

96,063

Readme

UNMAINTAINED

This module is no longer being maintained. If you're interested in becoming a maintainer, please open an issue.

YOU DON'T NEED THIS MODULE ON NODE >= 0.12

As of Node 0.12, Contextify has been merged into Node (thanks to @domenic), replacing the native vm module. The code has been improved on since then, so if at all possible, you should use a newer Node and the native vm module. Note that newer versions of JSDOM no longer depend on Contextify.

The README below applies to Node 0.10 and below.

Contextify

Installation issues

Make sure you have the node-gyp prerequisites installed: https://github.com/nodejs/node-gyp#installation

For Windows issues, see here: https://github.com/brianmcd/contextify/wiki/Windows-Installation-Guide

What is Contextify?

Turn an object into a V8 execution context. A contextified object acts as the global 'this' when executing scripts in its context. Contextify adds 3 methods to the contextified object: run(code, filename), getGlobal(), and dispose(). The main difference between Contextify and Node's vm methods is that Contextify allows asynchronous functions to continue executing in the Contextified object's context. See vm vs. Contextify below for more discussion.

Examples

var Contextify = require('contextify');
var sandbox = { console : console, prop1 : 'prop1'};
Contextify(sandbox);
sandbox.run('console.log(prop1);');
sandbox.dispose(); // free the resources allocated for the context.
var sandbox = Contextify(); // returns an empty contextified object.
sandbox.run('var x = 3;');
console.log(sandbox.x); // prints 3
sandbox.dispose();
var sandbox = Contextify({setTimeout : setTimeout});
sandbox.run("setTimeout(function () { x = 3; }, 5);");
console.log(sandbox.x); // prints undefined
setTimeout(function () {
    console.log(sandbox.x); // prints 3
    sandbox.dispose();
}, 10);

Details

Contextify([sandbox])

sandbox - The object to contextify, which will be modified as described below
          If no sandbox is specified, an empty object will be allocated and used instead.

Returns the contextified object.  It doesn't make a copy, so if you already have a reference
to the sandbox, you don't need to catch the return value.

A Contextified object has 2 methods added to it:

run(code, [filename])

code - string containing JavaScript to execute
filename  - an optional filename for debugging.

Runs the code in the Contextified object's context.

getGlobal()

Returns the actual global object for the V8 context. The global object is initialized with interceptors (discussed below) which forward accesses on it to the contextified object. This means the contextified object acts like the global object in most cases. Sometimes, though, you need to make a reference to the actual global object.

For example:

var window = Contextify({console : console});
window.window = window;
window.run("console.log(window === this);");
// prints false.
var window = Contextify({console : console});
window.window = window.getGlobal();
window.run("console.log(window === this);");
// prints true

The global object returned by getGlobal() can be treated like the contextified sandbox object, except that defining getters/setters will not work on it. Define getters and setters on the actual sandbox object instead.

dispose()

Frees the memory allocated for the underlying V8 context. If you don't call this when you're done, the V8 context memory will leak, as will the sandbox memory, since the context's global stores a strong reference to the sandbox object. You can still use your sandbox object after calling dispose(), but it's unsafe to use a global previously returned from getGlobal(). run, getGlobal, and dispose will be removed from the sandbox object.

Install

npm install contextify

require('vm') vs. Contextify

Node's vm functions (runInContext etc) work by copying the values from the sandbox object onto a context's global object, executing the passed in script, then copying the results back. This means that scripts that create asynchronous functions (using mechanisms like setTimeout) won't have see the results of executing those functions, since the copying in/out only occurs during an explicit call to runInContext and friends.

Contextify creates a V8 context, and uses interceptors (see: http://code.google.com/apis/v8/embed.html#interceptors) to forward global object accesses to the sandbox object. This means there is no copying in or out, so asynchronous functions have the expected effect on the sandbox object.

Tests

Testing is done with nodeunit. Run the tests with

nodeunit test/

Output:

OK: 92 assertions (27ms)

Building

node-gyp rebuild

Acknowledgments

Inspiration taken from Assaf's Zombie.js context solution: https://github.com/assaf/zombie