From 76c2976f051de21ef9bb4631c602d59ba7e7c20b Mon Sep 17 00:00:00 2001 From: Noah Kennedy Date: Tue, 6 Aug 2024 13:30:01 -0500 Subject: [PATCH] durable: add location hint support --- .../src/types/durable_object/namespace.rs | 7 +++++++ worker/src/durable.rs | 18 ++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/worker-sys/src/types/durable_object/namespace.rs b/worker-sys/src/types/durable_object/namespace.rs index e7ba7600..22ba167c 100644 --- a/worker-sys/src/types/durable_object/namespace.rs +++ b/worker-sys/src/types/durable_object/namespace.rs @@ -34,4 +34,11 @@ extern "C" { this: &DurableObjectNamespace, id: &DurableObjectId, ) -> Result; + + #[wasm_bindgen(method, catch, js_name=get)] + pub fn get_with_options( + this: &DurableObjectNamespace, + id: &DurableObjectId, + options: &JsValue, + ) -> Result; } diff --git a/worker/src/durable.rs b/worker/src/durable.rs index a192fd94..1b2188e4 100644 --- a/worker/src/durable.rs +++ b/worker/src/durable.rs @@ -155,6 +155,24 @@ impl ObjectId<'_> { }) .map_err(Error::from) } + + pub fn get_stub_with_location_hint(&self, location_hint: &str) -> Result { + let options = Object::new(); + js_sys::Reflect::set( + &options, + &JsValue::from("locationHint"), + &location_hint.into(), + )?; + + self.namespace + .ok_or_else(|| JsValue::from("Cannot get stub from within a Durable Object")) + .and_then(|n| { + Ok(Stub { + inner: n.inner.get_with_options(&self.inner, &options)?, + }) + }) + .map_err(Error::from) + } } impl Display for ObjectId<'_> {