I'm looking for a way to efficiently create a Node vm sandbox context which
can be quickly reused for the purposes of running unsafe code sequentially.
In my tests, setting-up and disposing of Node vm sandboxes is quite
expensive in CPU and memory. A way around this would be to reuse a default
sandboxed environment. However reusing a vm context causes the globals to
persist. See example below ( adapted from the Node documentation) :
var util = require('util');
var vm = require('vm');
var _ = require('lodash');
var context = { globalVar: 1 };
var sandbox = vm.createContext(context);
// sandbox = _.cloneDeep(sandbox) // fails with TypeError: needs a
'context' argument.
console.log('sandbox before', sandbox);
for (var i = 0; i < 10; ++i) {
vm.runInContext('globalVar *= 2;', sandbox);
}
console.log(util.inspect(sandbox));
// { globalVar: 1024 } // The global variable is incremented on each
loop instead of being disposed.
// would like an output of { globalVar: 2 }
I would like a method where the result is globalVar === 2 , ie. it gets
reset on every iteration of the loop.
I even tried a lodash cloneDeep, which so I could reuse the context object,
but no luck.
The goal is to have a cheap reusable clean default sandbox on every run.
That is: it should have a lower cpu and memory profile versus creating a
new context from scratch every time.
Cross posted
here:
http://stackoverflow.com/questions/38903390/reuse-a-node-vm-sandbox-and-delete-the-global-state-updates
--
Job board: http://jobs.nodejs.org/
New group rules:
https://gist.github.com/othiym23/9886289#file-moderation-policy-md
Old group rules:
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 unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/nodejs/3391373e-3aa7-4940-94c6-17b6a0e310ff%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.