ferrijs_std/lib.rs
1//! Vendored subset of [awslabs/llrt](https://github.com/awslabs/llrt)
2//! (Apache-2.0): the WHATWG Streams implementation plus the pieces it
3//! needs (`llrt_utils`, `llrt_events`, `llrt_exceptions`, `llrt_abort`).
4//!
5//! Upstream crate -> module here:
6//!
7//! | upstream | module |
8//! |---------------------|---------------|
9//! | `llrt_utils` | [`utils`] |
10//! | `llrt_context` | [`context`] |
11//! | `llrt_exceptions` | [`exceptions`]|
12//! | `llrt_events` | [`events`] |
13//! | `llrt_abort` | [`abort`] |
14//! | `llrt_os` | [`os`] |
15//! | `llrt_fs` | [`fs`] |
16//! | `llrt_path` (helpers)| [`pathutil`] |
17//! | `llrt_encoding` | [`encoding`] |
18//! | `llrt_buffer` | [`buffer`] |
19//! | `llrt_json` | [`json`] |
20//! | `llrt_crypto` | [`crypto`] |
21//! | `llrt_stream_web` | [`stream_web`]|
22//! | `llrt_zlib` | [`zlib`] |
23//! | `llrt_compression` | [`compression`]|
24//! | `llrt_string_decoder`| [`string_decoder`]|
25//! | `llrt_perf_hooks` | [`perf_hooks`]|
26//! | `llrt_tty` | [`tty`] |
27//! | `llrt_navigator` | [`navigator`] |
28//! | `llrt_url` | [`url`] |
29//! | `llrt_util` (codecs)| [`text`] |
30//! | `llrt_test` | `test` (dev) |
31//!
32//! Sources are kept byte-close to upstream — only `crate::` / `llrt_*`
33//! path prefixes are rewritten — so a re-sync stays a mechanical diff.
34//! Host-specific behaviour belongs in the embedding runtime, not here.
35
36pub mod abort;
37pub mod context;
38pub mod buffer;
39/// Codec back-ends behind `zlib` (upstream `llrt_compression`).
40pub mod compression;
41pub mod crypto;
42pub mod encoding;
43pub mod events;
44pub mod exceptions;
45pub mod fs;
46pub mod identity;
47pub mod json;
48pub mod modules;
49pub mod navigator;
50/// Node modules this crate implements itself, because upstream llrt has
51/// none or only a stub. Written to the repo's style, but compiled under
52/// this crate's relaxed lints: pedantic's `needless_pass_by_value` is
53/// unsatisfiable for rquickjs callback signatures, which must take owned
54/// JS values.
55pub mod node;
56pub mod os;
57pub mod perf_hooks;
58pub mod permissions;
59/// Path helpers the vendored `fs` needs (upstream `llrt_path`). The
60/// `path` MODULE is this crate's own; only these Rust helpers come from
61/// upstream, so `fs` stays byte-close to it.
62pub mod pathutil;
63pub mod stream_web;
64pub mod string_decoder;
65pub mod text;
66pub mod tty;
67pub mod url;
68pub mod utils;
69pub mod zlib;
70/// Web-platform globals with no upstream in llrt, written here so the
71/// runtime has exactly one implementation of each.
72pub mod web;
73
74#[cfg(test)]
75mod test;
76
77use rquickjs::{Ctx, Result};
78
79/// Install every web-standard global on `ctx`: `DOMException`, `Event` /
80/// `EventTarget`, `AbortController` / `AbortSignal`, the full Streams
81/// surface, `Buffer` / `Blob` / `File`, `crypto`, the text codecs,
82/// `URL` / `URLSearchParams`, `atob` / `btoa`, `structuredClone`,
83/// `performance`, `FormData`, the compression streams and `navigator`.
84///
85/// One entry point, so a host cannot install half the crate. What is
86/// deliberately NOT here: the timers (they carry host state, see
87/// [`web::timers`]), `process` (its `env` and `cwd` are the host's to
88/// supply, see [`node::process`]), the Node modules (served through
89/// [`modules::modules`] by the host's loader) and an `fs` global (Node
90/// has none; [`fs::init`] is the opt-in).
91pub fn init(ctx: &Ctx<'_>) -> Result<()> {
92 exceptions::init(ctx)?;
93 events::init(ctx)?;
94 abort::init(ctx)?;
95 stream_web::init(ctx)?;
96 buffer::init(ctx)?;
97 crypto::init(ctx)?;
98 text::init(ctx)?;
99 url::init(ctx)?;
100 web::init(ctx)?;
101 navigator::init(ctx)?;
102 Ok(())
103}