Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add response_body function to r2::ObjectBody. #462

Merged
merged 1 commit into from
Mar 7, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion worker/src/r2/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ use worker_sys::{
R2UploadedPart as EdgeR2UploadedPart,
};

use crate::{env::EnvBinding, ByteStream, Date, Error, FixedLengthStream, Headers, Result};
use crate::{
env::EnvBinding, ByteStream, Date, Error, FixedLengthStream, Headers, ResponseBody, Result,
};

mod builder;

Expand Down Expand Up @@ -282,6 +284,19 @@ impl<'body> ObjectBody<'body> {
})
}

/// Returns a [ResponseBody] containing the data in the [Object].
///
/// This function can be used to hand off the [Object] data to the workers runtime for streaming
/// to the client in a [crate::Response]. This ensures that the worker does not consume CPU time
/// while the streaming occurs, which can be significant if instead [ObjectBody::stream] is used.
pub fn response_body(self) -> Result<ResponseBody> {
if self.inner.body_used() {
return Err(Error::BodyUsed);
}

Ok(ResponseBody::Stream(self.inner.body()))
}

pub async fn bytes(self) -> Result<Vec<u8>> {
let js_buffer = JsFuture::from(self.inner.array_buffer()).await?;
let js_buffer = Uint8Array::new(&js_buffer);
Expand Down
Loading