fix issue with null bodies, move npm package to client/

This commit is contained in:
Toshit Chawda 2024-03-02 20:16:10 -08:00
parent 4edf27198a
commit 75c48ccded
No known key found for this signature in database
GPG key ID: 91480ED99E2B3D9D
8 changed files with 79 additions and 75 deletions

View file

@ -352,42 +352,48 @@ impl EpoxyClient {
.status(resp.status().as_u16())
.status_text(resp.status().canonical_reason().unwrap_or_default());
let compression = match resp
.headers()
.get("Content-Encoding")
.and_then(|val| val.to_str().ok())
.unwrap_or_default()
{
"gzip" => Some(EpxCompression::Gzip),
"br" => Some(EpxCompression::Brotli),
_ => None,
};
let stream = if !utils::is_null_body(resp.status().as_u16()) {
let compression = match resp
.headers()
.get("Content-Encoding")
.and_then(|val| val.to_str().ok())
.unwrap_or_default()
{
"gzip" => Some(EpxCompression::Gzip),
"br" => Some(EpxCompression::Brotli),
_ => None,
};
let incoming_body = IncomingBody::new(resp.into_body());
let decompressed_body = match compression {
Some(alg) => match alg {
EpxCompression::Gzip => Either::Left(Either::Left(ReaderStream::new(
async_comp::GzipDecoder::new(StreamReader::new(incoming_body)),
))),
EpxCompression::Brotli => Either::Left(Either::Right(ReaderStream::new(
async_comp::BrotliDecoder::new(StreamReader::new(incoming_body)),
))),
},
None => Either::Right(incoming_body),
};
let stream = wasm_streams::ReadableStream::from_stream(decompressed_body.map(|x| {
Ok(Uint8Array::from(
x.replace_err_jv("Failed to get frame from response")?
.as_ref(),
let incoming_body = IncomingBody::new(resp.into_body());
let decompressed_body = match compression {
Some(alg) => match alg {
EpxCompression::Gzip => Either::Left(Either::Left(ReaderStream::new(
async_comp::GzipDecoder::new(StreamReader::new(incoming_body)),
))),
EpxCompression::Brotli => Either::Left(Either::Right(ReaderStream::new(
async_comp::BrotliDecoder::new(StreamReader::new(incoming_body)),
))),
},
None => Either::Right(incoming_body),
};
Some(
wasm_streams::ReadableStream::from_stream(decompressed_body.map(|x| {
Ok(Uint8Array::from(
x.replace_err_jv("Failed to get frame from response")?
.as_ref(),
)
.into())
}))
.into_raw(),
)
.into())
}));
} else {
None
};
let resp = web_sys::Response::new_with_opt_readable_stream_and_init(
Some(&stream.into_raw()),
&respinit,
)
.replace_err("Failed to make response")?;
let resp =
web_sys::Response::new_with_opt_readable_stream_and_init(stream.as_ref(), &respinit)
.replace_err("Failed to make response")?;
Object::define_property(
&resp,