admin管理员组文章数量:1353470
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, becausevm.createContext([sandbox])
won't automatically provide basic things such asconsole
orrequire
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 definedRunning 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 municate 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.
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, becausevm.createContext([sandbox])
won't automatically provide basic things such asconsole
orrequire
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 definedRunning 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 municate 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.
Share Improve this question edited Apr 26, 2016 at 10:26 Oleg asked Apr 26, 2016 at 10:11 OlegOleg 9,3692 gold badges45 silver badges59 bronze badges 3- @dandavis Any type (serializable). – Oleg Commented Apr 26, 2016 at 10:20
-
child_process.exec(mand[, options][, callback])
's callback provides output to a stdOut buffer, so if your sub-processconsole.logs(strJSON)
, you can get it back in the callback. the other good ways are plicated, but you can use HTTP or temp files pretty simply to acplish some tasks... "IPC" – dandavis Commented Apr 26, 2016 at 10:21 - @dandavis I have thought about that, but the code may do its own logging and I would rather not interfere. – Oleg Commented Apr 26, 2016 at 10:23
3 Answers
Reset to default 5You can use advanced vm/sandbox for Node.js
var VM = require('vm2').NodeVM; // https://github./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);
How about just passing the parent's console
in a context?
const vm = require('vm');
var sandbox = {
console: console
};
var context = new vm.createContext(sandbox);
var script = new vm.Script('console.log("foo")');
script.runInContext(context);
In this code, we can send any of the response and can access the global variable with global function and can access the whole data returning from VM Sandbox.
function piler(){
let add = x*y
Return (add);
}
piler();
const vm = new NodeVM({
sandbox: {
Return(data) {
console.log('Data:', data);
},
x : 10,=
y : 20
},
require: {
external: true,
builtin: ["fs", "path"],
root: "./",
mock: {
fs: {
readFileSync() {
return "Nice try!";
}
}
}
}
});
try {
vm.run(req.query.code);
} catch (error) {
console.log("error: ", error);
}
本文标签: javascriptRun js code in a separate context and access its global variableStack Overflow
版权声明:本文标题:javascript - Run js code in a separate context and access its global variable - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743902261a2558889.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论