diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index f894184..0d57357 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -13,7 +13,7 @@ env: jobs: rustfmt: name: rustfmt - runs-on: ubuntu-18.04 + runs-on: ubuntu-latest steps: - name: Checkout repository uses: actions/checkout@v2 @@ -33,7 +33,7 @@ jobs: clippy: name: clippy - runs-on: ubuntu-18.04 + runs-on: ubuntu-latest steps: - name: Checkout repository uses: actions/checkout@v2 @@ -45,6 +45,10 @@ jobs: toolchain: stable profile: minimal components: clippy + - name: install protoc + uses: taiki-e/install-action@v2 + with: + tool: protoc - name: Clippy uses: actions-rs/cargo@v1 with: @@ -52,7 +56,7 @@ jobs: build-test-x86_64-unknown-linux-gnu: name: build and test (x86_64-unknown-linux-gnu) - runs-on: ubuntu-18.04 + runs-on: ubuntu-latest steps: - name: Checkout repository uses: actions/checkout@v2 @@ -64,6 +68,10 @@ jobs: toolchain: stable profile: minimal components: rustfmt + - name: install protoc + uses: taiki-e/install-action@v2 + with: + tool: protoc - name: Build uses: actions-rs/cargo@v1 with: @@ -76,11 +84,11 @@ jobs: build-test-x86_64-apple-darwin: name: build and test (x86_64-apple-darwin) runs-on: macos-latest - env: - SELECT_XCODE: /Applications/Xcode_12.2.app + # env: + # SELECT_XCODE: /Applications/Xcode_12.2.app steps: - - name: Select XCode version - run: sudo xcode-select -s "${SELECT_XCODE}" +# - name: Select XCode version +# run: sudo xcode-select -s "${SELECT_XCODE}" - name: Checkout repository uses: actions/checkout@v2 with: @@ -90,6 +98,10 @@ jobs: with: toolchain: stable profile: minimal + - name: install protoc + uses: taiki-e/install-action@v2 + with: + tool: protoc - name: Build uses: actions-rs/cargo@v1 with: @@ -101,7 +113,7 @@ jobs: buuild-client-wasm32-unknown-unknown: name: build client (wasm32-unknown-unknown) - runs-on: ubuntu-18.04 + runs-on: ubuntu-latest steps: - name: Checkout repository uses: actions/checkout@v2 @@ -113,7 +125,11 @@ jobs: toolchain: stable profile: minimal components: rustfmt + - name: install protoc + uses: taiki-e/install-action@v2 + with: + tool: protoc - name: Install wasm-pack uses: jetli/wasm-pack-action@v0.3.0 - name: Build client - run: cd examples/wasm-client && wasm-pack build --target web + run: cd examples/wasm-client && RUSTFLAGS=--cfg=tokio_unstable wasm-pack build --target web diff --git a/README.md b/README.md index 0d37120..7ff05ce 100644 --- a/README.md +++ b/README.md @@ -31,6 +31,16 @@ python3 -m http.server // open http://0.0.0.0:8000/ in browser ``` +## Usage +You can currently use this as a git dependency. You will need to enable the `tokio_unstable` config flag for tonic to compile. +Update your `.cargo/config.toml`: +```toml +[build] +# This is necessary because tonic needs the tower `make` feature which in turn needs tokios io-std feature which doesn't +# compile on wasm unless tokio_unstable is active +rustflags = ["--cfg=tokio_unstable"] +``` + ## License * Apache License, Version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or diff --git a/examples/wasm-client/Cargo.toml b/examples/wasm-client/Cargo.toml index 58b0994..740d43d 100644 --- a/examples/wasm-client/Cargo.toml +++ b/examples/wasm-client/Cargo.toml @@ -2,7 +2,7 @@ name = "wasm-client" version = "0.1.0" authors = ["boxdot "] -edition = "2018" +edition = "2021" publish = false [lib] diff --git a/transport/src/lib.rs b/transport/src/lib.rs index 412c4d7..3c19b90 100644 --- a/transport/src/lib.rs +++ b/transport/src/lib.rs @@ -11,6 +11,7 @@ use std::fmt::Debug; use std::future::Future; use std::io; use std::pin::Pin; +#[cfg(feature = "native")] use std::sync::Arc; use std::task::{Context, Poll}; @@ -50,6 +51,7 @@ impl From for Error { #[derive(Clone, Default)] pub struct WsConnector { + #[cfg(feature = "native")] resolve_bearer_token: Option String + Sync + Send + 'static>>, } @@ -64,6 +66,7 @@ impl WsConnector { Default::default() } + #[cfg(feature = "native")] pub fn with_bearer_resolver( resolve_token: impl Fn() -> String + Send + Sync + 'static, ) -> Self { @@ -91,10 +94,12 @@ impl WsConnector { let mut request = dst.into_client_request()?; if let Some(resolver) = self.resolve_bearer_token.as_ref() { let token = resolver(); - request.headers_mut().typed_insert(Authorization::bearer(&token)?); + request + .headers_mut() + .typed_insert(Authorization::bearer(&token)?); } - let (ws_stream, _) = tokio_tungstenite::connect_async(dst).await?; + let (ws_stream, _) = tokio_tungstenite::connect_async(request).await?; Ok(WsConnection::from_combined_channel(ws_stream)) } } diff --git a/transport/src/native.rs b/transport/src/native.rs index 2a7eca8..34fbedb 100644 --- a/transport/src/native.rs +++ b/transport/src/native.rs @@ -29,7 +29,7 @@ impl WsConnection { io::ErrorKind::ConnectionAborted, TungsteniteError::ConnectionClosed, ))), - Ok(Message::Frame(_)) => {None} + Ok(Message::Frame(_)) => None, Err(e) => Some(Err(io::Error::new(io::ErrorKind::Other, e))), }) }); diff --git a/transport/src/web.rs b/transport/src/web.rs index fd8e14d..355e15f 100644 --- a/transport/src/web.rs +++ b/transport/src/web.rs @@ -11,14 +11,14 @@ use web_sys::{MessageEvent, WebSocket}; use std::future::Future; use std::pin::Pin; -use std::sync::Arc; +use std::rc::Rc; use std::task::{Context, Poll}; #[cfg(not(feature = "native"))] pub async fn connect(dst: http::Uri) -> Result { use futures_util::{future, stream::TryStreamExt, SinkExt}; - let ws = Ws(Arc::new(WebSocket::new(&dst.to_string())?)); + let ws = Ws(Rc::new(WebSocket::new(&dst.to_string())?)); (*ws).set_binary_type(web_sys::BinaryType::Arraybuffer); let client = WebConnection { ws, wake_fn: None }.await?; @@ -47,7 +47,7 @@ pub async fn connect(dst: http::Uri) -> Result { } #[derive(Debug, Clone)] -struct Ws(Arc); +struct Ws(Rc); unsafe impl Send for Ws {} @@ -127,9 +127,12 @@ impl Drop for WakeFn { #[derive(Debug)] pub struct WebClient { + #[allow(dead_code)] ws: Ws, + #[allow(dead_code)] rx: UnboundedReceiver, Error>>, - handlers: Arc, // keeps the callbacks alive + #[allow(dead_code)] + handlers: Rc, // keeps the callbacks alive } impl WebClient { @@ -147,7 +150,7 @@ impl WebClient { } }) as Box); - let handlers = Arc::new(Handlers::register( + let handlers = Rc::new(Handlers::register( ws.clone(), Some(message_fn), Some(close_fn), @@ -203,13 +206,14 @@ impl Drop for Handlers { #[derive(Debug)] struct WebClientSink { ws: Ws, - handlers: Arc, // keeps the callbacks alive + #[allow(dead_code)] + handlers: Rc, // keeps the callbacks alive } #[pin_project] struct WebClientStream { ws: Ws, - handlers: Arc, // keeps the callbacks alive + handlers: Rc, // keeps the callbacks alive #[pin] rx: UnboundedReceiver, Error>>, }