Skip to content

Commit f7f456a

Browse files
committed
chore: move parameters & other properties to headers for e2ee
1 parent 2759d20 commit f7f456a

27 files changed

+789
-412
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { actor, setup } from "actor-core";
2+
3+
const counterWithParams = actor({
4+
state: { count: 0, initializers: [] as string[] },
5+
createConnState: (c, { params }: { params: { name?: string } }) => {
6+
return {
7+
name: params?.name || "anonymous",
8+
};
9+
},
10+
onConnect: (c, conn) => {
11+
// Record connection name
12+
c.state.initializers.push(conn.state.name);
13+
},
14+
actions: {
15+
increment: (c, x: number) => {
16+
c.state.count += x;
17+
c.broadcast("newCount", {
18+
count: c.state.count,
19+
by: c.conn.state.name,
20+
});
21+
return c.state.count;
22+
},
23+
getInitializers: (c) => {
24+
return c.state.initializers;
25+
},
26+
},
27+
});
28+
29+
export const app = setup({
30+
actors: { counter: counterWithParams },
31+
});
32+
33+
export type App = typeof app;
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { actor, setup } from "actor-core";
2+
3+
const lifecycleActor = actor({
4+
state: {
5+
count: 0,
6+
events: [] as string[],
7+
},
8+
createConnState: () => ({ joinTime: Date.now() }),
9+
onStart: (c) => {
10+
c.state.events.push("onStart");
11+
},
12+
onBeforeConnect: (c, { params }: { params: any }) => {
13+
c.state.events.push("onBeforeConnect");
14+
// Could throw here to reject connection
15+
},
16+
onConnect: (c) => {
17+
c.state.events.push("onConnect");
18+
},
19+
onDisconnect: (c) => {
20+
c.state.events.push("onDisconnect");
21+
},
22+
actions: {
23+
getEvents: (c) => {
24+
return c.state.events;
25+
},
26+
increment: (c, x: number) => {
27+
c.state.count += x;
28+
return c.state.count;
29+
},
30+
},
31+
});
32+
33+
export const app = setup({
34+
actors: { counter: lifecycleActor },
35+
});
36+
37+
export type App = typeof app;
38+

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

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -212,18 +212,6 @@ export class UserError extends ActorError {
212212
}
213213
}
214214

215-
// Proxy-related errors
216-
217-
export class MissingRequiredParameters extends ActorError {
218-
constructor(missingParams: string[]) {
219-
super(
220-
"missing_required_parameters",
221-
`Missing required parameters: ${missingParams.join(", ")}`,
222-
{ public: true }
223-
);
224-
}
225-
}
226-
227215
export class InvalidQueryJSON extends ActorError {
228216
constructor(error?: unknown) {
229217
super(
@@ -234,11 +222,11 @@ export class InvalidQueryJSON extends ActorError {
234222
}
235223
}
236224

237-
export class InvalidQueryFormat extends ActorError {
225+
export class InvalidRequest extends ActorError {
238226
constructor(error?: unknown) {
239227
super(
240-
"invalid_query_format",
241-
`Invalid query format: ${error}`,
228+
"invalid_request",
229+
`Invalid request: ${error}`,
242230
{ public: true, cause: error }
243231
);
244232
}
@@ -280,12 +268,6 @@ export class InvalidRpcRequest extends ActorError {
280268
}
281269
}
282270

283-
export class InvalidRequest extends ActorError {
284-
constructor(message: string) {
285-
super("invalid_request", message, { public: true });
286-
}
287-
}
288-
289271
export class InvalidParams extends ActorError {
290272
constructor(message: string) {
291273
super("invalid_params", message, { public: true });

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -720,7 +720,8 @@ export class ActorInstance<S, CP, CS, V> {
720720
new CachedSerializer<wsToClient.ToClient>({
721721
b: {
722722
i: {
723-
ci: `${conn.id}`,
723+
ai: this.id,
724+
ci: conn.id,
724725
ct: conn._token,
725726
},
726727
},

packages/actor-core/src/actor/protocol/message/mod.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
} from "@/actor/protocol/serde";
1616
import { deconstructError } from "@/common/utils";
1717
import { Actions } from "@/actor/config";
18+
import invariant from "invariant";
1819

1920
export const TransportSchema = z.enum(["websocket", "sse"]);
2021

@@ -91,7 +92,9 @@ export async function processMessage<S, CP, CS, V>(
9192
let rpcName: string | undefined;
9293

9394
try {
94-
if ("rr" in message.b) {
95+
if ("i" in message.b) {
96+
invariant(false, "should not be notified of init event");
97+
} else if ("rr" in message.b) {
9598
// RPC request
9699

97100
if (handler.onExecuteRpc === undefined) {

packages/actor-core/src/actor/protocol/message/to-client.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import { z } from "zod";
22

33
// Only called for SSE because we don't need this for WebSockets
44
export const InitSchema = z.object({
5+
// Actor ID
6+
ai: z.string(),
57
// Connection ID
68
ci: z.string(),
79
// Connection token

packages/actor-core/src/actor/protocol/message/to-server.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
import { z } from "zod";
22

3+
const InitSchema = z.object({
4+
// Conn Params
5+
p: z.unknown({}).optional(),
6+
});
7+
38
const RpcRequestSchema = z.object({
49
// ID
510
i: z.number().int(),
@@ -19,6 +24,7 @@ const SubscriptionRequestSchema = z.object({
1924
export const ToServerSchema = z.object({
2025
// Body
2126
b: z.union([
27+
z.object({ i: InitSchema }),
2228
z.object({ rr: RpcRequestSchema }),
2329
z.object({ sr: SubscriptionRequestSchema }),
2430
]),

0 commit comments

Comments
 (0)