From 2a9e50289335b792641d67a7e3ae03932a939a0e Mon Sep 17 00:00:00 2001 From: Greg Johnston Date: Fri, 19 Jan 2024 15:03:21 -0500 Subject: [PATCH] fix rkyv deserialization --- server_fn/src/codec/rkyv.rs | 22 ++++++++++++++++++---- server_fn/src/request/mod.rs | 2 +- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/server_fn/src/codec/rkyv.rs b/server_fn/src/codec/rkyv.rs index 2d9753ad1..2db8f3192 100644 --- a/server_fn/src/codec/rkyv.rs +++ b/server_fn/src/codec/rkyv.rs @@ -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. @@ -49,8 +50,21 @@ where + Deserialize, { async fn from_req(req: Request) -> Result> { - let body_bytes = req.try_into_bytes().await?; - rkyv::from_bytes::(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::(aligned.as_ref()) .map_err(|e| ServerFnError::Args(e.to_string())) } } diff --git a/server_fn/src/request/mod.rs b/server_fn/src/request/mod.rs index 785df34a5..2da49ecf3 100644 --- a/server_fn/src/request/mod.rs +++ b/server_fn/src/request/mod.rs @@ -99,7 +99,7 @@ where self, ) -> impl Future>> + 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<