Skip to content
Closed
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
2 changes: 1 addition & 1 deletion examples/trpc/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"dependencies": {
"@trpc/client": "^11.3.1",
"@trpc/server": "^11.4.2",
"zod": "^3.24.1"
"zod": "^3.25.67"
},
"stableVersion": "0.8.0"
}
1 change: 0 additions & 1 deletion packages/core/fixtures/driver-test-suite/action-inputs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,3 @@ export const inputActor = actor({
},
},
});

2 changes: 0 additions & 2 deletions packages/core/fixtures/driver-test-suite/action-timeout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,5 +66,3 @@ export const syncTimeoutActor = actor({
},
},
});


7 changes: 3 additions & 4 deletions packages/core/fixtures/driver-test-suite/action-types.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { actor, UserError } from "@rivetkit/core";
import { UserError, actor } from "@rivetkit/core";

// Actor with synchronous actions
export const syncActionActor = actor({
onAuth: () => {},
state: { value: 0 },
actions: {
// Simple synchronous action that returns a value directly
increment: (c, amount: number = 1) => {
increment: (c, amount = 1) => {
c.state.value += amount;
return c.state.value;
},
Expand All @@ -30,7 +30,7 @@ export const asyncActionActor = actor({
state: { value: 0, data: null as any },
actions: {
// Async action with a delay
delayedIncrement: async (c, amount: number = 1) => {
delayedIncrement: async (c, amount = 1) => {
await Promise.resolve();
c.state.value += amount;
return c.state.value;
Expand Down Expand Up @@ -83,4 +83,3 @@ export const promiseActor = actor({
},
},
});

32 changes: 17 additions & 15 deletions packages/core/fixtures/driver-test-suite/auth.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { actor, UserError } from "@rivetkit/core";
import { UserError, actor } from "@rivetkit/core";

// Basic auth actor - requires API key
export const authActor = actor({
Expand All @@ -9,11 +9,11 @@ export const authActor = actor({
if (!apiKey) {
throw new UserError("API key required", { code: "missing_auth" });
}

if (apiKey !== "valid-api-key") {
throw new UserError("Invalid API key", { code: "invalid_auth" });
}

return { userId: "user123", token: apiKey };
},
actions: {
Expand All @@ -30,17 +30,21 @@ export const intentAuthActor = actor({
state: { value: 0 },
onAuth: (opts) => {
const { req, intents, params } = opts;
console.log('intents', intents, params);
console.log("intents", intents, params);
const role = (params as any)?.role;

if (intents.has("create") && role !== "admin") {
throw new UserError("Admin role required for create operations", { code: "insufficient_permissions" });
throw new UserError("Admin role required for create operations", {
code: "insufficient_permissions",
});
}

if (intents.has("action") && !["admin", "user"].includes(role || "")) {
throw new UserError("User or admin role required for actions", { code: "insufficient_permissions" });
throw new UserError("User or admin role required for actions", {
code: "insufficient_permissions",
});
}

return { role, timestamp: Date.now() };
},
actions: {
Expand Down Expand Up @@ -79,20 +83,18 @@ export const noAuthActor = actor({
export const asyncAuthActor = actor({
state: { count: 0 },
onAuth: async (opts) => {
const { req, intents, params } = opts;
// Simulate async auth check (e.g., database lookup)
await new Promise(resolve => setTimeout(resolve, 10));

const { params } = opts;

const token = (params as any)?.token;
if (!token) {
throw new UserError("Token required", { code: "missing_token" });
}

// Simulate token validation
if (token === "invalid") {
throw new UserError("Token is invalid", { code: "invalid_token" });
}

return { userId: `user-${token}`, validated: true };
},
actions: {
Expand Down
1 change: 0 additions & 1 deletion packages/core/fixtures/driver-test-suite/conn-params.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,3 @@ export const counterWithParams = actor({
},
},
});

16 changes: 10 additions & 6 deletions packages/core/fixtures/driver-test-suite/conn-state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,12 @@ export const connStateActor = actor({
},
actions: {
// Action to increment the connection's counter
incrementConnCounter: (c, amount: number = 1) => {
incrementConnCounter: (c, amount = 1) => {
c.conn.state.counter += amount;
},

// Action to increment the shared counter
incrementSharedCounter: (c, amount: number = 1) => {
incrementSharedCounter: (c, amount = 1) => {
c.state.sharedCounter += amount;
return c.state.sharedCounter;
},
Expand All @@ -70,13 +70,18 @@ export const connStateActor = actor({

// Get all active connection states
getAllConnectionStates: (c) => {
return c.conns.entries().map(([id, conn]) => ({ id, ...conn.state })).toArray();
return c.conns
.entries()
.map(([id, conn]) => ({ id, ...conn.state }))
.toArray();
},

// Send message to a specific connection with matching ID
sendToConnection: (c, targetId: string, message: string) => {
if (c.conns.has(targetId)) {
c.conns.get(targetId)!.send("directMessage", { from: c.conn.id, message });
c.conns
.get(targetId)!
.send("directMessage", { from: c.conn.id, message });
return true;
} else {
return false;
Expand All @@ -90,8 +95,7 @@ export const connStateActor = actor({
) => {
if (updates.username) c.conn.state.username = updates.username;
if (updates.role) c.conn.state.role = updates.role;
return c.conn.state;
return c.conn.state;
},
},
});

3 changes: 1 addition & 2 deletions packages/core/fixtures/driver-test-suite/error-handling.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { actor, UserError } from "@rivetkit/core";
import { UserError, actor } from "@rivetkit/core";

export const errorHandlingActor = actor({
onAuth: () => {},
Expand Down Expand Up @@ -95,4 +95,3 @@ export const customTimeoutActor = actor({
},
},
});

1 change: 0 additions & 1 deletion packages/core/fixtures/driver-test-suite/lifecycle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,3 @@ export const counterWithLifecycle = actor({
},
},
});

2 changes: 0 additions & 2 deletions packages/core/fixtures/driver-test-suite/metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,5 +74,3 @@ export const metadataActor = actor({
},
},
});


36 changes: 18 additions & 18 deletions packages/core/fixtures/driver-test-suite/registry.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,39 @@
import { setup } from "@rivetkit/core";

// Import actors from individual files
import { counter } from "./counter";
import { counterWithLifecycle } from "./lifecycle";
import { scheduled } from "./scheduled";
import { errorHandlingActor, customTimeoutActor } from "./error-handling";
import { inputActor } from "./action-inputs";
import {
shortTimeoutActor,
longTimeoutActor,
defaultTimeoutActor,
longTimeoutActor,
shortTimeoutActor,
syncTimeoutActor,
} from "./action-timeout";
import {
syncActionActor,
asyncActionActor,
promiseActor,
syncActionActor,
} from "./action-types";
import {
asyncAuthActor,
authActor,
intentAuthActor,
noAuthActor,
publicActor,
} from "./auth";
import { counterWithParams } from "./conn-params";
import { connStateActor } from "./conn-state";
// Import actors from individual files
import { counter } from "./counter";
import { customTimeoutActor, errorHandlingActor } from "./error-handling";
import { counterWithLifecycle } from "./lifecycle";
import { metadataActor } from "./metadata";
import { scheduled } from "./scheduled";
import {
staticVarActor,
nestedVarActor,
driverCtxActor,
dynamicVarActor,
nestedVarActor,
staticVarActor,
uniqueVarActor,
driverCtxActor,
} from "./vars";
import {
authActor,
intentAuthActor,
publicActor,
noAuthActor,
asyncAuthActor,
} from "./auth";

// Consolidated setup with all actors
export const registry = setup({
Expand Down
2 changes: 0 additions & 2 deletions packages/core/fixtures/driver-test-suite/scheduled.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,5 +75,3 @@ export const scheduled = actor({
},
},
});


2 changes: 0 additions & 2 deletions packages/core/fixtures/driver-test-suite/vars.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,5 +94,3 @@ export const driverCtxActor = actor({
},
},
});


2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@
"invariant": "^2.2.4",
"on-change": "^5.0.1",
"p-retry": "^6.2.1",
"zod": "^3.24.1"
"zod": "^3.25.67"
},
"devDependencies": {
"@hono/node-server": "^1.14.0",
Expand Down
23 changes: 7 additions & 16 deletions packages/core/scripts/dump-openapi.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,16 @@
import * as fs from "node:fs/promises";
import { resolve } from "node:path";
import type { ConnectionHandlers } from "@/actor/router-endpoints";
import type { ClientDriver } from "@/client/client";
import { createManagerRouter } from "@/manager/router";
import { RegistryConfig, RegistryConfigSchema, Encoding, setup } from "@/mod";
import { ConnectionHandlers } from "@/actor/router-endpoints";
import { type RegistryConfig, RegistryConfigSchema, setup } from "@/mod";
import { type RunConfig, RunConfigSchema } from "@/registry/run-config";
import {
TestGlobalState,
TestActorDriver,
TestGlobalState,
TestManagerDriver,
} from "@/test/driver/mod";
import { OpenAPIHono } from "@hono/zod-openapi";
import { VERSION } from "@/utils";
import * as fs from "node:fs/promises";
import { resolve } from "node:path";
import { ClientDriver } from "@/client/client";
import { ActorQuery } from "@/manager/protocol/query";
import { ToServer } from "@/actor/protocol/message/to-server";
import { EventSource } from "eventsource";
import { Context } from "hono";
import {
DriverConfig,
RunConfig,
RunConfigSchema,
} from "@/registry/run-config";

function main() {
const registryConfig: RegistryConfig = RegistryConfigSchema.parse({
Expand Down
8 changes: 4 additions & 4 deletions packages/core/src/actor/action.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import type { Conn } from "./connection";
import type { Logger } from "@/common/log";
import type { ActorKey } from "@/common/utils";
import type { Schedule } from "./schedule";
import type { ActorKey } from "@/actor/mod";
import type { Conn } from "./connection";
import type { ConnId } from "./connection";
import type { SaveStateOptions } from "./instance";
import type { ActorContext } from "./context";
import type { SaveStateOptions } from "./instance";
import type { Schedule } from "./schedule";

/**
* Context for a remote procedure call.
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/actor/config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { Conn } from "./connection";
import { z } from "zod";
import type { ActionContext } from "./action";
import type { Conn } from "./connection";
import type { ActorContext } from "./context";
import { z } from "zod";

// This schema is used to validate the input at runtime. The generic types are defined below in `ActorConfig`.
//
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/actor/conn-routing-handler.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { UpgradeWebSocket } from "@/utils";
import type { Context as HonoContext } from "hono";
import type { Encoding } from "./protocol/serde";
import type { ConnectionHandlers as ConnHandlers } from "./router-endpoints";
import type { Context as HonoContext } from "hono";

/**
* Deterines how requests to actors should be routed.
Expand Down Expand Up @@ -37,7 +37,7 @@ export type SendRequestHandler = (
export type OpenWebSocketHandler = (
actorId: string,
encodingKind: Encoding,
params: unknown
params: unknown,
) => Promise<WebSocket>;

export type ProxyRequestHandler = (
Expand Down
19 changes: 7 additions & 12 deletions packages/core/src/actor/connection.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import type { ActorInstance } from "./instance";
import * as errors from "./errors";
import { generateSecureToken } from "./utils";
import { CachedSerializer } from "./protocol/serde";
import type * as messageToClient from "@/actor/protocol/message/to-client";
import type * as wsToClient from "@/actor/protocol/message/to-client";
import type { ConnDriver } from "./driver";
import type * as messageToClient from "@/actor/protocol/message/to-client";
import * as errors from "./errors";
import type { ActorInstance } from "./instance";
import type { PersistedConn } from "./persisted";
import type * as wsToClient from "@/actor/protocol/message/to-client";
import { CachedSerializer } from "./protocol/serde";
import { generateSecureToken } from "./utils";

export function generateConnId(): string {
return crypto.randomUUID();
Expand Down Expand Up @@ -157,11 +157,6 @@ export class Conn<S, CP, CS, V, I, AD, DB> {
* @param reason - The reason for disconnection.
*/
public async disconnect(reason?: string) {
await this.#driver.disconnect(
this.#actor,
this,
this.__persist.ds,
reason,
);
await this.#driver.disconnect(this.#actor, this, this.__persist.ds, reason);
}
}
4 changes: 2 additions & 2 deletions packages/core/src/actor/context.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { Logger } from "@/common/log";
import type { ActorInstance, SaveStateOptions } from "./instance";
import type { ActorKey } from "@/actor/mod";
import type { Conn, ConnId } from "./connection";
import type { ActorKey } from "@/common/utils";
import type { ActorInstance, SaveStateOptions } from "./instance";
import type { Schedule } from "./schedule";

/**
Expand Down
Loading
Loading