node-vm2


Namenode-vm2 JSON
Version 0.4.7 PyPI version JSON
download
home_page
SummaryA binding to Node.js + vm2, helps you execute JavaScript safely.
upload_time2023-10-22 17:29:14
maintainer
docs_urlNone
author
requires_python>=3.7
license
keywords node js sandbox execute javascript
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            node_vm2
========

.. image:: https://readthedocs.org/projects/node-vm2/badge/?version=latest
   :target: http://node-vm2.readthedocs.io/en/latest/?badge=latest
   :alt: Documentation Status
   
.. image:: https://github.com/eight04/node_vm2/actions/workflows/test.yml/badge.svg
   :target: https://github.com/eight04/node_vm2/actions/workflows/test.yml
   :alt: test

.. warning::
   This project is no longer maintained. Please use `deno_vm <https://github.com/eight04/deno_vm>`__ instead.

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

vm2
---

`vm2 <https://github.com/patriksimek/vm2>`__ is a node module to create **real** sandbox in node. The official node API `vm <https://nodejs.org/api/vm.html>`__, 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.

.. code-block::

   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 <https://github.com/patriksimek/vm2>`__.

Simple eval:

.. code-block:: python

   from node_vm2 import eval
   
   print(eval("['foo', 'bar'].join()"))
   
Use VM:

.. code-block:: python

   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:

.. code-block:: python

   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:

.. code-block:: python

   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:

.. code-block:: python

   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.4.6 (Oct 23, 2023)

  - **Change: add deprecation warning.**
  - Update vm2 to 3.9.19.

- 0.4.5 (Sep 1, 2022)

  - Update vm2 to 3.9.11.

- 0.4.4 (Mar 14, 2022)

  - Update vm2 to 3.9.9.

- 0.4.3 (Feb 15, 2022)

  - Update vm2 to 3.9.7.

- 0.4.2 (Feb 9, 2022)

  - Update vm2 to 3.9.6.

  - Fix: filename is optional.

-  0.4.1 (Oct 20, 2021)

   -  Update vm2 to 3.9.5.

-  0.4.0 (Sep 2, 2021)

   -  Update vm2 to 3.9.3.
   -  **Change: throw VMError when failed running node.**

-  0.3.7 (Mar 23, 2020)

   -  Update vm2 to 3.9.0.

-  0.3.6 (Apr 22, 2019)

   -  Update vm2 to 3.8.0. Fix security issues.

-  0.3.5 (Feb 10, 2019)

   -  Update vm2 to 3.6.10. Fix security issues.

-  0.3.4 (Aug 10, 2018)

   -  Update vm2 to 3.6.3. Fix security issues.

-  0.3.3 (Jul 23, 2018)

   -  Fix: don't bundle dev dependencies.

-  0.3.2 (Jul 23, 2018)

   -  Fix: getting a freezed object would crash the server.
   -  Update vm2 to 3.6.2. Fix security issues.

-  0.3.1 (Apr 25, 2017)
   
   -  Add ``command`` arg to ``VMServer``.
   -  Fix: A dead default server is created if process spawning failed.

-  0.3.0 (Apr 23, 2017)

   -  **Change: use event queue to handle console redirects.**
   -  Reconize object thrown by VM which doesn't inherit built-in Error.

-  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
   

            

