@inflatable-cookie/longhorn 0.5.0 → 0.7.0

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 (167) hide show
  1. package/README.md +2 -2
  2. package/package.json +1 -7
  3. package/src/agent-control/generated/protocol.ts +228 -0
  4. package/src/agent-control/index.ts +1 -5
  5. package/src/agent-control/inject.ts +4 -1
  6. package/src/agent-control/selection.ts +42 -25
  7. package/src/agent-control/shim.ts +428 -198
  8. package/src/bridge/direct.ts +1 -1
  9. package/src/bridge/generated/fields.ts +4 -3
  10. package/src/bridge/generated/protocol.ts +62 -7
  11. package/src/bridge/generated/variant-fields.ts +1 -1
  12. package/src/bridge/job.ts +49 -12
  13. package/src/bridge/loopback.ts +7 -25
  14. package/src/bridge/operation.ts +13 -43
  15. package/src/bridge/router.ts +10 -23
  16. package/src/bridge/runtime/connection.ts +78 -109
  17. package/src/bridge/runtime/policy.ts +8 -28
  18. package/src/bridge/runtime/retry.ts +6 -18
  19. package/src/bridge/session.ts +4 -14
  20. package/src/bridge/stream/connection.ts +24 -55
  21. package/src/bridge/stream/source.ts +2 -6
  22. package/src/bridge/stream/tracker.ts +12 -12
  23. package/src/bridge/supervision.ts +104 -97
  24. package/src/bridge/validation/base.ts +21 -63
  25. package/src/bridge/validation/jobs.ts +11 -25
  26. package/src/bridge/validation/negotiation.ts +28 -43
  27. package/src/bridge/validation/operations.ts +29 -65
  28. package/src/bridge/validation/streams.ts +3 -10
  29. package/src/commands/client.ts +37 -28
  30. package/src/commands/controller.ts +119 -96
  31. package/src/commands/generated/fields.ts +7 -6
  32. package/src/commands/generated/protocol.ts +28 -1
  33. package/src/commands/generated/variant-fields.ts +1 -1
  34. package/src/commands/keyboard.ts +94 -100
  35. package/src/commands/physical-code.ts +11 -0
  36. package/src/commands/ports.ts +8 -13
  37. package/src/commands/projectors.ts +100 -75
  38. package/src/commands/validation.ts +434 -241
  39. package/src/config/client.ts +9 -22
  40. package/src/config/generated/base.ts +22 -6
  41. package/src/config/generated/fields.ts +4 -3
  42. package/src/config/generated/label-template.ts +1 -1
  43. package/src/config/generated/labels.ts +1 -1
  44. package/src/config/generated/protocol.ts +1 -1
  45. package/src/config/generated/restore.ts +35 -12
  46. package/src/config/generated/variant-fields.ts +1 -1
  47. package/src/config/names.ts +6 -12
  48. package/src/config/validation/commands.ts +11 -25
  49. package/src/config/validation/outcomes.ts +163 -70
  50. package/src/config/validation/primitives.ts +35 -80
  51. package/src/config/validation/projection.ts +100 -128
  52. package/src/core/connection.ts +187 -83
  53. package/src/core/controller-lifecycle.ts +144 -0
  54. package/src/core/generated/identifiers.ts +4 -0
  55. package/src/core/identifiers.ts +40 -0
  56. package/src/core/index.ts +10 -1
  57. package/src/core/json-value.ts +58 -0
  58. package/src/core/listeners.ts +35 -0
  59. package/src/core/record.ts +80 -0
  60. package/src/core/transport.ts +9 -11
  61. package/src/core/wire-copy.ts +7 -0
  62. package/src/history/client.ts +11 -13
  63. package/src/history/controller.ts +106 -108
  64. package/src/history/direct.ts +1 -3
  65. package/src/history/generated/fields.ts +4 -3
  66. package/src/history/generated/protocol.ts +8 -5
  67. package/src/history/generated/variant-fields.ts +1 -1
  68. package/src/history/ports.ts +7 -4
  69. package/src/history/serialized.ts +9 -13
  70. package/src/history/validation.ts +69 -75
  71. package/src/history-tree/client.ts +89 -11
  72. package/src/history-tree/controller.ts +380 -96
  73. package/src/history-tree/direct.ts +3 -1
  74. package/src/history-tree/generated/fields.ts +5 -3
  75. package/src/history-tree/generated/protocol.ts +43 -7
  76. package/src/history-tree/generated/variant-fields.ts +7 -1
  77. package/src/history-tree/ports.ts +12 -5
  78. package/src/history-tree/serialized.ts +39 -11
  79. package/src/history-tree/validation.ts +286 -78
  80. package/src/layout/generated/protocol.ts +4 -3
  81. package/src/layout/generated/variant-fields.ts +47 -0
  82. package/src/layout/index.ts +1 -1
  83. package/src/layout/ratio.ts +10 -9
  84. package/src/layout/validation.ts +98 -32
  85. package/src/layout/visibility.ts +1 -2
  86. package/src/licence/client.ts +48 -7
  87. package/src/licence/controller.ts +127 -70
  88. package/src/licence/direct.ts +3 -1
  89. package/src/licence/generated/fields.ts +4 -3
  90. package/src/licence/generated/key-conformance.json +28 -0
  91. package/src/licence/generated/protocol.ts +9 -5
  92. package/src/licence/generated/variant-fields.ts +1 -1
  93. package/src/licence/key.ts +42 -18
  94. package/src/licence/ports.ts +10 -3
  95. package/src/licence/serialized.ts +32 -9
  96. package/src/licence/validation.ts +134 -43
  97. package/src/native-content/client.ts +207 -234
  98. package/src/native-content/generated/fields.ts +6 -6
  99. package/src/native-content/generated/protocol.ts +16 -5
  100. package/src/native-content/generated/variant-fields.ts +1 -1
  101. package/src/native-content/ports.ts +11 -16
  102. package/src/native-content/serialized.ts +12 -19
  103. package/src/native-content/validation/base.ts +57 -80
  104. package/src/native-content/validation/messages.ts +24 -35
  105. package/src/native-content/validation/state.ts +31 -33
  106. package/src/notifications/client.ts +85 -51
  107. package/src/notifications/controller.ts +400 -145
  108. package/src/notifications/direct.ts +3 -1
  109. package/src/notifications/generated/fields.ts +4 -3
  110. package/src/notifications/generated/labels.ts +1 -1
  111. package/src/notifications/generated/protocol.ts +5 -1
  112. package/src/notifications/generated/variant-fields.ts +1 -1
  113. package/src/notifications/ports.ts +9 -4
  114. package/src/notifications/serialized.ts +11 -9
  115. package/src/notifications/tone.ts +1 -1
  116. package/src/notifications/validation.ts +165 -71
  117. package/src/operation/bridge.ts +187 -60
  118. package/src/operation/client.ts +48 -62
  119. package/src/operation/controller.ts +67 -97
  120. package/src/operation/direct.ts +1 -3
  121. package/src/operation/generated/fields.ts +4 -3
  122. package/src/operation/generated/labels.ts +1 -1
  123. package/src/operation/generated/protocol.ts +5 -1
  124. package/src/operation/generated/variant-fields.ts +1 -1
  125. package/src/operation/ports.ts +12 -10
  126. package/src/operation/serialized.ts +8 -9
  127. package/src/operation/validation.ts +189 -104
  128. package/src/settings/client.ts +11 -44
  129. package/src/settings/connection.ts +35 -53
  130. package/src/settings/generated/fields.ts +5 -4
  131. package/src/settings/generated/protocol.ts +14 -5
  132. package/src/settings/generated/variant-fields.ts +1 -1
  133. package/src/settings/names.ts +2 -4
  134. package/src/settings/registry.ts +23 -25
  135. package/src/settings/validation/authority.ts +21 -19
  136. package/src/settings/validation/command.ts +22 -41
  137. package/src/settings/validation/error.ts +2 -8
  138. package/src/settings/validation/primitives.ts +46 -97
  139. package/src/settings/validation/projection.ts +8 -24
  140. package/src/settings/validation/registry.ts +33 -79
  141. package/src/surface-transfer/client.ts +6 -18
  142. package/src/surface-transfer/generated/fields.ts +3 -3
  143. package/src/surface-transfer/generated/protocol.ts +3 -3
  144. package/src/surface-transfer/generated/variant-fields.ts +1 -1
  145. package/src/surface-transfer/validation.ts +91 -86
  146. package/src/surfaces/client.ts +33 -55
  147. package/src/surfaces/generated/fields.ts +3 -3
  148. package/src/surfaces/generated/protocol.ts +1 -1
  149. package/src/surfaces/generated/variant-fields.ts +1 -1
  150. package/src/surfaces/validation.ts +227 -99
  151. package/src/transfer/client.ts +26 -60
  152. package/src/transfer/connection.ts +29 -51
  153. package/src/transfer/generated/fields.ts +7 -6
  154. package/src/transfer/generated/protocol.ts +13 -5
  155. package/src/transfer/generated/variant-fields.ts +1 -1
  156. package/src/transfer/index.ts +8 -1
  157. package/src/transfer/payload.ts +20 -35
  158. package/src/transfer/validation.ts +169 -119
  159. package/src/update/client.ts +86 -9
  160. package/src/update/controller.ts +264 -116
  161. package/src/update/direct.ts +3 -1
  162. package/src/update/generated/fields.ts +5 -4
  163. package/src/update/generated/protocol.ts +25 -9
  164. package/src/update/generated/variant-fields.ts +4 -2
  165. package/src/update/ports.ts +14 -6
  166. package/src/update/serialized.ts +36 -10
  167. package/src/update/validation.ts +118 -48
