Skip to content

Commit

Permalink
feat!: Update for Tonic v0.12
Browse files Browse the repository at this point in the history
  • Loading branch information
cdata committed Jul 24, 2024
1 parent fa2f66e commit 3538c08
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 58 deletions.
7 changes: 4 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
20 changes: 9 additions & 11 deletions src/body_stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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<Option<Result<Self::Data, Self::Error>>> {
self.body_stream.as_mut().poll_next(cx)
}

fn poll_trailers(
self: Pin<&mut Self>,
_: &mut Context<'_>,
) -> Poll<Result<Option<http::HeaderMap>, Self::Error>> {
Poll::Ready(Ok(None))
) -> Poll<Option<Result<http_body::Frame<Self::Data>, 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))))

Check failure on line 56 in src/body_stream.rs

View workflow job for this annotation

GitHub Actions / Clippy Check

redundant closure
}
Poll::Pending => Poll::Pending,
}
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -58,7 +58,7 @@ fn prepare_headers(header_map: &HeaderMap<HeaderValue>) -> Result<Headers, Error
}

async fn prepare_body(request: Request<BoxBody>) -> Result<Option<JsValue>, 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()))
}

Expand Down
54 changes: 15 additions & 39 deletions src/response_body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {

Check failure on line 149 in src/response_body.rs

View workflow job for this annotation

GitHub Actions / Clippy Check

you seem to be trying to use `match` for destructuring a single pattern. Consider using `if let`
Some(data) => {
if let Err(e) = this.buf.append(data.clone()) {
return Poll::Ready(Err(e));
}
}
_ => (),
};

Poll::Ready(Ok(()))
}
Expand Down Expand Up @@ -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<Option<Result<Self::Data, Self::Error>>> {
) -> Poll<Option<Result<http_body::Frame<Self::Data>, 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`
Expand All @@ -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);
Expand All @@ -305,36 +311,6 @@ impl Body for ResponseBody {
}
}
}

fn poll_trailers(
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
) -> Poll<Result<Option<HeaderMap>, 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 {
Expand Down
6 changes: 3 additions & 3 deletions test-suite/simple/client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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",
] }

Expand Down

0 comments on commit 3538c08

Please sign in to comment.