Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions .changeset/fix-basepath-reconnect.md

This file was deleted.

4 changes: 2 additions & 2 deletions fixtures/chat/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
},
"dependencies": {
"nanoid": "^5.1.6",
"partyserver": "^0.1.2",
"partysocket": "^1.1.11",
"partyserver": "^0.1.3",
"partysocket": "^1.1.12",
"react": "^19.2.3",
"react-dom": "^19.2.3"
},
Expand Down
4 changes: 2 additions & 2 deletions fixtures/globe/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
},
"dependencies": {
"cobe": "^0.6.5",
"partyserver": "^0.1.2",
"partysocket": "^1.1.11",
"partyserver": "^0.1.3",
"partysocket": "^1.1.12",
"react": "^19.2.3",
"react-dom": "^19.2.3"
},
Expand Down
4 changes: 2 additions & 2 deletions fixtures/hono/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
"dependencies": {
"hono": "^4.11.1",
"hono-party": "^1.0.0",
"partyserver": "^0.1.2",
"partysocket": "^1.1.11",
"partyserver": "^0.1.3",
"partysocket": "^1.1.12",
"react": "^19.2.3",
"react-dom": "^19.2.3"
},
Expand Down
2 changes: 1 addition & 1 deletion fixtures/monaco-yjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
},
"dependencies": {
"monaco-editor": "^0.55.1",
"partysocket": "^1.1.11",
"partysocket": "^1.1.12",
"y-monaco": "^0.1.6"
}
}
2 changes: 1 addition & 1 deletion fixtures/node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
"types": "wrangler types env.d.ts --include-runtime false"
},
"dependencies": {
"partysocket": "^1.1.11"
"partysocket": "^1.1.12"
}
}
2 changes: 1 addition & 1 deletion fixtures/pubsub/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@
},
"dependencies": {
"nanoid": "^5.1.6",
"partysocket": "^1.1.11"
"partysocket": "^1.1.12"
}
}
2 changes: 1 addition & 1 deletion fixtures/tiptap-yjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"@tiptap/extension-collaboration": "^3.14.0",
"@tiptap/react": "^3.14.0",
"@tiptap/starter-kit": "^3.14.0",
"partyserver": "^0.1.2",
"partyserver": "^0.1.3",
"react": "^19.2.3",
"react-dom": "^19.2.3",
"tailwindcss": "^4.1.18",
Expand Down
4 changes: 2 additions & 2 deletions fixtures/tldraw/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
"start": "vite dev"
},
"dependencies": {
"partyserver": "^0.1.2",
"partysocket": "^1.1.11",
"partyserver": "^0.1.3",
"partysocket": "^1.1.12",
"react": "^19.2.3",
"react-dom": "^19.2.3",
"tldraw": "^4.2.1"
Expand Down
4 changes: 2 additions & 2 deletions fixtures/todo-sync/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
},
"dependencies": {
"nanoid": "^5.1.6",
"partyserver": "^0.1.2",
"partysocket": "^1.1.11",
"partyserver": "^0.1.3",
"partysocket": "^1.1.12",
"react": "^19.2.3",
"react-dom": "^19.2.3",
"valibot": "^1.2.0"
Expand Down
32 changes: 16 additions & 16 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions packages/partyserver/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# partyflare

## 0.1.3

### Patch Changes

- [#319](https://github.com/cloudflare/partykit/pull/319) [`15a4157`](https://github.com/cloudflare/partykit/commit/15a41572a778526b496de94d5ef0909226c56e72) Thanks [@threepointone](https://github.com/threepointone)! - Add `configurable: true` to the `state`, `setState`, `serializeAttachment`, and `deserializeAttachment` property descriptors on connection objects. This allows downstream consumers (like the Cloudflare Agents SDK) to redefine these properties with `Object.defineProperty` for namespacing or wrapping internal state storage. Default behavior is unchanged.

## 0.1.2

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/partyserver/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "partyserver",
"version": "0.1.2",
"version": "0.1.3",
"repository": {
"type": "git",
"url": "git://github.com/cloudflare/partykit.git"
Expand Down
4 changes: 4 additions & 0 deletions packages/partyserver/src/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,11 +144,13 @@ export const createLazyConnection = (
}
},
state: {
configurable: true,
get() {
return ws.deserializeAttachment() as ConnectionState<unknown>;
}
},
setState: {
configurable: true,
value: function setState<T>(setState: T | ConnectionSetStateFn<T>) {
let state: T;
if (setState instanceof Function) {
Expand All @@ -163,13 +165,15 @@ export const createLazyConnection = (
},

deserializeAttachment: {
configurable: true,
value: function deserializeAttachment<T = unknown>() {
const attachment = attachments.get(ws);
return (attachment.__user ?? null) as T;
}
},

serializeAttachment: {
configurable: true,
value: function serializeAttachment<T = unknown>(attachment: T) {
const setting = {
...attachments.get(ws),
Expand Down
104 changes: 104 additions & 0 deletions packages/partyserver/src/tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,110 @@ describe("Server", () => {
return promise;
});

it("allows state and setState to be redefined on a connection", async () => {
const ctx = createExecutionContext();
const request = new Request(
"http://example.com/parties/configurable-state/room1",
{
headers: {
Upgrade: "websocket"
}
}
);
const response = await worker.fetch(request, env, ctx);
const ws = response.webSocket!;

const { promise, resolve, reject } = Promise.withResolvers<void>();
ws.accept();
ws.addEventListener("message", (message) => {
try {
// The server redefines state/setState and uses them to send back
// { answer: 42 }, proving that Object.defineProperty worked.
expect(JSON.parse(message.data as string)).toEqual({ answer: 42 });
resolve();
} catch (e) {
reject(e);
} finally {
ws.close();
}
});

return promise;
});

it("allows state and setState to be redefined on a non-hibernating connection", async () => {
const ctx = createExecutionContext();
const request = new Request(
"http://example.com/parties/configurable-state-in-memory/room1",
{
headers: {
Upgrade: "websocket"
}
}
);
const response = await worker.fetch(request, env, ctx);
const ws = response.webSocket!;

const { promise, resolve, reject } = Promise.withResolvers<void>();
ws.accept();
ws.addEventListener("message", (message) => {
try {
// The non-hibernating server redefines state/setState and sends back
// { answer: 99 }, proving Object.defineProperty works on this path too.
expect(JSON.parse(message.data as string)).toEqual({ answer: 99 });
resolve();
} catch (e) {
reject(e);
} finally {
ws.close();
}
});

return promise;
});

it("persists state through setState and reads it back via state getter", async () => {
const ctx = createExecutionContext();
const request = new Request(
"http://example.com/parties/state-round-trip/room1",
{
headers: { Upgrade: "websocket" }
}
);
const response = await worker.fetch(request, env, ctx);
const ws = response.webSocket!;
ws.accept();

// Collect all messages to verify the full round-trip
const messages: unknown[] = [];
const { promise, resolve, reject } = Promise.withResolvers<void>();

ws.addEventListener("message", (event) => {
try {
messages.push(JSON.parse(event.data as string));

if (messages.length === 1) {
// First response: "get" should return the initial state set in onConnect
expect(messages[0]).toEqual({ count: 1 });
// Now ask the server to increment using the updater function form
ws.send("increment");
} else if (messages.length === 2) {
// Second response: state should reflect the increment
expect(messages[1]).toEqual({ count: 2 });
resolve();
}
} catch (e) {
reject(e);
}
});

// Ask the server to read back the state that was set in onConnect
ws.send("get");

await promise;
ws.close();
});

// it("can be connected with a query parameter");
// it("can be connected with a header");

Expand Down
Loading
Loading