From 3538c0892b182f0a50bfed3d601b9b3caadfc23e Mon Sep 17 00:00:00 2001 From: Chris Joel <0xcda7a@gmail.com> Date: Wed, 24 Jul 2024 16:40:50 -0700 Subject: [PATCH 1/3] feat!: Update for Tonic v0.12 --- Cargo.toml | 7 ++-- src/body_stream.rs | 20 +++++------ src/call.rs | 4 +-- src/response_body.rs | 54 ++++++++--------------------- test-suite/simple/client/Cargo.toml | 6 ++-- 5 files changed, 33 insertions(+), 58 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 1d3b7d8..e2180a6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,13 +16,14 @@ base64 = "0.22.0" byteorder = "1.5.0" bytes = "1.5.0" futures-util = { version = "0.3.30", default-features = false } -http = "0.2.11" -http-body = "0.4.6" +http = "1" +http-body = "1" +http-body-util = "0.1" httparse = "1.8.0" js-sys = "0.3.69" pin-project = "1.1.5" thiserror = "1.0.57" -tonic = { version = "0.11.0", default-features = false } +tonic = { version = "0.12", default-features = false } tower-service = "0.3.2" wasm-bindgen = "0.2.92" wasm-bindgen-futures = "0.4.42" diff --git a/src/body_stream.rs b/src/body_stream.rs index 62e5769..e35fc7b 100644 --- a/src/body_stream.rs +++ b/src/body_stream.rs @@ -5,7 +5,7 @@ use std::{ use bytes::Bytes; use futures_util::{stream::empty, Stream, TryStreamExt}; -use http_body::Body; +use http_body::{Body, Frame}; use js_sys::Uint8Array; use wasm_streams::readable::IntoStream; @@ -47,18 +47,16 @@ impl Body for BodyStream { type Error = Error; - fn poll_data( + fn poll_frame( mut self: Pin<&mut Self>, cx: &mut Context<'_>, - ) -> Poll>> { - self.body_stream.as_mut().poll_next(cx) - } - - fn poll_trailers( - self: Pin<&mut Self>, - _: &mut Context<'_>, - ) -> Poll, Self::Error>> { - Poll::Ready(Ok(None)) + ) -> Poll, Self::Error>>> { + match self.body_stream.as_mut().poll_next(cx) { + Poll::Ready(maybe) => { + Poll::Ready(maybe.map(|result| result.map(|bytes| Frame::data(bytes)))) + } + Poll::Pending => Poll::Pending, + } } } diff --git a/src/call.rs b/src/call.rs index 993dc40..b23b618 100644 --- a/src/call.rs +++ b/src/call.rs @@ -3,7 +3,7 @@ use http::{ response::Builder, HeaderMap, HeaderValue, Request, Response, }; -use http_body::Body; +use http_body_util::BodyExt; use js_sys::{Array, Uint8Array}; use tonic::body::BoxBody; use wasm_bindgen::JsValue; @@ -58,7 +58,7 @@ fn prepare_headers(header_map: &HeaderMap) -> Result) -> Result, Error> { - let body = request.into_body().data().await.transpose()?; + let body = Some(request.collect().await?.to_bytes()); Ok(body.map(|bytes| Uint8Array::from(bytes.as_ref()).into())) } diff --git a/src/response_body.rs b/src/response_body.rs index 28d11cb..41008e3 100644 --- a/src/response_body.rs +++ b/src/response_body.rs @@ -144,11 +144,16 @@ impl ResponseBody { let this = self.project(); - match ready!(this.body_stream.poll_data(cx)) { - Some(Ok(data)) => { - if let Err(e) = this.buf.append(data) { - return Poll::Ready(Err(e)); - } + match ready!(this.body_stream.poll_frame(cx)) { + Some(Ok(frame)) => { + match frame.data_ref() { + Some(data) => { + if let Err(e) = this.buf.append(data.clone()) { + return Poll::Ready(Err(e)); + } + } + _ => (), + }; Poll::Ready(Ok(())) } @@ -266,14 +271,15 @@ impl Body for ResponseBody { type Error = Error; - fn poll_data( + fn poll_frame( mut self: Pin<&mut Self>, cx: &mut Context<'_>, - ) -> Poll>> { + ) -> Poll, Self::Error>>> { // Check if there's already some data in buffer and return that if self.data.is_some() { let data = self.data.take().unwrap(); - return Poll::Ready(Some(Ok(data.freeze()))); + + return Poll::Ready(Some(Ok(http_body::Frame::data(data.freeze())))); } // If reading data is finished return `None` @@ -295,7 +301,7 @@ impl Body for ResponseBody { if self.data.is_some() { // If data is available in buffer, return that let data = self.data.take().unwrap(); - return Poll::Ready(Some(Ok(data.freeze()))); + return Poll::Ready(Some(Ok(http_body::Frame::data(data.freeze())))); } else if self.state.finished_data() { // If we finished reading data continue return `None` return Poll::Ready(None); @@ -305,36 +311,6 @@ impl Body for ResponseBody { } } } - - fn poll_trailers( - mut self: Pin<&mut Self>, - cx: &mut Context<'_>, - ) -> Poll, Self::Error>> { - // If the state machine is complete, return trailer - if self.state.is_done() { - return Poll::Ready(Ok(self.trailer.take())); - } - - loop { - // Read bytes from stream - if let Err(e) = ready!(self.as_mut().read_stream(cx)) { - return Poll::Ready(Err(e)); - } - - // Step the state machine - if let Err(e) = self.as_mut().step() { - return Poll::Ready(Err(e)); - } - - if self.state.is_done() { - // If state machine is done, return trailer - return Poll::Ready(Ok(self.trailer.take())); - } else if self.finished_stream { - // If stream is finished but state machine is not done, return error - return Poll::Ready(Err(Error::MalformedResponse)); - } - } - } } impl Default for ResponseBody { diff --git a/test-suite/simple/client/Cargo.toml b/test-suite/simple/client/Cargo.toml index f1dd83c..aee6e2b 100644 --- a/test-suite/simple/client/Cargo.toml +++ b/test-suite/simple/client/Cargo.toml @@ -6,14 +6,14 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -prost = "0.12.3" -tonic = { version = "0.11.0", default-features = false, features = [ +prost = "0.13" +tonic = { version = "0.12", default-features = false, features = [ "prost", "codegen", ] } [build-dependencies] -tonic-build = { version = "0.11.0", default-features = false, features = [ +tonic-build = { version = "0.12", default-features = false, features = [ "prost", ] } From 6acf43ff991285744130b3985d96bcb2b80dbe82 Mon Sep 17 00:00:00 2001 From: Chris Joel <0xcda7a@gmail.com> Date: Wed, 24 Jul 2024 22:22:29 -0700 Subject: [PATCH 2/3] fix: Clippy errors --- src/body_stream.rs | 4 +--- src/response_body.rs | 13 +++---------- 2 files changed, 4 insertions(+), 13 deletions(-) diff --git a/src/body_stream.rs b/src/body_stream.rs index e35fc7b..b3052e7 100644 --- a/src/body_stream.rs +++ b/src/body_stream.rs @@ -52,9 +52,7 @@ impl Body for BodyStream { cx: &mut Context<'_>, ) -> Poll, Self::Error>>> { match self.body_stream.as_mut().poll_next(cx) { - Poll::Ready(maybe) => { - Poll::Ready(maybe.map(|result| result.map(|bytes| Frame::data(bytes)))) - } + Poll::Ready(maybe) => Poll::Ready(maybe.map(|result| result.map(Frame::data))), Poll::Pending => Poll::Pending, } } diff --git a/src/response_body.rs b/src/response_body.rs index 41008e3..08e4448 100644 --- a/src/response_body.rs +++ b/src/response_body.rs @@ -97,10 +97,6 @@ pub enum ReadState { } impl ReadState { - fn is_done(&self) -> bool { - matches!(self, ReadState::Done) - } - fn finished_data(&self) -> bool { matches!(self, ReadState::TrailerLength) || matches!(self, ReadState::Trailer(_)) @@ -146,13 +142,10 @@ impl ResponseBody { match ready!(this.body_stream.poll_frame(cx)) { Some(Ok(frame)) => { - match frame.data_ref() { - Some(data) => { - if let Err(e) = this.buf.append(data.clone()) { - return Poll::Ready(Err(e)); - } + if let Some(data) = frame.data_ref() { + if let Err(e) = this.buf.append(data.clone()) { + return Poll::Ready(Err(e)); } - _ => (), }; Poll::Ready(Ok(())) From 8606c125bb1b5219fba3fb9a350ff4652f88e082 Mon Sep 17 00:00:00 2001 From: Chris Joel <0xcda7a@gmail.com> Date: Wed, 24 Jul 2024 22:29:29 -0700 Subject: [PATCH 3/3] fix: Gzip tests --- test-suite/gzip/client/Cargo.toml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test-suite/gzip/client/Cargo.toml b/test-suite/gzip/client/Cargo.toml index 73ffd9b..7e5b45d 100644 --- a/test-suite/gzip/client/Cargo.toml +++ b/test-suite/gzip/client/Cargo.toml @@ -6,15 +6,15 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -prost = "0.12.3" -tonic = { version = "0.11.0", default-features = false, features = [ +prost = "0.13" +tonic = { version = "0.12", default-features = false, features = [ "prost", "codegen", "gzip", ] } [build-dependencies] -tonic-build = { version = "0.11.0", default-features = false, features = [ +tonic-build = { version = "0.12", default-features = false, features = [ "prost", ] }