Skip to content
This repository was archived by the owner on Oct 22, 2025. It is now read-only.

Commit c6ffb2b

Browse files
committed
feat(engine): add engine driver
1 parent 0433897 commit c6ffb2b

37 files changed

+1485
-342
lines changed

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@
77

88
*.png binary
99
*.jpg binary
10+
*.tgz binary
1011

NEW_SPEC.md

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

NEW_SPEC2.md

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

packages/core/package.json

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -158,20 +158,21 @@
158158
},
159159
"dependencies": {
160160
"@hono/standard-validator": "^0.1.3",
161-
"cbor-x": "^1.6.0",
161+
"@hono/zod-openapi": "^0.19.10",
162162
"@rivetkit/fast-json-patch": "^3.1.2",
163+
"cbor-x": "^1.6.0",
164+
"hono": "^4.7.0",
163165
"invariant": "^2.2.4",
164166
"nanoevents": "^9.1.0",
165167
"on-change": "^5.0.1",
166168
"p-retry": "^6.2.1",
167-
"zod": "^3.25.76",
168-
"@hono/zod-openapi": "^0.19.10",
169-
"hono": "^4.7.0"
169+
"zod": "^3.25.76"
170170
},
171171
"devDependencies": {
172-
"@hono/node-server": "^1.14.0",
172+
"@hono/node-server": "^1.18.2",
173173
"@hono/node-ws": "^1.1.1",
174174
"@rivet-gg/actor-core": "^25.1.0",
175+
"@rivetkit/engine-runner": "https://pkg.pr.new/rivet-gg/engine/@rivetkit/engine-runner@461",
175176
"@types/invariant": "^2",
176177
"@types/node": "^22.13.1",
177178
"@types/ws": "^8",

packages/core/src/actor/router-endpoints.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -98,15 +98,15 @@ export interface WebSocketOpts {
9898
* Creates a WebSocket connection handler
9999
*/
100100
export async function handleWebSocketConnect(
101-
c: HonoContext | undefined,
101+
req: Request | undefined,
102102
runConfig: RunConfig,
103103
actorDriver: ActorDriver,
104104
actorId: string,
105105
encoding: Encoding,
106106
parameters: unknown,
107107
authData: unknown,
108108
): Promise<UpgradeWebSocketArgs> {
109-
const exposeInternalError = c ? getRequestExposeInternalError(c.req) : false;
109+
const exposeInternalError = req ? getRequestExposeInternalError(req) : false;
110110

111111
// Setup promise for the init handlers since all other behavior depends on this
112112
const {
@@ -154,7 +154,7 @@ export async function handleWebSocketConnect(
154154
try {
155155
const connId = generateConnId();
156156
const connToken = generateConnToken();
157-
const connState = await actor.prepareConn(parameters, c?.req.raw);
157+
const connState = await actor.prepareConn(parameters, req);
158158

159159
// Save socket
160160
const connGlobalState =
@@ -575,7 +575,7 @@ export async function handleConnectionMessage(
575575
}
576576

577577
export async function handleRawWebSocketHandler(
578-
c: HonoContext | undefined,
578+
req: Request | undefined,
579579
path: string,
580580
actorDriver: ActorDriver,
581581
actorId: string,
@@ -599,8 +599,8 @@ export async function handleRawWebSocketHandler(
599599
const normalizedPath = pathname + url.search;
600600

601601
let newRequest: Request;
602-
if (c) {
603-
newRequest = new Request(`http://actor${normalizedPath}`, c.req.raw);
602+
if (req) {
603+
newRequest = new Request(`http://actor${normalizedPath}`, req);
604604
} else {
605605
newRequest = new Request(`http://actor${normalizedPath}`, {
606606
method: "GET",
@@ -657,8 +657,8 @@ export function getRequestEncoding(req: HonoRequest): Encoding {
657657
return result.data;
658658
}
659659

660-
export function getRequestExposeInternalError(req: HonoRequest): boolean {
661-
const param = req.header(HEADER_EXPOSE_INTERNAL_ERROR);
660+
export function getRequestExposeInternalError(req: Request): boolean {
661+
const param = req.headers.get(HEADER_EXPOSE_INTERNAL_ERROR);
662662
if (!param) {
663663
return false;
664664
}

packages/core/src/actor/router.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ export function createActorRouter(
9191
const authData = authDataRaw ? JSON.parse(authDataRaw) : undefined;
9292

9393
return await handleWebSocketConnect(
94-
c as HonoContext,
94+
c.req.raw,
9595
runConfig,
9696
actorDriver,
9797
c.env.actorId,
@@ -220,7 +220,7 @@ export function createActorRouter(
220220
});
221221

222222
return await handleRawWebSocketHandler(
223-
c,
223+
c.req.raw,
224224
pathWithQuery,
225225
actorDriver,
226226
c.env.actorId,

packages/core/src/client/actor-handle.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,14 +149,25 @@ export class ActorHandleRaw {
149149
"getOrCreateForKey" in this.#actorQuery
150150
) {
151151
// TODO:
152+
let name: string;
153+
if ("getForKey" in this.#actorQuery) {
154+
name = this.#actorQuery.getForKey.name;
155+
} else if ("getOrCreateForKey" in this.#actorQuery) {
156+
name = this.#actorQuery.getOrCreateForKey.name;
157+
} else {
158+
assertUnreachable(this.#actorQuery);
159+
}
160+
152161
const actorId = await this.#driver.resolveActorId(
153162
undefined,
154163
this.#actorQuery,
155164
this.#encodingKind,
156165
this.#params,
157166
signal ? { signal } : undefined,
158167
);
159-
this.#actorQuery = { getForId: { actorId } };
168+
169+
this.#actorQuery = { getForId: { actorId, name } };
170+
160171
return actorId;
161172
} else if ("getForId" in this.#actorQuery) {
162173
// SKip since it's already resolved

packages/core/src/client/client.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,8 +268,9 @@ export class ClientRaw {
268268
params: opts?.params,
269269
});
270270

271-
const actorQuery = {
271+
const actorQuery: ActorQuery = {
272272
getForId: {
273+
name,
273274
actorId,
274275
},
275276
};
@@ -400,6 +401,7 @@ export class ClientRaw {
400401
// Create handle with actor ID
401402
const getForIdQuery = {
402403
getForId: {
404+
name,
403405
actorId,
404406
},
405407
} satisfies ActorQuery;

packages/core/src/common/router.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ export function handleRouteError(
4747
c: HonoContext,
4848
) {
4949
const exposeInternalError =
50-
opts.enableExposeInternalError && getRequestExposeInternalError(c.req);
50+
opts.enableExposeInternalError && getRequestExposeInternalError(c.req.raw);
5151

5252
const { statusCode, code, message, metadata } = deconstructError(
5353
error,

packages/core/src/driver-test-suite/tests/actor-conn-state.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,10 @@ export function runActorConnStateTests(driverTestConfig: DriverTestConfig) {
9494
const conn1 = handle.connect();
9595
const conn2 = handle.connect();
9696

97+
// HACK: Wait for both connections to successfully connect by waiting for a round trip RPC
98+
await conn1.getConnectionState();
99+
await conn2.getConnectionState();
100+
97101
// Get state1 for reference
98102
const state1 = await conn1.getConnectionState();
99103

@@ -119,6 +123,10 @@ export function runActorConnStateTests(driverTestConfig: DriverTestConfig) {
119123
const conn1 = handle.connect();
120124
const conn2 = handle.connect();
121125

126+
// HACK: Wait for both connections to successfully connect by waiting for a round trip RPC
127+
await conn1.getConnectionState();
128+
await conn2.getConnectionState();
129+
122130
// Get all connection states
123131
const allStates = await conn1.getAllConnectionStates();
124132

0 commit comments

Comments
 (0)