diff --git a/lib/worker-sys/src/types/r2.rs b/lib/worker-sys/src/types/r2.rs index 34142ad..38480d8 100644 --- a/lib/worker-sys/src/types/r2.rs +++ b/lib/worker-sys/src/types/r2.rs @@ -5,7 +5,6 @@ mod object; mod object_body; mod objects; mod range; -mod uploaded_part; pub use bucket::*; pub use http_metadata::*; @@ -14,4 +13,3 @@ pub use object::*; pub use object_body::*; pub use objects::*; pub use range::*; -pub use uploaded_part::*; diff --git a/lib/worker-sys/src/types/r2/uploaded_part.rs b/lib/worker-sys/src/types/r2/uploaded_part.rs deleted file mode 100644 index c4421b4..0000000 --- a/lib/worker-sys/src/types/r2/uploaded_part.rs +++ /dev/null @@ -1,14 +0,0 @@ -use wasm_bindgen::prelude::*; - -#[wasm_bindgen] -extern "C" { - #[wasm_bindgen(extends=js_sys::Object)] - #[derive(Debug, Clone, PartialEq, Eq)] - pub type R2UploadedPart; - - #[wasm_bindgen(method, getter, js_name=partNumber)] - pub fn part_number(this: &R2UploadedPart) -> u16; - - #[wasm_bindgen(method, getter)] - pub fn etag(this: &R2UploadedPart) -> String; -} diff --git a/lib/worker/src/durable.rs b/lib/worker/src/durable.rs index 6cc7d7d..5953952 100644 --- a/lib/worker/src/durable.rs +++ b/lib/worker/src/durable.rs @@ -807,7 +807,7 @@ pub struct SetAlarmOptions { /// } /// } /// ``` -// #[async_trait(?Send)] +#[allow(async_fn_in_trait)] pub trait DurableObject { fn new(state: State, env: Env) -> Self; diff --git a/lib/worker/src/r2/mod.rs b/lib/worker/src/r2/mod.rs index 4d6a590..1e87164 100644 --- a/lib/worker/src/r2/mod.rs +++ b/lib/worker/src/r2/mod.rs @@ -5,11 +5,11 @@ use betterworker_sys::{ FixedLengthStream as EdgeFixedLengthStream, R2Bucket as EdgeR2Bucket, R2MultipartUpload as EdgeR2MultipartUpload, R2Object as EdgeR2Object, R2ObjectBody as EdgeR2ObjectBody, R2Objects as EdgeR2Objects, - R2UploadedPart as EdgeR2UploadedPart, }; pub use builder::*; use js_sys::{JsString, Object, Reflect, Uint8Array}; use send_wrapper::SendWrapper; +use serde::{Deserialize, Serialize}; use wasm_bindgen::{JsCast, JsValue}; use crate::date::Date; @@ -328,17 +328,40 @@ impl<'body> ObjectBody<'body> { /// [UploadedPart] objects are returned from /// [upload_part](MultipartUpload::upload_part) operations and must be passed to /// the [complete](MultipartUpload::complete) operation. +#[derive(Clone, Deserialize, Serialize)] +#[serde(rename_all = "camelCase")] pub struct UploadedPart { - inner: SendWrapper, + part_number: u16, + etag: String, } impl UploadedPart { + pub fn new(part_number: u16, etag: String) -> Self { + Self { part_number, etag } + } + pub fn part_number(&self) -> u16 { - self.inner.part_number() + self.part_number } - pub fn etag(&self) -> String { - self.inner.etag() + pub fn etag(&self) -> &str { + &self.etag + } +} + +impl TryFrom for UploadedPart { + type Error = WorkerError; + + fn try_from(value: JsValue) -> std::result::Result { + serde_wasm_bindgen::from_value(value).map_err(WorkerError::from) + } +} + +impl TryInto for UploadedPart { + type Error = WorkerError; + + fn try_into(self) -> std::result::Result { + serde_wasm_bindgen::to_value(&self).map_err(WorkerError::from) } } @@ -371,11 +394,11 @@ impl MultipartUpload { &self, part_number: u16, value: impl Into, ) -> Result { let fut = future_from_promise(self.inner.upload_part(part_number, value.into().into())); - let uploaded_part = fut.await.map_err(WorkerError::from_promise_err)?; - - Ok(UploadedPart { - inner: SendWrapper::new(uploaded_part.into()), - }) + let uploaded_part = fut + .await + .map_err(WorkerError::from_promise_err)? + .try_into()?; + Ok(uploaded_part) } /// Aborts the multipart upload. @@ -391,14 +414,11 @@ impl MultipartUpload { pub async fn complete( self, uploaded_parts: impl IntoIterator, ) -> Result { - let fut = future_from_promise( - self.inner.complete( - uploaded_parts - .into_iter() - .map(|part| part.inner.take().into()) - .collect(), - ), - ); + let uploaded_parts = uploaded_parts + .into_iter() + .map(TryInto::try_into) + .collect::>>()?; + let fut = future_from_promise(self.inner.complete(uploaded_parts)); let object = fut.await.map_err(WorkerError::from_promise_err)?; Ok(R2Object { inner: ObjectInner::Body(SendWrapper::new(object.into())),