Repository: awslabs/llrt
File: llrt_core/src/modules/http/response.rs (Rust)

Code before:
            start,
            status: status.as_u16(),
            status_text: None,
            redirected,
            headers,
            body_attributes,
            abort_receiver,
        })
    }

    async fn take_bytes(&mut self, ctx: &Ctx<'js>) -> Result<Option<Vec<u8>>> {
        let bytes = match &mut self.body {
            Some(BodyVariant::Incoming(incoming)) => {
                let mut body = incoming
                    .take()
                    .ok_or(Exception::throw_type(ctx, "Already read"))?;
                let bytes = if let Some(abort_signal) = &self.abort_receiver {
                    select! {
                        err = abort_signal.recv() => return Err(ctx.throw(err)),
                        collected_body = body.body_mut().collect() => collected_body.or_throw(ctx)?.to_bytes()
                    }
                } else {
                    body.body_mut().collect().await.or_throw(ctx)?.to_bytes()
                };

                if self.body_attributes.content_encoding.is_none() {
                    return Ok(Some(bytes.to_vec()));
                }

                let content_encoding = self.body_attributes.content_encoding.clone();
                let mut data: Vec<u8> = Vec::new();

                match content_encoding.unwrap().as_str() {
                    "gzip" => GzDecoder::new(&bytes[..]).read_to_end(&mut data)?,
                    "deflate" => ZlibDecoder::new(&bytes[..]).read_to_end(&mut data)?,
                    "br" => BrotliDecoder::new(&bytes[..]).read_to_end(&mut data)?,
                    "zstd" => ZstdDecoder::new(&bytes[..])?.read_to_end(&mut data)?,
                    _ => return Err(Exception::throw_message(ctx, "Unsupported encoding")),
                };
                data
            },
            Some(BodyVariant::Provided(provided)) => {
                if let Some(blob) = get_class::<Blob>(provided)? {
                    let blob = blob.borrow();
                    blob.get_bytes()
                } else {
                    get_bytes(ctx, provided.clone())?
                }
            },
            None => return Ok(None),
        };

Review comment (suggestion):
We can do the other way around and do `if let Some(encoding) =  self.body_attributes.content_encoding {...}` which would avoid the unwrap below.

Code after:
            start,
            status: status.as_u16(),
            status_text: None,
            redirected,
            headers,
            body_attributes,
            abort_receiver,
        })
    }

    async fn take_bytes(&mut self, ctx: &Ctx<'js>) -> Result<Option<Vec<u8>>> {
        let bytes = match &mut self.body {
            Some(BodyVariant::Incoming(incoming)) => {
                let mut body = incoming
                    .take()
                    .ok_or(Exception::throw_type(ctx, "Already read"))?;
                let bytes = if let Some(abort_signal) = &self.abort_receiver {
                    select! {
                        err = abort_signal.recv() => return Err(ctx.throw(err)),
                        collected_body = body.body_mut().collect() => collected_body.or_throw(ctx)?.to_bytes()
                    }
                } else {
                    body.body_mut().collect().await.or_throw(ctx)?.to_bytes()
                };

                if let Some(content_encoding) = &self.body_attributes.content_encoding {
                    let mut data: Vec<u8> = Vec::with_capacity(bytes.len());
                    match content_encoding.as_str() {
                        "zstd" => ZstdDecoder::new(&bytes[..])?.read_to_end(&mut data)?,
                        "br" => BrotliDecoder::new(&bytes[..]).read_to_end(&mut data)?,
                        "gzip" => GzDecoder::new(&bytes[..]).read_to_end(&mut data)?,
                        "deflate" => ZlibDecoder::new(&bytes[..]).read_to_end(&mut data)?,
                        _ => return Err(Exception::throw_message(ctx, "Unsupported encoding")),
                    };
                    data
                } else {
                    bytes.to_vec()
                }
            },
            Some(BodyVariant::Provided(provided)) => {
                if let Some(blob) = get_class::<Blob>(provided)? {
                    let blob = blob.borrow();
                    blob.get_bytes()
                } else {
                    get_bytes(ctx, provided.clone())?
                }
            },
            None => return Ok(None),
        };

        Ok(Some(bytes))