@@ -1,17 +1,26 @@
1
1
  import {
2
2
  BridgeJobTracker,
3
- type BridgeCommandEnvelope,
4
- type BridgeCommandReply,
5
- type BridgeDomainClient,
6
- type BridgeJobId,
7
- type BridgeJobTerminalEvent,
8
- type BridgeOperationDescriptor,
9
- type BridgeProgressEvent,
10
- type BridgeQueryEnvelope,
11
- type BridgeQueryReply,
12
- type BridgeRequestContext,
13
- type BridgeRequestId,
14
- type DomainId,
3
+ bridgeCodec,
4
+ parseBridgeCommandEnvelope,
5
+ parseBridgeCommandReply,
6
+ parseBridgeJobTerminalEvent,
7
+ parseBridgeQueryEnvelope,
8
+ parseBridgeQueryReply,
9
+ } from "@inflatable-cookie/longhorn/bridge";
10
+ import type {
11
+ BridgeCommandEnvelope,
12
+ BridgeCommandReply,
13
+ BridgeDomainClient,
14
+ BridgeFailure,
15
+ BridgeJobId,
16
+ BridgeJobTerminalEvent,
17
+ BridgeOperationDescriptor,
18
+ BridgeProgressEvent,
19
+ BridgeQueryEnvelope,
20
+ BridgeQueryReply,
21
+ BridgeRequestContext,
22
+ BridgeRequestId,
23
+ DomainId,
15
24
  } from "@inflatable-cookie/longhorn/bridge";
