Skip to content

Commit

Permalink
Implement require() function
Browse files Browse the repository at this point in the history
  • Loading branch information
felixge committed Jun 29, 2011
1 parent deb32c8 commit bd2b997
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
9 changes: 7 additions & 2 deletions Readme.md
Expand Up @@ -13,11 +13,11 @@ npm install sandboxed-module

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

## What to do with this
Expand All @@ -43,6 +43,11 @@ following:
* `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.
Expand Down
9 changes: 7 additions & 2 deletions lib/sandboxed_module.js
Expand Up @@ -18,15 +18,20 @@ function SandboxedModule() {
this._options = {};
}

SandboxedModule.load = function(moduleId, options) {
var trace = stackTrace.get(SandboxedModule.load);
SandboxedModule.load = function(moduleId, options, trace) {
trace = trace || stackTrace.get(SandboxedModule.load);

var injectableModule = new SandboxedModule();
injectableModule._init(moduleId, trace, options);

return injectableModule;
};

SandboxedModule.require = function(moduleId, options) {
var trace = stackTrace.get(SandboxedModule.require);
return this.load(moduleId, options, trace).exports;
};

SandboxedModule.prototype.getGlobalLeaks = function() {
var self = this;
return Object.keys(this.globals).filter(function(variable) {
Expand Down

0 comments on commit bd2b997

Please sign in to comment.