Skip to content

Commit 772154e

Browse files
committed
feat: Config support for wasm_client
Only supports `timeout`, unfortunately, and requires async-std...
1 parent cdb4972 commit 772154e

File tree

3 files changed

+62
-7
lines changed

3 files changed

+62
-7
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ docs = ["h1_client", "curl_client", "wasm_client", "hyper_client", "unstable-con
2626
h1_client = ["async-h1", "async-std", "deadpool", "futures"]
2727
native_client = ["curl_client", "wasm_client"]
2828
curl_client = ["isahc", "async-std"]
29-
wasm_client = ["js-sys", "web-sys", "wasm-bindgen", "wasm-bindgen-futures", "futures"]
29+
wasm_client = ["js-sys", "web-sys", "wasm-bindgen", "wasm-bindgen-futures", "futures", "async-std"]
3030
hyper_client = ["hyper", "hyper-tls", "http-types/hyperium_http", "futures-util", "tokio"]
3131

3232
native-tls = ["async-native-tls"]

src/config.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,14 @@ pub struct Config {
1111
/// HTTP/1.1 `keep-alive` (connection pooling).
1212
///
1313
/// Default: `true`.
14+
///
15+
/// Note: Does nothing on `wasm_client`.
1416
pub http_keep_alive: bool,
1517
/// TCP `NO_DELAY`.
1618
///
1719
/// Default: `false`.
20+
///
21+
/// Note: Does nothing on `wasm_client`.
1822
pub tcp_no_delay: bool,
1923
/// Connection timeout duration.
2024
///

src/wasm.rs

Lines changed: 57 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,34 @@
11
//! http-client implementation for fetch
22
3-
use super::{http_types::Headers, Body, Error, HttpClient, Request, Response};
4-
5-
use futures::prelude::*;
3+
#[cfg(feature = "unstable-config")]
4+
use std::convert::Infallible;
65

76
use std::convert::TryFrom;
87
use std::pin::Pin;
98
use std::task::{Context, Poll};
109

10+
use futures::prelude::*;
11+
12+
use crate::Config;
13+
14+
use super::{http_types::Headers, Body, Error, HttpClient, Request, Response};
15+
1116
/// WebAssembly HTTP Client.
1217
#[derive(Debug)]
1318
pub struct WasmClient {
14-
_priv: (),
19+
config: Config,
1520
}
1621

1722
impl WasmClient {
1823
/// Create a new instance.
1924
pub fn new() -> Self {
20-
Self { _priv: () }
25+
Self { config: Config::default() }
26+
}
27+
}
28+
29+
impl Default for WasmClient {
30+
fn default() -> Self {
31+
Self::new()
2132
}
2233
}
2334

@@ -30,9 +41,19 @@ impl HttpClient for WasmClient {
3041
'a: 'async_trait,
3142
Self: 'async_trait,
3243
{
44+
let config = self.config.clone();
45+
3346
InnerFuture::new(async move {
3447
let req: fetch::Request = fetch::Request::new(req).await?;
35-
let mut res = req.send().await?;
48+
let conn = req.send();
49+
#[cfg(feature = "unstable-config")]
50+
let mut res = if let Some(timeout) = config.timeout {
51+
async_std::future::timeout(timeout, conn).await??
52+
} else {
53+
conn.await?
54+
};
55+
#[cfg(not(feature = "unstable-config"))]
56+
let mut res = conn.await?;
3657

3758
let body = res.body_bytes();
3859
let mut response =
@@ -46,6 +67,36 @@ impl HttpClient for WasmClient {
4667
Ok(response)
4768
})
4869
}
70+
71+
#[cfg_attr(feature = "docs", doc(cfg(feature = "unstable-config")))]
72+
#[cfg(feature = "unstable-config")]
73+
/// Override the existing configuration with new configuration.
74+
///
75+
/// Config options may not impact existing connections.
76+
fn set_config(&mut self, config: Config) -> http_types::Result<()> {
77+
self.config = config;
78+
79+
Ok(())
80+
}
81+
82+
#[cfg_attr(feature = "docs", doc(cfg(feature = "unstable-config")))]
83+
#[cfg(feature = "unstable-config")]
84+
/// Get the current configuration.
85+
fn config(&self) -> &Config {
86+
&self.config
87+
}
88+
}
89+
90+
#[cfg_attr(feature = "docs", doc(cfg(feature = "unstable-config")))]
91+
#[cfg(feature = "unstable-config")]
92+
impl TryFrom<Config> for WasmClient {
93+
type Error = Infallible;
94+
95+
fn try_from(config: Config) -> Result<Self, Self::Error> {
96+
Ok(Self {
97+
config,
98+
})
99+
}
49100
}
50101

51102
struct InnerFuture {

0 commit comments

Comments
 (0)