Latest Socket ResearchMalicious Chrome Extension Performs Hidden Affiliate Hijacking.Details
Socket
Book a DemoInstallSign in
Socket

dslify

Package Overview
Dependencies
Maintainers
1
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

dslify

Rewrites functions or modules in terms of a DSL

Source
npmnpm
Version
0.3.0
Version published
Maintainers
1
Created
Source

dslify

Rewrites a JavaScript function, such that any global property access is transformed to call a member of a new dsl argument. Use dslify to interpret domain-specific languages without messing about in global scope.

Build Status

Dependency status

Install

npm install dslify

Example

var dslify = require('dslify');

var fn = function() { return shout(word); };
var shouter = dslify.transform(fn);

var dsl = {
    shout: function(something) {
        return something + "!!";
    },
    word: "unicorns"
};
shouter(dsl); // unicorns!!

Strings Example

Sometimes you might want to operate with strings instead of JavaScript functions. For example if you are generating templates or want to send JavaScript to the client.

var dslify = require('dslify');

var input = "function(input) { return shout(input, globalValue); };";
var output = dslify.transform(input, {asString: true});

output // function(input) { return shout(input, _dsl.globalValue); };

How?

dslify parses functions using esprima, rewriting them as new functions using escodegen.

Hold on, isn't this just a long-winded JavaScript 'with'?

Yes. But 'with' is leaky and dangerous, wheras dslify is like a sandbox because it rewrites access to global scope, e.g:

var dslify = require('dslify');

var dsl = {};

var withWith = function(dsl) {
    with (dsl) {
        y = 'leaks into global!';
    }
};
var withDslify = dslify.transform(function() {
    z = 'global is safe!';
});

withWith(dsl);
withDslify(dsl);

console.log(global.y);  // leaks into global!
console.log(global.z);  // undefined
console.log(dsl.z);     // global is safe!

Isn't it hard to debug dynamically-generated functions?

Yes. And dynamically generating functions is relatively slow, compared to calling functions. Therefore consider transforming functions at build time instead of run time.

License

BSD

Keywords

dsl

FAQs

Package last updated on 22 Feb 2014

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts