@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.
- package/LICENSE +21 -0
- package/README.md +38 -0
- package/package.json +205 -0
- package/src/agent-control/index.ts +20 -0
- package/src/agent-control/inject.ts +4 -0
- package/src/agent-control/shim.ts +722 -0
- package/src/bridge/direct.ts +22 -0
- package/src/bridge/generated/fields.ts +32 -0
- package/src/bridge/generated/protocol.ts +218 -0
- package/src/bridge/generated/variant-fields.ts +10 -0
- package/src/bridge/index.ts +15 -0
- package/src/bridge/job.ts +51 -0
- package/src/bridge/loopback.ts +84 -0
- package/src/bridge/operation.ts +166 -0
- package/src/bridge/router.ts +71 -0
- package/src/bridge/runtime/connection.ts +318 -0
- package/src/bridge/runtime/policy.ts +89 -0
- package/src/bridge/runtime/retry.ts +60 -0
- package/src/bridge/runtime.ts +3 -0
- package/src/bridge/session.ts +67 -0
- package/src/bridge/stream/connection.ts +146 -0
- package/src/bridge/stream/source.ts +61 -0
- package/src/bridge/stream/tracker.ts +109 -0
- package/src/bridge/stream.ts +6 -0
- package/src/bridge/supervision.ts +204 -0
- package/src/bridge/validation/base.ts +164 -0
- package/src/bridge/validation/jobs.ts +105 -0
- package/src/bridge/validation/negotiation.ts +242 -0
- package/src/bridge/validation/operations.ts +213 -0
- package/src/bridge/validation/streams.ts +47 -0
- package/src/bridge/validation.ts +5 -0
- package/src/commands/client.ts +124 -0
- package/src/commands/controller.ts +411 -0
- package/src/commands/generated/fields.ts +43 -0
- package/src/commands/generated/protocol.ts +876 -0
- package/src/commands/generated/variant-fields.ts +88 -0
- package/src/commands/index.ts +13 -0
- package/src/commands/keyboard.ts +361 -0
- package/src/commands/ports.ts +82 -0
- package/src/commands/projectors.ts +306 -0
- package/src/commands/validation.ts +444 -0
- package/src/config/client.ts +215 -0
- package/src/config/generated/base.ts +714 -0
- package/src/config/generated/fields.ts +54 -0
- package/src/config/generated/label-template.ts +14 -0
- package/src/config/generated/labels.ts +30 -0
- package/src/config/generated/protocol.ts +3 -0
- package/src/config/generated/restore.ts +658 -0
- package/src/config/generated/variant-fields.ts +129 -0
- package/src/config/index.ts +12 -0
- package/src/config/names.ts +19 -0
- package/src/config/validation/commands.ts +155 -0
- package/src/config/validation/outcomes.ts +243 -0
- package/src/config/validation/primitives.ts +174 -0
- package/src/config/validation/projection.ts +351 -0
- package/src/config/validation.ts +7 -0
- package/src/core/connection.ts +252 -0
- package/src/core/index.ts +2 -0
- package/src/core/transport.ts +24 -0
- package/src/history/client.ts +65 -0
- package/src/history/controller.ts +385 -0
- package/src/history/direct.ts +21 -0
- package/src/history/generated/fields.ts +18 -0
- package/src/history/generated/protocol.ts +389 -0
- package/src/history/generated/variant-fields.ts +27 -0
- package/src/history/index.ts +13 -0
- package/src/history/ports.ts +31 -0
- package/src/history/serialized.ts +38 -0
- package/src/history/validation.ts +334 -0
- package/src/history-tree/client.ts +16 -0
- package/src/history-tree/controller.ts +199 -0
- package/src/history-tree/direct.ts +3 -0
- package/src/history-tree/generated/fields.ts +26 -0
- package/src/history-tree/generated/protocol.ts +711 -0
- package/src/history-tree/generated/variant-fields.ts +39 -0
- package/src/history-tree/index.ts +13 -0
- package/src/history-tree/ports.ts +43 -0
- package/src/history-tree/serialized.ts +15 -0
- package/src/history-tree/validation.ts +269 -0
- package/src/layout/generated/protocol.ts +289 -0
- package/src/layout/index.ts +4 -0
- package/src/layout/ratio.ts +22 -0
- package/src/layout/validation.ts +110 -0
- package/src/layout/visibility.ts +30 -0
- package/src/licence/client.ts +32 -0
- package/src/licence/controller.ts +301 -0
- package/src/licence/direct.ts +3 -0
- package/src/licence/generated/fields.ts +17 -0
- package/src/licence/generated/key-conformance.json +42 -0
- package/src/licence/generated/protocol.ts +249 -0
- package/src/licence/generated/variant-fields.ts +34 -0
- package/src/licence/index.ts +14 -0
- package/src/licence/key.ts +125 -0
- package/src/licence/ports.ts +39 -0
- package/src/licence/serialized.ts +13 -0
- package/src/licence/validation.ts +226 -0
- package/src/native-content/client.ts +474 -0
- package/src/native-content/direct.ts +16 -0
- package/src/native-content/generated/fields.ts +32 -0
- package/src/native-content/generated/protocol.ts +498 -0
- package/src/native-content/generated/variant-fields.ts +77 -0
- package/src/native-content/index.ts +12 -0
- package/src/native-content/ports.ts +46 -0
- package/src/native-content/serialized.ts +50 -0
- package/src/native-content/validation/base.ts +194 -0
- package/src/native-content/validation/messages.ts +192 -0
- package/src/native-content/validation/state.ts +404 -0
- package/src/native-content/validation.ts +7 -0
- package/src/notifications/client.ts +106 -0
- package/src/notifications/controller.ts +240 -0
- package/src/notifications/direct.ts +12 -0
- package/src/notifications/generated/fields.ts +19 -0
- package/src/notifications/generated/labels.ts +20 -0
- package/src/notifications/generated/protocol.ts +522 -0
- package/src/notifications/generated/variant-fields.ts +37 -0
- package/src/notifications/index.ts +16 -0
- package/src/notifications/ports.ts +22 -0
- package/src/notifications/serialized.ts +27 -0
- package/src/notifications/toast.ts +41 -0
- package/src/notifications/tone.ts +27 -0
- package/src/notifications/validation.ts +246 -0
- package/src/operation/bridge.ts +168 -0
- package/src/operation/client.ts +167 -0
- package/src/operation/controller.ts +321 -0
- package/src/operation/direct.ts +15 -0
- package/src/operation/generated/fields.ts +20 -0
- package/src/operation/generated/labels.ts +15 -0
- package/src/operation/generated/protocol.ts +762 -0
- package/src/operation/generated/variant-fields.ts +60 -0
- package/src/operation/index.ts +14 -0
- package/src/operation/ports.ts +29 -0
- package/src/operation/serialized.ts +39 -0
- package/src/operation/validation.ts +479 -0
- package/src/settings/client.ts +142 -0
- package/src/settings/connection.ts +246 -0
- package/src/settings/generated/fields.ts +34 -0
- package/src/settings/generated/protocol.ts +668 -0
- package/src/settings/generated/variant-fields.ts +26 -0
- package/src/settings/index.ts +12 -0
- package/src/settings/names.ts +8 -0
- package/src/settings/registry.ts +141 -0
- package/src/settings/validation/authority.ts +66 -0
- package/src/settings/validation/command.ts +165 -0
- package/src/settings/validation/error.ts +33 -0
- package/src/settings/validation/primitives.ts +203 -0
- package/src/settings/validation/projection.ts +50 -0
- package/src/settings/validation/registry.ts +170 -0
- package/src/settings/validation.ts +4 -0
- package/src/surface-transfer/client.ts +54 -0
- package/src/surface-transfer/generated/fields.ts +12 -0
- package/src/surface-transfer/generated/protocol.ts +61 -0
- package/src/surface-transfer/generated/variant-fields.ts +30 -0
- package/src/surface-transfer/index.ts +9 -0
- package/src/surface-transfer/validation.ts +209 -0
- package/src/surfaces/client.ts +140 -0
- package/src/surfaces/generated/fields.ts +19 -0
- package/src/surfaces/generated/protocol.ts +217 -0
- package/src/surfaces/generated/variant-fields.ts +42 -0
- package/src/surfaces/index.ts +9 -0
- package/src/surfaces/validation.ts +199 -0
- package/src/transfer/client.ts +133 -0
- package/src/transfer/connection.ts +87 -0
- package/src/transfer/generated/fields.ts +23 -0
- package/src/transfer/generated/protocol.ts +171 -0
- package/src/transfer/generated/variant-fields.ts +45 -0
- package/src/transfer/index.ts +11 -0
- package/src/transfer/payload.ts +71 -0
- package/src/transfer/validation.ts +282 -0
- package/src/update/client.ts +29 -0
- package/src/update/controller.ts +255 -0
- package/src/update/direct.ts +3 -0
- package/src/update/generated/fields.ts +14 -0
- package/src/update/generated/protocol.ts +238 -0
- package/src/update/generated/variant-fields.ts +44 -0
- package/src/update/index.ts +14 -0
- package/src/update/ports.ts +36 -0
- package/src/update/serialized.ts +12 -0
- package/src/update/validation.ts +191 -0
|
@@ -0,0 +1,242 @@
|
|
|
1
|
+
import { BRIDGE_FIELDS } from "../generated/fields.ts";
|
|
2
|
+
import {
|
|
3
|
+
BRIDGE_MAXIMUM_AUTHORITY_DOMAINS,
|
|
4
|
+
BRIDGE_MAXIMUM_CAPABILITIES_PER_DOMAIN,
|
|
5
|
+
BRIDGE_MAXIMUM_CAPABILITY_DOMAINS,
|
|
6
|
+
BRIDGE_MAXIMUM_DIAGNOSTIC_MESSAGE_BYTES,
|
|
7
|
+
BRIDGE_MAXIMUM_DIAGNOSTICS,
|
|
8
|
+
BRIDGE_MAXIMUM_REQUESTED_DOMAINS,
|
|
9
|
+
BRIDGE_MAXIMUM_TRANSPORT_FEATURES,
|
|
10
|
+
BRIDGE_AUTHENTICATION_POSTURES,
|
|
11
|
+
BRIDGE_ADMITTED_CONNECTION_REASONS,
|
|
12
|
+
BRIDGE_CONNECTION_REASONS,
|
|
13
|
+
BRIDGE_CONNECTION_STATES,
|
|
14
|
+
BRIDGE_DOMAIN_AVAILABILITIES,
|
|
15
|
+
BRIDGE_EXECUTION_AUTHORITIES,
|
|
16
|
+
BRIDGE_HOST_FORMS,
|
|
17
|
+
BRIDGE_PROTOCOL_VERSION,
|
|
18
|
+
BRIDGE_READ_AUTHORITIES,
|
|
19
|
+
BRIDGE_WRITE_AUTHORITIES,
|
|
20
|
+
type BridgeConnectionReason,
|
|
21
|
+
type BridgeConnectionState,
|
|
22
|
+
type BridgeConnectionStatus,
|
|
23
|
+
type BridgeDiagnostic,
|
|
24
|
+
type BridgeHelloRequest,
|
|
25
|
+
type BridgeHostDescriptor,
|
|
26
|
+
type BridgeNegotiationReceipt,
|
|
27
|
+
type DomainAuthorityDescriptor,
|
|
28
|
+
type DomainCapabilityDescriptor,
|
|
29
|
+
} from "../generated/protocol.ts";
|
|
30
|
+
import {
|
|
31
|
+
array,
|
|
32
|
+
domainId,
|
|
33
|
+
incompatible,
|
|
34
|
+
integer,
|
|
35
|
+
nullable,
|
|
36
|
+
oneOf,
|
|
37
|
+
opaqueId,
|
|
38
|
+
record,
|
|
39
|
+
unique,
|
|
40
|
+
} from "./base.ts";
|
|
41
|
+
|
|
42
|
+
export function assertBridgeProtocolVersion(value: unknown): asserts value is 1 {
|
|
43
|
+
if (value !== BRIDGE_PROTOCOL_VERSION) {
|
|
44
|
+
incompatible("unsupported_protocol_version", value);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export function parseBridgeHelloRequest(value: unknown): BridgeHelloRequest {
|
|
49
|
+
const source = record(value, BRIDGE_FIELDS.BridgeHelloRequest);
|
|
50
|
+
assertBridgeProtocolVersion(source.protocolVersion);
|
|
51
|
+
const requestedDomains = array(source.requestedDomains, BRIDGE_MAXIMUM_REQUESTED_DOMAINS).map(domainId);
|
|
52
|
+
unique(requestedDomains, String);
|
|
53
|
+
return {
|
|
54
|
+
protocolVersion: BRIDGE_PROTOCOL_VERSION,
|
|
55
|
+
bridgeId: opaqueId(source.bridgeId),
|
|
56
|
+
requestedDomains,
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export function parseBridgeNegotiationReceipt(
|
|
61
|
+
value: unknown,
|
|
62
|
+
request?: BridgeHelloRequest,
|
|
63
|
+
): BridgeNegotiationReceipt {
|
|
64
|
+
const source = record(value, BRIDGE_FIELDS.BridgeNegotiationReceipt);
|
|
65
|
+
assertBridgeProtocolVersion(source.protocolVersion);
|
|
66
|
+
const capabilities = array(source.domainCapabilities, BRIDGE_MAXIMUM_CAPABILITY_DOMAINS).map(
|
|
67
|
+
parseDomainCapability,
|
|
68
|
+
);
|
|
69
|
+
const authorities = array(source.domainAuthorities, BRIDGE_MAXIMUM_AUTHORITY_DOMAINS).map(
|
|
70
|
+
parseDomainAuthority,
|
|
71
|
+
);
|
|
72
|
+
const connection = parseConnectionStatus(source.connection);
|
|
73
|
+
if (connection.state !== "ready") {
|
|
74
|
+
incompatible("invalid_connection_status", connection);
|
|
75
|
+
}
|
|
76
|
+
const features = array(source.transportFeatures, BRIDGE_MAXIMUM_TRANSPORT_FEATURES).map(opaqueId);
|
|
77
|
+
const diagnostics = array(source.diagnostics, BRIDGE_MAXIMUM_DIAGNOSTICS).map(parseDiagnostic);
|
|
78
|
+
unique(features, String);
|
|
79
|
+
unique(capabilities, (item) => item.domainId);
|
|
80
|
+
unique(authorities, (item) => item.domainId);
|
|
81
|
+
unique(diagnostics, (item) => item.diagnosticId);
|
|
82
|
+
|
|
83
|
+
const capabilityDomains = new Set(
|
|
84
|
+
capabilities.map((item) => item.domainId),
|
|
85
|
+
);
|
|
86
|
+
const writerScopes = new Set<string>();
|
|
87
|
+
for (const authority of authorities) {
|
|
88
|
+
if (!capabilityDomains.has(authority.domainId)) {
|
|
89
|
+
incompatible("authority_without_capability", authority);
|
|
90
|
+
}
|
|
91
|
+
if (authority.writeAuthority === "authoritative") {
|
|
92
|
+
if (writerScopes.has(authority.scopeId)) {
|
|
93
|
+
incompatible("multiple_writers", authority.scopeId);
|
|
94
|
+
}
|
|
95
|
+
writerScopes.add(authority.scopeId);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
if (request !== undefined) {
|
|
99
|
+
const requested = new Set(request.requestedDomains);
|
|
100
|
+
for (const capability of capabilities) {
|
|
101
|
+
if (!requested.has(capability.domainId)) {
|
|
102
|
+
incompatible("unrequested_domain", capability.domainId);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
return {
|
|
108
|
+
protocolVersion: BRIDGE_PROTOCOL_VERSION,
|
|
109
|
+
host: parseHost(source.host),
|
|
110
|
+
sessionId: opaqueId(source.sessionId),
|
|
111
|
+
connection,
|
|
112
|
+
authentication: oneOf(
|
|
113
|
+
source.authentication,
|
|
114
|
+
BRIDGE_AUTHENTICATION_POSTURES,
|
|
115
|
+
"unknown_authentication_posture",
|
|
116
|
+
),
|
|
117
|
+
transportFeatures: features,
|
|
118
|
+
domainCapabilities: capabilities,
|
|
119
|
+
domainAuthorities: authorities,
|
|
120
|
+
diagnostics,
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
function parseHost(value: unknown): BridgeHostDescriptor {
|
|
125
|
+
const source = record(value, BRIDGE_FIELDS.BridgeHostDescriptor);
|
|
126
|
+
return {
|
|
127
|
+
hostInstanceId: opaqueId(source.hostInstanceId),
|
|
128
|
+
form: oneOf(source.form, BRIDGE_HOST_FORMS, "unknown_host_form"),
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
function parseConnectionStatus(value: unknown): BridgeConnectionStatus {
|
|
133
|
+
const source = record(value, BRIDGE_FIELDS.BridgeConnectionStatus);
|
|
134
|
+
const state = oneOf(
|
|
135
|
+
source.state,
|
|
136
|
+
BRIDGE_CONNECTION_STATES,
|
|
137
|
+
"unknown_connection_state",
|
|
138
|
+
);
|
|
139
|
+
const reason = nullable(source.reason, (candidate) =>
|
|
140
|
+
oneOf(
|
|
141
|
+
candidate,
|
|
142
|
+
BRIDGE_CONNECTION_REASONS,
|
|
143
|
+
"invalid_connection_status",
|
|
144
|
+
)
|
|
145
|
+
);
|
|
146
|
+
if (!validConnectionReason(state, reason)) {
|
|
147
|
+
incompatible("invalid_connection_status", value);
|
|
148
|
+
}
|
|
149
|
+
return { state, reason };
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
function validConnectionReason(
|
|
153
|
+
state: BridgeConnectionState,
|
|
154
|
+
reason: BridgeConnectionReason | null,
|
|
155
|
+
): boolean {
|
|
156
|
+
// Generated from `BridgeConnectionStatus::ADMITTED_REASONS`. This used to be
|
|
157
|
+
// an eleven-arm literal here, and Card 160 recorded it as the one rule that
|
|
158
|
+
// could not be derived because the pairing exists in no type. That was
|
|
159
|
+
// wrong: it exists in Rust, in a `matches!` arm, which `ts-rs` cannot carry
|
|
160
|
+
// because it is a relation between two enums rather than a type. The two
|
|
161
|
+
// copies agreed arm for arm — by maintenance, not by construction.
|
|
162
|
+
return BRIDGE_ADMITTED_CONNECTION_REASONS[state].includes(reason);
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
function parseDomainCapability(value: unknown): DomainCapabilityDescriptor {
|
|
166
|
+
const source = record(value, BRIDGE_FIELDS.DomainCapabilityDescriptor);
|
|
167
|
+
const capabilities = array(source.capabilities, BRIDGE_MAXIMUM_CAPABILITIES_PER_DOMAIN).map(opaqueId);
|
|
168
|
+
if (capabilities.length === 0) {
|
|
169
|
+
incompatible("invalid_array", value);
|
|
170
|
+
}
|
|
171
|
+
unique(capabilities, String);
|
|
172
|
+
return {
|
|
173
|
+
domainId: domainId(source.domainId),
|
|
174
|
+
capabilities,
|
|
175
|
+
};
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
function parseDomainAuthority(value: unknown): DomainAuthorityDescriptor {
|
|
179
|
+
const source = record(value, BRIDGE_FIELDS.DomainAuthorityDescriptor);
|
|
180
|
+
const availability = oneOf(
|
|
181
|
+
source.availability,
|
|
182
|
+
BRIDGE_DOMAIN_AVAILABILITIES,
|
|
183
|
+
"unknown_availability",
|
|
184
|
+
);
|
|
185
|
+
const readAuthority = oneOf(
|
|
186
|
+
source.readAuthority,
|
|
187
|
+
BRIDGE_READ_AUTHORITIES,
|
|
188
|
+
"unknown_read_authority",
|
|
189
|
+
);
|
|
190
|
+
const writeAuthority = oneOf(
|
|
191
|
+
source.writeAuthority,
|
|
192
|
+
BRIDGE_WRITE_AUTHORITIES,
|
|
193
|
+
"unknown_write_authority",
|
|
194
|
+
);
|
|
195
|
+
const executionAuthority = oneOf(
|
|
196
|
+
source.executionAuthority,
|
|
197
|
+
BRIDGE_EXECUTION_AUTHORITIES,
|
|
198
|
+
"unknown_execution_authority",
|
|
199
|
+
);
|
|
200
|
+
const authoritativeRevision = nullable(
|
|
201
|
+
source.authoritativeRevision,
|
|
202
|
+
integer,
|
|
203
|
+
);
|
|
204
|
+
const ownsAnything = readAuthority !== "none" ||
|
|
205
|
+
writeAuthority !== "none" ||
|
|
206
|
+
executionAuthority !== "none";
|
|
207
|
+
const revisionIsAuthoritative = readAuthority === "authoritative" ||
|
|
208
|
+
writeAuthority === "authoritative";
|
|
209
|
+
if (
|
|
210
|
+
(availability === "offline" &&
|
|
211
|
+
(ownsAnything || authoritativeRevision !== null)) ||
|
|
212
|
+
(authoritativeRevision !== null && !revisionIsAuthoritative)
|
|
213
|
+
) {
|
|
214
|
+
incompatible("invalid_authority_descriptor", value);
|
|
215
|
+
}
|
|
216
|
+
return {
|
|
217
|
+
domainId: domainId(source.domainId),
|
|
218
|
+
scopeId: opaqueId(source.scopeId),
|
|
219
|
+
availability,
|
|
220
|
+
readAuthority,
|
|
221
|
+
writeAuthority,
|
|
222
|
+
executionAuthority,
|
|
223
|
+
authorityEpoch: integer(source.authorityEpoch, 1),
|
|
224
|
+
authoritativeRevision,
|
|
225
|
+
};
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
function parseDiagnostic(value: unknown): BridgeDiagnostic {
|
|
229
|
+
const source = record(value, BRIDGE_FIELDS.BridgeDiagnostic);
|
|
230
|
+
if (
|
|
231
|
+
typeof source.message !== "string" ||
|
|
232
|
+
source.message.length === 0 ||
|
|
233
|
+
new TextEncoder().encode(source.message).length >
|
|
234
|
+
BRIDGE_MAXIMUM_DIAGNOSTIC_MESSAGE_BYTES
|
|
235
|
+
) {
|
|
236
|
+
incompatible("invalid_message", source.message);
|
|
237
|
+
}
|
|
238
|
+
return {
|
|
239
|
+
diagnosticId: opaqueId(source.diagnosticId),
|
|
240
|
+
message: source.message,
|
|
241
|
+
};
|
|
242
|
+
}
|
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
import { BRIDGE_FIELDS } from "../generated/fields.ts";
|
|
2
|
+
import {
|
|
3
|
+
BRIDGE_MAXIMUM_DEDUPLICATION_ENTRIES,
|
|
4
|
+
BRIDGE_MAXIMUM_FAILURE_MESSAGE_BYTES,
|
|
5
|
+
BRIDGE_MAXIMUM_OPAQUE_ID_BYTES,
|
|
6
|
+
BRIDGE_FAILURE_PHASES,
|
|
7
|
+
BRIDGE_RETRY_CLASSES,
|
|
8
|
+
type BridgeCancellationRequest,
|
|
9
|
+
type BridgeCommandEnvelope,
|
|
10
|
+
type BridgeCommandReply,
|
|
11
|
+
type BridgeDeduplicationSupport,
|
|
12
|
+
type BridgeFailure,
|
|
13
|
+
type BridgeQueryEnvelope,
|
|
14
|
+
type BridgeQueryReply,
|
|
15
|
+
type BridgeRequestContext,
|
|
16
|
+
} from "../generated/protocol.ts";
|
|
17
|
+
import {
|
|
18
|
+
type BridgeCodec,
|
|
19
|
+
incompatible,
|
|
20
|
+
integer,
|
|
21
|
+
nullable,
|
|
22
|
+
oneOf,
|
|
23
|
+
opaqueId,
|
|
24
|
+
record,
|
|
25
|
+
} from "./base.ts";
|
|
26
|
+
|
|
27
|
+
export function parseBridgeRequestContext(
|
|
28
|
+
value: unknown,
|
|
29
|
+
): BridgeRequestContext {
|
|
30
|
+
const source = record(value, BRIDGE_FIELDS.BridgeRequestContext);
|
|
31
|
+
return {
|
|
32
|
+
requestId: opaqueId(source.requestId),
|
|
33
|
+
sessionId: opaqueId(source.sessionId),
|
|
34
|
+
domainId: parseDomainId(source.domainId),
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export function parseBridgeQueryEnvelope<P>(
|
|
39
|
+
value: unknown,
|
|
40
|
+
payload: BridgeCodec<P>,
|
|
41
|
+
): BridgeQueryEnvelope<P> {
|
|
42
|
+
const source = record(value, BRIDGE_FIELDS.BridgeQueryEnvelope);
|
|
43
|
+
return {
|
|
44
|
+
context: parseBridgeRequestContext(source.context),
|
|
45
|
+
payload: payload.parse(source.payload),
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export function parseBridgeCommandEnvelope<P>(
|
|
50
|
+
value: unknown,
|
|
51
|
+
payload: BridgeCodec<P>,
|
|
52
|
+
): BridgeCommandEnvelope<P> {
|
|
53
|
+
const source = record(value, BRIDGE_FIELDS.BridgeCommandEnvelope);
|
|
54
|
+
return {
|
|
55
|
+
context: parseBridgeRequestContext(source.context),
|
|
56
|
+
authorityEpoch: integer(source.authorityEpoch, 1),
|
|
57
|
+
expectedRevision: nullable(source.expectedRevision, integer),
|
|
58
|
+
idempotencyKey: nullable(source.idempotencyKey, opaqueId),
|
|
59
|
+
payload: payload.parse(source.payload),
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export function parseBridgeCancellationRequest(
|
|
64
|
+
value: unknown,
|
|
65
|
+
): BridgeCancellationRequest {
|
|
66
|
+
const source = record(value, BRIDGE_FIELDS.BridgeCancellationRequest);
|
|
67
|
+
return {
|
|
68
|
+
context: parseBridgeRequestContext(source.context),
|
|
69
|
+
targetRequestId: opaqueId(source.targetRequestId),
|
|
70
|
+
jobId: opaqueId(source.jobId),
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export function parseBridgeFailure<D>(
|
|
75
|
+
value: unknown,
|
|
76
|
+
details: BridgeCodec<D>,
|
|
77
|
+
): BridgeFailure<D> {
|
|
78
|
+
const source = record(value, BRIDGE_FIELDS.BridgeFailure);
|
|
79
|
+
if (
|
|
80
|
+
typeof source.message !== "string" ||
|
|
81
|
+
source.message.length === 0 ||
|
|
82
|
+
new TextEncoder().encode(source.message).length >
|
|
83
|
+
BRIDGE_MAXIMUM_FAILURE_MESSAGE_BYTES
|
|
84
|
+
) {
|
|
85
|
+
incompatible("invalid_message", source.message);
|
|
86
|
+
}
|
|
87
|
+
return {
|
|
88
|
+
code: opaqueId(source.code),
|
|
89
|
+
message: source.message,
|
|
90
|
+
retryClass: oneOf(
|
|
91
|
+
source.retryClass,
|
|
92
|
+
BRIDGE_RETRY_CLASSES,
|
|
93
|
+
"unknown_retry_class",
|
|
94
|
+
),
|
|
95
|
+
phase: oneOf(
|
|
96
|
+
source.phase,
|
|
97
|
+
BRIDGE_FAILURE_PHASES,
|
|
98
|
+
"unknown_failure_phase",
|
|
99
|
+
),
|
|
100
|
+
details: nullable(source.details, details.parse),
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
export function parseBridgeDeduplicationSupport(
|
|
105
|
+
value: unknown,
|
|
106
|
+
): BridgeDeduplicationSupport {
|
|
107
|
+
if (value === "unsupported") {
|
|
108
|
+
return value;
|
|
109
|
+
}
|
|
110
|
+
if (
|
|
111
|
+
typeof value !== "object" ||
|
|
112
|
+
value === null ||
|
|
113
|
+
Array.isArray(value) ||
|
|
114
|
+
Object.keys(value).length !== 1 ||
|
|
115
|
+
!("finite" in value)
|
|
116
|
+
) {
|
|
117
|
+
incompatible("unknown_deduplication_support", value);
|
|
118
|
+
}
|
|
119
|
+
const source = value as { finite: unknown };
|
|
120
|
+
const capacity = integer(source.finite, 1);
|
|
121
|
+
if (capacity > BRIDGE_MAXIMUM_DEDUPLICATION_ENTRIES) {
|
|
122
|
+
incompatible("unknown_deduplication_support", value);
|
|
123
|
+
}
|
|
124
|
+
return { finite: capacity };
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
export function parseBridgeQueryReply<S, D>(
|
|
128
|
+
value: unknown,
|
|
129
|
+
success: BridgeCodec<S>,
|
|
130
|
+
details: BridgeCodec<D>,
|
|
131
|
+
): BridgeQueryReply<S, D> {
|
|
132
|
+
const source = record(value, BRIDGE_FIELDS.BridgeQueryReply);
|
|
133
|
+
return {
|
|
134
|
+
requestId: opaqueId(source.requestId),
|
|
135
|
+
outcome: parseQueryOutcome(source.outcome, success, details),
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
export function parseBridgeCommandReply<S, D>(
|
|
140
|
+
value: unknown,
|
|
141
|
+
success: BridgeCodec<S>,
|
|
142
|
+
details: BridgeCodec<D>,
|
|
143
|
+
): BridgeCommandReply<S, D> {
|
|
144
|
+
const source = record(value, BRIDGE_FIELDS.BridgeCommandReply);
|
|
145
|
+
return {
|
|
146
|
+
requestId: opaqueId(source.requestId),
|
|
147
|
+
authoritativeRevision: nullable(
|
|
148
|
+
source.authoritativeRevision,
|
|
149
|
+
integer,
|
|
150
|
+
),
|
|
151
|
+
outcome: parseCommandOutcome(source.outcome, success, details),
|
|
152
|
+
};
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
function parseQueryOutcome<S, D>(
|
|
156
|
+
value: unknown,
|
|
157
|
+
success: BridgeCodec<S>,
|
|
158
|
+
details: BridgeCodec<D>,
|
|
159
|
+
): BridgeQueryReply<S, D>["outcome"] {
|
|
160
|
+
const source = variant(value, ["success", "rejected"], "unknown_query_outcome");
|
|
161
|
+
if ("success" in source) {
|
|
162
|
+
return { success: success.parse(source.success) };
|
|
163
|
+
}
|
|
164
|
+
return { rejected: parseBridgeFailure(source.rejected, details) };
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
function parseCommandOutcome<S, D>(
|
|
168
|
+
value: unknown,
|
|
169
|
+
success: BridgeCodec<S>,
|
|
170
|
+
details: BridgeCodec<D>,
|
|
171
|
+
): BridgeCommandReply<S, D>["outcome"] {
|
|
172
|
+
const source = variant(
|
|
173
|
+
value,
|
|
174
|
+
["applied", "rejected", "indeterminate"],
|
|
175
|
+
"unknown_command_outcome",
|
|
176
|
+
);
|
|
177
|
+
if ("applied" in source) {
|
|
178
|
+
return { applied: success.parse(source.applied) };
|
|
179
|
+
}
|
|
180
|
+
if ("rejected" in source) {
|
|
181
|
+
return { rejected: parseBridgeFailure(source.rejected, details) };
|
|
182
|
+
}
|
|
183
|
+
return {
|
|
184
|
+
indeterminate: parseBridgeFailure(source.indeterminate, details),
|
|
185
|
+
};
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
function variant(
|
|
189
|
+
value: unknown,
|
|
190
|
+
keys: readonly string[],
|
|
191
|
+
code: "unknown_query_outcome" | "unknown_command_outcome",
|
|
192
|
+
): Record<string, unknown> {
|
|
193
|
+
if (typeof value !== "object" || value === null || Array.isArray(value)) {
|
|
194
|
+
incompatible(code, value);
|
|
195
|
+
}
|
|
196
|
+
const source = value as Record<string, unknown>;
|
|
197
|
+
const present = Object.keys(source);
|
|
198
|
+
if (present.length !== 1 || !keys.includes(present[0]!)) {
|
|
199
|
+
incompatible(code, value);
|
|
200
|
+
}
|
|
201
|
+
return source;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
function parseDomainId(value: unknown): string {
|
|
205
|
+
if (
|
|
206
|
+
typeof value !== "string" ||
|
|
207
|
+
new TextEncoder().encode(value).length > BRIDGE_MAXIMUM_OPAQUE_ID_BYTES ||
|
|
208
|
+
!/^[a-z][a-z0-9_-]*(\.[a-z][a-z0-9_-]*)*$/.test(value)
|
|
209
|
+
) {
|
|
210
|
+
incompatible("invalid_domain_id", value);
|
|
211
|
+
}
|
|
212
|
+
return value;
|
|
213
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { BRIDGE_FIELDS } from "../generated/fields.ts";
|
|
2
|
+
import type {
|
|
3
|
+
BridgeEventEnvelope,
|
|
4
|
+
BridgeSnapshotEnvelope,
|
|
5
|
+
BridgeStreamCursor,
|
|
6
|
+
} from "../generated/protocol.ts";
|
|
7
|
+
import {
|
|
8
|
+
type BridgeCodec,
|
|
9
|
+
domainId,
|
|
10
|
+
integer,
|
|
11
|
+
opaqueId,
|
|
12
|
+
record,
|
|
13
|
+
} from "./base.ts";
|
|
14
|
+
|
|
15
|
+
export function parseBridgeStreamCursor(
|
|
16
|
+
value: unknown,
|
|
17
|
+
): BridgeStreamCursor {
|
|
18
|
+
const source = record(value, BRIDGE_FIELDS.BridgeStreamCursor);
|
|
19
|
+
return {
|
|
20
|
+
sessionId: opaqueId(source.sessionId),
|
|
21
|
+
domainId: domainId(source.domainId),
|
|
22
|
+
authorityEpoch: integer(source.authorityEpoch, 1),
|
|
23
|
+
sequence: integer(source.sequence),
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export function parseBridgeSnapshotEnvelope<P>(
|
|
28
|
+
value: unknown,
|
|
29
|
+
payload: BridgeCodec<P>,
|
|
30
|
+
): BridgeSnapshotEnvelope<P> {
|
|
31
|
+
const source = record(value, BRIDGE_FIELDS.BridgeEventEnvelope);
|
|
32
|
+
return {
|
|
33
|
+
cursor: parseBridgeStreamCursor(source.cursor),
|
|
34
|
+
payload: payload.parse(source.payload),
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export function parseBridgeEventEnvelope<P>(
|
|
39
|
+
value: unknown,
|
|
40
|
+
payload: BridgeCodec<P>,
|
|
41
|
+
): BridgeEventEnvelope<P> {
|
|
42
|
+
const source = record(value, BRIDGE_FIELDS.BridgeEventEnvelope);
|
|
43
|
+
return {
|
|
44
|
+
cursor: parseBridgeStreamCursor(source.cursor),
|
|
45
|
+
payload: payload.parse(source.payload),
|
|
46
|
+
};
|
|
47
|
+
}
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import {
|
|
2
|
+
assertValidCommandAvailabilitySnapshot,
|
|
3
|
+
assertValidCommandCatalogue,
|
|
4
|
+
assertValidCommandKeymapCommit,
|
|
5
|
+
assertValidCommandKeymapLoadOutcome,
|
|
6
|
+
assertValidCommandKeymapMutationResult,
|
|
7
|
+
assertValidCommandKeymapPreview,
|
|
8
|
+
assertValidCommandKeymapPreviewResult,
|
|
9
|
+
assertValidCommandKeymapReset,
|
|
10
|
+
} from "./validation.ts";
|
|
11
|
+
import type {
|
|
12
|
+
CommandAvailabilitySnapshot,
|
|
13
|
+
CommandCatalogueSnapshot,
|
|
14
|
+
CommandKeymapCommit,
|
|
15
|
+
CommandKeymapLoadOutcome,
|
|
16
|
+
CommandKeymapMutationResult,
|
|
17
|
+
CommandKeymapPreview,
|
|
18
|
+
CommandKeymapPreviewResult,
|
|
19
|
+
CommandKeymapReset,
|
|
20
|
+
} from "./generated/protocol.ts";
|
|
21
|
+
import type {
|
|
22
|
+
CommandExecutionIntent,
|
|
23
|
+
CommandExecutionOutcome,
|
|
24
|
+
CommandPorts,
|
|
25
|
+
CommandUnlisten,
|
|
26
|
+
} from "./ports.ts";
|
|
27
|
+
|
|
28
|
+
export interface CommandClientLoad {
|
|
29
|
+
readonly catalogue: CommandCatalogueSnapshot;
|
|
30
|
+
readonly keymap: CommandKeymapLoadOutcome;
|
|
31
|
+
readonly availability: CommandAvailabilitySnapshot;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export class CommandClient {
|
|
35
|
+
readonly #ports: CommandPorts;
|
|
36
|
+
|
|
37
|
+
constructor(ports: CommandPorts) {
|
|
38
|
+
this.#ports = ports;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
nextRequestId(): string {
|
|
42
|
+
return this.#ports.nextRequestId();
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
async load(): Promise<CommandClientLoad> {
|
|
46
|
+
const [catalogue, keymap, availability] = await Promise.all([
|
|
47
|
+
this.#ports.catalogue.load(),
|
|
48
|
+
this.#ports.keymap.load(),
|
|
49
|
+
this.#ports.availability.load(),
|
|
50
|
+
]);
|
|
51
|
+
assertValidCommandCatalogue(catalogue);
|
|
52
|
+
assertValidCommandKeymapLoadOutcome(keymap);
|
|
53
|
+
assertValidCommandAvailabilitySnapshot(availability);
|
|
54
|
+
return { catalogue, keymap, availability };
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
async listen(invalidate: () => void): Promise<readonly CommandUnlisten[]> {
|
|
58
|
+
const results = await Promise.allSettled(
|
|
59
|
+
[
|
|
60
|
+
this.#ports.catalogue,
|
|
61
|
+
this.#ports.keymap,
|
|
62
|
+
this.#ports.availability,
|
|
63
|
+
].flatMap((port) =>
|
|
64
|
+
port.listen === undefined ? [] : [port.listen(invalidate)],
|
|
65
|
+
),
|
|
66
|
+
);
|
|
67
|
+
const unlisten = results.flatMap((result) =>
|
|
68
|
+
result.status === "fulfilled" ? [result.value] : [],
|
|
69
|
+
);
|
|
70
|
+
const failure = results.find(
|
|
71
|
+
(result): result is PromiseRejectedResult =>
|
|
72
|
+
result.status === "rejected",
|
|
73
|
+
);
|
|
74
|
+
if (failure !== undefined) {
|
|
75
|
+
await Promise.allSettled(unlisten.map((dispose) => dispose()));
|
|
76
|
+
throw failure.reason;
|
|
77
|
+
}
|
|
78
|
+
return unlisten;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
execute(intent: CommandExecutionIntent): Promise<CommandExecutionOutcome> {
|
|
82
|
+
return this.#ports.executor.execute(intent);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
async preview(
|
|
86
|
+
request: CommandKeymapPreview,
|
|
87
|
+
): Promise<CommandKeymapPreviewResult> {
|
|
88
|
+
assertValidCommandKeymapPreview(request);
|
|
89
|
+
const preview = this.#ports.keymap.preview;
|
|
90
|
+
if (preview === undefined) throw new CommandKeymapReadOnlyError();
|
|
91
|
+
const result = await preview(request);
|
|
92
|
+
assertValidCommandKeymapPreviewResult(result);
|
|
93
|
+
return result;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
async commit(
|
|
97
|
+
request: CommandKeymapCommit,
|
|
98
|
+
): Promise<CommandKeymapMutationResult> {
|
|
99
|
+
assertValidCommandKeymapCommit(request);
|
|
100
|
+
const commit = this.#ports.keymap.commit;
|
|
101
|
+
if (commit === undefined) throw new CommandKeymapReadOnlyError();
|
|
102
|
+
const result = await commit(request);
|
|
103
|
+
assertValidCommandKeymapMutationResult(result);
|
|
104
|
+
return result;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
async reset(
|
|
108
|
+
request: CommandKeymapReset,
|
|
109
|
+
): Promise<CommandKeymapMutationResult> {
|
|
110
|
+
assertValidCommandKeymapReset(request);
|
|
111
|
+
const reset = this.#ports.keymap.reset;
|
|
112
|
+
if (reset === undefined) throw new CommandKeymapReadOnlyError();
|
|
113
|
+
const result = await reset(request);
|
|
114
|
+
assertValidCommandKeymapMutationResult(result);
|
|
115
|
+
return result;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
export class CommandKeymapReadOnlyError extends Error {
|
|
120
|
+
constructor() {
|
|
121
|
+
super("command keymap mutation is not composed");
|
|
122
|
+
this.name = "CommandKeymapReadOnlyError";
|
|
123
|
+
}
|
|
124
|
+
}
|