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

scoper

v1.0.0

Published

modify nested scope at runtime

Downloads

11

Readme

scoper

modify nested scope at runtime

build status

example

modify

Given this simple source:

var x = 3;

setInterval(function () {
    console.log(100 * x);
}, 500);

we can modify the values of the variable x and the constant 100 at runtime:

var fs = require('fs');
var scoper = require('scoper');

var src = fs.readFileSync(__dirname + '/source/simple.js', 'utf8');
var c = Function('return ' + scoper(src))();
c.run();

setInterval(function () {
    c.literal['arguments.0'][0] += 10;
}, 500);

setTimeout(function () {
    c.scope[''].x = 11;
}, 2000);

$ node example/simple.js
300
330
360
390
1540
1650
1760
1870
1980
2090
^C

attributes

Given some source:

var x = 5;

function foo (n) {
    var y = n + x + 100;
    
    return (function bar () {
        var z = 6;
        var f = function () {
            var q = y * 10;
            var x = z + 2;
            return q + x;
        };
        return f(z);
    })();
}

setInterval(function () {
    console.log(foo(32));
}, 500);

the attribute object looks like:

$ node example/attr
{ scope: 
   { '': {},
     'body.1': {},
     'body.1.callee': {},
     'body.1.callee.init': {},
     'body.1.callee.right': {},
     'arguments.0': {} },
  function: 
   { 'body.1.callee.right': [Function],
     'body.1.callee': [Function: bar],
     'body.1': [Function: foo],
     'arguments.0': [Function] },
  literal: 
   { '': [ 5, 500 ],
     'body.1': [ 100 ],
     'body.1.callee': [ 6 ],
     'body.1.callee.init': [ 10, 2 ],
     'arguments.0': [ 32 ] },
  patch: [Function],
  run: [Function] }

methods

var scoper = require('scoper')

var newSrc = scoper(src, opts)

Return a string that rewrites src to add scoping instrumentation hooks.

Optionally:

opts.names - an object mapping scope, function, and literal keys to internal ids to use for names in the rewritten source. By default, these names are randomly generated.

var c = Function('return ' + newSrc)()

Create a new scoper context c from the javascript source string newSrc returned by scoper().

c.run()

Run the source. You can run the source as many times as you like. You can modify the scope attributes before, after, or during a run.

c.patch(ctx)

Patch the current context with a new context ctx. ctx should have one or more attributes ctx.scope, ctx.function and ctx.literal to replace the internal attributes of the current object.

attributes

c.scope

All the variables are kept in this object.

c.function

Copies of all functions are kept here so you can modify the functions at runtime.

c.literal

The literal number, string, boolean, and regex values are stored in this object.

install

With npm do:

npm install scoper

license

MIT