Skip to content

Commit 7ba188c

Browse files
committed
Clarify that id.name is not available within DO
1 parent 4b1f72e commit 7ba188c

File tree

2 files changed

+28
-11
lines changed

2 files changed

+28
-11
lines changed

worker-sandbox/src/durable.rs

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use serde::Serialize;
1+
use serde::{Deserialize, Serialize};
22
use std::convert::TryFrom;
33
use std::{cell::RefCell, collections::HashMap};
44

@@ -11,16 +11,20 @@ use worker::{
1111

1212
#[durable_object]
1313
pub struct MyClass {
14-
name: String,
1514
state: State,
1615
number: RefCell<usize>,
1716
}
1817

18+
#[derive(Deserialize)]
19+
pub struct QueryParams {
20+
name: String,
21+
}
22+
1923
impl DurableObject for MyClass {
2024
fn new(state: State, _env: Env) -> Self {
21-
let name = state.id().name().unwrap_or_else(|| state.id().to_string());
25+
// Unfortunately we can't access the `name` property within the Durable Object (see <https://github.com/cloudflare/workerd/issues/2240>). Instead, we can pass it as a request parameter.
26+
assert!(state.id().name().is_none());
2227
Self {
23-
name,
2428
state,
2529
number: RefCell::new(0),
2630
}
@@ -30,7 +34,10 @@ impl DurableObject for MyClass {
3034
async fn fetch(&self, req: Request) -> Result<Response> {
3135
let handler = async move {
3236
match req.path().as_str() {
33-
"/hello" => Response::ok(format!("Hello from {}!", self.name)),
37+
"/hello" => {
38+
let name = &req.query::<QueryParams>()?.name;
39+
Response::ok(format!("Hello from {name}!"))
40+
}
3441
"/storage" => {
3542
let storage = self.state.storage();
3643
let map = [("one".to_string(), 1), ("two".to_string(), 2)]
@@ -164,8 +171,11 @@ pub async fn handle_hello(
164171
_data: crate::SomeSharedData,
165172
) -> Result<Response> {
166173
let namespace = env.durable_object("MY_CLASS")?;
167-
let stub = namespace.id_from_name("your Durable Object")?.get_stub()?;
168-
stub.fetch_with_str("https://fake-host/hello").await
174+
let name = "my-durable-object";
175+
let id = namespace.id_from_name(name)?;
176+
let stub = id.get_stub()?;
177+
stub.fetch_with_str(&format!("https://fake-host/hello?name={name}"))
178+
.await
169179
}
170180

171181
#[worker::send]
@@ -175,8 +185,11 @@ pub async fn handle_hello_unique(
175185
_data: crate::SomeSharedData,
176186
) -> Result<Response> {
177187
let namespace = env.durable_object("MY_CLASS")?;
178-
let stub = namespace.unique_id()?.get_stub()?;
179-
stub.fetch_with_str("https://fake-host/hello").await
188+
let id = namespace.unique_id()?;
189+
let name = id.to_string();
190+
let stub = id.get_stub()?;
191+
stub.fetch_with_str(&format!("https://fake-host/hello?name={name}"))
192+
.await
180193
}
181194

182195
#[worker::send]
@@ -208,13 +221,16 @@ pub async fn handle_basic_test(
208221

209222
let stub = id.get_stub()?;
210223
let res = stub
211-
.fetch_with_str("https://fake-host/hello")
224+
.fetch_with_str(&format!(
225+
"https://fake-host/hello?name={}",
226+
id.name().unwrap()
227+
))
212228
.await?
213229
.text()
214230
.await?;
215231
let res2 = stub
216232
.fetch_with_request(Request::new_with_init(
217-
"https://fake-host/hello",
233+
&format!("https://fake-host/hello?name={}", id.name().unwrap()),
218234
RequestInit::new()
219235
.with_body(Some("lol".into()))
220236
.with_method(Method::Post),

worker/src/durable.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ impl ObjectId<'_> {
179179

180180
/// The name that was used to create the `ObjectId` via [`id_from_name`](https://developers.cloudflare.com/durable-objects/api/namespace/#idfromname).
181181
/// `None` is returned if the `ObjectId` was constructed using [`unique_id`](https://developers.cloudflare.com/durable-objects/api/namespace/#newuniqueid).
182+
/// `None` is also returned within the Durable Object constructor, as the `name` property is not accessible there (see <https://github.com/cloudflare/workerd/issues/2240>).
182183
pub fn name(&self) -> Option<String> {
183184
self.inner.name()
184185
}

0 commit comments

Comments
 (0)