Skip to content

Commit 9c72b2f

Browse files
committed
refactor: rename all uses of "rpc" to "action" internally
1 parent 0c0da0f commit 9c72b2f

File tree

25 files changed

+306
-428
lines changed

25 files changed

+306
-428
lines changed

packages/actor-core/README.md

Lines changed: 0 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -12,77 +12,6 @@ Supports Rivet, Cloudflare Workers, Bun, and Node.js.
1212
- [Documentation](https://actorcore.org/)
1313
- [Examples](https://github.com/rivet-gg/actor-core/tree/main/examples)
1414

15-
## Getting Started
16-
17-
### Step 1: Installation
18-
19-
```bash
20-
# npm
21-
npm add actor-core
22-
23-
# pnpm
24-
pnpm add actor-core
25-
26-
# Yarn
27-
yarn add actor-core
28-
29-
# Bun
30-
bun add actor-core
31-
```
32-
33-
### Step 2: Create an Actor
34-
35-
```typescript
36-
import { Actor, type Rpc } from "actor-core";
37-
38-
export interface State {
39-
messages: { username: string; message: string }[];
40-
}
41-
42-
export default class ChatRoom extends Actor<State> {
43-
// initialize this._state
44-
_onInitialize() {
45-
return { messages: [] };
46-
}
47-
48-
// receive an remote procedure call from the client
49-
sendMessage(rpc: Rpc<ChatRoom>, username: string, message: string) {
50-
// save message to persistent storage
51-
this._state.messages.push({ username, message });
52-
53-
// broadcast message to all clients
54-
this._broadcast("newMessage", username, message);
55-
}
56-
}
57-
```
58-
59-
### Step 3: Connect to Actor
60-
61-
```typescript
62-
import { Client } from "actor-core/client";
63-
import type ChatRoom from "../src/chat-room.ts";
64-
65-
const client = new Client(/* manager endpoint */);
66-
67-
// connect to chat room
68-
const chatRoom = await client.get<ChatRoom>({ name: "chat" });
69-
70-
// listen for new messages
71-
chatRoom.on("newMessage", (username: string, message: string) =>
72-
console.log(`Message from ${username}: ${message}`),
73-
);
74-
75-
// send message to room
76-
await chatRoom.sendMessage("william", "All the world's a stage.");
77-
```
78-
79-
### Step 4: Deploy
80-
81-
Deploy to your platform of choice:
82-
83-
- [Cloudflare Workers](https://actorcore.org/platforms/cloudflare-workers)
84-
- [Rivet](https://actorcore.org/platforms/rivet)
85-
8615
## Community & Support
8716

8817
- Join our [Discord](https://rivet.gg/discord)

packages/actor-core/package.json

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -106,16 +106,6 @@
106106
"default": "./dist/cli/mod.cjs"
107107
}
108108
},
109-
"./protocol/http": {
110-
"import": {
111-
"types": "./dist/actor/protocol/http/rpc.d.ts",
112-
"default": "./dist/actor/protocol/http/rpc.js"
113-
},
114-
"require": {
115-
"types": "./dist/actor/protocol/http/rpc.d.cts",
116-
"default": "./dist/actor/protocol/http/rpc.cjs"
117-
}
118-
},
119109
"./test": {
120110
"import": {
121111
"types": "./dist/test/mod.d.ts",
@@ -163,7 +153,7 @@
163153
"sideEffects": false,
164154
"scripts": {
165155
"dev": "yarn build --watch",
166-
"build": "tsup src/mod.ts src/client/mod.ts src/common/log.ts src/actor/errors.ts src/topologies/coordinate/mod.ts src/topologies/partition/mod.ts src/utils.ts src/driver-helpers/mod.ts src/driver-test-suite/mod.ts src/cli/mod.ts src/actor/protocol/inspector/mod.ts src/actor/protocol/http/rpc.ts src/test/mod.ts src/inspector/protocol/actor/mod.ts src/inspector/protocol/manager/mod.ts src/inspector/mod.ts",
156+
"build": "tsup src/mod.ts src/client/mod.ts src/common/log.ts src/actor/errors.ts src/topologies/coordinate/mod.ts src/topologies/partition/mod.ts src/utils.ts src/driver-helpers/mod.ts src/driver-test-suite/mod.ts src/cli/mod.ts src/actor/protocol/inspector/mod.ts src/test/mod.ts src/inspector/protocol/actor/mod.ts src/inspector/protocol/manager/mod.ts src/inspector/mod.ts",
167157
"check-types": "tsc --noEmit",
168158
"boop": "tsc --outDir dist/test -d",
169159
"test": "vitest run",

packages/actor-core/src/actor/errors.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -272,9 +272,9 @@ export class ProxyError extends ActorError {
272272
}
273273
}
274274

275-
export class InvalidRpcRequest extends ActorError {
275+
export class InvalidActionRequest extends ActorError {
276276
constructor(message: string) {
277-
super("invalid_rpc_request", message, { public: true });
277+
super("invalid_action_request", message, { public: true });
278278
}
279279
}
280280

packages/actor-core/src/actor/instance.ts

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -458,7 +458,7 @@ export class ActorInstance<S, CP, CS, V> {
458458
}
459459
}
460460

461-
// State will be flushed at the end of the RPC
461+
// State will be flushed at the end of the action
462462
},
463463
{ ignoreDetached: true },
464464
);
@@ -734,8 +734,8 @@ export class ActorInstance<S, CP, CS, V> {
734734
// MARK: Messages
735735
async processMessage(message: wsToServer.ToServer, conn: Conn<S, CP, CS, V>) {
736736
await processMessage(message, this, conn, {
737-
onExecuteRpc: async (ctx, name, args) => {
738-
return await this.executeRpc(ctx, name, args);
737+
onExecuteAction: async (ctx, name, args) => {
738+
return await this.executeAction(ctx, name, args);
739739
},
740740
onSubscribe: async (eventName, conn) => {
741741
this.#addSubscription(eventName, conn, false);
@@ -820,98 +820,98 @@ export class ActorInstance<S, CP, CS, V> {
820820
}
821821

822822
/**
823-
* Execute an RPC call from a client.
823+
* Execute an action call from a client.
824824
*
825825
* This method handles:
826-
* 1. Validating the RPC name
827-
* 2. Executing the RPC function
828-
* 3. Processing the result through onBeforeRpcResponse (if configured)
826+
* 1. Validating the action name
827+
* 2. Executing the action function
828+
* 3. Processing the result through onBeforeActionResponse (if configured)
829829
* 4. Handling timeouts and errors
830830
* 5. Saving state changes
831831
*
832-
* @param ctx The RPC context
833-
* @param rpcName The name of the RPC being called
834-
* @param args The arguments passed to the RPC
835-
* @returns The result of the RPC call
836-
* @throws {RpcNotFound} If the RPC doesn't exist
837-
* @throws {RpcTimedOut} If the RPC times out
832+
* @param ctx The action context
833+
* @param actionName The name of the action being called
834+
* @param args The arguments passed to the action
835+
* @returns The result of the action call
836+
* @throws {ActionNotFound} If the action doesn't exist
837+
* @throws {ActionTimedOut} If the action times out
838838
* @internal
839839
*/
840-
async executeRpc(
840+
async executeAction(
841841
ctx: ActionContext<S, CP, CS, V>,
842-
rpcName: string,
842+
actionName: string,
843843
args: unknown[],
844844
): Promise<unknown> {
845-
invariant(this.#ready, "exucuting rpc before ready");
845+
invariant(this.#ready, "exucuting action before ready");
846846

847847
// Prevent calling private or reserved methods
848-
if (!(rpcName in this.#config.actions)) {
849-
logger().warn("rpc does not exist", { rpcName });
848+
if (!(actionName in this.#config.actions)) {
849+
logger().warn("action does not exist", { actionName });
850850
throw new errors.ActionNotFound();
851851
}
852852

853853
// Check if the method exists on this object
854-
// biome-ignore lint/suspicious/noExplicitAny: RPC name is dynamic from client
855-
const rpcFunction = this.#config.actions[rpcName];
856-
if (typeof rpcFunction !== "function") {
857-
logger().warn("action not found", { actionName: rpcName });
854+
// biome-ignore lint/suspicious/noExplicitAny: action name is dynamic from client
855+
const actionFunction = this.#config.actions[actionName];
856+
if (typeof actionFunction !== "function") {
857+
logger().warn("action not found", { actionName: actionName });
858858
throw new errors.ActionNotFound();
859859
}
860860

861-
// TODO: pass abortable to the rpc to decide when to abort
861+
// TODO: pass abortable to the action to decide when to abort
862862
// TODO: Manually call abortable for better error handling
863863
// Call the function on this object with those arguments
864864
try {
865865
// Log when we start executing the action
866-
logger().debug("executing action", { actionName: rpcName, args });
866+
logger().debug("executing action", { actionName: actionName, args });
867867

868-
const outputOrPromise = rpcFunction.call(undefined, ctx, ...args);
868+
const outputOrPromise = actionFunction.call(undefined, ctx, ...args);
869869
let output: unknown;
870870
if (outputOrPromise instanceof Promise) {
871871
// Log that we're waiting for an async action
872-
logger().debug("awaiting async action", { actionName: rpcName });
872+
logger().debug("awaiting async action", { actionName: actionName });
873873

874874
output = await deadline(
875875
outputOrPromise,
876876
this.#config.options.action.timeout,
877877
);
878878

879879
// Log that async action completed
880-
logger().debug("async action completed", { actionName: rpcName });
880+
logger().debug("async action completed", { actionName: actionName });
881881
} else {
882882
output = outputOrPromise;
883883
}
884884

885-
// Process the output through onBeforeRpcResponse if configured
885+
// Process the output through onBeforeActionResponse if configured
886886
if (this.#config.onBeforeActionResponse) {
887887
try {
888888
const processedOutput = this.#config.onBeforeActionResponse(
889889
this.actorContext,
890-
rpcName,
890+
actionName,
891891
args,
892892
output,
893893
);
894894
if (processedOutput instanceof Promise) {
895895
logger().debug("awaiting onBeforeActionResponse", {
896-
actionName: rpcName,
896+
actionName: actionName,
897897
});
898898
output = await processedOutput;
899899
logger().debug("onBeforeActionResponse completed", {
900-
actionName: rpcName,
900+
actionName: actionName,
901901
});
902902
} else {
903903
output = processedOutput;
904904
}
905905
} catch (error) {
906-
logger().error("error in `onBeforeRpcResponse`", {
906+
logger().error("error in `onBeforeActionResponse`", {
907907
error: stringifyError(error),
908908
});
909909
}
910910
}
911911

912912
// Log the output before returning
913913
logger().debug("action completed", {
914-
actionName: rpcName,
914+
actionName: actionName,
915915
outputType: typeof output,
916916
isPromise: output instanceof Promise,
917917
});
@@ -922,7 +922,7 @@ export class ActorInstance<S, CP, CS, V> {
922922
throw new errors.ActionTimedOut();
923923
}
924924
logger().error("action error", {
925-
actionName: rpcName,
925+
actionName: actionName,
926926
error: stringifyError(error),
927927
});
928928
throw error;
@@ -932,9 +932,9 @@ export class ActorInstance<S, CP, CS, V> {
932932
}
933933

934934
/**
935-
* Returns a list of RPC methods available on this actor.
935+
* Returns a list of action methods available on this actor.
936936
*/
937-
get rpcs(): string[] {
937+
get actions(): string[] {
938938
return Object.keys(this.#config.actions);
939939
}
940940

@@ -1040,7 +1040,7 @@ export class ActorInstance<S, CP, CS, V> {
10401040
* Runs a promise in the background.
10411041
*
10421042
* This allows the actor runtime to ensure that a promise completes while
1043-
* returning from an RPC request early.
1043+
* returning from an action request early.
10441044
*
10451045
* @param promise - The promise to run in the background.
10461046
*/
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { z } from "zod";
2+
3+
export const ActionRequestSchema = z.object({
4+
// Args
5+
a: z.array(z.unknown()),
6+
});
7+
8+
export const ActionResponseSchema = z.object({
9+
// Output
10+
o: z.unknown(),
11+
});
12+
13+
14+
export type ActionRequest = z.infer<typeof ActionRequestSchema>;
15+
export type ActionResponse = z.infer<typeof ActionResponseSchema>;

packages/actor-core/src/actor/protocol/http/rpc.ts

Lines changed: 0 additions & 15 deletions
This file was deleted.

0 commit comments

Comments
 (0)