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

Commit 5a74e24

Browse files
committed
feat(engine): add engine driver
1 parent d43e771 commit 5a74e24

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+1899
-1075
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@472",
175176
"@types/invariant": "^2",
176177
"@types/node": "^22.13.1",
177178
"@types/ws": "^8",

packages/core/src/actor/driver.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,7 @@ export interface ActorDriver {
3939
*/
4040
getDatabase(actorId: string): Promise<unknown | undefined>;
4141

42-
// TODO:
43-
//destroy(): Promise<void>;
44-
//readState(): void;
42+
shutdown?(immediate: boolean): Promise<void>;
4543
}
4644

4745
export enum ConnectionReadyState {

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

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

114114
// Setup promise for the init handlers since all other behavior depends on this
115115
const {
@@ -157,7 +157,7 @@ export async function handleWebSocketConnect(
157157
try {
158158
const connId = generateConnId();
159159
const connToken = generateConnToken();
160-
const connState = await actor.prepareConn(parameters, c?.req.raw);
160+
const connState = await actor.prepareConn(parameters, req);
161161

162162
// Save socket
163163
const connGlobalState =
@@ -578,7 +578,7 @@ export async function handleConnectionMessage(
578578
}
579579

580580
export async function handleRawWebSocketHandler(
581-
c: HonoContext | undefined,
581+
req: Request | undefined,
582582
path: string,
583583
actorDriver: ActorDriver,
584584
actorId: string,
@@ -602,8 +602,8 @@ export async function handleRawWebSocketHandler(
602602
const normalizedPath = pathname + url.search;
603603

604604
let newRequest: Request;
605-
if (c) {
606-
newRequest = new Request(`http://actor${normalizedPath}`, c.req.raw);
605+
if (req) {
606+
newRequest = new Request(`http://actor${normalizedPath}`, req);
607607
} else {
608608
newRequest = new Request(`http://actor${normalizedPath}`, {
609609
method: "GET",
@@ -660,8 +660,8 @@ export function getRequestEncoding(req: HonoRequest): Encoding {
660660
return result.data;
661661
}
662662

663-
export function getRequestExposeInternalError(req: HonoRequest): boolean {
664-
const param = req.header(HEADER_EXPOSE_INTERNAL_ERROR);
663+
export function getRequestExposeInternalError(req: Request): boolean {
664+
const param = req.headers.get(HEADER_EXPOSE_INTERNAL_ERROR);
665665
if (!param) {
666666
return false;
667667
}

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,

0 commit comments

Comments
 (0)