Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -335,19 +335,21 @@ export async function handleAction(
}

export async function handleRawRequest(
c: HonoContext,
req: Request,
actorDriver: ActorDriver,
actorId: string,
): Promise<Response> {
const actor = await actorDriver.loadActor(actorId);
const parameters = getRequestConnParams(c.req);

// Track connection outside of scope for cleanup
let createdConn: AnyConn | undefined;

try {
const conn = await actor.connectionManager.prepareAndConnectConn(
createRawRequestSocket(),
{},
parameters,
req,
);

Expand All @@ -368,6 +370,7 @@ export async function handleRawWebSocket(
actorDriver: ActorDriver,
actorId: string,
requestIdBuf: ArrayBuffer | undefined,
connParams: unknown | undefined,
): Promise<UpgradeWebSocketArgs> {
const exposeInternalError = req
? getRequestExposeInternalError(req)
Expand Down Expand Up @@ -414,7 +417,7 @@ export async function handleRawWebSocket(
);
const conn = await actor.connectionManager.prepareAndConnectConn(
driver,
{},
connParams ?? {},
newRequest,
);
createdConn = conn;
Expand Down
7 changes: 6 additions & 1 deletion rivetkit-typescript/packages/rivetkit/src/actor/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,6 @@ export function createActorRouter(
const upgradeWebSocket = runConfig.getUpgradeWebSocket?.();
if (upgradeWebSocket) {
return upgradeWebSocket(async (c) => {
// Parse configuration from Sec-WebSocket-Protocol header
const protocols = c.req.header("sec-websocket-protocol");
const { encoding, connParams } =
parseWebSocketProtocols(protocols);
Expand Down Expand Up @@ -165,6 +164,7 @@ export function createActorRouter(
});

return await handleRawRequest(
c,
correctedRequest,
actorDriver,
c.env.actorId,
Expand All @@ -178,12 +178,16 @@ export function createActorRouter(
const url = new URL(c.req.url);
const pathWithQuery = c.req.path + url.search;

const protocols = c.req.header("sec-websocket-protocol");
const { connParams } = parseWebSocketProtocols(protocols);

loggerWithoutContext().debug({
msg: "actor router raw websocket",
path: c.req.path,
url: c.req.url,
search: url.search,
pathWithQuery,
connParams,
});

return await handleRawWebSocket(
Expand All @@ -192,6 +196,7 @@ export function createActorRouter(
actorDriver,
c.env.actorId,
undefined,
connParams,
);
})(c, noopNext());
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,7 @@ export class EngineActorDriver implements ActorDriver {
this,
actorId,
requestIdBuf,
connParams,
);
} else {
throw new Error(`Unreachable path: ${url.pathname}`);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ export class FileSystemManagerDriver implements ManagerDriver {
this.#actorDriver,
actorId,
undefined,
params,
);
return new InlineWebSocketAdapter2(wsHandler);
} else {
Expand Down Expand Up @@ -246,6 +247,7 @@ export class FileSystemManagerDriver implements ManagerDriver {
this.#actorDriver,
actorId,
undefined,
connParams,
);
return upgradeWebSocket(() => wsHandler)(c, noopNext());
} else {
Expand Down
Loading