Evaluate arbitrary JavaScript from Python using a NodeJS sidecar
Project description
NodeJS Eval
Evaluate arbitrary JavaScript from Python, using a NodeJS sidecar process.
This combines:
- The Python
nodejs-bin
project, which bundles NodeJS (withnpm
andnpx
) into a PyPI package. - The JavaScript/TypeScript
http-eval
project, which runs a JavaScript evaluation server on a Unix domain socket.
Table of Contents
Installation
pip install nodejs-eval
License
nodejs-eval
is distributed under the terms of the
MIT license.
Usage instructions
Basic synchronous call
JavaScript code is evaluated as a function body within a synchronous or asynchronous JavaScript function, within an ECMAScript module.
Note that the NodeJS evaluator always only supports async
mode on the Python
side. However, the supplied JavaScript code can be either sync or async.
async with evaluator() as e:
result = await e.run("return 6*7;")
assert result == 42
Basic asynchronous call
async with evaluator() as e:
result = await e.run_async(
"""
await new Promise(r => setTimeout(r, 2000));
return 6*7;
""")
assert result == 42
Storing state on this
Evaluations run with a consistent JavaScript this
context, so state can be
stored on it:
async with evaluator() as e:
await e.run("this.x = 6*7;")
result = await e.run("return this.x;")
assert result == 42
Import using dynamic await import()
Because code is executed within an ECMAScript module, you can use
the dynamic async import()
to import other modules.
This is easiest done in async
mode:
async with evaluator() as e:
result = await e.run_async(
"""
const os = await import("os");
return os.cpus();
"""
)
assert len(result) > 0
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
Built Distribution
Hashes for nodejs_eval-0.0.2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b32cd6362d79877c153370b8dfad71cd851d4b4bfefa7c00b73c6264cac0814e |
|
MD5 | 77bcc337fc538b49a0186080d5ba11d7 |
|
BLAKE2b-256 | f7e5324661c3721803ba5f6af7c0439b0d7ba1fca241a1acb80a5aa2509f2c67 |