@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,7 +1,5 @@
1
- import {
2
- BRIDGE_RETRY_CLASSES,
3
- type BridgeRetryClass,
4
- } from "../generated/protocol.ts";
1
+ import { BRIDGE_RETRY_CLASSES } from "../generated/protocol.ts";
2
+ import type { BridgeRetryClass } from "../generated/protocol.ts";
5
3
 
6
4
  export const MAXIMUM_BRIDGE_AUTOMATIC_RETRIES = 64;
7
5
 
@@ -22,10 +20,7 @@ export class BridgeRuntimeError extends Error {
22
20
  | "invalid_time"
23
21
  | "retry_not_due";
24
22
 
25
- constructor(
26
- code: BridgeRuntimeError["code"],
27
- detail: string,
28
- ) {
23
+ constructor(code: BridgeRuntimeError["code"], detail: string) {
29
24
  super(`bridge runtime: ${detail}`);
30
25
  this.name = "BridgeRuntimeError";
31
26
  this.code = code;
@@ -33,11 +28,7 @@ export class BridgeRuntimeError extends Error {
33
28
  }
34
29
 
35
30
  export function checkedRetryLimit(value: number): number {
36
- if (
37
- !Number.isSafeInteger(value) ||
38
- value < 0 ||
39
- value > MAXIMUM_BRIDGE_AUTOMATIC_RETRIES
40
- ) {
31
+ if (!Number.isSafeInteger(value) || value < 0 || value > MAXIMUM_BRIDGE_AUTOMATIC_RETRIES) {
41
32
  throw new BridgeRuntimeError(
42
33
  "invalid_retry_limit",
43
34
  `retry limit must be 0..${MAXIMUM_BRIDGE_AUTOMATIC_RETRIES}`,
@@ -46,24 +37,16 @@ export function checkedRetryLimit(value: number): number {
46
37
  return value;
47
38
  }
48
39
 
49
- export function checkedRetryClass(
50
- value: BridgeRetryClass,
51
- ): BridgeRetryClass {
40
+ export function checkedRetryClass(value: BridgeRetryClass): BridgeRetryClass {
52
41
  if (!BRIDGE_RETRY_CLASSES.includes(value)) {
53
- throw new BridgeRuntimeError(
54
- "invalid_retry_class",
55
- "unknown retry class",
56
- );
42
+ throw new BridgeRuntimeError("invalid_retry_class", "unknown retry class");
57
43
  }
58
44
  return value;
59
45
  }
60
46
 
61
47
  export function checkedMonotonic(value: number): number {
62
48
  if (!Number.isSafeInteger(value) || value < 0) {
63
- throw new BridgeRuntimeError(
64
- "invalid_time",
65
- "clock must return a nonnegative safe integer",
66
- );
49
+ throw new BridgeRuntimeError("invalid_time", "clock must return a nonnegative safe integer");
67
50
  }
68
51
  return value;
69
52
  }
@@ -80,10 +63,7 @@ export function checkedDeadline(at: number, delay: number): number {
80
63
  export function checkedIncrement(value: number): number {
81
64
  const next = value + 1;
82
65
  if (!Number.isSafeInteger(next)) {
83
- throw new BridgeRuntimeError(
84
- "invalid_time",
85
- "transition sequence exhausted",
86
- );
66
+ throw new BridgeRuntimeError("invalid_time", "transition sequence exhausted");
87
67
  }
88
68
  return next;
89
69
  }
@@ -8,9 +8,8 @@ import {
8
8
  checkedMonotonic,
9
9
  checkedRetryClass,
10
10
  checkedRetryLimit,
11
- type BridgeRuntimeBackoff,
12
- type BridgeRuntimeClock,
13
11
  } from "./policy.ts";
12
+ import type { BridgeRuntimeBackoff, BridgeRuntimeClock } from "./policy.ts";
14
13
 
15
14
  export class BridgeQueryRetryRuntime {
16
15
  readonly #clock: BridgeRuntimeClock;
@@ -18,11 +17,7 @@ export class BridgeQueryRetryRuntime {
18
17
  readonly #limit: number;
19
18
  #scheduled = 0;
20
19
 
21
- constructor(
22
- clock: BridgeRuntimeClock,
23
- backoff: BridgeRuntimeBackoff,
24
- limit: number,
25
- ) {
20
+ constructor(clock: BridgeRuntimeClock, backoff: BridgeRuntimeBackoff, limit: number) {
26
21
  this.#clock = clock;
27
22
  this.#backoff = backoff;
28
23
  this.#limit = checkedRetryLimit(limit);
@@ -32,23 +27,16 @@ export class BridgeQueryRetryRuntime {
32
27
  decision: BridgeQueryRetryDecision,
33
28
  retryClass: BridgeRetryClass,
34
29
  ): BridgeReconnectSchedule | undefined {
35
- retryClass = checkedRetryClass(retryClass);
36
- if (
37
- decision !== "retry" ||
38
- retryClass === "never" ||
39
- this.#scheduled >= this.#limit
40
- ) {
30
+ const checkedClass = checkedRetryClass(retryClass);
31
+ if (decision !== "retry" || checkedClass === "never" || this.#scheduled >= this.#limit) {
41
32
  return undefined;
42
33
  }
43
34
  const attempt = this.#scheduled + 1;
44
35
  const at = checkedMonotonic(this.#clock.now());
45
36
  const schedule = {
46
37
  attempt,
47
- retryClass,
48
- notBefore: checkedDeadline(
49
- at,
50
- this.#backoff.delay(retryClass, attempt),
51
- ),
38
+ retryClass: checkedClass,
39
+ notBefore: checkedDeadline(at, this.#backoff.delay(checkedClass, attempt)),
52
40
  };
53
41
  this.#scheduled = attempt;
54
42
  return schedule;
@@ -5,10 +5,7 @@ import type {
5
5
  DomainCapabilityDescriptor,
6
6
  DomainId,
7
7
  } from "./generated/protocol.ts";
8
- import {
9
- parseBridgeHelloRequest,
10
- parseBridgeNegotiationReceipt,
11
- } from "./validation.ts";
8
+ import { parseBridgeHelloRequest, parseBridgeNegotiationReceipt } from "./validation.ts";
12
9
 
13
10
  export interface BridgeNegotiationAdapter {
14
11
  negotiate(request: BridgeHelloRequest): Promise<unknown>;
@@ -39,16 +36,10 @@ export class BridgeSession {
39
36
  constructor(receipt: BridgeNegotiationReceipt) {
40
37
  this.receipt = parseBridgeNegotiationReceipt(receipt);
41
38
  this.#capabilities = new Map(
42
- this.receipt.domainCapabilities.map((value) => [
43
- value.domainId,
44
- value,
45
- ]),
39
+ this.receipt.domainCapabilities.map((value) => [value.domainId, value]),
46
40
  );
47
41
  this.#authorities = new Map(
48
- this.receipt.domainAuthorities.map((value) => [
49
- value.domainId,
50
- value,
51
- ]),
42
+ this.receipt.domainAuthorities.map((value) => [value.domainId, value]),
52
43
  );
53
44
  }
54
45
 
@@ -61,7 +52,6 @@ export class BridgeSession {
61
52
  }
62
53
 
63
54
  supports(domainId: DomainId, capability: string): boolean {
64
- return this.capability(domainId)?.capabilities.includes(capability) ??
65
- false;
55
+ return this.capability(domainId)?.capabilities.includes(capability) ?? false;
66
56
  }
67
57
  }
@@ -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 type {
@@ -12,10 +12,7 @@ import type {
12
12
  BridgeSessionId,
13
13
  } from "../generated/protocol.ts";
14
14
  import type { BridgeCodec } from "../validation/base.ts";
15
- import {
16
- parseBridgeEventEnvelope,
17
- parseBridgeSnapshotEnvelope,
18
- } from "../validation/streams.ts";
15
+ import { parseBridgeEventEnvelope, parseBridgeSnapshotEnvelope } from "../validation/streams.ts";
19
16
  import { BridgeStreamTracker, newer } from "./tracker.ts";
20
17
  import type { BridgeStreamSource } from "./source.ts";
21
18
 
@@ -25,27 +22,16 @@ export interface BridgeStreamConnectionOptions<Snapshot, Event> {
25
22
  readonly source: BridgeStreamSource;
26
23
  readonly snapshot: BridgeCodec<Snapshot>;
27
24
  readonly event: BridgeCodec<Event>;
28
- readonly apply: (
29
- current: Snapshot,
30
- event: Event,
31
- ) => Snapshot;
32
- readonly onSnapshot?: (
33
- snapshot: BridgeSnapshotEnvelope<Snapshot>,
34
- ) => void;
25
+ readonly apply: (current: Snapshot, event: Event) => Snapshot;
26
+ readonly onSnapshot?: (snapshot: BridgeSnapshotEnvelope<Snapshot>) => void;
35
27
  readonly onFailure?: ConnectionFailureReporter;
36
28
  }
37
29
 
38
30
  export class BridgeStreamConnection<Snapshot> {
39
31
  readonly ready: Promise<BridgeSnapshotEnvelope<Snapshot> | undefined>;
40
- readonly #connection: CheckedSnapshotConnection<
41
- BridgeSnapshotEnvelope<Snapshot>
42
- >;
32
+ readonly #connection: CheckedSnapshotConnection<BridgeSnapshotEnvelope<Snapshot>>;
43
33
 
44
- constructor(
45
- connection: CheckedSnapshotConnection<
46
- BridgeSnapshotEnvelope<Snapshot>
47
- >,
48
- ) {
34
+ constructor(connection: CheckedSnapshotConnection<BridgeSnapshotEnvelope<Snapshot>>) {
49
35
  this.#connection = connection;
50
36
  this.ready = connection.ready;
51
37
  }
@@ -54,7 +40,7 @@ export class BridgeStreamConnection<Snapshot> {
54
40
  return this.#connection.current();
55
41
  }
56
42
 
57
- failures(): readonly ConnectionFailure[] {
43
+ failures(): ReadonlyArray<ConnectionFailure> {
58
44
  return this.#connection.failures();
59
45
  }
60
46
 
@@ -66,22 +52,14 @@ export class BridgeStreamConnection<Snapshot> {
66
52
  export function connectBridgeStream<Snapshot, Event>(
67
53
  options: BridgeStreamConnectionOptions<Snapshot, Event>,
68
54
  ): BridgeStreamConnection<Snapshot> {
69
- const tracker = new BridgeStreamTracker(
70
- options.sessionId,
71
- options.domainId,
72
- );
73
- const connection = new CheckedSnapshotConnection<
74
- BridgeSnapshotEnvelope<Snapshot>
75
- >({
55
+ const tracker = new BridgeStreamTracker(options.sessionId, options.domainId);
56
+ const connection = new CheckedSnapshotConnection<BridgeSnapshotEnvelope<Snapshot>>({
76
57
  listen: (listener) => options.source.listen(listener),
77
58
  loadSnapshot: () => options.source.loadSnapshot(),
78
59
  validateSnapshot: (value) => {
79
60
  const snapshot = parseBridgeSnapshotEnvelope(value, options.snapshot);
80
61
  const decision = tracker.acceptSnapshot(snapshot.cursor);
81
- if (
82
- decision === "supersededSession" ||
83
- decision === "wrongDomain"
84
- ) {
62
+ if (decision === "supersededSession" || decision === "wrongDomain") {
85
63
  throw new BridgeStreamAuthorityError(decision);
86
64
  }
87
65
  return snapshot;
@@ -118,9 +96,9 @@ function eventAction<Snapshot, Event>(
118
96
  | { readonly kind: "ignore" }
119
97
  | { readonly kind: "refresh" }
120
98
  | {
121
- readonly kind: "snapshot";
122
- readonly value: BridgeSnapshotEnvelope<Snapshot>;
123
- } {
99
+ readonly kind: "snapshot";
100
+ readonly value: BridgeSnapshotEnvelope<Snapshot>;
101
+ } {
124
102
  switch (decision) {
125
103
  case "apply":
126
104
  if (current === undefined) {
@@ -1,9 +1,6 @@
1
1
  import type { Unlisten } from "@inflatable-cookie/longhorn/core";
2
2
 
3
- import {
4
- deserialize,
5
- serialize,
6
- } from "../loopback.ts";
3
+ import { deserialize, serialize } from "../loopback.ts";
7
4
 
8
5
  export interface BridgeStreamSource {
9
6
  listen(listener: (value: unknown) => void): Promise<Unlisten>;
@@ -40,8 +37,7 @@ export class DirectBridgeStreamSource implements BridgeStreamSource {
40
37
  }
41
38
  }
42
39
 
43
- export class SerializedLoopbackBridgeStreamSource
44
- implements BridgeStreamSource {
40
+ export class SerializedLoopbackBridgeStreamSource implements BridgeStreamSource {
45
41
  readonly #source: BridgeStreamSource;
46
42
 
47
43
  constructor(source: BridgeStreamSource) {
@@ -34,14 +34,11 @@ export class BridgeStreamTracker {
34
34
  if (cursor.domainId !== this.#domainId) {
35
35
  return "wrongDomain";
36
36
  }
37
- const pendingIsNewer = this.#pending !== undefined &&
38
- newer(this.#pending, cursor);
37
+ const pendingIsNewer = this.#pending !== undefined && newer(this.#pending, cursor);
39
38
  this.#accepted = cursor;
40
39
  this.#pending = undefined;
41
40
  this.#requiresSnapshot = pendingIsNewer;
42
- return pendingIsNewer
43
- ? "acceptedResnapshotRequired"
44
- : "accepted";
41
+ return pendingIsNewer ? "acceptedResnapshotRequired" : "accepted";
45
42
  }
46
43
 
47
44
  classifyEvent(cursor: BridgeStreamCursor): BridgeStreamDecision {
@@ -99,11 +96,9 @@ export class BridgeStreamTracker {
99
96
  }
100
97
  }
101
98
 
102
- export function newer(
103
- candidate: BridgeStreamCursor,
104
- current: BridgeStreamCursor,
105
- ): boolean {
106
- return candidate.authorityEpoch > current.authorityEpoch ||
107
- (candidate.authorityEpoch === current.authorityEpoch &&
108
- candidate.sequence > current.sequence);
99
+ export function newer(candidate: BridgeStreamCursor, current: BridgeStreamCursor): boolean {
100
+ return (
101
+ candidate.authorityEpoch > current.authorityEpoch ||
102
+ (candidate.authorityEpoch === current.authorityEpoch && candidate.sequence > current.sequence)
103
+ );
109
104
  }
@@ -12,9 +12,7 @@ import { BRIDGE_SERVICE_OWNERSHIPS } from "./generated/protocol.ts";
12
12
  import { opaqueId } from "./validation/base.ts";
13
13
 
14
14
  export interface BridgeServiceSupervisorPort {
15
- perform(
16
- request: BridgeServiceRequest,
17
- ): Promise<unknown> | unknown;
15
+ perform(request: BridgeServiceRequest): Promise<unknown> | unknown;
18
16
  }
19
17
 
20
18
  export class BridgeSupervisionError extends Error {
@@ -55,9 +53,7 @@ export class BridgeServiceRuntime {
55
53
  this.#admit(action);
56
54
  const request: BridgeServiceRequest = {
57
55
  action,
58
- credentialRef: credentialRef === undefined
59
- ? null
60
- : opaqueId(credentialRef),
56
+ credentialRef: credentialRef === undefined ? null : opaqueId(credentialRef),
61
57
  };
62
58
  let rawOutcome: unknown;
63
59
  try {
@@ -68,10 +64,7 @@ export class BridgeServiceRuntime {
68
64
  return this.observe(action, rawOutcome);
69
65
  }
70
66
 
71
- observe(
72
- action: BridgeServiceAction,
73
- value: unknown,
74
- ): BridgeServiceTransitionReceipt {
67
+ observe(action: BridgeServiceAction, value: unknown): BridgeServiceTransitionReceipt {
75
68
  this.#admit(action);
76
69
  const outcome = parseOutcome(value);
77
70
  const current = transitionState(action, outcome);
@@ -97,12 +90,10 @@ export class BridgeServiceRuntime {
97
90
  let admitted = false;
98
91
  switch (action) {
99
92
  case "spawn":
100
- admitted = owned &&
101
- ["absent", "stopped", "failed"].includes(this.#state);
93
+ admitted = owned && ["absent", "stopped", "failed"].includes(this.#state);
102
94
  break;
103
95
  case "attach":
104
- admitted = !owned &&
105
- ["absent", "stopped", "failed"].includes(this.#state);
96
+ admitted = !owned && ["absent", "stopped", "failed"].includes(this.#state);
106
97
  break;
107
98
  case "checkReadiness":
108
99
  admitted = [
@@ -127,10 +118,7 @@ export class BridgeServiceRuntime {
127
118
  if (admitted) {
128
119
  return;
129
120
  }
130
- if (
131
- !owned &&
132
- ["spawn", "restart", "shutdown"].includes(action)
133
- ) {
121
+ if (!owned && ["spawn", "restart", "shutdown"].includes(action)) {
134
122
  throw new BridgeSupervisionError("lifecycle_not_owned");
135
123
  }
136
124
  throw new BridgeSupervisionError("invalid_transition");
@@ -169,12 +157,7 @@ function transitionState(
169
157
  }
170
158
 
171
159
  function parseOutcome(value: unknown): BridgeServiceOutcome {
172
- if (
173
- value === "accepted" ||
174
- value === "ready" ||
175
- value === "notReady" ||
176
- value === "stopped"
177
- ) {
160
+ if (value === "accepted" || value === "ready" || value === "notReady" || value === "stopped") {
178
161
  return value;
179
162
  }
180
163
  if (
@@ -191,7 +174,8 @@ function parseOutcome(value: unknown): BridgeServiceOutcome {
191
174
  }
192
175
 
193
176
  function isFailureCode(value: unknown): value is BridgeServiceFailureCode {
194
- return typeof value === "string" &&
177
+ return (
178
+ typeof value === "string" &&
195
179
  [
196
180
  "spawnFailed",
197
181
  "attachFailed",
@@ -200,5 +184,6 @@ function isFailureCode(value: unknown): value is BridgeServiceFailureCode {
200
184
  "restartFailed",
201
185
  "reconnectFailed",
202
186
  "shutdownFailed",
203
- ].includes(value);
187
+ ].includes(value)
188
+ );
204
189
  }
@@ -1,3 +1,4 @@
1
+ import { isRecord } from "../../core/record.ts";
1
2
  import { BRIDGE_MAXIMUM_OPAQUE_ID_BYTES } from "../generated/protocol.ts";
2
3
  export type BridgeProtocolValidationCode =
3
4
  | "unsupported_protocol_version"
@@ -34,10 +35,7 @@ export class BridgeProtocolValidationError extends Error {
34
35
  readonly code: BridgeProtocolValidationCode;
35
36
  readonly received: unknown;
36
37
 
37
- constructor(
38
- code: BridgeProtocolValidationCode,
39
- received: unknown,
40
- ) {
38
+ constructor(code: BridgeProtocolValidationCode, received: unknown) {
41
39
  super(`incompatible bridge protocol: ${code}`);
42
40
  this.name = "BridgeProtocolValidationError";
43
41
  this.code = code;
@@ -49,21 +47,19 @@ export interface BridgeCodec<T> {
49
47
  parse(value: unknown): T;
50
48
  }
51
49
 
52
- export function bridgeCodec<T>(
53
- parse: (value: unknown) => T,
54
- ): BridgeCodec<T> {
50
+ export function bridgeCodec<T>(parse: (value: unknown) => T): BridgeCodec<T> {
55
51
  return { parse };
56
52
  }
57
53
 
58
54
  export function record(
59
55
  value: unknown,
60
- required: readonly string[],
61
- optional: readonly string[] = [],
56
+ required: ReadonlyArray<string>,
57
+ optional: ReadonlyArray<string> = [],
62
58
  ): Record<string, unknown> {
63
- if (typeof value !== "object" || value === null || Array.isArray(value)) {
59
+ if (!isRecord(value)) {
64
60
  incompatible("invalid_object", value);
65
61
  }
66
- const result = value as Record<string, unknown>;
62
+ const result = value;
67
63
  const allowed = new Set([...required, ...optional]);
68
64
  for (const key of Object.keys(result)) {
69
65
  if (!allowed.has(key)) {
@@ -78,20 +74,14 @@ export function record(
78
74
  return result;
79
75
  }
80
76
 
81
- export function array(
82
- value: unknown,
83
- maximum: number,
84
- ): readonly unknown[] {
77
+ export function array(value: unknown, maximum: number): ReadonlyArray<unknown> {
85
78
  if (!Array.isArray(value) || value.length > maximum) {
86
79
  incompatible("invalid_array", value);
87
80
  }
88
81
  return value;
89
82
  }
90
83
 
91
- export function unique<T>(
92
- values: readonly T[],
93
- key: (value: T) => string,
94
- ): void {
84
+ export function unique<T>(values: ReadonlyArray<T>, key: (value: T) => string): void {
95
85
  const seen = new Set<string>();
96
86
  for (const value of values) {
97
87
  const identity = key(value);
@@ -124,15 +114,8 @@ export function domainId(value: unknown): string {
124
114
  return value;
125
115
  }
126
116
 
127
- export function integer(
128
- value: unknown,
129
- minimum = 0,
130
- ): number {
131
- if (
132
- typeof value !== "number" ||
133
- !Number.isSafeInteger(value) ||
134
- value < minimum
135
- ) {
117
+ export function integer(value: unknown, minimum = 0): number {
118
+ if (typeof value !== "number" || !Number.isSafeInteger(value) || value < minimum) {
136
119
  incompatible("invalid_number", value);
137
120
  }
138
121
  return value;
@@ -140,25 +123,20 @@ export function integer(
140
123
 
141
124
  export function oneOf<const T extends string>(
142
125
  value: unknown,
143
- values: readonly T[],
126
+ values: ReadonlyArray<T>,
144
127
  code: BridgeProtocolValidationCode,
145
128
  ): T {
146
- if (typeof value !== "string" || !values.includes(value as T)) {
129
+ const match = values.find((candidate) => candidate === value);
130
+ if (match === undefined) {
147
131
  incompatible(code, value);
148
132
  }
149
- return value as T;
133
+ return match;
150
134
  }
151
135
 
152
- export function nullable<T>(
153
- value: unknown,
154
- parse: (value: unknown) => T,
155
- ): T | null {
136
+ export function nullable<T>(value: unknown, parse: (value: unknown) => T): T | null {
156
137
  return value === null ? null : parse(value);
157
138
  }
158
139
 
159
- export function incompatible(
160
- code: BridgeProtocolValidationCode,
161
- received: unknown,
162
- ): never {
140
+ export function incompatible(code: BridgeProtocolValidationCode, received: unknown): never {
163
141
  throw new BridgeProtocolValidationError(code, received);
164
142
  }
@@ -1,15 +1,12 @@
1
+ import { isRecord } from "../../core/record.ts";
1
2
  import { BRIDGE_FIELDS } from "../generated/fields.ts";
2
3
  import type {
3
4
  BridgeCancellationReceipt,
4
5
  BridgeJobTerminalEvent,
5
6
  BridgeProgressEvent,
6
7
  } from "../generated/protocol.ts";
7
- import {
8
- type BridgeCodec,
9
- incompatible,
10
- opaqueId,
11
- record,
12
- } from "./base.ts";
8
+ import { incompatible, opaqueId, record } from "./base.ts";
9
+ import type { BridgeCodec } from "./base.ts";
13
10
  import { parseBridgeFailure } from "./operations.ts";
14
11
 
15
12
  export function parseBridgeProgressEvent<P>(
@@ -58,11 +55,7 @@ function parseTerminalOutcome<S, D>(
58
55
  if (value === "cancelled") {
59
56
  return value;
60
57
  }
61
- const source = singleVariant(
62
- value,
63
- ["succeeded", "failed"],
64
- "unknown_job_outcome",
65
- );
58
+ const source = singleVariant(value, ["succeeded", "failed"], "unknown_job_outcome");
66
59
  if ("succeeded" in source) {
67
60
  return { succeeded: success.parse(source.succeeded) };
68
61
  }
@@ -73,32 +66,25 @@ function parseCancellationOutcome<D>(
73
66
  value: unknown,
74
67
  details: BridgeCodec<D>,
75
68
  ): BridgeCancellationReceipt<D>["outcome"] {
76
- if (
77
- value === "accepted" ||
78
- value === "alreadyTerminal" ||
79
- value === "unknown"
80
- ) {
69
+ if (value === "accepted" || value === "alreadyTerminal" || value === "unknown") {
81
70
  return value;
82
71
  }
83
- const source = singleVariant(
84
- value,
85
- ["rejected"],
86
- "unknown_cancellation_outcome",
87
- );
72
+ const source = singleVariant(value, ["rejected"], "unknown_cancellation_outcome");
88
73
  return { rejected: parseBridgeFailure(source.rejected, details) };
89
74
  }
90
75
 
91
76
  function singleVariant(
92
77
  value: unknown,
93
- keys: readonly string[],
78
+ variants: ReadonlyArray<string>,
94
79
  code: "unknown_job_outcome" | "unknown_cancellation_outcome",
95
80
  ): Record<string, unknown> {
96
- if (typeof value !== "object" || value === null || Array.isArray(value)) {
81
+ if (!isRecord(value)) {
97
82
  incompatible(code, value);
98
83
  }
99
- const source = value as Record<string, unknown>;
84
+ const source = value;
100
85
  const present = Object.keys(source);
101
- if (present.length !== 1 || !keys.includes(present[0]!)) {
86
+ const [key] = present;
87
+ if (present.length !== 1 || key === undefined || !variants.includes(key)) {
102
88
  incompatible(code, value);
103
89
  }
104
90
  return source;