This repo is an experimentation of using WASM modules with a shared component interface as plugins.
- Shared component interface defined in the WIT format. (
wit/handler.wit
)- Plugins load the component interface via wit-bindgen which enforces the implementation of the guest side of the component
- Hosts load the interface via wasmtime which enforces the implementation of the host side of the component
- The plugin and server and currently both implemented in Rust, but the interface is language agnostic. As long as the WASM component module confirms to the WIT format, it can be used by as a handler.
- Route handlers are loaded as WASM modules. Currently the only one available is
hello-handler
. These handlers are compiled separately to thewasm32-wasi
orwasm32-unknown-unknown
targets - Actix server located in
server/
that dynamically loads WASM modules and executes them as route handlers (currently only 1 on/
but I will add more when it is not super late)
- Install wasm-tools
- Compile the handlers and convert into WASM components:
./build-handlers.sh
. These will show up inhandlers/*.wasm
- Run the server:
cargo run --package server
- Go to localhost:9090
The response of the request is the result of calling the WASM handler.
The handlers are also dynamically loaded on each request, which means you can change hello-handler/src/lib.rs
, run ./build-handlers.sh
and refresh the page to see the changes (no need to re-run the server).
- Add more handlers and expand the API interface
- Figure out how to run the modules with WASI available
- Re-use the execution engine so we are not re-creating it for each request
- Dynamically register handlers while the server is already running (possible with a request to POST /handler)
- Implement a handler in a non-Rust language
- See what a host and plugin in JS looks like
- How cool would it be to have a web server and each of the route handlers be implemented in a different language!
Note: Documentation for a lot of these libraries/tools is very sparse and often outdated. I've tried to compile a list of resources here, but use at your own risk.
- Introduction to WebAssembly components
- All of Radu's blog posts on wasm are great (even if 2-3 years old and often outdated)
- WIT Bindgen
- Wasmtime crate
- WIT spec
- WebAssembly binary toolkit
- WASI preview1 spec
I've also been saving a ton of links related to wasm in my bookmarks.