Skip to content

Commit ddf6de8

Browse files
committed
chore(rivetkit): add conn type
1 parent 30eb6e5 commit ddf6de8

File tree

11 files changed

+56
-39
lines changed

11 files changed

+56
-39
lines changed

rivetkit-typescript/packages/rivetkit/src/actor/conn/driver.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,21 @@ export enum DriverReadyState {
1212
}
1313

1414
export interface ConnDriver {
15+
/** The type of driver. Used for debug purposes only. */
16+
type: string;
17+
18+
/**
19+
* Unique request ID provided by the underlying provider. If none is
20+
* available for this conn driver, a random UUID is generated.
21+
**/
1522
requestId: string;
23+
24+
/** ArrayBuffer version of requestId if relevant. */
1625
requestIdBuf: ArrayBuffer | undefined;
26+
27+
/**
28+
* If the connection can be hibernated. If true, this will allow the actor to go to sleep while the connection is still active.
29+
**/
1730
hibernatable: boolean;
1831

1932
sendMessage?(

rivetkit-typescript/packages/rivetkit/src/actor/conn/drivers/http.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ export type ConnHttpState = Record<never, never>;
44

55
export function createHttpSocket(): ConnDriver {
66
return {
7+
type: "http",
78
requestId: crypto.randomUUID(),
89
requestIdBuf: undefined,
910
hibernatable: false,

rivetkit-typescript/packages/rivetkit/src/actor/conn/drivers/raw-http.ts renamed to rivetkit-typescript/packages/rivetkit/src/actor/conn/drivers/raw-request.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ import { DriverReadyState } from "../driver";
88
* Unlike the standard HTTP driver, this provides connection lifecycle management
99
* for tracking the HTTP request through the actor's onRequest handler.
1010
*/
11-
export function createRawHttpSocket(): ConnDriver {
11+
export function createRawRequestSocket(): ConnDriver {
1212
return {
13+
type: "raw-request",
1314
requestId: crypto.randomUUID(),
1415
requestIdBuf: undefined,
1516
hibernatable: false,

rivetkit-typescript/packages/rivetkit/src/actor/conn/drivers/raw-websocket.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ export function createRawWebSocketSocket(
1919
closePromise: Promise<void>,
2020
): ConnDriver {
2121
return {
22+
type: "raw-websocket",
2223
requestId,
2324
requestIdBuf,
2425
hibernatable,

rivetkit-typescript/packages/rivetkit/src/actor/conn/drivers/websocket.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export function createWebSocketSocket(
1616
closePromise: Promise<void>,
1717
): ConnDriver {
1818
return {
19+
type: "websocket",
1920
requestId,
2021
requestIdBuf,
2122
hibernatable,

rivetkit-typescript/packages/rivetkit/src/actor/instance/mod.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import type { ActorConfig, InitContext } from "../config";
1515
import type { ConnDriver } from "../conn/driver";
1616
import { createHttpSocket } from "../conn/drivers/http";
1717
import {
18+
CONN_DRIVER_SYMBOL,
1819
CONN_PERSIST_SYMBOL,
1920
CONN_SEND_MESSAGE_SYMBOL,
2021
CONN_STATE_ENABLED_SYMBOL,
@@ -138,6 +139,7 @@ export class ActorInstance<S, CP, CS, V, I, DB extends AnyDatabaseProvider> {
138139
return Array.from(
139140
this.#connectionManager.connections.entries(),
140141
).map(([id, conn]) => ({
142+
type: conn[CONN_DRIVER_SYMBOL]?.type,
141143
id,
142144
params: conn.params as any,
143145
state: conn[CONN_STATE_ENABLED_SYMBOL]

rivetkit-typescript/packages/rivetkit/src/actor/mod.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,6 @@ export {
9090
createActorRouter,
9191
} from "./router";
9292
export {
93-
handleRawWebSocketHandler,
93+
handleRawWebSocket as handleRawWebSocketHandler,
9494
handleWebSocketConnect,
9595
} from "./router-endpoints";

rivetkit-typescript/packages/rivetkit/src/actor/router-endpoints.ts

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ import {
3737
promiseWithResolvers,
3838
} from "@/utils";
3939
import { createHttpSocket } from "./conn/drivers/http";
40-
import { createRawHttpSocket } from "./conn/drivers/raw-http";
40+
import { createRawRequestSocket } from "./conn/drivers/raw-request";
4141
import { createRawWebSocketSocket } from "./conn/drivers/raw-websocket";
4242
import { createWebSocketSocket } from "./conn/drivers/websocket";
4343
import type { ActorDriver } from "./driver";
@@ -377,7 +377,31 @@ export async function handleAction(
377377
});
378378
}
379379

380-
export async function handleRawWebSocketHandler(
380+
export async function handleRawRequest(
381+
req: Request,
382+
actorDriver: ActorDriver,
383+
actorId: string,
384+
): Promise<Response> {
385+
const actor = await actorDriver.loadActor(actorId);
386+
387+
// Track connection outside of scope for cleanup
388+
let createdConn: AnyConn | undefined;
389+
390+
try {
391+
const conn = await actor.createConn(createRawRequestSocket(), {}, req);
392+
393+
createdConn = conn;
394+
395+
return await actor.handleRawRequest(req, {});
396+
} finally {
397+
// Clean up the connection after the request completes
398+
if (createdConn) {
399+
actor.connDisconnected(createdConn, true);
400+
}
401+
}
402+
}
403+
404+
export async function handleRawWebSocket(
381405
req: Request | undefined,
382406
path: string,
383407
actorDriver: ActorDriver,
@@ -548,30 +572,6 @@ export function getRequestConnParams(req: HonoRequest): unknown {
548572
}
549573
}
550574

551-
export async function handleRawHttpHandler(
552-
req: Request,
553-
actorDriver: ActorDriver,
554-
actorId: string,
555-
): Promise<Response> {
556-
const actor = await actorDriver.loadActor(actorId);
557-
558-
// Track connection outside of scope for cleanup
559-
let createdConn: AnyConn | undefined;
560-
561-
try {
562-
const conn = await actor.createConn(createRawHttpSocket(), {}, req);
563-
564-
createdConn = conn;
565-
566-
return await actor.handleRawRequest(req, {});
567-
} finally {
568-
// Clean up the connection after the request completes
569-
if (createdConn) {
570-
actor.connDisconnected(createdConn, true);
571-
}
572-
}
573-
}
574-
575575
/**
576576
* Truncase the PATH_WEBSOCKET_PREFIX path prefix in order to pass a clean
577577
* path to the onWebSocket handler.

rivetkit-typescript/packages/rivetkit/src/actor/router.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import {
88
type ConnectWebSocketOutput,
99
type ConnsMessageOpts,
1010
handleAction,
11-
handleRawHttpHandler,
12-
handleRawWebSocketHandler,
11+
handleRawRequest,
12+
handleRawWebSocket,
1313
handleWebSocketConnect,
1414
} from "@/actor/router-endpoints";
1515
import {
@@ -171,7 +171,6 @@ export function createActorRouter(
171171
);
172172
});
173173

174-
// Raw HTTP endpoints - /request/*
175174
router.all("/request/*", async (c) => {
176175
// TODO: This is not a clean way of doing this since `/http/` might exist mid-path
177176
// Strip the /http prefix from the URL to get the original path
@@ -193,14 +192,13 @@ export function createActorRouter(
193192
to: correctedRequest.url,
194193
});
195194

196-
return await handleRawHttpHandler(
195+
return await handleRawRequest(
197196
correctedRequest,
198197
actorDriver,
199198
c.env.actorId,
200199
);
201200
});
202201

203-
// Raw WebSocket endpoint - /websocket/*
204202
router.get(`${PATH_WEBSOCKET_PREFIX}*`, async (c) => {
205203
const upgradeWebSocket = runConfig.getUpgradeWebSocket?.();
206204
if (upgradeWebSocket) {
@@ -216,7 +214,7 @@ export function createActorRouter(
216214
pathWithQuery,
217215
});
218216

219-
return await handleRawWebSocketHandler(
217+
return await handleRawWebSocket(
220218
c.req.raw,
221219
pathWithQuery,
222220
actorDriver,

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import { deserializeActorKey } from "@/actor/keys";
1616
import { EncodingSchema } from "@/actor/protocol/serde";
1717
import { type ActorRouter, createActorRouter } from "@/actor/router";
1818
import {
19-
handleRawWebSocketHandler,
19+
handleRawWebSocket,
2020
handleWebSocketConnect,
2121
truncateRawWebSocketPathPrefix,
2222
} from "@/actor/router-endpoints";
@@ -582,7 +582,7 @@ export class EngineActorDriver implements ActorDriver {
582582
requestIdBuf,
583583
);
584584
} else if (url.pathname.startsWith(PATH_WEBSOCKET_PREFIX)) {
585-
wsHandlerPromise = handleRawWebSocketHandler(
585+
wsHandlerPromise = handleRawWebSocket(
586586
request,
587587
url.pathname + url.search,
588588
this,

0 commit comments

Comments
 (0)