(&self, ctx: &Ctx<'js>)
| 866 | #[allow(clippy::await_holding_lock)] |
| 867 | #[allow(clippy::readonly_write_lock)] |
| 868 | async fn take_bytes(&self, ctx: &Ctx<'js>) -> Result<Option<Vec<u8>>> { |
| 869 | // Fast path: if no stream was ever created, skip all stream checks |
| 870 | // and take the incoming body directly without cloning Arc/String. |
| 871 | let body_stream_exists = self.body_stream.read().unwrap().is_some(); |
| 872 | |
| 873 | if !body_stream_exists { |
| 874 | let mut body_guard = self.body.write().unwrap(); |
| 875 | match &mut *body_guard { |
| 876 | BodyVariant::Incoming(incoming, enc) => { |
| 877 | let body = incoming |
| 878 | .write() |
| 879 | .unwrap() |
| 880 | .take() |
| 881 | .ok_or(Exception::throw_type(ctx, "Body is already read"))?; |
| 882 | let encoding = enc.take(); |
| 883 | drop(body_guard); |
| 884 | |
| 885 | let has_decoder = encoding |
| 886 | .as_deref() |
| 887 | .is_some_and(|e| !matches!(e, "" | "identity")); |
| 888 | |
| 889 | if !has_decoder { |
| 890 | let collected = body |
| 891 | .collect() |
| 892 | .await |
| 893 | .map_err(|e| Exception::throw_message(ctx, &e.to_string()))?; |
| 894 | return Ok(Some(collected.to_bytes().into())); |
| 895 | } |
| 896 | |
| 897 | let collected = body |
| 898 | .collect() |
| 899 | .await |
| 900 | .map_err(|e| Exception::throw_message(ctx, &e.to_string()))?; |
| 901 | let raw = collected.to_bytes(); |
| 902 | if let Some(mut dec) = encoding |
| 903 | .as_deref() |
| 904 | .and_then(|enc| StreamingDecoder::new(enc).ok()) |
| 905 | { |
| 906 | let mut decompressed = dec |
| 907 | .decompress_chunk(&raw) |
| 908 | .map_err(|e| Exception::throw_message(ctx, &e.to_string()))?; |
| 909 | let remaining = dec |
| 910 | .finish() |
| 911 | .map_err(|e| Exception::throw_message(ctx, &e.to_string()))?; |
| 912 | if !remaining.is_empty() { |
| 913 | decompressed.extend_from_slice(&remaining); |
| 914 | } |
| 915 | return Ok(Some(decompressed)); |
| 916 | } |
| 917 | return Ok(Some(raw.into())); |
| 918 | }, |
| 919 | BodyVariant::Empty => return Ok(None), |
| 920 | BodyVariant::Provided(None) => { |
| 921 | return Err(Exception::throw_type(ctx, "Body is already read")) |
| 922 | }, |
| 923 | BodyVariant::Provided(Some(_)) => { |
| 924 | // Fall through to take_provided |
| 925 | drop(body_guard); |
no test coverage detected