runtime/globals/fetch/response/
body.rs

1/*
2 * This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
5 */
6
7use http_body_util::BodyExt as _;
8use ion::{Result, Traceable};
9
10use crate::globals::fetch::body::{Body, FetchBody};
11
12#[derive(Traceable)]
13pub enum ResponseBody {
14	Fetch(FetchBody),
15	Hyper(#[trace(no_trace)] Body),
16}
17
18impl ResponseBody {
19	pub async fn read_to_bytes(self) -> Result<Vec<u8>> {
20		let body = match self {
21			ResponseBody::Fetch(body) => body.to_http_body(),
22			ResponseBody::Hyper(body) => body,
23		};
24
25		Ok(body.collect().await?.to_bytes().to_vec())
26	}
27}