MCPcopy Create free account
hub / github.com/awslabs/llrt / take_bytes

Method take_bytes

modules/llrt_fetch/src/response.rs:868–958  ·  view source on GitHub ↗
(&self, ctx: &Ctx<'js>)

Source from the content-addressed store, hash-verified

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);

Callers 6

textMethod · 0.80
jsonMethod · 0.80
array_bufferMethod · 0.80
bytesMethod · 0.80
blobMethod · 0.80
form_dataMethod · 0.80

Calls 12

take_providedFunction · 0.85
validate_stream_usableFunction · 0.85
take_incomingFunction · 0.85
okMethod · 0.80
decompress_chunkMethod · 0.80
finishMethod · 0.80
as_objectMethod · 0.80
readMethod · 0.65
writeMethod · 0.65
to_stringMethod · 0.45
is_emptyMethod · 0.45
cloneMethod · 0.45

Tested by

no test coverage detected