Skip to content

Commit 290e897

Browse files
committed
chore(rivetkit): auto-generate inspector token for engine driver
1 parent 989db0f commit 290e897

File tree

6 files changed

+42
-5
lines changed

6 files changed

+42
-5
lines changed

rivetkit-typescript/packages/cloudflare-workers/src/actor-driver.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ import type {
66
} from "rivetkit";
77
import { lookupInRegistry } from "rivetkit";
88
import type { Client } from "rivetkit/client";
9-
import type {
10-
ActorDriver,
11-
AnyActorInstance,
12-
ManagerDriver,
9+
import {
10+
type ActorDriver,
11+
type AnyActorInstance,
12+
type ManagerDriver,
1313
} from "rivetkit/driver-helpers";
1414
import { promiseWithResolvers } from "rivetkit/utils";
1515
import { KEYS } from "./actor-handler-do";
@@ -239,6 +239,7 @@ export class CloudflareActorsActorDriver implements ActorDriver {
239239
// Persist data key
240240
return Uint8Array.from([1]);
241241
}
242+
242243
}
243244

244245
export function createCloudflareActorsActorDriverBuilder(

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
export const KEYS = {
22
PERSIST_DATA: Uint8Array.from([1]),
33
CONN_PREFIX: Uint8Array.from([2]), // Prefix for connection keys
4+
INSPECTOR_TOKEN: Uint8Array.from([3]), // Inspector token key
45
};
56

67
// Helper to create a connection key

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

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import { serializeActorKey } from "../keys";
2424
import { processMessage } from "../protocol/old";
2525
import { CachedSerializer } from "../protocol/serde";
2626
import { Schedule } from "../schedule";
27-
import { DeadlineError, deadline } from "../utils";
27+
import { DeadlineError, deadline, generateSecureToken } from "../utils";
2828
import { ConnectionManager } from "./connection-manager";
2929
import { EventManager } from "./event-manager";
3030
import { KEYS } from "./kv";
@@ -104,6 +104,7 @@ export class ActorInstance<S, CP, CS, V, I, DB extends AnyDatabaseProvider> {
104104
#schedule!: Schedule;
105105

106106
// MARK: - Inspector
107+
#inspectorToken?: string;
107108
#inspector = new ActorInspector(() => {
108109
return {
109110
isDbEnabled: async () => {
@@ -218,6 +219,10 @@ export class ActorInstance<S, CP, CS, V, I, DB extends AnyDatabaseProvider> {
218219
return this.#inspector;
219220
}
220221

222+
get inspectorToken(): string | undefined {
223+
return this.#inspectorToken;
224+
}
225+
221226
get conns(): Map<ConnId, Conn<S, CP, CS, V, I, DB>> {
222227
return this.#connectionManager.connections;
223228
}
@@ -309,6 +314,9 @@ export class ActorInstance<S, CP, CS, V, I, DB extends AnyDatabaseProvider> {
309314
// Read and initialize state
310315
await this.#initializeState();
311316

317+
// Generate or load inspector token
318+
await this.#initializeInspectorToken();
319+
312320
// Initialize variables
313321
if (this.#varsEnabled) {
314322
await this.#initializeVars();
@@ -831,6 +839,29 @@ export class ActorInstance<S, CP, CS, V, I, DB extends AnyDatabaseProvider> {
831839
}
832840
}
833841

842+
async #initializeInspectorToken() {
843+
// Try to load existing token
844+
const [tokenBuffer] = await this.#actorDriver.kvBatchGet(
845+
this.#actorId,
846+
[KEYS.INSPECTOR_TOKEN],
847+
);
848+
849+
if (tokenBuffer !== null) {
850+
// Token exists, decode it
851+
const decoder = new TextDecoder();
852+
this.#inspectorToken = decoder.decode(tokenBuffer);
853+
this.#rLog.debug({ msg: "loaded existing inspector token" });
854+
} else {
855+
// Generate new token
856+
this.#inspectorToken = generateSecureToken();
857+
const tokenBytes = new TextEncoder().encode(this.#inspectorToken);
858+
await this.#actorDriver.kvBatchPut(this.#actorId, [
859+
[KEYS.INSPECTOR_TOKEN, tokenBytes],
860+
]);
861+
this.#rLog.debug({ msg: "generated new inspector token" });
862+
}
863+
}
864+
834865
async #initializeVars() {
835866
let vars: V | undefined;
836867
if ("createVars" in this.#config) {

rivetkit-typescript/packages/rivetkit/src/inspector/utils.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ export function getInspectorUrl(runConfig: RunnerConfigInput | undefined) {
7777

7878
export const isInspectorEnabled = (
7979
runConfig: RunConfig,
80+
// TODO(kacper): Remove context in favor of using the gateway, so only context is the actor
8081
context: "actor" | "manager",
8182
) => {
8283
if (typeof runConfig.inspector?.enabled === "boolean") {

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,13 @@ export interface ManagerDriver {
4444
router: Hono,
4545
) => void;
4646

47+
// TODO(kacper): Remove this in favor of standard manager API
4748
/**
4849
* @internal
4950
*/
5051
readonly inspector?: ManagerInspector;
5152

53+
// TODO(kacper): Remove this in favor of ActorDriver.getinspectorToken
5254
/**
5355
* Get or create the inspector access token.
5456
* @experimental

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,7 @@ function addManagerRoutes(
218218
managerDriver: ManagerDriver,
219219
router: OpenAPIHono,
220220
) {
221+
// TODO(kacper): Remove this in favor of standard manager API
221222
// Inspector
222223
if (isInspectorEnabled(runConfig, "manager")) {
223224
if (!managerDriver.inspector) {

0 commit comments

Comments
 (0)