Skip to content

Commit c5d3e12

Browse files
authored
feat: feature gate json, websocket and http; enable them by default (#16)
* feat: feature gate json support * feat: feature gate weboscket api * ci: check websocket and json features seperately in CI, check no default features * feat: feature gate the http API * refactor: use futures-core and futures-sink instead of depending on whole of futures * ci: test http feature seperately in CI * fix: only compile error conversion funcs if either APIs are enabled * fix: add futures to dev-deps for tests, fix doc test
1 parent f6531f5 commit c5d3e12

File tree

7 files changed

+111
-57
lines changed

7 files changed

+111
-57
lines changed

.github/workflows/ci.yml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,30 @@ jobs:
6868
with:
6969
command: clippy
7070
args: --target wasm32-unknown-unknown -- -D warnings
71+
72+
- name: Run clippy (no default features)
73+
uses: actions-rs/cargo@v1
74+
with:
75+
command: clippy
76+
args: --no-default-features --target wasm32-unknown-unknown -- -D warnings
77+
78+
- name: Run clippy (with json feature)
79+
uses: actions-rs/cargo@v1
80+
with:
81+
command: clippy
82+
args: --no-default-features --features json --target wasm32-unknown-unknown -- -D warnings
83+
84+
- name: Run clippy (with websocket feature)
85+
uses: actions-rs/cargo@v1
86+
with:
87+
command: clippy
88+
args: --no-default-features --features websocket --target wasm32-unknown-unknown -- -D warnings
89+
90+
- name: Run clippy (with http feature)
91+
uses: actions-rs/cargo@v1
92+
with:
93+
command: clippy
94+
args: --no-default-features --features http --target wasm32-unknown-unknown -- -D warnings
7195

7296
test:
7397
name: Test

Cargo.toml

Lines changed: 55 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -13,47 +13,66 @@ exclude = [
1313
".idea",
1414
]
1515

16+
[package.metadata.docs.rs]
17+
all-features = true
18+
19+
[features]
20+
default = ["json", "websocket", "http"]
21+
22+
# Enables `.json()` on `Response`
23+
json = ["wasm-bindgen/serde-serialize", "serde", "serde_json"]
24+
# Enables the WebSocket API
25+
websocket = [
26+
'web-sys/WebSocket',
27+
'web-sys/ErrorEvent',
28+
'web-sys/FileReader',
29+
'web-sys/MessageEvent',
30+
'web-sys/ProgressEvent',
31+
'web-sys/CloseEvent',
32+
'web-sys/BinaryType',
33+
'web-sys/Blob',
34+
"async-broadcast",
35+
"pin-project",
36+
"futures-core",
37+
"futures-sink",
38+
]
39+
# Enables the HTTP API
40+
http = [
41+
'web-sys/Headers',
42+
'web-sys/Request',
43+
'web-sys/RequestInit',
44+
'web-sys/RequestMode',
45+
'web-sys/Response',
46+
'web-sys/Window',
47+
'web-sys/RequestCache',
48+
'web-sys/RequestCredentials',
49+
'web-sys/ObserverCallback',
50+
'web-sys/RequestRedirect',
51+
'web-sys/ReferrerPolicy',
52+
'web-sys/AbortSignal',
53+
'web-sys/ReadableStream',
54+
'web-sys/Blob',
55+
'web-sys/FormData',
56+
]
57+
1658
[dependencies]
17-
wasm-bindgen = { version = "0.2", features = ["serde-serialize"] }
59+
wasm-bindgen = "0.2"
60+
web-sys = "0.3"
1861
js-sys = "0.3"
62+
gloo-utils = "0.1.0"
63+
1964
wasm-bindgen-futures = "0.4"
20-
serde = { version = "1.0", features = ["derive"] }
21-
serde_json = "1.0"
65+
futures-core = { version = "0.3", optional = true }
66+
futures-sink = { version = "0.3", optional = true }
67+
2268
thiserror = "1.0"
23-
futures = "0.3.14"
24-
gloo-utils = "0.1.0"
2569

26-
async-broadcast = "0.3"
27-
pin-project = "1"
28-
29-
[dependencies.web-sys]
30-
version = "0.3.4"
31-
features = [
32-
'Headers',
33-
'Request',
34-
'RequestInit',
35-
'RequestMode',
36-
'Response',
37-
'Window',
38-
'RequestCache',
39-
'RequestCredentials',
40-
'ObserverCallback',
41-
'RequestRedirect',
42-
'ReferrerPolicy',
43-
'AbortSignal',
44-
'ReadableStream',
45-
'Blob',
46-
'FormData',
47-
'FileReader',
48-
'CloseEvent',
49-
50-
'WebSocket',
51-
'ErrorEvent',
52-
'FileReader',
53-
'MessageEvent',
54-
'ProgressEvent',
55-
'BinaryType',
56-
]
70+
serde = { version = "1.0", features = ["derive"], optional = true }
71+
serde_json = { version = "1.0", optional = true }
72+
73+
async-broadcast = { version = "0.3", optional = true }
74+
pin-project = { version = "1.0", optional = true }
5775

5876
[dev-dependencies]
5977
wasm-bindgen-test = "0.3"
78+
futures = "0.3"

src/error.rs

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use gloo_utils::errors::JsError;
22
use thiserror::Error as ThisError;
3-
use wasm_bindgen::JsValue;
43

54
/// All the errors returned by this crate.
65
#[derive(Debug, ThisError)]
@@ -9,6 +8,7 @@ pub enum Error {
98
#[error("{0}")]
109
JsError(JsError),
1110
/// Error returned by `serde` during deserialization.
11+
#[cfg(feature = "json")]
1212
#[error("{0}")]
1313
SerdeError(
1414
#[source]
@@ -17,13 +17,22 @@ pub enum Error {
1717
),
1818
}
1919

20-
pub(crate) fn js_to_error(js_value: JsValue) -> Error {
21-
Error::JsError(js_to_js_error(js_value))
22-
}
20+
#[cfg(any(feature = "http", feature = "websocket"))]
21+
pub(crate) use conversion::*;
22+
#[cfg(any(feature = "http", feature = "websocket"))]
23+
mod conversion {
24+
use gloo_utils::errors::JsError;
25+
use wasm_bindgen::JsValue;
26+
27+
#[cfg(feature = "http")]
28+
pub(crate) fn js_to_error(js_value: JsValue) -> super::Error {
29+
super::Error::JsError(js_to_js_error(js_value))
30+
}
2331

24-
pub(crate) fn js_to_js_error(js_value: JsValue) -> JsError {
25-
match JsError::try_from(js_value) {
26-
Ok(error) => error,
27-
Err(_) => unreachable!("JsValue passed is not an Error type -- this is a bug"),
32+
pub(crate) fn js_to_js_error(js_value: JsValue) -> JsError {
33+
match JsError::try_from(js_value) {
34+
Ok(error) => error,
35+
Err(_) => unreachable!("JsValue passed is not an Error type -- this is a bug"),
36+
}
2837
}
2938
}

src/http.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,15 @@
1515
1616
use crate::{js_to_error, Error};
1717
use js_sys::{ArrayBuffer, Uint8Array};
18-
use serde::de::DeserializeOwned;
1918
use std::fmt;
2019
use wasm_bindgen::prelude::*;
2120
use wasm_bindgen::JsCast;
2221
use wasm_bindgen_futures::JsFuture;
2322
use web_sys::window;
23+
24+
#[cfg(feature = "json")]
25+
use serde::de::DeserializeOwned;
26+
2427
pub use web_sys::{
2528
AbortSignal, FormData, Headers, ObserverCallback, ReadableStream, ReferrerPolicy, RequestCache,
2629
RequestCredentials, RequestMode, RequestRedirect,
@@ -259,6 +262,7 @@ impl Response {
259262
}
260263

261264
/// Gets and parses the json.
265+
#[cfg(feature = "json")]
262266
pub async fn json<T: DeserializeOwned>(&self) -> Result<T, Error> {
263267
let promise = self.response.json().map_err(js_to_error)?;
264268
let json = JsFuture::from(promise).await.map_err(js_to_error)?;

src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010
)]
1111

1212
mod error;
13+
#[cfg(feature = "http")]
1314
pub mod http;
15+
#[cfg(feature = "websocket")]
1416
pub mod websocket;
1517

1618
pub use error::*;

src/websocket/futures.rs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,16 @@
1111
//! # ($($expr:expr),*) => {{}};
1212
//! # }
1313
//! # fn no_run() {
14-
//! let mut ws = WebSocket::open("wss://echo.websocket.org").unwrap();
14+
//! let mut ws = WebSocket::open("wss://echo.websocket.org").unwrap();
15+
//! let (mut write, mut read) = ws.split();
1516
//!
16-
//! spawn_local({
17-
//! let mut ws = ws.clone();
18-
//! async move {
19-
//! ws.send(Message::Text(String::from("test"))).await.unwrap();
20-
//! ws.send(Message::Text(String::from("test 2"))).await.unwrap();
21-
//! }
17+
//! spawn_local(async move {
18+
//! write.send(Message::Text(String::from("test"))).await.unwrap();
19+
//! write.send(Message::Text(String::from("test 2"))).await.unwrap();
2220
//! });
2321
//!
2422
//! spawn_local(async move {
25-
//! while let Some(msg) = ws.next().await {
23+
//! while let Some(msg) = read.next().await {
2624
//! console_log!(format!("1. {:?}", msg))
2725
//! }
2826
//! console_log!("WebSocket Closed")
@@ -35,8 +33,8 @@ use crate::websocket::{
3533
Message, State, WebSocketError,
3634
};
3735
use async_broadcast::Receiver;
38-
use futures::ready;
39-
use futures::{Sink, Stream};
36+
use futures_core::{ready, Stream};
37+
use futures_sink::Sink;
4038
use gloo_utils::errors::JsError;
4139
use pin_project::{pin_project, pinned_drop};
4240
use std::cell::RefCell;

tests/http.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
use js_sys::Uint8Array;
21
use reqwasm::http::*;
32
use serde::{Deserialize, Serialize};
4-
use wasm_bindgen::JsValue;
53
use wasm_bindgen_test::*;
64

75
wasm_bindgen_test_configure!(run_in_browser);

0 commit comments

Comments
 (0)