16
25
 
17
26
  import {
@@ -44,6 +53,40 @@ export interface BridgeOperationPortOptions {
44
53
  readonly nextOperationRequestId: () => string;
45
54
  }
46
55
 
56
+ export type BridgeOperationFailure =
57
+ | {
58
+ readonly kind: "rejected" | "indeterminate";
59
+ readonly requestId: BridgeRequestId;
60
+ readonly failure: BridgeFailure<null>;
61
+ }
62
+ | {
63
+ readonly kind: "miscorrelated";
64
+ readonly requestId: BridgeRequestId;
65
+ readonly receivedRequestId: BridgeRequestId;
66
+ }
67
+ | {
68
+ readonly kind: "failed";
69
+ readonly requestId: BridgeRequestId;
70
+ readonly jobId: BridgeJobId;
71
+ readonly failure: BridgeFailure<null>;
72
+ }
73
+ | {
74
+ readonly kind: "cancelled";
75
+ readonly requestId: BridgeRequestId;
76
+ readonly jobId: BridgeJobId;
77
+ };
78
+
79
+ export class BridgeOperationError extends Error {
80
+ constructor(readonly outcome: BridgeOperationFailure) {
81
+ super(
82
+ "failure" in outcome
83
+ ? `operation bridge ${outcome.kind}: ${outcome.failure.message}`
84
+ : `operation bridge ${outcome.kind}`,
85
+ );
86
+ this.name = "BridgeOperationError";
87
+ }
88
+ }
89
+
47
90
  export function createBridgeOperationPort(options: BridgeOperationPortOptions): OperationPort {
48
91
  return {
49
92
  snapshot: (query) => executeQuery(options, query),
@@ -57,84 +100,150 @@ async function executeQuery(options: BridgeOperationPortOptions, query: Operatio
57
100
  const context = nextContext(options);
58
101
  const descriptor = queryDescriptor(options.domainId);
59
102
  const reply = await options.client.execute(descriptor, { context, payload: query });
60
- if (reply.requestId !== context.requestId || !("success" in reply.outcome)) {
61
- throw new Error("operation bridge query was rejected or miscorrelated");
62
- }
63
- return reply.outcome.success;
103
+ return queryResult(context.requestId, reply);
64
104
  }
65
105
 
66
- async function executeCommand(options: BridgeOperationPortOptions, command: OperationMutationCommand) {
106
+ async function executeCommand(
107
+ options: BridgeOperationPortOptions,
108
+ command: OperationMutationCommand,
109
+ ) {
67
110
  const context = nextContext(options);
68
111
  const descriptor = commandDescriptor(options.domainId);
69
112
  const reply = await options.client.execute(descriptor, {
70
113
  context,
71
114
  authorityEpoch: options.authorityEpoch,
72
- expectedRevision: command.kind === "register" || command.kind === "changeRetention" || command.kind === "teardown"
73
- ? command.expectedCatalogueRevision
74
- : null,
115
+ expectedRevision:
116
+ command.kind === "register" ||
117
+ command.kind === "changeRetention" ||
118
+ command.kind === "teardown"
119
+ ? command.expectedCatalogueRevision
120
+ : null,
75
121
  idempotencyKey: context.requestId,
76
122
  payload: command,
77
123
  });
78
- if (reply.requestId !== context.requestId || !("applied" in reply.outcome)) {
79
- throw new Error("operation bridge command was rejected, indeterminate, or miscorrelated");
124
+ const { requestId } = context;
125
+ checkCorrelation(requestId, reply.requestId);
126
+ const { outcome } = reply;
127
+ if ("rejected" in outcome) {
128
+ throw new BridgeOperationError({ kind: "rejected", requestId, failure: outcome.rejected });
80
129
  }
81
- return reply.outcome.applied;
130
+ if ("indeterminate" in outcome) {
131
+ throw new BridgeOperationError({
132
+ kind: "indeterminate",
133
+ requestId,
134
+ failure: outcome.indeterminate,
135
+ });
136
+ }
137
+ return outcome.applied;
82
138
  }
83
139
 
84
- async function executeCancellation(options: BridgeOperationPortOptions, command: OperationCancellationCommand) {
140
+ async function executeCancellation(
141
+ options: BridgeOperationPortOptions,
142
+ command: OperationCancellationCommand,
143
+ ) {
85
144
  const context = nextContext(options);
86
145
  const descriptor = cancellationDescriptor(options.domainId);
87
146
  const reply = await options.client.execute(descriptor, { context, payload: command });
88
- if (reply.requestId !== context.requestId || !("success" in reply.outcome)) {
89
- throw new Error("operation bridge cancellation was rejected or miscorrelated");
147
+ return queryResult(context.requestId, reply);
148
+ }
149
+
150
+ function queryResult<Result>(requestId: BridgeRequestId, reply: BridgeQueryReply<Result, null>) {
151
+ checkCorrelation(requestId, reply.requestId);
152
+ if ("rejected" in reply.outcome) {
153
+ throw new BridgeOperationError({
154
+ kind: "rejected",
155
+ requestId,
156
+ failure: reply.outcome.rejected,
157
+ });
90
158
  }
91
159
  return reply.outcome.success;
92
160
  }
93
161
 
162
+ function checkCorrelation(requestId: BridgeRequestId, receivedRequestId: BridgeRequestId): void {
163
+ if (receivedRequestId !== requestId) {
164
+ throw new BridgeOperationError({ kind: "miscorrelated", requestId, receivedRequestId });
165
+ }
166
+ }
167
+
94
168
  function nextContext(options: BridgeOperationPortOptions): BridgeRequestContext {
95
- return { requestId: options.nextBridgeRequestId(), sessionId: options.sessionId, domainId: options.domainId };
169
+ return {
170
+ requestId: options.nextBridgeRequestId(),
171
+ sessionId: options.sessionId,
172
+ domainId: options.domainId,
173
+ };
96
174
  }
97
175
 
98
- function codec<Value>(assertValue: (value: unknown) => void) {
99
- return { parse(value: unknown): Value { assertValue(value); return value as Value; } };
176
+ function codec<Value>(assertValue: (value: unknown) => asserts value is Value) {
177
+ return bridgeCodec((value) => {
178
+ assertValue(value);
179
+ return value;
180
+ });
100
181
  }
101
182
 
102
- function queryDescriptor(domainId: DomainId): BridgeOperationDescriptor<BridgeQueryEnvelope<OperationSnapshotQuery>, BridgeQueryReply<OperationSnapshotResponse, null>> {
183
+ const noDetails = bridgeCodec((value): null => {
184
+ if (value !== null) {
185
+ throw new Error("operation bridge failure details must be null");
186
+ }
187
+ return null;
188
+ });
189
+
190
+ function queryDescriptor(
191
+ domainId: DomainId,
192
+ ): BridgeOperationDescriptor<
193
+ BridgeQueryEnvelope<OperationSnapshotQuery>,
194
+ BridgeQueryReply<OperationSnapshotResponse, null>
195
+ > {
103
196
  return {
104
- route: "longhorn.operation.snapshot", domainId, kind: "query",
197
+ route: "longhorn.operation.snapshot",
198
+ domainId,
199
+ kind: "query",
105
200
  requiredCapability: "longhorn.operation.read",
106
- request: codec((value) => {
107
- const payload = (value as BridgeQueryEnvelope<unknown>).payload;
108
- assertValidOperationSnapshotQuery(payload);
109
- }),
110
- reply: codec((value) => {
111
- const outcome = (value as BridgeQueryReply<unknown, null>).outcome;
112
- if ("success" in outcome) assertValidOperationSnapshotResponse(outcome.success);
113
- }),
201
+ request: bridgeCodec((value) =>
202
+ parseBridgeQueryEnvelope(value, codec(assertValidOperationSnapshotQuery)),
203
+ ),
204
+ reply: bridgeCodec((value) =>
205
+ parseBridgeQueryReply(value, codec(assertValidOperationSnapshotResponse), noDetails),
206
+ ),
114
207
  };
115
208
  }
116
209
 
117
- function commandDescriptor(domainId: DomainId): BridgeOperationDescriptor<BridgeCommandEnvelope<OperationMutationCommand>, BridgeCommandReply<OperationMutationResult, null>> {
210
+ function commandDescriptor(
211
+ domainId: DomainId,
212
+ ): BridgeOperationDescriptor<
213
+ BridgeCommandEnvelope<OperationMutationCommand>,
214
+ BridgeCommandReply<OperationMutationResult, null>
215
+ > {
118
216
  return {
119
- route: "longhorn.operation.mutate", domainId, kind: "command",
217
+ route: "longhorn.operation.mutate",
218
+ domainId,
219
+ kind: "command",
120
220
  requiredCapability: "longhorn.operation.manage",
121
- request: codec((value) => assertValidOperationMutationCommand((value as BridgeCommandEnvelope<unknown>).payload)),
122
- reply: codec((value) => {
123
- const outcome = (value as BridgeCommandReply<unknown, null>).outcome;
124
- if ("applied" in outcome) assertValidOperationMutationResult(outcome.applied);
125
- }),
221
+ request: bridgeCodec((value) =>
222
+ parseBridgeCommandEnvelope(value, codec(assertValidOperationMutationCommand)),
223
+ ),
224
+ reply: bridgeCodec((value) =>
225
+ parseBridgeCommandReply(value, codec(assertValidOperationMutationResult), noDetails),
226
+ ),
126
227
  };
127
228
  }
128
229
 
129
- function cancellationDescriptor(domainId: DomainId): BridgeOperationDescriptor<BridgeQueryEnvelope<OperationCancellationCommand>, BridgeQueryReply<OperationCancellationResult, null>> {
230
+ function cancellationDescriptor(
231
+ domainId: DomainId,
232
+ ): BridgeOperationDescriptor<
233
+ BridgeQueryEnvelope<OperationCancellationCommand>,
234
+ BridgeQueryReply<OperationCancellationResult, null>
235
+ > {
130
236
  return {
131
- route: "longhorn.operation.cancel", domainId, kind: "cancellation",
237
+ route: "longhorn.operation.cancel",
238
+ domainId,
239
+ kind: "cancellation",
132
240
  requiredCapability: "longhorn.operation.cancel",
133
- request: codec((value) => assertValidOperationCancellationCommand((value as BridgeQueryEnvelope<unknown>).payload)),
134
- reply: codec((value) => {
135
- const outcome = (value as BridgeQueryReply<unknown, null>).outcome;
136
- if ("success" in outcome) assertValidOperationCancellationResult(outcome.success);
137
- }),
241
+ request: bridgeCodec((value) =>
242
+ parseBridgeQueryEnvelope(value, codec(assertValidOperationCancellationCommand)),
243
+ ),
244
+ reply: bridgeCodec((value) =>
245
+ parseBridgeQueryReply(value, codec(assertValidOperationCancellationResult), noDetails),
246
+ ),
138
247
  };
139
248
  }
140
249
 
@@ -153,16 +262,34 @@ export class BridgeOperationJobCorrelation {
153
262
  this.#operationId = correlation.operationId;
154
263
  }
155
264
 
156
- acceptProgress(event: BridgeProgressEvent<OperationChangedEvent>): OperationChangedEvent | undefined {
157
- if (this.#tracker.classifyProgress(event) !== "accept") return undefined;
265
+ acceptProgress(
266
+ event: BridgeProgressEvent<OperationChangedEvent>,
267
+ ): OperationChangedEvent | undefined {
268
+ if (this.#tracker.classifyProgress(event) !== "accept") {
269
+ return undefined;
270
+ }
158
271
  assertValidOperationChangedEvent(event.progress);
159
272
  return event.progress.operationId === this.#operationId ? event.progress : undefined;
160
273
  }
161
274
 
162
- acceptTerminal(event: BridgeJobTerminalEvent<OperationChangedEvent, null>): OperationChangedEvent | undefined {
163
- if (this.#tracker.classifyTerminal(event) !== "accept") return undefined;
164
- if (typeof event.outcome !== "object" || event.outcome === null || !("succeeded" in event.outcome)) return undefined;
165
- assertValidOperationChangedEvent(event.outcome.succeeded);
166
- return event.outcome.succeeded.operationId === this.#operationId ? event.outcome.succeeded : undefined;
275
+ /** Returns a correlated success or throws for a correlated failed or cancelled job. */
276
+ acceptTerminal(
277
+ event: BridgeJobTerminalEvent<OperationChangedEvent, null>,
278
+ ): OperationChangedEvent | undefined {
279
+ if (this.#tracker.classifyTerminal(event) !== "accept") {
280
+ return undefined;
281
+ }
282
+ const { requestId, jobId, outcome } = parseBridgeJobTerminalEvent(
283
+ event,
284
+ codec(assertValidOperationChangedEvent),
285
+ noDetails,
286
+ );
287
+ if (outcome === "cancelled") {
288
+ throw new BridgeOperationError({ kind: "cancelled", requestId, jobId });
289
+ }
290
+ if ("failed" in outcome) {
291
+ throw new BridgeOperationError({ kind: "failed", requestId, jobId, failure: outcome.failed });
292
+ }
293
+ return outcome.succeeded.operationId === this.#operationId ? outcome.succeeded : undefined;
167
294
  }
168
295
  }
@@ -1,7 +1,7 @@
1
- import {
2
- CheckedSnapshotConnection,
3
- type ConnectionFailure,
4
- type ConnectionFailureReporter,
1
+ import { CheckedSnapshotConnection, CheckedSubscription } from "@inflatable-cookie/longhorn/core";
2
+ import type {
3
+ ConnectionFailure,
4
+ ConnectionFailureReporter,
5
5
  } from "@inflatable-cookie/longhorn/core";
6
6
 
7
7
  import {
@@ -10,24 +10,24 @@ import {
10
10
  assertValidOperationChangedEvent,
11
11
  assertValidOperationMutationCommand,
12
12
  assertValidOperationMutationResult,
13
+ assertValidOperationSnapshot,
13
14
  assertValidOperationSnapshotResponse,
14
15
  } from "./validation.ts";
15
- import {
16
- OPERATION_PROTOCOL_VERSION,
17
- type OperationCancellationCommand,
18
- type OperationCancellationResult,
19
- type OperationChangedEvent,
20
- type OperationMutationCommand,
21
- type OperationMutationResult,
22
- type OperationSnapshot,
23
- type OperationSnapshotResponse,
16
+ import { OPERATION_PROTOCOL_VERSION } from "./generated/protocol.ts";
17
+ import type {
18
+ OperationCancellationCommand,
19
+ OperationCancellationResult,
20
+ OperationMutationCommand,
21
+ OperationMutationResult,
22
+ OperationSnapshot,
23
+ OperationSnapshotResponse,
24
24
  } from "./generated/protocol.ts";
25
25
  import type { OperationPort } from "./ports.ts";
26
26
 
27
27
  export interface OperationSubscription {
28
28
  readonly ready: Promise<void>;
29
29
  current(): OperationSnapshot | undefined;
30
- failures(): readonly ConnectionFailure[];
30
+ failures(): ReadonlyArray<ConnectionFailure>;
31
31
  dispose(): Promise<void>;
32
32
  }
33
33
 
@@ -73,63 +73,43 @@ export class OperationClient {
73
73
  listener: (snapshot: OperationSnapshot) => void,
74
74
  onFailure?: ConnectionFailureReporter,
75
75
  ): OperationSubscription {
76
- return new CheckedOperationSubscription(this, this.#port, listener, onFailure);
76
+ const port = this.#port;
77
+ return new CheckedSubscription(
78
+ new CheckedSnapshotConnection({
79
+ listen: async (receive) => (port.listen === undefined ? () => {} : port.listen(receive)),
80
+ loadSnapshot: async () => (await this.snapshot()).snapshot,
81
+ validateSnapshot: parseSnapshot,
82
+ handleEvent: operationEventAction,
83
+ isNewer: isNewerOperationSnapshot,
84
+ onSnapshot: listener,
85
+ onFailure,
86
+ }),
87
+ () => {},
88
+ );
77
89
  }
78
90
  }
79
91
 
80
92
  export class OperationResponseCorrelationError extends Error {
81
- constructor(readonly expectedRequestId: string, readonly receivedRequestId: string) {
82
- super(`operation response correlation mismatch: expected ${expectedRequestId}; received ${receivedRequestId}`);
93
+ constructor(
94
+ readonly expectedRequestId: string,
95
+ readonly receivedRequestId: string,
96
+ ) {
97
+ super(
98
+ `operation response correlation mismatch: expected ${expectedRequestId}; received ${receivedRequestId}`,
99
+ );
83
100
  this.name = "OperationResponseCorrelationError";
84
101
  }
85
102
  }
86
103
 
87
104
  function assertResponseCorrelation(received: string, expected: string): void {
88
- if (received !== expected) throw new OperationResponseCorrelationError(expected, received);
89
- }
90
-
91
- class CheckedOperationSubscription implements OperationSubscription {
92
- readonly ready: Promise<void>;
93
- readonly #connection: CheckedSnapshotConnection<OperationSnapshot>;
94
-
95
- constructor(
96
- client: OperationClient,
97
- port: OperationPort,
98
- listener: (snapshot: OperationSnapshot) => void,
99
- onFailure?: ConnectionFailureReporter,
100
- ) {
101
- this.#connection = new CheckedSnapshotConnection({
102
- listen: async (receive) => {
103
- if (port.listen === undefined) return () => {};
104
- return port.listen(receive);
105
- },
106
- loadSnapshot: async () => (await client.snapshot()).snapshot,
107
- validateSnapshot: parseSnapshot,
108
- handleEvent: operationEventAction,
109
- isNewer: isNewerOperationSnapshot,
110
- onSnapshot: listener,
111
- onFailure,
112
- });
113
- this.ready = this.#connection.ready.then(() => undefined);
114
- }
115
-
116
- current(): OperationSnapshot | undefined {
117
- return this.#connection.current();
118
- }
119
-
120
- failures(): readonly ConnectionFailure[] {
121
- return this.#connection.failures();
122
- }
123
-
124
- dispose(): Promise<void> {
125
- return this.#connection.dispose();
105
+ if (received !== expected) {
106
+ throw new OperationResponseCorrelationError(expected, received);
126
107
  }
127
108
  }
128
109
 
129
110
  function parseSnapshot(value: unknown): OperationSnapshot {
130
- const response = { requestId: "request:validation", snapshot: value };
131
- assertValidOperationSnapshotResponse(response);
132
- return value as OperationSnapshot;
111
+ assertValidOperationSnapshot(value);
112
+ return value;
133
113
  }
134
114
 
135
115
  export function operationEventAction(
@@ -137,8 +117,10 @@ export function operationEventAction(
137
117
  current: OperationSnapshot | undefined,
138
118
  ): { kind: "ignore" } | { kind: "refresh" } {
139
119
  assertValidOperationChangedEvent(value);
140
- const event = value as OperationChangedEvent;
141
- if (current === undefined) return { kind: "refresh" };
120
+ const event = value;
121
+ if (current === undefined) {
122
+ return { kind: "refresh" };
123
+ }
142
124
  if (event.authority.authorityId !== current.authority.authorityId) {
143
125
  return { kind: "ignore" };
144
126
  }
@@ -158,8 +140,12 @@ export function isNewerOperationSnapshot(
158
140
  candidate: OperationSnapshot,
159
141
  current: OperationSnapshot | undefined,
160
142
  ): boolean {
161
- if (current === undefined) return true;
162
- if (candidate.authority.authorityId !== current.authority.authorityId) return false;
143
+ if (current === undefined) {
144
+ return true;
145
+ }
146
+ if (candidate.authority.authorityId !== current.authority.authorityId) {
147
+ return false;
148
+ }
163
149
  if (candidate.authority.authorityEpoch !== current.authority.authorityEpoch) {
164
150
  return candidate.authority.authorityEpoch > current.authority.authorityEpoch;
165
151
  }