@inflatable-cookie/longhorn 0.5.0 → 0.6.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 (126) hide show
  1. package/package.json +1 -1
  2. package/src/agent-control/inject.ts +4 -1
  3. package/src/agent-control/selection.ts +35 -20
  4. package/src/agent-control/shim.ts +302 -144
  5. package/src/bridge/direct.ts +1 -1
  6. package/src/bridge/generated/fields.ts +3 -2
  7. package/src/bridge/generated/protocol.ts +7 -3
  8. package/src/bridge/job.ts +4 -14
  9. package/src/bridge/loopback.ts +7 -25
  10. package/src/bridge/operation.ts +13 -43
  11. package/src/bridge/router.ts +10 -23
  12. package/src/bridge/runtime/connection.ts +77 -105
  13. package/src/bridge/runtime/policy.ts +8 -28
  14. package/src/bridge/runtime/retry.ts +6 -18
  15. package/src/bridge/session.ts +4 -14
  16. package/src/bridge/stream/connection.ts +16 -38
  17. package/src/bridge/stream/source.ts +2 -6
  18. package/src/bridge/stream/tracker.ts +7 -12
  19. package/src/bridge/supervision.ts +11 -26
  20. package/src/bridge/validation/base.ts +17 -39
  21. package/src/bridge/validation/jobs.ts +11 -25
  22. package/src/bridge/validation/negotiation.ts +27 -37
  23. package/src/bridge/validation/operations.ts +28 -52
  24. package/src/bridge/validation/streams.ts +3 -10
  25. package/src/commands/client.ts +21 -26
  26. package/src/commands/controller.ts +32 -47
  27. package/src/commands/generated/fields.ts +3 -2
  28. package/src/commands/generated/protocol.ts +4 -0
  29. package/src/commands/keyboard.ts +92 -89
  30. package/src/commands/ports.ts +6 -12
  31. package/src/commands/projectors.ts +50 -48
  32. package/src/commands/validation.ts +70 -97
  33. package/src/config/client.ts +6 -19
  34. package/src/config/generated/fields.ts +2 -2
  35. package/src/config/names.ts +6 -12
  36. package/src/config/validation/commands.ts +5 -17
  37. package/src/config/validation/outcomes.ts +31 -39
  38. package/src/config/validation/primitives.ts +36 -53
  39. package/src/config/validation/projection.ts +66 -94
  40. package/src/core/connection.ts +70 -61
  41. package/src/core/record.ts +4 -0
  42. package/src/core/transport.ts +4 -15
  43. package/src/core/wire-copy.ts +6 -0
  44. package/src/history/client.ts +6 -12
  45. package/src/history/controller.ts +46 -46
  46. package/src/history/direct.ts +1 -3
  47. package/src/history/generated/fields.ts +2 -2
  48. package/src/history/ports.ts +4 -6
  49. package/src/history/serialized.ts +9 -13
  50. package/src/history/validation.ts +71 -55
  51. package/src/history-tree/client.ts +87 -11
  52. package/src/history-tree/controller.ts +313 -70
  53. package/src/history-tree/direct.ts +3 -1
  54. package/src/history-tree/generated/fields.ts +2 -2
  55. package/src/history-tree/ports.ts +3 -1
  56. package/src/history-tree/serialized.ts +39 -11
  57. package/src/history-tree/validation.ts +262 -63
  58. package/src/layout/generated/protocol.ts +2 -2
  59. package/src/layout/ratio.ts +2 -8
  60. package/src/layout/validation.ts +10 -19
  61. package/src/layout/visibility.ts +1 -2
  62. package/src/licence/client.ts +44 -7
  63. package/src/licence/controller.ts +76 -40
  64. package/src/licence/direct.ts +3 -1
  65. package/src/licence/generated/fields.ts +2 -2
  66. package/src/licence/generated/key-conformance.json +28 -0
  67. package/src/licence/generated/protocol.ts +1 -1
  68. package/src/licence/key.ts +40 -12
  69. package/src/licence/ports.ts +3 -1
  70. package/src/licence/serialized.ts +32 -9
  71. package/src/licence/validation.ts +120 -36
  72. package/src/native-content/client.ts +178 -236
  73. package/src/native-content/generated/fields.ts +2 -2
  74. package/src/native-content/ports.ts +9 -15
  75. package/src/native-content/serialized.ts +12 -19
  76. package/src/native-content/validation/base.ts +57 -50
  77. package/src/native-content/validation/messages.ts +21 -35
  78. package/src/native-content/validation/state.ts +31 -33
  79. package/src/notifications/client.ts +87 -31
  80. package/src/notifications/controller.ts +215 -61
  81. package/src/notifications/direct.ts +3 -1
  82. package/src/notifications/generated/fields.ts +2 -2
  83. package/src/notifications/ports.ts +7 -3
  84. package/src/notifications/serialized.ts +11 -9
  85. package/src/notifications/validation.ts +147 -43
  86. package/src/operation/bridge.ts +117 -50
  87. package/src/operation/client.ts +38 -24
  88. package/src/operation/controller.ts +33 -42
  89. package/src/operation/direct.ts +1 -3
  90. package/src/operation/generated/fields.ts +2 -2
  91. package/src/operation/ports.ts +10 -9
  92. package/src/operation/serialized.ts +8 -9
  93. package/src/operation/validation.ts +152 -65
  94. package/src/settings/client.ts +11 -44
  95. package/src/settings/connection.ts +26 -40
  96. package/src/settings/generated/fields.ts +3 -3
  97. package/src/settings/generated/protocol.ts +9 -4
  98. package/src/settings/names.ts +2 -4
  99. package/src/settings/registry.ts +23 -25
  100. package/src/settings/validation/authority.ts +19 -17
  101. package/src/settings/validation/command.ts +17 -37
  102. package/src/settings/validation/error.ts +2 -8
  103. package/src/settings/validation/primitives.ts +26 -42
  104. package/src/settings/validation/projection.ts +8 -24
  105. package/src/settings/validation/registry.ts +25 -73
  106. package/src/surface-transfer/client.ts +6 -18
  107. package/src/surface-transfer/generated/fields.ts +2 -2
  108. package/src/surface-transfer/validation.ts +31 -49
  109. package/src/surfaces/client.ts +12 -22
  110. package/src/surfaces/generated/fields.ts +2 -2
  111. package/src/surfaces/validation.ts +40 -49
  112. package/src/transfer/client.ts +26 -60
  113. package/src/transfer/connection.ts +12 -20
  114. package/src/transfer/generated/fields.ts +5 -5
  115. package/src/transfer/generated/protocol.ts +4 -4
  116. package/src/transfer/payload.ts +13 -31
  117. package/src/transfer/validation.ts +39 -75
  118. package/src/update/client.ts +59 -9
  119. package/src/update/controller.ts +113 -48
  120. package/src/update/direct.ts +3 -1
  121. package/src/update/generated/fields.ts +3 -3
  122. package/src/update/generated/protocol.ts +7 -3
  123. package/src/update/generated/variant-fields.ts +1 -0
  124. package/src/update/ports.ts +6 -2
  125. package/src/update/serialized.ts +34 -10
  126. package/src/update/validation.ts +115 -35
