@inflatable-cookie/longhorn 0.1.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 (178) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +38 -0
  3. package/package.json +205 -0
  4. package/src/agent-control/index.ts +20 -0
  5. package/src/agent-control/inject.ts +4 -0
  6. package/src/agent-control/shim.ts +722 -0
  7. package/src/bridge/direct.ts +22 -0
  8. package/src/bridge/generated/fields.ts +32 -0
  9. package/src/bridge/generated/protocol.ts +218 -0
  10. package/src/bridge/generated/variant-fields.ts +10 -0
  11. package/src/bridge/index.ts +15 -0
  12. package/src/bridge/job.ts +51 -0
  13. package/src/bridge/loopback.ts +84 -0
  14. package/src/bridge/operation.ts +166 -0
  15. package/src/bridge/router.ts +71 -0
  16. package/src/bridge/runtime/connection.ts +318 -0
  17. package/src/bridge/runtime/policy.ts +89 -0
  18. package/src/bridge/runtime/retry.ts +60 -0
  19. package/src/bridge/runtime.ts +3 -0
  20. package/src/bridge/session.ts +67 -0
  21. package/src/bridge/stream/connection.ts +146 -0
  22. package/src/bridge/stream/source.ts +61 -0
  23. package/src/bridge/stream/tracker.ts +109 -0
  24. package/src/bridge/stream.ts +6 -0
  25. package/src/bridge/supervision.ts +204 -0
  26. package/src/bridge/validation/base.ts +164 -0
  27. package/src/bridge/validation/jobs.ts +105 -0
  28. package/src/bridge/validation/negotiation.ts +242 -0
  29. package/src/bridge/validation/operations.ts +213 -0
  30. package/src/bridge/validation/streams.ts +47 -0
  31. package/src/bridge/validation.ts +5 -0
  32. package/src/commands/client.ts +124 -0
  33. package/src/commands/controller.ts +411 -0
  34. package/src/commands/generated/fields.ts +43 -0
  35. package/src/commands/generated/protocol.ts +876 -0
  36. package/src/commands/generated/variant-fields.ts +88 -0
  37. package/src/commands/index.ts +13 -0
  38. package/src/commands/keyboard.ts +361 -0
  39. package/src/commands/ports.ts +82 -0
  40. package/src/commands/projectors.ts +306 -0
  41. package/src/commands/validation.ts +444 -0
  42. package/src/config/client.ts +215 -0
  43. package/src/config/generated/base.ts +714 -0
  44. package/src/config/generated/fields.ts +54 -0
  45. package/src/config/generated/label-template.ts +14 -0
  46. package/src/config/generated/labels.ts +30 -0
  47. package/src/config/generated/protocol.ts +3 -0
  48. package/src/config/generated/restore.ts +658 -0
  49. package/src/config/generated/variant-fields.ts +129 -0
  50. package/src/config/index.ts +12 -0
  51. package/src/config/names.ts +19 -0
  52. package/src/config/validation/commands.ts +155 -0
  53. package/src/config/validation/outcomes.ts +243 -0
  54. package/src/config/validation/primitives.ts +174 -0
  55. package/src/config/validation/projection.ts +351 -0
  56. package/src/config/validation.ts +7 -0
  57. package/src/core/connection.ts +252 -0
  58. package/src/core/index.ts +2 -0
  59. package/src/core/transport.ts +24 -0
  60. package/src/history/client.ts +65 -0
  61. package/src/history/controller.ts +385 -0
  62. package/src/history/direct.ts +21 -0
  63. package/src/history/generated/fields.ts +18 -0
  64. package/src/history/generated/protocol.ts +389 -0
  65. package/src/history/generated/variant-fields.ts +27 -0
  66. package/src/history/index.ts +13 -0
  67. package/src/history/ports.ts +31 -0
  68. package/src/history/serialized.ts +38 -0
  69. package/src/history/validation.ts +334 -0
  70. package/src/history-tree/client.ts +16 -0
  71. package/src/history-tree/controller.ts +199 -0
  72. package/src/history-tree/direct.ts +3 -0
  73. package/src/history-tree/generated/fields.ts +26 -0
  74. package/src/history-tree/generated/protocol.ts +711 -0
  75. package/src/history-tree/generated/variant-fields.ts +39 -0
  76. package/src/history-tree/index.ts +13 -0
  77. package/src/history-tree/ports.ts +43 -0
  78. package/src/history-tree/serialized.ts +15 -0
  79. package/src/history-tree/validation.ts +269 -0
  80. package/src/layout/generated/protocol.ts +289 -0
  81. package/src/layout/index.ts +4 -0
  82. package/src/layout/ratio.ts +22 -0
  83. package/src/layout/validation.ts +110 -0
  84. package/src/layout/visibility.ts +30 -0
  85. package/src/licence/client.ts +32 -0
  86. package/src/licence/controller.ts +301 -0
  87. package/src/licence/direct.ts +3 -0
  88. package/src/licence/generated/fields.ts +17 -0
  89. package/src/licence/generated/key-conformance.json +42 -0
  90. package/src/licence/generated/protocol.ts +249 -0
  91. package/src/licence/generated/variant-fields.ts +34 -0
  92. package/src/licence/index.ts +14 -0
  93. package/src/licence/key.ts +125 -0
  94. package/src/licence/ports.ts +39 -0
  95. package/src/licence/serialized.ts +13 -0
  96. package/src/licence/validation.ts +226 -0
  97. package/src/native-content/client.ts +474 -0
  98. package/src/native-content/direct.ts +16 -0
  99. package/src/native-content/generated/fields.ts +32 -0
  100. package/src/native-content/generated/protocol.ts +498 -0
  101. package/src/native-content/generated/variant-fields.ts +77 -0
  102. package/src/native-content/index.ts +12 -0
  103. package/src/native-content/ports.ts +46 -0
  104. package/src/native-content/serialized.ts +50 -0
  105. package/src/native-content/validation/base.ts +194 -0
  106. package/src/native-content/validation/messages.ts +192 -0
  107. package/src/native-content/validation/state.ts +404 -0
  108. package/src/native-content/validation.ts +7 -0
  109. package/src/notifications/client.ts +106 -0
  110. package/src/notifications/controller.ts +240 -0
  111. package/src/notifications/direct.ts +12 -0
  112. package/src/notifications/generated/fields.ts +19 -0
  113. package/src/notifications/generated/labels.ts +20 -0
  114. package/src/notifications/generated/protocol.ts +522 -0
  115. package/src/notifications/generated/variant-fields.ts +37 -0
  116. package/src/notifications/index.ts +16 -0
  117. package/src/notifications/ports.ts +22 -0
  118. package/src/notifications/serialized.ts +27 -0
  119. package/src/notifications/toast.ts +41 -0
  120. package/src/notifications/tone.ts +27 -0
  121. package/src/notifications/validation.ts +246 -0
  122. package/src/operation/bridge.ts +168 -0
  123. package/src/operation/client.ts +167 -0
  124. package/src/operation/controller.ts +321 -0
  125. package/src/operation/direct.ts +15 -0
  126. package/src/operation/generated/fields.ts +20 -0
  127. package/src/operation/generated/labels.ts +15 -0
  128. package/src/operation/generated/protocol.ts +762 -0
  129. package/src/operation/generated/variant-fields.ts +60 -0
  130. package/src/operation/index.ts +14 -0
  131. package/src/operation/ports.ts +29 -0
  132. package/src/operation/serialized.ts +39 -0
  133. package/src/operation/validation.ts +479 -0
  134. package/src/settings/client.ts +142 -0
  135. package/src/settings/connection.ts +246 -0
  136. package/src/settings/generated/fields.ts +34 -0
  137. package/src/settings/generated/protocol.ts +668 -0
  138. package/src/settings/generated/variant-fields.ts +26 -0
  139. package/src/settings/index.ts +12 -0
  140. package/src/settings/names.ts +8 -0
  141. package/src/settings/registry.ts +141 -0
  142. package/src/settings/validation/authority.ts +66 -0
  143. package/src/settings/validation/command.ts +165 -0
  144. package/src/settings/validation/error.ts +33 -0
  145. package/src/settings/validation/primitives.ts +203 -0
  146. package/src/settings/validation/projection.ts +50 -0
  147. package/src/settings/validation/registry.ts +170 -0
  148. package/src/settings/validation.ts +4 -0
  149. package/src/surface-transfer/client.ts +54 -0
  150. package/src/surface-transfer/generated/fields.ts +12 -0
  151. package/src/surface-transfer/generated/protocol.ts +61 -0
  152. package/src/surface-transfer/generated/variant-fields.ts +30 -0
  153. package/src/surface-transfer/index.ts +9 -0
  154. package/src/surface-transfer/validation.ts +209 -0
  155. package/src/surfaces/client.ts +140 -0
  156. package/src/surfaces/generated/fields.ts +19 -0
  157. package/src/surfaces/generated/protocol.ts +217 -0
  158. package/src/surfaces/generated/variant-fields.ts +42 -0
  159. package/src/surfaces/index.ts +9 -0
  160. package/src/surfaces/validation.ts +199 -0
  161. package/src/transfer/client.ts +133 -0
  162. package/src/transfer/connection.ts +87 -0
  163. package/src/transfer/generated/fields.ts +23 -0
  164. package/src/transfer/generated/protocol.ts +171 -0
  165. package/src/transfer/generated/variant-fields.ts +45 -0
  166. package/src/transfer/index.ts +11 -0
  167. package/src/transfer/payload.ts +71 -0
  168. package/src/transfer/validation.ts +282 -0
  169. package/src/update/client.ts +29 -0
  170. package/src/update/controller.ts +255 -0
  171. package/src/update/direct.ts +3 -0
  172. package/src/update/generated/fields.ts +14 -0
  173. package/src/update/generated/protocol.ts +238 -0
  174. package/src/update/generated/variant-fields.ts +44 -0
  175. package/src/update/index.ts +14 -0
  176. package/src/update/ports.ts +36 -0
  177. package/src/update/serialized.ts +12 -0
  178. package/src/update/validation.ts +191 -0
