@inflatable-cookie/longhorn 0.5.0 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (126) hide show
  1. package/package.json +1 -1
  2. package/src/agent-control/inject.ts +4 -1
  3. package/src/agent-control/selection.ts +35 -20
  4. package/src/agent-control/shim.ts +302 -144
  5. package/src/bridge/direct.ts +1 -1
  6. package/src/bridge/generated/fields.ts +3 -2
  7. package/src/bridge/generated/protocol.ts +7 -3
  8. package/src/bridge/job.ts +4 -14
  9. package/src/bridge/loopback.ts +7 -25
  10. package/src/bridge/operation.ts +13 -43
  11. package/src/bridge/router.ts +10 -23
  12. package/src/bridge/runtime/connection.ts +77 -105
  13. package/src/bridge/runtime/policy.ts +8 -28
  14. package/src/bridge/runtime/retry.ts +6 -18
  15. package/src/bridge/session.ts +4 -14
  16. package/src/bridge/stream/connection.ts +16 -38
  17. package/src/bridge/stream/source.ts +2 -6
  18. package/src/bridge/stream/tracker.ts +7 -12
  19. package/src/bridge/supervision.ts +11 -26
  20. package/src/bridge/validation/base.ts +17 -39
  21. package/src/bridge/validation/jobs.ts +11 -25
  22. package/src/bridge/validation/negotiation.ts +27 -37
  23. package/src/bridge/validation/operations.ts +28 -52
  24. package/src/bridge/validation/streams.ts +3 -10
  25. package/src/commands/client.ts +21 -26
  26. package/src/commands/controller.ts +32 -47
  27. package/src/commands/generated/fields.ts +3 -2
  28. package/src/commands/generated/protocol.ts +4 -0
  29. package/src/commands/keyboard.ts +92 -89
  30. package/src/commands/ports.ts +6 -12
  31. package/src/commands/projectors.ts +50 -48
  32. package/src/commands/validation.ts +70 -97
  33. package/src/config/client.ts +6 -19
  34. package/src/config/generated/fields.ts +2 -2
  35. package/src/config/names.ts +6 -12
  36. package/src/config/validation/commands.ts +5 -17
  37. package/src/config/validation/outcomes.ts +31 -39
  38. package/src/config/validation/primitives.ts +36 -53
  39. package/src/config/validation/projection.ts +66 -94
  40. package/src/core/connection.ts +70 -61
  41. package/src/core/record.ts +4 -0
  42. package/src/core/transport.ts +4 -15
  43. package/src/core/wire-copy.ts +6 -0
  44. package/src/history/client.ts +6 -12
  45. package/src/history/controller.ts +46 -46
  46. package/src/history/direct.ts +1 -3
  47. package/src/history/generated/fields.ts +2 -2
  48. package/src/history/ports.ts +4 -6
  49. package/src/history/serialized.ts +9 -13
  50. package/src/history/validation.ts +71 -55
  51. package/src/history-tree/client.ts +87 -11
  52. package/src/history-tree/controller.ts +313 -70
  53. package/src/history-tree/direct.ts +3 -1
  54. package/src/history-tree/generated/fields.ts +2 -2
  55. package/src/history-tree/ports.ts +3 -1
  56. package/src/history-tree/serialized.ts +39 -11
  57. package/src/history-tree/validation.ts +262 -63
  58. package/src/layout/generated/protocol.ts +2 -2
  59. package/src/layout/ratio.ts +2 -8
  60. package/src/layout/validation.ts +10 -19
  61. package/src/layout/visibility.ts +1 -2
  62. package/src/licence/client.ts +44 -7
  63. package/src/licence/controller.ts +76 -40
  64. package/src/licence/direct.ts +3 -1
  65. package/src/licence/generated/fields.ts +2 -2
  66. package/src/licence/generated/key-conformance.json +28 -0
  67. package/src/licence/generated/protocol.ts +1 -1
  68. package/src/licence/key.ts +40 -12
  69. package/src/licence/ports.ts +3 -1
  70. package/src/licence/serialized.ts +32 -9
  71. package/src/licence/validation.ts +120 -36
  72. package/src/native-content/client.ts +178 -236
  73. package/src/native-content/generated/fields.ts +2 -2
  74. package/src/native-content/ports.ts +9 -15
  75. package/src/native-content/serialized.ts +12 -19
  76. package/src/native-content/validation/base.ts +57 -50
  77. package/src/native-content/validation/messages.ts +21 -35
  78. package/src/native-content/validation/state.ts +31 -33
  79. package/src/notifications/client.ts +87 -31
  80. package/src/notifications/controller.ts +215 -61
  81. package/src/notifications/direct.ts +3 -1
  82. package/src/notifications/generated/fields.ts +2 -2
  83. package/src/notifications/ports.ts +7 -3
  84. package/src/notifications/serialized.ts +11 -9
  85. package/src/notifications/validation.ts +147 -43
  86. package/src/operation/bridge.ts +117 -50
  87. package/src/operation/client.ts +38 -24
  88. package/src/operation/controller.ts +33 -42
  89. package/src/operation/direct.ts +1 -3
  90. package/src/operation/generated/fields.ts +2 -2
  91. package/src/operation/ports.ts +10 -9
  92. package/src/operation/serialized.ts +8 -9
  93. package/src/operation/validation.ts +152 -65
  94. package/src/settings/client.ts +11 -44
  95. package/src/settings/connection.ts +26 -40
  96. package/src/settings/generated/fields.ts +3 -3
  97. package/src/settings/generated/protocol.ts +9 -4
  98. package/src/settings/names.ts +2 -4
  99. package/src/settings/registry.ts +23 -25
  100. package/src/settings/validation/authority.ts +19 -17
  101. package/src/settings/validation/command.ts +17 -37
  102. package/src/settings/validation/error.ts +2 -8
  103. package/src/settings/validation/primitives.ts +26 -42
  104. package/src/settings/validation/projection.ts +8 -24
  105. package/src/settings/validation/registry.ts +25 -73
  106. package/src/surface-transfer/client.ts +6 -18
  107. package/src/surface-transfer/generated/fields.ts +2 -2
  108. package/src/surface-transfer/validation.ts +31 -49
  109. package/src/surfaces/client.ts +12 -22
  110. package/src/surfaces/generated/fields.ts +2 -2
  111. package/src/surfaces/validation.ts +40 -49
  112. package/src/transfer/client.ts +26 -60
  113. package/src/transfer/connection.ts +12 -20
  114. package/src/transfer/generated/fields.ts +5 -5
  115. package/src/transfer/generated/protocol.ts +4 -4
  116. package/src/transfer/payload.ts +13 -31
  117. package/src/transfer/validation.ts +39 -75
  118. package/src/update/client.ts +59 -9
  119. package/src/update/controller.ts +113 -48
  120. package/src/update/direct.ts +3 -1
  121. package/src/update/generated/fields.ts +3 -3
  122. package/src/update/generated/protocol.ts +7 -3
  123. package/src/update/generated/variant-fields.ts +1 -0
  124. package/src/update/ports.ts +6 -2
  125. package/src/update/serialized.ts +34 -10
  126. package/src/update/validation.ts +115 -35
