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

Commit 489a38c

Browse files
committed
chore(core): migrate actor persist & file system to bare (#1204)
1 parent 9b53b7b commit 489a38c

File tree

13 files changed

+419
-223
lines changed

13 files changed

+419
-223
lines changed

packages/core/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@
150150
"scripts": {
151151
"dev": "pnpm build --watch",
152152
"build": "tsup src/mod.ts src/client/mod.ts src/common/log.ts src/common/websocket.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/test/mod.ts src/inspector/mod.ts",
153-
"build:schema": "node ../../packages/misc/bare-compiler/dist/cli.js compile schemas/client-protocol/v1.bare -o dist/schemas/client-protocol/v1.ts && node ../../packages/misc/bare-compiler/dist/cli.js compile schemas/file-system-driver/v1.bare -o dist/schemas/file-system-driver/v1.ts ",
153+
"build:schema": "node ../../packages/misc/bare-compiler/dist/cli.js compile schemas/client-protocol/v1.bare -o dist/schemas/client-protocol/v1.ts && node ../../packages/misc/bare-compiler/dist/cli.js compile schemas/file-system-driver/v1.bare -o dist/schemas/file-system-driver/v1.ts && node ../../packages/misc/bare-compiler/dist/cli.js compile schemas/actor-persist/v1.bare -o dist/schemas/actor-persist/v1.ts",
154154
"check-types": "tsc --noEmit",
155155
"test": "vitest run",
156156
"test:watch": "vitest",
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# MARK: Connection
2+
# Represents an event subscription.
3+
type PersistedSubscription struct {
4+
# Event name
5+
eventName: str
6+
}
7+
8+
# Represents a persisted connection to an actor.
9+
type PersistedConnection struct {
10+
# Connection ID
11+
id: str
12+
# Connection token
13+
token: str
14+
# Connection driver type
15+
driver: str
16+
# Connection driver state
17+
driverState: data
18+
# Connection parameters
19+
parameters: data
20+
# Connection state
21+
state: data
22+
# Authentication data
23+
auth: optional<data>
24+
# Active subscriptions
25+
subscriptions: list<PersistedSubscription>
26+
# Last seen timestamp
27+
lastSeen: u64
28+
}
29+
30+
# MARK: Schedule Event
31+
# Represents a generic scheduled event.
32+
type GenericPersistedScheduleEvent struct {
33+
# Action name
34+
action: str
35+
# Arguments for the action
36+
#
37+
# CBOR array
38+
args: optional<data>
39+
}
40+
41+
# Event kind union
42+
type PersistedScheduleEventKind union {
43+
GenericPersistedScheduleEvent
44+
}
45+
46+
# Scheduled event with metadata
47+
type PersistedScheduleEvent struct {
48+
# Event ID
49+
eventId: str
50+
# Timestamp when the event should fire
51+
timestamp: u64
52+
# Event kind
53+
kind: PersistedScheduleEventKind
54+
}
55+
56+
# MARK: Actor
57+
# Represents the persisted state of an actor.
58+
type PersistedActor struct {
59+
# Input data passed to the actor on initialization
60+
input: optional<data>
61+
# Whether the actor has been initialized
62+
hasInitialized: bool
63+
# Actor's state
64+
state: data
65+
# Active connections
66+
connections: list<PersistedConnection>
67+
# Scheduled events
68+
scheduledEvents: list<PersistedScheduleEvent>
69+
}

packages/core/schemas/file-system-driver/v1.bare

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,17 @@
22

33
# MARK: Actor State
44
# Represents the persisted state for an actor on disk.
5-
# Note: createdAt is not persisted; it is derived from the file's birthtime.
65
type ActorState struct {
7-
id: str
8-
name: str
9-
key: list<str>
10-
persistedData: data
6+
actorId: str
7+
name: str
8+
key: list<str>
9+
persistedData: data
10+
createdAt: u64
1111
}
1212

1313
# MARK: Actor Alarm
14-
# Represents a scheduled alarm for an actor.
15-
# Stored per-actor; the actor id is implied by the filename.
16-
# The timestamp is milliseconds since epoch.
1714
type ActorAlarm struct {
18-
timestamp: uint
15+
actorId: str
16+
timestamp: uint
1917
}
2018

packages/core/src/actor/connection.ts

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -68,15 +68,15 @@ export class Conn<S, CP, CS, V, I, AD, DB extends AnyDatabaseProvider> {
6868
#driver: ConnDriver;
6969

7070
public get params(): CP {
71-
return this.__persist.p;
71+
return this.__persist.params;
7272
}
7373

7474
public get auth(): AD {
75-
return this.__persist.a as AD;
75+
return this.__persist.authData as AD;
7676
}
7777

7878
public get driver(): ConnectionDriver {
79-
return this.__persist.d as ConnectionDriver;
79+
return this.__persist.connDriver as ConnectionDriver;
8080
}
8181

8282
public get _stateEnabled() {
@@ -90,8 +90,8 @@ export class Conn<S, CP, CS, V, I, AD, DB extends AnyDatabaseProvider> {
9090
*/
9191
public get state(): CS {
9292
this.#validateStateEnabled();
93-
if (!this.__persist.s) throw new Error("state should exists");
94-
return this.__persist.s;
93+
if (!this.__persist.state) throw new Error("state should exists");
94+
return this.__persist.state;
9595
}
9696

9797
/**
@@ -101,21 +101,21 @@ export class Conn<S, CP, CS, V, I, AD, DB extends AnyDatabaseProvider> {
101101
*/
102102
public set state(value: CS) {
103103
this.#validateStateEnabled();
104-
this.__persist.s = value;
104+
this.__persist.state = value;
105105
}
106106

107107
/**
108108
* Unique identifier for the connection.
109109
*/
110110
public get id(): ConnId {
111-
return this.__persist.i;
111+
return this.__persist.connId;
112112
}
113113

114114
/**
115115
* Token used to authenticate this request.
116116
*/
117117
public get _token(): string {
118-
return this.__persist.t;
118+
return this.__persist.token;
119119
}
120120

121121
/**
@@ -129,7 +129,7 @@ export class Conn<S, CP, CS, V, I, AD, DB extends AnyDatabaseProvider> {
129129
* Timestamp of the last time the connection was seen, i.e. the last time the connection was active and checked for liveness.
130130
*/
131131
public get lastSeen(): number {
132-
return this.__persist.l;
132+
return this.__persist.lastSeen;
133133
}
134134

135135
/**
@@ -165,7 +165,12 @@ export class Conn<S, CP, CS, V, I, AD, DB extends AnyDatabaseProvider> {
165165
* @protected
166166
*/
167167
public _sendMessage(message: CachedSerializer<protocol.ToClient>) {
168-
this.#driver.sendMessage?.(this.#actor, this, this.__persist.ds, message);
168+
this.#driver.sendMessage?.(
169+
this.#actor,
170+
this,
171+
this.__persist.connDriverState,
172+
message,
173+
);
169174
}
170175

171176
/**
@@ -205,7 +210,12 @@ export class Conn<S, CP, CS, V, I, AD, DB extends AnyDatabaseProvider> {
205210
*/
206211
public async disconnect(reason?: string) {
207212
this.#status = "reconnecting";
208-
await this.#driver.disconnect(this.#actor, this, this.__persist.ds, reason);
213+
await this.#driver.disconnect(
214+
this.#actor,
215+
this,
216+
this.__persist.connDriverState,
217+
reason,
218+
);
209219
}
210220

211221
/**
@@ -233,18 +243,18 @@ export class Conn<S, CP, CS, V, I, AD, DB extends AnyDatabaseProvider> {
233243
status: this.#status,
234244
newStatus,
235245

236-
lastSeen: this.__persist.l,
246+
lastSeen: this.__persist.lastSeen,
237247
currentTs: newLastSeen,
238248
});
239249

240250
if (!isConnectionClosed) {
241-
this.__persist.l = newLastSeen;
251+
this.__persist.lastSeen = newLastSeen;
242252
}
243253

244254
this.#status = newStatus;
245255
return {
246256
status: this.#status,
247-
lastSeen: this.__persist.l,
257+
lastSeen: this.__persist.lastSeen,
248258
};
249259
}
250260
}

0 commit comments

Comments
 (0)