Skip to content

Commit ea8ef90

Browse files
committed
feat(rivetkit): support routing via query params
1 parent 4b9f24b commit ea8ef90

File tree

1 file changed

+43
-3
lines changed
  • rivetkit-typescript/packages/rivetkit/src/manager

1 file changed

+43
-3
lines changed

rivetkit-typescript/packages/rivetkit/src/manager/gateway.ts

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ import { logger } from "./log";
2424
*
2525
* Routes requests based on the Upgrade header:
2626
* - WebSocket requests: Uses sec-websocket-protocol for routing (target.actor, actor.{id})
27+
* with fallback to query parameters (x_rivet_target, x_rivet_actor, etc.)
2728
* - HTTP requests: Uses x-rivet-target and x-rivet-actor headers for routing
29+
* with fallback to query parameters (x_rivet_target, x_rivet_actor)
2830
*/
2931
export async function actorGateway(
3032
runConfig: RunnerConfig,
@@ -63,6 +65,7 @@ export async function actorGateway(
6365

6466
/**
6567
* Handle WebSocket requests using sec-websocket-protocol for routing
68+
* with fallback to query parameters
6669
*/
6770
async function handleWebSocketGateway(
6871
runConfig: RunnerConfig,
@@ -75,6 +78,9 @@ async function handleWebSocketGateway(
7578
throw new WebSocketsNotEnabled();
7679
}
7780

81+
// Parse query parameters for fallback
82+
const queryParams = parseQueryParams(c.req.url);
83+
7884
// Parse configuration from Sec-WebSocket-Protocol header
7985
const protocols = c.req.header("sec-websocket-protocol");
8086
let target: string | undefined;
@@ -107,8 +113,19 @@ async function handleWebSocketGateway(
107113
}
108114
}
109115

116+
// Fallback to query parameters if not provided via protocols
117+
target = target || queryParams.get("x_rivet_target");
118+
actorId = actorId || queryParams.get("x_rivet_actor");
119+
encodingRaw = encodingRaw || queryParams.get("x_rivet_encoding");
120+
connParamsRaw = connParamsRaw || queryParams.get("x_rivet_conn_params");
121+
connIdRaw = connIdRaw || queryParams.get("x_rivet_conn_id");
122+
connTokenRaw = connTokenRaw || queryParams.get("x_rivet_conn_token");
123+
110124
if (target !== "actor") {
111-
return c.text("WebSocket upgrade requires target.actor protocol", 400);
125+
return c.text(
126+
"WebSocket upgrade requires target.actor protocol or x_rivet_target=actor query parameter",
127+
400,
128+
);
112129
}
113130

114131
if (!actorId) {
@@ -143,15 +160,22 @@ async function handleWebSocketGateway(
143160

144161
/**
145162
* Handle HTTP requests using x-rivet headers for routing
163+
* with fallback to query parameters
146164
*/
147165
async function handleHttpGateway(
148166
managerDriver: ManagerDriver,
149167
c: HonoContext,
150168
next: Next,
151169
strippedPath: string,
152170
) {
153-
const target = c.req.header(HEADER_RIVET_TARGET);
154-
const actorId = c.req.header(HEADER_RIVET_ACTOR);
171+
// Parse query parameters for fallback
172+
const queryParams = parseQueryParams(c.req.url);
173+
174+
// Try headers first, then fallback to query parameters
175+
const target =
176+
c.req.header(HEADER_RIVET_TARGET) || queryParams.get("x_rivet_target");
177+
const actorId =
178+
c.req.header(HEADER_RIVET_ACTOR) || queryParams.get("x_rivet_actor");
155179

156180
if (target !== "actor") {
157181
return next();
@@ -188,6 +212,22 @@ async function handleHttpGateway(
188212
return await managerDriver.proxyRequest(c, proxyRequest, actorId);
189213
}
190214

215+
/**
216+
* Parse query parameters from URL
217+
*/
218+
function parseQueryParams(url: string): Map<string, string> {
219+
const params = new Map<string, string>();
220+
try {
221+
const urlObj = new URL(url, "http://dummy"); // Use dummy base for relative URLs
222+
urlObj.searchParams.forEach((value, key) => {
223+
params.set(key, value);
224+
});
225+
} catch {
226+
// If URL parsing fails, return empty params
227+
}
228+
return params;
229+
}
230+
191231
/**
192232
* Creates a WebSocket proxy for test endpoints that forwards messages between server and client WebSockets
193233
*/

0 commit comments

Comments
 (0)