@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,4 +1,4 @@
1
- // @generated by `effigy generate:licence`; do not edit.
1
+ // @generated by `effigy generate:bindings`; do not edit.
2
2
  // Allowed field names per tagged-union variant, from the Rust enums.
3
3
  // Keyed by type, then by discriminant. The discriminant itself is in
4
4
  // each list, because `exact()` compares every key.
@@ -1,12 +1,8 @@
1
1
  /**
2
2
  * The licence key format, in TypeScript.
3
3
  *
4
- * A second implementation of a format that already exists in Rust, which is
5
- * normally a mistake. It is here because Card 158 requires key entry to fail
6
- * locally: a mistyped key must never produce a message implying the key is
7
- * invalid, and telling those apart after a round trip is too late — the
8
- * authority answers `notRecognised`, which sends a customer to check their
9
- * purchase when they should check their typing.
4
+ * Check the format locally so a mistyped key can be corrected before the
5
+ * authority answers `notRecognised`.
10
6
  *
11
7
  * The two implementations are bound by `key-conformance.json`, generated from
12
8
  * the Rust and asserted against here. Neither can change the format without
@@ -68,7 +64,9 @@ export function isProbablyATypo(problem: LicenceKeyProblem): boolean {
68
64
  */
69
65
  export function parseLicenceKey(input: string): LicenceKeyResult {
70
66
  const normalized = normalize(input);
71
- if (!normalized.ok) return normalized;
67
+ if (!normalized.ok) {
68
+ return normalized;
69
+ }
72
70
 
73
71
  const value = normalized.value;
74
72
  if (value.length < MINIMUM_KEY_SYMBOLS) {
@@ -87,22 +85,45 @@ export function parseLicenceKey(input: string): LicenceKeyResult {
87
85
 
88
86
  /** Renders a normalized key as `XXXXX-XXXXX-…`. */
89
87
  export function group(key: string): string {
90
- const groups: string[] = [];
91
- for (let at = 0; at < key.length; at += GROUP) groups.push(key.slice(at, at + GROUP));
88
+ const groups: Array<string> = [];
89
+ for (let at = 0; at < key.length; at += GROUP) {
90
+ groups.push(key.slice(at, at + GROUP));
91
+ }
92
92
  return groups.join("-");
93
93
  }
94
94
 
95
+ /** Matches Rust's `char::is_whitespace` (Unicode `White_Space`: includes U+0085, excludes U+FEFF). */
96
+ const UNICODE_WHITESPACE = /\p{White_Space}/u;
97
+
98
+ /** Matches Rust's `char::to_ascii_uppercase` so non-ASCII letters like U+017F never fold into ASCII. */
99
+ function toAsciiUpper(character: string): string {
100
+ const code = character.charCodeAt(0);
101
+ if (code >= 0x61 && code <= 0x7a) {
102
+ return String.fromCharCode(code - 0x20);
103
+ }
104
+ return character;
105
+ }
106
+
95
107
  function normalize(
96
108
  input: string,
97
- ): { readonly ok: true; readonly value: string } | { readonly ok: false; readonly problem: LicenceKeyProblem } {
98
- let output = "";
109
+ ):
110
+ | { readonly ok: true; readonly value: string }
111
+ | { readonly ok: false; readonly problem: LicenceKeyProblem } {
112
+ const symbols: Array<{ readonly original: string; readonly mapped: string }> = [];
99
113
  for (const character of input) {
100
- if (character === "-" || /\s/u.test(character)) continue;
101
- const upper = character.toUpperCase();
114
+ if (character === "-" || UNICODE_WHITESPACE.test(character)) {
115
+ continue;
116
+ }
117
+ const upper = toAsciiUpper(character);
102
118
  // The confusions Crockford excluded the symbols to avoid.
103
119
  const mapped = upper === "I" || upper === "L" ? "1" : upper === "O" ? "0" : upper;
104
- if (!CHECK_ALPHABET.includes(mapped) || mapped.length !== 1) {
105
- return { ok: false, problem: { kind: "unexpectedSymbol", symbol: character } };
120
+ symbols.push({ original: character, mapped });
121
+ }
122
+ let output = "";
123
+ for (const [index, { original, mapped }] of symbols.entries()) {
124
+ const alphabet = index + 1 < symbols.length ? ALPHABET : CHECK_ALPHABET;
125
+ if (mapped.length !== 1 || !alphabet.includes(mapped)) {
126
+ return { ok: false, problem: { kind: "unexpectedSymbol", symbol: original } };
106
127
  }
107
128
  output += mapped;
108
129
  }
@@ -118,8 +139,11 @@ function normalize(
118
139
  function checkSymbol(body: string): string {
119
140
  let sum = 0;
120
141
  for (let index = 0; index < body.length; index += 1) {
121
- const value = ALPHABET.indexOf(body[index]!);
122
- sum += (value === -1 ? 0 : value) * (index + 1);
142
+ const value = ALPHABET.indexOf(body.charAt(index));
143
+ if (value === -1) {
144
+ throw new Error(`normalized key body contains unexpected symbol: ${body[index]}`);
145
+ }
146
+ sum += value * (index + 1);
123
147
  }
124
- return CHECK_ALPHABET[sum % 37]!;
148
+ return CHECK_ALPHABET.charAt(sum % 37);
125
149
  }
@@ -1,3 +1,5 @@
1
+ import type { ConnectionFailureReporter } from "../core/connection.ts";
2
+ import type { Unlisten } from "../core/transport.ts";
1
3
  import type {
2
4
  LicenceActivateCommand,
3
5
  LicenceChangedEvent,
@@ -9,7 +11,7 @@ import type {
9
11
  LicenceSnapshot,
10
12
  } from "./generated/protocol.ts";
11
13
 
12
- export type LicenceUnlisten = () => void | Promise<void>;
14
+ export type LicenceUnlisten = Unlisten;
13
15
 
14
16
  /**
15
17
  * The raw seam.
@@ -24,7 +26,9 @@ export interface LicencePort {
24
26
  refresh(command: LicenceRefreshCommand): Promise<unknown>;
25
27
  releaseSeat(command: LicenceReleaseSeatCommand): Promise<unknown>;
26
28
  renameSeat(command: LicenceRenameSeatCommand): Promise<unknown>;
27
- listen?(listener: (event: unknown) => void): LicenceUnlisten | Promise<LicenceUnlisten>;
29
+ listen?:
30
+ | ((listener: (event: unknown) => void) => LicenceUnlisten | Promise<LicenceUnlisten>)
31
+ | undefined;
28
32
  }
29
33
 
30
34
  /** The same seam after validation. */
@@ -35,5 +39,8 @@ export interface CheckedLicencePort {
35
39
  refresh(command: LicenceRefreshCommand): Promise<LicenceOutcomeProjection>;
36
40
  releaseSeat(command: LicenceReleaseSeatCommand): Promise<LicenceOutcomeProjection>;
37
41
  renameSeat(command: LicenceRenameSeatCommand): Promise<LicenceOutcomeProjection>;
38
- listen(listener: (event: LicenceChangedEvent) => void): Promise<LicenceUnlisten>;
42
+ listen(
43
+ listener: (event: LicenceChangedEvent) => void,
44
+ onFailure?: ConnectionFailureReporter,
45
+ ): Promise<LicenceUnlisten>;
39
46
  }
@@ -1,13 +1,36 @@
1
- import type { LicenceActivateCommand, LicenceDeactivateCommand, LicenceRefreshCommand, LicenceReleaseSeatCommand, LicenceRenameSeatCommand } from "./generated/protocol.ts";
1
+ import { wireCopy } from "../core/wire-copy.ts";
2
+ import type {
3
+ LicenceActivateCommand,
4
+ LicenceDeactivateCommand,
5
+ LicenceRefreshCommand,
6
+ LicenceReleaseSeatCommand,
7
+ LicenceRenameSeatCommand,
8
+ } from "./generated/protocol.ts";
2
9
  import type { LicencePort, LicenceUnlisten } from "./ports.ts";
3
10
  export class SerializedLicencePort implements LicencePort {
4
11
  constructor(readonly inner: LicencePort) {}
5
- async snapshot(): Promise<unknown> { return clone(await this.inner.snapshot()); }
6
- async activate(command: LicenceActivateCommand): Promise<unknown> { return clone(await this.inner.activate(clone(command))); }
7
- async deactivate(command: LicenceDeactivateCommand): Promise<unknown> { return clone(await this.inner.deactivate(clone(command))); }
8
- async refresh(command: LicenceRefreshCommand): Promise<unknown> { return clone(await this.inner.refresh(clone(command))); }
9
- async releaseSeat(command: LicenceReleaseSeatCommand): Promise<unknown> { return clone(await this.inner.releaseSeat(clone(command))); }
10
- async renameSeat(command: LicenceRenameSeatCommand): Promise<unknown> { return clone(await this.inner.renameSeat(clone(command))); }
11
- async listen(listener: (event: unknown) => void): Promise<LicenceUnlisten> { if (this.inner.listen === undefined) return () => {}; return this.inner.listen((event) => listener(clone(event))); }
12
+ async snapshot(): Promise<unknown> {
13
+ return wireCopy(await this.inner.snapshot());
14
+ }
15
+ async activate(command: LicenceActivateCommand): Promise<unknown> {
16
+ return wireCopy(await this.inner.activate(wireCopy(command)));
17
+ }
18
+ async deactivate(command: LicenceDeactivateCommand): Promise<unknown> {
19
+ return wireCopy(await this.inner.deactivate(wireCopy(command)));
20
+ }
21
+ async refresh(command: LicenceRefreshCommand): Promise<unknown> {
22
+ return wireCopy(await this.inner.refresh(wireCopy(command)));
23
+ }
24
+ async releaseSeat(command: LicenceReleaseSeatCommand): Promise<unknown> {
25
+ return wireCopy(await this.inner.releaseSeat(wireCopy(command)));
26
+ }
27
+ async renameSeat(command: LicenceRenameSeatCommand): Promise<unknown> {
28
+ return wireCopy(await this.inner.renameSeat(wireCopy(command)));
29
+ }
30
+ async listen(listener: (event: unknown) => void): Promise<LicenceUnlisten> {
31
+ if (this.inner.listen === undefined) {
32
+ return () => {};
33
+ }
34
+ return this.inner.listen((event) => listener(wireCopy(event)));
35
+ }
12
36
  }
13
- function clone<T>(value: T): T { return JSON.parse(JSON.stringify(value)) as T; }
@@ -1,3 +1,4 @@
1
+ import { checkWireFields, readWireRecord, wireVariantFields } from "../core/record.ts";
1
2
  import { LICENCE_FIELDS } from "./generated/fields.ts";
2
3
  import {
3
4
  LICENCE_CHANGED_KINDS,
@@ -7,14 +8,16 @@ import {
7
8
  LICENCE_REJECTION_CODES,
8
9
  LICENCE_TRUST_BASIS_KINDS,
9
10
  LICENCE_USABILITY_STATES,
10
- type LicenceActivateCommand,
11
- type LicenceChangedEvent,
12
- type LicenceDeactivateCommand,
13
- type LicenceOutcomeProjection,
14
- type LicenceRefreshCommand,
15
- type LicenceReleaseSeatCommand,
16
- type LicenceRenameSeatCommand,
17
- type LicenceSnapshot,
11
+ } from "./generated/protocol.ts";
12
+ import type {
13
+ LicenceActivateCommand,
14
+ LicenceChangedEvent,
15
+ LicenceDeactivateCommand,
16
+ LicenceOutcomeProjection,
17
+ LicenceRefreshCommand,
18
+ LicenceReleaseSeatCommand,
19
+ LicenceRenameSeatCommand,
20
+ LicenceSnapshot,
18
21
  } from "./generated/protocol.ts";
19
22
  import {
20
23
  LICENCE_VARIANT_FIELDS,
@@ -37,9 +40,10 @@ export function assertLicenceSnapshot(value: unknown): asserts value is LicenceS
37
40
  exact(root, "$", LICENCE_FIELDS.LicenceSnapshot);
38
41
  protocol(root.protocolVersion, "$.protocolVersion");
39
42
  positive(root.authorityEpoch, "$.authorityEpoch");
40
- // Absent means unlicensed, which is the absence of a licence rather than a
41
- // licence that cannot be used. Card 193 kept those apart deliberately.
42
- if (root.licence !== null) held(root.licence, "$.licence");
43
+ // A held licence reports its usability even when that usability is blocked.
44
+ if (root.licence !== null) {
45
+ held(root.licence, "$.licence");
46
+ }
43
47
  }
44
48
 
45
49
  export function assertLicenceOutcome(value: unknown): asserts value is LicenceOutcomeProjection {
@@ -47,7 +51,9 @@ export function assertLicenceOutcome(value: unknown): asserts value is LicenceOu
47
51
  const root = object(value, "$");
48
52
  oneOf(root.status, "$.status", LICENCE_OUTCOME_STATUSES);
49
53
  exact(root, "$", variantFields("LicenceOutcomeProjection", root, "$"));
50
- if (root.status === "rejected") oneOf(root.code, "$.code", LICENCE_REJECTION_CODES);
54
+ if (root.status === "rejected") {
55
+ oneOf(root.code, "$.code", LICENCE_REJECTION_CODES);
56
+ }
51
57
  assertLicenceSnapshot(root.snapshot);
52
58
  }
53
59
 
@@ -74,14 +80,20 @@ export function assertLicenceActivateCommand(
74
80
  // The one place credential material crosses, and it crosses inward only.
75
81
  // Checked for shape, never echoed: `noCredential` runs on everything coming
76
82
  // back and would reject a projection that returned any of this.
77
- if (credential.kind === "key") nonEmpty(credential.key, "$.credential.key");
78
- if (credential.kind === "accountToken") nonEmpty(credential.token, "$.credential.token");
83
+ if (credential.kind === "key") {
84
+ nonEmpty(credential.key, "$.credential.key");
85
+ }
86
+ if (credential.kind === "accountToken") {
87
+ nonEmpty(credential.token, "$.credential.token");
88
+ }
79
89
  if (credential.kind === "licenceFile") {
80
- nonEmpty(credential.contentsBase64, "$.credential.contentsBase64");
90
+ base64(credential.contentsBase64, "$.credential.contentsBase64");
81
91
  }
82
92
  // The label a customer gives this machine, asked for at the only moment it
83
93
  // can be without inventing a settings screen. Absent is fine.
84
- if (root.label !== null) nonEmpty(root.label, "$.label");
94
+ if (root.label !== null) {
95
+ nonEmpty(root.label, "$.label");
96
+ }
85
97
  }
86
98
 
87
99
  export function assertLicenceDeactivateCommand(
@@ -110,7 +122,9 @@ export function assertLicenceRenameSeatCommand(
110
122
  nonEmpty(root.machineId, "$.machineId");
111
123
  // Null clears the label back to unnamed; an empty string is a mistake
112
124
  // wearing null's clothes and is refused rather than stored.
113
- if (root.label !== null) nonEmpty(root.label, "$.label");
125
+ if (root.label !== null) {
126
+ nonEmpty(root.label, "$.label");
127
+ }
114
128
  }
115
129
 
116
130
  function held(value: unknown, path: string): void {
@@ -131,7 +145,9 @@ function held(value: unknown, path: string): void {
131
145
  // checks it is present and never what it says, because anything derived
132
146
  // from the host would be a different value wearing the same field.
133
147
  nonEmpty(seat.machineId, `${at}.machineId`);
134
- if (seat.label !== null) nonEmpty(seat.label, `${at}.label`);
148
+ if (seat.label !== null) {
149
+ nonEmpty(seat.label, `${at}.label`);
150
+ }
135
151
  boolean(seat.thisMachine, `${at}.thisMachine`);
136
152
  });
137
153
  array(licence.entitlements, `${path}.entitlements`).forEach((entry, index) => {
@@ -141,7 +157,9 @@ function held(value: unknown, path: string): void {
141
157
  // Opaque by design: Longhorn enumerates no features, so this checks that
142
158
  // an id is present and never what it says.
143
159
  nonEmpty(entitlement.id, `${at}.id`);
144
- if (entitlement.atMost !== null) positive(entitlement.atMost, `${at}.atMost`);
160
+ if (entitlement.atMost !== null) {
161
+ positive(entitlement.atMost, `${at}.atMost`);
162
+ }
145
163
  });
146
164
  }
147
165
 
@@ -152,7 +170,9 @@ function usability(value: unknown, path: string): void {
152
170
  // `clockRefused` carries no timestamp on purpose: a licence refused because
153
171
  // the machine clock moved is not expired, and giving it a date would invite
154
172
  // a surface to render it as one.
155
- if (root.state === "inGrace") timestamp(root.until, `${path}.until`);
173
+ if (root.state === "inGrace") {
174
+ timestamp(root.until, `${path}.until`);
175
+ }
156
176
  if (root.state === "useWindowExpired" || root.state === "leaseLapsed") {
157
177
  timestamp(root.at, `${path}.at`);
158
178
  }
@@ -162,10 +182,12 @@ function trustBasis(value: unknown, path: string): void {
162
182
  const root = object(value, path);
163
183
  oneOf(root.kind, `${path}.kind`, LICENCE_TRUST_BASIS_KINDS);
164
184
  exact(root, path, variantFields("LicenceTrustBasisProjection", root, path));
165
- if (root.kind === "remoteAssertion") timestamp(root.checked, `${path}.checked`);
185
+ if (root.kind === "remoteAssertion") {
186
+ timestamp(root.checked, `${path}.checked`);
187
+ }
166
188
  }
167
189
 
168
- function commandBase(value: unknown, expected: readonly string[]): Record<string, unknown> {
190
+ function commandBase(value: unknown, expected: ReadonlyArray<string>): Record<string, unknown> {
169
191
  const root = object(value, "$");
170
192
  exact(root, "$", expected);
171
193
  protocol(root.protocolVersion, "$.protocolVersion");
@@ -187,8 +209,10 @@ function commandBase(value: unknown, expected: readonly string[]): Record<string
187
209
  function noCredential(value: unknown): void {
188
210
  const forbidden = ["credential", "secret", "signature", "token", "privatekey", "keyid"];
189
211
  const visit = (candidate: unknown, path: string): void => {
190
- if (Array.isArray(candidate)) return candidate.forEach((item, index) => visit(item, `${path}[${index}]`));
191
- if (candidate !== null && typeof candidate === "object")
212
+ if (Array.isArray(candidate)) {
213
+ return candidate.forEach((item, index) => visit(item, `${path}[${index}]`));
214
+ }
215
+ if (candidate !== null && typeof candidate === "object") {
192
216
  for (const [key, child] of Object.entries(candidate)) {
193
217
  const lowered = key.toLocaleLowerCase();
194
218
  if (forbidden.some((needle) => lowered.includes(needle))) {
@@ -196,6 +220,7 @@ function noCredential(value: unknown): void {
196
220
  }
197
221
  visit(child, `${path}.${key}`);
198
222
  }
223
+ }
199
224
  };
200
225
  visit(value, "$");
201
226
  }
@@ -204,23 +229,89 @@ function variantFields(
204
229
  type: string,
205
230
  value: Record<string, unknown>,
206
231
  path: string,
207
- ): readonly string[] {
208
- const discriminant = value[LICENCE_VARIANT_FIELDS_DISCRIMINANTS[type] ?? "kind"];
209
- const fields = LICENCE_VARIANT_FIELDS[type]?.[discriminant as string];
210
- if (fields === undefined) fail(path, `no generated fields for ${type}.${String(discriminant)}`);
211
- return fields;
212
- }
213
-
214
- function object(value: unknown, path: string): Record<string, unknown> { if (value === null || typeof value !== "object" || Array.isArray(value)) fail(path, "expected object"); return value as Record<string, unknown>; }
215
- function array(value: unknown, path: string): unknown[] { if (!Array.isArray(value)) fail(path, "expected array"); return value; }
216
- function exact(value: Record<string, unknown>, path: string, expected: readonly string[]): void { const actual = Object.keys(value).sort(); const wanted = [...expected].sort(); if (actual.length !== wanted.length || actual.some((key, index) => key !== wanted[index])) fail(path, `unexpected keys: ${actual.join(",")}`); }
217
- function protocol(value: unknown, path: string): void { if (value !== LICENCE_PROTOCOL_VERSION) fail(path, `expected exact protocol ${LICENCE_PROTOCOL_VERSION}`); }
218
- function positive(value: unknown, path: string): void { if (typeof value !== "number" || !Number.isSafeInteger(value) || value <= 0) fail(path, "expected positive safe integer"); }
219
- function string(value: unknown, path: string): void { if (typeof value !== "string") fail(path, "expected string"); }
220
- function nonEmpty(value: unknown, path: string): void { string(value, path); if ((value as string).length === 0) fail(path, "expected a non-empty string"); }
232
+ ): ReadonlyArray<string> {
233
+ return wireVariantFields(
234
+ LICENCE_VARIANT_FIELDS,
235
+ LICENCE_VARIANT_FIELDS_DISCRIMINANTS,
236
+ type,
237
+ value,
238
+ (discriminant) => fail(path, `no generated fields for ${type}.${String(discriminant)}`),
239
+ );
240
+ }
241
+
242
+ function object(value: unknown, path: string): Record<string, unknown> {
243
+ const result = readWireRecord(value, () => fail(path, "expected object"));
244
+ return result;
245
+ }
246
+ function array(value: unknown, path: string): Array<unknown> {
247
+ if (!Array.isArray(value)) {
248
+ fail(path, "expected array");
249
+ }
250
+ return value;
251
+ }
252
+ function exact(
253
+ value: Record<string, unknown>,
254
+ path: string,
255
+ expected: ReadonlyArray<string>,
256
+ ): void {
257
+ checkWireFields(value, expected, () =>
258
+ fail(path, `unexpected keys: ${Object.keys(value).sort().join(",")}`),
259
+ );
260
+ }
261
+ function protocol(value: unknown, path: string): void {
262
+ if (value !== LICENCE_PROTOCOL_VERSION) {
263
+ fail(path, `expected exact protocol ${LICENCE_PROTOCOL_VERSION}`);
264
+ }
265
+ }
266
+ function positive(value: unknown, path: string): void {
267
+ if (typeof value !== "number" || !Number.isSafeInteger(value) || value <= 0) {
268
+ fail(path, "expected positive safe integer");
269
+ }
270
+ }
271
+ function string(value: unknown, path: string): asserts value is string {
272
+ if (typeof value !== "string") {
273
+ fail(path, "expected string");
274
+ }
275
+ }
276
+ function nonEmpty(value: unknown, path: string): void {
277
+ string(value, path);
278
+ if (value.length === 0) {
279
+ fail(path, "expected a non-empty string");
280
+ }
281
+ }
282
+ function base64(value: unknown, path: string): void {
283
+ string(value, path);
284
+ // Canonical padded standard base64, including zero pad bits.
285
+ if (
286
+ !/^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/.test(value) ||
287
+ value.length === 0 ||
288
+ (value.endsWith("==") && !/[AQgw]==$/.test(value)) ||
289
+ (value.endsWith("=") && !value.endsWith("==") && !/[AEIMQUYcgkosw048]=$/.test(value))
290
+ ) {
291
+ fail(path, "expected padded standard base64");
292
+ }
293
+ }
221
294
  /** Unix seconds, and negative is a real point in time. */
222
- function timestamp(value: unknown, path: string): void { if (typeof value !== "number" || !Number.isSafeInteger(value)) fail(path, "expected a unix-second timestamp"); }
223
- function optionalTimestamp(value: unknown, path: string): void { if (value !== null) timestamp(value, path); }
224
- function boolean(value: unknown, path: string): void { if (typeof value !== "boolean") fail(path, "expected boolean"); }
225
- function oneOf(value: unknown, path: string, values: readonly string[]): void { if (typeof value !== "string" || !values.includes(value)) fail(path, "unsupported value"); }
226
- function fail(path: string, message: string): never { throw new LicenceValidationError(path, message); }
295
+ function timestamp(value: unknown, path: string): void {
296
+ if (typeof value !== "number" || !Number.isSafeInteger(value)) {
297
+ fail(path, "expected a unix-second timestamp");
298
+ }
299
+ }
300
+ function optionalTimestamp(value: unknown, path: string): void {
301
+ if (value !== null) {
302
+ timestamp(value, path);
303
+ }
304
+ }
305
+ function boolean(value: unknown, path: string): void {
306
+ if (typeof value !== "boolean") {
307
+ fail(path, "expected boolean");
308
+ }
309
+ }
310
+ function oneOf(value: unknown, path: string, values: ReadonlyArray<string>): void {
311
+ if (typeof value !== "string" || !values.includes(value)) {
312
+ fail(path, "unsupported value");
313
+ }
314
+ }
315
+ function fail(path: string, message: string): never {
316
+ throw new LicenceValidationError(path, message);
317
+ }