use http_body_util streaming body

This commit is contained in:
Toshit Chawda 2024-10-29 22:09:15 -07:00
parent 7f3d122108
commit f226dd9939
No known key found for this signature in database
GPG key ID: 91480ED99E2B3D9D
3 changed files with 233 additions and 253 deletions

View file

@ -17,6 +17,7 @@ import initEpoxy, { EpoxyClient, EpoxyClientOptions, EpoxyHandlers, info as epox
const wisp_url = params.get("wisp") || "ws://localhost:4000/";
const wisp_v1 = params.has("v1");
const wisp_udp = params.has("udp_extension");
const disable_certverif = params.has("disable_certverif");
console.log(
"%cWASM is significantly slower with DevTools open!",
"color:red;font-size:3rem;font-weight:bold"
@ -30,11 +31,13 @@ import initEpoxy, { EpoxyClient, EpoxyClientOptions, EpoxyHandlers, info as epox
window.scrollTo(0, document.body.scrollHeight);
}
try {
await initEpoxy();
let epoxy_client_options = new EpoxyClientOptions();
epoxy_client_options.user_agent = navigator.userAgent;
epoxy_client_options.wisp_v2 = !wisp_v1;
epoxy_client_options.udp_extension_required = wisp_udp;
epoxy_client_options.disable_certificate_validation = disable_certverif;
let epoxy_client;
@ -295,8 +298,12 @@ import initEpoxy, { EpoxyClient, EpoxyClientOptions, EpoxyHandlers, info as epox
console.time();
let resp = await epoxy_client.fetch(test_url);
console.log(resp, resp.rawHeaders);
log(await resp.text());
log(await resp.arrayBuffer());
console.timeEnd();
}
log("done");
} catch (err) {
console.error(err);
log(err.stack);
}
})();

View file

@ -17,6 +17,7 @@ use http::{
uri::{InvalidUri, InvalidUriParts},
HeaderName, HeaderValue, Method, Request, Response,
};
use http_body_util::BodyDataStream;
use hyper::{body::Incoming, Uri};
use hyper_util_wasm::client::legacy::Client;
#[cfg(feature = "full")]
@ -27,8 +28,8 @@ use stream_provider::{StreamProvider, StreamProviderService};
use thiserror::Error;
use utils::{
asyncread_to_readablestream, convert_body, entries_of_object, from_entries, is_null_body,
is_redirect, object_get, object_set, object_truthy, ws_protocol, IncomingBody, UriExt,
WasmExecutor, WispTransportRead, WispTransportWrite,
is_redirect, object_get, object_set, object_truthy, ws_protocol, UriExt, WasmExecutor,
WispTransportRead, WispTransportWrite,
};
use wasm_bindgen::prelude::*;
use wasm_bindgen_futures::JsFuture;
@ -701,7 +702,7 @@ impl EpoxyClient {
_ => None,
};
let response_body = IncomingBody::new(response.into_body()).into_async_read();
let response_body = BodyDataStream::new(response.into_body()).map_err(std::io::Error::other).into_async_read();
let decompressed_body = match compression {
Some(alg) => match alg {
EpoxyCompression::Gzip => {
@ -719,7 +720,7 @@ impl EpoxyClient {
};
} else {
let response_stream = if !is_null_body(response.status().as_u16()) {
let response_body = IncomingBody::new(response.into_body()).into_async_read();
let response_body = BodyDataStream::new(response.into_body()).map_err(std::io::Error::other).into_async_read();
Some(asyncread_to_readablestream(Box::pin(response_body), self.buffer_size))
} else {
None

View file

@ -17,7 +17,7 @@ use futures_rustls::{
};
use futures_util::{ready, AsyncRead, AsyncWrite, Future, Stream, StreamExt, TryStreamExt};
use http::{HeaderValue, Uri};
use hyper::{body::Body, rt::Executor};
use hyper::rt::Executor;
use js_sys::{Array, ArrayBuffer, JsString, Object, Uint8Array};
use pin_project_lite::pin_project;
use rustls_pki_types::{CertificateDer, ServerName, UnixTime};
@ -90,34 +90,6 @@ where
}
}
pin_project! {
pub struct IncomingBody {
#[pin]
incoming: hyper::body::Incoming,
}
}
impl IncomingBody {
pub fn new(incoming: hyper::body::Incoming) -> IncomingBody {
IncomingBody { incoming }
}
}
impl Stream for IncomingBody {
type Item = std::io::Result<Bytes>;
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
self.project().incoming.poll_frame(cx).map(|x| {
x.map(|x| {
x.map_err(std::io::Error::other).and_then(|x| {
x.into_data().map_err(|_| {
std::io::Error::other("trailer frame recieved; not implemented")
})
})
})
})
}
}
pin_project! {
#[derive(Debug)]
pub struct ReaderStream<R> {