Skip to content
This repository was archived by the owner on Mar 13, 2025. It is now read-only.

Commit a598cd0

Browse files
committed
Remove name from IDs in DurableObjectStates, closes #219
1 parent dab03dc commit a598cd0

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

packages/durable-objects/src/plugin.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,10 @@ export class DurableObjectsPlugin
163163
await storage.storage(key, this.#persist),
164164
this.#alarmStore.buildBridge(key)
165165
);
166-
const state = new DurableObjectState(id, objectStorage);
166+
// `name` should not be passed to the constructed `state`:
167+
// https://github.com/cloudflare/miniflare/issues/219
168+
const unnamedId = new DurableObjectId(objectName, id.toString());
169+
const state = new DurableObjectState(unnamedId, objectStorage);
167170

168171
// Create and store new instance if none found
169172
const constructor = this.#constructors.get(objectName);

packages/durable-objects/test/plugin.spec.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
DurableObject,
66
DurableObjectError,
77
DurableObjectNamespace,
8+
DurableObjectState,
89
DurableObjectsPlugin,
910
} from "@miniflare/durable-objects";
1011
import {
@@ -226,6 +227,30 @@ test("DurableObjectsPlugin: setup: includes namespaces for all objects", async (
226227
t.is(await res1.text(), "object1");
227228
t.is(await res2.text(), "object2");
228229
});
230+
test("DurableObjectsPlugin: setup: name removed from id passed to object constructors", async (t) => {
231+
// https://github.com/cloudflare/miniflare/issues/219
232+
class TestObject implements DurableObject {
233+
constructor(readonly state: DurableObjectState) {}
234+
fetch = () => new Response(String(this.state.id.name));
235+
}
236+
237+
const factory = new MemoryStorageFactory();
238+
const plugin = new DurableObjectsPlugin(ctx, {
239+
durableObjects: { TEST_OBJECT: "TestObject" },
240+
});
241+
242+
const result = await plugin.setup(factory);
243+
plugin.beforeReload();
244+
plugin.reload({}, { TestObject }, new Map());
245+
246+
const ns: DurableObjectNamespace = result.bindings?.TEST_OBJECT;
247+
const id = ns.idFromName("name");
248+
t.is(id.name, "name");
249+
const stub = ns.get(id);
250+
t.is(stub.id.name, "name");
251+
const res = await stub.fetch("/");
252+
t.is(await res.text(), "undefined");
253+
});
229254

230255
test("DurableObjectsPlugin: beforeReload: deletes all instances", async (t) => {
231256
const factory = new MemoryStorageFactory();

0 commit comments

Comments
 (0)