@inflatable-cookie/longhorn 0.5.0 → 0.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -2
- package/package.json +1 -7
- package/src/agent-control/generated/protocol.ts +228 -0
- package/src/agent-control/index.ts +1 -5
- package/src/agent-control/inject.ts +4 -1
- package/src/agent-control/selection.ts +42 -25
- package/src/agent-control/shim.ts +428 -198
- package/src/bridge/direct.ts +1 -1
- package/src/bridge/generated/fields.ts +4 -3
- package/src/bridge/generated/protocol.ts +62 -7
- package/src/bridge/generated/variant-fields.ts +1 -1
- package/src/bridge/job.ts +49 -12
- package/src/bridge/loopback.ts +7 -25
- package/src/bridge/operation.ts +13 -43
- package/src/bridge/router.ts +10 -23
- package/src/bridge/runtime/connection.ts +78 -109
- package/src/bridge/runtime/policy.ts +8 -28
- package/src/bridge/runtime/retry.ts +6 -18
- package/src/bridge/session.ts +4 -14
- package/src/bridge/stream/connection.ts +24 -55
- package/src/bridge/stream/source.ts +2 -6
- package/src/bridge/stream/tracker.ts +12 -12
- package/src/bridge/supervision.ts +104 -97
- package/src/bridge/validation/base.ts +21 -63
- package/src/bridge/validation/jobs.ts +11 -25
- package/src/bridge/validation/negotiation.ts +28 -43
- package/src/bridge/validation/operations.ts +29 -65
- package/src/bridge/validation/streams.ts +3 -10
- package/src/commands/client.ts +37 -28
- package/src/commands/controller.ts +119 -96
- package/src/commands/generated/fields.ts +7 -6
- package/src/commands/generated/protocol.ts +28 -1
- package/src/commands/generated/variant-fields.ts +1 -1
- package/src/commands/keyboard.ts +94 -100
- package/src/commands/physical-code.ts +11 -0
- package/src/commands/ports.ts +8 -13
- package/src/commands/projectors.ts +100 -75
- package/src/commands/validation.ts +434 -241
- package/src/config/client.ts +9 -22
- package/src/config/generated/base.ts +22 -6
- package/src/config/generated/fields.ts +4 -3
- package/src/config/generated/label-template.ts +1 -1
- package/src/config/generated/labels.ts +1 -1
- package/src/config/generated/protocol.ts +1 -1
- package/src/config/generated/restore.ts +35 -12
- package/src/config/generated/variant-fields.ts +1 -1
- package/src/config/names.ts +6 -12
- package/src/config/validation/commands.ts +11 -25
- package/src/config/validation/outcomes.ts +163 -70
- package/src/config/validation/primitives.ts +35 -80
- package/src/config/validation/projection.ts +100 -128
- package/src/core/connection.ts +187 -83
- package/src/core/controller-lifecycle.ts +144 -0
- package/src/core/generated/identifiers.ts +4 -0
- package/src/core/identifiers.ts +40 -0
- package/src/core/index.ts +10 -1
- package/src/core/json-value.ts +58 -0
- package/src/core/listeners.ts +35 -0
- package/src/core/record.ts +80 -0
- package/src/core/transport.ts +9 -11
- package/src/core/wire-copy.ts +7 -0
- package/src/history/client.ts +11 -13
- package/src/history/controller.ts +106 -108
- package/src/history/direct.ts +1 -3
- package/src/history/generated/fields.ts +4 -3
- package/src/history/generated/protocol.ts +8 -5
- package/src/history/generated/variant-fields.ts +1 -1
- package/src/history/ports.ts +7 -4
- package/src/history/serialized.ts +9 -13
- package/src/history/validation.ts +69 -75
- package/src/history-tree/client.ts +89 -11
- package/src/history-tree/controller.ts +380 -96
- package/src/history-tree/direct.ts +3 -1
- package/src/history-tree/generated/fields.ts +5 -3
- package/src/history-tree/generated/protocol.ts +43 -7
- package/src/history-tree/generated/variant-fields.ts +7 -1
- package/src/history-tree/ports.ts +12 -5
- package/src/history-tree/serialized.ts +39 -11
- package/src/history-tree/validation.ts +286 -78
- package/src/layout/generated/protocol.ts +4 -3
- package/src/layout/generated/variant-fields.ts +47 -0
- package/src/layout/index.ts +1 -1
- package/src/layout/ratio.ts +10 -9
- package/src/layout/validation.ts +98 -32
- package/src/layout/visibility.ts +1 -2
- package/src/licence/client.ts +48 -7
- package/src/licence/controller.ts +127 -70
- package/src/licence/direct.ts +3 -1
- package/src/licence/generated/fields.ts +4 -3
- package/src/licence/generated/key-conformance.json +28 -0
- package/src/licence/generated/protocol.ts +9 -5
- package/src/licence/generated/variant-fields.ts +1 -1
- package/src/licence/key.ts +42 -18
- package/src/licence/ports.ts +10 -3
- package/src/licence/serialized.ts +32 -9
- package/src/licence/validation.ts +134 -43
- package/src/native-content/client.ts +207 -234
- package/src/native-content/generated/fields.ts +6 -6
- package/src/native-content/generated/protocol.ts +16 -5
- package/src/native-content/generated/variant-fields.ts +1 -1
- package/src/native-content/ports.ts +11 -16
- package/src/native-content/serialized.ts +12 -19
- package/src/native-content/validation/base.ts +57 -80
- package/src/native-content/validation/messages.ts +24 -35
- package/src/native-content/validation/state.ts +31 -33
- package/src/notifications/client.ts +85 -51
- package/src/notifications/controller.ts +400 -145
- package/src/notifications/direct.ts +3 -1
- package/src/notifications/generated/fields.ts +4 -3
- package/src/notifications/generated/labels.ts +1 -1
- package/src/notifications/generated/protocol.ts +5 -1
- package/src/notifications/generated/variant-fields.ts +1 -1
- package/src/notifications/ports.ts +9 -4
- package/src/notifications/serialized.ts +11 -9
- package/src/notifications/tone.ts +1 -1
- package/src/notifications/validation.ts +165 -71
- package/src/operation/bridge.ts +187 -60
- package/src/operation/client.ts +48 -62
- package/src/operation/controller.ts +67 -97
- package/src/operation/direct.ts +1 -3
- package/src/operation/generated/fields.ts +4 -3
- package/src/operation/generated/labels.ts +1 -1
- package/src/operation/generated/protocol.ts +5 -1
- package/src/operation/generated/variant-fields.ts +1 -1
- package/src/operation/ports.ts +12 -10
- package/src/operation/serialized.ts +8 -9
- package/src/operation/validation.ts +189 -104
- package/src/settings/client.ts +11 -44
- package/src/settings/connection.ts +35 -53
- package/src/settings/generated/fields.ts +5 -4
- package/src/settings/generated/protocol.ts +14 -5
- package/src/settings/generated/variant-fields.ts +1 -1
- package/src/settings/names.ts +2 -4
- package/src/settings/registry.ts +23 -25
- package/src/settings/validation/authority.ts +21 -19
- package/src/settings/validation/command.ts +22 -41
- package/src/settings/validation/error.ts +2 -8
- package/src/settings/validation/primitives.ts +46 -97
- package/src/settings/validation/projection.ts +8 -24
- package/src/settings/validation/registry.ts +33 -79
- package/src/surface-transfer/client.ts +6 -18
- package/src/surface-transfer/generated/fields.ts +3 -3
- package/src/surface-transfer/generated/protocol.ts +3 -3
- package/src/surface-transfer/generated/variant-fields.ts +1 -1
- package/src/surface-transfer/validation.ts +91 -86
- package/src/surfaces/client.ts +33 -55
- package/src/surfaces/generated/fields.ts +3 -3
- package/src/surfaces/generated/protocol.ts +1 -1
- package/src/surfaces/generated/variant-fields.ts +1 -1
- package/src/surfaces/validation.ts +227 -99
- package/src/transfer/client.ts +26 -60
- package/src/transfer/connection.ts +29 -51
- package/src/transfer/generated/fields.ts +7 -6
- package/src/transfer/generated/protocol.ts +13 -5
- package/src/transfer/generated/variant-fields.ts +1 -1
- package/src/transfer/index.ts +8 -1
- package/src/transfer/payload.ts +20 -35
- package/src/transfer/validation.ts +169 -119
- package/src/update/client.ts +86 -9
- package/src/update/controller.ts +264 -116
- package/src/update/direct.ts +3 -1
- package/src/update/generated/fields.ts +5 -4
- package/src/update/generated/protocol.ts +25 -9
- package/src/update/generated/variant-fields.ts +4 -2
- package/src/update/ports.ts +14 -6
- package/src/update/serialized.ts +36 -10
- package/src/update/validation.ts +118 -48
package/src/operation/bridge.ts
CHANGED
|
@@ -1,17 +1,26 @@
|
|
|
1
1
|
import {
|
|
2
2
|
BridgeJobTracker,
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
3
|
+
bridgeCodec,
|
|
4
|
+
parseBridgeCommandEnvelope,
|
|
5
|
+
parseBridgeCommandReply,
|
|
6
|
+
parseBridgeJobTerminalEvent,
|
|
7
|
+
parseBridgeQueryEnvelope,
|
|
8
|
+
parseBridgeQueryReply,
|
|
9
|
+
} from "@inflatable-cookie/longhorn/bridge";
|
|
10
|
+
import type {
|
|
11
|
+
BridgeCommandEnvelope,
|
|
12
|
+
BridgeCommandReply,
|
|
13
|
+
BridgeDomainClient,
|
|
14
|
+
BridgeFailure,
|
|
15
|
+
BridgeJobId,
|
|
16
|
+
BridgeJobTerminalEvent,
|
|
17
|
+
BridgeOperationDescriptor,
|
|
18
|
+
BridgeProgressEvent,
|
|
19
|
+
BridgeQueryEnvelope,
|
|
20
|
+
BridgeQueryReply,
|
|
21
|
+
BridgeRequestContext,
|
|
22
|
+
BridgeRequestId,
|
|
23
|
+
DomainId,
|
|
15
24
|
} from "@inflatable-cookie/longhorn/bridge";
|
|
16
25
|
|
|
17
26
|
import {
|
|
@@ -44,6 +53,40 @@ export interface BridgeOperationPortOptions {
|
|
|
44
53
|
readonly nextOperationRequestId: () => string;
|
|
45
54
|
}
|
|
46
55
|
|
|
56
|
+
export type BridgeOperationFailure =
|
|
57
|
+
| {
|
|
58
|
+
readonly kind: "rejected" | "indeterminate";
|
|
59
|
+
readonly requestId: BridgeRequestId;
|
|
60
|
+
readonly failure: BridgeFailure<null>;
|
|
61
|
+
}
|
|
62
|
+
| {
|
|
63
|
+
readonly kind: "miscorrelated";
|
|
64
|
+
readonly requestId: BridgeRequestId;
|
|
65
|
+
readonly receivedRequestId: BridgeRequestId;
|
|
66
|
+
}
|
|
67
|
+
| {
|
|
68
|
+
readonly kind: "failed";
|
|
69
|
+
readonly requestId: BridgeRequestId;
|
|
70
|
+
readonly jobId: BridgeJobId;
|
|
71
|
+
readonly failure: BridgeFailure<null>;
|
|
72
|
+
}
|
|
73
|
+
| {
|
|
74
|
+
readonly kind: "cancelled";
|
|
75
|
+
readonly requestId: BridgeRequestId;
|
|
76
|
+
readonly jobId: BridgeJobId;
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
export class BridgeOperationError extends Error {
|
|
80
|
+
constructor(readonly outcome: BridgeOperationFailure) {
|
|
81
|
+
super(
|
|
82
|
+
"failure" in outcome
|
|
83
|
+
? `operation bridge ${outcome.kind}: ${outcome.failure.message}`
|
|
84
|
+
: `operation bridge ${outcome.kind}`,
|
|
85
|
+
);
|
|
86
|
+
this.name = "BridgeOperationError";
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
47
90
|
export function createBridgeOperationPort(options: BridgeOperationPortOptions): OperationPort {
|
|
48
91
|
return {
|
|
49
92
|
snapshot: (query) => executeQuery(options, query),
|
|
@@ -57,84 +100,150 @@ async function executeQuery(options: BridgeOperationPortOptions, query: Operatio
|
|
|
57
100
|
const context = nextContext(options);
|
|
58
101
|
const descriptor = queryDescriptor(options.domainId);
|
|
59
102
|
const reply = await options.client.execute(descriptor, { context, payload: query });
|
|
60
|
-
|
|
61
|
-
throw new Error("operation bridge query was rejected or miscorrelated");
|
|
62
|
-
}
|
|
63
|
-
return reply.outcome.success;
|
|
103
|
+
return queryResult(context.requestId, reply);
|
|
64
104
|
}
|
|
65
105
|
|
|
66
|
-
async function executeCommand(
|
|
106
|
+
async function executeCommand(
|
|
107
|
+
options: BridgeOperationPortOptions,
|
|
108
|
+
command: OperationMutationCommand,
|
|
109
|
+
) {
|
|
67
110
|
const context = nextContext(options);
|
|
68
111
|
const descriptor = commandDescriptor(options.domainId);
|
|
69
112
|
const reply = await options.client.execute(descriptor, {
|
|
70
113
|
context,
|
|
71
114
|
authorityEpoch: options.authorityEpoch,
|
|
72
|
-
expectedRevision:
|
|
73
|
-
|
|
74
|
-
|
|
115
|
+
expectedRevision:
|
|
116
|
+
command.kind === "register" ||
|
|
117
|
+
command.kind === "changeRetention" ||
|
|
118
|
+
command.kind === "teardown"
|
|
119
|
+
? command.expectedCatalogueRevision
|
|
120
|
+
: null,
|
|
75
121
|
idempotencyKey: context.requestId,
|
|
76
122
|
payload: command,
|
|
77
123
|
});
|
|
78
|
-
|
|
79
|
-
|
|
124
|
+
const { requestId } = context;
|
|
125
|
+
checkCorrelation(requestId, reply.requestId);
|
|
126
|
+
const { outcome } = reply;
|
|
127
|
+
if ("rejected" in outcome) {
|
|
128
|
+
throw new BridgeOperationError({ kind: "rejected", requestId, failure: outcome.rejected });
|
|
80
129
|
}
|
|
81
|
-
|
|
130
|
+
if ("indeterminate" in outcome) {
|
|
131
|
+
throw new BridgeOperationError({
|
|
132
|
+
kind: "indeterminate",
|
|
133
|
+
requestId,
|
|
134
|
+
failure: outcome.indeterminate,
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
return outcome.applied;
|
|
82
138
|
}
|
|
83
139
|
|
|
84
|
-
async function executeCancellation(
|
|
140
|
+
async function executeCancellation(
|
|
141
|
+
options: BridgeOperationPortOptions,
|
|
142
|
+
command: OperationCancellationCommand,
|
|
143
|
+
) {
|
|
85
144
|
const context = nextContext(options);
|
|
86
145
|
const descriptor = cancellationDescriptor(options.domainId);
|
|
87
146
|
const reply = await options.client.execute(descriptor, { context, payload: command });
|
|
88
|
-
|
|
89
|
-
|
|
147
|
+
return queryResult(context.requestId, reply);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
function queryResult<Result>(requestId: BridgeRequestId, reply: BridgeQueryReply<Result, null>) {
|
|
151
|
+
checkCorrelation(requestId, reply.requestId);
|
|
152
|
+
if ("rejected" in reply.outcome) {
|
|
153
|
+
throw new BridgeOperationError({
|
|
154
|
+
kind: "rejected",
|
|
155
|
+
requestId,
|
|
156
|
+
failure: reply.outcome.rejected,
|
|
157
|
+
});
|
|
90
158
|
}
|
|
91
159
|
return reply.outcome.success;
|
|
92
160
|
}
|
|
93
161
|
|
|
162
|
+
function checkCorrelation(requestId: BridgeRequestId, receivedRequestId: BridgeRequestId): void {
|
|
163
|
+
if (receivedRequestId !== requestId) {
|
|
164
|
+
throw new BridgeOperationError({ kind: "miscorrelated", requestId, receivedRequestId });
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
|
|
94
168
|
function nextContext(options: BridgeOperationPortOptions): BridgeRequestContext {
|
|
95
|
-
return {
|
|
169
|
+
return {
|
|
170
|
+
requestId: options.nextBridgeRequestId(),
|
|
171
|
+
sessionId: options.sessionId,
|
|
172
|
+
domainId: options.domainId,
|
|
173
|
+
};
|
|
96
174
|
}
|
|
97
175
|
|
|
98
|
-
function codec<Value>(assertValue: (value: unknown) =>
|
|
99
|
-
return
|
|
176
|
+
function codec<Value>(assertValue: (value: unknown) => asserts value is Value) {
|
|
177
|
+
return bridgeCodec((value) => {
|
|
178
|
+
assertValue(value);
|
|
179
|
+
return value;
|
|
180
|
+
});
|
|
100
181
|
}
|
|
101
182
|
|
|
102
|
-
|
|
183
|
+
const noDetails = bridgeCodec((value): null => {
|
|
184
|
+
if (value !== null) {
|
|
185
|
+
throw new Error("operation bridge failure details must be null");
|
|
186
|
+
}
|
|
187
|
+
return null;
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
function queryDescriptor(
|
|
191
|
+
domainId: DomainId,
|
|
192
|
+
): BridgeOperationDescriptor<
|
|
193
|
+
BridgeQueryEnvelope<OperationSnapshotQuery>,
|
|
194
|
+
BridgeQueryReply<OperationSnapshotResponse, null>
|
|
195
|
+
> {
|
|
103
196
|
return {
|
|
104
|
-
route: "longhorn.operation.snapshot",
|
|
197
|
+
route: "longhorn.operation.snapshot",
|
|
198
|
+
domainId,
|
|
199
|
+
kind: "query",
|
|
105
200
|
requiredCapability: "longhorn.operation.read",
|
|
106
|
-
request:
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
if ("success" in outcome) assertValidOperationSnapshotResponse(outcome.success);
|
|
113
|
-
}),
|
|
201
|
+
request: bridgeCodec((value) =>
|
|
202
|
+
parseBridgeQueryEnvelope(value, codec(assertValidOperationSnapshotQuery)),
|
|
203
|
+
),
|
|
204
|
+
reply: bridgeCodec((value) =>
|
|
205
|
+
parseBridgeQueryReply(value, codec(assertValidOperationSnapshotResponse), noDetails),
|
|
206
|
+
),
|
|
114
207
|
};
|
|
115
208
|
}
|
|
116
209
|
|
|
117
|
-
function commandDescriptor(
|
|
210
|
+
function commandDescriptor(
|
|
211
|
+
domainId: DomainId,
|
|
212
|
+
): BridgeOperationDescriptor<
|
|
213
|
+
BridgeCommandEnvelope<OperationMutationCommand>,
|
|
214
|
+
BridgeCommandReply<OperationMutationResult, null>
|
|
215
|
+
> {
|
|
118
216
|
return {
|
|
119
|
-
route: "longhorn.operation.mutate",
|
|
217
|
+
route: "longhorn.operation.mutate",
|
|
218
|
+
domainId,
|
|
219
|
+
kind: "command",
|
|
120
220
|
requiredCapability: "longhorn.operation.manage",
|
|
121
|
-
request:
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
221
|
+
request: bridgeCodec((value) =>
|
|
222
|
+
parseBridgeCommandEnvelope(value, codec(assertValidOperationMutationCommand)),
|
|
223
|
+
),
|
|
224
|
+
reply: bridgeCodec((value) =>
|
|
225
|
+
parseBridgeCommandReply(value, codec(assertValidOperationMutationResult), noDetails),
|
|
226
|
+
),
|
|
126
227
|
};
|
|
127
228
|
}
|
|
128
229
|
|
|
129
|
-
function cancellationDescriptor(
|
|
230
|
+
function cancellationDescriptor(
|
|
231
|
+
domainId: DomainId,
|
|
232
|
+
): BridgeOperationDescriptor<
|
|
233
|
+
BridgeQueryEnvelope<OperationCancellationCommand>,
|
|
234
|
+
BridgeQueryReply<OperationCancellationResult, null>
|
|
235
|
+
> {
|
|
130
236
|
return {
|
|
131
|
-
route: "longhorn.operation.cancel",
|
|
237
|
+
route: "longhorn.operation.cancel",
|
|
238
|
+
domainId,
|
|
239
|
+
kind: "cancellation",
|
|
132
240
|
requiredCapability: "longhorn.operation.cancel",
|
|
133
|
-
request:
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
241
|
+
request: bridgeCodec((value) =>
|
|
242
|
+
parseBridgeQueryEnvelope(value, codec(assertValidOperationCancellationCommand)),
|
|
243
|
+
),
|
|
244
|
+
reply: bridgeCodec((value) =>
|
|
245
|
+
parseBridgeQueryReply(value, codec(assertValidOperationCancellationResult), noDetails),
|
|
246
|
+
),
|
|
138
247
|
};
|
|
139
248
|
}
|
|
140
249
|
|
|
@@ -153,16 +262,34 @@ export class BridgeOperationJobCorrelation {
|
|
|
153
262
|
this.#operationId = correlation.operationId;
|
|
154
263
|
}
|
|
155
264
|
|
|
156
|
-
acceptProgress(
|
|
157
|
-
|
|
265
|
+
acceptProgress(
|
|
266
|
+
event: BridgeProgressEvent<OperationChangedEvent>,
|
|
267
|
+
): OperationChangedEvent | undefined {
|
|
268
|
+
if (this.#tracker.classifyProgress(event) !== "accept") {
|
|
269
|
+
return undefined;
|
|
270
|
+
}
|
|
158
271
|
assertValidOperationChangedEvent(event.progress);
|
|
159
272
|
return event.progress.operationId === this.#operationId ? event.progress : undefined;
|
|
160
273
|
}
|
|
161
274
|
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
275
|
+
/** Returns a correlated success or throws for a correlated failed or cancelled job. */
|
|
276
|
+
acceptTerminal(
|
|
277
|
+
event: BridgeJobTerminalEvent<OperationChangedEvent, null>,
|
|
278
|
+
): OperationChangedEvent | undefined {
|
|
279
|
+
if (this.#tracker.classifyTerminal(event) !== "accept") {
|
|
280
|
+
return undefined;
|
|
281
|
+
}
|
|
282
|
+
const { requestId, jobId, outcome } = parseBridgeJobTerminalEvent(
|
|
283
|
+
event,
|
|
284
|
+
codec(assertValidOperationChangedEvent),
|
|
285
|
+
noDetails,
|
|
286
|
+
);
|
|
287
|
+
if (outcome === "cancelled") {
|
|
288
|
+
throw new BridgeOperationError({ kind: "cancelled", requestId, jobId });
|
|
289
|
+
}
|
|
290
|
+
if ("failed" in outcome) {
|
|
291
|
+
throw new BridgeOperationError({ kind: "failed", requestId, jobId, failure: outcome.failed });
|
|
292
|
+
}
|
|
293
|
+
return outcome.succeeded.operationId === this.#operationId ? outcome.succeeded : undefined;
|
|
167
294
|
}
|
|
168
295
|
}
|
package/src/operation/client.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
import { CheckedSnapshotConnection, CheckedSubscription } from "@inflatable-cookie/longhorn/core";
|
|
2
|
+
import type {
|
|
3
|
+
ConnectionFailure,
|
|
4
|
+
ConnectionFailureReporter,
|
|
5
5
|
} from "@inflatable-cookie/longhorn/core";
|
|
6
6
|
|
|
7
7
|
import {
|
|
@@ -10,24 +10,24 @@ import {
|
|
|
10
10
|
assertValidOperationChangedEvent,
|
|
11
11
|
assertValidOperationMutationCommand,
|
|
12
12
|
assertValidOperationMutationResult,
|
|
13
|
+
assertValidOperationSnapshot,
|
|
13
14
|
assertValidOperationSnapshotResponse,
|
|
14
15
|
} from "./validation.ts";
|
|
15
|
-
import {
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
type OperationSnapshotResponse,
|
|
16
|
+
import { OPERATION_PROTOCOL_VERSION } from "./generated/protocol.ts";
|
|
17
|
+
import type {
|
|
18
|
+
OperationCancellationCommand,
|
|
19
|
+
OperationCancellationResult,
|
|
20
|
+
OperationMutationCommand,
|
|
21
|
+
OperationMutationResult,
|
|
22
|
+
OperationSnapshot,
|
|
23
|
+
OperationSnapshotResponse,
|
|
24
24
|
} from "./generated/protocol.ts";
|
|
25
25
|
import type { OperationPort } from "./ports.ts";
|
|
26
26
|
|
|
27
27
|
export interface OperationSubscription {
|
|
28
28
|
readonly ready: Promise<void>;
|
|
29
29
|
current(): OperationSnapshot | undefined;
|
|
30
|
-
failures():
|
|
30
|
+
failures(): ReadonlyArray<ConnectionFailure>;
|
|
31
31
|
dispose(): Promise<void>;
|
|
32
32
|
}
|
|
33
33
|
|
|
@@ -73,63 +73,43 @@ export class OperationClient {
|
|
|
73
73
|
listener: (snapshot: OperationSnapshot) => void,
|
|
74
74
|
onFailure?: ConnectionFailureReporter,
|
|
75
75
|
): OperationSubscription {
|
|
76
|
-
|
|
76
|
+
const port = this.#port;
|
|
77
|
+
return new CheckedSubscription(
|
|
78
|
+
new CheckedSnapshotConnection({
|
|
79
|
+
listen: async (receive) => (port.listen === undefined ? () => {} : port.listen(receive)),
|
|
80
|
+
loadSnapshot: async () => (await this.snapshot()).snapshot,
|
|
81
|
+
validateSnapshot: parseSnapshot,
|
|
82
|
+
handleEvent: operationEventAction,
|
|
83
|
+
isNewer: isNewerOperationSnapshot,
|
|
84
|
+
onSnapshot: listener,
|
|
85
|
+
onFailure,
|
|
86
|
+
}),
|
|
87
|
+
() => {},
|
|
88
|
+
);
|
|
77
89
|
}
|
|
78
90
|
}
|
|
79
91
|
|
|
80
92
|
export class OperationResponseCorrelationError extends Error {
|
|
81
|
-
constructor(
|
|
82
|
-
|
|
93
|
+
constructor(
|
|
94
|
+
readonly expectedRequestId: string,
|
|
95
|
+
readonly receivedRequestId: string,
|
|
96
|
+
) {
|
|
97
|
+
super(
|
|
98
|
+
`operation response correlation mismatch: expected ${expectedRequestId}; received ${receivedRequestId}`,
|
|
99
|
+
);
|
|
83
100
|
this.name = "OperationResponseCorrelationError";
|
|
84
101
|
}
|
|
85
102
|
}
|
|
86
103
|
|
|
87
104
|
function assertResponseCorrelation(received: string, expected: string): void {
|
|
88
|
-
if (received !== expected)
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
class CheckedOperationSubscription implements OperationSubscription {
|
|
92
|
-
readonly ready: Promise<void>;
|
|
93
|
-
readonly #connection: CheckedSnapshotConnection<OperationSnapshot>;
|
|
94
|
-
|
|
95
|
-
constructor(
|
|
96
|
-
client: OperationClient,
|
|
97
|
-
port: OperationPort,
|
|
98
|
-
listener: (snapshot: OperationSnapshot) => void,
|
|
99
|
-
onFailure?: ConnectionFailureReporter,
|
|
100
|
-
) {
|
|
101
|
-
this.#connection = new CheckedSnapshotConnection({
|
|
102
|
-
listen: async (receive) => {
|
|
103
|
-
if (port.listen === undefined) return () => {};
|
|
104
|
-
return port.listen(receive);
|
|
105
|
-
},
|
|
106
|
-
loadSnapshot: async () => (await client.snapshot()).snapshot,
|
|
107
|
-
validateSnapshot: parseSnapshot,
|
|
108
|
-
handleEvent: operationEventAction,
|
|
109
|
-
isNewer: isNewerOperationSnapshot,
|
|
110
|
-
onSnapshot: listener,
|
|
111
|
-
onFailure,
|
|
112
|
-
});
|
|
113
|
-
this.ready = this.#connection.ready.then(() => undefined);
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
current(): OperationSnapshot | undefined {
|
|
117
|
-
return this.#connection.current();
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
failures(): readonly ConnectionFailure[] {
|
|
121
|
-
return this.#connection.failures();
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
dispose(): Promise<void> {
|
|
125
|
-
return this.#connection.dispose();
|
|
105
|
+
if (received !== expected) {
|
|
106
|
+
throw new OperationResponseCorrelationError(expected, received);
|
|
126
107
|
}
|
|
127
108
|
}
|
|
128
109
|
|
|
129
110
|
function parseSnapshot(value: unknown): OperationSnapshot {
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
return value as OperationSnapshot;
|
|
111
|
+
assertValidOperationSnapshot(value);
|
|
112
|
+
return value;
|
|
133
113
|
}
|
|
134
114
|
|
|
135
115
|
export function operationEventAction(
|
|
@@ -137,8 +117,10 @@ export function operationEventAction(
|
|
|
137
117
|
current: OperationSnapshot | undefined,
|
|
138
118
|
): { kind: "ignore" } | { kind: "refresh" } {
|
|
139
119
|
assertValidOperationChangedEvent(value);
|
|
140
|
-
const event = value
|
|
141
|
-
if (current === undefined)
|
|
120
|
+
const event = value;
|
|
121
|
+
if (current === undefined) {
|
|
122
|
+
return { kind: "refresh" };
|
|
123
|
+
}
|
|
142
124
|
if (event.authority.authorityId !== current.authority.authorityId) {
|
|
143
125
|
return { kind: "ignore" };
|
|
144
126
|
}
|
|
@@ -158,8 +140,12 @@ export function isNewerOperationSnapshot(
|
|
|
158
140
|
candidate: OperationSnapshot,
|
|
159
141
|
current: OperationSnapshot | undefined,
|
|
160
142
|
): boolean {
|
|
161
|
-
if (current === undefined)
|
|
162
|
-
|
|
143
|
+
if (current === undefined) {
|
|
144
|
+
return true;
|
|
145
|
+
}
|
|
146
|
+
if (candidate.authority.authorityId !== current.authority.authorityId) {
|
|
147
|
+
return false;
|
|
148
|
+
}
|
|
163
149
|
if (candidate.authority.authorityEpoch !== current.authority.authorityEpoch) {
|
|
164
150
|
return candidate.authority.authorityEpoch > current.authority.authorityEpoch;
|
|
165
151
|
}
|