Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 如何获取节点vm2运行的代码的结果_Node.js_Node Vm2_Nodevm - Fatal编程技术网

Node.js 如何获取节点vm2运行的代码的结果

Node.js 如何获取节点vm2运行的代码的结果,node.js,node-vm2,nodevm,Node.js,Node Vm2,Nodevm,最近,我一直在尝试使用@PatrikŠimek发布的包vm2实现沙盒执行 我试图运行一些js代码,我认为它是一个自定义逻辑,我将这个逻辑存储在一个字符串变量中 我需要在一个沙箱环境中执行这个自定义逻辑(因为这是不可信的代码),并在实际环境中获取响应,以基于此结果继续正常的应用程序流 我尝试了几种方法以得到最终结果。自定义逻辑正在沙盒内成功执行,但我无法找到将此结果发送回主进程的方法,但我得到的结果为未定义。因此,到目前为止没有任何进展 希望我在这里得到一些答案 自定义逻辑(将存储在字符串中) 实

最近,我一直在尝试使用@PatrikŠimek发布的包vm2实现沙盒执行

我试图运行一些js代码,我认为它是一个自定义逻辑,我将这个逻辑存储在一个字符串变量中

我需要在一个沙箱环境中执行这个自定义逻辑(因为这是不可信的代码),并在实际环境中获取响应,以基于此结果继续正常的应用程序流

我尝试了几种方法以得到最终结果。自定义逻辑正在沙盒内成功执行,但我无法找到将此结果发送回主进程的方法,但我得到的结果为未定义。因此,到目前为止没有任何进展

希望我在这里得到一些答案

自定义逻辑(将存储在字符串中)

实际实施

// vm2 
 const {NodeVM} = require('vm2');
        const vm = new NodeVM({
            console: 'inherit',
            sandbox: {},
            require: {
                external: true,
                builtin: ['fs','path'],
                root: "./",
                mock: {
                    fs: {
                        readFileSync() { return 'Nice try!';}
                    }
                },
                wrapper : ""
            }
        });


// Sandbox function
let functionInSandbox = vm.run("module.exports = function(customLogic){
                                    customLogic //need to execute this custom logic 
                               });



// Make a call to execute untrusty code by passing it as an argument
// to sandbox environment and obtain the result 

var resultOfSandbox =  functionInSandbox(customLogic);
console.log("Result of Sandbox  :");
console.log(resultOfSandbox); // undefined (need to get the result of custom logic execution)

您需要定义一个沙盒变量。声明一个空对象,将其附加到沙盒选项,并在脚本中向对象添加另一个属性。我想一段代码片段可以说明问题:

const c = `
function addValues(a,b){
  var c = a + b;
  console.log('Addition of 2 values');
  console.log(c); 
  return c;
}

// we'll define ext as a sandbox variable, so it will be available
ext.exports = addValues(10,10); // function call
`;

let ext = {};
const { NodeVM } = require( 'vm2' );
const vm = new NodeVM( {
  console: 'inherit',
  // pass our declared ext variable to the sandbox
  sandbox: { ext },
  require: {
    external: true,
    builtin: ['fs', 'path'],
    root: './',
  },
} );

// run your code and see the results in ext
vm.run( c, 'vm.js' );
console.log( ext );

感谢您的回复。您的建议对于检索执行结果非常有效。但是,在我的例子中,自定义逻辑是动态获取的,因此不能修改。例如:“无法按照建议将沙盒变量(ext)分配给函数调用”(即:ext.exports=addValues(10,10);)。有没有其他方法可以让我按原样执行逻辑并检索其结果?
const c = `
function addValues(a,b){
  var c = a + b;
  console.log('Addition of 2 values');
  console.log(c); 
  return c;
}

// we'll define ext as a sandbox variable, so it will be available
ext.exports = addValues(10,10); // function call
`;

let ext = {};
const { NodeVM } = require( 'vm2' );
const vm = new NodeVM( {
  console: 'inherit',
  // pass our declared ext variable to the sandbox
  sandbox: { ext },
  require: {
    external: true,
    builtin: ['fs', 'path'],
    root: './',
  },
} );

// run your code and see the results in ext
vm.run( c, 'vm.js' );
console.log( ext );