Questions tagged [nodevm]

12 questions
6
votes
2 answers

Is it possible to make all of Node.js Globals available in Node's VM context?

Consider the following code: vm = require('vm'); context = vm.createContext({}); vm.runInContext("Buffer.from('abc').toString()", context); Observe that this produces ReferenceError: Buffer is not defined as Buffer is a Node.js specific construct…
Jacob Horbulyk
  • 2,366
  • 5
  • 22
  • 34
6
votes
1 answer

NodeJS vm use cases

I am confused about the use cases of the VM module in NodeJS. After reading a little bit in the documentations about this module is just looks like a fancy way to do eval. Anyone used it and lived to tell the tail about it use cases?
Amit Wagner
  • 3,134
  • 3
  • 19
  • 35
5
votes
2 answers

How to get the result of the code which is run by Node vm2

Recently, i have been trying to implement sandbox execution using the package vm2 which has be published by @Patrik Šimek I am trying to run some js code, which i am considering it to be a custom Logic, i am storing this logic in a string…
Nishi Bangar
  • 720
  • 9
  • 20
3
votes
0 answers

Node VM and async functions

Consider the following node code that invokes an async function with node vm: const vm = require('vm'); const ctx = new vm.createContext({ console: console, foo: function(){ return new Promise(r => {console.log(2); r()}) …
Gleno
  • 16,621
  • 12
  • 64
  • 85
2
votes
0 answers

VM2 Usage with Webpack

I've been having trouble using webpack with a typescript project that uses vm2.webpack --config webpack.config.js gives the following error: ERROR in index.js from Terser Invalid function parameter…
WeffJen
  • 70
  • 4
2
votes
1 answer

How to provide source map in Nodejs VM

I am evaluating javascript bundled source code in nodejs using the below code const javascriptCode = "..." const javascriptSourceMap = "..." const wrapper = NativeModule.wrap(javascriptCode); const script = new vm.Script(wrapper, { …
ZaidRehman
  • 1,631
  • 2
  • 19
  • 30
2
votes
1 answer

Combine NodeJS Fibers + VM Sandbox

I want to run some untrusted code in Node that might look like this: for (var i = 0; i < 5; i++){ green_led(1); sleep(500); green_led(0); sleep(500); } Using Fibers, I got the synchronous behaviour working as expected: var Fiber =…
Jodes
  • 14,118
  • 26
  • 97
  • 156
0
votes
0 answers

How to link to a SourceTextModule without importing it in the first place?

I've found here that imported modules are accepted as SyntheticModule()-s when linking a SourceTextModule(), but i've managed to get it work as SourceTextModule()-s as well, like the documentation describes:…
bpstrngr
  • 339
  • 3
  • 13
0
votes
1 answer

Reference error when trying to return a string Nodejs VM

I'm using a npm module called safe-eval to evaluate some JavaScript code i send to the backend. The code is usually a function that will be executed inside of the safe-eval with the parameters I'll provide. The problem is that when returning certain…
0
votes
1 answer

Nodejs vm2 - how to import scripts

Suppose you have script A: defines a library with: class MathFunctions { add(a, b) => { return a+b; } } And you have script B: reuses MathFunctions somehow: const mf = import MathFunctions; console.log(mf.add(2, 2)); And you want to execute B in…
Robert Christian
  • 18,218
  • 20
  • 74
  • 89
0
votes
2 answers

Halt the promise chain or stop the runInNewContext returning promise chain

I am facing an issue with the vm while executing the code using runInNewContext. The custom code can return a promise which might be nested and can have nested call stacks. The code looks like follow function executeInSandbox(code, sandbox){ …
jbmyid
  • 1,985
  • 19
  • 22
-1
votes
1 answer

How to make the `import` statement work properly inside the code created using the node:vm module?

In our project we have the custom console object which is used as a logger. So we need to replace the default console. For this purpose we decided to use the vm module. But we are facing the problem while using imports inside the code created with…
pospolitaki
  • 313
  • 2
  • 9