I would like to run a third-party JavaScript file (I don't have much control over its contents) in node and access a global variable created by that file's code in its context.
There are two things I've considered:
Running the code in a vm
sandbox. The problem is that I don't know how to properly create the context, because vm.createContext([sandbox])
won't automatically provide basic things such as console
or require
or whatever to the script I want to run.
This is a bit of a bummer, because the documentation explicitly states (emphasis mine):
If given a sandbox object, will "contextify" that sandbox so that it can be used in calls to vm.runInContext() or script.runInContext(). Inside scripts run as such, sandbox will be the global object, retaining all its existing properties but also having the built-in objects and functions any standard global object has.
What are "the built-in objects and functions any standard global object has"? I'm naively assuming it's things like console
, process
, require
, etc. But if so, the API doesn't work, because those are not set. I'm probably misunderstanding something here.
var sandbox = vm.createContext({foo: 'foo'});
var code = 'console.log(foo);';
vm.runInContext(code, sandbox);
Which results in:
evalmachine.:1
console.log(foo);
^
ReferenceError: console is not defined
Running the code in a child process. But I can't find any documentation on accessing global variables of child processes. I'm assuming the only way to communicate with a child process is by message passing, but even that seems to be from parent to child, not the other way round...
Basically, I'm stuck. Halp.
You can use advanced vm/sandbox for Node.js
var VM = require('vm2').NodeVM; // https://github.com/patriksimek/vm2#nodevm
var options = {
console: 'inherit',
sandbox: {
foo: 'foo'
}
}
vm = new VM(options);
var code = `
console.log(foo);
oldFoo = foo;
foo = Math.random();
`;
vm.run(code);
console.log(vm.context.oldFoo, vm.context.foo);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With