Skip to main content

A binding to Node.js + vm2, helps you execute JavaScript safely.

Project description

https://api.codacy.com/project/badge/Grade/fb30c7193b6b43cf818457e3ff23e60c Documentation Status

A Python 3 to Node.js + vm2 binding, helps you execute JavaScript safely.

vm2

vm2 is a node module to create real sandbox in node. The official node API vm, can only create isolate context and doesn’t prevent harmful code to damage your computer.

How it works

The module launchs a Node.js REPL server, which can be communicated with JSON. All JavaScript code are encoded in JSON and sent to the server. After the server executing the code in vm2, the result is sent back to Python.

Install

You need Node.js.

https://nodejs.org/

Install node_vm2 from pypi wheel.

pip install node_vm2

Also make sure you have node executable in PATH, or you can specify the executable with environment variable NODE_EXECUTABLE.

Additionally, you will need npm to build node_vm2 from source.

Usage

Most of the APIs are bound to vm2.

Simple eval:

from node_vm2 import eval

print(eval("['foo', 'bar'].join()"))

Use VM:

from node_vm2 import VM

with VM() as vm:
   vm.run("""
      var sum = 0, i;
      for (i = 0; i < 10; i++) sum += i;
   """)
   print(vm.run("sum"))

Use NodeVM:

from node_vm2 import NodeVM

js = """exports.greet = name => console.log(`Hello ${name}!`);"""

with NodeVM.code(js) as module:
   module.call_member("greet", "John")

It is possible to do async task with Promise:

from datetime import datetime
from node_vm2 import NodeVM

js = """
exports.test = () => {
   return new Promise(resolve => {
      setTimeout(() => {
         resolve("hello")
      }, 3000);
   });
};
"""
with NodeVM.code(js) as module:
   print(datetime.now())
   print(module.call_member("test"))
   print(datetime.now())

If you like to allow the VM to crash your server (e.g. process.exit()), you should create the VM in a separate server so it won’t affect other VMs:

from node_vm2 import VMServer, VM

with VMServer() as server:
   with VM(server=server) as vm:
      # now the vm is created in a new server
      print(vm.run("1 + 2 + 3"))

API reference

http://node-vm2.readthedocs.io/

Changelog

  • 0.2.0 (Mar 25, 2017)

    • Drop NodeBridge.

    • Add VMServer.

    • Make all VMs share a default VMServer.

    • Method rename: VM.connect -> VM.create, VM.close -> VM.destroy.

  • 0.1.0 (Mar 23, 2017)

    • First release

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

node_vm2-0.2.0.tar.gz (8.1 kB view details)

Uploaded Source

Built Distribution

node_vm2-0.2.0-py3-none-any.whl (31.9 kB view details)

Uploaded Python 3

File details

Details for the file node_vm2-0.2.0.tar.gz.

File metadata

  • Download URL: node_vm2-0.2.0.tar.gz
  • Upload date:
  • Size: 8.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for node_vm2-0.2.0.tar.gz
Algorithm Hash digest
SHA256 a4f03d2345d3ee10ce6723ee93df73bb0e5bcf158102b553c0bb194f0e505ef5
MD5 9170bb69dc6795558e3c617ef25d6a98
BLAKE2b-256 0594cfd44638b3b585dedbaa4ee2ecd5c4893dae2f4302b7a29eb3efbcf07180

See more details on using hashes here.

File details

Details for the file node_vm2-0.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for node_vm2-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d7b1b322aeaef7f22df69e1c8fe7aff7b4657c547a7e52422ae4482770e3802b
MD5 4faaa7686a27fd9e69ab2c38aa945646
BLAKE2b-256 11f5195d134fc0b11eb744220f306b28545a464067aadebc754347609d52f4e4

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page