Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.lock

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

8 changes: 1 addition & 7 deletions components/client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,9 @@ crate-type = ["cdylib"]

[dependencies]
url = { version = "2.5", default-features = false }
# without std, see crates/wasip3-rt
wasip3-rt = { workspace = true }
wit-bindgen = { version = "0.63.0", default-features = false, features = ["macros", "realloc", "async"] }
wit-bindgen = { workspace = true, features = ["async-spawn"] }

[dev-dependencies]
test-harness = { workspace = true }
tokio = { workspace = true }
wasmtime = { workspace = true }

[package.metadata.componentized]
# built for wasm32-wasip3, see build/components.mk
target = "wasm32-wasip3"
37 changes: 15 additions & 22 deletions components/client/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,4 @@
//! Without std, see crates/wasip3-rt.

#![no_std]

extern crate alloc;

use alloc::format;
use alloc::string::{String, ToString};
use alloc::vec::Vec;
use url::Url;
use wasip3_rt as _;

wit_bindgen::generate!({
path: "../wit",
Expand Down Expand Up @@ -311,21 +301,25 @@ fn read_fields(fields: &Fields) -> Vec<(String, String)> {
.collect()
}

/// The trailers of a response, as a future that resolves once the caller has read the body.
///
/// The WASI trailers resolve after the body is consumed, which the caller does after the response
/// is returned, so they're mapped in a task spawned into the export's task, which keeps running
/// after the export returns.
fn map_trailers(
wasi: FutureReader<Result<Option<Trailers>, WasiErrorCode>>,
) -> FutureReader<Result<Vec<(String, String)>, TrailersErrorCode>> {
let (tx, rx) = wit_future::new(|| Ok(Vec::new()));
wasip3_rt::spawn(move || {
wit_bindgen::block_on(async move {
let resolved = match wasi.await {
Ok(Some(t)) => Ok(read_fields(&t)),
Ok(None) => Ok(Vec::new()),
Err(e) => Err(TrailersErrorCode::Other(Some(format!(
"wasi:http error: {e:?}"
)))),
};
tx.write(resolved);
})
wit_bindgen::spawn_local(async move {
let resolved = match wasi.await {
Ok(Some(t)) => Ok(read_fields(&t)),
Ok(None) => Ok(Vec::new()),
Err(e) => Err(TrailersErrorCode::Other(Some(format!(
"wasi:http error: {e:?}"
)))),
};
// the caller dropped the trailers when the write fails, nothing is waiting for them
let _ = tx.write(resolved).await;
});
rx
}
Expand Down Expand Up @@ -458,7 +452,6 @@ export!(HttpClient);
#[cfg(test)]
mod tests {
use super::*;
use alloc::vec;

fn location(value: &str) -> Vec<(String, String)> {
vec![("Location".to_string(), value.to_string())]
Expand Down