You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@opendal.apache.org by xu...@apache.org on 2025/10/19 08:34:57 UTC

(opendal-reqsign) branch http-send-reqwest-wasm created (now 11442ee)

This is an automated email from the ASF dual-hosted git repository.

xuanwo pushed a change to branch http-send-reqwest-wasm
in repository https://gitbox.apache.org/repos/asf/opendal-reqsign.git


      at 11442ee  fix ci

This branch includes the following new commits:

     new 327effe  feat: Make http-send-reqwest available on wasm
     new 11442ee  fix ci

The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.



(opendal-reqsign) 02/02: fix ci

Posted by xu...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

xuanwo pushed a commit to branch http-send-reqwest-wasm
in repository https://gitbox.apache.org/repos/asf/opendal-reqsign.git

commit 11442eed96c667618d6c90f5b95e54bbc482f021
Author: Xuanwo <gi...@xuanwo.io>
AuthorDate: Sun Oct 19 16:34:41 2025 +0800

    fix ci
    
    Signed-off-by: Xuanwo <gi...@xuanwo.io>
---
 .github/workflows/ci.yml                           | 12 ++++++++++-
 context/http-send-reqwest/Cargo.toml               |  5 ++++-
 .../http-send-reqwest/examples/custom_client.rs    | 12 +++++++++++
 context/http-send-reqwest/tests/wasm.rs            | 25 ++++++++++++++++++++++
 4 files changed, 52 insertions(+), 2 deletions(-)

diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index a13abda..76b3b61 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -85,9 +85,19 @@ jobs:
         env:
           RUST_LOG: DEBUG
           RUST_BACKTRACE: full
-
       - name: Doc Test
         run: cargo test --doc --all-features --workspace
         env:
           RUST_LOG: DEBUG
           RUST_BACKTRACE: full
+
+  wasm_tests:
+    runs-on: ubuntu-latest
+    steps:
+      - uses: actions/checkout@v5
+      - name: Run wasm tests
+        env:
+          CARGO_TARGET_WASM32_UNKNOWN_UNKNOWN_RUNNER: wasm-bindgen-test-runner
+        run: |
+          rustup target add wasm32-unknown-unknown
+          cargo test --target wasm32-unknown-unknown -p reqsign-http-send-reqwest --test wasm
diff --git a/context/http-send-reqwest/Cargo.toml b/context/http-send-reqwest/Cargo.toml
index c5a211a..4b5a21c 100644
--- a/context/http-send-reqwest/Cargo.toml
+++ b/context/http-send-reqwest/Cargo.toml
@@ -38,9 +38,12 @@ reqsign-core = { workspace = true }
 reqwest = { workspace = true, default-features = false }
 
 [dev-dependencies]
-tokio = { version = "1", features = ["macros", "rt-multi-thread"] }
 reqwest = { workspace = true, default-features = false, features = ["default-tls"] }
+wasm-bindgen-test = { version = "0.3" }
 
 [target.'cfg(target_arch = "wasm32")'.dependencies]
 futures-channel = { version = "0.3" }
 wasm-bindgen-futures = { version = "0.4" }
+
+[target.'cfg(not(target_arch = "wasm32"))'.dev-dependencies]
+tokio = { version = "1", features = ["macros", "rt-multi-thread"] }
diff --git a/context/http-send-reqwest/examples/custom_client.rs b/context/http-send-reqwest/examples/custom_client.rs
index 1124ff5..546ef85 100644
--- a/context/http-send-reqwest/examples/custom_client.rs
+++ b/context/http-send-reqwest/examples/custom_client.rs
@@ -15,13 +15,20 @@
 // specific language governing permissions and limitations
 // under the License.
 
+#[cfg(not(target_arch = "wasm32"))]
 use anyhow::Result;
+#[cfg(not(target_arch = "wasm32"))]
 use bytes::Bytes;
+#[cfg(not(target_arch = "wasm32"))]
 use reqsign_core::{Context, OsEnv};
+#[cfg(not(target_arch = "wasm32"))]
 use reqsign_http_send_reqwest::ReqwestHttpSend;
+#[cfg(not(target_arch = "wasm32"))]
 use reqwest::Client;
+#[cfg(not(target_arch = "wasm32"))]
 use std::time::Duration;
 
