Skip to main content

Sandboxed JavaScript execution for Python, via PyO3 + rquickjs.

Project description

quickjs-rs

Sandboxed JavaScript execution for Python.

Native Python extension (PyO3 + rquickjs) wrapping quickjs-ng (a QuickJS fork). Single self-contained wheel, zero runtime dependencies, microsecond-range runtime startup. ES modules with a composable scope registry. Inline TypeScript support via oxidase.

[!WARNING] quickjs-rs is experimental. Before putting this in production, you should read the Security guide.

Install

pip install quickjs-rs
uv add quickjs-rs

Wheels ship for Linux (x86_64 + aarch64), macOS (x86_64 + arm64), and Windows (x86_64), against Python 3.11, 3.12, and 3.13.

Quickstart

from quickjs_rs import Runtime

with Runtime() as rt:
    with rt.new_context() as ctx:
        assert ctx.eval("1 + 2") == 3

        # Register a Python callable as a JS global.
        @ctx.function
        def greet(name: str) -> str:
            return f"hi {name}"
        assert ctx.eval("greet('world')") == "hi world"

Async + top-level await:

import asyncio

async def main():
    with Runtime() as rt:
        with rt.new_context() as ctx:
            @ctx.function
            async def fetch_thing() -> str:
                await asyncio.sleep(0.01)
                return "from python"

            result = await ctx.eval_async("await fetch_thing()")
            assert result == "from python"

asyncio.run(main())

ES modules

Register modules via ModuleScope, then import them from module-mode eval. Scopes are recursive, self-contained resolver boundaries — each scope sees only what its own dict declares.

from quickjs_rs import ModuleScope, Runtime

stdlib = ModuleScope({
    "@agent/utils": ModuleScope({
        "index.js": """
            export { slugify } from './strings.js';
        """,
        "strings.js": """
            export function slugify(s) {
                return s.toLowerCase().replace(/ /g, '-');
            }
        """,
    }),
    "@agent/config": ModuleScope({
        "index.js": "export const MAX_RETRIES = 3;",
    }),
})

with Runtime() as rt:
    with rt.new_context() as ctx:
        rt.install(stdlib)
        assert await ctx.eval_async("""
            const { slugify } = await import("@agent/utils");
            const { MAX_RETRIES } = await import("@agent/config");
            slugify("Hello World") + '/' + MAX_RETRIES;
        """) == "hello-world/3"

Shared deps are declared by spreading (**utils.modules) into each scope that needs them. Resolver conventions are documented in AGENTS.md.

TypeScript

Source strings whose key ends in .ts, .mts, .cts, or .tsx are type-stripped at install() time via oxidase. Enums, namespaces, and parameter properties are transformed; plain type annotations erase to whitespace. No type checking — run tsc --noEmit separately if you want that.

rt.install(ModuleScope({
    "@util": ModuleScope({
        "index.ts": """
            export enum Mode { Strict = 1, Loose = 2 }
            export function slug(s: string, mode: Mode): string {
                return s.toLowerCase().replace(/ /g, mode === Mode.Strict ? '_' : '-');
            }
        """,
    }),
}))

TypeScript syntax errors surface at install() time (oxidase parses during stripping) rather than at eval.

Snapshots

quickjs-rs can snapshot the restorable portion of a context's script-mode top-level state and restore it into another context.

It does not attempt to snapshot module-local bindings, pending async work, host callback identity, or full lexical-environment state.

from quickjs_rs import Runtime, Snapshot

with Runtime() as rt:
    with rt.new_context() as ctx:
        ctx.eval("""
            const shared = { count: 1 };
            const a = shared;
            const b = shared;
        """)
        snap = ctx.create_snapshot()
        payload = snap.to_bytes()

with Runtime() as rt2:
    with rt2.new_context() as ctx2:
        snap = Snapshot.from_bytes(payload)
        rt2.restore_snapshot(snap, ctx2)
        assert ctx2.eval("a === b") is True
        assert ctx2.eval("a.count") == 1

Snapshot creation supports two policy knobs:

  • on_missing_name: skip, tombstone, or error
  • on_unserializable: tombstone or error

Example:

with Runtime() as rt:
    with rt.new_context() as ctx:
        ctx.eval("const fn = () => 1;")
        snap = ctx.create_snapshot(on_unserializable="tombstone")

On restore, a tombstoned name is installed as a global property whose getter throws a descriptive error if read. This makes missing or unserializable bindings explicit instead of silently disappearing unless you choose skip.

Async contexts use the same snapshot model:

