rivetkit 2.0.27 → 2.0.28

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (124) hide show
  1. package/dist/schemas/file-system-driver/v3.ts +167 -0
  2. package/dist/tsup/{chunk-4GJQDTAG.cjs → chunk-3ZC6SBX6.cjs} +652 -329
  3. package/dist/tsup/chunk-3ZC6SBX6.cjs.map +1 -0
  4. package/dist/tsup/{chunk-D7A47BVR.js → chunk-7IAEY5UZ.js} +2 -2
  5. package/dist/tsup/{chunk-6F2NCX7R.js → chunk-AE7BB3M2.js} +2 -2
  6. package/dist/tsup/{chunk-XU4GGB6J.js → chunk-AHPMXTSB.js} +503 -180
  7. package/dist/tsup/chunk-AHPMXTSB.js.map +1 -0
  8. package/dist/tsup/{chunk-XNKWXMIT.cjs → chunk-CJLXW36F.cjs} +768 -292
  9. package/dist/tsup/chunk-CJLXW36F.cjs.map +1 -0
  10. package/dist/tsup/chunk-DATRTJVZ.js +891 -0
  11. package/dist/tsup/chunk-DATRTJVZ.js.map +1 -0
  12. package/dist/tsup/{chunk-C3UREFUI.js → chunk-EJXISR3H.js} +14 -5
  13. package/dist/tsup/chunk-EJXISR3H.js.map +1 -0
  14. package/dist/tsup/{chunk-GUH2PNPG.js → chunk-IDYDUETM.js} +3 -3
  15. package/dist/tsup/{chunk-2NNICHGY.js → chunk-ILK4JEMF.js} +2 -2
  16. package/dist/tsup/chunk-JALGQWHW.cjs +129 -0
  17. package/dist/tsup/chunk-JALGQWHW.cjs.map +1 -0
  18. package/dist/tsup/{chunk-BBTOBXEO.js → chunk-KXSSOVFA.js} +578 -102
  19. package/dist/tsup/chunk-KXSSOVFA.js.map +1 -0
  20. package/dist/tsup/{chunk-DRJCTDDT.cjs → chunk-M54KFQQP.cjs} +18 -9
  21. package/dist/tsup/chunk-M54KFQQP.cjs.map +1 -0
  22. package/dist/tsup/chunk-NQZ643FB.cjs +891 -0
  23. package/dist/tsup/chunk-NQZ643FB.cjs.map +1 -0
  24. package/dist/tsup/{chunk-LZADH4QA.cjs → chunk-NR2N4UA2.cjs} +10 -10
  25. package/dist/tsup/{chunk-LZADH4QA.cjs.map → chunk-NR2N4UA2.cjs.map} +1 -1
  26. package/dist/tsup/{chunk-KSQQU7NC.js → chunk-S5URQ3CI.js} +7 -6
  27. package/dist/tsup/chunk-S5URQ3CI.js.map +1 -0
  28. package/dist/tsup/{chunk-QC4AE54W.cjs → chunk-VQRYS6VW.cjs} +3 -3
  29. package/dist/tsup/{chunk-QC4AE54W.cjs.map → chunk-VQRYS6VW.cjs.map} +1 -1
  30. package/dist/tsup/{chunk-TJ2PJ5C7.cjs → chunk-Y2AKEZRY.cjs} +3 -3
  31. package/dist/tsup/{chunk-TJ2PJ5C7.cjs.map → chunk-Y2AKEZRY.cjs.map} +1 -1
  32. package/dist/tsup/{chunk-F3SQLO3X.cjs → chunk-Y5LN2XRH.cjs} +7 -6
  33. package/dist/tsup/chunk-Y5LN2XRH.cjs.map +1 -0
  34. package/dist/tsup/{chunk-G7YZSSWV.cjs → chunk-YW2E3UPH.cjs} +6 -6
  35. package/dist/tsup/{chunk-G7YZSSWV.cjs.map → chunk-YW2E3UPH.cjs.map} +1 -1
  36. package/dist/tsup/{chunk-VRZNWBDK.js → chunk-ZL3SUOIM.js} +45 -46
  37. package/dist/tsup/chunk-ZL3SUOIM.js.map +1 -0
  38. package/dist/tsup/client/mod.cjs +8 -8
  39. package/dist/tsup/client/mod.d.cts +3 -2
  40. package/dist/tsup/client/mod.d.ts +3 -2
  41. package/dist/tsup/client/mod.js +7 -7
  42. package/dist/tsup/common/log.cjs +2 -2
  43. package/dist/tsup/common/log.d.cts +9 -1
  44. package/dist/tsup/common/log.d.ts +9 -1
  45. package/dist/tsup/common/log.js +1 -1
  46. package/dist/tsup/common/websocket.cjs +3 -3
  47. package/dist/tsup/common/websocket.js +2 -2
  48. package/dist/tsup/{config-Bo-blHpJ.d.ts → config-CcMdKDv9.d.ts} +301 -899
  49. package/dist/tsup/{config-BRDYDraU.d.cts → config-DxlmiJS1.d.cts} +301 -899
  50. package/dist/tsup/driver-helpers/mod.cjs +4 -4
  51. package/dist/tsup/driver-helpers/mod.d.cts +2 -1
  52. package/dist/tsup/driver-helpers/mod.d.ts +2 -1
  53. package/dist/tsup/driver-helpers/mod.js +3 -3
  54. package/dist/tsup/driver-test-suite/mod.cjs +71 -71
  55. package/dist/tsup/driver-test-suite/mod.d.cts +2 -1
  56. package/dist/tsup/driver-test-suite/mod.d.ts +2 -1
  57. package/dist/tsup/driver-test-suite/mod.js +10 -10
  58. package/dist/tsup/inspector/mod.cjs +5 -5
  59. package/dist/tsup/inspector/mod.d.cts +5 -4
  60. package/dist/tsup/inspector/mod.d.ts +5 -4
  61. package/dist/tsup/inspector/mod.js +4 -4
  62. package/dist/tsup/mod.cjs +9 -9
  63. package/dist/tsup/mod.d.cts +3 -2
  64. package/dist/tsup/mod.d.ts +3 -2
  65. package/dist/tsup/mod.js +8 -8
  66. package/dist/tsup/test/mod.cjs +11 -10
  67. package/dist/tsup/test/mod.cjs.map +1 -1
  68. package/dist/tsup/test/mod.d.cts +2 -6
  69. package/dist/tsup/test/mod.d.ts +2 -6
  70. package/dist/tsup/test/mod.js +10 -9
  71. package/dist/tsup/utils.cjs +2 -2
  72. package/dist/tsup/utils.js +1 -1
  73. package/package.json +7 -6
  74. package/src/actor/config.ts +21 -20
  75. package/src/actor/conn/mod.ts +5 -1
  76. package/src/actor/instance/connection-manager.ts +5 -1
  77. package/src/actor/instance/event-manager.ts +5 -1
  78. package/src/actor/instance/state-manager.ts +19 -3
  79. package/src/actor/protocol/old.ts +3 -0
  80. package/src/actor/protocol/serde.ts +5 -1
  81. package/src/actor/router-endpoints.ts +2 -0
  82. package/src/client/actor-conn.ts +2 -0
  83. package/src/client/actor-handle.ts +3 -0
  84. package/src/client/config.ts +1 -1
  85. package/src/client/utils.ts +4 -1
  86. package/src/common/router.ts +5 -1
  87. package/src/driver-helpers/utils.ts +8 -2
  88. package/src/drivers/engine/config.ts +6 -3
  89. package/src/drivers/file-system/global-state.ts +46 -1
  90. package/src/drivers/file-system/manager.ts +4 -0
  91. package/src/inspector/config.ts +1 -2
  92. package/src/manager/driver.ts +4 -0
  93. package/src/manager/router-schema.ts +7 -7
  94. package/src/manager/router.ts +4 -4
  95. package/src/registry/run-config.ts +9 -5
  96. package/src/remote-manager-driver/actor-http-client.ts +1 -1
  97. package/src/remote-manager-driver/api-utils.ts +2 -0
  98. package/src/schemas/actor-persist/versioned.ts +126 -54
  99. package/src/schemas/client-protocol/versioned.ts +173 -42
  100. package/src/schemas/file-system-driver/mod.ts +1 -1
  101. package/src/schemas/file-system-driver/versioned.ts +129 -45
  102. package/src/serde.ts +9 -2
  103. package/src/test/config.ts +13 -12
  104. package/src/test/mod.ts +56 -82
  105. package/dist/tsup/chunk-2TZH6VO6.cjs +0 -514
  106. package/dist/tsup/chunk-2TZH6VO6.cjs.map +0 -1
  107. package/dist/tsup/chunk-4GJQDTAG.cjs.map +0 -1
  108. package/dist/tsup/chunk-5YDKTW6Y.js +0 -514
  109. package/dist/tsup/chunk-5YDKTW6Y.js.map +0 -1
  110. package/dist/tsup/chunk-BBTOBXEO.js.map +0 -1
  111. package/dist/tsup/chunk-C3UREFUI.js.map +0 -1
  112. package/dist/tsup/chunk-DRJCTDDT.cjs.map +0 -1
  113. package/dist/tsup/chunk-F3SQLO3X.cjs.map +0 -1
  114. package/dist/tsup/chunk-FYZLEH57.cjs +0 -130
  115. package/dist/tsup/chunk-FYZLEH57.cjs.map +0 -1
  116. package/dist/tsup/chunk-KSQQU7NC.js.map +0 -1
  117. package/dist/tsup/chunk-VRZNWBDK.js.map +0 -1
  118. package/dist/tsup/chunk-XNKWXMIT.cjs.map +0 -1
  119. package/dist/tsup/chunk-XU4GGB6J.js.map +0 -1
  120. package/src/common/versioned-data.ts +0 -95
  121. /package/dist/tsup/{chunk-D7A47BVR.js.map → chunk-7IAEY5UZ.js.map} +0 -0
  122. /package/dist/tsup/{chunk-6F2NCX7R.js.map → chunk-AE7BB3M2.js.map} +0 -0
  123. /package/dist/tsup/{chunk-GUH2PNPG.js.map → chunk-IDYDUETM.js.map} +0 -0
  124. /package/dist/tsup/{chunk-2NNICHGY.js.map → chunk-ILK4JEMF.js.map} +0 -0
