Git Product home page Git Product logo

deno_vm's Introduction

deno_vm

Documentation Status test

A Python 3 to Deno + worker-vm binding, helps you execute JavaScript safely.

How it works

The module launches a Deno 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 vm, the result is sent back to Python.

Install

Install Deno

Follow the instruction here: https://docs.deno.com/runtime/manual/getting_started/installation

Also make sure deno command works as expected: https://docs.deno.com/runtime/manual/getting_started/installation#testing-your-installation

Install deno_vm

pip install deno_vm

Usage

Most of the APIs are bound to worker-vm.

Simple eval:

from deno_vm import eval

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

Use VM:

from deno_vm import VM

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

It is possible to do async task with Promise:

from datetime import datetime
from deno_vm import VM

js = """
function test() {
   return new Promise(resolve => {
      setTimeout(() => {
         resolve("hello")
      }, 3000);
   });
};
"""
with VM() as vm:
   vm.run(js)
   print(datetime.now())
   print(vm.call("test"))
   print(datetime.now())

API reference

http://deno_vm.readthedocs.io/

Changelog

  • 0.6.0 (Mar 4, 2024)
    • Change: use --unstable-worker-options instead of --unstable.
    • Change: vendor deno dependencies. Now deno_vm doesn't require network and filesystem write access.
    • Fix: suppres cleanup error.
    • Fix: improve uninitialized error message.
  • 0.5.1 (Oct 10, 2023)
    • Fix: unable to pass initial code to VM().
  • 0.5.0 (Oct 10, 2023)
    • Switch to deno_vm.

deno_vm's People

Contributors

eight04 avatar

Stargazers

Manuel García-Amado  avatar 伏毅 avatar 0xff avatar color avatar 周祺皓 avatar  avatar Chen avatar Faded avatar  avatar wvengen avatar  avatar shadow443 avatar wangzhibo avatar  avatar

Watchers

 avatar  avatar

deno_vm's Issues

Allow running without write access to site-packages

When running a program using deno_vm without a writable filesystem, it errors out.
First DENO_DIR needs to be pointing to a writable location, that is easy enough (but would be good to document).
Then the lock file cannot be written, which is in the Python package, often this is not writable (e.g. when installed system-wide).
As the lock file is checked in, I don't think writing it is actually needed.

Example:

# Dockerfile
FROM denoland/deno:debian-1.40.4 as deno

FROM python:3.11-slim as python
COPY --from=deno /usr/bin/deno /usr/bin/

RUN pip3 install deno-vm

USER nobody
CMD ["python3", "-c", "from deno_vm import eval; print(eval('hello from deno'))"]
docker build -t dt .
docker run --rm dt

shows

Download https://deno.land/[email protected]/streams/mod.ts
Download https://deno.land/x/[email protected]/mod.ts
error: Permission denied (os error 13) (for '/nonexistent/.cache/deno/deps/https/deno.land')
Check the permission of the directory.
    at file:///usr/local/lib/python3.11/site-packages/deno_vm/vm-server/index.js:1:32

and

docker run --rm -e DENO_DIR=/tmp dt

show

