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

Commit e972ad5

Browse files
committed
fix(core): fix websocket protocol for rivetkit
1 parent ab42017 commit e972ad5

File tree

5 files changed

+75
-72
lines changed

5 files changed

+75
-72
lines changed

packages/cloudflare-workers/src/websocket.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,10 @@ export const upgradeWebSocket: UpgradeWebSocket<
6565
const protocols = c.req.header("Sec-WebSocket-Protocol");
6666
if (
6767
typeof protocols === "string" &&
68-
protocols.includes(WS_PROTOCOL_STANDARD)
68+
protocols
69+
.split(",")
70+
.map((x) => x.trim())
71+
.includes(WS_PROTOCOL_STANDARD)
6972
) {
7073
headers["Sec-WebSocket-Protocol"] = WS_PROTOCOL_STANDARD;
7174
}

packages/rivetkit/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,10 +163,10 @@
163163
"@bare-ts/lib": "~0.3.0",
164164
"@hono/standard-validator": "^0.1.3",
165165
"@hono/zod-openapi": "^0.19.10",
166-
"@rivetkit/engine-runner": "https://pkg.pr.new/rivet-dev/engine/@rivetkit/engine-runner@9676fe4",
166+
"@rivetkit/engine-runner": "https://pkg.pr.new/rivet-dev/engine/@rivetkit/engine-runner@a0639d0",
167167
"@rivetkit/fast-json-patch": "^3.1.2",
168168
"cbor-x": "^1.6.0",
169-
"hono": "^4.9.8",
169+
"hono": "^4.7.0",
170170
"invariant": "^2.2.4",
171171
"nanoevents": "^9.1.0",
172172
"on-change": "^5.0.1",