@@ -0,0 +1,249 @@
1
+ // @generated by `effigy generate:licence`; do not edit.
2
+ // Rust serde types are the wire authority. Entitlements are opaque
3
+ // ids: Longhorn answers "entitled?" and enumerates no features.
4
+
5
+ export const LICENCE_PROTOCOL_VERSION = 1 as const;
6
+ export const LICENCE_USABILITY_STATES = ["active","inGrace","useWindowExpired","leaseLapsed","clockRefused"] as const;
7
+ export const LICENCE_TRUST_BASIS_KINDS = ["offlineSignature","remoteAssertion"] as const;
8
+ export const LICENCE_CREDENTIAL_KINDS = ["key","accountToken","licenceFile"] as const;
9
+ export const LICENCE_REJECTION_CODES = ["malformed","notRecognised","noSeatsFree","revoked","clockRefused","staleAuthority","seatNotFound","unreachable"] as const;
10
+ export const LICENCE_OUTCOME_STATUSES = ["committed","rejected"] as const;
11
+ export const LICENCE_CHANGED_KINDS = ["activated","deactivated","seatRelabelled","refreshed","usabilityChanged"] as const;
12
+
13
+ export type LicenceProtocolVersion = number;
14
+
15
+ export type Timestamp = number;
16
+
17
+ export type LicenceUsabilityProjection = { "state": "active" } | { "state": "inGrace",
18
+ /**
19
+ * When grace runs out.
20
+ */
21
+ until: Timestamp, } | { "state": "useWindowExpired",
22
+ /**
23
+ * When it passed.
24
+ */
25
+ at: Timestamp, } | { "state": "leaseLapsed",
26
+ /**
27
+ * When grace ran out.
28
+ */
29
+ at: Timestamp, } | { "state": "clockRefused" };
30
+
31
+ export type LicenceTrustBasisProjection = { "kind": "offlineSignature" } | { "kind": "remoteAssertion",
32
+ /**
33
+ * When the assertion was obtained.
34
+ */
35
+ checked: Timestamp, };
36
+
37
+ export type LicenceEntitlementProjection = {
38
+ /**
39
+ * The entitlement id. Opaque: Longhorn enumerates no features.
40
+ */
41
+ id: string,
42
+ /**
43
+ * The most this entitlement permits, or absent for unlimited.
44
+ *
45
+ * Annotated as a number rather than left to bind as `bigint`. `u64` maps
46
+ * to `bigint` by default, and `JSON.parse` never produces one -- so the
47
+ * binding would describe a wire that cannot occur, and a validator
48
+ * written against it would reject every real payload. A seat count is
49
+ * small; the epochs above take the same annotation for the same reason.
50
+ */
51
+ atMost: number | null, };
52
+
53
+ export type LicenceSeatProjection = {
54
+ /**
55
+ * The installation's `MachineId`.
56
+ *
57
+ * Random and derived from nothing — not a MAC address, not a hardware
58
+ * serial, not anything about the user. `MachineId`'s own documentation
59
+ * makes that argument; this projection inherits it rather than restating
60
+ * it, and must never be widened to something derived.
61
+ */
62
+ machineId: string,
63
+ /**
64
+ * What the customer called this machine when they activated it.
65
+ *
66
+ * Supplied by the operator, never sniffed from the host. A hostname would
67
+ * be the convenient answer and reads as a fact even when it is wrong or
68
+ * personal; an absent label reads as "unnamed machine", which is honest.
69
+ */
70
+ label: string | null,
71
+ /**
72
+ * Whether this is the installation asking.
73
+ *
74
+ * Answered by the authority rather than left to the client to work out by
75
+ * comparing identifiers it had to be told separately.
76
+ */
77
+ thisMachine: boolean, };
78
+
79
+ export type HeldLicenceProjection = {
80
+ /**
81
+ * The product this licence covers.
82
+ */
83
+ product: string,
84
+ /**
85
+ * Whether the software may be used, and on what footing.
86
+ */
87
+ usability: LicenceUsabilityProjection,
88
+ /**
89
+ * How the licence was established.
90
+ */
91
+ trustBasis: LicenceTrustBasisProjection,
92
+ /**
93
+ * Every entitlement held.
94
+ */
95
+ entitlements: Array<LicenceEntitlementProjection>,
96
+ /**
97
+ * When use stops being permitted, when a window applies.
98
+ */
99
+ useUntil: Timestamp | null,
100
+ /**
101
+ * When updates stop being covered, when a window applies. Distinct from
102
+ * `use_until`: a perpetual licence with a lapsed update window still runs.
103
+ */
104
+ updateUntil: Timestamp | null,
105
+ /**
106
+ * Every machine holding a seat, this one included.
107
+ *
108
+ * Empty when the authority does not account for seats, which is a
109
+ * different thing from a licence with one seat and is why this is not an
110
+ * `Option`: a surface shows a list or shows nothing, and both are states
111
+ * rather than an absence of information.
112
+ */
113
+ seats: Array<LicenceSeatProjection>, };
114
+
115
+ export type LicenceSnapshot = {
116
+ /**
117
+ * Exact metadata protocol line.
118
+ */
119
+ protocolVersion: LicenceProtocolVersion,
120
+ /**
121
+ * Live authority lifetime. A plain count, as the operation, notification
122
+ * and update domains carry it.
123
+ */
124
+ authorityEpoch: number,
125
+ /**
126
+ * The licence held, or absent when none is.
127
+ */
128
+ licence: HeldLicenceProjection | null, };
129
+
130
+ export type LicenceCredentialProjection = { "kind": "key",
131
+ /**
132
+ * The key as typed. Grouping, case and confusable symbols are the
133
+ * authority's problem, not the client's.
134
+ */
135
+ key: string, } | { "kind": "accountToken",
136
+ /**
137
+ * The token.
138
+ */
139
+ token: string, } | { "kind": "licenceFile",
140
+ /**
141
+ * The file's bytes, base64. A `Vec<u8>` would arrive as an array of
142
+ * numbers, which is a poor way to move a file across a boundary.
143
+ */
144
+ contentsBase64: string, };
145
+
146
+ export type LicenceActivateCommand = {
147
+ /**
148
+ * Exact metadata protocol line.
149
+ */
150
+ protocolVersion: LicenceProtocolVersion,
151
+ /**
152
+ * Authority lifetime observed by the caller.
153
+ */
154
+ authorityEpoch: number,
155
+ /**
156
+ * What the customer presented.
157
+ */
158
+ credential: LicenceCredentialProjection,
159
+ /**
160
+ * What to call this machine in the seat list.
161
+ *
162
+ * Optional, and the only moment it can be asked for without inventing a
163
+ * separate settings screen. Absent is fine and shows as unnamed.
164
+ */
165
+ label: string | null, };
166
+
167
+ export type LicenceDeactivateCommand = {
168
+ /**
169
+ * Exact metadata protocol line.
170
+ */
171
+ protocolVersion: LicenceProtocolVersion,
172
+ /**
173
+ * Authority lifetime observed by the caller.
174
+ */
175
+ authorityEpoch: number, };
176
+
177
+ export type LicenceRefreshCommand = {
178
+ /**
179
+ * Exact metadata protocol line.
180
+ */
181
+ protocolVersion: LicenceProtocolVersion,
182
+ /**
183
+ * Authority lifetime observed by the caller.
184
+ */
185
+ authorityEpoch: number, };
186
+
187
+ export type LicenceReleaseSeatCommand = {
188
+ /**
189
+ * Exact metadata protocol line.
190
+ */
191
+ protocolVersion: LicenceProtocolVersion,
192
+ /**
193
+ * Authority lifetime observed by the caller.
194
+ */
195
+ authorityEpoch: number,
196
+ /**
197
+ * The machine to release.
198
+ */
199
+ machineId: string, };
200
+
201
+ export type LicenceRenameSeatCommand = {
202
+ /**
203
+ * Exact metadata protocol line.
204
+ */
205
+ protocolVersion: LicenceProtocolVersion,
206
+ /**
207
+ * Authority lifetime observed by the caller.
208
+ */
209
+ authorityEpoch: number,
210
+ /**
211
+ * The machine to rename.
212
+ */
213
+ machineId: string,
214
+ /**
215
+ * What to call it, or `None` for unnamed.
216
+ */
217
+ label: string | null, };
218
+
219
+ export type LicenceRejectionCode = "malformed" | "notRecognised" | "noSeatsFree" | "revoked" | "clockRefused" | "staleAuthority" | "seatNotFound" | "unreachable";
220
+
221
+ export type LicenceOutcomeProjection = { "status": "committed",
222
+ /**
223
+ * The state the command produced.
224
+ */
225
+ snapshot: LicenceSnapshot, } | { "status": "rejected",
226
+ /**
227
+ * What a surface should tell the operator.
228
+ */
229
+ code: LicenceRejectionCode,
230
+ /**
231
+ * The state as it remains.
232
+ */
233
+ snapshot: LicenceSnapshot, };
234
+
235
+ export type LicenceChangedKind = "activated" | "deactivated" | "seatRelabelled" | "refreshed" | "usabilityChanged";
236
+
237
+ export type LicenceChangedEvent = {
238
+ /**
239
+ * Exact metadata protocol line.
240
+ */
241
+ protocolVersion: LicenceProtocolVersion,
242
+ /**
243
+ * Live authority lifetime.
244
+ */
245
+ authorityEpoch: number,
246
+ /**
247
+ * Coarse invalidation category.
248
+ */
249
+ kind: LicenceChangedKind, };
@@ -0,0 +1,34 @@
1
+ // @generated by `effigy generate:licence`; do not edit.
2
+ // Allowed field names per tagged-union variant, from the Rust enums.
3
+ // Keyed by type, then by discriminant. The discriminant itself is in
4
+ // each list, because `exact()` compares every key.
5
+
6
+ export const LICENCE_VARIANT_FIELDS: Record<string, Record<string, readonly string[]>> = {
7
+ "LicenceUsabilityProjection": {
8
+ "active": ["state"],
9
+ "inGrace": ["state", "until"],
10
+ "useWindowExpired": ["state", "at"],
11
+ "leaseLapsed": ["state", "at"],
12
+ "clockRefused": ["state"],
13
+ },
14
+ "LicenceTrustBasisProjection": {
15
+ "offlineSignature": ["kind"],
16
+ "remoteAssertion": ["kind", "checked"],
17
+ },
18
+ "LicenceCredentialProjection": {
19
+ "key": ["kind", "key"],
20
+ "accountToken": ["kind", "token"],
21
+ "licenceFile": ["kind", "contentsBase64"],
22
+ },
23
+ "LicenceOutcomeProjection": {
24
+ "committed": ["status", "snapshot"],
25
+ "rejected": ["status", "code", "snapshot"],
26
+ },
27
+ };
28
+
29
+ export const LICENCE_VARIANT_FIELDS_DISCRIMINANTS: Record<string, string> = {
30
+ "LicenceUsabilityProjection": "state",
31
+ "LicenceTrustBasisProjection": "kind",
32
+ "LicenceCredentialProjection": "kind",
33
+ "LicenceOutcomeProjection": "status",
34
+ };
@@ -0,0 +1,14 @@
1
+ export * from "./generated/protocol.ts";
2
+ // The generated field maps are public API. A consumer that mirrors these types
3
+ // structurally has no compile-time link to them, so a field added here is
4
+ // silent on their side; whoever sees both can assert against these and fail
5
+ // when it drifts.
6
+ export * from "./generated/fields.ts";
7
+ export * from "./generated/variant-fields.ts";
8
+ export * from "./key.ts";
9
+ export * from "./validation.ts";
10
+ export * from "./ports.ts";
11
+ export * from "./client.ts";
12
+ export * from "./direct.ts";
13
+ export * from "./serialized.ts";
14
+ export * from "./controller.ts";
@@ -0,0 +1,125 @@
1
+ /**
2
+ * The licence key format, in TypeScript.
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.
10
+ *
11
+ * The two implementations are bound by `key-conformance.json`, generated from
12
+ * the Rust and asserted against here. Neither can change the format without
13
+ * the other's tests failing.
14
+ *
15
+ * This validates *shape*. Whether a well-formed key exists is the authority's
16
+ * question and cannot be answered here.
17
+ */
18
+
19
+ /** Crockford base32: no I, L, O, or U. */
20
+ const ALPHABET = "0123456789ABCDEFGHJKMNPQRSTVWXYZ";
21
+
22
+ /** The 32 symbols plus five check-only ones, giving the prime modulus 37. */
23
+ const CHECK_ALPHABET = "0123456789ABCDEFGHJKMNPQRSTVWXYZ*~$=U";
24
+
25
+ /** Symbols per group in the display form. */
26
+ const GROUP = 5;
27
+
28
+ /**
29
+ * The shortest key that may exist, check symbol included.
30
+ *
31
+ * Not a typing convenience. It is what makes distinguishing "not recognised"
32
+ * from "revoked" safe: telling a caller a well-formed key is unknown lets them
33
+ * learn which keys exist, and that only matters if keys are guessable.
34
+ * Nineteen body symbols of Crockford base32 is ninety-five bits.
35
+ */
36
+ export const MINIMUM_KEY_SYMBOLS = 20;
37
+
38
+ export type LicenceKeyProblem =
39
+ /** A symbol outside the alphabet. Mistyped. */
40
+ | { readonly kind: "unexpectedSymbol"; readonly symbol: string }
41
+ /** Too few symbols to be a key. Not a typo — a truncation or a wrong value. */
42
+ | { readonly kind: "tooShort"; readonly minimum: number; readonly actual: number }
43
+ /** The check symbol disagrees with the body. Mistyped. */
44
+ | { readonly kind: "checkFailed" };
45
+
46
+ export type LicenceKeyResult =
47
+ | { readonly ok: true; readonly key: string; readonly grouped: string }
48
+ | { readonly ok: false; readonly problem: LicenceKeyProblem };
49
+
50
+ /**
51
+ * Whether a problem means the customer mistyped.
52
+ *
53
+ * The distinction the whole file exists for. A surface that cannot tell these
54
+ * apart says "invalid licence key" to someone who transposed two characters,
55
+ * and they believe they were sold a dud.
56
+ */
57
+ export function isProbablyATypo(problem: LicenceKeyProblem): boolean {
58
+ return problem.kind === "checkFailed" || problem.kind === "unexpectedSymbol";
59
+ }
60
+
61
+ /**
62
+ * Parses a key as typed, accepting anything a person plausibly enters.
63
+ *
64
+ * Lower case, missing dashes, extra dashes, and surrounding or embedded
65
+ * whitespace are all accepted. So are the classic confusions: `I` and `L` read
66
+ * as `1`, `O` reads as `0`. Rejecting these would be rejecting the customer
67
+ * for the typeface's mistake.
68
+ */
69
+ export function parseLicenceKey(input: string): LicenceKeyResult {
70
+ const normalized = normalize(input);
71
+ if (!normalized.ok) return normalized;
72
+
73
+ const value = normalized.value;
74
+ if (value.length < MINIMUM_KEY_SYMBOLS) {
75
+ return {
76
+ ok: false,
77
+ problem: { kind: "tooShort", minimum: MINIMUM_KEY_SYMBOLS, actual: value.length },
78
+ };
79
+ }
80
+
81
+ const body = value.slice(0, -1);
82
+ if (value.slice(-1) !== checkSymbol(body)) {
83
+ return { ok: false, problem: { kind: "checkFailed" } };
84
+ }
85
+ return { ok: true, key: value, grouped: group(value) };
86
+ }
87
+
88
+ /** Renders a normalized key as `XXXXX-XXXXX-…`. */
89
+ 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));
92
+ return groups.join("-");
93
+ }
94
+
95
+ function normalize(
96
+ input: string,
97
+ ): { readonly ok: true; readonly value: string } | { readonly ok: false; readonly problem: LicenceKeyProblem } {
98
+ let output = "";
99
+ for (const character of input) {
100
+ if (character === "-" || /\s/u.test(character)) continue;
101
+ const upper = character.toUpperCase();
102
+ // The confusions Crockford excluded the symbols to avoid.
103
+ 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 } };
106
+ }
107
+ output += mapped;
108
+ }
109
+ return { ok: true, value: output };
110
+ }
111
+
112
+ /**
113
+ * The position-weighted check symbol for a body.
114
+ *
115
+ * Weighted by position on purpose: an unweighted sum accepts any reordering,
116
+ * and transposition is one of the two mistakes people actually make.
117
+ */
118
+ function checkSymbol(body: string): string {
119
+ let sum = 0;
120
+ for (let index = 0; index < body.length; index += 1) {
121
+ const value = ALPHABET.indexOf(body[index]!);
122
+ sum += (value === -1 ? 0 : value) * (index + 1);
123
+ }
124
+ return CHECK_ALPHABET[sum % 37]!;
125
+ }
@@ -0,0 +1,39 @@
1
+ import type {
2
+ LicenceActivateCommand,
3
+ LicenceChangedEvent,
4
+ LicenceDeactivateCommand,
5
+ LicenceOutcomeProjection,
6
+ LicenceRefreshCommand,
7
+ LicenceReleaseSeatCommand,
8
+ LicenceRenameSeatCommand,
9
+ LicenceSnapshot,
10
+ } from "./generated/protocol.ts";
11
+
12
+ export type LicenceUnlisten = () => void | Promise<void>;
13
+
14
+ /**
15
+ * The raw seam.
16
+ *
17
+ * Every result is `unknown`. What arrives over a transport is untrusted until
18
+ * a validator says otherwise, and `LicenceClient` is what says so.
19
+ */
20
+ export interface LicencePort {
21
+ snapshot(): Promise<unknown>;
22
+ activate(command: LicenceActivateCommand): Promise<unknown>;
23
+ deactivate(command: LicenceDeactivateCommand): Promise<unknown>;
24
+ refresh(command: LicenceRefreshCommand): Promise<unknown>;
25
+ releaseSeat(command: LicenceReleaseSeatCommand): Promise<unknown>;
26
+ renameSeat(command: LicenceRenameSeatCommand): Promise<unknown>;
27
+ listen?(listener: (event: unknown) => void): LicenceUnlisten | Promise<LicenceUnlisten>;
28
+ }
29
+
30
+ /** The same seam after validation. */
31
+ export interface CheckedLicencePort {
32
+ snapshot(): Promise<LicenceSnapshot>;
33
+ activate(command: LicenceActivateCommand): Promise<LicenceOutcomeProjection>;
34
+ deactivate(command: LicenceDeactivateCommand): Promise<LicenceOutcomeProjection>;
35
+ refresh(command: LicenceRefreshCommand): Promise<LicenceOutcomeProjection>;
36
+ releaseSeat(command: LicenceReleaseSeatCommand): Promise<LicenceOutcomeProjection>;
37
+ renameSeat(command: LicenceRenameSeatCommand): Promise<LicenceOutcomeProjection>;
38
+ listen(listener: (event: LicenceChangedEvent) => void): Promise<LicenceUnlisten>;
39
+ }
@@ -0,0 +1,13 @@
1
+ import type { LicenceActivateCommand, LicenceDeactivateCommand, LicenceRefreshCommand, LicenceReleaseSeatCommand, LicenceRenameSeatCommand } from "./generated/protocol.ts";
2
+ import type { LicencePort, LicenceUnlisten } from "./ports.ts";
3
+ export class SerializedLicencePort implements LicencePort {
4
+ 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
+ }
13
+ function clone<T>(value: T): T { return JSON.parse(JSON.stringify(value)) as T; }