rivetkit 2.0.26 → 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 (114) hide show
  1. package/dist/schemas/file-system-driver/v3.ts +167 -0
  2. package/dist/tsup/{chunk-SWFPKHMT.cjs → chunk-3ZC6SBX6.cjs} +628 -306
  3. package/dist/tsup/chunk-3ZC6SBX6.cjs.map +1 -0
  4. package/dist/tsup/{chunk-AYDCXNX6.js → chunk-7IAEY5UZ.js} +2 -2
  5. package/dist/tsup/{chunk-GMQ44WPZ.js → chunk-AE7BB3M2.js} +2 -2
  6. package/dist/tsup/{chunk-WIFZEE5R.js → chunk-AHPMXTSB.js} +479 -157
  7. package/dist/tsup/chunk-AHPMXTSB.js.map +1 -0
  8. package/dist/tsup/{chunk-NEM3XQSZ.cjs → chunk-CJLXW36F.cjs} +758 -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-ZX3AUO3B.js → chunk-EJXISR3H.js} +14 -5
  13. package/dist/tsup/chunk-EJXISR3H.js.map +1 -0
  14. package/dist/tsup/{chunk-E6MTJS4R.js → chunk-IDYDUETM.js} +3 -3
  15. package/dist/tsup/{chunk-EX6XY7A4.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-K5JMIH4N.js → chunk-KXSSOVFA.js} +566 -100
  19. package/dist/tsup/chunk-KXSSOVFA.js.map +1 -0
  20. package/dist/tsup/{chunk-ZIAFEFUK.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-N6VIFATJ.cjs → chunk-NR2N4UA2.cjs} +10 -10
  25. package/dist/tsup/{chunk-N6VIFATJ.cjs.map → chunk-NR2N4UA2.cjs.map} +1 -1
  26. package/dist/tsup/{chunk-3ZTRZIA5.js → chunk-S5URQ3CI.js} +5 -4
  27. package/dist/tsup/chunk-S5URQ3CI.js.map +1 -0
  28. package/dist/tsup/{chunk-X4OVVIPR.cjs → chunk-VQRYS6VW.cjs} +3 -3
  29. package/dist/tsup/{chunk-X4OVVIPR.cjs.map → chunk-VQRYS6VW.cjs.map} +1 -1
  30. package/dist/tsup/{chunk-WJKKE5CN.cjs → chunk-Y2AKEZRY.cjs} +3 -3
  31. package/dist/tsup/{chunk-WJKKE5CN.cjs.map → chunk-Y2AKEZRY.cjs.map} +1 -1
  32. package/dist/tsup/{chunk-77UIPJTD.cjs → chunk-Y5LN2XRH.cjs} +5 -4
  33. package/dist/tsup/chunk-Y5LN2XRH.cjs.map +1 -0
  34. package/dist/tsup/{chunk-UORP3ABY.cjs → chunk-YW2E3UPH.cjs} +6 -6
  35. package/dist/tsup/{chunk-UORP3ABY.cjs.map → chunk-YW2E3UPH.cjs.map} +1 -1
  36. package/dist/tsup/{chunk-6ZAGP4Y3.js → chunk-ZL3SUOIM.js} +45 -49
  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.js +1 -1
  44. package/dist/tsup/common/websocket.cjs +3 -3
  45. package/dist/tsup/common/websocket.js +2 -2
  46. package/dist/tsup/{config-B7x9hjVu.d.ts → config-CcMdKDv9.d.ts} +6 -19
  47. package/dist/tsup/{config-DdCBN35E.d.cts → config-DxlmiJS1.d.cts} +6 -19
  48. package/dist/tsup/driver-helpers/mod.cjs +4 -4
  49. package/dist/tsup/driver-helpers/mod.d.cts +2 -1
  50. package/dist/tsup/driver-helpers/mod.d.ts +2 -1
  51. package/dist/tsup/driver-helpers/mod.js +3 -3
  52. package/dist/tsup/driver-test-suite/mod.cjs +71 -71
  53. package/dist/tsup/driver-test-suite/mod.d.cts +2 -1
  54. package/dist/tsup/driver-test-suite/mod.d.ts +2 -1
  55. package/dist/tsup/driver-test-suite/mod.js +10 -10
  56. package/dist/tsup/inspector/mod.cjs +5 -5
  57. package/dist/tsup/inspector/mod.d.cts +3 -2
  58. package/dist/tsup/inspector/mod.d.ts +3 -2
  59. package/dist/tsup/inspector/mod.js +4 -4
  60. package/dist/tsup/mod.cjs +9 -9
  61. package/dist/tsup/mod.d.cts +3 -2
  62. package/dist/tsup/mod.d.ts +3 -2
  63. package/dist/tsup/mod.js +8 -8
  64. package/dist/tsup/test/mod.cjs +11 -10
  65. package/dist/tsup/test/mod.cjs.map +1 -1
  66. package/dist/tsup/test/mod.d.cts +2 -6
  67. package/dist/tsup/test/mod.d.ts +2 -6
  68. package/dist/tsup/test/mod.js +10 -9
  69. package/dist/tsup/utils.cjs +2 -2
  70. package/dist/tsup/utils.js +1 -1
  71. package/package.json +4 -3
  72. package/src/actor/conn/mod.ts +5 -1
  73. package/src/actor/instance/connection-manager.ts +5 -1
  74. package/src/actor/instance/event-manager.ts +5 -1
  75. package/src/actor/instance/state-manager.ts +7 -1
  76. package/src/actor/protocol/old.ts +3 -0
  77. package/src/actor/protocol/serde.ts +5 -1
  78. package/src/actor/router-endpoints.ts +2 -0
  79. package/src/client/actor-conn.ts +2 -0
  80. package/src/client/actor-handle.ts +3 -0
  81. package/src/client/utils.ts +4 -1
  82. package/src/common/router.ts +5 -1
  83. package/src/driver-helpers/utils.ts +8 -2
  84. package/src/drivers/file-system/global-state.ts +46 -1
  85. package/src/drivers/file-system/manager.ts +4 -0
  86. package/src/manager/driver.ts +4 -0
  87. package/src/manager/router.ts +4 -4
  88. package/src/remote-manager-driver/api-utils.ts +2 -0
  89. package/src/schemas/actor-persist/versioned.ts +126 -54
  90. package/src/schemas/client-protocol/versioned.ts +173 -42
  91. package/src/schemas/file-system-driver/mod.ts +1 -1
  92. package/src/schemas/file-system-driver/versioned.ts +129 -45
  93. package/src/serde.ts +9 -2
  94. package/src/test/mod.ts +56 -82
  95. package/dist/tsup/chunk-3ZTRZIA5.js.map +0 -1
  96. package/dist/tsup/chunk-6ZAGP4Y3.js.map +0 -1
  97. package/dist/tsup/chunk-77UIPJTD.cjs.map +0 -1
  98. package/dist/tsup/chunk-CFMLY3P6.cjs +0 -521
  99. package/dist/tsup/chunk-CFMLY3P6.cjs.map +0 -1
  100. package/dist/tsup/chunk-K5JMIH4N.js.map +0 -1
  101. package/dist/tsup/chunk-NEM3XQSZ.cjs.map +0 -1
  102. package/dist/tsup/chunk-NFQAYLE5.js +0 -521
  103. package/dist/tsup/chunk-NFQAYLE5.js.map +0 -1
  104. package/dist/tsup/chunk-SWFPKHMT.cjs.map +0 -1
  105. package/dist/tsup/chunk-WIFZEE5R.js.map +0 -1
  106. package/dist/tsup/chunk-Z2YB6STQ.cjs +0 -133
  107. package/dist/tsup/chunk-Z2YB6STQ.cjs.map +0 -1
  108. package/dist/tsup/chunk-ZIAFEFUK.cjs.map +0 -1
  109. package/dist/tsup/chunk-ZX3AUO3B.js.map +0 -1
  110. package/src/common/versioned-data.ts +0 -95
  111. /package/dist/tsup/{chunk-AYDCXNX6.js.map → chunk-7IAEY5UZ.js.map} +0 -0
  112. /package/dist/tsup/{chunk-GMQ44WPZ.js.map → chunk-AE7BB3M2.js.map} +0 -0
  113. /package/dist/tsup/{chunk-E6MTJS4R.js.map → chunk-IDYDUETM.js.map} +0 -0
  114. /package/dist/tsup/{chunk-EX6XY7A4.js.map → chunk-ILK4JEMF.js.map} +0 -0