Download https://deno.land/[email protected]/streams/mod.ts
Download https://deno.land/x/[email protected]/mod.ts
Download https://deno.land/[email protected]/streams/buffer.ts
Download https://deno.land/[email protected]/streams/byte_slice_stream.ts
Download https://deno.land/[email protected]/streams/copy.ts
Download https://deno.land/[email protected]/streams/delimiter_stream.ts
Download https://deno.land/[email protected]/streams/early_zip_readable_streams.ts
Download https://deno.land/[email protected]/streams/iterate_reader.ts
Download https://deno.land/[email protected]/streams/limited_bytes_transform_stream.ts
Download https://deno.land/[email protected]/streams/limited_transform_stream.ts
Download https://deno.land/[email protected]/streams/merge_readable_streams.ts
Download https://deno.land/[email protected]/streams/read_all.ts
Download https://deno.land/[email protected]/streams/readable_stream_from_reader.ts
Download https://deno.land/[email protected]/streams/reader_from_iterable.ts
Download https://deno.land/[email protected]/streams/reader_from_stream_reader.ts
Download https://deno.land/[email protected]/streams/text_delimiter_stream.ts
Download https://deno.land/[email protected]/streams/text_line_stream.ts
Download https://deno.land/[email protected]/streams/to_array_buffer.ts
Download https://deno.land/[email protected]/streams/to_blob.ts
Download https://deno.land/[email protected]/streams/to_json.ts
Download https://deno.land/[email protected]/streams/to_text.ts
Download https://deno.land/[email protected]/streams/to_transform_stream.ts
Download https://deno.land/[email protected]/streams/writable_stream_from_writer.ts
Download https://deno.land/[email protected]/streams/write_all.ts
Download https://deno.land/[email protected]/streams/writer_from_stream_writer.ts
Download https://deno.land/[email protected]/streams/zip_readable_streams.ts
Download https://deno.land/[email protected]/assert/assert.ts
Download https://deno.land/[email protected]/bytes/copy.ts
Download https://deno.land/[email protected]/streams/_common.ts
Download https://deno.land/[email protected]/bytes/concat.ts
Download https://deno.land/[email protected]/async/deferred.ts
Download https://deno.land/[email protected]/io/buffer.ts
Download https://deno.land/[email protected]/types.d.ts
Download https://deno.land/[email protected]/assert/assertion_error.ts
error: Failed writing lockfile.

Caused by:
    Permission denied (os error 13)

Allow running without network access

I came to deno_vm as an alternative for the now unmaintained node_vm2. Something that is really unexpected for me, is that additional dependencies seem to be downloaded at run-time. Even with cryptographic hashes (so at least tampering with those files would result in an error), this is something I would not really be comfortable with in a production system.
Apart from that, there are cases where network access is not available, and this package would seem to break completely.

Hence the question: could deno be run with completely no network access?
Could the necessary dependencies be downloaded in the Python package, e.g. with a custom DENO_DIR containing these files?

This can be tested as follows.

# Dockerfile
FROM denoland/deno:debian-1.40.4 as deno

FROM python:3.11-slim as python
COPY --from=deno /usr/bin/deno /usr/bin/

RUN pip3 install deno-vm

CMD ["python3", "-c", "from deno_vm import eval; print(eval('hello from deno'))"]
docker build -t dt .
docker run --rm --network=none dt

shows

Download https://deno.land/[email protected]/streams/mod.ts
Download https://deno.land/x/[email protected]/mod.ts
error: Import 'https://deno.land/[email protected]/streams/mod.ts' failed: error sending request for url (https://deno.land/[email protected]/streams/mod.ts): error trying to connect: dns error: failed to lookup address information: Temporary failure in name resolution
    at file:///usr/local/lib/python3.11/site-packages/deno_vm/vm-server/index.js:1:32

atexit hook no longer works with Python 3.12

Exception ignored in atexit callback: <function close at 0x000001D6D1F222A0>
Traceback (most recent call last):
  File "C:\Users\eight04\dev\ComicCrawler\.venv\Lib\site-packages\deno_vm\__init__.py", line 43, in close
    DEFAULT_BRIDGE.close()
  File "C:\Users\eight04\dev\ComicCrawler\.venv\Lib\site-packages\deno_vm\__init__.py", line 306, in close
    self.process.communicate()
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.12_3.12.496.0_x64__qbz5n2kfra8p0\Lib\subprocess.py", line 1209, in communicate
    stdout, stderr = self._communicate(input, endtime, timeout)
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.12_3.12.496.0_x64__qbz5n2kfra8p0\Lib\subprocess.py", line 1610, in _communicate
    self.stdout_thread.start()
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.12_3.12.496.0_x64__qbz5n2kfra8p0\Lib\threading.py", line 992, in start
    _start_new_thread(self._bootstrap, ())
RuntimeError: can't create new thread at interpreter shutdown

https://docs.python.org/3/library/atexit.html#atexit.register

Changed in version 3.12: Attempts to start a new thread or os.fork() a new process in a registered function now leads to RuntimeError.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.