@@ -1,3 +1,4 @@
1
+ import { isRecord } from "../core/record.ts";
1
2
  import {
2
3
  NOTIFICATION_VARIANT_FIELDS,
3
4
  NOTIFICATION_VARIANT_FIELDS_DISCRIMINANTS,
@@ -12,23 +13,30 @@ import {
12
13
  NOTIFICATION_REJECTION_CODES,
13
14
  NOTIFICATION_RETENTION_CLASSES,
14
15
  NOTIFICATION_SEVERITIES,
15
- type NotificationChangedEvent,
16
- type NotificationMutationCommand,
17
- type NotificationMutationResult,
18
- type NotificationRecordProjection,
19
- type NotificationSnapshot,
20
- type NotificationSnapshotQuery,
21
- type NotificationSnapshotResponse,
16
+ } from "./generated/protocol.ts";
17
+ import type {
18
+ NotificationChangedEvent,
19
+ NotificationMutationCommand,
20
+ NotificationMutationResult,
21
+ NotificationRecordProjection,
22
+ NotificationSnapshot,
23
+ NotificationSnapshotQuery,
24
+ NotificationSnapshotResponse,
22
25
  } from "./generated/protocol.ts";
23
26
 
24
27
  export class NotificationProtocolValidationError extends Error {
25
- constructor(readonly path: string, message: string) {
28
+ constructor(
29
+ readonly path: string,
30
+ message: string,
31
+ ) {
26
32
  super(`incompatible notification protocol at ${path}: ${message}`);
27
33
  this.name = "NotificationProtocolValidationError";
28
34
  }
29
35
  }
30
36
 
31
- export function assertValidNotificationSnapshotQuery(value: unknown): asserts value is NotificationSnapshotQuery {
37
+ export function assertValidNotificationSnapshotQuery(
38
+ value: unknown,
39
+ ): asserts value is NotificationSnapshotQuery {
32
40
  const object = exact(value, "$", NOTIFICATIONS_FIELDS.NotificationSnapshotQuery);
33
41
  protocol(object.protocolVersion, "$.protocolVersion");
34
42
  text(object.requestId, "$.requestId");
@@ -36,13 +44,17 @@ export function assertValidNotificationSnapshotQuery(value: unknown): asserts va
36
44
  natural(object.limit, "$.limit");
37
45
  }
38
46
 
39
- export function assertValidNotificationSnapshotResponse(value: unknown): asserts value is NotificationSnapshotResponse {
47
+ export function assertValidNotificationSnapshotResponse(
48
+ value: unknown,
49
+ ): asserts value is NotificationSnapshotResponse {
40
50
  const object = exact(value, "$", NOTIFICATIONS_FIELDS.NotificationSnapshotResponse);
41
51
  text(object.requestId, "$.requestId");
42
52
  assertValidNotificationSnapshot(object.snapshot);
43
53
  }
44
54
 
45
- export function assertValidNotificationSnapshot(value: unknown): asserts value is NotificationSnapshot {
55
+ export function assertValidNotificationSnapshot(
56
+ value: unknown,
57
+ ): asserts value is NotificationSnapshot {
46
58
  const object = exact(value, "$", NOTIFICATIONS_FIELDS.NotificationSnapshot);
47
59
  protocol(object.protocolVersion, "$.protocolVersion");
48
60
  authority(object.authority, "$.authority");
@@ -56,10 +68,14 @@ export function assertValidNotificationSnapshot(value: unknown): asserts value i
56
68
  natural(page.offset, "$.page.offset");
57
69
  natural(page.totalCount, "$.page.totalCount");
58
70
  boolean(page.hasMore, "$.page.hasMore");
59
- array(page.records, "$.page.records").forEach((item, index) => recordProjection(item, `$.page.records[${index}]`));
71
+ array(page.records, "$.page.records").forEach((item, index) =>
72
+ recordProjection(item, `$.page.records[${index}]`),
73
+ );
60
74
  }
61
75
 
62
- export function assertValidNotificationMutationCommand(value: unknown): asserts value is NotificationMutationCommand {
76
+ export function assertValidNotificationMutationCommand(
77
+ value: unknown,
78
+ ): asserts value is NotificationMutationCommand {
63
79
  const object = record(value, "$");
64
80
  member(object.kind, NOTIFICATION_MUTATION_KINDS, "$.kind");
65
81
  protocol(object.protocolVersion, "$.protocolVersion");
@@ -86,8 +102,16 @@ export function assertValidNotificationMutationCommand(value: unknown): asserts
86
102
  exactKeys(object, "$", variantKeys("NotificationMutationCommand", object, "$"));
87
103
  const target = record(object.target, "$.target");
88
104
  member(target.kind, ["all", "records"] as const, "$.target.kind");
89
- exactKeys(target, "$.target", variantKeys("NotificationClearTargetProjection", target, "$.target"));
90
- if (target.kind === "records") array(target.notificationIds, "$.target.notificationIds").forEach((id, index) => text(id, `$.target.notificationIds[${index}]`));
105
+ exactKeys(
106
+ target,
107
+ "$.target",
108
+ variantKeys("NotificationClearTargetProjection", target, "$.target"),
109
+ );
110
+ if (target.kind === "records") {
111
+ array(target.notificationIds, "$.target.notificationIds").forEach((id, index) =>
112
+ text(id, `$.target.notificationIds[${index}]`),
113
+ );
114
+ }
91
115
  break;
92
116
  }
93
117
  case "changeRetention":
@@ -97,7 +121,9 @@ export function assertValidNotificationMutationCommand(value: unknown): asserts
97
121
  }
98
122
  }
99
123
 
100
- export function assertValidNotificationMutationResult(value: unknown): asserts value is NotificationMutationResult {
124
+ export function assertValidNotificationMutationResult(
125
+ value: unknown,
126
+ ): asserts value is NotificationMutationResult {
101
127
  const object = record(value, "$");
102
128
  member(object.status, NOTIFICATION_MUTATION_STATUSES, "$.status");
103
129
  text(object.requestId, "$.requestId");
@@ -107,21 +133,29 @@ export function assertValidNotificationMutationResult(value: unknown): asserts v
107
133
  receipt(object.receipt, "$.receipt");
108
134
  } else {
109
135
  exactKeys(object, "$", variantKeys("NotificationMutationResult", object, "$"));
110
- const rejection = exact(object.rejection, "$.rejection", NOTIFICATIONS_FIELDS.NotificationRejection);
136
+ const rejection = exact(
137
+ object.rejection,
138
+ "$.rejection",
139
+ NOTIFICATIONS_FIELDS.NotificationRejection,
140
+ );
111
141
  member(rejection.code, NOTIFICATION_REJECTION_CODES, "$.rejection.code");
112
142
  text(rejection.detail, "$.rejection.detail");
113
143
  boolean(rejection.refreshRequired, "$.rejection.refreshRequired");
114
144
  }
115
145
  }
116
146
 
117
- export function assertValidNotificationChangedEvent(value: unknown): asserts value is NotificationChangedEvent {
147
+ export function assertValidNotificationChangedEvent(
148
+ value: unknown,
149
+ ): asserts value is NotificationChangedEvent {
118
150
  const object = exact(value, "$", NOTIFICATIONS_FIELDS.NotificationChangedEvent);
119
151
  protocol(object.protocolVersion, "$.protocolVersion");
120
152
  text(object.requestId, "$.requestId");
121
153
  authority(object.authority, "$.authority");
122
154
  natural(object.previousLedgerRevision, "$.previousLedgerRevision");
123
155
  natural(object.committedLedgerRevision, "$.committedLedgerRevision");
124
- array(object.affectedNotificationIds, "$.affectedNotificationIds").forEach((id, index) => text(id, `$.affectedNotificationIds[${index}]`));
156
+ array(object.affectedNotificationIds, "$.affectedNotificationIds").forEach((id, index) =>
157
+ text(id, `$.affectedNotificationIds[${index}]`),
158
+ );
125
159
  member(object.kind, NOTIFICATION_CHANGED_KINDS, "$.kind");
126
160
  }
127
161
 
@@ -145,17 +179,26 @@ function draft(value: unknown, path: string): void {
145
179
  text(object.summary, `${path}.summary`);
146
180
  nullableText(object.causeId, `${path}.causeId`);
147
181
  array(object.actions, `${path}.actions`).forEach((action, index) => {
148
- const item = exact(action, `${path}.actions[${index}]`, NOTIFICATIONS_FIELDS.NotificationActionProjection);
182
+ const item = exact(
183
+ action,
184
+ `${path}.actions[${index}]`,
185
+ NOTIFICATIONS_FIELDS.NotificationActionProjection,
186
+ );
149
187
  text(item.referenceId, `${path}.actions[${index}].referenceId`);
150
188
  text(item.label, `${path}.actions[${index}].label`);
151
189
  });
152
190
  nullableText(object.replacementKey, `${path}.replacementKey`);
153
191
  nullableText(object.producerToken, `${path}.producerToken`);
154
192
  member(object.retentionClass, NOTIFICATION_RETENTION_CLASSES, `${path}.retentionClass`);
155
- if (object.presentationTimeUnixMs !== null) integer(object.presentationTimeUnixMs, `${path}.presentationTimeUnixMs`);
193
+ if (object.presentationTimeUnixMs !== null) {
194
+ integer(object.presentationTimeUnixMs, `${path}.presentationTimeUnixMs`);
195
+ }
156
196
  }
157
197
 
158
- function recordProjection(value: unknown, path: string): asserts value is NotificationRecordProjection {
198
+ function recordProjection(
199
+ value: unknown,
200
+ path: string,
201
+ ): asserts value is NotificationRecordProjection {
159
202
  const object = exact(value, path, NOTIFICATIONS_FIELDS.NotificationRecordProjection);
160
203
  text(object.notificationId, `${path}.notificationId`);
161
204
  draft(object.draft, `${path}.draft`);
@@ -167,7 +210,11 @@ function recordProjection(value: unknown, path: string): asserts value is Notifi
167
210
 
168
211
  function receipt(value: unknown, path: string): void {
169
212
  const object = record(value, path);
170
- member(object.kind, ["added", "replaced", "seen", "removed", "retentionChanged"] as const, `${path}.kind`);
213
+ member(
214
+ object.kind,
215
+ ["added", "replaced", "seen", "removed", "retentionChanged"] as const,
216
+ `${path}.kind`,
217
+ );
171
218
  natural(object.previousLedgerRevision, `${path}.previousLedgerRevision`);
172
219
  natural(object.committedLedgerRevision, `${path}.committedLedgerRevision`);
173
220
  if (object.kind === "added" || object.kind === "replaced") {
@@ -190,46 +237,94 @@ function receipt(value: unknown, path: string): void {
190
237
 
191
238
  function removals(value: unknown, path: string): void {
192
239
  array(value, path).forEach((removal, index) => {
193
- const item = exact(removal, `${path}[${index}]`, NOTIFICATIONS_FIELDS.NotificationRemovalProjection);
240
+ const item = exact(
241
+ removal,
242
+ `${path}[${index}]`,
243
+ NOTIFICATIONS_FIELDS.NotificationRemovalProjection,
244
+ );
194
245
  recordProjection(item.record, `${path}[${index}].record`);
195
246
  member(item.reason, ["dismissed", "cleared", "pruned"] as const, `${path}[${index}].reason`);
196
247
  });
197
248
  }
198
249
 
199
250
  function protocol(value: unknown, path: string): void {
200
- if (value !== NOTIFICATION_PROTOCOL_VERSION) fail(path, `expected protocol ${NOTIFICATION_PROTOCOL_VERSION}`);
251
+ if (value !== NOTIFICATION_PROTOCOL_VERSION) {
252
+ fail(path, `expected protocol ${NOTIFICATION_PROTOCOL_VERSION}`);
253
+ }
201
254
  }
202
255
 
203
256
  function record(value: unknown, path: string): Record<string, unknown> {
204
- if (typeof value !== "object" || value === null || Array.isArray(value)) fail(path, "expected object");
205
- return value as Record<string, unknown>;
257
+ if (!isRecord(value)) {
258
+ fail(path, "expected object");
259
+ }
260
+ return value;
206
261
  }
207
262
 
208
- function exact(value: unknown, path: string, keys: readonly string[]): Record<string, unknown> {
263
+ function exact(value: unknown, path: string, keys: ReadonlyArray<string>): Record<string, unknown> {
209
264
  const object = record(value, path);
210
265
  exactKeys(object, path, keys);
211
266
  return object;
212
267
  }
213
268
 
214
- function exactKeys(object: Record<string, unknown>, path: string, keys: readonly string[]): void {
269
+ function exactKeys(
270
+ object: Record<string, unknown>,
271
+ path: string,
272
+ keys: ReadonlyArray<string>,
273
+ ): void {
215
274
  const actual = Object.keys(object).sort();
216
275
  const expected = [...keys].sort();
217
- if (actual.length !== expected.length || actual.some((key, index) => key !== expected[index])) fail(path, `expected keys ${expected.join(", ")}`);
276
+ if (actual.length !== expected.length || actual.some((key, index) => key !== expected[index])) {
277
+ fail(path, `expected keys ${expected.join(", ")}`);
278
+ }
218
279
  }
219
280
 
220
- function array(value: unknown, path: string): unknown[] {
221
- if (!Array.isArray(value)) fail(path, "expected array");
281
+ function array(value: unknown, path: string): Array<unknown> {
282
+ if (!Array.isArray(value)) {
283
+ fail(path, "expected array");
284
+ }
222
285
  return value;
223
286
  }
224
287
 
225
- function text(value: unknown, path: string): void { if (typeof value !== "string" || value.length === 0) fail(path, "expected nonempty string"); }
226
- function nullableText(value: unknown, path: string): void { if (value !== null) text(value, path); }
227
- function boolean(value: unknown, path: string): void { if (typeof value !== "boolean") fail(path, "expected boolean"); }
228
- function integer(value: unknown, path: string): void { if (typeof value !== "number" || !Number.isSafeInteger(value)) fail(path, "expected safe integer"); }
229
- function natural(value: unknown, path: string): void { integer(value, path); if ((value as number) < 0) fail(path, "expected nonnegative integer"); }
230
- function positive(value: unknown, path: string): void { natural(value, path); if ((value as number) === 0) fail(path, "expected positive integer"); }
231
- function member(value: unknown, members: readonly string[], path: string): void { if (typeof value !== "string" || !members.includes(value)) fail(path, `expected one of ${members.join(", ")}`); }
232
- function fail(path: string, message: string): never { throw new NotificationProtocolValidationError(path, message); }
288
+ function text(value: unknown, path: string): void {
289
+ if (typeof value !== "string" || value.length === 0) {
290
+ fail(path, "expected nonempty string");
291
+ }
292
+ }
293
+ function nullableText(value: unknown, path: string): void {
294
+ if (value !== null) {
295
+ text(value, path);
296
+ }
297
+ }
298
+ function boolean(value: unknown, path: string): void {
299
+ if (typeof value !== "boolean") {
300
+ fail(path, "expected boolean");
301
+ }
302
+ }
303
+ function integer(value: unknown, path: string): asserts value is number {
304
+ if (typeof value !== "number" || !Number.isSafeInteger(value)) {
305
+ fail(path, "expected safe integer");
306
+ }
307
+ }
308
+ function natural(value: unknown, path: string): asserts value is number {
309
+ integer(value, path);
310
+ if (value < 0) {
311
+ fail(path, "expected nonnegative integer");
312
+ }
313
+ }
314
+ function positive(value: unknown, path: string): void {
315
+ natural(value, path);
316
+ if (value === 0) {
317
+ fail(path, "expected positive integer");
318
+ }
319
+ }
320
+ function member(value: unknown, members: ReadonlyArray<string>, path: string): void {
321
+ if (typeof value !== "string" || !members.includes(value)) {
322
+ fail(path, `expected one of ${members.join(", ")}`);
323
+ }
324
+ }
325
+ function fail(path: string, message: string): never {
326
+ throw new NotificationProtocolValidationError(path, message);
327
+ }
233
328
 
234
329
  /**
235
330
  * Allowed keys for one tagged-union variant, from the generated map, with the
@@ -238,9 +333,18 @@ function fail(path: string, message: string): never { throw new NotificationProt
238
333
  * A missing entry means the generator failed to read the union: every caller
239
334
  * checks the discriminant above this call.
240
335
  */
241
- function variantKeys(type: string, object: Record<string, unknown>, path: string): readonly string[] {
336
+ function variantKeys(
337
+ type: string,
338
+ object: Record<string, unknown>,
339
+ path: string,
340
+ ): ReadonlyArray<string> {
242
341
  const discriminant = object[NOTIFICATION_VARIANT_FIELDS_DISCRIMINANTS[type] ?? "kind"];
243
- const keys = NOTIFICATION_VARIANT_FIELDS[type]?.[discriminant as string];
244
- if (keys === undefined) fail(path, `no generated fields for ${type}.${String(discriminant)}`);
342
+ const keys =
343
+ typeof discriminant === "string"
344
+ ? NOTIFICATION_VARIANT_FIELDS[type]?.[discriminant]
345
+ : undefined;
346
+ if (keys === undefined) {
347
+ fail(path, `no generated fields for ${type}.${String(discriminant)}`);
348
+ }
245
349
  return keys;
246
350
  }
@@ -1,17 +1,24 @@
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
+ parseBridgeQueryEnvelope,
7
+ parseBridgeQueryReply,
8
+ } from "@inflatable-cookie/longhorn/bridge";
9
+ import type {
10
+ BridgeCommandEnvelope,
11
+ BridgeCommandReply,
12
+ BridgeDomainClient,
13
+ BridgeJobId,
14
+ BridgeJobTerminalEvent,
15
+ BridgeOperationDescriptor,
16
+ BridgeProgressEvent,
17
+ BridgeQueryEnvelope,
18
+ BridgeQueryReply,
19
+ BridgeRequestContext,
20
+ BridgeRequestId,
21
+ DomainId,
15
22
  } from "@inflatable-cookie/longhorn/bridge";
16
23
 
17
24
  import {
@@ -63,15 +70,21 @@ async function executeQuery(options: BridgeOperationPortOptions, query: Operatio
63
70
  return reply.outcome.success;
64
71
  }
65
72
 
66
- async function executeCommand(options: BridgeOperationPortOptions, command: OperationMutationCommand) {
73
+ async function executeCommand(
74
+ options: BridgeOperationPortOptions,
75
+ command: OperationMutationCommand,
76
+ ) {
67
77
  const context = nextContext(options);
68
78
  const descriptor = commandDescriptor(options.domainId);
69
79
  const reply = await options.client.execute(descriptor, {
70
80
  context,
71
81
  authorityEpoch: options.authorityEpoch,
72
- expectedRevision: command.kind === "register" || command.kind === "changeRetention" || command.kind === "teardown"
73
- ? command.expectedCatalogueRevision
74
- : null,
82
+ expectedRevision:
83
+ command.kind === "register" ||
84
+ command.kind === "changeRetention" ||
85
+ command.kind === "teardown"
86
+ ? command.expectedCatalogueRevision
87
+ : null,
75
88
  idempotencyKey: context.requestId,
76
89
  payload: command,
77
90
  });
@@ -81,7 +94,10 @@ async function executeCommand(options: BridgeOperationPortOptions, command: Oper
81
94
  return reply.outcome.applied;
82
95
  }
83
96
 
84
- async function executeCancellation(options: BridgeOperationPortOptions, command: OperationCancellationCommand) {
97
+ async function executeCancellation(
98
+ options: BridgeOperationPortOptions,
99
+ command: OperationCancellationCommand,
100
+ ) {
85
101
  const context = nextContext(options);
86
102
  const descriptor = cancellationDescriptor(options.domainId);
87
103
  const reply = await options.client.execute(descriptor, { context, payload: command });
@@ -92,49 +108,84 @@ async function executeCancellation(options: BridgeOperationPortOptions, command:
92
108
  }
93
109
 
94
110
  function nextContext(options: BridgeOperationPortOptions): BridgeRequestContext {
95
- return { requestId: options.nextBridgeRequestId(), sessionId: options.sessionId, domainId: options.domainId };
111
+ return {
112
+ requestId: options.nextBridgeRequestId(),
113
+ sessionId: options.sessionId,
114
+ domainId: options.domainId,
115
+ };
96
116
  }
97
117
 
98
- function codec<Value>(assertValue: (value: unknown) => void) {
99
- return { parse(value: unknown): Value { assertValue(value); return value as Value; } };
118
+ function codec<Value>(assertValue: (value: unknown) => asserts value is Value) {
119
+ return bridgeCodec((value) => {
120
+ assertValue(value);
121
+ return value;
122
+ });
100
123
  }
101
124
 
102
- function queryDescriptor(domainId: DomainId): BridgeOperationDescriptor<BridgeQueryEnvelope<OperationSnapshotQuery>, BridgeQueryReply<OperationSnapshotResponse, null>> {
125
+ const noDetails = bridgeCodec((value): null => {
126
+ if (value !== null) {
127
+ throw new Error("operation bridge failure details must be null");
128
+ }
129
+ return null;
130
+ });
131
+
132
+ function queryDescriptor(
133
+ domainId: DomainId,
134
+ ): BridgeOperationDescriptor<
135
+ BridgeQueryEnvelope<OperationSnapshotQuery>,
136
+ BridgeQueryReply<OperationSnapshotResponse, null>
137
+ > {
103
138
  return {
104
- route: "longhorn.operation.snapshot", domainId, kind: "query",
139
+ route: "longhorn.operation.snapshot",
140
+ domainId,
141
+ kind: "query",
105
142
  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
- }),
143
+ request: bridgeCodec((value) =>
144
+ parseBridgeQueryEnvelope(value, codec(assertValidOperationSnapshotQuery)),
145
+ ),
146
+ reply: bridgeCodec((value) =>
147
+ parseBridgeQueryReply(value, codec(assertValidOperationSnapshotResponse), noDetails),
148
+ ),
114
149
  };
115
150
  }
116
151
 
117
- function commandDescriptor(domainId: DomainId): BridgeOperationDescriptor<BridgeCommandEnvelope<OperationMutationCommand>, BridgeCommandReply<OperationMutationResult, null>> {
152
+ function commandDescriptor(
153
+ domainId: DomainId,
154
+ ): BridgeOperationDescriptor<
155
+ BridgeCommandEnvelope<OperationMutationCommand>,
156
+ BridgeCommandReply<OperationMutationResult, null>
157
+ > {
118
158
  return {
119
- route: "longhorn.operation.mutate", domainId, kind: "command",
159
+ route: "longhorn.operation.mutate",
160
+ domainId,
161
+ kind: "command",
120
162
  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
- }),
163
+ request: bridgeCodec((value) =>
164
+ parseBridgeCommandEnvelope(value, codec(assertValidOperationMutationCommand)),
165
+ ),
166
+ reply: bridgeCodec((value) =>
167
+ parseBridgeCommandReply(value, codec(assertValidOperationMutationResult), noDetails),
168
+ ),
126
169
  };
127
170
  }
128
171
 
129
- function cancellationDescriptor(domainId: DomainId): BridgeOperationDescriptor<BridgeQueryEnvelope<OperationCancellationCommand>, BridgeQueryReply<OperationCancellationResult, null>> {
172
+ function cancellationDescriptor(
173
+ domainId: DomainId,
174
+ ): BridgeOperationDescriptor<
175
+ BridgeQueryEnvelope<OperationCancellationCommand>,
176
+ BridgeQueryReply<OperationCancellationResult, null>
177
+ > {
130
178
  return {
131
- route: "longhorn.operation.cancel", domainId, kind: "cancellation",
179
+ route: "longhorn.operation.cancel",
180
+ domainId,
181
+ kind: "cancellation",
132
182
  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
- }),
183
+ request: bridgeCodec((value) =>
184
+ parseBridgeQueryEnvelope(value, codec(assertValidOperationCancellationCommand)),
185
+ ),
186
+ reply: bridgeCodec((value) =>
187
+ parseBridgeQueryReply(value, codec(assertValidOperationCancellationResult), noDetails),
188
+ ),
138
189
  };
139
190
  }
140
191
 
@@ -153,16 +204,32 @@ export class BridgeOperationJobCorrelation {
153
204
  this.#operationId = correlation.operationId;
154
205
  }
155
206
 
156
- acceptProgress(event: BridgeProgressEvent<OperationChangedEvent>): OperationChangedEvent | undefined {
157
- if (this.#tracker.classifyProgress(event) !== "accept") return undefined;
207
+ acceptProgress(
208
+ event: BridgeProgressEvent<OperationChangedEvent>,
209
+ ): OperationChangedEvent | undefined {
210
+ if (this.#tracker.classifyProgress(event) !== "accept") {
211
+ return undefined;
212
+ }
158
213
  assertValidOperationChangedEvent(event.progress);
159
214
  return event.progress.operationId === this.#operationId ? event.progress : undefined;
160
215
  }
161
216
 
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;
217
+ acceptTerminal(
218
+ event: BridgeJobTerminalEvent<OperationChangedEvent, null>,
219
+ ): OperationChangedEvent | undefined {
220
+ if (this.#tracker.classifyTerminal(event) !== "accept") {
221
+ return undefined;
222
+ }
223
+ if (
224
+ typeof event.outcome !== "object" ||
225
+ event.outcome === null ||
226
+ !("succeeded" in event.outcome)
227
+ ) {
228
+ return undefined;
229
+ }
165
230
  assertValidOperationChangedEvent(event.outcome.succeeded);
166
- return event.outcome.succeeded.operationId === this.#operationId ? event.outcome.succeeded : undefined;
231
+ return event.outcome.succeeded.operationId === this.#operationId
232
+ ? event.outcome.succeeded
233
+ : undefined;
167
234
  }
168
235
  }
@@ -1,7 +1,7 @@
1
- import {
2
- CheckedSnapshotConnection,
3
- type ConnectionFailure,
4
- type ConnectionFailureReporter,
1
+ import { CheckedSnapshotConnection } from "@inflatable-cookie/longhorn/core";
2
+ import type {
3
+ ConnectionFailure,
4
+ ConnectionFailureReporter,
5
5
  } from "@inflatable-cookie/longhorn/core";
6
6
 
7
7
  import {
@@ -12,22 +12,21 @@ import {
12
12
  assertValidOperationMutationResult,
13
13
  assertValidOperationSnapshotResponse,
14
14
  } 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,
15
+ import { OPERATION_PROTOCOL_VERSION } from "./generated/protocol.ts";
16
+ import type {
17
+ OperationCancellationCommand,
18
+ OperationCancellationResult,
19
+ OperationMutationCommand,
20
+ OperationMutationResult,
21
+ OperationSnapshot,
22
+ OperationSnapshotResponse,
24
23
  } from "./generated/protocol.ts";
25
24
  import type { OperationPort } from "./ports.ts";
26
25
 
27
26
  export interface OperationSubscription {
28
27
  readonly ready: Promise<void>;
29
28
  current(): OperationSnapshot | undefined;
30
- failures(): readonly ConnectionFailure[];
29
+ failures(): ReadonlyArray<ConnectionFailure>;
31
30
  dispose(): Promise<void>;
32
31
  }
33
32
 
@@ -78,14 +77,21 @@ export class OperationClient {
78
77
  }
79
78
 
80
79
  export class OperationResponseCorrelationError extends Error {
81
- constructor(readonly expectedRequestId: string, readonly receivedRequestId: string) {
82
- super(`operation response correlation mismatch: expected ${expectedRequestId}; received ${receivedRequestId}`);
80
+ constructor(
81
+ readonly expectedRequestId: string,
82
+ readonly receivedRequestId: string,
83
+ ) {
84
+ super(
85
+ `operation response correlation mismatch: expected ${expectedRequestId}; received ${receivedRequestId}`,
86
+ );
83
87
  this.name = "OperationResponseCorrelationError";
84
88
  }
85
89
  }
86
90
 
87
91
  function assertResponseCorrelation(received: string, expected: string): void {
88
- if (received !== expected) throw new OperationResponseCorrelationError(expected, received);
92
+ if (received !== expected) {
93
+ throw new OperationResponseCorrelationError(expected, received);
94
+ }
89
95
  }
90
96
 
91
97
  class CheckedOperationSubscription implements OperationSubscription {
@@ -100,7 +106,9 @@ class CheckedOperationSubscription implements OperationSubscription {
100
106
  ) {
101
107
  this.#connection = new CheckedSnapshotConnection({
102
108
  listen: async (receive) => {
103
- if (port.listen === undefined) return () => {};
109
+ if (port.listen === undefined) {
110
+ return () => {};
111
+ }
104
112
  return port.listen(receive);
105
113
  },
106
114
  loadSnapshot: async () => (await client.snapshot()).snapshot,
@@ -117,7 +125,7 @@ class CheckedOperationSubscription implements OperationSubscription {
117
125
  return this.#connection.current();
118
126
  }
119
127
 
120
- failures(): readonly ConnectionFailure[] {
128
+ failures(): ReadonlyArray<ConnectionFailure> {
121
129
  return this.#connection.failures();
122
130
  }
123
131
 
@@ -129,7 +137,7 @@ class CheckedOperationSubscription implements OperationSubscription {
129
137
  function parseSnapshot(value: unknown): OperationSnapshot {
130
138
  const response = { requestId: "request:validation", snapshot: value };
131
139
  assertValidOperationSnapshotResponse(response);
132
- return value as OperationSnapshot;
140
+ return response.snapshot;
133
141
  }
134
142
 
135
143
  export function operationEventAction(
@@ -137,8 +145,10 @@ export function operationEventAction(
137
145
  current: OperationSnapshot | undefined,
138
146
  ): { kind: "ignore" } | { kind: "refresh" } {
139
147
  assertValidOperationChangedEvent(value);
140
- const event = value as OperationChangedEvent;
141
- if (current === undefined) return { kind: "refresh" };
148
+ const event = value;
149
+ if (current === undefined) {
150
+ return { kind: "refresh" };
151
+ }
142
152
  if (event.authority.authorityId !== current.authority.authorityId) {
143
153
  return { kind: "ignore" };
144
154
  }
@@ -158,8 +168,12 @@ export function isNewerOperationSnapshot(
158
168
  candidate: OperationSnapshot,
159
169
  current: OperationSnapshot | undefined,
160
170
  ): boolean {
161
- if (current === undefined) return true;
162
- if (candidate.authority.authorityId !== current.authority.authorityId) return false;
171
+ if (current === undefined) {
172
+ return true;
173
+ }
174
+ if (candidate.authority.authorityId !== current.authority.authorityId) {
175
+ return false;
176
+ }
163
177
  if (candidate.authority.authorityEpoch !== current.authority.authorityEpoch) {
164
178
  return candidate.authority.authorityEpoch > current.authority.authorityEpoch;
165
179
  }