@@ -8,7 +8,10 @@ import {
8
8
  import { buildActorNames, type RegistryConfig } from "@/registry/config";
9
9
  import type { RunnerConfig } from "@/registry/run-config";
10
10
  import type * as protocol from "@/schemas/client-protocol/mod";
11
- import { HTTP_RESPONSE_ERROR_VERSIONED } from "@/schemas/client-protocol/versioned";
11
+ import {
12
+ CURRENT_VERSION as CLIENT_PROTOCOL_CURRENT_VERSION,
13
+ HTTP_RESPONSE_ERROR_VERSIONED,
14
+ } from "@/schemas/client-protocol/versioned";
12
15
  import {
13
16
  type HttpResponseError as HttpResponseErrorJson,
14
17
  HttpResponseErrorSchema,
@@ -76,6 +79,7 @@ export function handleRouteError(error: unknown, c: HonoContext) {
76
79
  encoding,
77
80
  errorData,
78
81
  HTTP_RESPONSE_ERROR_VERSIONED,
82
+ CLIENT_PROTOCOL_CURRENT_VERSION,
79
83
  HttpResponseErrorSchema,
80
84
  // JSON: metadata is the raw value (will be serialized by jsonStringifyCompat)
81
85
  (value): HttpResponseErrorJson => ({
@@ -1,7 +1,10 @@
1
1
  import * as cbor from "cbor-x";
2
2
  import { KEYS } from "@/actor/instance/kv";
3
3
  import type * as persistSchema from "@/schemas/actor-persist/mod";
4
- import { ACTOR_VERSIONED } from "@/schemas/actor-persist/versioned";
4
+ import {
5
+ ACTOR_VERSIONED,
6
+ CURRENT_VERSION,
7
+ } from "@/schemas/actor-persist/versioned";
5
8
  import { bufferToArrayBuffer } from "@/utils";
6
9
  import type { ActorDriver } from "./mod";
7
10
 
@@ -15,7 +18,10 @@ function serializeEmptyPersistData(input: unknown | undefined): Uint8Array {
15
18
  state: bufferToArrayBuffer(cbor.encode(undefined)),
16
19
  scheduledEvents: [],
17
20
  };
18
- return ACTOR_VERSIONED.serializeWithEmbeddedVersion(persistData);
21
+ return ACTOR_VERSIONED.serializeWithEmbeddedVersion(
22
+ persistData,
23
+ CURRENT_VERSION,
24
+ );
19
25
  }
20
26
 
21
27
  /**
@@ -12,6 +12,7 @@ import type * as schema from "@/schemas/file-system-driver/mod";
12
12
  import {
13
13
  ACTOR_ALARM_VERSIONED,
14
14
  ACTOR_STATE_VERSIONED,
15
+ CURRENT_VERSION as FILE_SYSTEM_DRIVER_CURRENT_VERSION,
15
16
  } from "@/schemas/file-system-driver/versioned";
16
17
  import {
17
18
  arrayBuffersEqual,
@@ -263,6 +264,10 @@ export class FileSystemGlobalState {
263
264
  key,
264
265
  createdAt: BigInt(Date.now()),
265
266
  kvStorage,
267
+ startTs: null,
268
+ connectableTs: null,
269
+ sleepTs: null,
270
+ destroyTs: null,
266
271
  };
267
272
  entry.lifecycleState = ActorLifecycleState.AWAKE;
268
273
 
@@ -369,6 +374,10 @@ export class FileSystemGlobalState {
369
374
  key: key as readonly string[],
370
375
  createdAt: BigInt(Date.now()),
371
376
  kvStorage,
377
+ startTs: null,
378
+ connectableTs: null,
379
+ sleepTs: null,
380
+ destroyTs: null,
372
381
  };
373
382
  await this.writeActor(actorId, entry.generation, entry.state);
374
383
  }
@@ -396,6 +405,15 @@ export class FileSystemGlobalState {
396
405
  if (actor.startPromise?.promise)
397
406
  await actor.startPromise.promise.catch();
398
407
 
408
+ // Update state with sleep timestamp
409
+ if (actor.state) {
410
+ actor.state = {
411
+ ...actor.state,
412
+ sleepTs: BigInt(Date.now()),
413
+ };
414
+ await this.writeActor(actorId, actor.generation, actor.state);
415
+ }
416
+
399
417
  // Stop actor
400
418
  invariant(actor.actor, "actor should be loaded");
401
419
  await actor.actor.onStop("sleep");
@@ -420,6 +438,15 @@ export class FileSystemGlobalState {
420
438
  if (actor.startPromise?.promise)
421
439
  await actor.startPromise.promise.catch();
422
440
 
441
+ // Update state with destroy timestamp
442
+ if (actor.state) {
443
+ actor.state = {
444
+ ...actor.state,
445
+ destroyTs: BigInt(Date.now()),
446
+ };
447
+ await this.writeActor(actorId, actor.generation, actor.state);
448
+ }
449
+
423
450
  // Stop actor if it's running
424
451
  if (actor.actor) {
425
452
  await actor.actor.onStop("destroy");
@@ -561,6 +588,7 @@ export class FileSystemGlobalState {
561
588
  const data =
562
589
  ACTOR_ALARM_VERSIONED.serializeWithEmbeddedVersion(
563
590
  alarmData,
591
+ FILE_SYSTEM_DRIVER_CURRENT_VERSION,
564
592
  );
565
593
  const fs = getNodeFs();
566
594
  await fs.writeFile(tempPath, data);
@@ -621,11 +649,18 @@ export class FileSystemGlobalState {
621
649
  key: state.key,
622
650
  createdAt: state.createdAt,
623
651
  kvStorage: state.kvStorage,
652
+ startTs: state.startTs,
653
+ connectableTs: state.connectableTs,
654
+ sleepTs: state.sleepTs,
655
+ destroyTs: state.destroyTs,
624
656
  };
625
657
 
626
658
  // Perform atomic write
627
659
  const serializedState =
628
- ACTOR_STATE_VERSIONED.serializeWithEmbeddedVersion(bareState);
660
+ ACTOR_STATE_VERSIONED.serializeWithEmbeddedVersion(
661
+ bareState,
662
+ FILE_SYSTEM_DRIVER_CURRENT_VERSION,
663
+ );
629
664
  const fs = getNodeFs();
630
665
  await fs.writeFile(tempPath, serializedState);
631
666
 
@@ -738,6 +773,16 @@ export class FileSystemGlobalState {
738
773
  "unknown",
739
774
  );
740
775
 
776
+ // Update state with start timestamp
777
+ // NOTE: connectableTs is always in sync with startTs since actors become connectable immediately after starting
778
+ const now = BigInt(Date.now());
779
+ entry.state = {
780
+ ...entry.state,
781
+ startTs: now,
782
+ connectableTs: now,
783
+ };
784
+ await this.writeActor(actorId, entry.generation, entry.state);
785
+
741
786
  // Finish
742
787
  entry.startPromise.resolve();
743
788
  entry.startPromise = undefined;
@@ -315,6 +315,10 @@ export class FileSystemManagerDriver implements ManagerDriver {
315
315
  name: actor.name,
316
316
  key: actor.key as string[],
317
317
  createTs: Number(actor.createdAt),
318
+ startTs: actor.startTs !== null ? Number(actor.startTs) : null,
319
+ connectableTs: actor.connectableTs !== null ? Number(actor.connectableTs) : null,
320
+ sleepTs: actor.sleepTs !== null ? Number(actor.sleepTs) : null,
321
+ destroyTs: actor.destroyTs !== null ? Number(actor.destroyTs) : null,
318
322
  });
319
323
  }
320
324
  }
@@ -105,4 +105,8 @@ export interface ActorOutput {
105
105
  name: string;
106
106
  key: ActorKey;
107
107
  createTs?: number;
108
+ startTs?: number | null;
109
+ connectableTs?: number | null;
110
+ sleepTs?: number | null;
111
+ destroyTs?: number | null;
108
112
  }
@@ -731,9 +731,9 @@ function createApiActor(
731
731
  namespace_id: "default", // Assert default namespace
732
732
  runner_name_selector: runnerName,
733
733
  create_ts: actor.createTs ?? Date.now(),
734
- connectable_ts: null,
735
- destroy_ts: null,
736
- sleep_ts: null,
737
- start_ts: null,
734
+ connectable_ts: actor.connectableTs ?? null,
735
+ destroy_ts: actor.destroyTs ?? null,
736
+ sleep_ts: actor.sleepTs ?? null,
737
+ start_ts: actor.startTs ?? null,
738
738
  };
739
739
  }
@@ -51,7 +51,9 @@ export async function apiCall<TInput = unknown, TOutput = unknown>(
51
51
  encoding: "json",
52
52
  skipParseResponse: false,
53
53
  requestVersionedDataHandler: undefined,
54
+ requestVersion: undefined,
54
55
  responseVersionedDataHandler: undefined,
56
+ responseVersion: undefined,
55
57
  requestZodSchema: z.any() as z.ZodType<TInput>,
56
58
  responseZodSchema: z.any() as z.ZodType<TOutput>,
57
59
  // Identity conversions (passthrough for generic API calls)
@@ -1,66 +1,138 @@
1
- import {
2
- createVersionedDataHandler,
3
- type MigrationFn,
4
- } from "@/common/versioned-data";
5
- import type * as v1 from "../../../dist/schemas/actor-persist/v1";
6
- import type * as v2 from "../../../dist/schemas/actor-persist/v2";
1
+ import { createVersionedDataHandler } from "vbare";
2
+ import * as v1 from "../../../dist/schemas/actor-persist/v1";
3
+ import * as v2 from "../../../dist/schemas/actor-persist/v2";
7
4
  import * as v3 from "../../../dist/schemas/actor-persist/v3";
8
5
 
9
6
  export const CURRENT_VERSION = 3;
10
7
 
11
- const migrations = new Map<number, MigrationFn<any, any>>([
12
- [
13
- 1,
14
- (v1Data: v1.PersistedActor): v2.PersistedActor => ({
15
- ...v1Data,
16
- connections: v1Data.connections.map((conn) => ({
17
- ...conn,
18
- hibernatableRequestId: null,
19
- })),
20
- hibernatableWebSockets: [],
21
- }),
22
- ],
23
- [
24
- 2,
25
- (v2Data: v2.PersistedActor): v3.Actor => {
26
- // Transform scheduled events from nested structure to flat structure
27
- const scheduledEvents: v3.ScheduleEvent[] =
28
- v2Data.scheduledEvents.map((event) => {
29
- // Extract action and args from the kind wrapper
30
- if (event.kind.tag === "GenericPersistedScheduleEvent") {
31
- return {
32
- eventId: event.eventId,
33
- timestamp: event.timestamp,
34
- action: event.kind.val.action,
35
- args: event.kind.val.args,
36
- };
37
- }
38
- // Fallback for unknown kinds
39
- throw new Error(
40
- `Unknown schedule event kind: ${event.kind.tag}`,
41
- );
42
- });
8
+ // Converter from v1 to v2
9
+ const v1ToV2 = (v1Data: v1.PersistedActor): v2.PersistedActor => ({
10
+ ...v1Data,
11
+ connections: v1Data.connections.map((conn) => ({
12
+ ...conn,
13
+ hibernatableRequestId: null,
14
+ })),
15
+ hibernatableWebSockets: [],
16
+ });
43
17
 
44
- return {
45
- input: v2Data.input,
46
- hasInitialized: v2Data.hasInitialized,
47
- state: v2Data.state,
48
- scheduledEvents,
49
- };
18
+ // Converter from v2 to v3
19
+ const v2ToV3 = (v2Data: v2.PersistedActor): v3.Actor => {
20
+ // Transform scheduled events from nested structure to flat structure
21
+ const scheduledEvents: v3.ScheduleEvent[] = v2Data.scheduledEvents.map(
22
+ (event) => {
23
+ // Extract action and args from the kind wrapper
24
+ if (event.kind.tag === "GenericPersistedScheduleEvent") {
25
+ return {
26
+ eventId: event.eventId,
27
+ timestamp: event.timestamp,
28
+ action: event.kind.val.action,
29
+ args: event.kind.val.args,
30
+ };
31
+ }
32
+ // Fallback for unknown kinds
33
+ throw new Error(`Unknown schedule event kind: ${event.kind.tag}`);
50
34
  },
51
- ],
52
- ]);
35
+ );
36
+
37
+ return {
38
+ input: v2Data.input,
39
+ hasInitialized: v2Data.hasInitialized,
40
+ state: v2Data.state,
41
+ scheduledEvents,
42
+ };
43
+ };
44
+
45
+ // Converter from v3 to v2
46
+ const v3ToV2 = (v3Data: v3.Actor): v2.PersistedActor => {
47
+ // Transform scheduled events from flat structure back to nested structure
48
+ const scheduledEvents: v2.PersistedScheduleEvent[] = v3Data.scheduledEvents.map(
49
+ (event) => ({
50
+ eventId: event.eventId,
51
+ timestamp: event.timestamp,
52
+ kind: {
53
+ tag: "GenericPersistedScheduleEvent" as const,
54
+ val: {
55
+ action: event.action,
56
+ args: event.args,
57
+ },
58
+ },
59
+ }),
60
+ );
61
+
62
+ return {
63
+ input: v3Data.input,
64
+ hasInitialized: v3Data.hasInitialized,
65
+ state: v3Data.state,
66
+ scheduledEvents,
67
+ connections: [],
68
+ hibernatableWebSockets: [],
69
+ };
70
+ };
71
+
72
+ // Converter from v2 to v1
73
+ const v2ToV1 = (v2Data: v2.PersistedActor): v1.PersistedActor => {
74
+ return {
75
+ input: v2Data.input,
76
+ hasInitialized: v2Data.hasInitialized,
77
+ state: v2Data.state,
78
+ scheduledEvents: v2Data.scheduledEvents,
79
+ connections: v2Data.connections.map((conn) => {
80
+ const { hibernatableRequestId, ...rest } = conn;
81
+ return rest;
82
+ }),
83
+ };
84
+ };
53
85
 
54
86
  export const ACTOR_VERSIONED = createVersionedDataHandler<v3.Actor>({
55
- currentVersion: CURRENT_VERSION,
56
- migrations,
57
- serializeVersion: (data) => v3.encodeActor(data),
58
- deserializeVersion: (bytes) => v3.decodeActor(bytes),
87
+ deserializeVersion: (bytes, version) => {
88
+ switch (version) {
89
+ case 1:
90
+ return v1.decodePersistedActor(bytes);
91
+ case 2:
92
+ return v2.decodePersistedActor(bytes);
93
+ case 3:
94
+ return v3.decodeActor(bytes);
95
+ default:
96
+ throw new Error(`Unknown version ${version}`);
97
+ }
98
+ },
99
+ serializeVersion: (data, version) => {
100
+ switch (version) {
101
+ case 1:
102
+ return v1.encodePersistedActor(data as v1.PersistedActor);
103
+ case 2:
104
+ return v2.encodePersistedActor(data as v2.PersistedActor);
105
+ case 3:
106
+ return v3.encodeActor(data as v3.Actor);
107
+ default:
108
+ throw new Error(`Unknown version ${version}`);
109
+ }
110
+ },
111
+ deserializeConverters: () => [v1ToV2, v2ToV3],
112
+ serializeConverters: () => [v3ToV2, v2ToV1],
59
113
  });
60
114
 
61
115
  export const CONN_VERSIONED = createVersionedDataHandler<v3.Conn>({
62
- currentVersion: CURRENT_VERSION,
63
- migrations: new Map(),
64
- serializeVersion: (data) => v3.encodeConn(data),
65
- deserializeVersion: (bytes) => v3.decodeConn(bytes),
116
+ deserializeVersion: (bytes, version) => {
117
+ switch (version) {
118
+ case 3:
119
+ return v3.decodeConn(bytes);
120
+ default:
121
+ throw new Error(
122
+ `Conn type only exists in version 3+, got version ${version}`,
123
+ );
124
+ }
125
+ },
126
+ serializeVersion: (data, version) => {
127
+ switch (version) {
128
+ case 3:
129
+ return v3.encodeConn(data as v3.Conn);
130
+ default:
131
+ throw new Error(
132
+ `Conn type only exists in version 3+, got version ${version}`,
133
+ );
134
+ }
135
+ },
136
+ deserializeConverters: () => [],
137
+ serializeConverters: () => [],
66
138
  });
@@ -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";