Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: Update for Tonic v0.12 #60

Merged
merged 3 commits into from
Jul 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
18 changes: 7 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,14 @@ 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(Frame::data))),
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
55 changes: 12 additions & 43 deletions src/response_body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(_))
Expand Down Expand Up @@ -144,11 +140,13 @@ 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)) => {
if let Some(data) = frame.data_ref() {
if let Err(e) = this.buf.append(data.clone()) {
return Poll::Ready(Err(e));
}
};

Poll::Ready(Ok(()))
}
Expand Down Expand Up @@ -266,14 +264,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 +294,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 +304,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/gzip/client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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",
] }

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
Loading