fix rkyv deserialization

This commit is contained in:
Greg Johnston 2024-01-19 15:03:21 -05:00
parent a519859a66
commit 2a9e502893
2 changed files with 19 additions and 5 deletions

View File

@ -5,11 +5,12 @@ use crate::{
response::{ClientRes, Res}, response::{ClientRes, Res},
}; };
use bytes::Bytes; use bytes::Bytes;
use futures::StreamExt;
use http::Method; use http::Method;
use rkyv::{ use rkyv::{
de::deserializers::SharedDeserializeMap, ser::serializers::AllocSerializer, de::deserializers::SharedDeserializeMap, ser::serializers::AllocSerializer,
validation::validators::DefaultValidator, Archive, CheckBytes, Deserialize, validation::validators::DefaultValidator, AlignedVec, Archive, CheckBytes,
Serialize, Deserialize, Serialize,
}; };
/// Pass arguments and receive responses using `rkyv` in a `POST` request. /// Pass arguments and receive responses using `rkyv` in a `POST` request.
@ -49,8 +50,21 @@ where
+ Deserialize<T, SharedDeserializeMap>, + Deserialize<T, SharedDeserializeMap>,
{ {
async fn from_req(req: Request) -> Result<Self, ServerFnError<CustErr>> { async fn from_req(req: Request) -> Result<Self, ServerFnError<CustErr>> {
let body_bytes = req.try_into_bytes().await?; let mut aligned = AlignedVec::new();
rkyv::from_bytes::<T>(body_bytes.as_ref()) 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())) .map_err(|e| ServerFnError::Args(e.to_string()))
} }
} }

View File

@ -99,7 +99,7 @@ where
self, self,
) -> impl Future<Output = Result<String, ServerFnError<CustErr>>> + Send; ) -> 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( fn try_into_stream(
self, self,
) -> Result< ) -> Result<