From 0debf00bf809cd5c9d2fae6851e34d1e4bb239e9 Mon Sep 17 00:00:00 2001 From: Roman Volosatovs Date: Mon, 22 Jun 2026 19:56:00 +0200 Subject: [PATCH 1/2] feat(js): add wRPC codec package and use it in the web example Add `js/` (`@bytecodealliance/wrpc`), a dependency-free, transport-agnostic JavaScript codec for the wRPC wire format, mirroring `wrpc-transport`'s `value.rs` and default framing: - `t`/`types` build the structural WIT type tree; - `value` encodes/decodes component-model values (a jco-compatible mapping); - `wit` parses inlined WIT text back into the type tree; - `frame` builds an invocation's request bytes and decodes its results. The ESM source runs directly in the browser and is publishable to npm; it ships a hand-written `.d.ts`, JSDoc, `node --test` round-trip tests and a `tsc` typecheck. Use the codec in the `wasi:keyvalue` web example: the UI imports it from `/wrpc` (served via `ServeDir`) and drops the hand-rolled byte encoding, removing the 127-byte request/response size limits. The example keeps its own WebTransport stream handling. Assisted-by: claude:claude-opus-4-8 --- examples/web/rust/src/main.rs | 6 + examples/web/ui/index.html | 380 +++++++--------------------------- js/.gitignore | 3 + js/README.md | 118 +++++++++++ js/package.json | 45 ++++ js/src/frame.js | 85 ++++++++ js/src/index.d.ts | 104 ++++++++++ js/src/index.js | 15 ++ js/src/io.js | 151 ++++++++++++++ js/src/types.js | 94 +++++++++ js/src/value.js | 297 ++++++++++++++++++++++++++ js/src/wit.js | 251 ++++++++++++++++++++++ js/test/frame.test.js | 52 +++++ js/test/value.test.js | 114 ++++++++++ js/test/wit.test.js | 62 ++++++ js/tsconfig.json | 14 ++ 16 files changed, 1491 insertions(+), 300 deletions(-) create mode 100644 js/.gitignore create mode 100644 js/README.md create mode 100644 js/package.json create mode 100644 js/src/frame.js create mode 100644 js/src/index.d.ts create mode 100644 js/src/index.js create mode 100644 js/src/io.js create mode 100644 js/src/types.js create mode 100644 js/src/value.js create mode 100644 js/src/wit.js create mode 100644 js/test/frame.test.js create mode 100644 js/test/value.test.js create mode 100644 js/test/wit.test.js create mode 100644 js/tsconfig.json diff --git a/examples/web/rust/src/main.rs b/examples/web/rust/src/main.rs index b02d422a7..37e0b58bf 100644 --- a/examples/web/rust/src/main.rs +++ b/examples/web/rust/src/main.rs @@ -23,6 +23,7 @@ use rustls::{DigitallySignedStruct, SignatureScheme}; use tokio::sync::{Notify, RwLock}; use tokio::task::JoinSet; use tokio::{select, signal}; +use tower_http::services::ServeDir; use tower_http::trace::TraceLayer; use tracing::{debug, error, info, instrument, trace, warn}; use tracing_subscriber::layer::SubscriberExt as _; @@ -691,6 +692,11 @@ export const PORT = "{port}" ), )), ) + // Serve the `@bytecodealliance/wrpc` codec the UI imports. + .nest_service( + "/wrpc", + ServeDir::new(concat!(env!("CARGO_MANIFEST_DIR"), "/../../../js/src")), + ) .fallback(index) .layer(TraceLayer::new_for_http()), ); diff --git a/examples/web/ui/index.html b/examples/web/ui/index.html index 8cf5f7d09..67d568432 100644 --- a/examples/web/ui/index.html +++ b/examples/web/ui/index.html @@ -32,12 +32,65 @@ }