@@ -1,16 +1,11 @@
1
- import {
2
- createVersionedDataHandler,
3
- type MigrationFn,
4
- } from "@/common/versioned-data";
5
- import type * as v1 from "../../../dist/schemas/client-protocol/v1";
1
+ import { createVersionedDataHandler } from "vbare";
2
+ import * as v1 from "../../../dist/schemas/client-protocol/v1";
6
3
  import * as v2 from "../../../dist/schemas/client-protocol/v2";
7
4
 
8
5
  export const CURRENT_VERSION = 2;
9
6
 
10
- const migrations = new Map<number, MigrationFn<any, any>>();
11
-
12
- // Migration from v1 to v2: Remove connectionToken from Init message
13
- migrations.set(1, (v1Data: v1.ToClient): v2.ToClient => {
7
+ // Converter from v1 to v2: Remove connectionToken from Init message
8
+ const v1ToV2 = (v1Data: v1.ToClient): v2.ToClient => {
14
9
  // Handle Init message specifically to remove connectionToken
15
10
  if (v1Data.body.tag === "Init") {
16
11
  const { actorId, connectionId } = v1Data.body.val as v1.Init;
@@ -26,58 +21,194 @@ migrations.set(1, (v1Data: v1.ToClient): v2.ToClient => {
26
21
  }
27
22
  // All other messages are unchanged
28
23
  return v1Data as unknown as v2.ToClient;
29
- });
24
+ };
25
+
26
+ // Converter from v2 to v1: Add empty connectionToken to Init message
27
+ const v2ToV1 = (v2Data: v2.ToClient): v1.ToClient => {
28
+ // Handle Init message specifically to add connectionToken
29
+ if (v2Data.body.tag === "Init") {
30
+ const { actorId, connectionId } = v2Data.body.val;
31
+ return {
32
+ body: {
33
+ tag: "Init",
34
+ val: {
35
+ actorId,
36
+ connectionId,
37
+ connectionToken: "", // Add empty connectionToken for v1 compatibility
38
+ },
39
+ },
40
+ };
41
+ }
42
+ // All other messages are unchanged
43
+ return v2Data as unknown as v1.ToClient;
44
+ };
30
45
 
31
46
  export const TO_SERVER_VERSIONED = createVersionedDataHandler<v2.ToServer>({
32
- currentVersion: CURRENT_VERSION,
33
- migrations,
34
- serializeVersion: (data) => v2.encodeToServer(data),
35
- deserializeVersion: (bytes) => v2.decodeToServer(bytes),
47
+ deserializeVersion: (bytes, version) => {
48
+ switch (version) {
49
+ case 1:
50
+ return v1.decodeToServer(bytes);
51
+ case 2:
52
+ return v2.decodeToServer(bytes);
53
+ default:
54
+ throw new Error(`Unknown version ${version}`);
55
+ }
56
+ },
57
+ serializeVersion: (data, version) => {
58
+ switch (version) {
59
+ case 1:
60
+ return v1.encodeToServer(data as v1.ToServer);
61
+ case 2:
62
+ return v2.encodeToServer(data as v2.ToServer);
63
+ default:
64
+ throw new Error(`Unknown version ${version}`);
65
+ }
66
+ },
67
+ deserializeConverters: () => [v1ToV2],
68
+ serializeConverters: () => [v2ToV1],
36
69
  });
37
70
 
38
71
  export const TO_CLIENT_VERSIONED = createVersionedDataHandler<v2.ToClient>({
39
- currentVersion: CURRENT_VERSION,
40
- migrations,
41
- serializeVersion: (data) => v2.encodeToClient(data),
42
- deserializeVersion: (bytes) => v2.decodeToClient(bytes),
72
+ deserializeVersion: (bytes, version) => {
73
+ switch (version) {
74
+ case 1:
75
+ return v1.decodeToClient(bytes);
76
+ case 2:
77
+ return v2.decodeToClient(bytes);
78
+ default:
79
+ throw new Error(`Unknown version ${version}`);
80
+ }
81
+ },
82
+ serializeVersion: (data, version) => {
83
+ switch (version) {
84
+ case 1:
85
+ return v1.encodeToClient(data as v1.ToClient);
86
+ case 2:
87
+ return v2.encodeToClient(data as v2.ToClient);
88
+ default:
89
+ throw new Error(`Unknown version ${version}`);
90
+ }
91
+ },
92
+ deserializeConverters: () => [v1ToV2],
93
+ serializeConverters: () => [v2ToV1],
43
94
  });
44
95
 
45
96
  export const HTTP_ACTION_REQUEST_VERSIONED =
46
97
  createVersionedDataHandler<v2.HttpActionRequest>({
47
- currentVersion: CURRENT_VERSION,
48
- migrations,
49
- serializeVersion: (data) => v2.encodeHttpActionRequest(data),
50
- deserializeVersion: (bytes) => v2.decodeHttpActionRequest(bytes),
98
+ deserializeVersion: (bytes, version) => {
99
+ switch (version) {
100
+ case 1:
101
+ return v1.decodeHttpActionRequest(bytes);
102
+ case 2:
103
+ return v2.decodeHttpActionRequest(bytes);
104
+ default:
105
+ throw new Error(`Unknown version ${version}`);
106
+ }
107
+ },
108
+ serializeVersion: (data, version) => {
109
+ switch (version) {
110
+ case 1:
111
+ return v1.encodeHttpActionRequest(
112
+ data as v1.HttpActionRequest,
113
+ );
114
+ case 2:
115
+ return v2.encodeHttpActionRequest(
116
+ data as v2.HttpActionRequest,
117
+ );
118
+ default:
119
+ throw new Error(`Unknown version ${version}`);
120
+ }
121
+ },
122
+ deserializeConverters: () => [],
123
+ serializeConverters: () => [],
51
124
  });
52
125
 
53
126
  export const HTTP_ACTION_RESPONSE_VERSIONED =
54
127
  createVersionedDataHandler<v2.HttpActionResponse>({
55
- currentVersion: CURRENT_VERSION,
56
- migrations,
57
- serializeVersion: (data) => v2.encodeHttpActionResponse(data),
58
- deserializeVersion: (bytes) => v2.decodeHttpActionResponse(bytes),
128
+ deserializeVersion: (bytes, version) => {
129
+ switch (version) {
130
+ case 1:
131
+ return v1.decodeHttpActionResponse(bytes);
132
+ case 2:
133
+ return v2.decodeHttpActionResponse(bytes);
134
+ default:
135
+ throw new Error(`Unknown version ${version}`);
136
+ }
137
+ },
138
+ serializeVersion: (data, version) => {
139
+ switch (version) {
140
+ case 1:
141
+ return v1.encodeHttpActionResponse(
142
+ data as v1.HttpActionResponse,
143
+ );
144
+ case 2:
145
+ return v2.encodeHttpActionResponse(
146
+ data as v2.HttpActionResponse,
147
+ );
148
+ default:
149
+ throw new Error(`Unknown version ${version}`);
150
+ }
151
+ },
152
+ deserializeConverters: () => [],
153
+ serializeConverters: () => [],
59
154
  });
60
155
 
61
156
  export const HTTP_RESPONSE_ERROR_VERSIONED =
62
157
  createVersionedDataHandler<v2.HttpResponseError>({
63
- currentVersion: CURRENT_VERSION,
64
- migrations,
65
- serializeVersion: (data) => v2.encodeHttpResponseError(data),
66
- deserializeVersion: (bytes) => v2.decodeHttpResponseError(bytes),
67
- });
68
-
69
- export const HTTP_RESOLVE_REQUEST_VERSIONED =
70
- createVersionedDataHandler<v2.HttpResolveRequest>({
71
- currentVersion: CURRENT_VERSION,
72
- migrations,
73
- serializeVersion: (_) => new Uint8Array(),
74
- deserializeVersion: (bytes) => null,
158
+ deserializeVersion: (bytes, version) => {
159
+ switch (version) {
160
+ case 1:
161
+ return v1.decodeHttpResponseError(bytes);
162
+ case 2:
163
+ return v2.decodeHttpResponseError(bytes);
164
+ default:
165
+ throw new Error(`Unknown version ${version}`);
166
+ }
167
+ },
168
+ serializeVersion: (data, version) => {
169
+ switch (version) {
170
+ case 1:
171
+ return v1.encodeHttpResponseError(
172
+ data as v1.HttpResponseError,
173
+ );
174
+ case 2:
175
+ return v2.encodeHttpResponseError(
176
+ data as v2.HttpResponseError,
177
+ );
178
+ default:
179
+ throw new Error(`Unknown version ${version}`);
180
+ }
181
+ },
182
+ deserializeConverters: () => [],
183
+ serializeConverters: () => [],
75
184
  });
76
185
 
77
186
  export const HTTP_RESOLVE_RESPONSE_VERSIONED =
78
187
  createVersionedDataHandler<v2.HttpResolveResponse>({
79
- currentVersion: CURRENT_VERSION,
80
- migrations,
81
- serializeVersion: (data) => v2.encodeHttpResolveResponse(data),
82
- deserializeVersion: (bytes) => v2.decodeHttpResolveResponse(bytes),
188
+ deserializeVersion: (bytes, version) => {
189
+ switch (version) {
190
+ case 1:
191
+ return v1.decodeHttpResolveResponse(bytes);
192
+ case 2:
193
+ return v2.decodeHttpResolveResponse(bytes);
194
+ default:
195
+ throw new Error(`Unknown version ${version}`);
196
+ }
197
+ },
198
+ serializeVersion: (data, version) => {
199
+ switch (version) {
200
+ case 1:
201
+ return v1.encodeHttpResolveResponse(
202
+ data as v1.HttpResolveResponse,
203
+ );
204
+ case 2:
205
+ return v2.encodeHttpResolveResponse(
206
+ data as v2.HttpResolveResponse,
207
+ );
208
+ default:
209
+ throw new Error(`Unknown version ${version}`);
210
+ }
211
+ },
212
+ deserializeConverters: () => [],
213
+ serializeConverters: () => [],
83
214
  });
@@ -1 +1 @@
1
- export * from "../../../dist/schemas/file-system-driver/v2";
1
+ export * from "../../../dist/schemas/file-system-driver/v3";
@@ -1,51 +1,135 @@
1
- import {
2
- createVersionedDataHandler,
3
- type MigrationFn,
4
- } from "@/common/versioned-data";
1
+ import { createVersionedDataHandler } from "vbare";
5
2
  import { bufferToArrayBuffer } from "@/utils";
6
- import type * as v1 from "../../../dist/schemas/file-system-driver/v1";
3
+ import * as v1 from "../../../dist/schemas/file-system-driver/v1";
7
4
  import * as v2 from "../../../dist/schemas/file-system-driver/v2";
5
+ import * as v3 from "../../../dist/schemas/file-system-driver/v3";
8
6
 
9
- export const CURRENT_VERSION = 2;
10
-
11
- const migrations = new Map<number, MigrationFn<any, any>>([
12
- [
13
- 2,
14
- (v1State: v1.ActorState): v2.ActorState => {
15
- // Create a new kvStorage list with the legacy persist data
16
- const kvStorage: v2.ActorKvEntry[] = [];
17
-
18
- // Store the legacy persist data under key [1]
19
- if (v1State.persistedData) {
20
- // Key [1] as Uint8Array
21
- const key = new Uint8Array([1]);
22
- kvStorage.push({
23
- key: bufferToArrayBuffer(key),
24
- value: v1State.persistedData,
25
- });
26
- }
27
-
28
- return {
29
- actorId: v1State.actorId,
30
- name: v1State.name,
31
- key: v1State.key,
32
- kvStorage,
33
- createdAt: v1State.createdAt,
34
- };
35
- },
36
- ],
37
- ]);
38
-
39
- export const ACTOR_STATE_VERSIONED = createVersionedDataHandler<v2.ActorState>({
40
- currentVersion: CURRENT_VERSION,
41
- migrations,
42
- serializeVersion: (data) => v2.encodeActorState(data),
43
- deserializeVersion: (bytes) => v2.decodeActorState(bytes),
7
+ export const CURRENT_VERSION = 3;
8
+
9
+ // Converter from v1 to v2
10
+ const v1ToV2 = (v1State: v1.ActorState): v2.ActorState => {
11
+ // Create a new kvStorage list with the legacy persist data
12
+ const kvStorage: v2.ActorKvEntry[] = [];
13
+
14
+ // Store the legacy persist data under key [1]
15
+ if (v1State.persistedData) {
16
+ // Key [1] as Uint8Array
17
+ const key = new Uint8Array([1]);
18
+ kvStorage.push({
19
+ key: bufferToArrayBuffer(key),
20
+ value: v1State.persistedData,
21
+ });
22
+ }
23
+
24
+ return {
25
+ actorId: v1State.actorId,
26
+ name: v1State.name,
27
+ key: v1State.key,
28
+ kvStorage,
29
+ createdAt: v1State.createdAt,
30
+ };
31
+ };
32
+
33
+ // Converter from v2 to v3
34
+ const v2ToV3 = (v2State: v2.ActorState): v3.ActorState => {
35
+ // Migrate from v2 to v3 by adding the new optional timestamp fields
36
+ return {
37
+ actorId: v2State.actorId,
38
+ name: v2State.name,
39
+ key: v2State.key,
40
+ kvStorage: v2State.kvStorage,
41
+ createdAt: v2State.createdAt,
42
+ startTs: null,
43
+ connectableTs: null,
44
+ sleepTs: null,
45
+ destroyTs: null,
46
+ };
47
+ };
48
+
49
+ // Converter from v3 to v2
50
+ const v3ToV2 = (v3State: v3.ActorState): v2.ActorState => {
51
+ // Downgrade from v3 to v2 by removing the timestamp fields
52
+ return {
53
+ actorId: v3State.actorId,
54
+ name: v3State.name,
55
+ key: v3State.key,
56
+ kvStorage: v3State.kvStorage,
57
+ createdAt: v3State.createdAt,
58
+ };
59
+ };
60
+
61
+ // Converter from v2 to v1
62
+ const v2ToV1 = (v2State: v2.ActorState): v1.ActorState => {
63
+ // Downgrade from v2 to v1 by converting kvStorage back to persistedData
64
+ // Find the persist data entry (key [1])
65
+ const persistDataEntry = v2State.kvStorage.find((entry) => {
66
+ const key = new Uint8Array(entry.key);
67
+ return key.length === 1 && key[0] === 1;
68
+ });
69
+
70
+ return {
71
+ actorId: v2State.actorId,
72
+ name: v2State.name,
73
+ key: v2State.key,
74
+ persistedData: persistDataEntry?.value || new ArrayBuffer(0),
75
+ createdAt: v2State.createdAt,
76
+ };
77
+ };
78
+
79
+ export const ACTOR_STATE_VERSIONED = createVersionedDataHandler<v3.ActorState>({
80
+ deserializeVersion: (bytes, version) => {
81
+ switch (version) {
82
+ case 1:
83
+ return v1.decodeActorState(bytes);
84
+ case 2:
85
+ return v2.decodeActorState(bytes);
86
+ case 3:
87
+ return v3.decodeActorState(bytes);
88
+ default:
89
+ throw new Error(`Unknown version ${version}`);
90
+ }
91
+ },
92
+ serializeVersion: (data, version) => {
93
+ switch (version) {
94
+ case 1:
95
+ return v1.encodeActorState(data as v1.ActorState);
96
+ case 2:
97
+ return v2.encodeActorState(data as v2.ActorState);
98
+ case 3:
99
+ return v3.encodeActorState(data as v3.ActorState);
100
+ default:
101
+ throw new Error(`Unknown version ${version}`);
102
+ }
103
+ },
104
+ deserializeConverters: () => [v1ToV2, v2ToV3],
105
+ serializeConverters: () => [v3ToV2, v2ToV1],
44
106
  });
45
107
 
46
- export const ACTOR_ALARM_VERSIONED = createVersionedDataHandler<v2.ActorAlarm>({
47
- currentVersion: CURRENT_VERSION,
48
- migrations,
49
- serializeVersion: (data) => v2.encodeActorAlarm(data),
50
- deserializeVersion: (bytes) => v2.decodeActorAlarm(bytes),
108
+ export const ACTOR_ALARM_VERSIONED = createVersionedDataHandler<v3.ActorAlarm>({
109
+ deserializeVersion: (bytes, version) => {
110
+ switch (version) {
111
+ case 1:
112
+ return v1.decodeActorAlarm(bytes);
113
+ case 2:
114
+ return v2.decodeActorAlarm(bytes);
115
+ case 3:
116
+ return v3.decodeActorAlarm(bytes);
117
+ default:
118
+ throw new Error(`Unknown version ${version}`);
119
+ }
120
+ },
121
+ serializeVersion: (data, version) => {
122
+ switch (version) {
123
+ case 1:
124
+ return v1.encodeActorAlarm(data as v1.ActorAlarm);
125
+ case 2:
126
+ return v2.encodeActorAlarm(data as v2.ActorAlarm);
127
+ case 3:
128
+ return v3.encodeActorAlarm(data as v3.ActorAlarm);
129
+ default:
130
+ throw new Error(`Unknown version ${version}`);
131
+ }
132
+ },
133
+ deserializeConverters: () => [],
134
+ serializeConverters: () => [],
51
135
  });
package/src/serde.ts CHANGED
@@ -2,7 +2,7 @@ import * as cbor from "cbor-x";
2
2
  import invariant from "invariant";
3
3
  import type { z } from "zod";
4
4
  import { assertUnreachable } from "@/common/utils";
5
- import type { VersionedDataHandler } from "@/common/versioned-data";
5
+ import type { VersionedDataHandler } from "vbare";
6
6
  import type { Encoding } from "@/mod";
7
7
  import { jsonParseCompat, jsonStringifyCompat } from "./actor/protocol/serde";
8
8
 
@@ -57,6 +57,7 @@ export function serializeWithEncoding<TBare, TJson, T = TBare>(
57
57
  encoding: Encoding,
58
58
  value: T,
59
59
  versionedDataHandler: VersionedDataHandler<TBare> | undefined,
60
+ version: number | undefined,
60
61
  zodSchema: z.ZodType<TJson>,
61
62
  toJson: (value: T) => TJson,
62
63
  toBare: (value: T) => TBare,
@@ -75,8 +76,14 @@ export function serializeWithEncoding<TBare, TJson, T = TBare>(
75
76
  "VersionedDataHandler is required for 'bare' encoding",
76
77
  );
77
78
  }
79
+ if (version === undefined) {
80
+ throw new Error("version is required for 'bare' encoding");
81
+ }
78
82
  const bareValue = toBare(value);
79
- return versionedDataHandler.serializeWithEmbeddedVersion(bareValue);
83
+ return versionedDataHandler.serializeWithEmbeddedVersion(
84
+ bareValue,
85
+ version,
86
+ );
80
87
  } else {
81
88
  assertUnreachable(encoding);
82
89
  }
@@ -1,16 +1,17 @@
1
1
  import { z } from "zod";
2
2
  import { RunnerConfigSchema } from "@/registry/run-config";
3
3
 
4
- export const ConfigSchema = RunnerConfigSchema.removeDefault()
5
- .extend({
6
- hostname: z
7
- .string()
8
- .optional()
9
- .default(process.env.HOSTNAME ?? "127.0.0.1"),
10
- port: z
11
- .number()
12
- .optional()
13
- .default(Number.parseInt(process.env.PORT ?? "8080")),
14
- })
15
- .default({});
4
+ const ConfigSchemaBase = RunnerConfigSchema.removeDefault().extend({
5
+ hostname: z
6
+ .string()
7
+ .optional()
8
+ .default(process.env.HOSTNAME ?? "127.0.0.1"),
9
+ port: z
10
+ .number()
11
+ .optional()
12
+ .default(Number.parseInt(process.env.PORT ?? "8080")),
13
+ });
14
+ export const ConfigSchema = ConfigSchemaBase.default(() =>
15
+ ConfigSchemaBase.parse({}),
16
+ );
16
17
  export type InputConfig = z.input<typeof ConfigSchema>;
package/src/test/mod.ts CHANGED
@@ -1,122 +1,96 @@
1
1
  import { createServer } from "node:net";
2
- import { serve as honoServe, type ServerType } from "@hono/node-server";
2
+ import { serve as honoServe } from "@hono/node-server";
3
3
  import { createNodeWebSocket } from "@hono/node-ws";
4
+ import invariant from "invariant";
4
5
  import { type TestContext, vi } from "vitest";
5
- import { ClientConfigSchema } from "@/client/config";
6
6
  import { type Client, createClient } from "@/client/mod";
7
- import { chooseDefaultDriver } from "@/drivers/default";
8
7
  import { createFileSystemOrMemoryDriver } from "@/drivers/file-system/mod";
9
- import {
10
- configureInspectorAccessToken,
11
- getInspectorUrl,
12
- } from "@/inspector/utils";
8
+ import { configureInspectorAccessToken } from "@/inspector/utils";
13
9
  import { createManagerRouter } from "@/manager/router";
14
- import { createClientWithDriver } from "@/mod";
15
- import type { Registry } from "@/registry/mod";
10
+ import { createClientWithDriver, type Registry, type RunConfig } from "@/mod";
11
+ import { ClientConfigSchema } from "@/client/config";
16
12
  import { RunnerConfigSchema } from "@/registry/run-config";
17
- import { ConfigSchema, type InputConfig } from "./config";
18
13
  import { logger } from "./log";
19
14
 
20
- async function serve(
21
- registry: Registry<any>,
22
- inputConfig?: InputConfig,
23
- ): Promise<ServerType> {
24
- // Configure default configuration
25
- inputConfig ??= {};
15
+ export interface SetupTestResult<A extends Registry<any>> {
16
+ client: Client<A>;
17
+ }
18
+
19
+ // Must use `TestContext` since global hooks do not work when running concurrently
20
+ export async function setupTest<A extends Registry<any>>(
21
+ c: TestContext,
22
+ registry: A,
23
+ ): Promise<SetupTestResult<A>> {
24
+ // Force enable test mode
25
+ registry.config.test.enabled = true;
26
26
 
27
- const config = ConfigSchema.parse(inputConfig);
27
+ // Create driver
28
+ const driver = await createFileSystemOrMemoryDriver(
29
+ true,
30
+ `/tmp/rivetkit-test-${crypto.randomUUID()}`,
31
+ );
28
32
 
33
+ // Build driver config
34
+ // biome-ignore lint/style/useConst: Assigned later
29
35
  let upgradeWebSocket: any;
30
- if (!config.getUpgradeWebSocket) {
31
- config.getUpgradeWebSocket = () => upgradeWebSocket!;
32
- }
36
+ const config: RunConfig = RunnerConfigSchema.parse({
37
+ driver,
38
+ getUpgradeWebSocket: () => upgradeWebSocket!,
39
+ inspector: {
40
+ enabled: true,
41
+ token: () => "token",
42
+ },
43
+ });
33
44
 
34
45
  // Create router
35
- const runConfig = RunnerConfigSchema.parse(inputConfig);
36
- const driver =
37
- inputConfig.driver ?? (await createFileSystemOrMemoryDriver(false));
38
46
  const managerDriver = driver.manager(registry.config, config);
39
- const client = createClientWithDriver(
47
+ const internalClient = createClientWithDriver(
40
48
  managerDriver,
41
49
  ClientConfigSchema.parse({}),
42
50
  );
43
51
  configureInspectorAccessToken(config, managerDriver);
44
52
  const { router } = createManagerRouter(
45
53
  registry.config,
46
- runConfig,
54
+ config,
47
55
  managerDriver,
48
56
  driver,
49
- client,
57
+ internalClient,
50
58
  );
51
59
 
52
60
  // Inject WebSocket
53
61
  const nodeWebSocket = createNodeWebSocket({ app: router });
54
62
  upgradeWebSocket = nodeWebSocket.upgradeWebSocket;
55
63
 
64
+ // Start server
65
+ const port = await getPort();
56
66
  const server = honoServe({
57
67
  fetch: router.fetch,
58
- hostname: config.hostname,
59
- port: config.port,
68
+ hostname: "127.0.0.1",
69
+ port,
60
70
  });
71
+ invariant(
72
+ nodeWebSocket.injectWebSocket !== undefined,
73
+ "should have injectWebSocket",
74
+ );
61
75
  nodeWebSocket.injectWebSocket(server);
76
+ const endpoint = `http://127.0.0.1:${port}`;
62
77
 
63
- logger().info({
64
- msg: "rivetkit started",
65
- hostname: config.hostname,
66
- port: config.port,
67
- definitions: Object.keys(registry.config.use).length,
68
- });
78
+ logger().info({ msg: "test server listening", port });
69
79
 
70
- return server;
71
- }
72
-
73
- export interface SetupTestResult<A extends Registry<any>> {
74
- client: Client<A>;
75
- mockDriver: {
76
- actorDriver: {
77
- setCreateVarsContext: (ctx: any) => void;
78
- };
79
- };
80
- }
81
-
82
- // Must use `TestContext` since global hooks do not work when running concurrently
83
- export async function setupTest<A extends Registry<any>>(
84
- c: TestContext,
85
- registry: A,
86
- ): Promise<SetupTestResult<A>> {
87
- vi.useFakeTimers();
88
-
89
- // Set up mock driver for testing createVars context
90
- const mockDriverContext: any = {};
91
- const setDriverContextFn = (ctx: any) => {
92
- mockDriverContext.current = ctx;
93
- };
94
-
95
- // We don't need to modify the driver context anymore since we're testing with the actual context
80
+ // Cleanup on test finish
81
+ c.onTestFinished(async () => {
82
+ await new Promise((resolve) => server.close(() => resolve(undefined)));
83
+ });
96
84
 
97
- // Start server with a random port
98
- const port = await getPort();
99
- const server = await serve(registry, { port });
100
- c.onTestFinished(
101
- async () =>
102
- await new Promise((resolve) => server.close(() => resolve())),
103
- );
85
+ // Create client
86
+ const client = createClient<A>({
87
+ endpoint,
88
+ namespace: "default",
89
+ runnerName: "default",
90
+ });
91
+ c.onTestFinished(async () => await client.dispose());
104
92
 
105
- throw "TODO: Fix engine port";
106
-
107
- // // TODO: Figure out how to make this the correct endpoint
108
- // // Create client
109
- // const client = createClient<A>(`http://127.0.0.1:${port}`);
110
- // c.onTestFinished(async () => await client.dispose());
111
- //
112
- // return {
113
- // client,
114
- // mockDriver: {
115
- // actorDriver: {
116
- // setCreateVarsContext: setDriverContextFn,
117
- // },
118
- // },
119
- // };
93
+ return { client };
120
94
  }
121
95
 
122
96
  export async function getPort(): Promise<number> {