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.wasmbinary into theruntime/dir and then use it with the--adaptparam ofwasm-tools component newto 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/depsdirectory. These were sourced from thewasi-httpGH repo and thewasi:httpspecific 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:httplibrary 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:httplibrary to construct anOutgoingRequest, send it, block the thread awaiting the response using aPollablesubscribed to via theFutureIncomingResponse, and then parse out the body of the response. - Add
"@bytecodealliance/preview2-shim"to ourpackage.jsonas a dependency sojcocan access its wasi shims.
To execute the code run:
npm run build && npm run test