A few weeks back i built a node like stack to run web apps in a
sandbox.

What this stack did was simulate the environment or location of the
app. So if my app.js file saved to “/home/stack/myapp/app.js” then
__dirname would be just “/” as in the root of the file system.
The problem you have it that the commonjs module system is locked into
node very dearly. To get around this you need to “simulate”
module.require you been given so you have the right __dirname for
where you are requiring it.


If you have ever played with the cydia stack from heroku you will see
that you app is running in a sandbox. Now what heroku does with there
sand-boxing is a lot more but you get the idea.


Now what i have here are 4 files. main.js, app.js, evn.js and
modules.js.

main.js:
https://gist.github.com/1836771
app.js:
https://gist.github.com/1836762
evn.js:
https://gist.github.com/1836764
modules.js:
https://gist.github.com/1836767

main.js shows how to start the “app”.
evn.js is how it requires each module.
app.js is the context loader.
modules.js well you can see what it does.

On Feb 14, 7:30 pm, Tomasz Janczuk <tom...@janczuk.org> wrote:
> Is there a way to control module resolution logic ('require') for code
> executing in a child context AS WELL AS any other further modules
> imported by that code?
>
> Defining 'require' function in the sandbox passed to a new script
> context created though the vm module allows me to intercept module
> resolution for the immediate code executing in that script context:
>
> function myRequire(name) {
>   var m = require(name);
>   //... my custom code here
>   return m;
>
> }
>
> vm.runInNewContext('var a = require("./a.js")', {require: myRequire});
>
> Given the code above, myRequire is called to resolve './a.js'.
> However, if a.js contains the following:
>
> var b = require('b')
>
> the myRequire is no longer called for 'b'.

-- 
Job Board: http://jobs.nodejs.org/
Posting guidelines: 
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to nodejs@googlegroups.com
To unsubscribe from this group, send email to
nodejs+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en

Reply via email to