Raw data

            {
    "_id": null,
    "home_page": "",
    "name": "node-vm2",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.7",
    "maintainer_email": "",
    "keywords": "node,js,sandbox,execute,javascript",
    "author": "",
    "author_email": "eight04 <eight04@gmail.com>",
    "download_url": "https://files.pythonhosted.org/packages/e2/e2/e3abdf5542d4dd7472fddc7b817e3e6268138ccb0249edf358c59b09b709/node_vm2-0.4.7.tar.gz",
    "platform": null,
    "description": "node_vm2\r\n========\r\n\r\n.. image:: https://readthedocs.org/projects/node-vm2/badge/?version=latest\r\n   :target: http://node-vm2.readthedocs.io/en/latest/?badge=latest\r\n   :alt: Documentation Status\r\n   \r\n.. image:: https://github.com/eight04/node_vm2/actions/workflows/test.yml/badge.svg\r\n   :target: https://github.com/eight04/node_vm2/actions/workflows/test.yml\r\n   :alt: test\r\n\r\n.. warning::\r\n   This project is no longer maintained. Please use `deno_vm <https://github.com/eight04/deno_vm>`__ instead.\r\n\r\nA Python 3 to Node.js + vm2 binding, helps you execute JavaScript safely.\r\n\r\nvm2\r\n---\r\n\r\n`vm2 <https://github.com/patriksimek/vm2>`__ is a node module to create **real** sandbox in node. The official node API `vm <https://nodejs.org/api/vm.html>`__, can only create isolate context and doesn't prevent harmful code to damage your computer.\r\n\r\nHow it works\r\n------------\r\n\r\nThe 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.\r\n\r\nInstall\r\n-------\r\n\r\nYou need Node.js.\r\n\r\nhttps://nodejs.org/\r\n\r\nInstall node_vm2 from pypi wheel.\r\n\r\n.. code-block::\r\n\r\n   pip install node_vm2\r\n\r\nAlso make sure you have ``node`` executable in ``PATH``, or you can specify the executable with environment variable ``NODE_EXECUTABLE``.\r\n\r\nAdditionally, you will need ``npm`` to build node_vm2 from source.\r\n\r\nUsage\r\n-----\r\n\r\nMost of the APIs are bound to `vm2 <https://github.com/patriksimek/vm2>`__.\r\n\r\nSimple eval:\r\n\r\n.. code-block:: python\r\n\r\n   from node_vm2 import eval\r\n   \r\n   print(eval(\"['foo', 'bar'].join()\"))\r\n   \r\nUse VM:\r\n\r\n.. code-block:: python\r\n\r\n   from node_vm2 import VM\r\n   \r\n   with VM() as vm:\r\n      vm.run(\"\"\"\r\n         var sum = 0, i;\r\n         for (i = 0; i < 10; i++) sum += i;\r\n      \"\"\")\r\n      print(vm.run(\"sum\"))\r\n      \r\nUse NodeVM:\r\n\r\n.. code-block:: python\r\n\r\n   from node_vm2 import NodeVM\r\n   \r\n   js = \"\"\"exports.greet = name => console.log(`Hello ${name}!`);\"\"\"\r\n   \r\n   with NodeVM.code(js) as module:\r\n      module.call_member(\"greet\", \"John\")\r\n      \r\nIt is possible to do async task with Promise:\r\n\r\n.. code-block:: python\r\n\r\n   from datetime import datetime\r\n   from node_vm2 import NodeVM\r\n\r\n   js = \"\"\"\r\n   exports.test = () => {\r\n      return new Promise(resolve => {\r\n         setTimeout(() => {\r\n            resolve(\"hello\")\r\n         }, 3000);\r\n      });\r\n   };\r\n   \"\"\"\r\n   with NodeVM.code(js) as module:\r\n      print(datetime.now())\r\n      print(module.call_member(\"test\"))\r\n      print(datetime.now())\r\n      \r\nIf 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:\r\n\r\n.. code-block:: python\r\n\r\n   from node_vm2 import VMServer, VM\r\n\r\n   with VMServer() as server:\r\n      with VM(server=server) as vm:\r\n         # now the vm is created in a new server\r\n         print(vm.run(\"1 + 2 + 3\"))\r\n\r\nAPI reference\r\n-------------\r\n\r\nhttp://node-vm2.readthedocs.io/\r\n\r\nChangelog\r\n---------\r\n\r\n- 0.4.6 (Oct 23, 2023)\r\n\r\n  - **Change: add deprecation warning.**\r\n  - Update vm2 to 3.9.19.\r\n\r\n- 0.4.5 (Sep 1, 2022)\r\n\r\n  - Update vm2 to 3.9.11.\r\n\r\n- 0.4.4 (Mar 14, 2022)\r\n\r\n  - Update vm2 to 3.9.9.\r\n\r\n- 0.4.3 (Feb 15, 2022)\r\n\r\n  - Update vm2 to 3.9.7.\r\n\r\n- 0.4.2 (Feb 9, 2022)\r\n\r\n  - Update vm2 to 3.9.6.\r\n\r\n  - Fix: filename is optional.\r\n\r\n-  0.4.1 (Oct 20, 2021)\r\n\r\n   -  Update vm2 to 3.9.5.\r\n\r\n-  0.4.0 (Sep 2, 2021)\r\n\r\n   -  Update vm2 to 3.9.3.\r\n   -  **Change: throw VMError when failed running node.**\r\n\r\n-  0.3.7 (Mar 23, 2020)\r\n\r\n   -  Update vm2 to 3.9.0.\r\n\r\n-  0.3.6 (Apr 22, 2019)\r\n\r\n   -  Update vm2 to 3.8.0. Fix security issues.\r\n\r\n-  0.3.5 (Feb 10, 2019)\r\n\r\n   -  Update vm2 to 3.6.10. Fix security issues.\r\n\r\n-  0.3.4 (Aug 10, 2018)\r\n\r\n   -  Update vm2 to 3.6.3. Fix security issues.\r\n\r\n-  0.3.3 (Jul 23, 2018)\r\n\r\n   -  Fix: don't bundle dev dependencies.\r\n\r\n-  0.3.2 (Jul 23, 2018)\r\n\r\n   -  Fix: getting a freezed object would crash the server.\r\n   -  Update vm2 to 3.6.2. Fix security issues.\r\n\r\n-  0.3.1 (Apr 25, 2017)\r\n   \r\n   -  Add ``command`` arg to ``VMServer``.\r\n   -  Fix: A dead default server is created if process spawning failed.\r\n\r\n-  0.3.0 (Apr 23, 2017)\r\n\r\n   -  **Change: use event queue to handle console redirects.**\r\n   -  Reconize object thrown by VM which doesn't inherit built-in Error.\r\n\r\n-  0.2.0 (Mar 25, 2017)\r\n\r\n   -  **Drop NodeBridge.**\r\n   -  Add VMServer.\r\n   -  **Make all VMs share a default VMServer.**\r\n   -  **Method rename: VM.connect -> VM.create, VM.close -> VM.destroy.**\r\n\r\n-  0.1.0 (Mar 23, 2017)\r\n\r\n   -  First release\r\n   \r\n",
    "bugtrack_url": null,
    "license": "",
    "summary": "A binding to Node.js + vm2, helps you execute JavaScript safely.",
    "version": "0.4.7",
    "project_urls": {
        "Bug Tracker": "https://github.com/eight04/node_vm2/issues",
        "Documentation": "https://node-vm2.readthedocs.io/en/latest/?badge=latest",
        "Homepage": "https://github.com/eight04/node_vm2"
    },
    "split_keywords": [
        "node",
        "js",
        "sandbox",
        "execute",
        "javascript"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "31cfea851e4c8f3ddbeeb3401b67f7a485930a9f9433e9169d1c4237133deef4",
                "md5": "c3143ab15627ce9ada9b5d7062d22348",
                "sha256": "3b2e09319d5da4cb5846b4c065cb06cbfc941fcc237c80023c9c1a65d5e5d457"
            },
            "downloads": -1,
            "filename": "node_vm2-0.4.7-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "c3143ab15627ce9ada9b5d7062d22348",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.7",
            "size": 232831,
            "upload_time": "2023-10-22T17:29:12",
            "upload_time_iso_8601": "2023-10-22T17:29:12.131443Z",
            "url": "https://files.pythonhosted.org/packages/31/cf/ea851e4c8f3ddbeeb3401b67f7a485930a9f9433e9169d1c4237133deef4/node_vm2-0.4.7-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "e2e2e3abdf5542d4dd7472fddc7b817e3e6268138ccb0249edf358c59b09b709",
                "md5": "8ecb27d6f60ee8debcb2ba5c4b44c14d",
                "sha256": "2a5ffa77e6ddeff304fda0b3687bb59856b83ada5a3b7c57e42c579614c292d9"
            },
            "downloads": -1,
            "filename": "node_vm2-0.4.7.tar.gz",
            "has_sig": false,
            "md5_digest": "8ecb27d6f60ee8debcb2ba5c4b44c14d",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.7",
            "size": 217672,
            "upload_time": "2023-10-22T17:29:14",
            "upload_time_iso_8601": "2023-10-22T17:29:14.723396Z",
            "url": "https://files.pythonhosted.org/packages/e2/e2/e3abdf5542d4dd7472fddc7b817e3e6268138ccb0249edf358c59b09b709/node_vm2-0.4.7.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-10-22 17:29:14",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "eight04",
    "github_project": "node_vm2",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "requirements": [],
    "lcname": "node-vm2"
}
        
Elapsed time: 0.12500s