Other languages
The registry and runner only see components and WIT, so a module can be written in any language that produces a WebAssembly component. The SDK for AssemblyScript is a convenience, nothing more.
What a component must do:
- Export
wasi:http/incoming-handler(thewasi:http/proxyworld), an interface for other packages, or both. See Writing modules. - Import only what the runner provides: WASI 0.2 (
wasi:cli,wasi:clocks,wasi:random,wasi:io,wasi:httpand the rest of WASIp2), plus interfaces of other packages. Imports such as sockets or the file system are allowed, but the sandbox gives them nothing to use.
The registry validates every component against the runner's real linker when you publish, so anything it accepts will run. A core WebAssembly module (not a component) is rejected.
Rust
Build for wasm32-wasip2. For HTTP handlers, the wstd crate gives you an async handler and an HTTP client:
use wstd::http::{Body, Client, Error, Request, Response};
#[wstd::http_server]
async fn main(request: Request<Body>) -> Result<Response<Body>, Error> {
match request.uri().path() {
"/" => Ok(Response::new("Hello!\n".into())),
_ => {
let upstream = Request::get("https://api.example.com/status").body(Body::empty())?;
let mut resp = Client::new().send(upstream).await?;
let bytes = resp.body_mut().bytes_contents().await?;
Ok(Response::new(bytes.into()))
}
}
}
cargo build --release --target wasm32-wasip2
rt publish target/wasm32-wasip2/release/app.wasm <you>/[email protected]
println! and eprintln! go to the package's logs, and std::env::var reads its environment.
Adding a manifest
Without the SDK, embed a manifest when you publish:
rt publish app.wasm <you>/[email protected] --manifest manifest.json
See The manifest for its format.
Importing and exporting packages
rt wit ns/name prints the WIT of what a package exports, which your bindings generator can use. A Rust package can export an interface named after itself, such as acme:weather/api, for others to import. See Calling other packages.