Skip to content

Commit

Permalink
add support for wasi:http/[email protected] (#2166)
Browse files Browse the repository at this point in the history
* add support for `wasi:http/[email protected]`

This builds upon Alex's excellent work in
#2108, adding the ability to run guests
which target the `0.2.0-rc-2023-11-10` version of `wasi-http`.

I've added a test which uses `wit-bindgen` and `wit-component` directly to
ensure it uses the correct snapshot for everything `wasi-http`, `wasi-cli`, etc.

Note that we're not yet updating any SDKs to use the new snapshot, since we
still want people to be able to use the new SDK to target Spin 2.0.

Signed-off-by: Joel Dice <[email protected]>

* use different names for `wasi-http-rust-rc-2023-11-10` module and component

Previously, I tried to be clever and overwrite the module with the component,
but that led to a filesystem-level race condition and was just generally
fragile.

Signed-off-by: Joel Dice <[email protected]>

---------

Signed-off-by: Joel Dice <[email protected]>
  • Loading branch information
dicej authored Dec 13, 2023
1 parent de76f07 commit 9b2c657
Show file tree
Hide file tree
Showing 46 changed files with 3,469 additions and 17 deletions.
48 changes: 34 additions & 14 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ vergen = { version = "^8.2.1", default-features = false, features = [
"gitcl",
"cargo",
] }
wit-component = "0.19.0"

[features]
default = ["llm"]
Expand Down
28 changes: 28 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const RUST_HTTP_VAULT_VARIABLES_TEST: &str = "tests/http/vault-variables-test";
const TIMER_TRIGGER_INTEGRATION_TEST: &str = "examples/spin-timer/app-example";
const WASI_HTTP_INTEGRATION_TEST: &str = "examples/wasi-http-rust-streaming-outgoing-body";
const OUTBOUND_HTTP_POST_INTEGRATION_TEST: &str = "examples/http-rust-outbound-post";
const WASI_HTTP_RC_11_10_INTEGRATION_TEST: &str = "tests/http/wasi-http-rust-0.2.0-rc-2023-11-10";

fn main() {
// Extract environment information to be passed to plugins.
Expand Down Expand Up @@ -92,6 +93,33 @@ error: the `wasm32-wasi` target is not installed
cargo_build(TIMER_TRIGGER_INTEGRATION_TEST);
cargo_build(WASI_HTTP_INTEGRATION_TEST);
cargo_build(OUTBOUND_HTTP_POST_INTEGRATION_TEST);
cargo_build(WASI_HTTP_RC_11_10_INTEGRATION_TEST);

// Rather than let `spin-componentize` turn the `WASI_HTTP_RC_11_10_INTEGRATION_TEST` module into a component,
// we use Wasmtime 15.0.1's adapter to ensure it uses the WASI 0.2.0-rc-2023-11-10 snapshot for everything.
let wasi_http_rc_11_10_module = format!(
"{WASI_HTTP_RC_11_10_INTEGRATION_TEST}/target/wasm32-wasi/release/wasi_http_rust_rc_2023_11_10.wasm"
);
let wasi_http_rc_11_10_adapter =
format!("{WASI_HTTP_RC_11_10_INTEGRATION_TEST}/wasi_snapshot_preview1.reactor.wasm");
let wasi_http_rc_11_10_component = format!(
"{WASI_HTTP_RC_11_10_INTEGRATION_TEST}/target/wasm32-wasi/release/wasi_http_rust_rc_2023_11_10.component.wasm"
);
std::fs::write(
wasi_http_rc_11_10_component,
wit_component::ComponentEncoder::default()
.validate(true)
.module(&std::fs::read(wasi_http_rc_11_10_module).unwrap())
.unwrap()
.adapter(
"wasi_snapshot_preview1",
&std::fs::read(wasi_http_rc_11_10_adapter).unwrap(),
)
.unwrap()
.encode()
.unwrap(),
)
.unwrap();
}

fn build_wasm_test_program(name: &'static str, root: &'static str) {
Expand Down
12 changes: 9 additions & 3 deletions crates/trigger-http/src/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,10 @@ impl HttpExecutor for HttpHandlerExecutor {
.await
.map_err(contextualise_err)?
}
None => bail!("Expected component to either export `{}` or `fermyon:spin/inbound-http` but it exported neither", WASI_HTTP_EXPORT)
None => bail!(
"Expected component to either export `{WASI_HTTP_EXPORT_2023_10_18}`, \
`{WASI_HTTP_EXPORT_2023_11_10}`, or `fermyon:spin/inbound-http` but it exported neither"
)
};

tracing::info!(
Expand Down Expand Up @@ -302,12 +305,15 @@ enum HandlerType {
Wasi,
}

const WASI_HTTP_EXPORT: &str = "wasi:http/[email protected]";
const WASI_HTTP_EXPORT_2023_10_18: &str = "wasi:http/[email protected]";
const WASI_HTTP_EXPORT_2023_11_10: &str = "wasi:http/[email protected]";

impl HandlerType {
/// Determine the handler type from the exports
fn from_exports(mut exports: wasmtime::component::Exports<'_>) -> Option<HandlerType> {
if exports.instance(WASI_HTTP_EXPORT).is_some() {
if exports.instance(WASI_HTTP_EXPORT_2023_10_18).is_some()
|| exports.instance(WASI_HTTP_EXPORT_2023_11_10).is_some()
{
return Some(HandlerType::Wasi);
}
if exports.instance("fermyon:spin/inbound-http").is_some() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[build]
target = "wasm32-wasi"
Loading

0 comments on commit 9b2c657

Please sign in to comment.