@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,3 +1,13 @@
1
+ import { isOpaqueId } from "../core/identifiers.ts";
2
+ import { isUnicodeText } from "../core/json-value.ts";
3
+ import { isLayoutRatio } from "../layout/ratio.ts";
4
+ import {
5
+ checkWireFields,
6
+ readWireRecord,
7
+ unsigned32,
8
+ unsignedInteger,
9
+ wireVariantFields,
10
+ } from "../core/record.ts";
1
11
  import {
2
12
  SURFACE_VARIANT_FIELDS,
3
13
  SURFACE_VARIANT_FIELDS_DISCRIMINANTS,
@@ -7,12 +17,15 @@ import {
7
17
  SURFACE_MUTATION_OUTCOME_KINDS,
8
18
  SURFACE_MUTATION_REJECTION_CODES,
9
19
  SURFACE_PROTOCOL_VERSION,
10
- type SurfaceChangedEvent,
11
- type SurfaceMutationCommand,
12
- type SurfaceMutationOutcome,
13
- type SurfaceMutationRejectionCode,
14
- type SurfaceMutationResponse,
15
- type SurfaceSnapshot,
20
+ } from "./generated/protocol.ts";
21
+ import type {
22
+ SurfaceChangedEvent,
23
+ SurfaceDocument,
24
+ SurfaceMutationCommand,
25
+ SurfaceMutationOutcome,
26
+ SurfaceMutationRejectionCode,
27
+ SurfaceMutationResponse,
28
+ SurfaceSnapshot,
16
29
  } from "./generated/protocol.ts";
17
30
  import { SURFACE_FIELDS } from "./generated/fields.ts";
18
31
 
@@ -22,6 +35,7 @@ export type SurfaceProtocolValidationCode =
22
35
  | "unknown_outcome"
23
36
  | "unknown_rejection_code"
24
37
  | "unknown_response_status"
38
+ | "uncorrelated_response"
25
39
  | "invalid_object"
26
40
  | "unknown_field"
27
41
  | "missing_field";
@@ -42,23 +56,30 @@ export function assertSurfaceProtocolVersion(
42
56
  version: unknown,
43
57
  ): asserts version is typeof SURFACE_PROTOCOL_VERSION {
44
58
  if (version !== SURFACE_PROTOCOL_VERSION) {
45
- throw new SurfaceProtocolValidationError(
46
- "unsupported_protocol_version",
47
- version,
48
- );
59
+ throw new SurfaceProtocolValidationError("unsupported_protocol_version", version);
49
60
  }
50
61
  }
51
62
 
52
63
  export function assertValidSurfaceMutationCommand(
53
64
  value: unknown,
54
65
  ): asserts value is SurfaceMutationCommand {
55
- assertKnownKind(value, SURFACE_MUTATION_COMMAND_KINDS, "unknown_command", "SurfaceMutationCommand");
66
+ assertKnownKind(
67
+ value,
68
+ SURFACE_MUTATION_COMMAND_KINDS,
69
+ "unknown_command",
70
+ "SurfaceMutationCommand",
71
+ );
56
72
  }
57
73
 
58
74
  export function assertValidSurfaceMutationOutcome(
59
75
  value: unknown,
60
76
  ): asserts value is SurfaceMutationOutcome {
61
- assertKnownKind(value, SURFACE_MUTATION_OUTCOME_KINDS, "unknown_outcome", "SurfaceMutationOutcome");
77
+ assertKnownKind(
78
+ value,
79
+ SURFACE_MUTATION_OUTCOME_KINDS,
80
+ "unknown_outcome",
81
+ "SurfaceMutationOutcome",
82
+ );
62
83
  }
63
84
 
64
85
  export function assertValidSurfaceMutationRejectionCode(
@@ -66,134 +87,241 @@ export function assertValidSurfaceMutationRejectionCode(
66
87
  ): asserts value is SurfaceMutationRejectionCode {
67
88
  if (
68
89
  typeof value !== "string" ||
69
- !SURFACE_MUTATION_REJECTION_CODES.includes(
70
- value as (typeof SURFACE_MUTATION_REJECTION_CODES)[number],
71
- )
90
+ !SURFACE_MUTATION_REJECTION_CODES.some((code) => code === value)
72
91
  ) {
73
- throw new SurfaceProtocolValidationError(
74
- "unknown_rejection_code",
75
- value,
76
- );
92
+ throw new SurfaceProtocolValidationError("unknown_rejection_code", value);
77
93
  }
78
94
  }
79
95
 
80
96
  export function assertValidSurfaceMutationResponse(
81
97
  value: unknown,
82
98
  ): asserts value is SurfaceMutationResponse {
83
- // Was `record(value, SURFACE_FIELDS.SurfaceMutationResponse)`. The flat
84
- // field map skips unions by design, so that lookup was `undefined` and
85
- // `record` checked no keys at all -- a call that read as strict and was not.
86
99
  const response = record(value);
87
100
  switch (response.status) {
88
101
  case "committed": {
89
102
  record(response, variantKeys("SurfaceMutationResponse", response));
90
103
  const receipt = record(response.receipt, SURFACE_FIELDS.SurfaceMutationReceipt);
91
- assertValidSurfaceMutationOutcome(record(receipt.outcome));
104
+ assertFields(receipt, {
105
+ request_id: isOpaqueId,
106
+ previous_revision: unsignedInteger,
107
+ committed_revision: unsignedInteger,
108
+ });
109
+ assertValidSurfaceMutationOutcome(receipt.outcome);
110
+ assertValidSurfaceDocument(receipt.authoritative_document);
92
111
  return;
93
112
  }
94
113
  case "rejected": {
95
114
  record(response, variantKeys("SurfaceMutationResponse", response));
96
115
  const rejection = record(response.rejection, SURFACE_FIELDS.SurfaceMutationRejection);
116
+ assertFields(rejection, {
117
+ request_id: isOpaqueId,
118
+ current_revision: unsignedInteger,
119
+ detail: isString,
120
+ });
97
121
  assertValidSurfaceMutationRejectionCode(rejection.code);
122
+ assertValidSurfaceDocument(rejection.authoritative_document);
98
123
  return;
99
124
  }
100
125
  default:
101
- throw new SurfaceProtocolValidationError(
102
- "unknown_response_status",
103
- response.status,
104
- );
126
+ throw new SurfaceProtocolValidationError("unknown_response_status", response.status);
105
127
  }
106
128
  }
107
129
 
108
- export function assertValidSurfaceSnapshot(
109
- value: unknown,
110
- ): asserts value is SurfaceSnapshot {
111
- assertSurfaceProtocolVersion(
112
- record(value, SURFACE_FIELDS.SurfaceSnapshot).protocol_version,
113
- );
130
+ export function assertValidSurfaceSnapshot(value: unknown): asserts value is SurfaceSnapshot {
131
+ const snapshot = record(value, SURFACE_FIELDS.SurfaceSnapshot);
132
+ assertSurfaceCursor(snapshot);
133
+ const document = snapshot.document;
134
+ assertValidSurfaceDocument(document);
135
+ if (document.revision !== snapshot.revision) {
136
+ throw new SurfaceProtocolValidationError("invalid_object", document);
137
+ }
138
+ }
139
+
140
+ /** The whole authoritative document a snapshot, receipt or transfer carries. */
141
+ export function assertValidSurfaceDocument(value: unknown): asserts value is SurfaceDocument {
142
+ const document = record(value, SURFACE_FIELDS.SurfaceDocument);
143
+ assertFields(document, { revision: unsignedInteger });
144
+ for (const entry of array(document.surfaces)) {
145
+ validateSurface(entry);
146
+ }
147
+ for (const entry of array(document.panel_instances)) {
148
+ assertFields(record(entry, SURFACE_FIELDS.PanelInstance), {
149
+ id: isOpaqueId,
150
+ definition_id: isOpaqueId,
151
+ });
152
+ }
153
+ for (const entry of array(document.windows)) {
154
+ assertFields(record(entry, SURFACE_FIELDS.ParticipatingWindow), {
155
+ id: isOpaqueId,
156
+ active_surface_id: isNullableId,
157
+ });
158
+ }
114
159
  }
115
160
 
116
161
  export function assertValidSurfaceChangedEvent(
117
162
  value: unknown,
118
163
  ): asserts value is SurfaceChangedEvent {
119
- assertSurfaceProtocolVersion(
120
- record(value, SURFACE_FIELDS.SurfaceChangedEvent).protocol_version,
121
- );
164
+ assertSurfaceCursor(record(value, SURFACE_FIELDS.SurfaceChangedEvent));
165
+ }
166
+
167
+ function assertSurfaceCursor(value: Record<string, unknown>): void {
168
+ assertSurfaceProtocolVersion(value.protocol_version);
169
+ if (!unsignedInteger(value.epoch) || !unsignedInteger(value.revision)) {
170
+ throw new SurfaceProtocolValidationError("invalid_object", value);
171
+ }
172
+ }
173
+
174
+ function validateSurface(value: unknown): void {
175
+ const surface = record(value, SURFACE_FIELDS.SurfaceRecord);
176
+ assertFields(surface, { id: isOpaqueId, schema_id: isOpaqueId, label: isNullableText });
177
+ validatePresentation(surface.presentation);
178
+ for (const entry of array(surface.regions)) {
179
+ const region = record(entry, SURFACE_FIELDS.RegionState);
180
+ assertFields(region, {
181
+ region_id: isOpaqueId,
182
+ active_panel_instance_id: isNullableId,
183
+ collapsed: isNullableBoolean,
184
+ });
185
+ assertIds(region.panel_instance_ids);
186
+ }
187
+ for (const entry of array(surface.sizing_slots)) {
188
+ assertFields(record(entry, SURFACE_FIELDS.SizingSlotState), {
189
+ sizing_slot_id: isOpaqueId,
190
+ ratio: isLayoutRatio,
191
+ });
192
+ }
193
+ validateHostPreferences(surface.host_preferences);
194
+ }
195
+
196
+ function validatePresentation(value: unknown): void {
197
+ const presentation = record(value);
198
+ record(presentation, variantKeys("SurfacePresentation", presentation));
199
+ if (presentation.kind === "focused_panel") {
200
+ assertFields(presentation, { panel_definition_id: isOpaqueId });
201
+ }
202
+ }
203
+
204
+ function validateHostPreferences(value: unknown): void {
205
+ for (const entry of array(value)) {
206
+ assertFields(record(entry, SURFACE_FIELDS.SurfaceHostPreference), {
207
+ window_id: isOpaqueId,
208
+ order: unsigned32,
209
+ });
210
+ }
211
+ }
212
+
213
+ type MutationFields<Variant> = Variant extends { kind: string }
214
+ ? Exclude<keyof Variant, "kind">
215
+ : never;
216
+
217
+ // Each check throws, and a new Rust variant field fails to typecheck until it
218
+ // has one.
219
+ const mutationFields = {
220
+ surface_id: field(isOpaqueId),
221
+ source_surface_id: field(isOpaqueId),
222
+ schema_id: field(isOpaqueId),
223
+ window_id: field(isOpaqueId),
224
+ source_window_id: field(isOpaqueId),
225
+ target_window_id: field(isOpaqueId),
226
+ previous_active_surface_id: field(isNullableId),
227
+ label: field(isNullableText),
228
+ insertion_index: field(unsigned32),
229
+ surface_ids: assertIds,
230
+ host_preferences: validateHostPreferences,
231
+ presentation: validatePresentation,
232
+ previous_presentation: validatePresentation,
233
+ } satisfies Record<
234
+ MutationFields<SurfaceMutationCommand | SurfaceMutationOutcome>,
235
+ (value: unknown) => void
236
+ >;
237
+
238
+ function field(admits: (value: unknown) => boolean): (value: unknown) => void {
239
+ return (value) => {
240
+ if (!admits(value)) {
241
+ throw new SurfaceProtocolValidationError("invalid_object", value);
242
+ }
243
+ };
244
+ }
245
+
246
+ function assertFields(
247
+ value: Record<string, unknown>,
248
+ checks: Readonly<Record<string, (member: unknown) => boolean>>,
249
+ ): void {
250
+ for (const [key, admits] of Object.entries(checks)) {
251
+ if (!admits(value[key])) {
252
+ throw new SurfaceProtocolValidationError("invalid_object", { key, value });
253
+ }
254
+ }
255
+ }
256
+
257
+ function assertIds(value: unknown): void {
258
+ for (const id of array(value)) {
259
+ if (!isOpaqueId(id)) {
260
+ throw new SurfaceProtocolValidationError("invalid_object", value);
261
+ }
262
+ }
263
+ }
264
+
265
+ function isString(value: unknown): boolean {
266
+ return typeof value === "string";
267
+ }
268
+
269
+ function isNullableText(value: unknown): boolean {
270
+ return value === null || (typeof value === "string" && isUnicodeText(value));
271
+ }
272
+
273
+ function isNullableId(value: unknown): boolean {
274
+ return value === null || isOpaqueId(value);
275
+ }
276
+
277
+ function isNullableBoolean(value: unknown): boolean {
278
+ return value === null || typeof value === "boolean";
279
+ }
280
+
281
+ function array(value: unknown): ReadonlyArray<unknown> {
282
+ if (!Array.isArray(value)) {
283
+ throw new SurfaceProtocolValidationError("invalid_object", value);
284
+ }
285
+ return value;
122
286
  }
123
287
 
124
288
  function assertKnownKind(
125
289
  value: unknown,
126
- known: readonly string[],
290
+ known: ReadonlyArray<string>,
127
291
  code: "unknown_command" | "unknown_outcome",
128
292
  type: string,
129
293
  ): asserts value is Record<"kind", string> {
130
294
  const candidate = record(value);
131
- if (
132
- typeof candidate.kind !== "string" ||
133
- !known.includes(candidate.kind)
134
- ) {
295
+ if (typeof candidate.kind !== "string" || !known.includes(candidate.kind)) {
135
296
  throw new SurfaceProtocolValidationError(code, value);
136
297
  }
137
- // The discriminant is checked above, so a missing map entry here means the
138
- // generator failed rather than that a caller sent something odd.
139
- record(candidate, variantKeys(type, candidate));
298
+ const keys = variantKeys(type, candidate);
299
+ record(candidate, keys);
300
+ for (const [name, check] of Object.entries(mutationFields)) {
301
+ if (keys.includes(name)) {
302
+ check(candidate[name]);
303
+ }
304
+ }
140
305
  }
141
306
 
142
- /**
143
- * Allowed keys for one tagged-union variant, from the generated map, with the
144
- * discriminant's name read from the map too.
145
- */
146
- function variantKeys(
147
- type: string,
148
- value: Record<string, unknown>,
149
- ): readonly string[] {
150
- const discriminant = value[SURFACE_VARIANT_FIELDS_DISCRIMINANTS[type] ?? "kind"];
151
- const keys = SURFACE_VARIANT_FIELDS[type]?.[discriminant as string];
152
- if (keys === undefined) {
153
- throw new SurfaceProtocolValidationError("unknown_outcome", { type, discriminant });
154
- }
155
- return keys;
156
- }
157
-
158
- /**
159
- * Rejects a non-object, an unknown key, and a missing key.
160
- *
161
- * `allowed` comes from the generated field map, so the keys this accepts are
162
- * the Rust struct's and nothing else — contract 010's Boundary Validation
163
- * Target. Passing no list keeps the old shape-only behaviour for the tagged
164
- * unions, whose allowed keys depend on their discriminant and so are not one
165
- * flat set.
166
- *
167
- * The non-object case used to throw `unknown_response_status`, because the
168
- * incompatibility union had no code for it. It reported the wrong thing for
169
- * every caller but one.
170
- */
171
- function record(
172
- value: unknown,
173
- allowed?: readonly string[],
174
- ): Record<string, unknown> {
175
- if (typeof value !== "object" || value === null || Array.isArray(value)) {
176
- throw new SurfaceProtocolValidationError("invalid_object", value);
177
- }
178
- const result = value as Record<string, unknown>;
179
- if (allowed === undefined) return result;
307
+ function variantKeys(type: string, value: Record<string, unknown>): ReadonlyArray<string> {
308
+ return wireVariantFields(
309
+ SURFACE_VARIANT_FIELDS,
310
+ SURFACE_VARIANT_FIELDS_DISCRIMINANTS,
311
+ type,
312
+ value,
313
+ (discriminant) => {
314
+ throw new SurfaceProtocolValidationError("unknown_outcome", { type, discriminant });
315
+ },
316
+ );
317
+ }
180
318
 
181
- const permitted = new Set(allowed);
182
- for (const key of Object.keys(result)) {
183
- if (!permitted.has(key)) {
184
- throw new SurfaceProtocolValidationError("unknown_field", {
185
- key,
186
- value,
187
- });
188
- }
189
- }
190
- for (const key of allowed) {
191
- if (!(key in result)) {
192
- throw new SurfaceProtocolValidationError("missing_field", {
193
- key,
194
- value,
195
- });
196
- }
197
- }
319
+ function record(value: unknown, allowed?: ReadonlyArray<string>): Record<string, unknown> {
320
+ const result = readWireRecord(value, () => {
321
+ throw new SurfaceProtocolValidationError("invalid_object", value);
322
+ });
323
+ checkWireFields(result, allowed, (code, key) => {
324
+ throw new SurfaceProtocolValidationError(code, { key, value });
325
+ });
198
326
  return result;
199
327
  }
@@ -1,19 +1,16 @@
1
- import {
2
- isEventTransport,
3
- type ConnectionFailureReporter,
4
- type InvokeTransport,
5
- } from "@inflatable-cookie/longhorn/core";
1
+ import { isEventTransport } from "@inflatable-cookie/longhorn/core";
2
+ import type { ConnectionFailureReporter, InvokeTransport } from "@inflatable-cookie/longhorn/core";
6
3
 
7
- import {
8
- type PanelSessionStartRequest,
9
- type PanelTransferCommand,
10
- type PanelTransferResponse,
11
- type TransferCancelRequest,
12
- type TransferCancelResponse,
13
- type TransferClientSnapshot,
14
- type TransferLeaseRequest,
15
- type TransferLeaseResponse,
16
- type TransferSessionResponse,
4
+ import type {
5
+ PanelSessionStartRequest,
6
+ PanelTransferCommand,
7
+ PanelTransferResponse,
8
+ TransferCancelRequest,
9
+ TransferCancelResponse,
10
+ TransferClientSnapshot,
11
+ TransferLeaseRequest,
12
+ TransferLeaseResponse,
13
+ TransferSessionResponse,
17
14
  } from "./generated/protocol.ts";
18
15
  import {
19
16
  assertValidPanelTransferResponse,
@@ -25,18 +22,13 @@ import {
25
22
  assertValidTransferTargetBinding,
26
23
  assertTransferProtocolVersion,
27
24
  } from "./validation.ts";
28
- import {
29
- TransferClientConnection,
30
- type TransferClientSnapshotListener,
31
- } from "./connection.ts";
25
+ import { TransferClientConnection } from "./connection.ts";
26
+ import type { TransferClientSnapshotListener } from "./connection.ts";
32
27
 
33
28
  export const TRANSFER_SNAPSHOT_COMMAND = "longhorn_transfer_snapshot";
34
- export const TRANSFER_START_PANEL_COMMAND =
35
- "longhorn_transfer_start_panel";
36
- export const TRANSFER_PUBLISH_LEASE_COMMAND =
37
- "longhorn_transfer_publish_lease";
38
- export const TRANSFER_COMMIT_PANEL_COMMAND =
39
- "longhorn_transfer_commit_panel";
29
+ export const TRANSFER_START_PANEL_COMMAND = "longhorn_transfer_start_panel";
30
+ export const TRANSFER_PUBLISH_LEASE_COMMAND = "longhorn_transfer_publish_lease";
31
+ export const TRANSFER_COMMIT_PANEL_COMMAND = "longhorn_transfer_commit_panel";
40
32
  export const TRANSFER_CANCEL_COMMAND = "longhorn_transfer_cancel";
41
33
 
42
34
  export class TransferClient {
@@ -57,9 +49,7 @@ export class TransferClient {
57
49
  onFailure?: ConnectionFailureReporter,
58
50
  ): TransferClientConnection {
59
51
  if (!isEventTransport(this.#transport)) {
60
- throw new TypeError(
61
- "transfer connection requires an event-capable transport",
62
- );
52
+ throw new TypeError("transfer connection requires an event-capable transport");
63
53
  }
64
54
  return new TransferClientConnection(
65
55
  this.#transport,
@@ -69,52 +59,28 @@ export class TransferClient {
69
59
  );
70
60
  }
71
61
 
72
- async startPanel(
73
- request: PanelSessionStartRequest,
74
- ): Promise<TransferSessionResponse> {
62
+ async startPanel(request: PanelSessionStartRequest): Promise<TransferSessionResponse> {
75
63
  assertTransferProtocolVersion(request.protocol_version);
76
- return this.#invoke(
77
- TRANSFER_START_PANEL_COMMAND,
78
- request,
79
- assertValidTransferSessionResponse,
80
- );
64
+ return this.#invoke(TRANSFER_START_PANEL_COMMAND, request, assertValidTransferSessionResponse);
81
65
  }
82
66
 
83
- async publishLease(
84
- request: TransferLeaseRequest,
85
- ): Promise<TransferLeaseResponse> {
67
+ async publishLease(request: TransferLeaseRequest): Promise<TransferLeaseResponse> {
86
68
  assertTransferProtocolVersion(request.protocol_version);
87
69
  for (const zone of request.zones) {
88
70
  assertValidTransferTargetBinding(zone.target);
89
71
  }
90
- return this.#invoke(
91
- TRANSFER_PUBLISH_LEASE_COMMAND,
92
- request,
93
- assertValidTransferLeaseResponse,
94
- );
72
+ return this.#invoke(TRANSFER_PUBLISH_LEASE_COMMAND, request, assertValidTransferLeaseResponse);
95
73
  }
96
74
 
97
- async commitPanel(
98
- request: PanelTransferCommand,
99
- ): Promise<PanelTransferResponse> {
75
+ async commitPanel(request: PanelTransferCommand): Promise<PanelTransferResponse> {
100
76
  assertTransferProtocolVersion(request.protocol_version);
101
77
  assertValidTransferCommitSelector(request.selector);
102
- return this.#invoke(
103
- TRANSFER_COMMIT_PANEL_COMMAND,
104
- request,
105
- assertValidPanelTransferResponse,
106
- );
78
+ return this.#invoke(TRANSFER_COMMIT_PANEL_COMMAND, request, assertValidPanelTransferResponse);
107
79
  }
108
80
 
109
- async cancel(
110
- request: TransferCancelRequest,
111
- ): Promise<TransferCancelResponse> {
81
+ async cancel(request: TransferCancelRequest): Promise<TransferCancelResponse> {
112
82
  assertTransferProtocolVersion(request.protocol_version);
113
- return this.#invoke(
114
- TRANSFER_CANCEL_COMMAND,
115
- request,
116
- assertValidTransferCancelResponse,
117
- );
83
+ return this.#invoke(TRANSFER_CANCEL_COMMAND, request, assertValidTransferCancelResponse);
118
84
  }
119
85
 
120
86
  async #invoke<T>(
@@ -1,61 +1,42 @@
1
- import {
2
- CheckedSnapshotConnection,
3
- type ConnectionFailure,
4
- type ConnectionFailureReporter,
5
- type EventTransport,
6
- } from "@inflatable-cookie/longhorn/core";
1
+ import { CheckedSnapshotConnection, CheckedSubscription } from "@inflatable-cookie/longhorn/core";
2
+ import type { ConnectionFailureReporter, EventTransport } from "@inflatable-cookie/longhorn/core";
7
3
 
8
4
  import type { TransferClientSnapshot } from "./generated/protocol.ts";
9
5
  import { assertValidTransferClientSnapshot } from "./validation.ts";
10
6
 
11
- export const TRANSFER_CLIENT_CHANGED_EVENT =
12
- "longhorn://transfer/client-changed";
7
+ export const TRANSFER_CLIENT_CHANGED_EVENT = "longhorn://transfer/client-changed";
13
8
 
14
- export type TransferClientSnapshotListener = (
15
- snapshot: TransferClientSnapshot,
16
- ) => void;
17
-
18
- export class TransferClientConnection {
19
- readonly ready: Promise<TransferClientSnapshot>;
20
-
21
- readonly #connection: CheckedSnapshotConnection<TransferClientSnapshot>;
9
+ export type TransferClientSnapshotListener = (snapshot: TransferClientSnapshot) => void;
22
10
 
11
+ export class TransferClientConnection extends CheckedSubscription<
12
+ TransferClientSnapshot,
13
+ TransferClientSnapshot
14
+ > {
23
15
  constructor(
24
16
  transport: EventTransport,
25
17
  loadSnapshot: () => Promise<TransferClientSnapshot>,
26
18
  listener?: TransferClientSnapshotListener,
27
19
  onFailure?: ConnectionFailureReporter,
28
20
  ) {
29
- this.#connection = new CheckedSnapshotConnection({
30
- listen: (receive) =>
31
- transport.listen(TRANSFER_CLIENT_CHANGED_EVENT, receive),
32
- loadSnapshot,
33
- validateSnapshot: parseTransferClientSnapshot,
34
- handleEvent: (value) => ({ kind: "snapshot", value }),
35
- isNewer: isCurrentTransferClientSnapshot,
36
- onSnapshot: listener,
37
- onFailure,
38
- disposedBeforeReadyError: () =>
39
- new TransferClientConnectionDisposedError(),
40
- });
41
- this.ready = this.#connection.ready.then((snapshot) => {
42
- if (snapshot === undefined) {
43
- throw new TransferClientConnectionDisposedError();
44
- }
45
- return snapshot;
46
- });
47
- }
48
-
49
- current(): TransferClientSnapshot | undefined {
50
- return this.#connection.current();
51
- }
52
-
53
- failures(): readonly ConnectionFailure[] {
54
- return this.#connection.failures();
55
- }
56
-
57
- dispose(): Promise<void> {
58
- return this.#connection.dispose();
21
+ super(
22
+ new CheckedSnapshotConnection({
23
+ listen: (receive) =>
24
+ transport.listen(TRANSFER_CLIENT_CHANGED_EVENT, receive, { scope: "current-webview" }),
25
+ loadSnapshot,
26
+ validateSnapshot: parseTransferClientSnapshot,
27
+ handleEvent: (value) => ({ kind: "snapshot", value }),
28
+ isNewer: isCurrentTransferClientSnapshot,
29
+ onSnapshot: listener,
30
+ onFailure,
31
+ disposedBeforeReadyError: () => new TransferClientConnectionDisposedError(),
32
+ }),
33
+ (snapshot) => {
34
+ if (snapshot === undefined) {
35
+ throw new TransferClientConnectionDisposedError();
36
+ }
37
+ return snapshot;
38
+ },
39
+ );
59
40
  }
60
41
  }
61
42
 
@@ -66,9 +47,7 @@ export class TransferClientConnectionDisposedError extends Error {
66
47
  }
67
48
  }
68
49
 
69
- function parseTransferClientSnapshot(
70
- value: unknown,
71
- ): TransferClientSnapshot {
50
+ function parseTransferClientSnapshot(value: unknown): TransferClientSnapshot {
72
51
  assertValidTransferClientSnapshot(value);
73
52
  return value;
74
53
  }
@@ -81,7 +60,6 @@ function isCurrentTransferClientSnapshot(
81
60
  return true;
82
61
  }
83
62
  return (
84
- candidate.client_epoch === current.client_epoch &&
85
- candidate.client_id === current.client_id
63
+ candidate.client_epoch === current.client_epoch && candidate.client_id === current.client_id
86
64
  );
87
65
  }
@@ -1,23 +1,24 @@
1
- // @generated by `effigy generate:transfer`; do not edit.
1
+ // @generated by `effigy generate:bindings`; do not edit.
2
2
  // Allowed field names per protocol type, from the Rust structs.
3
3
  // A `record()` given one of these rejects unknown and missing keys —
4
4
  // contract 010's Boundary Validation Target.
5
5
 
6
- export const TRANSFER_FIELDS: Record<string, readonly string[]> = {
6
+ export const TRANSFER_FIELDS = {
7
7
  "ClientPoint": ["x", "y"],
8
8
  "ClientSize": ["width", "height"],
9
9
  "ClientRect": ["origin", "size"],
10
10
  "TransferPayload": ["protocol_version", "session_id"],
11
11
  "PanelSessionStartRequest": ["protocol_version", "request_id", "panel_instance_id"],
12
12
  "TransferCancelRequest": ["protocol_version", "request_id", "session_id"],
13
- "ClientDropZone": ["id", "bounds", "insertion_position", "accepted_capability", "target"],
13
+ "ClientDropZone": ["id", "bounds", "insertion_position", "target"],
14
14
  "TransferLeaseRequest": ["protocol_version", "request_id", "client_id", "client_epoch", "generation", "zones"],
15
15
  "PanelTransferCommand": ["protocol_version", "request_id", "session_id", "selector"],
16
- "TransferClientSnapshot": ["protocol_version", "client_id", "client_epoch", "current_lease_generation"],
16
+ "TransferClientSnapshot": ["protocol_version", "client_id", "client_epoch"],
17
17
  "TransferSessionStarted": ["protocol_version", "request_id", "payload"],
18
18
  "TransferLeaseReceipt": ["protocol_version", "request_id", "client_id", "client_epoch", "generation", "zone_count"],
19
19
  "TransferCancelReceipt": ["protocol_version", "request_id", "session_id", "status"],
20
20
  "TransferCommittedTarget": ["path", "window_id", "drop_zone_id", "insertion_position", "binding"],
21
21
  "PanelTransferCompletion": ["protocol_version", "request_id", "session_id", "source_host_binding_id", "target_host_binding_id", "source_binding_kind", "target_binding_kind", "previous_revision", "committed_revision", "authoritative_document", "target"],
22
- "TransferAbort": ["protocol_version", "request_id", "source", "message", "retryable", "session_consumed"],
23
- };
22
+ "TransferAbort": ["protocol_version", "request_id", "source", "layout_code", "message", "retryable", "session_consumed"],
23
+ "TransferHostError": ["code", "message", "retryable"],
24
+ } as const;