@@ -1,3 +1,4 @@
1
+ import { isRecord } from "../../core/record.ts";
1
2
  import {
2
3
  SETTINGS_VARIANT_FIELDS,
3
4
  SETTINGS_VARIANT_FIELDS_DISCRIMINANTS,
@@ -6,20 +7,17 @@ import { SETTINGS_FIELDS } from "../generated/fields.ts";
6
7
  import {
7
8
  SETTINGS_HARD_MAXIMUM_OPAQUE_VALUE_BYTES,
8
9
  SETTINGS_PROTOCOL_VERSION,
9
- type SettingsOpaqueValue,
10
10
  } from "../generated/protocol.ts";
11
- import {
12
- incompatible,
13
- type SettingsProtocolValidationCode,
14
- } from "./error.ts";
11
+ import type { SettingsOpaqueValue } from "../generated/protocol.ts";
12
+ import { incompatible } from "./error.ts";
13
+ import type { SettingsProtocolValidationCode } from "./error.ts";
15
14
 
16
15
  /**
17
16
  * Kept under its original name so the twenty call sites do not churn, but the
18
17
  * value is now the Rust `SettingsLimits::HARD_MAXIMUM_OPAQUE_VALUE_BYTES`
19
18
  * rather than a literal that happened to match it.
20
19
  */
21
- export const HARD_MAXIMUM_OPAQUE_VALUE_BYTES: number =
22
- SETTINGS_HARD_MAXIMUM_OPAQUE_VALUE_BYTES;
20
+ export const HARD_MAXIMUM_OPAQUE_VALUE_BYTES: number = SETTINGS_HARD_MAXIMUM_OPAQUE_VALUE_BYTES;
23
21
  const OPAQUE_ID = /^[a-z0-9][a-z0-9._:/-]{0,254}$/;
24
22
 
25
23
  export function authority(value: unknown): void {
@@ -35,10 +33,7 @@ export function optionalOpaque(value: unknown, maximum: number): void {
35
33
  }
36
34
  }
37
35
 
38
- export function opaque(
39
- value: unknown,
40
- maximum: number,
41
- ): asserts value is SettingsOpaqueValue {
36
+ export function opaque(value: unknown, maximum: number): asserts value is SettingsOpaqueValue {
42
37
  const envelope = record(value, SETTINGS_FIELDS.SettingsOpaqueValue);
43
38
  positive(envelope.codecVersion, "invalid_opaque_value");
44
39
  let encoded: string | undefined;
@@ -62,11 +57,11 @@ export function definition(value: unknown): void {
62
57
  identity(valueRecord.moduleId);
63
58
  }
64
59
 
65
- export function arraysOfIdentities(...values: unknown[]): void {
60
+ export function arraysOfIdentities(...values: Array<unknown>): void {
66
61
  values.forEach((value) => array(value).forEach(identity));
67
62
  }
68
63
 
69
- export function identity(value: unknown): void {
64
+ export function identity(value: unknown): asserts value is string {
70
65
  if (typeof value !== "string" || !OPAQUE_ID.test(value)) {
71
66
  incompatible("invalid_identity", value);
72
67
  }
@@ -78,7 +73,7 @@ export function protocolVersion(value: unknown): void {
78
73
  }
79
74
  }
80
75
 
81
- export function known(value: unknown, choices: readonly string[]): void {
76
+ export function known(value: unknown, choices: ReadonlyArray<string>): void {
82
77
  if (typeof value !== "string" || !choices.includes(value)) {
83
78
  incompatible("unknown_discriminant", value);
84
79
  }
@@ -106,25 +101,15 @@ export function integer(value: unknown): void {
106
101
  }
107
102
  }
108
103
 
109
- export function positive(
110
- value: unknown,
111
- code: SettingsProtocolValidationCode,
112
- ): void {
104
+ export function positive(value: unknown, code: SettingsProtocolValidationCode): void {
113
105
  unsigned(value, code);
114
106
  if (value === 0) {
115
107
  incompatible(code, value);
116
108
  }
117
109
  }
118
110
 
119
- export function unsigned(
120
- value: unknown,
121
- code: SettingsProtocolValidationCode,
122
- ): void {
123
- if (
124
- typeof value !== "number" ||
125
- !Number.isSafeInteger(value) ||
126
- value < 0
127
- ) {
111
+ export function unsigned(value: unknown, code: SettingsProtocolValidationCode): void {
112
+ if (typeof value !== "number" || !Number.isSafeInteger(value) || value < 0) {
128
113
  incompatible(code, value);
129
114
  }
130
115
  }
@@ -133,7 +118,7 @@ export function boundedArray(
133
118
  value: unknown,
134
119
  maximum: number,
135
120
  code: SettingsProtocolValidationCode,
136
- ): unknown[] {
121
+ ): Array<unknown> {
137
122
  const values = array(value);
138
123
  if (values.length > maximum) {
139
124
  incompatible(code, values.length);
@@ -141,7 +126,7 @@ export function boundedArray(
141
126
  return values;
142
127
  }
143
128
 
144
- export function array(value: unknown): unknown[] {
129
+ export function array(value: unknown): Array<unknown> {
145
130
  if (!Array.isArray(value)) {
146
131
  incompatible("invalid_shape", value);
147
132
  }
@@ -156,15 +141,14 @@ export function array(value: unknown): unknown[] {
156
141
  * Called without a list, this keeps shape-only behaviour, which is correct
157
142
  * for a tagged union and a gap anywhere else.
158
143
  */
159
- export function record(
160
- value: unknown,
161
- allowed?: readonly string[],
162
- ): Record<string, unknown> {
163
- if (typeof value !== "object" || value === null || Array.isArray(value)) {
144
+ export function record(value: unknown, allowed?: ReadonlyArray<string>): Record<string, unknown> {
145
+ if (!isRecord(value)) {
164
146
  incompatible("invalid_shape", value);
165
147
  }
166
- const result = value as Record<string, unknown>;
167
- if (allowed === undefined) return result;
148
+ const result = value;
149
+ if (allowed === undefined) {
150
+ return result;
151
+ }
168
152
 
169
153
  const permitted = new Set(allowed);
170
154
  for (const key of Object.keys(result)) {
@@ -192,12 +176,12 @@ export function record(
192
176
  * caller sent something odd — every caller checks the discriminant with
193
177
  * `known()` above this call.
194
178
  */
195
- export function variantKeys(
196
- type: string,
197
- value: Record<string, unknown>,
198
- ): readonly string[] {
179
+ export function variantKeys(type: string, value: Record<string, unknown>): ReadonlyArray<string> {
199
180
  const discriminant = value[SETTINGS_VARIANT_FIELDS_DISCRIMINANTS[type] ?? "kind"];
200
- const keys = SETTINGS_VARIANT_FIELDS[type]?.[discriminant as string];
201
- if (keys === undefined) incompatible("unknown_field", { type, discriminant });
181
+ const keys =
182
+ typeof discriminant === "string" ? SETTINGS_VARIANT_FIELDS[type]?.[discriminant] : undefined;
183
+ if (keys === undefined) {
184
+ incompatible("unknown_field", { type, discriminant });
185
+ }
202
186
  return keys;
203
187
  }
@@ -1,37 +1,25 @@
1
1
  import { SETTINGS_FIELDS } from "../generated/fields.ts";
2
- import {
3
- array,
4
- identity,
5
- known,
6
- optionalOpaque,
7
- record,
8
- } from "./primitives.ts";
2
+ import { array, identity, known, optionalOpaque, record } from "./primitives.ts";
9
3
 
10
- export function policy(
4
+ export function hasPolicyOverride(
11
5
  value: unknown,
12
6
  maximum: number,
13
- effects: readonly string[],
14
- ): void {
7
+ effects: ReadonlyArray<string>,
8
+ ): boolean {
15
9
  const valueRecord = record(value, SETTINGS_FIELDS.SettingsPolicyProjection);
16
10
  identity(valueRecord.sourceId);
17
11
  known(valueRecord.effect, effects);
18
12
  optionalOpaque(valueRecord.constraints, maximum);
13
+ return valueRecord.effect === "override";
19
14
  }
20
15
 
21
- export function recovery(
22
- value: unknown,
23
- maximum: number,
24
- codes: readonly string[],
25
- ): void {
16
+ export function recovery(value: unknown, maximum: number, codes: ReadonlyArray<string>): void {
26
17
  const valueRecord = record(value, SETTINGS_FIELDS.SettingsRecoveryState);
27
18
  known(valueRecord.code, codes);
28
19
  optionalOpaque(valueRecord.diagnostic, maximum);
29
20
  }
30
21
 
31
- export function activation(
32
- value: unknown,
33
- states: readonly string[],
34
- ): void {
22
+ export function activation(value: unknown, states: ReadonlyArray<string>): void {
35
23
  array(value).forEach((value) => {
36
24
  const requirement = record(value, SETTINGS_FIELDS.SettingsActivationRequirement);
37
25
  identity(requirement.targetId);
@@ -39,11 +27,7 @@ export function activation(
39
27
  });
40
28
  }
41
29
 
42
- export function rejection(
43
- value: unknown,
44
- maximum: number,
45
- codes: readonly string[],
46
- ): void {
30
+ export function rejection(value: unknown, maximum: number, codes: ReadonlyArray<string>): void {
47
31
  const valueRecord = record(value, SETTINGS_FIELDS.SettingsRejection);
48
32
  known(valueRecord.code, codes);
49
33
  optionalOpaque(valueRecord.diagnostic, maximum);
@@ -1,8 +1,6 @@
1
1
  import { SETTINGS_FIELDS } from "../generated/fields.ts";
2
- import {
3
- SETTINGS_MUTATION_TIMINGS,
4
- type SettingsRegistrySnapshot,
5
- } from "../generated/protocol.ts";
2
+ import { SETTINGS_MUTATION_TIMINGS } from "../generated/protocol.ts";
3
+ import type { SettingsRegistrySnapshot, SettingsLimits } from "../generated/protocol.ts";
6
4
  import { incompatible } from "./error.ts";
7
5
  import {
8
6
  arraysOfIdentities,
@@ -32,31 +30,11 @@ export function assertValidSettingsRegistrySnapshot(
32
30
  incompatible("invalid_registry", registry.digest);
33
31
  }
34
32
  const limits = settingsLimits(registry.limits);
35
- const modules = boundedArray(
36
- registry.modules,
37
- limits.maximumModules,
38
- "invalid_registry",
39
- );
40
- const sections = boundedArray(
41
- registry.sections,
42
- limits.maximumSections,
43
- "invalid_registry",
44
- );
45
- const pages = boundedArray(
46
- registry.pages,
47
- limits.maximumPages,
48
- "invalid_registry",
49
- );
50
- const renderers = boundedArray(
51
- registry.renderers,
52
- limits.maximumRenderers,
53
- "invalid_registry",
54
- );
55
- const scopes = boundedArray(
56
- registry.scopes,
57
- limits.maximumScopes,
58
- "invalid_registry",
59
- );
33
+ const modules = boundedArray(registry.modules, limits.maximumModules, "invalid_registry");
34
+ const sections = boundedArray(registry.sections, limits.maximumSections, "invalid_registry");
35
+ const pages = boundedArray(registry.pages, limits.maximumPages, "invalid_registry");
36
+ const renderers = boundedArray(registry.renderers, limits.maximumRenderers, "invalid_registry");
37
+ const scopes = boundedArray(registry.scopes, limits.maximumScopes, "invalid_registry");
60
38
  const applyUnits = boundedArray(
61
39
  registry.applyUnits,
62
40
  limits.maximumApplyUnits,
@@ -100,39 +78,26 @@ export function assertValidSettingsRegistrySnapshot(
100
78
  pages.forEach((value) => validatePage(value, limits));
101
79
  }
102
80
 
103
- function validatePage(
104
- value: unknown,
105
- limits: Record<string, number>,
106
- ): void {
81
+ function validatePage(value: unknown, limits: SettingsLimits): void {
107
82
  const page = record(value, SETTINGS_FIELDS.SettingsPageDefinition);
108
83
  identity(page.id);
109
84
  identity(page.moduleId);
110
85
  identity(page.sectionId);
111
86
  identity(page.rendererId);
112
- text(page.label, limits.maximumLabelBytes!);
87
+ text(page.label, limits.maximumLabelBytes);
113
88
  integer(page.order);
114
- boundedArray(
115
- page.keywords,
116
- limits.maximumKeywordsPerPage!,
117
- "invalid_registry",
118
- ).forEach((keyword) => text(keyword, limits.maximumKeywordBytes!));
119
- boundedArray(
120
- page.anchors,
121
- limits.maximumAnchorsPerPage!,
122
- "invalid_registry",
123
- ).forEach((value) => {
89
+ boundedArray(page.keywords, limits.maximumKeywordsPerPage, "invalid_registry").forEach(
90
+ (keyword) => text(keyword, limits.maximumKeywordBytes),
91
+ );
92
+ boundedArray(page.anchors, limits.maximumAnchorsPerPage, "invalid_registry").forEach((value) => {
124
93
  const anchor = record(value, SETTINGS_FIELDS.SettingsAnchorDefinition);
125
94
  identity(anchor.id);
126
95
  if (anchor.label !== null) {
127
- text(anchor.label, limits.maximumLabelBytes!);
96
+ text(anchor.label, limits.maximumLabelBytes);
128
97
  }
129
98
  integer(anchor.order);
130
99
  });
131
- arraysOfIdentities(
132
- page.requiredCapabilities,
133
- page.readableScopeIds,
134
- page.writableApplyUnitIds,
135
- );
100
+ arraysOfIdentities(page.requiredCapabilities, page.readableScopeIds, page.writableApplyUnitIds);
136
101
  const features = record(page.features, SETTINGS_FIELDS.SettingsPageFeatures);
137
102
  boolean(features.reset);
138
103
  boolean(features.import);
@@ -141,30 +106,17 @@ function validatePage(
141
106
  boolean(features.confirmation);
142
107
  }
143
108
 
144
- function settingsLimits(value: unknown): Record<string, number> {
109
+ function settingsLimits(value: unknown): SettingsLimits {
145
110
  const limits = record(value, SETTINGS_FIELDS.SettingsLimits);
146
- const names = [
147
- "maximumModules",
148
- "maximumSections",
149
- "maximumPages",
150
- "maximumRenderers",
151
- "maximumScopes",
152
- "maximumApplyUnits",
153
- "maximumCapabilities",
154
- "maximumAnchorsPerPage",
155
- "maximumKeywordsPerPage",
156
- "maximumLabelBytes",
157
- "maximumKeywordBytes",
158
- "maximumOpaqueValueBytes",
159
- ] as const;
160
- for (const name of names) {
161
- positive(limits[name], "invalid_registry");
162
- }
163
- if (
164
- (limits.maximumOpaqueValueBytes as number) >
165
- HARD_MAXIMUM_OPAQUE_VALUE_BYTES
166
- ) {
111
+ assertPositiveLimits(limits);
112
+ if (limits.maximumOpaqueValueBytes > HARD_MAXIMUM_OPAQUE_VALUE_BYTES) {
167
113
  incompatible("invalid_registry", limits.maximumOpaqueValueBytes);
168
114
  }
169
- return limits as Record<(typeof names)[number], number>;
115
+ return limits;
116
+ }
117
+
118
+ function assertPositiveLimits(limits: Record<string, unknown>): asserts limits is SettingsLimits {
119
+ for (const name of SETTINGS_FIELDS.SettingsLimits) {
120
+ positive(limits[name], "invalid_registry");
121
+ }
170
122
  }
@@ -15,10 +15,8 @@ import {
15
15
  assertValidSurfaceTransferResponse,
16
16
  } from "./validation.ts";
17
17
 
18
- export const TRANSFER_START_SURFACE_COMMAND =
19
- "longhorn_transfer_start_surface";
20
- export const TRANSFER_COMMIT_SURFACE_COMMAND =
21
- "longhorn_transfer_commit_surface";
18
+ export const TRANSFER_START_SURFACE_COMMAND = "longhorn_transfer_start_surface";
19
+ export const TRANSFER_COMMIT_SURFACE_COMMAND = "longhorn_transfer_commit_surface";
22
20
 
23
21
  export class SurfaceTransferClient {
24
22
  readonly #transport: InvokeTransport;
@@ -27,27 +25,17 @@ export class SurfaceTransferClient {
27
25
  this.#transport = transport;
28
26
  }
29
27
 
30
- async start(
31
- request: SurfaceSessionStartRequest,
32
- ): Promise<SurfaceSessionResponse> {
28
+ async start(request: SurfaceSessionStartRequest): Promise<SurfaceSessionResponse> {
33
29
  assertTransferProtocolVersion(request.protocol_version);
34
- const value = await this.#transport.invoke(
35
- TRANSFER_START_SURFACE_COMMAND,
36
- { request },
37
- );
30
+ const value = await this.#transport.invoke(TRANSFER_START_SURFACE_COMMAND, { request });
38
31
  assertValidSurfaceSessionResponse(value);
39
32
  return value;
40
33
  }
41
34
 
42
- async commit(
43
- request: SurfaceTransferCommand,
44
- ): Promise<SurfaceTransferResponse> {
35
+ async commit(request: SurfaceTransferCommand): Promise<SurfaceTransferResponse> {
45
36
  assertTransferProtocolVersion(request.protocol_version);
46
37
  assertValidTransferCommitSelector(request.selector);
47
- const value = await this.#transport.invoke(
48
- TRANSFER_COMMIT_SURFACE_COMMAND,
49
- { request },
50
- );
38
+ const value = await this.#transport.invoke(TRANSFER_COMMIT_SURFACE_COMMAND, { request });
51
39
  assertValidSurfaceTransferResponse(value);
52
40
  return value;
53
41
  }
@@ -3,10 +3,10 @@
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 SURFACE_TRANSFER_FIELDS: Record<string, readonly string[]> = {
6
+ export const SURFACE_TRANSFER_FIELDS = {
7
7
  "SurfaceSessionStartRequest": ["protocol_version", "request_id", "surface_id"],
8
8
  "SurfaceTransferCommand": ["protocol_version", "request_id", "session_id", "selector"],
9
9
  "SurfaceProvisioningCompletion": ["window_id", "host_binding_id", "display_id"],
10
10
  "SurfaceTransferCompletion": ["protocol_version", "request_id", "session_id", "source_host_binding_id", "target_host_binding_id", "previous_revision", "committed_revision", "authoritative_document", "target"],
11
11
  "SurfaceTransferAbort": ["protocol_version", "request_id", "source", "surface_code", "message", "retryable", "session_consumed", "reconciliation_required"],
12
- };
12
+ } as const;
@@ -1,3 +1,4 @@
1
+ import { isRecord } from "../core/record.ts";
1
2
  import {
2
3
  SURFACE_TRANSFER_VARIANT_FIELDS,
3
4
  SURFACE_TRANSFER_VARIANT_FIELDS_DISCRIMINANTS,
@@ -8,10 +9,12 @@ import {
8
9
  SURFACE_TRANSFER_ERROR_CODES,
9
10
  SURFACE_TRANSFER_RESPONSE_STATUSES,
10
11
  SURFACE_TRANSFER_TARGET_KINDS,
11
- type SurfaceSessionResponse,
12
- type SurfaceTransferAbort,
13
- type SurfaceTransferResponse,
14
- type SurfaceTransferTarget,
12
+ } from "./generated/protocol.ts";
13
+ import type {
14
+ SurfaceSessionResponse,
15
+ SurfaceTransferAbort,
16
+ SurfaceTransferResponse,
17
+ SurfaceTransferTarget,
15
18
  } from "./generated/protocol.ts";
16
19
  import { SURFACE_TRANSFER_FIELDS } from "./generated/fields.ts";
17
20
  import {
@@ -19,9 +22,7 @@ import {
19
22
  assertValidTransferTargetBinding,
20
23
  assertTransferProtocolVersion,
21
24
  } from "@inflatable-cookie/longhorn/transfer";
22
- import {
23
- assertValidSurfaceMutationRejectionCode,
24
- } from "@inflatable-cookie/longhorn/surfaces";
25
+ import { assertValidSurfaceMutationRejectionCode } from "@inflatable-cookie/longhorn/surfaces";
25
26
 
26
27
  export type SurfaceTransferProtocolValidationCode =
27
28
  | "unknown_target"
@@ -37,10 +38,7 @@ export class SurfaceTransferProtocolValidationError extends Error {
37
38
  readonly code: SurfaceTransferProtocolValidationCode;
38
39
  readonly received: unknown;
39
40
 
40
- constructor(
41
- code: SurfaceTransferProtocolValidationCode,
42
- received: unknown,
43
- ) {
41
+ constructor(code: SurfaceTransferProtocolValidationCode, received: unknown) {
44
42
  super(`incompatible Surface transfer protocol: ${code}`);
45
43
  this.name = "SurfaceTransferProtocolValidationError";
46
44
  this.code = code;
@@ -55,16 +53,10 @@ export function assertValidSurfaceTransferTarget(
55
53
  // its discriminant and one list is wrong for every variant. That used to
56
54
  // mean no key check at all here; the generated per-variant map supplies one.
57
55
  const target = record(value);
58
- assertKnown(
59
- target.kind,
60
- SURFACE_TRANSFER_TARGET_KINDS,
61
- "unknown_target",
62
- );
56
+ assertKnown(target.kind, SURFACE_TRANSFER_TARGET_KINDS, "unknown_target");
63
57
  record(target, variantKeys("SurfaceTransferTarget", target));
64
58
  if (target.kind === "existing") {
65
- assertValidTransferTargetBinding(
66
- record(record(target.target).binding),
67
- );
59
+ assertValidTransferTargetBinding(record(record(target.target).binding));
68
60
  }
69
61
  }
70
62
 
@@ -74,24 +66,12 @@ export function assertValidSurfaceTransferAbort(
74
66
  const abort = record(value, SURFACE_TRANSFER_FIELDS.SurfaceTransferAbort);
75
67
  assertTransferProtocolVersion(abort.protocol_version);
76
68
  const source = record(abort.source);
77
- assertKnown(
78
- source.domain,
79
- SURFACE_TRANSFER_ABORT_DOMAINS,
80
- "unknown_abort_domain",
81
- );
69
+ assertKnown(source.domain, SURFACE_TRANSFER_ABORT_DOMAINS, "unknown_abort_domain");
82
70
  record(source, variantKeys("SurfaceTransferAbortSource", source));
83
71
  if (source.domain === "transfer") {
84
- assertKnown(
85
- source.code,
86
- TRANSFER_ERROR_CODES,
87
- "unknown_transfer_error_code",
88
- );
72
+ assertKnown(source.code, TRANSFER_ERROR_CODES, "unknown_transfer_error_code");
89
73
  } else {
90
- assertKnown(
91
- source.code,
92
- SURFACE_TRANSFER_ERROR_CODES,
93
- "unknown_surface_transfer_error_code",
94
- );
74
+ assertKnown(source.code, SURFACE_TRANSFER_ERROR_CODES, "unknown_surface_transfer_error_code");
95
75
  }
96
76
  if (abort.surface_code !== null) {
97
77
  assertValidSurfaceMutationRejectionCode(abort.surface_code);
@@ -124,7 +104,10 @@ export function assertValidSurfaceTransferResponse(
124
104
  "SurfaceTransferResponse",
125
105
  );
126
106
  if (response.status === "committed") {
127
- const completion = record(response.completion, SURFACE_TRANSFER_FIELDS.SurfaceTransferCompletion);
107
+ const completion = record(
108
+ response.completion,
109
+ SURFACE_TRANSFER_FIELDS.SurfaceTransferCompletion,
110
+ );
128
111
  assertTransferProtocolVersion(completion.protocol_version);
129
112
  assertValidSurfaceTransferTarget(completion.target);
130
113
  } else {
@@ -134,7 +117,7 @@ export function assertValidSurfaceTransferResponse(
134
117
 
135
118
  function responseWithStatus(
136
119
  value: unknown,
137
- statuses: readonly string[],
120
+ statuses: ReadonlyArray<string>,
138
121
  type: string,
139
122
  ): Record<string, unknown> {
140
123
  const response = record(value);
@@ -151,12 +134,12 @@ function responseWithStatus(
151
134
  * A missing entry means the generator failed to read the union — every caller
152
135
  * runs `assertKnown` over the discriminant above this.
153
136
  */
154
- function variantKeys(
155
- type: string,
156
- value: Record<string, unknown>,
157
- ): readonly string[] {
137
+ function variantKeys(type: string, value: Record<string, unknown>): ReadonlyArray<string> {
158
138
  const discriminant = value[SURFACE_TRANSFER_VARIANT_FIELDS_DISCRIMINANTS[type] ?? "kind"];
159
- const keys = SURFACE_TRANSFER_VARIANT_FIELDS[type]?.[discriminant as string];
139
+ const keys =
140
+ typeof discriminant === "string"
141
+ ? SURFACE_TRANSFER_VARIANT_FIELDS[type]?.[discriminant]
142
+ : undefined;
160
143
  if (keys === undefined) {
161
144
  throw new SurfaceTransferProtocolValidationError("unknown_response_status", {
162
145
  type,
@@ -168,7 +151,7 @@ function variantKeys(
168
151
 
169
152
  function assertKnown(
170
153
  value: unknown,
171
- known: readonly string[],
154
+ known: ReadonlyArray<string>,
172
155
  code: SurfaceTransferProtocolValidationCode,
173
156
  ): asserts value is string {
174
157
  if (typeof value !== "string" || !known.includes(value)) {
@@ -184,15 +167,14 @@ function assertKnown(
184
167
  * Passing no list keeps shape-only behaviour for the tagged unions, whose
185
168
  * allowed keys depend on their discriminant and so are not one flat set.
186
169
  */
187
- function record(
188
- value: unknown,
189
- allowed?: readonly string[],
190
- ): Record<string, unknown> {
191
- if (typeof value !== "object" || value === null || Array.isArray(value)) {
170
+ function record(value: unknown, allowed?: ReadonlyArray<string>): Record<string, unknown> {
171
+ if (!isRecord(value)) {
192
172
  throw new SurfaceTransferProtocolValidationError("invalid_object", value);
193
173
  }
194
- const result = value as Record<string, unknown>;
195
- if (allowed === undefined) return result;
174
+ const result = value;
175
+ if (allowed === undefined) {
176
+ return result;
177
+ }
196
178
 
197
179
  const permitted = new Set(allowed);
198
180
  for (const key of Object.keys(result)) {
@@ -1,12 +1,11 @@
1
- import {
2
- CheckedSnapshotConnection,
3
- type ConnectionFailure,
4
- type ConnectionFailureReporter,
5
- type EventTransport,
1
+ import { CheckedSnapshotConnection } from "@inflatable-cookie/longhorn/core";
2
+ import type {
3
+ ConnectionFailure,
4
+ ConnectionFailureReporter,
5
+ EventTransport,
6
6
  } from "@inflatable-cookie/longhorn/core";
7
7
 
8
8
  import type {
9
- SurfaceChangedEvent,
10
9
  SurfaceMutationRequest,
11
10
  SurfaceMutationResponse,
12
11
  SurfaceSnapshot,
@@ -25,7 +24,7 @@ export const SURFACE_CHANGED_EVENT = "longhorn://surfaces/changed";
25
24
  export interface SurfaceSubscription {
26
25
  readonly ready: Promise<void>;
27
26
  current(): SurfaceSnapshot | undefined;
28
- failures(): readonly ConnectionFailure[];
27
+ failures(): ReadonlyArray<ConnectionFailure>;
29
28
  dispose(): Promise<void>;
30
29
  }
31
30
 
@@ -41,9 +40,7 @@ export class SurfaceClient {
41
40
  return parseSurfaceSnapshot(value);
42
41
  }
43
42
 
44
- async mutate(
45
- request: SurfaceMutationRequest,
46
- ): Promise<SurfaceMutationResponse> {
43
+ async mutate(request: SurfaceMutationRequest): Promise<SurfaceMutationResponse> {
47
44
  assertValidSurfaceMutationCommand(request.command);
48
45
  const value = await this.#transport.invoke(SURFACE_MUTATE_COMMAND, {
49
46
  request,
@@ -56,12 +53,7 @@ export class SurfaceClient {
56
53
  listener: (snapshot: SurfaceSnapshot) => void,
57
54
  onFailure?: ConnectionFailureReporter,
58
55
  ): SurfaceSubscription {
59
- return new SurfaceSnapshotSubscription(
60
- this,
61
- this.#transport,
62
- listener,
63
- onFailure,
64
- );
56
+ return new SurfaceSnapshotSubscription(this, this.#transport, listener, onFailure);
65
57
  }
66
58
  }
67
59
 
@@ -76,8 +68,7 @@ class SurfaceSnapshotSubscription implements SurfaceSubscription {
76
68
  onFailure?: ConnectionFailureReporter,
77
69
  ) {
78
70
  this.#connection = new CheckedSnapshotConnection({
79
- listen: (receive) =>
80
- transport.listen(SURFACE_CHANGED_EVENT, receive),
71
+ listen: (receive) => transport.listen(SURFACE_CHANGED_EVENT, receive),
81
72
  loadSnapshot: () => client.snapshot(),
82
73
  validateSnapshot: parseSurfaceSnapshot,
83
74
  handleEvent: surfaceEventAction,
@@ -92,7 +83,7 @@ class SurfaceSnapshotSubscription implements SurfaceSubscription {
92
83
  return this.#connection.current();
93
84
  }
94
85
 
95
- failures(): readonly ConnectionFailure[] {
86
+ failures(): ReadonlyArray<ConnectionFailure> {
96
87
  return this.#connection.failures();
97
88
  }
98
89
 
@@ -115,11 +106,10 @@ function surfaceEventAction(
115
106
  return { kind: "refresh" };
116
107
  }
117
108
 
118
- const event = value as SurfaceChangedEvent;
109
+ const event = value;
119
110
  if (
120
111
  event.epoch < current.epoch ||
121
- (event.epoch === current.epoch &&
122
- event.revision <= current.revision)
112
+ (event.epoch === current.epoch && event.revision <= current.revision)
123
113
  ) {
124
114
  return { kind: "ignore" };
125
115
  }
@@ -3,7 +3,7 @@
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 SURFACE_FIELDS: Record<string, readonly string[]> = {
6
+ export const SURFACE_FIELDS = {
7
7
  "RegionState": ["region_id", "panel_instance_ids", "active_panel_instance_id", "collapsed"],
8
8
  "SizingSlotState": ["sizing_slot_id", "ratio"],
9
9
  "PanelInstance": ["id", "definition_id"],
@@ -16,4 +16,4 @@ export const SURFACE_FIELDS: Record<string, readonly string[]> = {
16
16
  "SurfaceMutationRejection": ["request_id", "current_revision", "code", "detail", "authoritative_document"],
17
17
  "SurfaceSnapshot": ["protocol_version", "epoch", "revision", "document"],
18
18
  "SurfaceChangedEvent": ["protocol_version", "epoch", "revision"],
19
- };
19
+ } as const;