Skip to content
This repository was archived by the owner on Oct 22, 2025. It is now read-only.
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
3 changes: 3 additions & 0 deletions packages/rivetkit/scripts/dump-openapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ function main() {
const driverConfig: RunConfig = RunConfigSchema.parse({
driver: createFileSystemOrMemoryDriver(false),
getUpgradeWebSocket: () => () => unimplemented(),
inspector: {
enabled: false,
},
});

const managerDriver: ManagerDriver = {
Expand Down
21 changes: 13 additions & 8 deletions packages/rivetkit/src/actor/router.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Hono, type Context as HonoContext } from "hono";
import { cors } from "hono/cors";
import invariant from "invariant";
import { EncodingSchema } from "@/actor/protocol/serde";
import {
Expand Down Expand Up @@ -241,14 +242,18 @@ export function createActorRouter(
router.route(
"/inspect",
new Hono<ActorInspectorRouterEnv & { Bindings: ActorRouterBindings }>()
.use(secureInspector(runConfig), async (c, next) => {
const inspector = (await actorDriver.loadActor(c.env.actorId))
.inspector;
invariant(inspector, "inspector not supported on this platform");

c.set("inspector", inspector);
await next();
})
.use(
cors(runConfig.inspector.cors),
secureInspector(runConfig),
async (c, next) => {
const inspector = (await actorDriver.loadActor(c.env.actorId))
.inspector;
invariant(inspector, "inspector not supported on this platform");

c.set("inspector", inspector);
return next();
},
)
.route("/", createActorInspectorRouter()),
);
}
Expand Down
10 changes: 7 additions & 3 deletions packages/rivetkit/src/inspector/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const defaultEnabled = () => {

const defaultInspectorOrigins = [
"http://localhost:43708",
"http://localhost:43709",
"https://studio.rivet.gg",
];

Expand All @@ -40,10 +41,13 @@ const defaultCors: CorsOptions = {
},
allowMethods: ["GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"],
allowHeaders: [
"Content-Type",
"Authorization",
HEADER_ACTOR_QUERY,
"last-event-id",
"Content-Type",
"User-Agent",
"baggage",
"sentry-trace",
"x-rivet-actor",
"x-rivet-target",
],
maxAge: 3600,
credentials: true,
Expand Down
55 changes: 43 additions & 12 deletions packages/rivetkit/src/manager/router.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
import { createRoute, OpenAPIHono } from "@hono/zod-openapi";
import * as cbor from "cbor-x";
import type { Hono } from "hono";
import { cors } from "hono/cors";
import { Hono } from "hono";
import { cors as corsMiddleware } from "hono/cors";
import { createMiddleware } from "hono/factory";
import { z } from "zod";
import {
ActorError,
ActorNotFound,
FeatureNotImplemented,
MissingActorHeader,
RouteNotFound,
Unsupported,
WebSocketsNotEnabled,
} from "@/actor/errors";
import {
handleRouteError,
handleRouteNotFound,
loggerMiddleware,
} from "@/common/router";
import { createManagerInspectorRouter } from "@/inspector/manager";
import { secureInspector } from "@/inspector/utils";
import {
type ActorsCreateRequest,
ActorsCreateRequestSchema,
Expand Down Expand Up @@ -68,12 +70,12 @@ export function createManagerRouter(

router.use("*", loggerMiddleware(logger()));

if (runConfig.cors) {
router.use("*", cors(runConfig.cors));
}
const cors = runConfig.cors
? corsMiddleware(runConfig.cors)
: createMiddleware((_c, next) => next());

// Actor proxy middleware - intercept requests with x-rivet-target=actor
router.use("*", async (c, next) => {
router.use("*", cors, async (c, next) => {
const target = c.req.header("x-rivet-target");
const actorId = c.req.header("x-rivet-actor");

Expand All @@ -98,9 +100,16 @@ export function createManagerRouter(

// For WebSocket, use the driver's proxyWebSocket method
// Extract any additional headers that might be needed
const encoding = c.req.header("x-rivet-encoding") || "json";
const connParams = c.req.header("x-rivet-conn-params");
const authData = c.req.header("x-rivet-auth-data");
const encoding =
c.req.header("X-RivetKit-Encoding") ||
c.req.header("x-rivet-encoding") ||
"json";
const connParams =
c.req.header("X-RivetKit-Conn-Params") ||
c.req.header("x-rivet-conn-params");
const authData =
c.req.header("X-RivetKit-Auth-Data") ||
c.req.header("x-rivet-auth-data");

return await managerDriver.proxyWebSocket(
c,
Expand Down Expand Up @@ -135,7 +144,7 @@ export function createManagerRouter(
});

// GET /
router.get("/", (c) => {
router.get("/", cors, (c) => {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Inconsistent CORS middleware application. The root route handler manually applies CORS middleware, but this creates inconsistent behavior since CORS is already applied globally in the proxy middleware. This could lead to duplicate CORS headers or conflicts. Remove the manual 'cors' parameter from this route handler.

Suggested change
router.get("/", cors, (c) => {
router.get("/", (c) => {

Spotted by Diamond

Fix in Graphite


Is this helpful? React 👍 or 👎 to let us know.

return c.text(
"This is a RivetKit server.\n\nLearn more at https://rivetkit.org",
);
Expand All @@ -144,6 +153,7 @@ export function createManagerRouter(
// GET /actors/by-id
{
const route = createRoute({
middleware: [cors],
method: "get",
path: "/actors/by-id",
request: {
Expand Down Expand Up @@ -177,6 +187,7 @@ export function createManagerRouter(
// PUT /actors/by-id
{
const route = createRoute({
cors: [cors],
method: "put",
path: "/actors/by-id",
request: {
Expand Down Expand Up @@ -241,6 +252,7 @@ export function createManagerRouter(
// GET /actors/{actor_id}
{
const route = createRoute({
middleware: [cors],
method: "get",
path: "/actors/{actor_id}",
request: {
Expand Down Expand Up @@ -287,6 +299,7 @@ export function createManagerRouter(
// POST /actors
{
const route = createRoute({
middleware: [cors],
method: "post",
path: "/actors",
request: {
Expand Down Expand Up @@ -346,6 +359,7 @@ export function createManagerRouter(
// DELETE /actors/{actor_id}
{
const route = createRoute({
middleware: [cors],
method: "delete",
path: "/actors/{actor_id}",
request: {
Expand All @@ -370,6 +384,23 @@ export function createManagerRouter(
});
}

if (runConfig.inspector?.enabled) {
if (!managerDriver.inspector) {
throw new Unsupported("inspector");
}
router.route(
"/inspect",
new Hono<{ Variables: { inspector: any } }>()
.use(corsMiddleware(runConfig.inspector.cors))
.use(secureInspector(runConfig))
.use((c, next) => {
c.set("inspector", managerDriver.inspector!);
return next();
})
.route("/", createManagerInspectorRouter()),
);
}

// Error handling
router.notFound(handleRouteNotFound);
router.onError(handleRouteError);
Expand Down
10 changes: 3 additions & 7 deletions packages/rivetkit/src/registry/mod.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import type { Hono } from "hono";
import { type Client, createClientWithDriver } from "@/client/client";
import {
configureBaseLogger,
configureDefaultLogger,
getPinoLevel,
} from "@/common/log";
import { configureBaseLogger, configureDefaultLogger } from "@/common/log";
import { chooseDefaultDriver } from "@/drivers/default";
import { getInspectorUrl } from "@/inspector/utils";
import { createManagerRouter } from "@/manager/router";
Expand Down Expand Up @@ -86,7 +82,7 @@ export class Registry<A extends RegistryActors> {
definitions: Object.keys(this.#config.use).length,
...driverLog,
});
if (config.inspector?.enabled) {
if (config.inspector?.enabled && managerDriver.inspector) {
logger().info({ msg: "inspector ready", url: getInspectorUrl(config) });
}

Expand All @@ -100,7 +96,7 @@ export class Registry<A extends RegistryActors> {
const padding = " ".repeat(Math.max(0, 13 - k.length));
console.log(` - ${k}:${padding}${v}`);
}
if (config.inspector?.enabled) {
if (config.inspector?.enabled && managerDriver.inspector) {
console.log(` - Inspector: ${getInspectorUrl(config)}`);
}
console.log();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ export async function openWebSocketToActor(
headers: buildGuardHeadersForWebSocket(actorId, encoding, params),
});

// Set binary type to arraybuffer for proper encoding support
ws.binaryType = "arraybuffer";

logger().debug({ msg: "websocket connection opened", actorId });

return ws as UniversalWebSocket;
Expand Down
Loading