@inflatable-cookie/longhorn 0.4.3 → 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.
- package/package.json +1 -1
- package/src/agent-control/inject.ts +4 -1
- package/src/agent-control/selection.ts +35 -20
- package/src/agent-control/shim.ts +302 -144
- package/src/bridge/direct.ts +1 -1
- package/src/bridge/generated/fields.ts +3 -2
- package/src/bridge/generated/protocol.ts +7 -3
- package/src/bridge/job.ts +4 -14
- 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 +77 -105
- 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 +16 -38
- package/src/bridge/stream/source.ts +2 -6
- package/src/bridge/stream/tracker.ts +7 -12
- package/src/bridge/supervision.ts +11 -26
- package/src/bridge/validation/base.ts +17 -39
- package/src/bridge/validation/jobs.ts +11 -25
- package/src/bridge/validation/negotiation.ts +27 -37
- package/src/bridge/validation/operations.ts +28 -52
- package/src/bridge/validation/streams.ts +3 -10
- package/src/commands/client.ts +21 -26
- package/src/commands/controller.ts +32 -47
- package/src/commands/generated/fields.ts +3 -2
- package/src/commands/generated/protocol.ts +4 -0
- package/src/commands/keyboard.ts +92 -89
- package/src/commands/ports.ts +6 -12
- package/src/commands/projectors.ts +50 -48
- package/src/commands/validation.ts +70 -97
- package/src/config/client.ts +6 -19
- package/src/config/generated/fields.ts +2 -2
- package/src/config/names.ts +6 -12
- package/src/config/validation/commands.ts +5 -17
- package/src/config/validation/outcomes.ts +31 -39
- package/src/config/validation/primitives.ts +36 -53
- package/src/config/validation/projection.ts +66 -94
- package/src/core/connection.ts +70 -61
- package/src/core/record.ts +4 -0
- package/src/core/transport.ts +4 -15
- package/src/core/wire-copy.ts +6 -0
- package/src/history/client.ts +6 -12
- package/src/history/controller.ts +46 -46
- package/src/history/direct.ts +1 -3
- package/src/history/generated/fields.ts +2 -2
- package/src/history/ports.ts +4 -6
- package/src/history/serialized.ts +9 -13
- package/src/history/validation.ts +71 -55
- package/src/history-tree/client.ts +87 -11
- package/src/history-tree/controller.ts +313 -70
- package/src/history-tree/direct.ts +3 -1
- package/src/history-tree/generated/fields.ts +2 -2
- package/src/history-tree/ports.ts +3 -1
- package/src/history-tree/serialized.ts +39 -11
- package/src/history-tree/validation.ts +262 -63
- package/src/layout/generated/protocol.ts +2 -2
- package/src/layout/ratio.ts +2 -8
- package/src/layout/validation.ts +10 -19
- package/src/layout/visibility.ts +1 -2
- package/src/licence/client.ts +44 -7
- package/src/licence/controller.ts +76 -40
- package/src/licence/direct.ts +3 -1
- package/src/licence/generated/fields.ts +2 -2
- package/src/licence/generated/key-conformance.json +28 -0
- package/src/licence/generated/protocol.ts +1 -1
- package/src/licence/key.ts +40 -12
- package/src/licence/ports.ts +3 -1
- package/src/licence/serialized.ts +32 -9
- package/src/licence/validation.ts +120 -36
- package/src/native-content/client.ts +178 -236
- package/src/native-content/generated/fields.ts +2 -2
- package/src/native-content/ports.ts +9 -15
- package/src/native-content/serialized.ts +12 -19
- package/src/native-content/validation/base.ts +57 -50
- package/src/native-content/validation/messages.ts +21 -35
- package/src/native-content/validation/state.ts +31 -33
- package/src/notifications/client.ts +87 -31
- package/src/notifications/controller.ts +215 -61
- package/src/notifications/direct.ts +3 -1
- package/src/notifications/generated/fields.ts +2 -2
- package/src/notifications/ports.ts +7 -3
- package/src/notifications/serialized.ts +11 -9
- package/src/notifications/validation.ts +147 -43
- package/src/operation/bridge.ts +117 -50
- package/src/operation/client.ts +38 -24
- package/src/operation/controller.ts +33 -42
- package/src/operation/direct.ts +1 -3
- package/src/operation/generated/fields.ts +2 -2
- package/src/operation/ports.ts +10 -9
- package/src/operation/serialized.ts +8 -9
- package/src/operation/validation.ts +152 -65
- package/src/settings/client.ts +11 -44
- package/src/settings/connection.ts +26 -40
- package/src/settings/generated/fields.ts +3 -3
- package/src/settings/generated/protocol.ts +9 -4
- package/src/settings/names.ts +2 -4
- package/src/settings/registry.ts +23 -25
- package/src/settings/validation/authority.ts +19 -17
- package/src/settings/validation/command.ts +17 -37
- package/src/settings/validation/error.ts +2 -8
- package/src/settings/validation/primitives.ts +26 -42
- package/src/settings/validation/projection.ts +8 -24
- package/src/settings/validation/registry.ts +25 -73
- package/src/surface-transfer/client.ts +6 -18
- package/src/surface-transfer/generated/fields.ts +2 -2
- package/src/surface-transfer/validation.ts +31 -49
- package/src/surfaces/client.ts +12 -22
- package/src/surfaces/generated/fields.ts +2 -2
- package/src/surfaces/validation.ts +40 -49
- package/src/transfer/client.ts +26 -60
- package/src/transfer/connection.ts +12 -20
- package/src/transfer/generated/fields.ts +5 -5
- package/src/transfer/generated/protocol.ts +4 -4
- package/src/transfer/payload.ts +13 -31
- package/src/transfer/validation.ts +39 -75
- package/src/update/client.ts +59 -9
- package/src/update/controller.ts +113 -48
- package/src/update/direct.ts +3 -1
- package/src/update/generated/fields.ts +3 -3
- package/src/update/generated/protocol.ts +7 -3
- package/src/update/generated/variant-fields.ts +1 -0
- package/src/update/ports.ts +6 -2
- package/src/update/serialized.ts +34 -10
- package/src/update/validation.ts +115 -35
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { isRecord } from "../core/record.ts";
|
|
1
2
|
import {
|
|
2
3
|
TRANSFER_VARIANT_FIELDS,
|
|
3
4
|
TRANSFER_VARIANT_FIELDS_DISCRIMINANTS,
|
|
@@ -13,16 +14,19 @@ import {
|
|
|
13
14
|
TRANSFER_PROTOCOL_VERSION,
|
|
14
15
|
TRANSFER_SESSION_RESPONSE_STATUSES,
|
|
15
16
|
TRANSFER_TARGET_BINDING_KINDS,
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
17
|
+
} from "./generated/protocol.ts";
|
|
18
|
+
import type {
|
|
19
|
+
PanelTransferResponse,
|
|
20
|
+
TransferAbort,
|
|
21
|
+
TransferCancelResponse,
|
|
22
|
+
TransferClientSnapshot,
|
|
23
|
+
TransferCommitSelector,
|
|
24
|
+
TransferLeaseResponse,
|
|
25
|
+
TransferSessionResponse,
|
|
26
|
+
TransferTargetBinding,
|
|
24
27
|
} from "./generated/protocol.ts";
|
|
25
28
|
import { TRANSFER_FIELDS } from "./generated/fields.ts";
|
|
29
|
+
import { assertValidLayoutMutationRejectionCode } from "@inflatable-cookie/longhorn/layout";
|
|
26
30
|
|
|
27
31
|
export type TransferProtocolValidationCode =
|
|
28
32
|
| "unsupported_protocol_version"
|
|
@@ -53,10 +57,7 @@ export function assertTransferProtocolVersion(
|
|
|
53
57
|
version: unknown,
|
|
54
58
|
): asserts version is typeof TRANSFER_PROTOCOL_VERSION {
|
|
55
59
|
if (version !== TRANSFER_PROTOCOL_VERSION) {
|
|
56
|
-
throw new TransferProtocolValidationError(
|
|
57
|
-
"unsupported_protocol_version",
|
|
58
|
-
version,
|
|
59
|
-
);
|
|
60
|
+
throw new TransferProtocolValidationError("unsupported_protocol_version", version);
|
|
60
61
|
}
|
|
61
62
|
}
|
|
62
63
|
|
|
@@ -64,11 +65,7 @@ export function assertValidTransferTargetBinding(
|
|
|
64
65
|
value: unknown,
|
|
65
66
|
): asserts value is TransferTargetBinding {
|
|
66
67
|
const candidate = record(value);
|
|
67
|
-
assertKnown(
|
|
68
|
-
candidate.kind,
|
|
69
|
-
TRANSFER_TARGET_BINDING_KINDS,
|
|
70
|
-
"unknown_target_binding",
|
|
71
|
-
);
|
|
68
|
+
assertKnown(candidate.kind, TRANSFER_TARGET_BINDING_KINDS, "unknown_target_binding");
|
|
72
69
|
record(candidate, variantKeys("TransferTargetBinding", candidate));
|
|
73
70
|
}
|
|
74
71
|
|
|
@@ -77,16 +74,8 @@ export function assertValidTransferClientSnapshot(
|
|
|
77
74
|
): asserts value is TransferClientSnapshot {
|
|
78
75
|
const snapshot = record(value, TRANSFER_FIELDS.TransferClientSnapshot);
|
|
79
76
|
assertTransferProtocolVersion(snapshot.protocol_version);
|
|
80
|
-
if (
|
|
81
|
-
|
|
82
|
-
!unsignedInteger(snapshot.client_epoch) ||
|
|
83
|
-
(snapshot.current_lease_generation !== null &&
|
|
84
|
-
!unsignedInteger(snapshot.current_lease_generation))
|
|
85
|
-
) {
|
|
86
|
-
throw new TransferProtocolValidationError(
|
|
87
|
-
"invalid_client_snapshot",
|
|
88
|
-
value,
|
|
89
|
-
);
|
|
77
|
+
if (typeof snapshot.client_id !== "string" || !unsignedInteger(snapshot.client_epoch)) {
|
|
78
|
+
throw new TransferProtocolValidationError("invalid_client_snapshot", value);
|
|
90
79
|
}
|
|
91
80
|
}
|
|
92
81
|
|
|
@@ -94,38 +83,23 @@ export function assertValidTransferCommitSelector(
|
|
|
94
83
|
value: unknown,
|
|
95
84
|
): asserts value is TransferCommitSelector {
|
|
96
85
|
const candidate = record(value);
|
|
97
|
-
assertKnown(
|
|
98
|
-
candidate.kind,
|
|
99
|
-
TRANSFER_COMMIT_SELECTOR_KINDS,
|
|
100
|
-
"unknown_commit_selector",
|
|
101
|
-
);
|
|
86
|
+
assertKnown(candidate.kind, TRANSFER_COMMIT_SELECTOR_KINDS, "unknown_commit_selector");
|
|
102
87
|
record(candidate, variantKeys("TransferCommitSelector", candidate));
|
|
103
88
|
}
|
|
104
89
|
|
|
105
|
-
export function assertValidTransferAbort(
|
|
106
|
-
value: unknown,
|
|
107
|
-
): asserts value is TransferAbort {
|
|
90
|
+
export function assertValidTransferAbort(value: unknown): asserts value is TransferAbort {
|
|
108
91
|
const abort = record(value, TRANSFER_FIELDS.TransferAbort);
|
|
109
92
|
assertTransferProtocolVersion(abort.protocol_version);
|
|
110
93
|
const source = record(abort.source);
|
|
111
|
-
assertKnown(
|
|
112
|
-
source.domain,
|
|
113
|
-
TRANSFER_ABORT_DOMAINS,
|
|
114
|
-
"unknown_abort_domain",
|
|
115
|
-
);
|
|
94
|
+
assertKnown(source.domain, TRANSFER_ABORT_DOMAINS, "unknown_abort_domain");
|
|
116
95
|
record(source, variantKeys("TransferAbortSource", source));
|
|
117
96
|
if (source.domain === "transfer") {
|
|
118
|
-
assertKnown(
|
|
119
|
-
source.code,
|
|
120
|
-
TRANSFER_ERROR_CODES,
|
|
121
|
-
"unknown_transfer_error_code",
|
|
122
|
-
);
|
|
97
|
+
assertKnown(source.code, TRANSFER_ERROR_CODES, "unknown_transfer_error_code");
|
|
123
98
|
} else {
|
|
124
|
-
assertKnown(
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
);
|
|
99
|
+
assertKnown(source.code, PANEL_TRANSFER_ERROR_CODES, "unknown_panel_error_code");
|
|
100
|
+
}
|
|
101
|
+
if (abort.layout_code !== null) {
|
|
102
|
+
assertValidLayoutMutationRejectionCode(abort.layout_code);
|
|
129
103
|
}
|
|
130
104
|
}
|
|
131
105
|
|
|
@@ -170,9 +144,7 @@ export function assertValidTransferCancelResponse(
|
|
|
170
144
|
"TransferCancelResponse",
|
|
171
145
|
);
|
|
172
146
|
if (response.status === "cancelled") {
|
|
173
|
-
assertTransferProtocolVersion(
|
|
174
|
-
record(response.cancellation).protocol_version,
|
|
175
|
-
);
|
|
147
|
+
assertTransferProtocolVersion(record(response.cancellation).protocol_version);
|
|
176
148
|
} else {
|
|
177
149
|
assertValidTransferAbort(response.abort);
|
|
178
150
|
}
|
|
@@ -189,9 +161,8 @@ export function assertValidPanelTransferResponse(
|
|
|
189
161
|
if (response.status === "committed") {
|
|
190
162
|
const completion = record(response.completion, TRANSFER_FIELDS.PanelTransferCompletion);
|
|
191
163
|
assertTransferProtocolVersion(completion.protocol_version);
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
);
|
|
164
|
+
// oxlint-disable-next-line quality/no-known-value-widening -- Runtime validation also covers structurally typed callers and untrusted wire values.
|
|
165
|
+
assertValidTransferTargetBinding(record(record(completion.target).binding));
|
|
195
166
|
} else {
|
|
196
167
|
assertValidTransferAbort(response.abort);
|
|
197
168
|
}
|
|
@@ -199,7 +170,7 @@ export function assertValidPanelTransferResponse(
|
|
|
199
170
|
|
|
200
171
|
function responseWithStatus(
|
|
201
172
|
value: unknown,
|
|
202
|
-
statuses:
|
|
173
|
+
statuses: ReadonlyArray<string>,
|
|
203
174
|
type: string,
|
|
204
175
|
): Record<string, unknown> {
|
|
205
176
|
const response = record(value);
|
|
@@ -216,12 +187,10 @@ function responseWithStatus(
|
|
|
216
187
|
* A missing entry means the generator failed to read the union; every caller
|
|
217
188
|
* runs `assertKnown` over the discriminant above this.
|
|
218
189
|
*/
|
|
219
|
-
function variantKeys(
|
|
220
|
-
type: string,
|
|
221
|
-
value: Record<string, unknown>,
|
|
222
|
-
): readonly string[] {
|
|
190
|
+
function variantKeys(type: string, value: Record<string, unknown>): ReadonlyArray<string> {
|
|
223
191
|
const discriminant = value[TRANSFER_VARIANT_FIELDS_DISCRIMINANTS[type] ?? "kind"];
|
|
224
|
-
const keys =
|
|
192
|
+
const keys =
|
|
193
|
+
typeof discriminant === "string" ? TRANSFER_VARIANT_FIELDS[type]?.[discriminant] : undefined;
|
|
225
194
|
if (keys === undefined) {
|
|
226
195
|
throw new TransferProtocolValidationError("unknown_response_status", {
|
|
227
196
|
type,
|
|
@@ -233,7 +202,7 @@ function variantKeys(
|
|
|
233
202
|
|
|
234
203
|
function assertKnown(
|
|
235
204
|
value: unknown,
|
|
236
|
-
known:
|
|
205
|
+
known: ReadonlyArray<string>,
|
|
237
206
|
code: TransferProtocolValidationCode,
|
|
238
207
|
): asserts value is string {
|
|
239
208
|
if (typeof value !== "string" || !known.includes(value)) {
|
|
@@ -249,15 +218,14 @@ function assertKnown(
|
|
|
249
218
|
* Passing no list keeps shape-only behaviour for the tagged unions, whose
|
|
250
219
|
* allowed keys depend on their discriminant and so are not one flat set.
|
|
251
220
|
*/
|
|
252
|
-
function record(
|
|
253
|
-
value
|
|
254
|
-
allowed?: readonly string[],
|
|
255
|
-
): Record<string, unknown> {
|
|
256
|
-
if (typeof value !== "object" || value === null || Array.isArray(value)) {
|
|
221
|
+
function record(value: unknown, allowed?: ReadonlyArray<string>): Record<string, unknown> {
|
|
222
|
+
if (!isRecord(value)) {
|
|
257
223
|
throw new TransferProtocolValidationError("invalid_object", value);
|
|
258
224
|
}
|
|
259
|
-
const result = value
|
|
260
|
-
if (allowed === undefined)
|
|
225
|
+
const result = value;
|
|
226
|
+
if (allowed === undefined) {
|
|
227
|
+
return result;
|
|
228
|
+
}
|
|
261
229
|
|
|
262
230
|
const permitted = new Set(allowed);
|
|
263
231
|
for (const key of Object.keys(result)) {
|
|
@@ -274,9 +242,5 @@ function record(
|
|
|
274
242
|
}
|
|
275
243
|
|
|
276
244
|
function unsignedInteger(value: unknown): value is number {
|
|
277
|
-
return (
|
|
278
|
-
typeof value === "number" &&
|
|
279
|
-
Number.isSafeInteger(value) &&
|
|
280
|
-
value >= 0
|
|
281
|
-
);
|
|
245
|
+
return typeof value === "number" && Number.isSafeInteger(value) && value >= 0;
|
|
282
246
|
}
|
package/src/update/client.ts
CHANGED
|
@@ -26,13 +26,63 @@ import {
|
|
|
26
26
|
|
|
27
27
|
export class UpdateClient implements CheckedUpdatePort {
|
|
28
28
|
constructor(readonly port: UpdatePort) {}
|
|
29
|
-
async snapshot(): Promise<UpdateSnapshot> {
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
async
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
29
|
+
async snapshot(): Promise<UpdateSnapshot> {
|
|
30
|
+
const value = await this.port.snapshot();
|
|
31
|
+
assertUpdateSnapshot(value);
|
|
32
|
+
return value;
|
|
33
|
+
}
|
|
34
|
+
async check(command: UpdateCheckCommand): Promise<UpdateOutcomeProjection> {
|
|
35
|
+
assertUpdateCheckCommand(command);
|
|
36
|
+
const value = await this.port.check(command);
|
|
37
|
+
assertUpdateOutcome(value);
|
|
38
|
+
return value;
|
|
39
|
+
}
|
|
40
|
+
async selectChannel(command: UpdateSelectChannelCommand): Promise<UpdateOutcomeProjection> {
|
|
41
|
+
assertUpdateSelectChannelCommand(command);
|
|
42
|
+
const value = await this.port.selectChannel(command);
|
|
43
|
+
assertUpdateOutcome(value);
|
|
44
|
+
return value;
|
|
45
|
+
}
|
|
46
|
+
async defer(command: UpdateDeferCommand): Promise<UpdateOutcomeProjection> {
|
|
47
|
+
assertUpdateDeferCommand(command);
|
|
48
|
+
const value = await this.port.defer(command);
|
|
49
|
+
assertUpdateOutcome(value);
|
|
50
|
+
return value;
|
|
51
|
+
}
|
|
52
|
+
async prepare(command: UpdatePrepareCommand): Promise<UpdateOutcomeProjection> {
|
|
53
|
+
assertUpdatePrepareCommand(command);
|
|
54
|
+
const value = await this.port.prepare(command);
|
|
55
|
+
assertUpdateOutcome(value);
|
|
56
|
+
return value;
|
|
57
|
+
}
|
|
58
|
+
async apply(command: UpdateApplyCommand): Promise<UpdateOutcomeProjection> {
|
|
59
|
+
assertUpdateApplyCommand(command);
|
|
60
|
+
const value = await this.port.apply(command);
|
|
61
|
+
assertUpdateOutcome(value);
|
|
62
|
+
return value;
|
|
63
|
+
}
|
|
64
|
+
async cancel(command: UpdateCancelCommand): Promise<UpdateOutcomeProjection> {
|
|
65
|
+
assertUpdateCancelCommand(command);
|
|
66
|
+
const value = await this.port.cancel(command);
|
|
67
|
+
assertUpdateOutcome(value);
|
|
68
|
+
return value;
|
|
69
|
+
}
|
|
70
|
+
async listen(listener: (event: UpdateChangedEvent) => void): Promise<UpdateUnlisten> {
|
|
71
|
+
if (this.port.listen === undefined) {
|
|
72
|
+
return () => {};
|
|
73
|
+
}
|
|
74
|
+
return this.port.listen((value) => {
|
|
75
|
+
assertUpdateChangedEvent(value);
|
|
76
|
+
listener(value);
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
async listenProgress(listener: (event: UpdateProgressEvent) => void): Promise<UpdateUnlisten> {
|
|
80
|
+
if (this.port.listenProgress === undefined) {
|
|
81
|
+
return () => {};
|
|
82
|
+
}
|
|
83
|
+
return this.port.listenProgress((value) => {
|
|
84
|
+
assertUpdateProgressEvent(value);
|
|
85
|
+
listener(value);
|
|
86
|
+
});
|
|
87
|
+
}
|
|
38
88
|
}
|
package/src/update/controller.ts
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
import { UpdateClient } from "./client.ts";
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
2
|
+
import { UPDATE_PROTOCOL_VERSION } from "./generated/protocol.ts";
|
|
3
|
+
import type {
|
|
4
|
+
Channel,
|
|
5
|
+
DeferralCause,
|
|
6
|
+
UpdateAvailabilityProjection,
|
|
7
|
+
UpdateOutcomeProjection,
|
|
8
|
+
UpdateProgressProjection,
|
|
9
|
+
UpdateRejectionCode,
|
|
10
|
+
UpdateSnapshot,
|
|
11
|
+
UpdateStagedArtifactProjection,
|
|
12
12
|
} from "./generated/protocol.ts";
|
|
13
13
|
import type { UpdatePort, UpdateUnlisten } from "./ports.ts";
|
|
14
14
|
|
|
@@ -71,12 +71,12 @@ export class UpdateController {
|
|
|
71
71
|
readonly #observers = new Set<() => void>();
|
|
72
72
|
#status: UpdateControllerStatus = { kind: "idle" };
|
|
73
73
|
#snapshot?: UpdateSnapshot;
|
|
74
|
-
#lastRejection
|
|
74
|
+
#lastRejection: UpdateRejectionCode | undefined;
|
|
75
75
|
#started = false;
|
|
76
76
|
#lifecycle = 0;
|
|
77
77
|
#commandSequence = 0;
|
|
78
78
|
#pending = false;
|
|
79
|
-
#unlisten: UpdateUnlisten
|
|
79
|
+
#unlisten: Array<UpdateUnlisten> = [];
|
|
80
80
|
/**
|
|
81
81
|
* The most recent live progress report.
|
|
82
82
|
*
|
|
@@ -85,15 +85,21 @@ export class UpdateController {
|
|
|
85
85
|
* said before the transfer began. It is cleared when the command that
|
|
86
86
|
* started the transfer settles, so a stale report cannot outlive it.
|
|
87
87
|
*/
|
|
88
|
-
#liveProgress
|
|
88
|
+
#liveProgress: UpdateProgressProjection | undefined;
|
|
89
89
|
|
|
90
90
|
constructor(options: UpdateControllerOptions) {
|
|
91
91
|
this.#client = new UpdateClient(options.port);
|
|
92
92
|
}
|
|
93
93
|
|
|
94
|
-
get status(): UpdateControllerStatus {
|
|
95
|
-
|
|
96
|
-
|
|
94
|
+
get status(): UpdateControllerStatus {
|
|
95
|
+
return this.#status;
|
|
96
|
+
}
|
|
97
|
+
get snapshot(): UpdateSnapshot | undefined {
|
|
98
|
+
return this.#snapshot;
|
|
99
|
+
}
|
|
100
|
+
get availability(): UpdateAvailabilityProjection | undefined {
|
|
101
|
+
return this.#snapshot?.availability;
|
|
102
|
+
}
|
|
97
103
|
/**
|
|
98
104
|
* What the transfer is doing right now.
|
|
99
105
|
*
|
|
@@ -101,12 +107,22 @@ export class UpdateController {
|
|
|
101
107
|
* fresh as the last commit and the transfer deliberately does not hold the
|
|
102
108
|
* authority while it runs.
|
|
103
109
|
*/
|
|
104
|
-
get progress(): UpdateProgressProjection | undefined {
|
|
110
|
+
get progress(): UpdateProgressProjection | undefined {
|
|
111
|
+
return this.#liveProgress ?? this.#snapshot?.progress;
|
|
112
|
+
}
|
|
105
113
|
/** The retained verified artifact, when one is staged. */
|
|
106
|
-
get staged(): UpdateStagedArtifactProjection | undefined {
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
get
|
|
114
|
+
get staged(): UpdateStagedArtifactProjection | undefined {
|
|
115
|
+
return this.#snapshot?.staged ?? undefined;
|
|
116
|
+
}
|
|
117
|
+
get channel(): Channel | undefined {
|
|
118
|
+
return this.#snapshot?.channel;
|
|
119
|
+
}
|
|
120
|
+
get installedVersion(): string | undefined {
|
|
121
|
+
return this.#snapshot?.installedVersion;
|
|
122
|
+
}
|
|
123
|
+
get pending(): boolean {
|
|
124
|
+
return this.#pending;
|
|
125
|
+
}
|
|
110
126
|
|
|
111
127
|
/**
|
|
112
128
|
* Why the last install did not proceed, when it did not.
|
|
@@ -119,7 +135,9 @@ export class UpdateController {
|
|
|
119
135
|
}
|
|
120
136
|
|
|
121
137
|
/** The latest refusal, cleared as soon as the next command starts. */
|
|
122
|
-
get lastRejection(): UpdateRejectionCode | undefined {
|
|
138
|
+
get lastRejection(): UpdateRejectionCode | undefined {
|
|
139
|
+
return this.#lastRejection;
|
|
140
|
+
}
|
|
123
141
|
|
|
124
142
|
/**
|
|
125
143
|
* Whether this install sits ahead of the channel it follows.
|
|
@@ -132,7 +150,9 @@ export class UpdateController {
|
|
|
132
150
|
*/
|
|
133
151
|
get aheadOfChannel(): { readonly installed: string; readonly channel: string } | undefined {
|
|
134
152
|
const availability = this.#snapshot?.availability;
|
|
135
|
-
if (availability?.state !== "aheadOfChannel")
|
|
153
|
+
if (availability?.state !== "aheadOfChannel") {
|
|
154
|
+
return undefined;
|
|
155
|
+
}
|
|
136
156
|
return { installed: availability.installed, channel: availability.channel };
|
|
137
157
|
}
|
|
138
158
|
|
|
@@ -156,7 +176,9 @@ export class UpdateController {
|
|
|
156
176
|
*/
|
|
157
177
|
get presence(): UpdatePresence {
|
|
158
178
|
const snapshot = this.#snapshot;
|
|
159
|
-
if (snapshot === undefined)
|
|
179
|
+
if (snapshot === undefined) {
|
|
180
|
+
return "hidden";
|
|
181
|
+
}
|
|
160
182
|
|
|
161
183
|
switch (this.progress?.state) {
|
|
162
184
|
case "downloading":
|
|
@@ -170,7 +192,9 @@ export class UpdateController {
|
|
|
170
192
|
break;
|
|
171
193
|
}
|
|
172
194
|
|
|
173
|
-
if (snapshot.availability.state !== "offer")
|
|
195
|
+
if (snapshot.availability.state !== "offer") {
|
|
196
|
+
return "hidden";
|
|
197
|
+
}
|
|
174
198
|
// Postponed by the operator, not by the gate. A gate deferral is transient
|
|
175
199
|
// and the offer is still the answer; `userPostponed` is a decision.
|
|
176
200
|
return snapshot.deferral?.cause.cause === "userPostponed" ? "quiet" : "attention";
|
|
@@ -182,7 +206,9 @@ export class UpdateController {
|
|
|
182
206
|
}
|
|
183
207
|
|
|
184
208
|
async start(): Promise<void> {
|
|
185
|
-
if (this.#started)
|
|
209
|
+
if (this.#started) {
|
|
210
|
+
return;
|
|
211
|
+
}
|
|
186
212
|
this.#started = true;
|
|
187
213
|
const lifecycle = ++this.#lifecycle;
|
|
188
214
|
this.#setStatus({ kind: "loading" });
|
|
@@ -197,7 +223,9 @@ export class UpdateController {
|
|
|
197
223
|
}
|
|
198
224
|
this.#unlisten.push(unlisten);
|
|
199
225
|
const unlistenProgress = await this.#client.listenProgress((event) => {
|
|
200
|
-
if (!this.#started || lifecycle !== this.#lifecycle)
|
|
226
|
+
if (!this.#started || lifecycle !== this.#lifecycle) {
|
|
227
|
+
return;
|
|
228
|
+
}
|
|
201
229
|
this.#liveProgress = event.progress;
|
|
202
230
|
this.#notify();
|
|
203
231
|
});
|
|
@@ -208,7 +236,9 @@ export class UpdateController {
|
|
|
208
236
|
this.#unlisten.push(unlistenProgress);
|
|
209
237
|
await this.refresh();
|
|
210
238
|
} catch (error) {
|
|
211
|
-
if (this.#started && lifecycle === this.#lifecycle)
|
|
239
|
+
if (this.#started && lifecycle === this.#lifecycle) {
|
|
240
|
+
this.#setStatus({ kind: "failed", error });
|
|
241
|
+
}
|
|
212
242
|
}
|
|
213
243
|
}
|
|
214
244
|
|
|
@@ -217,7 +247,9 @@ export class UpdateController {
|
|
|
217
247
|
this.#lifecycle += 1;
|
|
218
248
|
const unlisten = this.#unlisten;
|
|
219
249
|
this.#unlisten = [];
|
|
220
|
-
for (const stop of unlisten)
|
|
250
|
+
for (const stop of unlisten) {
|
|
251
|
+
await stop();
|
|
252
|
+
}
|
|
221
253
|
this.#setStatus({ kind: "idle" });
|
|
222
254
|
}
|
|
223
255
|
|
|
@@ -225,11 +257,15 @@ export class UpdateController {
|
|
|
225
257
|
const lifecycle = this.#lifecycle;
|
|
226
258
|
try {
|
|
227
259
|
const snapshot = await this.#client.snapshot();
|
|
228
|
-
if (lifecycle !== this.#lifecycle)
|
|
260
|
+
if (lifecycle !== this.#lifecycle) {
|
|
261
|
+
return;
|
|
262
|
+
}
|
|
229
263
|
this.#snapshot = snapshot;
|
|
230
264
|
this.#setStatus({ kind: "ready" });
|
|
231
265
|
} catch (error) {
|
|
232
|
-
if (lifecycle === this.#lifecycle)
|
|
266
|
+
if (lifecycle === this.#lifecycle) {
|
|
267
|
+
this.#setStatus({ kind: "failed", error });
|
|
268
|
+
}
|
|
233
269
|
}
|
|
234
270
|
}
|
|
235
271
|
|
|
@@ -243,14 +279,24 @@ export class UpdateController {
|
|
|
243
279
|
/** Follows a different channel from now on. */
|
|
244
280
|
async selectChannel(channel: Channel): Promise<UpdateCommandOutcome> {
|
|
245
281
|
return this.#command((client, epoch) =>
|
|
246
|
-
client.selectChannel({
|
|
282
|
+
client.selectChannel({
|
|
283
|
+
protocolVersion: UPDATE_PROTOCOL_VERSION,
|
|
284
|
+
authorityEpoch: epoch,
|
|
285
|
+
channel,
|
|
286
|
+
}),
|
|
247
287
|
);
|
|
248
288
|
}
|
|
249
289
|
|
|
250
290
|
/** Declines a version for now. */
|
|
251
291
|
async defer(version: string, cause: DeferralCause): Promise<UpdateCommandOutcome> {
|
|
252
|
-
return this.#command(
|
|
253
|
-
client
|
|
292
|
+
return this.#command(
|
|
293
|
+
(client, epoch) =>
|
|
294
|
+
client.defer({
|
|
295
|
+
protocolVersion: UPDATE_PROTOCOL_VERSION,
|
|
296
|
+
authorityEpoch: epoch,
|
|
297
|
+
version,
|
|
298
|
+
cause,
|
|
299
|
+
}),
|
|
254
300
|
true,
|
|
255
301
|
);
|
|
256
302
|
}
|
|
@@ -269,10 +315,18 @@ export class UpdateController {
|
|
|
269
315
|
* can offer the restart again.
|
|
270
316
|
*/
|
|
271
317
|
async apply(version: string): Promise<UpdateCommandOutcome> {
|
|
272
|
-
return this.#command((client, epoch) =>
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
318
|
+
return this.#command(async (client, epoch, snapshot) => {
|
|
319
|
+
const staged = snapshot.staged;
|
|
320
|
+
if (staged?.version !== version) {
|
|
321
|
+
return { status: "rejected", code: "noOffer", snapshot };
|
|
322
|
+
}
|
|
323
|
+
return client.apply({
|
|
324
|
+
protocolVersion: UPDATE_PROTOCOL_VERSION,
|
|
325
|
+
authorityEpoch: epoch,
|
|
326
|
+
version,
|
|
327
|
+
digest: staged.digest,
|
|
328
|
+
});
|
|
329
|
+
}, true);
|
|
276
330
|
}
|
|
277
331
|
|
|
278
332
|
/** Discards the retained staged artifact. */
|
|
@@ -283,13 +337,17 @@ export class UpdateController {
|
|
|
283
337
|
}
|
|
284
338
|
|
|
285
339
|
async #command(
|
|
286
|
-
run: (
|
|
340
|
+
run: (
|
|
341
|
+
client: UpdateClient,
|
|
342
|
+
epoch: number,
|
|
343
|
+
snapshot: UpdateSnapshot,
|
|
344
|
+
) => Promise<UpdateOutcomeProjection>,
|
|
287
345
|
committedMayDefer = false,
|
|
288
346
|
): Promise<UpdateCommandOutcome> {
|
|
289
347
|
const commandSequence = ++this.#commandSequence;
|
|
290
348
|
this.#lastRejection = undefined;
|
|
291
|
-
const
|
|
292
|
-
if (
|
|
349
|
+
const snapshot = this.#snapshot;
|
|
350
|
+
if (snapshot === undefined) {
|
|
293
351
|
// No snapshot means no epoch to send, and inventing one would have the
|
|
294
352
|
// authority refuse it as stale. Say what is actually wrong.
|
|
295
353
|
const error = new Error("update state has not been read yet");
|
|
@@ -301,12 +359,17 @@ export class UpdateController {
|
|
|
301
359
|
this.#liveProgress = undefined;
|
|
302
360
|
this.#notify();
|
|
303
361
|
try {
|
|
304
|
-
const outcome = await run(this.#client,
|
|
305
|
-
const result: UpdateCommandOutcome =
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
362
|
+
const outcome = await run(this.#client, snapshot.authorityEpoch, snapshot);
|
|
363
|
+
const result: UpdateCommandOutcome =
|
|
364
|
+
outcome.status === "rejected"
|
|
365
|
+
? { status: "rejected", snapshot: outcome.snapshot, code: outcome.code }
|
|
366
|
+
: committedMayDefer && outcome.snapshot.deferral !== null
|
|
367
|
+
? {
|
|
368
|
+
status: "committedWithDeferral",
|
|
369
|
+
snapshot: outcome.snapshot,
|
|
370
|
+
deferral: outcome.snapshot.deferral,
|
|
371
|
+
}
|
|
372
|
+
: { status: "committed", snapshot: outcome.snapshot };
|
|
310
373
|
if (lifecycle === this.#lifecycle && commandSequence === this.#commandSequence) {
|
|
311
374
|
this.#snapshot = outcome.snapshot;
|
|
312
375
|
this.#liveProgress = undefined;
|
|
@@ -333,6 +396,8 @@ export class UpdateController {
|
|
|
333
396
|
}
|
|
334
397
|
|
|
335
398
|
#notify(): void {
|
|
336
|
-
for (const observer of this.#observers)
|
|
399
|
+
for (const observer of this.#observers) {
|
|
400
|
+
observer();
|
|
401
|
+
}
|
|
337
402
|
}
|
|
338
403
|
}
|
package/src/update/direct.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
1
|
import type { UpdatePort } from "./ports.ts";
|
|
2
2
|
export type DirectUpdateHandlers = UpdatePort;
|
|
3
|
-
export function createDirectUpdatePort(handlers: DirectUpdateHandlers): UpdatePort {
|
|
3
|
+
export function createDirectUpdatePort(handlers: DirectUpdateHandlers): UpdatePort {
|
|
4
|
+
return handlers;
|
|
5
|
+
}
|
|
@@ -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 UPDATE_FIELDS
|
|
6
|
+
export const UPDATE_FIELDS = {
|
|
7
7
|
"UpdateDeferralProjection": ["version", "cause"],
|
|
8
8
|
"UpdateStagedArtifactProjection": ["version", "channel", "digest"],
|
|
9
9
|
"UpdateProgressEvent": ["protocolVersion", "authorityEpoch", "progress"],
|
|
@@ -12,7 +12,7 @@ export const UPDATE_FIELDS: Record<string, readonly string[]> = {
|
|
|
12
12
|
"UpdateSelectChannelCommand": ["protocolVersion", "authorityEpoch", "channel"],
|
|
13
13
|
"UpdateDeferCommand": ["protocolVersion", "authorityEpoch", "version", "cause"],
|
|
14
14
|
"UpdatePrepareCommand": ["protocolVersion", "authorityEpoch", "version"],
|
|
15
|
-
"UpdateApplyCommand": ["protocolVersion", "authorityEpoch", "version"],
|
|
15
|
+
"UpdateApplyCommand": ["protocolVersion", "authorityEpoch", "version", "digest"],
|
|
16
16
|
"UpdateCancelCommand": ["protocolVersion", "authorityEpoch"],
|
|
17
17
|
"UpdateChangedEvent": ["protocolVersion", "authorityEpoch", "kind"],
|
|
18
|
-
};
|
|
18
|
+
} as const;
|
|
@@ -7,7 +7,7 @@ export const UPDATE_CHANNELS = ["production","beta","nightly"] as const;
|
|
|
7
7
|
export const UPDATE_OFFER_REASONS = ["staged","belowMinimumVersion","userInitiated"] as const;
|
|
8
8
|
export const UPDATE_INSTALL_MANAGERS = ["macAppStore","homebrewCask","flatpak","snap","appImage","nix","linuxDistribution"] as const;
|
|
9
9
|
export const UPDATE_DEFERRAL_CAUSES = ["userPostponed","workInFlight","installationNotWritable","externallyManaged","installFailed"] as const;
|
|
10
|
-
export const UPDATE_AVAILABILITY_STATES = ["offer","upToDate","aheadOfChannel","withheldByRollout","managedElsewhere"] as const;
|
|
10
|
+
export const UPDATE_AVAILABILITY_STATES = ["notChecked","offer","upToDate","aheadOfChannel","withheldByRollout","managedElsewhere"] as const;
|
|
11
11
|
export const UPDATE_PROGRESS_STATES = ["idle","downloading","verifying","readyToInstall","installing"] as const;
|
|
12
12
|
export const UPDATE_INSTALL_AUTHORIZATION_STATUSES = ["held","deferred"] as const;
|
|
13
13
|
export const UPDATE_REJECTION_CODES = ["staleAuthority","noOffer","unavailable","channelMismatch","unreachable","signatureRejected","notWritable","installFailed"] as const;
|
|
@@ -44,7 +44,7 @@ command: string | null, } | { "cause": "installFailed",
|
|
|
44
44
|
*/
|
|
45
45
|
detail: string, };
|
|
46
46
|
|
|
47
|
-
export type UpdateAvailabilityProjection = { "state": "offer",
|
|
47
|
+
export type UpdateAvailabilityProjection = { "state": "notChecked" } | { "state": "offer",
|
|
48
48
|
/**
|
|
49
49
|
* The version offered.
|
|
50
50
|
*/
|
|
@@ -253,7 +253,11 @@ authorityEpoch: number,
|
|
|
253
253
|
/**
|
|
254
254
|
* The version the caller believes is staged.
|
|
255
255
|
*/
|
|
256
|
-
version: string,
|
|
256
|
+
version: string,
|
|
257
|
+
/**
|
|
258
|
+
* SHA-256 of the staged bytes observed by the caller, lowercase hex.
|
|
259
|
+
*/
|
|
260
|
+
digest: string, };
|
|
257
261
|
|
|
258
262
|
export type UpdateCancelCommand = {
|
|
259
263
|
/**
|
|
@@ -12,6 +12,7 @@ export const UPDATE_VARIANT_FIELDS: Record<string, Record<string, readonly strin
|
|
|
12
12
|
"installFailed": ["cause", "detail"],
|
|
13
13
|
},
|
|
14
14
|
"UpdateAvailabilityProjection": {
|
|
15
|
+
"notChecked": ["state"],
|
|
15
16
|
"offer": ["state", "version", "reason", "notes"],
|
|
16
17
|
"upToDate": ["state"],
|
|
17
18
|
"aheadOfChannel": ["state", "installed", "channel"],
|
package/src/update/ports.ts
CHANGED
|
@@ -27,8 +27,12 @@ export interface UpdatePort {
|
|
|
27
27
|
prepare(command: UpdatePrepareCommand): Promise<unknown>;
|
|
28
28
|
apply(command: UpdateApplyCommand): Promise<unknown>;
|
|
29
29
|
cancel(command: UpdateCancelCommand): Promise<unknown>;
|
|
30
|
-
listen
|
|
31
|
-
|
|
30
|
+
listen?:
|
|
31
|
+
| ((listener: (event: unknown) => void) => UpdateUnlisten | Promise<UpdateUnlisten>)
|
|
32
|
+
| undefined;
|
|
33
|
+
listenProgress?:
|
|
34
|
+
| ((listener: (event: unknown) => void) => UpdateUnlisten | Promise<UpdateUnlisten>)
|
|
35
|
+
| undefined;
|
|
32
36
|
}
|
|
33
37
|
|
|
34
38
|
/** The same seam after validation. */
|