1# Chapter 9: jevhooks-mod, Rust that becomes JavaScript 2 3The mod's routing is written in Rust, and the mod is TypeScript running in 4a place that has no `WebAssembly` object. Both of those are true at once, 5and this crate is how. 6 7Rust can compile to **WebAssembly** (wasm): a compact, portable machine code 8that browsers and Node normally run with `WebAssembly.instantiate`. A 9mod's realm inside Claude Code has no `WebAssembly`, so that door is shut. 10But wasm is simple enough to translate, instruction by instruction, into 11ordinary JavaScript, and binaryen's **wasm2js** does exactly that. What 12comes out is a JavaScript module that behaves like the wasm would have: 13the same functions, the same flat array of memory, only larger: about 14100 KB of optimised wasm becomes about 740 KB of JavaScript. 15 16```mermaid 17flowchart LR 18 src["src/lib.rs<br/>src/read_only.rs"] -- "cargo build --release<br/>--target wasm32-unknown-unknown" --> wasm["jevhooks_mod.wasm"] 19 wasm -- "wasm-opt -Oz<br/>(smaller)" --> opt["jevhooks_mod.opt.wasm"] 20 opt -- "wasm2js -O2" --> js["plugin/hooks/jevhooks.js"] 21 js -- "imported by" --> bridge["plugin/hooks/bridge.ts"] 22``` 23 24That is `just mod`, line for line. The result is committed, because the 25plugin loads it as it stands: no build step happens on your machine when 26Claude Code starts. 27 28> **Aside: why bother with Rust here at all?** Because the routing is the 29> part that must be right in only one direction (chapter 10), and Rust 30> lets it be tested natively with `cargo test`, in milliseconds, against 31> the same `HookEvent` the daemon matches on. The TypeScript side is left 32> with plumbing that cannot get a command wrong. 33 34The crate is built two ways at once. `crate-type = ["cdylib", "rlib"]`: 35the `cdylib` is the standalone library that becomes `jevhooks_mod.wasm`, 36and the `rlib` is an ordinary Rust library, which is what `cargo test` 37links its native tests against. 38 39> **Try it.** With the devshell (`nix develop`) and access to the rmcp 40> fork, `just mod` rebuilds `plugin/hooks/jevhooks.js` from this crate, 41> and `cargo test -p jevhooks-mod` runs the routing and filter tests 42> natively. 43 44## For the people who maintain it 45 46### In this folder 47 48| Path | What | 49| --- | --- | 50| [src/](src/) | Chapter 10: the routing, the read-only filter, and the wasm entry points. | 51| [Cargo.toml](Cargo.toml) | `cdylib` and `rlib`; depends on `jevhooks-events`, `serde`, `serde_json`. | 52 53← Previous: [Chapter 8, jevhooks-events/src/](../jevhooks-events/src/) · Up: [crates](../) · Next: [Chapter 10, jevhooks-mod/src/](src/) →