Back to Llrt

LLRT Modules

llrt_modules/README.md

0.8.1-beta6.6 KB
Original Source

LLRT Modules

LLRT Modules is a meta-module of rquickjs modules that can be used independantly of LLRT (Low Latency Runtime). They aim to bring to quickjs APIs from Node.js and WinterTC. You can use this meta-module, but each module is also a unique crate.

LLRT (Low Latency Runtime) is a lightweight JavaScript runtime designed to address the growing demand for fast and efficient Serverless applications.

Usage

The package is not available in the crate registry yet, but you can clone the repo and import it as a local path.

Use this script to set everything up:

bash
cd your_project_dir
git clone https://github.com/awslabs/llrt.git

cd llrt
npm i
make js

Each module has a feature flag, they are all enabled by default but if you prefer to can decide which one you need. Check the Compability matrix for the full list.

toml
[dependencies]
llrt_modules = { path = "llrt/llrt_modules", default-features = true } # load from local path
rquickjs = { version = "0.11", features = ["full-async"] }
tokio = { version = "1", features = ["full"] }

Once you have enable a module, you can import it in your runtime.

[!NOTE] Some modules currently require that you call an init function before they evaluated.

rust
use llrt_modules::buffer;
use rquickjs::{async_with, context::EvalOptions, AsyncContext, AsyncRuntime, Error, Module};


#[tokio::main(flavor = "current_thread")]
async fn main() -> Result<(), Error> {
    let runtime = AsyncRuntime::new()?;
    let context = AsyncContext::full(&runtime).await?;

    async_with!(context => |ctx| {
        buffer::init(&ctx)?;
        let (_module, module_eval) = Module::evaluate_def::<buffer::BufferModule,_>(ctx.clone(), "buffer")?;
        module_eval.into_future::<()>().await?;

        let mut options = EvalOptions::default();
        options.global = false;
        if let Err(Error::Exception) = ctx.eval_with_options::<(), _>(
            r#"
            import { Buffer } from "node:buffer";
            Buffer.alloc(10);
            "#,
            options
        ){
            println!("{:#?}", ctx.catch());
        };

        Ok::<_, Error>(())
    })
    .await?;

    Ok(())
}

Using ModuleBuilder makes it even simpler.

rust
use llrt_modules::module_builder::ModuleBuilder;
use rquickjs::{async_with, context::EvalOptions, AsyncContext, AsyncRuntime, Error, Module};


#[tokio::main(flavor = "current_thread")]
async fn main() -> Result<(), Error> {
    let runtime = AsyncRuntime::new()?;

    let module_builder = ModuleBuilder::default();
    let (module_resolver, module_loader, global_attachment) = module_builder.build();
    runtime.set_loader((module_resolver,), (module_loader,)).await;

    let context = AsyncContext::full(&runtime).await?;

    async_with!(context => |ctx| {
        global_attachment.attach(&ctx)?;

        let mut options = EvalOptions::default();
        options.global = false;
        if let Err(Error::Exception) = ctx.eval_with_options::<(), _>(
            r#"
            import { Buffer } from "node:buffer";
            Buffer.alloc(10);
            "#,
            options
        ){
            println!("{:#?}", ctx.catch());
        };

        Ok::<_, Error>(())
    })
    .await?;

    Ok(())
}

Compatibility matrix

[!NOTE] Only a fraction of the Node.js APIs are supported. Below is a high level overview of partially supported APIs and modules.

Node.jsLLRT ModulesFeatureCrate
abort✔︎✔︎️abortllrt_abort
assert✔︎⚠️assertllrt_assert
async_hooks✔︎⚠️async-hooksllrt_async_hooks
buffer✔︎⚠️bufferllrt_buffer
child process✔︎⚠️child-processllrt_child_process
console✔︎⚠️consolellrt_console
crypto✔︎⚠️cryptollrt_crypto
dgram✔︎⚠️dgramllrt_dgram
dns✔︎⚠️dnsllrt_dns
events✔︎⚠️eventsllrt_events
exceptions✔︎⚠️exceptionsllrt_exceptions
fetch✔︎⚠️fetchllrt_fetch
fs/promises✔︎⚠️fsllrt_fs
fs✔︎⚠️fsllrt_fs
intl✔︎⚠️N/Allrt_intl
navigator✔︎⚠️navigatorllrt_navigator
net✔︎⚠️netllrt_net
os✔︎⚠️osllrt_os
path✔︎⚠️pathllrt_path
perf hooks✔︎⚠️perf-hooksllrt_perf_hooks
stream (lib)N/A✔︎N/Allrt_stream
string_decoder✔︎✔︎string_decoderllrt_string_decoder
timers✔︎⚠️timersllrt_timers
process✔︎⚠️processllrt_process
temporal✔︎⚠️N/Allrt_temporal
tty✔︎⚠️ttyllrt_tty
url✔︎⚠️urlllrt_url
util✔︎⚠️utilllrt_util
zlib✔︎⚠️zlibllrt_zlib
Other modules✔︎N/AN/A

⚠️ = partially supported

License

This module is licensed under the Apache-2.0 License.