+#[cfg(not(target_arch = "wasm32"))]
 #[tokio::main]
 async fn main() -> Result<()> {
     // Create a custom reqwest client with specific configuration
@@ -95,3 +102,8 @@ async fn main() -> Result<()> {
 
     Ok(())
 }
+
+#[cfg(target_arch = "wasm32")]
+fn main() {
+    panic!("custom_client example is not supported on wasm targets");
+}
diff --git a/context/http-send-reqwest/tests/wasm.rs b/context/http-send-reqwest/tests/wasm.rs
new file mode 100644
index 0000000..d214ac3
--- /dev/null
+++ b/context/http-send-reqwest/tests/wasm.rs
@@ -0,0 +1,25 @@
+#![cfg(target_arch = "wasm32")]
+
+use bytes::Bytes;
+use http::Request;
+use reqsign_core::HttpSend;
+use reqsign_http_send_reqwest::ReqwestHttpSend;
+use wasm_bindgen_test::*;
+
+#[wasm_bindgen_test]
+async fn http_send_returns_error_for_unreachable_host() {
+    let client = reqwest::Client::new();
+    let http_send = ReqwestHttpSend::new(client);
+
+    let req = Request::builder()
+        .method("GET")
+        .uri("https://nonexistent.invalid")
+        .body(Bytes::new())
+        .expect("request builds");
+
+    let result = http_send.http_send(req).await;
+    assert!(
+        result.is_err(),
+        "expected unreachable host to produce error"
+    );
+}


(opendal-reqsign) 01/02: feat: Make http-send-reqwest available on wasm

Posted by xu...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

xuanwo pushed a commit to branch http-send-reqwest-wasm
in repository https://gitbox.apache.org/repos/asf/opendal-reqsign.git

commit 327effe5379f92879c34480acb7c1c567a9c538f
Author: Xuanwo <gi...@xuanwo.io>
AuthorDate: Sun Oct 19 16:27:24 2025 +0800

    feat: Make http-send-reqwest available on wasm
    
    Signed-off-by: Xuanwo <gi...@xuanwo.io>
---
 context/http-send-reqwest/Cargo.toml |  4 ++
 context/http-send-reqwest/src/lib.rs | 79 ++++++++++++++++++++++++++++++------
 2 files changed, 71 insertions(+), 12 deletions(-)

diff --git a/context/http-send-reqwest/Cargo.toml b/context/http-send-reqwest/Cargo.toml
index f215dd6..c5a211a 100644
--- a/context/http-send-reqwest/Cargo.toml
+++ b/context/http-send-reqwest/Cargo.toml
@@ -40,3 +40,7 @@ reqwest = { workspace = true, default-features = false }
 [dev-dependencies]
 tokio = { version = "1", features = ["macros", "rt-multi-thread"] }
 reqwest = { workspace = true, default-features = false, features = ["default-tls"] }
+
+[target.'cfg(target_arch = "wasm32")'.dependencies]
+futures-channel = { version = "0.3" }
+wasm-bindgen-futures = { version = "0.4" }
diff --git a/context/http-send-reqwest/src/lib.rs b/context/http-send-reqwest/src/lib.rs
index eb81c84..dfbf029 100644
--- a/context/http-send-reqwest/src/lib.rs
+++ b/context/http-send-reqwest/src/lib.rs
@@ -95,9 +95,14 @@
 
 use async_trait::async_trait;
 use bytes::Bytes;
+#[cfg(target_arch = "wasm32")]
+use futures_channel::oneshot;
+#[cfg(not(target_arch = "wasm32"))]
 use http_body_util::BodyExt;
 use reqsign_core::{Error, HttpSend, Result};
 use reqwest::{Client, Request};
+#[cfg(target_arch = "wasm32")]
+use wasm_bindgen_futures::spawn_local;
 
 /// Reqwest-based implementation of the `HttpSend` trait.
 ///
@@ -137,18 +142,68 @@ impl HttpSend for ReqwestHttpSend {
     async fn http_send(&self, req: http::Request<Bytes>) -> Result<http::Response<Bytes>> {
         let req = Request::try_from(req)
             .map_err(|e| Error::unexpected("failed to convert request").with_source(e))?;
-        let resp: http::Response<_> = self
-            .client
-            .execute(req)
-            .await
-            .map_err(|e| Error::unexpected("failed to send HTTP request").with_source(e))?
-            .into();
 
-        let (parts, body) = resp.into_parts();
-        let bs = BodyExt::collect(body)
-            .await
-            .map(|buf| buf.to_bytes())
-            .map_err(|e| Error::unexpected("failed to collect response body").with_source(e))?;
-        Ok(http::Response::from_parts(parts, bs))
+        #[cfg(not(target_arch = "wasm32"))]
+        {
+            return http_send_native(&self.client, req).await;
+        }
+
+        #[cfg(target_arch = "wasm32")]
+        {
+            return http_send_wasm(&self.client, req).await;
+        }
     }
 }
+
+#[cfg(not(target_arch = "wasm32"))]
+async fn http_send_native(client: &Client, req: Request) -> Result<http::Response<Bytes>> {
+    let resp = client
+        .execute(req)
+        .await
+        .map_err(|e| Error::unexpected("failed to send HTTP request").with_source(e))?;
+
+    let resp: http::Response<_> = resp.into();
+    let (parts, body) = resp.into_parts();
+    let bs = BodyExt::collect(body)
+        .await
+        .map(|buf| buf.to_bytes())
+        .map_err(|e| Error::unexpected("failed to collect response body").with_source(e))?;
+    Ok(http::Response::from_parts(parts, bs))
+}
+
+#[cfg(target_arch = "wasm32")]
+async fn http_send_wasm(client: &Client, req: Request) -> Result<http::Response<Bytes>> {
+    let (tx, rx) = oneshot::channel();
+    let client = client.clone();
+
+    // reqwest's wasm client is !Send, so drive the request on the local executor
+    // and forward the result back through a channel to satisfy HttpSend's Send requirement.
+    spawn_local(async move {
+        let result = async move {
+            let resp = client
+                .execute(req)
+                .await
+                .map_err(|e| Error::unexpected("failed to send HTTP request").with_source(e))?;
+
+            let status = resp.status();
+            let headers = resp.headers().clone();
+            let body = resp
+                .bytes()
+                .await
+                .map_err(|e| Error::unexpected("failed to collect response body").with_source(e))?;
+
+            let mut response = http::Response::builder()
+                .status(status)
+                .body(body)
+                .map_err(|e| Error::unexpected("failed to build HTTP response").with_source(e))?;
+            *response.headers_mut() = headers;
+            Ok(response)
+        }
+        .await;
+
+        let _ = tx.send(result);
+    });
+
+    rx.await
+        .map_err(|_| Error::unexpected("failed to receive response from wasm task"))?
+}