Skip to content

chore: move parameters & other properties to headers for e2ee #963

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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
33 changes: 33 additions & 0 deletions packages/actor-core/fixtures/driver-test-suite/conn-params.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { actor, setup } from "actor-core";

const counterWithParams = actor({
state: { count: 0, initializers: [] as string[] },
createConnState: (c, { params }: { params: { name?: string } }) => {
return {
name: params?.name || "anonymous",
};
},
onConnect: (c, conn) => {
// Record connection name
c.state.initializers.push(conn.state.name);
},
actions: {
increment: (c, x: number) => {
c.state.count += x;
c.broadcast("newCount", {
count: c.state.count,
by: c.conn.state.name,
});
return c.state.count;
},
getInitializers: (c) => {
return c.state.initializers;
},
},
});

export const app = setup({
actors: { counter: counterWithParams },
});

export type App = typeof app;
38 changes: 38 additions & 0 deletions packages/actor-core/fixtures/driver-test-suite/lifecycle.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { actor, setup } from "actor-core";

const lifecycleActor = actor({
state: {
count: 0,
events: [] as string[],
},
createConnState: () => ({ joinTime: Date.now() }),
onStart: (c) => {
c.state.events.push("onStart");
},
onBeforeConnect: (c, { params }: { params: any }) => {
c.state.events.push("onBeforeConnect");
// Could throw here to reject connection
},
onConnect: (c) => {
c.state.events.push("onConnect");
},
onDisconnect: (c) => {
c.state.events.push("onDisconnect");
},
actions: {
getEvents: (c) => {
return c.state.events;
},
increment: (c, x: number) => {
c.state.count += x;
return c.state.count;
},
},
});

export const app = setup({
actors: { counter: lifecycleActor },
});

export type App = typeof app;

24 changes: 3 additions & 21 deletions packages/actor-core/src/actor/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,18 +212,6 @@ export class UserError extends ActorError {
}
}

// Proxy-related errors

export class MissingRequiredParameters extends ActorError {
constructor(missingParams: string[]) {
super(
"missing_required_parameters",
`Missing required parameters: ${missingParams.join(", ")}`,
{ public: true }
);
}
}

export class InvalidQueryJSON extends ActorError {
constructor(error?: unknown) {
super(
Expand All @@ -234,11 +222,11 @@ export class InvalidQueryJSON extends ActorError {
}
}

export class InvalidQueryFormat extends ActorError {
export class InvalidRequest extends ActorError {
constructor(error?: unknown) {
super(
"invalid_query_format",
`Invalid query format: ${error}`,
"invalid_request",
`Invalid request: ${error}`,
{ public: true, cause: error }
);
}
Expand Down Expand Up @@ -280,12 +268,6 @@ export class InvalidRpcRequest extends ActorError {
}
}

export class InvalidRequest extends ActorError {
constructor(message: string) {
super("invalid_request", message, { public: true });
}
}

export class InvalidParams extends ActorError {
constructor(message: string) {
super("invalid_params", message, { public: true });
Expand Down
3 changes: 2 additions & 1 deletion packages/actor-core/src/actor/instance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -720,7 +720,8 @@ export class ActorInstance<S, CP, CS, V> {
new CachedSerializer<wsToClient.ToClient>({
b: {
i: {
ci: `${conn.id}`,
ai: this.id,
ci: conn.id,
ct: conn._token,
},
},
Expand Down
5 changes: 4 additions & 1 deletion packages/actor-core/src/actor/protocol/message/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
} from "@/actor/protocol/serde";
import { deconstructError } from "@/common/utils";
import { Actions } from "@/actor/config";
import invariant from "invariant";

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

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

try {
if ("rr" in message.b) {
if ("i" in message.b) {
invariant(false, "should not be notified of init event");
} else if ("rr" in message.b) {
// RPC request

if (handler.onExecuteRpc === undefined) {
Expand Down
2 changes: 2 additions & 0 deletions packages/actor-core/src/actor/protocol/message/to-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { z } from "zod";

// Only called for SSE because we don't need this for WebSockets
export const InitSchema = z.object({
// Actor ID
ai: z.string(),
// Connection ID
ci: z.string(),
// Connection token
Expand Down
6 changes: 6 additions & 0 deletions packages/actor-core/src/actor/protocol/message/to-server.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { z } from "zod";

const InitSchema = z.object({
// Conn Params
p: z.unknown({}).optional(),
});

const RpcRequestSchema = z.object({
// ID
i: z.number().int(),
Expand All @@ -19,6 +24,7 @@ const SubscriptionRequestSchema = z.object({
export const ToServerSchema = z.object({
// Body
b: z.union([
z.object({ i: InitSchema }),
z.object({ rr: RpcRequestSchema }),
z.object({ sr: SubscriptionRequestSchema }),
]),
Expand Down
Loading
Loading