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: 5 additions & 0 deletions .changeset/fix-basepath-reconnect.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"partysocket": patch
---

Fix `PartySocket.reconnect()` crashing when using `basePath` without `room`. The reconnect guard now accepts either `room` or `basePath` as sufficient context to construct a connection URL.
10 changes: 5 additions & 5 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion packages/hono-party/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,6 @@
"devDependencies": {
"@cloudflare/workers-types": "^4.20251218.0",
"hono": "^4.11.1",
"partyserver": "^0.1.1"
"partyserver": "^0.1.2"
}
}
17 changes: 13 additions & 4 deletions packages/partysocket/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ export default class PartySocket extends ReconnectingWebSocket {
room?: string;
host!: string;
path!: string;
basePath?: string;

constructor(readonly partySocketOptions: PartySocketOptions) {
const wsOptions = getWSOptions(partySocketOptions);
Expand Down Expand Up @@ -169,7 +170,8 @@ export default class PartySocket extends ReconnectingWebSocket {
...partySocketOptions,
host: partySocketOptions.host ?? this.host,
room: partySocketOptions.room ?? this.room,
path: partySocketOptions.path ?? this.path
path: partySocketOptions.path ?? this.path,
basePath: partySocketOptions.basePath ?? this.basePath
});

this._url = wsOptions.urlProvider;
Expand All @@ -180,23 +182,29 @@ export default class PartySocket extends ReconnectingWebSocket {
}

private setWSProperties(wsOptions: ReturnType<typeof getWSOptions>) {
const { _pk, _pkurl, name, room, host, path } = wsOptions;
const { _pk, _pkurl, name, room, host, path, basePath } = wsOptions;

this._pk = _pk;
this._pkurl = _pkurl;
this.name = name;
this.room = room;
this.host = host;
this.path = path;
this.basePath = basePath;
}

public reconnect(
code?: number | undefined,
reason?: string | undefined
): void {
if (!this.room || !this.host) {
if (!this.host) {
throw new Error(
"The room and host must be set before connecting, use `updateProperties` method to set them or pass them to the constructor."
"The host must be set before connecting, use `updateProperties` method to set it or pass it to the constructor."
);
}
if (!this.room && !this.basePath) {
throw new Error(
"The room (or basePath) must be set before connecting, use `updateProperties` method to set it or pass it to the constructor."
);
}
super.reconnect(code, reason);
Expand Down Expand Up @@ -256,6 +264,7 @@ function getWSOptions(partySocketOptions: PartySocketOptions) {
room: party.room,
host: party.host,
path: party.path,
basePath: partySocketOptions.basePath,
protocols: protocols,
socketOptions: socketOptions,
urlProvider: party.urlProvider
Expand Down
16 changes: 14 additions & 2 deletions packages/partysocket/src/tests/error-handling.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ describe.skip("Error Handling - PartySocket Validation", () => {

expect(() => {
ps.reconnect();
}).toThrow("The room and host must be set");
}).toThrow("The host must be set");
});

test("throws when reconnecting without room", () => {
Expand All @@ -140,7 +140,19 @@ describe.skip("Error Handling - PartySocket Validation", () => {

expect(() => {
ps.reconnect();
}).toThrow("The room and host must be set");
}).toThrow("The room (or basePath) must be set");
});

test("does not throw when reconnecting with basePath and no room", () => {
const ps = new PartySocket({
host: "example.com",
basePath: "custom/path",
startClosed: true
});

expect(() => {
ps.reconnect();
}).not.toThrow();
});

test("handles missing WebSocket constructor gracefully", async () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/partysocket/src/tests/partysocket-url.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ describe.skip("PartySocket.updateProperties", () => {
ps.updateProperties({ host: "", room: "" });
expect(() => {
ps.reconnect();
}).toThrow("The room and host must be set");
}).toThrow("The host must be set");
});
});

Expand Down
2 changes: 1 addition & 1 deletion packages/partysub/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
},
"devDependencies": {
"@cloudflare/workers-types": "^4.20251218.0",
"partyserver": "^0.1.1",
"partyserver": "^0.1.2",
"partysocket": "^1.1.11"
}
}
2 changes: 1 addition & 1 deletion packages/partysync/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,6 @@
},
"devDependencies": {
"@cloudflare/workers-types": "^4.20251218.0",
"partyserver": "^0.1.1"
"partyserver": "^0.1.2"
}
}
2 changes: 1 addition & 1 deletion packages/partywhen/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@
"description": "A library for scheduling and running tasks in Cloudflare Workers",
"dependencies": {
"cron-parser": "^5.4.0",
"partyserver": "^0.1.1"
"partyserver": "^0.1.2"
}
}
2 changes: 1 addition & 1 deletion packages/y-partyserver/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
"devDependencies": {
"@cloudflare/workers-types": "^4.20251218.0",
"@types/lodash.debounce": "^4.0.9",
"partyserver": "^0.1.1",
"partyserver": "^0.1.2",
"ws": "^8.18.3",
"yjs": "^13.6.28"
}
Expand Down
Loading