packages/rivetkit/src/actor/router.ts

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ import {
2323
HEADER_ENCODING,
2424
PATH_CONNECT_WEBSOCKET,
2525
PATH_RAW_WEBSOCKET_PREFIX,
26+
WS_PROTOCOL_CONN_PARAMS,
27+
WS_PROTOCOL_ENCODING,
28+
WS_PROTOCOL_TOKEN,
2629
} from "@/common/actor-router-consts";
2730
import {
2831
handleRouteError,
@@ -81,8 +84,23 @@ export function createActorRouter(
8184
const upgradeWebSocket = runConfig.getUpgradeWebSocket?.();
8285
if (upgradeWebSocket) {
8386
return upgradeWebSocket(async (c) => {
84-
const encodingRaw = c.req.header(HEADER_ENCODING);
85-
const connParamsRaw = c.req.header(HEADER_CONN_PARAMS);
87+
// Parse configuration from Sec-WebSocket-Protocol header
88+
const protocols = c.req.header("sec-websocket-protocol");
89+
let encodingRaw: string | undefined;
90+
let connParamsRaw: string | undefined;
91+
92+
if (protocols) {
93+
const protocolList = protocols.split(",").map((p) => p.trim());
94+
for (const protocol of protocolList) {
95+
if (protocol.startsWith(WS_PROTOCOL_ENCODING)) {
96+
encodingRaw = protocol.substring(WS_PROTOCOL_ENCODING.length);
97+
} else if (protocol.startsWith(WS_PROTOCOL_CONN_PARAMS)) {
98+
connParamsRaw = decodeURIComponent(
99+
protocol.substring(WS_PROTOCOL_CONN_PARAMS.length),
100+
);
101+
}
102+
}
103+
}
86104

87105
const encoding = EncodingSchema.parse(encodingRaw);
88106
const connParams = connParamsRaw
@@ -172,8 +190,26 @@ export function createActorRouter(
172190
const upgradeWebSocket = runConfig.getUpgradeWebSocket?.();
173191
if (upgradeWebSocket) {
174192
return upgradeWebSocket(async (c) => {
175-
const encodingRaw = c.req.header(HEADER_ENCODING);
176-
const connParamsRaw = c.req.header(HEADER_CONN_PARAMS);
193+
// Parse configuration from Sec-WebSocket-Protocol header
194+
const protocols = c.req.header("sec-websocket-protocol");
195+
let encodingRaw: string | undefined;
196+
let connParamsRaw: string | undefined;
197+
let token: string | undefined;
198+
199+
if (protocols) {
200+
const protocolList = protocols.split(",").map((p) => p.trim());
201+
for (const protocol of protocolList) {
202+
if (protocol.startsWith(WS_PROTOCOL_ENCODING)) {
203+
encodingRaw = protocol.substring(WS_PROTOCOL_ENCODING.length);
204+
} else if (protocol.startsWith(WS_PROTOCOL_CONN_PARAMS)) {
205+
connParamsRaw = decodeURIComponent(
206+
protocol.substring(WS_PROTOCOL_CONN_PARAMS.length),
207+
);
208+
} else if (protocol.startsWith(WS_PROTOCOL_TOKEN)) {
209+
token = protocol.substring(WS_PROTOCOL_TOKEN.length);
210+
}
211+
}
212+
}
177213

178214
const encoding = EncodingSchema.parse(encodingRaw);
179215
const connParams = connParamsRaw

packages/rivetkit/src/drivers/engine/actor-driver.ts

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,14 @@ import {
2424
PATH_RAW_WEBSOCKET_PREFIX,
2525
WS_PROTOCOL_CONN_PARAMS,
2626
WS_PROTOCOL_ENCODING,
27+
WS_PROTOCOL_TOKEN,
2728
} from "@/common/actor-router-consts";
2829
import type { UpgradeWebSocketArgs } from "@/common/inline-websocket-adapter2";
2930
import { getLogger } from "@/common/log";
3031
import type { UniversalWebSocket } from "@/common/websocket-interface";
3132
import {
3233
type ActorDriver,
3334
type AnyActorInstance,
34-
HEADER_CONN_PARAMS,
35-
HEADER_ENCODING,
3635
type ManagerDriver,
3736
serializeEmptyPersistData,
3837
} from "@/driver-helpers/mod";
@@ -300,18 +299,24 @@ export class EngineActorDriver implements ActorDriver {
300299
if (protocols === null)
301300
throw new Error(`Missing sec-websocket-protocol header`);
302301

302+
// Parse configuration from Sec-WebSocket-Protocol header
303+
const protocols = request.headers.get("sec-websocket-protocol");
303304
let encodingRaw: string | undefined;
304305
let connParamsRaw: string | undefined;
305-
306-
// Parse protocols
307-
const protocolList = protocols.split(",").map((p) => p.trim());
308-
for (const protocol of protocolList) {
309-
if (protocol.startsWith(WS_PROTOCOL_ENCODING)) {
310-
encodingRaw = protocol.substring(WS_PROTOCOL_ENCODING.length);
311-
} else if (protocol.startsWith(WS_PROTOCOL_CONN_PARAMS)) {
312-
connParamsRaw = decodeURIComponent(
313-
protocol.substring(WS_PROTOCOL_CONN_PARAMS.length),
314-
);
306+
let token: string | undefined;
307+
308+
if (protocols) {
309+
const protocolList = protocols.split(",").map((p) => p.trim());
310+
for (const protocol of protocolList) {
311+
if (protocol.startsWith(WS_PROTOCOL_ENCODING)) {
312+
encodingRaw = protocol.substring(WS_PROTOCOL_ENCODING.length);
313+
} else if (protocol.startsWith(WS_PROTOCOL_CONN_PARAMS)) {
314+
connParamsRaw = decodeURIComponent(
315+
protocol.substring(WS_PROTOCOL_CONN_PARAMS.length),
316+
);
317+
} else if (protocol.startsWith(WS_PROTOCOL_TOKEN)) {
318+
token = protocol.substring(WS_PROTOCOL_TOKEN.length);
319+
}
315320
}
316321
}
317322

pnpm-lock.yaml

Lines changed: 12 additions & 53 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)