Skip to content

Commit eec6607

Browse files
committed
Use async storage.get/put for server name
Replace synchronous ctx.storage.kv usage with the async ctx.storage.get/put for persisting Server.name. Add a #hydrateNameFromStorage() helper and call it from entry points (#ensureInitialized and fetch path) so the name is loaded on cold starts; await storage.put when setting the name. Update tests to await ctx.storage.put. Add a changeset documenting the migration and remove the previous changeset that required SQLite-backed Durable Objects.
1 parent b82fd4d commit eec6607

4 files changed

Lines changed: 25 additions & 34 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"partyserver": patch
3+
---
4+
5+
Switch name persistence from sync `ctx.storage.kv` to async `ctx.storage.get/put`, removing the requirement for SQLite-backed Durable Objects.

.changeset/require-sqlite-classes.md

Lines changed: 0 additions & 11 deletions
This file was deleted.

packages/partyserver/src/index.ts

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -392,21 +392,15 @@ export class Server<
392392
this.#_props = JSON.parse(props);
393393
}
394394
if (!this.#_name) {
395-
// Try hydrating from storage first (covers cold starts after
396-
// a previous request already persisted the name).
397-
const stored = this.ctx.storage.kv.get<string>(NAME_STORAGE_KEY);
398-
if (stored) {
399-
this.#_name = stored;
400-
} else {
401-
// First-time contact: name must come from the request header
402-
// (set by routePartykitRequest or getServerByName).
403-
const room = request.headers.get("x-partykit-room");
404-
if (!room) {
405-
throw new Error(`Missing namespace or room headers when connecting to ${this.#ParentClass.name}.
395+
await this.#hydrateNameFromStorage();
396+
}
397+
if (!this.#_name) {
398+
const room = request.headers.get("x-partykit-room");
399+
if (!room) {
400+
throw new Error(`Missing namespace or room headers when connecting to ${this.#ParentClass.name}.
406401
Did you try connecting directly to this Durable Object? Try using getServerByName(namespace, id) instead.`);
407-
}
408-
await this.setName(room);
409402
}
403+
await this.setName(room);
410404
}
411405

412406
await this.#ensureInitialized();
@@ -549,8 +543,17 @@ Did you try connecting directly to this Durable Object? Try using getServerByNam
549543
}
550544
}
551545

546+
async #hydrateNameFromStorage(): Promise<void> {
547+
if (this.#_name) return;
548+
const stored = await this.ctx.storage.get<string>(NAME_STORAGE_KEY);
549+
if (stored) {
550+
this.#_name = stored;
551+
}
552+
}
553+
552554
async #ensureInitialized(): Promise<void> {
553555
if (this.#status === "started") return;
556+
await this.#hydrateNameFromStorage();
554557
let error: unknown;
555558
await this.ctx.blockConcurrencyWhile(async () => {
556559
this.#status = "starting";
@@ -603,16 +606,10 @@ Did you try connecting directly to this Durable Object? Try using getServerByNam
603606

604607
/**
605608
* The name for this server. Write-once-only.
606-
* Hydrates from durable storage on first access if the name was
607-
* previously persisted (e.g. during an alarm wake-up with no HTTP request).
609+
* Hydrated from durable storage by #ensureInitialized() on every
610+
* entry point (fetch, alarm, webSocketMessage/Close/Error).
608611
*/
609612
get name(): string {
610-
if (!this.#_name) {
611-
const stored = this.ctx.storage.kv.get<string>(NAME_STORAGE_KEY);
612-
if (stored) {
613-
this.#_name = stored;
614-
}
615-
}
616613
if (!this.#_name) {
617614
throw new Error(
618615
`Attempting to read .name on ${this.#ParentClass.name} before it was set. The name can be set by explicitly calling .setName(name) on the stub, or by using routePartyKitRequest(). This is a known issue and will be fixed soon. Follow https://github.com/cloudflare/workerd/issues/2240 for more updates.`
@@ -631,7 +628,7 @@ Did you try connecting directly to this Durable Object? Try using getServerByNam
631628
);
632629
}
633630
this.#_name = name;
634-
this.ctx.storage.kv.put(NAME_STORAGE_KEY, name);
631+
await this.ctx.storage.put(NAME_STORAGE_KEY, name);
635632

636633
await this.#ensureInitialized();
637634
}

packages/partyserver/src/tests/worker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ export class AlarmNameServer extends Server {
167167
// wakes cold — #_name is unset, only storage has the name.
168168
if (url.searchParams.get("seed")) {
169169
const name = url.searchParams.get("name")!;
170-
this.ctx.storage.kv.put("__ps_name", name);
170+
await this.ctx.storage.put("__ps_name", name);
171171
await this.ctx.storage.setAlarm(Date.now() + 60_000);
172172
return new Response("seeded");
173173
}

0 commit comments

Comments
 (0)