Skip to content

Commit

Permalink
fix rkyv deserialization
Browse files Browse the repository at this point in the history
  • Loading branch information
gbj committed Jan 19, 2024
1 parent a519859 commit 2a9e502
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
22 changes: 18 additions & 4 deletions server_fn/src/codec/rkyv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ use crate::{
response::{ClientRes, Res},
};
use bytes::Bytes;
use futures::StreamExt;
use http::Method;
use rkyv::{
de::deserializers::SharedDeserializeMap, ser::serializers::AllocSerializer,
validation::validators::DefaultValidator, Archive, CheckBytes, Deserialize,
Serialize,
validation::validators::DefaultValidator, AlignedVec, Archive, CheckBytes,
Deserialize, Serialize,
};

/// Pass arguments and receive responses using `rkyv` in a `POST` request.
Expand Down Expand Up @@ -49,8 +50,21 @@ where
+ Deserialize<T, SharedDeserializeMap>,
{
async fn from_req(req: Request) -> Result<Self, ServerFnError<CustErr>> {
let body_bytes = req.try_into_bytes().await?;
rkyv::from_bytes::<T>(body_bytes.as_ref())
let mut aligned = AlignedVec::new();
let mut body_stream = Box::pin(req.try_into_stream()?);
while let Some(chunk) = body_stream.next().await {
match chunk {
Err(e) => {
return Err(ServerFnError::Deserialization(e.to_string()))
}
Ok(bytes) => {
for byte in bytes {
aligned.push(byte);
}
}
}
}
rkyv::from_bytes::<T>(aligned.as_ref())
.map_err(|e| ServerFnError::Args(e.to_string()))
}
}
Expand Down
2 changes: 1 addition & 1 deletion server_fn/src/request/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ where
self,
) -> impl Future<Output = Result<String, ServerFnError<CustErr>>> + Send;

/// Attempts to convert the body of the request into a string.
/// Attempts to convert the body of the request into a stream of bytes.
fn try_into_stream(
self,
) -> Result<
Expand Down

0 comments on commit 2a9e502

Please sign in to comment.