@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,318 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
BridgeAuthorityCursorDecision,
|
|
3
|
+
BridgeAuthorityRequirement,
|
|
4
|
+
BridgeConnectionReason,
|
|
5
|
+
BridgeConnectionState,
|
|
6
|
+
BridgeConnectionStatus,
|
|
7
|
+
BridgeConnectionTransitionReceipt,
|
|
8
|
+
BridgeHelloRequest,
|
|
9
|
+
BridgeNegotiationReceipt,
|
|
10
|
+
BridgeReconnectSchedule,
|
|
11
|
+
BridgeRequiredAuthority,
|
|
12
|
+
BridgeRetryClass,
|
|
13
|
+
BridgeStreamCursor,
|
|
14
|
+
DomainAuthorityDescriptor,
|
|
15
|
+
} from "../generated/protocol.ts";
|
|
16
|
+
import {
|
|
17
|
+
parseBridgeHelloRequest,
|
|
18
|
+
parseBridgeNegotiationReceipt,
|
|
19
|
+
parseBridgeStreamCursor,
|
|
20
|
+
} from "../validation.ts";
|
|
21
|
+
import { BridgeSession } from "../session.ts";
|
|
22
|
+
import {
|
|
23
|
+
checkedDeadline,
|
|
24
|
+
checkedIncrement,
|
|
25
|
+
checkedMonotonic,
|
|
26
|
+
checkedRetryClass,
|
|
27
|
+
checkedRetryLimit,
|
|
28
|
+
BridgeRuntimeError,
|
|
29
|
+
type BridgeRuntimeBackoff,
|
|
30
|
+
type BridgeRuntimeClock,
|
|
31
|
+
} from "./policy.ts";
|
|
32
|
+
|
|
33
|
+
export class BridgeConnectionRuntime {
|
|
34
|
+
readonly #clock: BridgeRuntimeClock;
|
|
35
|
+
readonly #backoff: BridgeRuntimeBackoff;
|
|
36
|
+
readonly #reconnectLimit: number;
|
|
37
|
+
#status: BridgeConnectionStatus = { state: "idle", reason: null };
|
|
38
|
+
#sequence = 0;
|
|
39
|
+
#reconnectAttempts = 0;
|
|
40
|
+
#reconnectNotBefore: number | undefined;
|
|
41
|
+
#session: BridgeSession | undefined;
|
|
42
|
+
|
|
43
|
+
constructor(
|
|
44
|
+
clock: BridgeRuntimeClock,
|
|
45
|
+
backoff: BridgeRuntimeBackoff,
|
|
46
|
+
reconnectLimit: number,
|
|
47
|
+
) {
|
|
48
|
+
this.#clock = clock;
|
|
49
|
+
this.#backoff = backoff;
|
|
50
|
+
this.#reconnectLimit = checkedRetryLimit(reconnectLimit);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
get status(): BridgeConnectionStatus {
|
|
54
|
+
return this.#status;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
get session(): BridgeSession | undefined {
|
|
58
|
+
return this.#session;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
connect(): BridgeConnectionTransitionReceipt {
|
|
62
|
+
return this.#transition("connecting", "connectRequested");
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
transportReady(): BridgeConnectionTransitionReceipt {
|
|
66
|
+
const at = checkedMonotonic(this.#clock.now());
|
|
67
|
+
if (
|
|
68
|
+
this.#status.state === "reconnecting" &&
|
|
69
|
+
this.#reconnectNotBefore !== undefined &&
|
|
70
|
+
at < this.#reconnectNotBefore
|
|
71
|
+
) {
|
|
72
|
+
throw new BridgeRuntimeError(
|
|
73
|
+
"retry_not_due",
|
|
74
|
+
"reconnect backoff has not elapsed",
|
|
75
|
+
);
|
|
76
|
+
}
|
|
77
|
+
const receipt = this.#commitAt(
|
|
78
|
+
"negotiating",
|
|
79
|
+
"transportReady",
|
|
80
|
+
at,
|
|
81
|
+
null,
|
|
82
|
+
);
|
|
83
|
+
this.#reconnectNotBefore = undefined;
|
|
84
|
+
return receipt;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
acceptNegotiation(
|
|
88
|
+
value: unknown,
|
|
89
|
+
requirements: readonly BridgeAuthorityRequirement[],
|
|
90
|
+
request?: BridgeHelloRequest,
|
|
91
|
+
): BridgeConnectionTransitionReceipt {
|
|
92
|
+
this.#requireTransition("ready");
|
|
93
|
+
const checkedRequest = request === undefined
|
|
94
|
+
? undefined
|
|
95
|
+
: parseBridgeHelloRequest(request);
|
|
96
|
+
const receipt = parseBridgeNegotiationReceipt(value, checkedRequest);
|
|
97
|
+
assertRequiredAuthorities(receipt, requirements);
|
|
98
|
+
this.#session = new BridgeSession(receipt);
|
|
99
|
+
this.#reconnectAttempts = 0;
|
|
100
|
+
this.#reconnectNotBefore = undefined;
|
|
101
|
+
return this.#commit("ready", "negotiationAccepted", null);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
degrade(
|
|
105
|
+
reason: Extract<
|
|
106
|
+
BridgeConnectionReason,
|
|
107
|
+
"capabilityChanged" | "transportLost" | "hostFailure"
|
|
108
|
+
>,
|
|
109
|
+
): BridgeConnectionTransitionReceipt {
|
|
110
|
+
if (
|
|
111
|
+
reason !== "capabilityChanged" &&
|
|
112
|
+
reason !== "transportLost" &&
|
|
113
|
+
reason !== "hostFailure"
|
|
114
|
+
) {
|
|
115
|
+
throw new BridgeRuntimeError(
|
|
116
|
+
"invalid_transition",
|
|
117
|
+
"invalid degraded-state reason",
|
|
118
|
+
);
|
|
119
|
+
}
|
|
120
|
+
return this.#transition("degraded", reason);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
reconnect(retryClass: BridgeRetryClass): BridgeConnectionTransitionReceipt {
|
|
124
|
+
retryClass = checkedRetryClass(retryClass);
|
|
125
|
+
const attempt = this.#reconnectAttempts + 1;
|
|
126
|
+
const admitted = retryClass !== "never" &&
|
|
127
|
+
attempt <= this.#reconnectLimit;
|
|
128
|
+
this.#requireTransition(admitted ? "reconnecting" : "offline");
|
|
129
|
+
const at = checkedMonotonic(this.#clock.now());
|
|
130
|
+
let reconnect: BridgeReconnectSchedule | null = null;
|
|
131
|
+
if (admitted) {
|
|
132
|
+
reconnect = {
|
|
133
|
+
attempt,
|
|
134
|
+
retryClass,
|
|
135
|
+
notBefore: checkedDeadline(
|
|
136
|
+
at,
|
|
137
|
+
this.#backoff.delay(retryClass, attempt),
|
|
138
|
+
),
|
|
139
|
+
};
|
|
140
|
+
this.#reconnectAttempts = attempt;
|
|
141
|
+
this.#reconnectNotBefore = reconnect.notBefore;
|
|
142
|
+
} else {
|
|
143
|
+
this.#reconnectNotBefore = undefined;
|
|
144
|
+
}
|
|
145
|
+
this.#session = undefined;
|
|
146
|
+
return this.#commitAt(
|
|
147
|
+
admitted ? "reconnecting" : "offline",
|
|
148
|
+
admitted ? "retryScheduled" : "transportLost",
|
|
149
|
+
at,
|
|
150
|
+
reconnect,
|
|
151
|
+
);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
incompatible(): BridgeConnectionTransitionReceipt {
|
|
155
|
+
return this.#terminal("incompatible", "versionMismatch");
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
unauthorized(): BridgeConnectionTransitionReceipt {
|
|
159
|
+
return this.#terminal("unauthorized", "authorizationRejected");
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
fail(): BridgeConnectionTransitionReceipt {
|
|
163
|
+
return this.#terminal("failed", "hostFailure");
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
close(): BridgeConnectionTransitionReceipt {
|
|
167
|
+
return this.#terminal("closed", "shutdown");
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
classifyCursor(cursor: BridgeStreamCursor): BridgeAuthorityCursorDecision {
|
|
171
|
+
cursor = parseBridgeStreamCursor(cursor);
|
|
172
|
+
const session = this.#session;
|
|
173
|
+
if (session === undefined || cursor.sessionId !== session.receipt.sessionId) {
|
|
174
|
+
return "supersededSession";
|
|
175
|
+
}
|
|
176
|
+
const authority = session.authority(cursor.domainId);
|
|
177
|
+
if (authority === undefined) {
|
|
178
|
+
return "unknownDomain";
|
|
179
|
+
}
|
|
180
|
+
if (cursor.authorityEpoch < authority.authorityEpoch) {
|
|
181
|
+
return "staleAuthority";
|
|
182
|
+
}
|
|
183
|
+
if (cursor.authorityEpoch > authority.authorityEpoch) {
|
|
184
|
+
return "refreshAuthority";
|
|
185
|
+
}
|
|
186
|
+
return "current";
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
#terminal(
|
|
190
|
+
state: Extract<
|
|
191
|
+
BridgeConnectionState,
|
|
192
|
+
"incompatible" | "unauthorized" | "failed" | "closed"
|
|
193
|
+
>,
|
|
194
|
+
reason: BridgeConnectionReason,
|
|
195
|
+
): BridgeConnectionTransitionReceipt {
|
|
196
|
+
this.#requireTransition(state);
|
|
197
|
+
this.#session = undefined;
|
|
198
|
+
this.#reconnectNotBefore = undefined;
|
|
199
|
+
return this.#commit(state, reason, null);
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
#transition(
|
|
203
|
+
state: BridgeConnectionState,
|
|
204
|
+
reason: BridgeConnectionReason,
|
|
205
|
+
): BridgeConnectionTransitionReceipt {
|
|
206
|
+
this.#requireTransition(state);
|
|
207
|
+
return this.#commit(state, reason, null);
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
#commit(
|
|
211
|
+
state: BridgeConnectionState,
|
|
212
|
+
reason: BridgeConnectionReason,
|
|
213
|
+
reconnect: BridgeReconnectSchedule | null,
|
|
214
|
+
): BridgeConnectionTransitionReceipt {
|
|
215
|
+
return this.#commitAt(
|
|
216
|
+
state,
|
|
217
|
+
reason,
|
|
218
|
+
checkedMonotonic(this.#clock.now()),
|
|
219
|
+
reconnect,
|
|
220
|
+
);
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
#commitAt(
|
|
224
|
+
state: BridgeConnectionState,
|
|
225
|
+
reason: BridgeConnectionReason,
|
|
226
|
+
at: number,
|
|
227
|
+
reconnect: BridgeReconnectSchedule | null,
|
|
228
|
+
): BridgeConnectionTransitionReceipt {
|
|
229
|
+
const previous = this.#status;
|
|
230
|
+
const current = { state, reason } as BridgeConnectionStatus;
|
|
231
|
+
this.#sequence = checkedIncrement(this.#sequence);
|
|
232
|
+
this.#status = current;
|
|
233
|
+
return {
|
|
234
|
+
sequence: this.#sequence,
|
|
235
|
+
at,
|
|
236
|
+
previous,
|
|
237
|
+
current,
|
|
238
|
+
sessionId: this.#session?.receipt.sessionId ?? null,
|
|
239
|
+
reconnect,
|
|
240
|
+
};
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
#requireTransition(next: BridgeConnectionState): void {
|
|
244
|
+
if (!transitionAllowed(this.#status.state, next)) {
|
|
245
|
+
throw new BridgeRuntimeError(
|
|
246
|
+
"invalid_transition",
|
|
247
|
+
`cannot transition from ${this.#status.state} to ${next}`,
|
|
248
|
+
);
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
function assertRequiredAuthorities(
|
|
254
|
+
receipt: BridgeNegotiationReceipt,
|
|
255
|
+
requirements: readonly BridgeAuthorityRequirement[],
|
|
256
|
+
): void {
|
|
257
|
+
for (const requirement of requirements) {
|
|
258
|
+
const authority = receipt.domainAuthorities.find(
|
|
259
|
+
(candidate) => candidate.domainId === requirement.domainId,
|
|
260
|
+
);
|
|
261
|
+
if (
|
|
262
|
+
authority === undefined ||
|
|
263
|
+
authority.availability === "offline" ||
|
|
264
|
+
!authoritySatisfies(authority, requirement.authority)
|
|
265
|
+
) {
|
|
266
|
+
throw new BridgeRuntimeError(
|
|
267
|
+
"required_authority_unavailable",
|
|
268
|
+
`${requirement.authority} unavailable for ${requirement.domainId}`,
|
|
269
|
+
);
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
function authoritySatisfies(
|
|
275
|
+
authority: DomainAuthorityDescriptor,
|
|
276
|
+
required: BridgeRequiredAuthority,
|
|
277
|
+
): boolean {
|
|
278
|
+
switch (required) {
|
|
279
|
+
case "available":
|
|
280
|
+
return true;
|
|
281
|
+
case "readable":
|
|
282
|
+
return authority.readAuthority !== "none";
|
|
283
|
+
case "authoritativeRead":
|
|
284
|
+
return authority.readAuthority === "authoritative";
|
|
285
|
+
case "writable":
|
|
286
|
+
return authority.writeAuthority === "authoritative";
|
|
287
|
+
case "executable":
|
|
288
|
+
return authority.executionAuthority === "executor";
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
function transitionAllowed(
|
|
293
|
+
current: BridgeConnectionState,
|
|
294
|
+
next: BridgeConnectionState,
|
|
295
|
+
): boolean {
|
|
296
|
+
if (next === "closed") {
|
|
297
|
+
return current !== "closed";
|
|
298
|
+
}
|
|
299
|
+
const transitions: Partial<
|
|
300
|
+
Record<BridgeConnectionState, readonly BridgeConnectionState[]>
|
|
301
|
+
> = {
|
|
302
|
+
idle: ["connecting"],
|
|
303
|
+
connecting: ["negotiating", "reconnecting", "offline", "failed"],
|
|
304
|
+
negotiating: [
|
|
305
|
+
"ready",
|
|
306
|
+
"reconnecting",
|
|
307
|
+
"offline",
|
|
308
|
+
"incompatible",
|
|
309
|
+
"unauthorized",
|
|
310
|
+
"failed",
|
|
311
|
+
],
|
|
312
|
+
ready: ["degraded", "reconnecting", "offline", "failed"],
|
|
313
|
+
degraded: ["ready", "reconnecting", "offline", "failed"],
|
|
314
|
+
reconnecting: ["negotiating", "reconnecting", "offline", "failed"],
|
|
315
|
+
offline: ["reconnecting", "failed"],
|
|
316
|
+
};
|
|
317
|
+
return transitions[current]?.includes(next) ?? false;
|
|
318
|
+
}
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import {
|
|
2
|
+
BRIDGE_RETRY_CLASSES,
|
|
3
|
+
type BridgeRetryClass,
|
|
4
|
+
} from "../generated/protocol.ts";
|
|
5
|
+
|
|
6
|
+
export const MAXIMUM_BRIDGE_AUTOMATIC_RETRIES = 64;
|
|
7
|
+
|
|
8
|
+
export interface BridgeRuntimeClock {
|
|
9
|
+
now(): number;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export interface BridgeRuntimeBackoff {
|
|
13
|
+
delay(retryClass: BridgeRetryClass, attempt: number): number;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export class BridgeRuntimeError extends Error {
|
|
17
|
+
readonly code:
|
|
18
|
+
| "invalid_transition"
|
|
19
|
+
| "required_authority_unavailable"
|
|
20
|
+
| "invalid_retry_limit"
|
|
21
|
+
| "invalid_retry_class"
|
|
22
|
+
| "invalid_time"
|
|
23
|
+
| "retry_not_due";
|
|
24
|
+
|
|
25
|
+
constructor(
|
|
26
|
+
code: BridgeRuntimeError["code"],
|
|
27
|
+
detail: string,
|
|
28
|
+
) {
|
|
29
|
+
super(`bridge runtime: ${detail}`);
|
|
30
|
+
this.name = "BridgeRuntimeError";
|
|
31
|
+
this.code = code;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export function checkedRetryLimit(value: number): number {
|
|
36
|
+
if (
|
|
37
|
+
!Number.isSafeInteger(value) ||
|
|
38
|
+
value < 0 ||
|
|
39
|
+
value > MAXIMUM_BRIDGE_AUTOMATIC_RETRIES
|
|
40
|
+
) {
|
|
41
|
+
throw new BridgeRuntimeError(
|
|
42
|
+
"invalid_retry_limit",
|
|
43
|
+
`retry limit must be 0..${MAXIMUM_BRIDGE_AUTOMATIC_RETRIES}`,
|
|
44
|
+
);
|
|
45
|
+
}
|
|
46
|
+
return value;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export function checkedRetryClass(
|
|
50
|
+
value: BridgeRetryClass,
|
|
51
|
+
): BridgeRetryClass {
|
|
52
|
+
if (!BRIDGE_RETRY_CLASSES.includes(value)) {
|
|
53
|
+
throw new BridgeRuntimeError(
|
|
54
|
+
"invalid_retry_class",
|
|
55
|
+
"unknown retry class",
|
|
56
|
+
);
|
|
57
|
+
}
|
|
58
|
+
return value;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export function checkedMonotonic(value: number): number {
|
|
62
|
+
if (!Number.isSafeInteger(value) || value < 0) {
|
|
63
|
+
throw new BridgeRuntimeError(
|
|
64
|
+
"invalid_time",
|
|
65
|
+
"clock must return a nonnegative safe integer",
|
|
66
|
+
);
|
|
67
|
+
}
|
|
68
|
+
return value;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export function checkedDeadline(at: number, delay: number): number {
|
|
72
|
+
const checkedDelay = checkedMonotonic(delay);
|
|
73
|
+
const value = at + checkedDelay;
|
|
74
|
+
if (!Number.isSafeInteger(value)) {
|
|
75
|
+
throw new BridgeRuntimeError("invalid_time", "retry deadline overflow");
|
|
76
|
+
}
|
|
77
|
+
return value;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export function checkedIncrement(value: number): number {
|
|
81
|
+
const next = value + 1;
|
|
82
|
+
if (!Number.isSafeInteger(next)) {
|
|
83
|
+
throw new BridgeRuntimeError(
|
|
84
|
+
"invalid_time",
|
|
85
|
+
"transition sequence exhausted",
|
|
86
|
+
);
|
|
87
|
+
}
|
|
88
|
+
return next;
|
|
89
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
BridgeQueryRetryDecision,
|
|
3
|
+
BridgeReconnectSchedule,
|
|
4
|
+
BridgeRetryClass,
|
|
5
|
+
} from "../generated/protocol.ts";
|
|
6
|
+
import {
|
|
7
|
+
checkedDeadline,
|
|
8
|
+
checkedMonotonic,
|
|
9
|
+
checkedRetryClass,
|
|
10
|
+
checkedRetryLimit,
|
|
11
|
+
type BridgeRuntimeBackoff,
|
|
12
|
+
type BridgeRuntimeClock,
|
|
13
|
+
} from "./policy.ts";
|
|
14
|
+
|
|
15
|
+
export class BridgeQueryRetryRuntime {
|
|
16
|
+
readonly #clock: BridgeRuntimeClock;
|
|
17
|
+
readonly #backoff: BridgeRuntimeBackoff;
|
|
18
|
+
readonly #limit: number;
|
|
19
|
+
#scheduled = 0;
|
|
20
|
+
|
|
21
|
+
constructor(
|
|
22
|
+
clock: BridgeRuntimeClock,
|
|
23
|
+
backoff: BridgeRuntimeBackoff,
|
|
24
|
+
limit: number,
|
|
25
|
+
) {
|
|
26
|
+
this.#clock = clock;
|
|
27
|
+
this.#backoff = backoff;
|
|
28
|
+
this.#limit = checkedRetryLimit(limit);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
schedule(
|
|
32
|
+
decision: BridgeQueryRetryDecision,
|
|
33
|
+
retryClass: BridgeRetryClass,
|
|
34
|
+
): BridgeReconnectSchedule | undefined {
|
|
35
|
+
retryClass = checkedRetryClass(retryClass);
|
|
36
|
+
if (
|
|
37
|
+
decision !== "retry" ||
|
|
38
|
+
retryClass === "never" ||
|
|
39
|
+
this.#scheduled >= this.#limit
|
|
40
|
+
) {
|
|
41
|
+
return undefined;
|
|
42
|
+
}
|
|
43
|
+
const attempt = this.#scheduled + 1;
|
|
44
|
+
const at = checkedMonotonic(this.#clock.now());
|
|
45
|
+
const schedule = {
|
|
46
|
+
attempt,
|
|
47
|
+
retryClass,
|
|
48
|
+
notBefore: checkedDeadline(
|
|
49
|
+
at,
|
|
50
|
+
this.#backoff.delay(retryClass, attempt),
|
|
51
|
+
),
|
|
52
|
+
};
|
|
53
|
+
this.#scheduled = attempt;
|
|
54
|
+
return schedule;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
reset(): void {
|
|
58
|
+
this.#scheduled = 0;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
BridgeHelloRequest,
|
|
3
|
+
BridgeNegotiationReceipt,
|
|
4
|
+
DomainAuthorityDescriptor,
|
|
5
|
+
DomainCapabilityDescriptor,
|
|
6
|
+
DomainId,
|
|
7
|
+
} from "./generated/protocol.ts";
|
|
8
|
+
import {
|
|
9
|
+
parseBridgeHelloRequest,
|
|
10
|
+
parseBridgeNegotiationReceipt,
|
|
11
|
+
} from "./validation.ts";
|
|
12
|
+
|
|
13
|
+
export interface BridgeNegotiationAdapter {
|
|
14
|
+
negotiate(request: BridgeHelloRequest): Promise<unknown>;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export class BridgeSessionClient {
|
|
18
|
+
readonly #adapter: BridgeNegotiationAdapter;
|
|
19
|
+
|
|
20
|
+
constructor(adapter: BridgeNegotiationAdapter) {
|
|
21
|
+
this.#adapter = adapter;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
async connect(request: BridgeHelloRequest): Promise<BridgeSession> {
|
|
25
|
+
const checkedRequest = parseBridgeHelloRequest(request);
|
|
26
|
+
const receipt = parseBridgeNegotiationReceipt(
|
|
27
|
+
await this.#adapter.negotiate(checkedRequest),
|
|
28
|
+
checkedRequest,
|
|
29
|
+
);
|
|
30
|
+
return new BridgeSession(receipt);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export class BridgeSession {
|
|
35
|
+
readonly receipt: BridgeNegotiationReceipt;
|
|
36
|
+
readonly #capabilities: ReadonlyMap<DomainId, DomainCapabilityDescriptor>;
|
|
37
|
+
readonly #authorities: ReadonlyMap<DomainId, DomainAuthorityDescriptor>;
|
|
38
|
+
|
|
39
|
+
constructor(receipt: BridgeNegotiationReceipt) {
|
|
40
|
+
this.receipt = parseBridgeNegotiationReceipt(receipt);
|
|
41
|
+
this.#capabilities = new Map(
|
|
42
|
+
this.receipt.domainCapabilities.map((value) => [
|
|
43
|
+
value.domainId,
|
|
44
|
+
value,
|
|
45
|
+
]),
|
|
46
|
+
);
|
|
47
|
+
this.#authorities = new Map(
|
|
48
|
+
this.receipt.domainAuthorities.map((value) => [
|
|
49
|
+
value.domainId,
|
|
50
|
+
value,
|
|
51
|
+
]),
|
|
52
|
+
);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
capability(domainId: DomainId): DomainCapabilityDescriptor | undefined {
|
|
56
|
+
return this.#capabilities.get(domainId);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
authority(domainId: DomainId): DomainAuthorityDescriptor | undefined {
|
|
60
|
+
return this.#authorities.get(domainId);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
supports(domainId: DomainId, capability: string): boolean {
|
|
64
|
+
return this.capability(domainId)?.capabilities.includes(capability) ??
|
|
65
|
+
false;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
import {
|
|
2
|
+
CheckedSnapshotConnection,
|
|
3
|
+
type ConnectionFailure,
|
|
4
|
+
type ConnectionFailureReporter,
|
|
5
|
+
} from "@inflatable-cookie/longhorn/core";
|
|
6
|
+
|
|
7
|
+
import type {
|
|
8
|
+
BridgeEventEnvelope,
|
|
9
|
+
BridgeSnapshotEnvelope,
|
|
10
|
+
BridgeStreamDecision,
|
|
11
|
+
DomainId,
|
|
12
|
+
BridgeSessionId,
|
|
13
|
+
} from "../generated/protocol.ts";
|
|
14
|
+
import type { BridgeCodec } from "../validation/base.ts";
|
|
15
|
+
import {
|
|
16
|
+
parseBridgeEventEnvelope,
|
|
17
|
+
parseBridgeSnapshotEnvelope,
|
|
18
|
+
} from "../validation/streams.ts";
|
|
19
|
+
import { BridgeStreamTracker, newer } from "./tracker.ts";
|
|
20
|
+
import type { BridgeStreamSource } from "./source.ts";
|
|
21
|
+
|
|
22
|
+
export interface BridgeStreamConnectionOptions<Snapshot, Event> {
|
|
23
|
+
readonly sessionId: BridgeSessionId;
|
|
24
|
+
readonly domainId: DomainId;
|
|
25
|
+
readonly source: BridgeStreamSource;
|
|
26
|
+
readonly snapshot: BridgeCodec<Snapshot>;
|
|
27
|
+
readonly event: BridgeCodec<Event>;
|
|
28
|
+
readonly apply: (
|
|
29
|
+
current: Snapshot,
|
|
30
|
+
event: Event,
|
|
31
|
+
) => Snapshot;
|
|
32
|
+
readonly onSnapshot?: (
|
|
33
|
+
snapshot: BridgeSnapshotEnvelope<Snapshot>,
|
|
34
|
+
) => void;
|
|
35
|
+
readonly onFailure?: ConnectionFailureReporter;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export class BridgeStreamConnection<Snapshot> {
|
|
39
|
+
readonly ready: Promise<BridgeSnapshotEnvelope<Snapshot> | undefined>;
|
|
40
|
+
readonly #connection: CheckedSnapshotConnection<
|
|
41
|
+
BridgeSnapshotEnvelope<Snapshot>
|
|
42
|
+
>;
|
|
43
|
+
|
|
44
|
+
constructor(
|
|
45
|
+
connection: CheckedSnapshotConnection<
|
|
46
|
+
BridgeSnapshotEnvelope<Snapshot>
|
|
47
|
+
>,
|
|
48
|
+
) {
|
|
49
|
+
this.#connection = connection;
|
|
50
|
+
this.ready = connection.ready;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
current(): BridgeSnapshotEnvelope<Snapshot> | undefined {
|
|
54
|
+
return this.#connection.current();
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
failures(): readonly ConnectionFailure[] {
|
|
58
|
+
return this.#connection.failures();
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
dispose(): Promise<void> {
|
|
62
|
+
return this.#connection.dispose();
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export function connectBridgeStream<Snapshot, Event>(
|
|
67
|
+
options: BridgeStreamConnectionOptions<Snapshot, Event>,
|
|
68
|
+
): BridgeStreamConnection<Snapshot> {
|
|
69
|
+
const tracker = new BridgeStreamTracker(
|
|
70
|
+
options.sessionId,
|
|
71
|
+
options.domainId,
|
|
72
|
+
);
|
|
73
|
+
const connection = new CheckedSnapshotConnection<
|
|
74
|
+
BridgeSnapshotEnvelope<Snapshot>
|
|
75
|
+
>({
|
|
76
|
+
listen: (listener) => options.source.listen(listener),
|
|
77
|
+
loadSnapshot: () => options.source.loadSnapshot(),
|
|
78
|
+
validateSnapshot: (value) => {
|
|
79
|
+
const snapshot = parseBridgeSnapshotEnvelope(value, options.snapshot);
|
|
80
|
+
const decision = tracker.acceptSnapshot(snapshot.cursor);
|
|
81
|
+
if (
|
|
82
|
+
decision === "supersededSession" ||
|
|
83
|
+
decision === "wrongDomain"
|
|
84
|
+
) {
|
|
85
|
+
throw new BridgeStreamAuthorityError(decision);
|
|
86
|
+
}
|
|
87
|
+
return snapshot;
|
|
88
|
+
},
|
|
89
|
+
handleEvent: (value, current) => {
|
|
90
|
+
const event = parseBridgeEventEnvelope(value, options.event);
|
|
91
|
+
const decision = tracker.classifyEvent(event.cursor);
|
|
92
|
+
return eventAction(decision, current, event, options.apply);
|
|
93
|
+
},
|
|
94
|
+
isNewer: (candidate, current) =>
|
|
95
|
+
current === undefined || newer(candidate.cursor, current.cursor),
|
|
96
|
+
onSnapshot: options.onSnapshot,
|
|
97
|
+
onFailure: options.onFailure,
|
|
98
|
+
});
|
|
99
|
+
return new BridgeStreamConnection(connection);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
export class BridgeStreamAuthorityError extends Error {
|
|
103
|
+
readonly decision: "supersededSession" | "wrongDomain";
|
|
104
|
+
|
|
105
|
+
constructor(decision: "supersededSession" | "wrongDomain") {
|
|
106
|
+
super(`bridge stream snapshot rejected: ${decision}`);
|
|
107
|
+
this.name = "BridgeStreamAuthorityError";
|
|
108
|
+
this.decision = decision;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
function eventAction<Snapshot, Event>(
|
|
113
|
+
decision: BridgeStreamDecision,
|
|
114
|
+
current: BridgeSnapshotEnvelope<Snapshot> | undefined,
|
|
115
|
+
event: BridgeEventEnvelope<Event>,
|
|
116
|
+
apply: (current: Snapshot, event: Event) => Snapshot,
|
|
117
|
+
):
|
|
118
|
+
| { readonly kind: "ignore" }
|
|
119
|
+
| { readonly kind: "refresh" }
|
|
120
|
+
| {
|
|
121
|
+
readonly kind: "snapshot";
|
|
122
|
+
readonly value: BridgeSnapshotEnvelope<Snapshot>;
|
|
123
|
+
} {
|
|
124
|
+
switch (decision) {
|
|
125
|
+
case "apply":
|
|
126
|
+
if (current === undefined) {
|
|
127
|
+
return { kind: "refresh" };
|
|
128
|
+
}
|
|
129
|
+
return {
|
|
130
|
+
kind: "snapshot",
|
|
131
|
+
value: {
|
|
132
|
+
cursor: event.cursor,
|
|
133
|
+
payload: apply(current.payload, event.payload),
|
|
134
|
+
},
|
|
135
|
+
};
|
|
136
|
+
case "resnapshotGap":
|
|
137
|
+
case "resnapshotNewEpoch":
|
|
138
|
+
case "resnapshotRequired":
|
|
139
|
+
return { kind: "refresh" };
|
|
140
|
+
case "ignoreDuplicate":
|
|
141
|
+
case "ignoreStale":
|
|
142
|
+
case "ignoreSupersededSession":
|
|
143
|
+
case "ignoreWrongDomain":
|
|
144
|
+
return { kind: "ignore" };
|
|
145
|
+
}
|
|
146
|
+
}
|