This is a quick proof of concept getting the wasi:http
functionality working.
The main things required to get this to work:
- Save the
wasi_snapshot_preview1.reactor.wasm
binary into theruntime/
dir and then use it with the--adapt
param ofwasm-tools component new
to include the wasi preview1 functionality into my binary. The pre-build binary was sourced from a tagged wasmtime release (16.0.0). - Include all necessary
wasi:*
wit files in theruntime/wit/deps
directory. These were sourced from thewasi-http
GH repo and thewasi:http
specific ones hadpackage wasi:http@0.2.0-rc-2023-12-05;
added at the top of each to make them easily referenceable. - We add a
import wasi:http/outgoing-handler@0.2.0-rc-2023-12-05;
statement to the world so that the Rust parts of thewasi:http
library are generated and we can reference them like:
use wasi::http::outgoing_handler::handle;
use wasi::http::types::{
ErrorCode, Fields, FutureIncomingResponse, IncomingBody, IncomingResponse, OutgoingRequest,
RequestOptions, Scheme,
};
- We then use the
wasi:http
library to construct anOutgoingRequest
, send it, block the thread awaiting the response using aPollable
subscribed to via theFutureIncomingResponse
, and then parse out the body of the response. - Add
"@bytecodealliance/preview2-shim"
to ourpackage.json
as a dependency sojco
can access its wasi shims.
To execute the code run:
npm run build && npm run test