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-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
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file nodejs_eval-0.0.5.tar.gz.
File metadata
- Download URL: nodejs_eval-0.0.5.tar.gz
- Upload date:
- Size: 5.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: python-httpx/0.26.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3d0ea7e6e4ae141b44d963d1a75fe097b198cacdb01fbc36b036a666f3b8c8f7
|
|
| MD5 |
51b61b95c7e86f6ee112aa22d0f159dc
|
|
| BLAKE2b-256 |
b157bc68b7612d5d12c18cba73a50a2cdc86b3b7d2976e396596fadcc388527f
|
File details
Details for the file nodejs_eval-0.0.5-py3-none-any.whl.
File metadata
- Download URL: nodejs_eval-0.0.5-py3-none-any.whl
- Upload date:
- Size: 5.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: python-httpx/0.26.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
50f14b9a69abdbe453b33066d5b06ab6bd5d5052c822ea666b1ca9fad4d0b00e
|
|
| MD5 |
5d99ccf440504803be70a6a50999cc4b
|
|
| BLAKE2b-256 |
e65b41216d78802b454e92b707267c7e1a1203254a99af5fb76b5b424cb04a50
|