snap = await ctx.create_snapshot_async(on_missing_name="tombstone")
rt.restore_snapshot(snap, other_ctx, inject_globals=True)

Security

  • This library is not a host-memory isolation boundary. The JS engine (quickjs-ng via rquickjs/rquickjs-sys) runs in the same process/address space as Python.

    • When running untrusted or semi-trusted JS, run execution in isolated worker processes/containers with restricted network/filesystem access and recycle workers on timeout/OOM/failure.
  • Registered host callbacks are capability boundaries. Any callback exposed to JS should be treated as privileged if this runtime is being used to run untrusted code

  • Do not share a single Runtime across different trust domains/tenants. Use one runtime per trust domain to avoid cross-context module contamination.

See .github/THREAT_MODEL.md for more information on the threat boundaries and supply-chain posture of quickjs-rs

Development

# Dev install (maturin handles the Rust build).
pip install -e ".[dev]"
maturin develop --release

# Run tests, type-check, lint.
pytest
mypy quickjs_rs
ruff check

License

MIT. See LICENSE.

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

quickjs_rs-0.1.2.tar.gz (155.5 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

quickjs_rs-0.1.2-cp313-cp313-win_amd64.whl (1.3 MB view details)

Uploaded CPython 3.13Windows x86-64

quickjs_rs-0.1.2-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.4 MB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ x86-64

quickjs_rs-0.1.2-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (1.3 MB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ ARM64

quickjs_rs-0.1.2-cp313-cp313-macosx_11_0_arm64.whl (1.3 MB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

quickjs_rs-0.1.2-cp313-cp313-macosx_10_12_x86_64.whl (1.4 MB view details)

Uploaded CPython 3.13macOS 10.12+ x86-64

quickjs_rs-0.1.2-cp312-cp312-win_amd64.whl (1.3 MB view details)

Uploaded CPython 3.12Windows x86-64

quickjs_rs-0.1.2-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.4 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ x86-64

quickjs_rs-0.1.2-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (1.3 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ ARM64

quickjs_rs-0.1.2-cp312-cp312-macosx_11_0_arm64.whl (1.3 MB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

quickjs_rs-0.1.2-cp312-cp312-macosx_10_12_x86_64.whl (1.4 MB view details)

Uploaded CPython 3.12macOS 10.12+ x86-64

quickjs_rs-0.1.2-cp311-cp311-win_amd64.whl (1.3 MB view details)

Uploaded CPython 3.11Windows x86-64

quickjs_rs-0.1.2-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.4 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ x86-64

quickjs_rs-0.1.2-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (1.3 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ ARM64

quickjs_rs-0.1.2-cp311-cp311-macosx_11_0_arm64.whl (1.3 MB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

quickjs_rs-0.1.2-cp311-cp311-macosx_10_12_x86_64.whl (1.4 MB view details)

Uploaded CPython 3.11macOS 10.12+ x86-64

File details

Details for the file quickjs_rs-0.1.2.tar.gz.

File metadata

  • Download URL: quickjs_rs-0.1.2.tar.gz
  • Upload date:
  • Size: 155.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.13

File hashes

Hashes for quickjs_rs-0.1.2.tar.gz
Algorithm Hash digest
SHA256 95c42dcb40f067ae3b95eb3f79836cc8bcab62d4fd4cb276970ca2a211e687c7
MD5 909fc3cefe997bfaca7dce90102a6d2f
BLAKE2b-256 96594c596144ee2dfe49024cd3abf32a97ba62c34cd9f0f72e6fe23021a73181

See more details on using hashes here.

Provenance

The following attestation bundles were made for quickjs_rs-0.1.2.tar.gz:

Publisher: release.yml on langchain-ai/quickjs-rs

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file quickjs_rs-0.1.2-cp313-cp313-win_amd64.whl.

File metadata

  • Download URL: quickjs_rs-0.1.2-cp313-cp313-win_amd64.whl
  • Upload date:
  • Size: 1.3 MB
  • Tags: CPython 3.13, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.13

File hashes

Hashes for quickjs_rs-0.1.2-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 606a7b4da741ceb51e4f79723ad366f9272f69bca4f438b985d1b54c56c8fcc0
MD5 ff84dfa1b1ae400d9a16675aaafcd1e2
BLAKE2b-256 0cdb9e3bfba55953fe5af5f39f1144b033fe572ae55a5e1bc68a4408f636cb54

See more details on using hashes here.

Provenance

The following attestation bundles were made for quickjs_rs-0.1.2-cp313-cp313-win_amd64.whl:

Publisher: release.yml on langchain-ai/quickjs-rs

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file quickjs_rs-0.1.2-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for quickjs_rs-0.1.2-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 fed6aa295b80f5ebc7b077af37895a70234c566961735ca9c803a3fe8be511ec
MD5 bb1c08b1393859776efc9e7088765d7d
BLAKE2b-256 5eb97f0aef4047a7a2469a9945cdb493b5e8937249b036c8b53699c84eed9e0b

See more details on using hashes here.

Provenance

The following attestation bundles were made for quickjs_rs-0.1.2-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: release.yml on langchain-ai/quickjs-rs

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file quickjs_rs-0.1.2-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for quickjs_rs-0.1.2-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 806eec8c3ba699931372e34743b3894795eb382f2ee70321a25795228524840f
MD5 c7c300745961c3d3003e48996c937a43
BLAKE2b-256 63bf88760ad6331a08d1749678d50127d3d7ba7309a815fb4c325d4d6ffa740e

See more details on using hashes here.

Provenance

The following attestation bundles were made for quickjs_rs-0.1.2-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl:

Publisher: release.yml on langchain-ai/quickjs-rs

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file quickjs_rs-0.1.2-cp313-cp313-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for quickjs_rs-0.1.2-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 2af1fa9cca2009e8ab69d39d5b4579b59ec69607fb2cf4394cccfab14bd89caa
MD5 46a1e344876bb6d0afe28ca30fa50ae4
BLAKE2b-256 5c55f5fc4bf2c237a8570ce57ee8f9ddd76bc34b8c4e0013d408bc3a697dea0e

See more details on using hashes here.

Provenance

The following attestation bundles were made for quickjs_rs-0.1.2-cp313-cp313-macosx_11_0_arm64.whl:

Publisher: release.yml on langchain-ai/quickjs-rs

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file quickjs_rs-0.1.2-cp313-cp313-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for quickjs_rs-0.1.2-cp313-cp313-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 673a5c9824905754f67f52e7d05a23ca8094828b44cd389dd63976caa109420f
MD5 cf61131d8be7c0cd1f39e3b9a648cee4
BLAKE2b-256 81e26497237126abea9f058e72904d406f31d0bde387796f67b13cdb7b86184c

See more details on using hashes here.

Provenance

The following attestation bundles were made for quickjs_rs-0.1.2-cp313-cp313-macosx_10_12_x86_64.whl:

Publisher: release.yml on langchain-ai/quickjs-rs

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file quickjs_rs-0.1.2-cp312-cp312-win_amd64.whl.

File metadata

  • Download URL: quickjs_rs-0.1.2-cp312-cp312-win_amd64.whl
  • Upload date:
  • Size: 1.3 MB
  • Tags: CPython 3.12, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.13

File hashes

Hashes for quickjs_rs-0.1.2-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 b4229db014881f56828c8ffd120b6be822f2886d570ab1bea99cfe70da83f401
MD5 3745e43bf1758501889bf5928e266519
BLAKE2b-256 00a772c50e1f52ac928902657f19ca0f549676e6be84252e5d3fa286cecc6c01

See more details on using hashes here.

Provenance

The following attestation bundles were made for quickjs_rs-0.1.2-cp312-cp312-win_amd64.whl:

Publisher: release.yml on langchain-ai/quickjs-rs

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file quickjs_rs-0.1.2-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for quickjs_rs-0.1.2-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 a7dc9d446b00c24437ae22ba0acefc238abfbdca223716bd8c2ce4223b836d14
MD5 2c8fe3f658bc3434e590b6940039607e
BLAKE2b-256 e2cbee09b2d9797d248ca9c492e88ded527798693bb013f5d921c9fb07229f8f

See more details on using hashes here.

Provenance

The following attestation bundles were made for quickjs_rs-0.1.2-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: release.yml on langchain-ai/quickjs-rs

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file quickjs_rs-0.1.2-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for quickjs_rs-0.1.2-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 7b3d5bde9f2c12bfce1702f9d1bc2ccfa14f3993211e3d9ed8eceaf7069d432d
MD5 0c3cf25aa2e4d09a632fa76df1580a78
BLAKE2b-256 d0f277f04f177df196674e926eb7a019127cea5551fa8174d621300536888be9

See more details on using hashes here.

Provenance

The following attestation bundles were made for quickjs_rs-0.1.2-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl:

Publisher: release.yml on langchain-ai/quickjs-rs

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file quickjs_rs-0.1.2-cp312-cp312-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for quickjs_rs-0.1.2-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 68bb46c3909bd40768da20f0ffbdd1e0da70702e0638711f5e2bb31d778dd92a
MD5 aa54c34b27c2d47e06eeff397a737c43
BLAKE2b-256 51326f2c18a39388b1f45fecbc8639053b15b5a7d8d5345dba1886e7ae120dc7

See more details on using hashes here.

Provenance

The following attestation bundles were made for quickjs_rs-0.1.2-cp312-cp312-macosx_11_0_arm64.whl:

Publisher: release.yml on langchain-ai/quickjs-rs

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file quickjs_rs-0.1.2-cp312-cp312-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for quickjs_rs-0.1.2-cp312-cp312-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 543ae2a983f677f7c83457fbea6fb33abf938215b512439340c90768c15b0b96
MD5 8b8769df20950d0e08f0858943aab081
BLAKE2b-256 6701e50258535da0f38167d1df6e8370027a1629f3553ecebe188a501eaee826

See more details on using hashes here.

Provenance

The following attestation bundles were made for quickjs_rs-0.1.2-cp312-cp312-macosx_10_12_x86_64.whl:

Publisher: release.yml on langchain-ai/quickjs-rs

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file quickjs_rs-0.1.2-cp311-cp311-win_amd64.whl.

File metadata

  • Download URL: quickjs_rs-0.1.2-cp311-cp311-win_amd64.whl
  • Upload date:
  • Size: 1.3 MB
  • Tags: CPython 3.11, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.13

File hashes

Hashes for quickjs_rs-0.1.2-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 93c1ac55e811f582c1b7affd92274ded945e77009010d4a028bfa2d6577b8322
MD5 58144d9935af76dc1d8afd86fe136fb4
BLAKE2b-256 1fd14d2cb369278c0b6333b68224d35a37161ba1408dc42588c7033756a62416

See more details on using hashes here.

Provenance

The following attestation bundles were made for quickjs_rs-0.1.2-cp311-cp311-win_amd64.whl:

Publisher: release.yml on langchain-ai/quickjs-rs

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file quickjs_rs-0.1.2-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for quickjs_rs-0.1.2-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 efb68efe12b600e18dc7b8709dfb3bd61acf3246683713757101bec6db2dd5ad
MD5 031fc6601d6db6080f049c306d65f6d0
BLAKE2b-256 c4eb591cd04c38cbc0770a0609ed87fdb1121e38af64b78b507d67d4ad31f7cc

See more details on using hashes here.

Provenance

The following attestation bundles were made for quickjs_rs-0.1.2-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: release.yml on langchain-ai/quickjs-rs

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file quickjs_rs-0.1.2-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for quickjs_rs-0.1.2-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 71f22f01b871800978b8bc23aeeb9edc4c708ada2552da1bfe15227af1717e11
MD5 38dbd3f7c65319153ba5f1ffd9bd097e
BLAKE2b-256 a9184452836b1d94a3e9f4a9294c06ae4c4e6a0e8ec6e4842cf95ac7cc51fec1

See more details on using hashes here.

Provenance

The following attestation bundles were made for quickjs_rs-0.1.2-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl:

Publisher: release.yml on langchain-ai/quickjs-rs

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file quickjs_rs-0.1.2-cp311-cp311-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for quickjs_rs-0.1.2-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 bf0941f4d6903ac4ce24d3734f71c4391aff403d34774e66405f25045a538ad5
MD5 656e6c53fd6d91c480a788e25cfaa3a5
BLAKE2b-256 18c3f5f98bb3f7e4d8df209c470592d700b54d3f26b91c5b3203133d5940e0c4

See more details on using hashes here.

Provenance

The following attestation bundles were made for quickjs_rs-0.1.2-cp311-cp311-macosx_11_0_arm64.whl:

Publisher: release.yml on langchain-ai/quickjs-rs

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file quickjs_rs-0.1.2-cp311-cp311-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for quickjs_rs-0.1.2-cp311-cp311-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 4a03488c73626a129072eae966bcadede77dcc79cfe36f2ea3b0f1ae351bae75
MD5 4f91131ec942e75626af4bbec58444ec
BLAKE2b-256 f5cd0375e8324d581e3c8c54c167359643063cbd6a824a73d0d5d37ad84ecad0

See more details on using hashes here.

Provenance

The following attestation bundles were made for quickjs_rs-0.1.2-cp311-cp311-macosx_10_12_x86_64.whl:

Publisher: release.yml on langchain-ai/quickjs-rs

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page