NodeJS Eval
Evaluate arbitrary JavaScript from Python, using a NodeJS sidecar process.
This combines:
- The Python
nodejs-binproject, which bundles NodeJS (withnpmandnpx) into a PyPI package, and - The JavaScript/TypeScript
http-evalproject, 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.
from nodejs_eval import evaluator
async with evaluator() as e:
result = await e.run("return 6*7;")
assert result == 42
Basic asynchronous call
from nodejs_eval import evaluator
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:
from nodejs_eval import evaluator
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:
from nodejs_eval import evaluator
async with evaluator() as e:
result = await e.run_async(
"""
const os = await import("os");
return os.cpus();
"""
)
assert len(result) > 0
Release files for nodejs-eval 0.0.5
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| nodejs_eval-0.0.5.tar.gz | 5.1 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| nodejs_eval-0.0.5-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 10.3 kB
Release files / nodejs_eval-0.0.5.tar.gz
| Download URL | nodejs_eval-0.0.5.tar.gz |
|---|---|
| Size | 5.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
3d0ea7e6e4ae141b44d963d1a75fe097b198cacdb01fbc36b036a666f3b8c8f7
|
|
BLAKE2b-256 checksum How to use checksums |
b157bc68b7612d5d12c18cba73a50a2cdc86b3b7d2976e396596fadcc388527f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
python-httpx/0.26.0
|
Release files / nodejs_eval-0.0.5-py3-none-any.whl
| Download URL | nodejs_eval-0.0.5-py3-none-any.whl |
|---|---|
| Size | 5.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
50f14b9a69abdbe453b33066d5b06ab6bd5d5052c822ea666b1ca9fad4d0b00e
|
|
BLAKE2b-256 checksum How to use checksums |
e65b41216d78802b454e92b707267c7e1a1203254a99af5fb76b5b424cb04a50
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
python-httpx/0.26.0
|