@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,61 @@
|
|
|
1
|
+
import type { Unlisten } from "@inflatable-cookie/longhorn/core";
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
deserialize,
|
|
5
|
+
serialize,
|
|
6
|
+
} from "../loopback.ts";
|
|
7
|
+
|
|
8
|
+
export interface BridgeStreamSource {
|
|
9
|
+
listen(listener: (value: unknown) => void): Promise<Unlisten>;
|
|
10
|
+
loadSnapshot(): Promise<unknown>;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export class DirectBridgeStreamSource implements BridgeStreamSource {
|
|
14
|
+
readonly #loadSnapshot: () => unknown | Promise<unknown>;
|
|
15
|
+
readonly #listeners = new Set<(value: unknown) => void>();
|
|
16
|
+
|
|
17
|
+
constructor(loadSnapshot: () => unknown | Promise<unknown>) {
|
|
18
|
+
this.#loadSnapshot = loadSnapshot;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
async listen(listener: (value: unknown) => void): Promise<Unlisten> {
|
|
22
|
+
this.#listeners.add(listener);
|
|
23
|
+
return () => {
|
|
24
|
+
this.#listeners.delete(listener);
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
loadSnapshot(): Promise<unknown> {
|
|
29
|
+
return Promise.resolve(this.#loadSnapshot());
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
emit(value: unknown): void {
|
|
33
|
+
for (const listener of this.#listeners) {
|
|
34
|
+
listener(value);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
listenerCount(): number {
|
|
39
|
+
return this.#listeners.size;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export class SerializedLoopbackBridgeStreamSource
|
|
44
|
+
implements BridgeStreamSource {
|
|
45
|
+
readonly #source: BridgeStreamSource;
|
|
46
|
+
|
|
47
|
+
constructor(source: BridgeStreamSource) {
|
|
48
|
+
this.#source = source;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
listen(listener: (value: unknown) => void): Promise<Unlisten> {
|
|
52
|
+
return this.#source.listen((value) => {
|
|
53
|
+
listener(deserialize(serialize(value, "event"), "event"));
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
async loadSnapshot(): Promise<unknown> {
|
|
58
|
+
const value = await this.#source.loadSnapshot();
|
|
59
|
+
return deserialize(serialize(value, "snapshot"), "snapshot");
|
|
60
|
+
}
|
|
61
|
+
}
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
BridgeSnapshotDecision,
|
|
3
|
+
BridgeStreamCursor,
|
|
4
|
+
BridgeStreamDecision,
|
|
5
|
+
DomainId,
|
|
6
|
+
BridgeSessionId,
|
|
7
|
+
} from "../generated/protocol.ts";
|
|
8
|
+
|
|
9
|
+
export class BridgeStreamTracker {
|
|
10
|
+
#sessionId: BridgeSessionId;
|
|
11
|
+
readonly #domainId: DomainId;
|
|
12
|
+
#accepted: BridgeStreamCursor | undefined;
|
|
13
|
+
#pending: BridgeStreamCursor | undefined;
|
|
14
|
+
#requiresSnapshot = true;
|
|
15
|
+
|
|
16
|
+
constructor(sessionId: BridgeSessionId, domainId: DomainId) {
|
|
17
|
+
this.#sessionId = sessionId;
|
|
18
|
+
this.#domainId = domainId;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
advanceSession(sessionId: BridgeSessionId): void {
|
|
22
|
+
if (this.#sessionId !== sessionId) {
|
|
23
|
+
this.#sessionId = sessionId;
|
|
24
|
+
this.#accepted = undefined;
|
|
25
|
+
this.#pending = undefined;
|
|
26
|
+
this.#requiresSnapshot = true;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
acceptSnapshot(cursor: BridgeStreamCursor): BridgeSnapshotDecision {
|
|
31
|
+
if (cursor.sessionId !== this.#sessionId) {
|
|
32
|
+
return "supersededSession";
|
|
33
|
+
}
|
|
34
|
+
if (cursor.domainId !== this.#domainId) {
|
|
35
|
+
return "wrongDomain";
|
|
36
|
+
}
|
|
37
|
+
const pendingIsNewer = this.#pending !== undefined &&
|
|
38
|
+
newer(this.#pending, cursor);
|
|
39
|
+
this.#accepted = cursor;
|
|
40
|
+
this.#pending = undefined;
|
|
41
|
+
this.#requiresSnapshot = pendingIsNewer;
|
|
42
|
+
return pendingIsNewer
|
|
43
|
+
? "acceptedResnapshotRequired"
|
|
44
|
+
: "accepted";
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
classifyEvent(cursor: BridgeStreamCursor): BridgeStreamDecision {
|
|
48
|
+
if (cursor.sessionId !== this.#sessionId) {
|
|
49
|
+
return "ignoreSupersededSession";
|
|
50
|
+
}
|
|
51
|
+
if (cursor.domainId !== this.#domainId) {
|
|
52
|
+
return "ignoreWrongDomain";
|
|
53
|
+
}
|
|
54
|
+
if (this.#requiresSnapshot) {
|
|
55
|
+
this.#remember(cursor);
|
|
56
|
+
return "resnapshotRequired";
|
|
57
|
+
}
|
|
58
|
+
const current = this.#accepted;
|
|
59
|
+
if (current === undefined) {
|
|
60
|
+
this.#requiresSnapshot = true;
|
|
61
|
+
this.#remember(cursor);
|
|
62
|
+
return "resnapshotRequired";
|
|
63
|
+
}
|
|
64
|
+
if (cursor.authorityEpoch < current.authorityEpoch) {
|
|
65
|
+
return "ignoreStale";
|
|
66
|
+
}
|
|
67
|
+
if (cursor.authorityEpoch > current.authorityEpoch) {
|
|
68
|
+
this.#requiresSnapshot = true;
|
|
69
|
+
this.#remember(cursor);
|
|
70
|
+
return "resnapshotNewEpoch";
|
|
71
|
+
}
|
|
72
|
+
if (cursor.sequence === current.sequence) {
|
|
73
|
+
return "ignoreDuplicate";
|
|
74
|
+
}
|
|
75
|
+
if (cursor.sequence < current.sequence) {
|
|
76
|
+
return "ignoreStale";
|
|
77
|
+
}
|
|
78
|
+
if (cursor.sequence === current.sequence + 1) {
|
|
79
|
+
this.#accepted = cursor;
|
|
80
|
+
return "apply";
|
|
81
|
+
}
|
|
82
|
+
this.#requiresSnapshot = true;
|
|
83
|
+
this.#remember(cursor);
|
|
84
|
+
return "resnapshotGap";
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
acceptedCursor(): BridgeStreamCursor | undefined {
|
|
88
|
+
return this.#accepted;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
requiresSnapshot(): boolean {
|
|
92
|
+
return this.#requiresSnapshot;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
#remember(cursor: BridgeStreamCursor): void {
|
|
96
|
+
if (this.#pending === undefined || newer(cursor, this.#pending)) {
|
|
97
|
+
this.#pending = cursor;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
export function newer(
|
|
103
|
+
candidate: BridgeStreamCursor,
|
|
104
|
+
current: BridgeStreamCursor,
|
|
105
|
+
): boolean {
|
|
106
|
+
return candidate.authorityEpoch > current.authorityEpoch ||
|
|
107
|
+
(candidate.authorityEpoch === current.authorityEpoch &&
|
|
108
|
+
candidate.sequence > current.sequence);
|
|
109
|
+
}
|
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
BridgeCredentialRef,
|
|
3
|
+
BridgeServiceAction,
|
|
4
|
+
BridgeServiceFailureCode,
|
|
5
|
+
BridgeServiceOutcome,
|
|
6
|
+
BridgeServiceOwnership,
|
|
7
|
+
BridgeServiceRequest,
|
|
8
|
+
BridgeServiceState,
|
|
9
|
+
BridgeServiceTransitionReceipt,
|
|
10
|
+
} from "./generated/protocol.ts";
|
|
11
|
+
import { BRIDGE_SERVICE_OWNERSHIPS } from "./generated/protocol.ts";
|
|
12
|
+
import { opaqueId } from "./validation/base.ts";
|
|
13
|
+
|
|
14
|
+
export interface BridgeServiceSupervisorPort {
|
|
15
|
+
perform(
|
|
16
|
+
request: BridgeServiceRequest,
|
|
17
|
+
): Promise<unknown> | unknown;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export class BridgeSupervisionError extends Error {
|
|
21
|
+
readonly code:
|
|
22
|
+
| "lifecycle_not_owned"
|
|
23
|
+
| "invalid_transition"
|
|
24
|
+
| "invalid_observation"
|
|
25
|
+
| "adapter_failed";
|
|
26
|
+
|
|
27
|
+
constructor(code: BridgeSupervisionError["code"]) {
|
|
28
|
+
super(`bridge supervision: ${code}`);
|
|
29
|
+
this.name = "BridgeSupervisionError";
|
|
30
|
+
this.code = code;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export class BridgeServiceRuntime {
|
|
35
|
+
readonly #ownership: BridgeServiceOwnership;
|
|
36
|
+
#state: BridgeServiceState = "absent";
|
|
37
|
+
#generation = 0;
|
|
38
|
+
|
|
39
|
+
constructor(ownership: BridgeServiceOwnership) {
|
|
40
|
+
if (!BRIDGE_SERVICE_OWNERSHIPS.includes(ownership)) {
|
|
41
|
+
throw new BridgeSupervisionError("invalid_transition");
|
|
42
|
+
}
|
|
43
|
+
this.#ownership = ownership;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
get state(): BridgeServiceState {
|
|
47
|
+
return this.#state;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
async execute(
|
|
51
|
+
port: BridgeServiceSupervisorPort,
|
|
52
|
+
action: BridgeServiceAction,
|
|
53
|
+
credentialRef?: BridgeCredentialRef,
|
|
54
|
+
): Promise<BridgeServiceTransitionReceipt> {
|
|
55
|
+
this.#admit(action);
|
|
56
|
+
const request: BridgeServiceRequest = {
|
|
57
|
+
action,
|
|
58
|
+
credentialRef: credentialRef === undefined
|
|
59
|
+
? null
|
|
60
|
+
: opaqueId(credentialRef),
|
|
61
|
+
};
|
|
62
|
+
let rawOutcome: unknown;
|
|
63
|
+
try {
|
|
64
|
+
rawOutcome = await port.perform(request);
|
|
65
|
+
} catch {
|
|
66
|
+
throw new BridgeSupervisionError("adapter_failed");
|
|
67
|
+
}
|
|
68
|
+
return this.observe(action, rawOutcome);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
observe(
|
|
72
|
+
action: BridgeServiceAction,
|
|
73
|
+
value: unknown,
|
|
74
|
+
): BridgeServiceTransitionReceipt {
|
|
75
|
+
this.#admit(action);
|
|
76
|
+
const outcome = parseOutcome(value);
|
|
77
|
+
const current = transitionState(action, outcome);
|
|
78
|
+
const generation = this.#generation + 1;
|
|
79
|
+
if (!Number.isSafeInteger(generation)) {
|
|
80
|
+
throw new BridgeSupervisionError("invalid_transition");
|
|
81
|
+
}
|
|
82
|
+
const receipt = {
|
|
83
|
+
generation,
|
|
84
|
+
ownership: this.#ownership,
|
|
85
|
+
action,
|
|
86
|
+
previous: this.#state,
|
|
87
|
+
current,
|
|
88
|
+
outcome,
|
|
89
|
+
};
|
|
90
|
+
this.#generation = generation;
|
|
91
|
+
this.#state = current;
|
|
92
|
+
return receipt;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
#admit(action: BridgeServiceAction): void {
|
|
96
|
+
const owned = this.#ownership === "ownedLocal";
|
|
97
|
+
let admitted = false;
|
|
98
|
+
switch (action) {
|
|
99
|
+
case "spawn":
|
|
100
|
+
admitted = owned &&
|
|
101
|
+
["absent", "stopped", "failed"].includes(this.#state);
|
|
102
|
+
break;
|
|
103
|
+
case "attach":
|
|
104
|
+
admitted = !owned &&
|
|
105
|
+
["absent", "stopped", "failed"].includes(this.#state);
|
|
106
|
+
break;
|
|
107
|
+
case "checkReadiness":
|
|
108
|
+
admitted = [
|
|
109
|
+
"starting",
|
|
110
|
+
"attaching",
|
|
111
|
+
"awaitingReadiness",
|
|
112
|
+
"restarting",
|
|
113
|
+
"reconnecting",
|
|
114
|
+
"ready",
|
|
115
|
+
].includes(this.#state);
|
|
116
|
+
break;
|
|
117
|
+
case "restart":
|
|
118
|
+
admitted = owned && ["ready", "failed"].includes(this.#state);
|
|
119
|
+
break;
|
|
120
|
+
case "reconnect":
|
|
121
|
+
admitted = ["ready", "failed", "stopped"].includes(this.#state);
|
|
122
|
+
break;
|
|
123
|
+
case "shutdown":
|
|
124
|
+
admitted = owned && !["absent", "stopped"].includes(this.#state);
|
|
125
|
+
break;
|
|
126
|
+
}
|
|
127
|
+
if (admitted) {
|
|
128
|
+
return;
|
|
129
|
+
}
|
|
130
|
+
if (
|
|
131
|
+
!owned &&
|
|
132
|
+
["spawn", "restart", "shutdown"].includes(action)
|
|
133
|
+
) {
|
|
134
|
+
throw new BridgeSupervisionError("lifecycle_not_owned");
|
|
135
|
+
}
|
|
136
|
+
throw new BridgeSupervisionError("invalid_transition");
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
function transitionState(
|
|
141
|
+
action: BridgeServiceAction,
|
|
142
|
+
outcome: BridgeServiceOutcome,
|
|
143
|
+
): BridgeServiceState {
|
|
144
|
+
if (typeof outcome === "object") {
|
|
145
|
+
return "failed";
|
|
146
|
+
}
|
|
147
|
+
if (action === "checkReadiness" && outcome === "ready") {
|
|
148
|
+
return "ready";
|
|
149
|
+
}
|
|
150
|
+
if (action === "checkReadiness" && outcome === "notReady") {
|
|
151
|
+
return "awaitingReadiness";
|
|
152
|
+
}
|
|
153
|
+
if (action === "shutdown" && outcome === "stopped") {
|
|
154
|
+
return "stopped";
|
|
155
|
+
}
|
|
156
|
+
if (outcome === "accepted") {
|
|
157
|
+
const accepted: Partial<Record<BridgeServiceAction, BridgeServiceState>> = {
|
|
158
|
+
spawn: "starting",
|
|
159
|
+
attach: "attaching",
|
|
160
|
+
restart: "restarting",
|
|
161
|
+
reconnect: "reconnecting",
|
|
162
|
+
};
|
|
163
|
+
const state = accepted[action];
|
|
164
|
+
if (state !== undefined) {
|
|
165
|
+
return state;
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
throw new BridgeSupervisionError("invalid_observation");
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
function parseOutcome(value: unknown): BridgeServiceOutcome {
|
|
172
|
+
if (
|
|
173
|
+
value === "accepted" ||
|
|
174
|
+
value === "ready" ||
|
|
175
|
+
value === "notReady" ||
|
|
176
|
+
value === "stopped"
|
|
177
|
+
) {
|
|
178
|
+
return value;
|
|
179
|
+
}
|
|
180
|
+
if (
|
|
181
|
+
typeof value === "object" &&
|
|
182
|
+
value !== null &&
|
|
183
|
+
!Array.isArray(value) &&
|
|
184
|
+
Object.keys(value).length === 1 &&
|
|
185
|
+
"failed" in value &&
|
|
186
|
+
isFailureCode(value.failed)
|
|
187
|
+
) {
|
|
188
|
+
return { failed: value.failed };
|
|
189
|
+
}
|
|
190
|
+
throw new BridgeSupervisionError("invalid_observation");
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
function isFailureCode(value: unknown): value is BridgeServiceFailureCode {
|
|
194
|
+
return typeof value === "string" &&
|
|
195
|
+
[
|
|
196
|
+
"spawnFailed",
|
|
197
|
+
"attachFailed",
|
|
198
|
+
"readinessFailed",
|
|
199
|
+
"serviceExited",
|
|
200
|
+
"restartFailed",
|
|
201
|
+
"reconnectFailed",
|
|
202
|
+
"shutdownFailed",
|
|
203
|
+
].includes(value);
|
|
204
|
+
}
|
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
import { BRIDGE_MAXIMUM_OPAQUE_ID_BYTES } from "../generated/protocol.ts";
|
|
2
|
+
export type BridgeProtocolValidationCode =
|
|
3
|
+
| "unsupported_protocol_version"
|
|
4
|
+
| "invalid_object"
|
|
5
|
+
| "unknown_field"
|
|
6
|
+
| "missing_field"
|
|
7
|
+
| "invalid_id"
|
|
8
|
+
| "invalid_domain_id"
|
|
9
|
+
| "invalid_number"
|
|
10
|
+
| "invalid_message"
|
|
11
|
+
| "invalid_array"
|
|
12
|
+
| "duplicate_value"
|
|
13
|
+
| "unknown_host_form"
|
|
14
|
+
| "unknown_connection_state"
|
|
15
|
+
| "invalid_connection_status"
|
|
16
|
+
| "unknown_authentication_posture"
|
|
17
|
+
| "unknown_availability"
|
|
18
|
+
| "unknown_read_authority"
|
|
19
|
+
| "unknown_write_authority"
|
|
20
|
+
| "unknown_execution_authority"
|
|
21
|
+
| "invalid_authority_descriptor"
|
|
22
|
+
| "unknown_failure_phase"
|
|
23
|
+
| "unknown_retry_class"
|
|
24
|
+
| "unknown_query_outcome"
|
|
25
|
+
| "unknown_command_outcome"
|
|
26
|
+
| "unknown_deduplication_support"
|
|
27
|
+
| "unknown_job_outcome"
|
|
28
|
+
| "unknown_cancellation_outcome"
|
|
29
|
+
| "authority_without_capability"
|
|
30
|
+
| "multiple_writers"
|
|
31
|
+
| "unrequested_domain";
|
|
32
|
+
|
|
33
|
+
export class BridgeProtocolValidationError extends Error {
|
|
34
|
+
readonly code: BridgeProtocolValidationCode;
|
|
35
|
+
readonly received: unknown;
|
|
36
|
+
|
|
37
|
+
constructor(
|
|
38
|
+
code: BridgeProtocolValidationCode,
|
|
39
|
+
received: unknown,
|
|
40
|
+
) {
|
|
41
|
+
super(`incompatible bridge protocol: ${code}`);
|
|
42
|
+
this.name = "BridgeProtocolValidationError";
|
|
43
|
+
this.code = code;
|
|
44
|
+
this.received = received;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export interface BridgeCodec<T> {
|
|
49
|
+
parse(value: unknown): T;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export function bridgeCodec<T>(
|
|
53
|
+
parse: (value: unknown) => T,
|
|
54
|
+
): BridgeCodec<T> {
|
|
55
|
+
return { parse };
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export function record(
|
|
59
|
+
value: unknown,
|
|
60
|
+
required: readonly string[],
|
|
61
|
+
optional: readonly string[] = [],
|
|
62
|
+
): Record<string, unknown> {
|
|
63
|
+
if (typeof value !== "object" || value === null || Array.isArray(value)) {
|
|
64
|
+
incompatible("invalid_object", value);
|
|
65
|
+
}
|
|
66
|
+
const result = value as Record<string, unknown>;
|
|
67
|
+
const allowed = new Set([...required, ...optional]);
|
|
68
|
+
for (const key of Object.keys(result)) {
|
|
69
|
+
if (!allowed.has(key)) {
|
|
70
|
+
incompatible("unknown_field", { key, value });
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
for (const key of required) {
|
|
74
|
+
if (!(key in result)) {
|
|
75
|
+
incompatible("missing_field", { key, value });
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
return result;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export function array(
|
|
82
|
+
value: unknown,
|
|
83
|
+
maximum: number,
|
|
84
|
+
): readonly unknown[] {
|
|
85
|
+
if (!Array.isArray(value) || value.length > maximum) {
|
|
86
|
+
incompatible("invalid_array", value);
|
|
87
|
+
}
|
|
88
|
+
return value;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
export function unique<T>(
|
|
92
|
+
values: readonly T[],
|
|
93
|
+
key: (value: T) => string,
|
|
94
|
+
): void {
|
|
95
|
+
const seen = new Set<string>();
|
|
96
|
+
for (const value of values) {
|
|
97
|
+
const identity = key(value);
|
|
98
|
+
if (seen.has(identity)) {
|
|
99
|
+
incompatible("duplicate_value", identity);
|
|
100
|
+
}
|
|
101
|
+
seen.add(identity);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
export function opaqueId(value: unknown): string {
|
|
106
|
+
if (
|
|
107
|
+
typeof value !== "string" ||
|
|
108
|
+
new TextEncoder().encode(value).length > BRIDGE_MAXIMUM_OPAQUE_ID_BYTES ||
|
|
109
|
+
!/^[a-z0-9._:-]+$/.test(value)
|
|
110
|
+
) {
|
|
111
|
+
incompatible("invalid_id", value);
|
|
112
|
+
}
|
|
113
|
+
return value;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
export function domainId(value: unknown): string {
|
|
117
|
+
if (
|
|
118
|
+
typeof value !== "string" ||
|
|
119
|
+
new TextEncoder().encode(value).length > BRIDGE_MAXIMUM_OPAQUE_ID_BYTES ||
|
|
120
|
+
!/^[a-z][a-z0-9_-]*(\.[a-z][a-z0-9_-]*)*$/.test(value)
|
|
121
|
+
) {
|
|
122
|
+
incompatible("invalid_domain_id", value);
|
|
123
|
+
}
|
|
124
|
+
return value;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
export function integer(
|
|
128
|
+
value: unknown,
|
|
129
|
+
minimum = 0,
|
|
130
|
+
): number {
|
|
131
|
+
if (
|
|
132
|
+
typeof value !== "number" ||
|
|
133
|
+
!Number.isSafeInteger(value) ||
|
|
134
|
+
value < minimum
|
|
135
|
+
) {
|
|
136
|
+
incompatible("invalid_number", value);
|
|
137
|
+
}
|
|
138
|
+
return value;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
export function oneOf<const T extends string>(
|
|
142
|
+
value: unknown,
|
|
143
|
+
values: readonly T[],
|
|
144
|
+
code: BridgeProtocolValidationCode,
|
|
145
|
+
): T {
|
|
146
|
+
if (typeof value !== "string" || !values.includes(value as T)) {
|
|
147
|
+
incompatible(code, value);
|
|
148
|
+
}
|
|
149
|
+
return value as T;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
export function nullable<T>(
|
|
153
|
+
value: unknown,
|
|
154
|
+
parse: (value: unknown) => T,
|
|
155
|
+
): T | null {
|
|
156
|
+
return value === null ? null : parse(value);
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
export function incompatible(
|
|
160
|
+
code: BridgeProtocolValidationCode,
|
|
161
|
+
received: unknown,
|
|
162
|
+
): never {
|
|
163
|
+
throw new BridgeProtocolValidationError(code, received);
|
|
164
|
+
}
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import { BRIDGE_FIELDS } from "../generated/fields.ts";
|
|
2
|
+
import type {
|
|
3
|
+
BridgeCancellationReceipt,
|
|
4
|
+
BridgeJobTerminalEvent,
|
|
5
|
+
BridgeProgressEvent,
|
|
6
|
+
} from "../generated/protocol.ts";
|
|
7
|
+
import {
|
|
8
|
+
type BridgeCodec,
|
|
9
|
+
incompatible,
|
|
10
|
+
opaqueId,
|
|
11
|
+
record,
|
|
12
|
+
} from "./base.ts";
|
|
13
|
+
import { parseBridgeFailure } from "./operations.ts";
|
|
14
|
+
|
|
15
|
+
export function parseBridgeProgressEvent<P>(
|
|
16
|
+
value: unknown,
|
|
17
|
+
progress: BridgeCodec<P>,
|
|
18
|
+
): BridgeProgressEvent<P> {
|
|
19
|
+
const source = record(value, BRIDGE_FIELDS.BridgeProgressEvent);
|
|
20
|
+
return {
|
|
21
|
+
requestId: opaqueId(source.requestId),
|
|
22
|
+
jobId: opaqueId(source.jobId),
|
|
23
|
+
progress: progress.parse(source.progress),
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export function parseBridgeJobTerminalEvent<S, D>(
|
|
28
|
+
value: unknown,
|
|
29
|
+
success: BridgeCodec<S>,
|
|
30
|
+
details: BridgeCodec<D>,
|
|
31
|
+
): BridgeJobTerminalEvent<S, D> {
|
|
32
|
+
const source = record(value, BRIDGE_FIELDS.BridgeJobTerminalEvent);
|
|
33
|
+
return {
|
|
34
|
+
requestId: opaqueId(source.requestId),
|
|
35
|
+
jobId: opaqueId(source.jobId),
|
|
36
|
+
outcome: parseTerminalOutcome(source.outcome, success, details),
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export function parseBridgeCancellationReceipt<D>(
|
|
41
|
+
value: unknown,
|
|
42
|
+
details: BridgeCodec<D>,
|
|
43
|
+
): BridgeCancellationReceipt<D> {
|
|
44
|
+
const source = record(value, BRIDGE_FIELDS.BridgeCancellationReceipt);
|
|
45
|
+
return {
|
|
46
|
+
requestId: opaqueId(source.requestId),
|
|
47
|
+
targetRequestId: opaqueId(source.targetRequestId),
|
|
48
|
+
jobId: opaqueId(source.jobId),
|
|
49
|
+
outcome: parseCancellationOutcome(source.outcome, details),
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function parseTerminalOutcome<S, D>(
|
|
54
|
+
value: unknown,
|
|
55
|
+
success: BridgeCodec<S>,
|
|
56
|
+
details: BridgeCodec<D>,
|
|
57
|
+
): BridgeJobTerminalEvent<S, D>["outcome"] {
|
|
58
|
+
if (value === "cancelled") {
|
|
59
|
+
return value;
|
|
60
|
+
}
|
|
61
|
+
const source = singleVariant(
|
|
62
|
+
value,
|
|
63
|
+
["succeeded", "failed"],
|
|
64
|
+
"unknown_job_outcome",
|
|
65
|
+
);
|
|
66
|
+
if ("succeeded" in source) {
|
|
67
|
+
return { succeeded: success.parse(source.succeeded) };
|
|
68
|
+
}
|
|
69
|
+
return { failed: parseBridgeFailure(source.failed, details) };
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function parseCancellationOutcome<D>(
|
|
73
|
+
value: unknown,
|
|
74
|
+
details: BridgeCodec<D>,
|
|
75
|
+
): BridgeCancellationReceipt<D>["outcome"] {
|
|
76
|
+
if (
|
|
77
|
+
value === "accepted" ||
|
|
78
|
+
value === "alreadyTerminal" ||
|
|
79
|
+
value === "unknown"
|
|
80
|
+
) {
|
|
81
|
+
return value;
|
|
82
|
+
}
|
|
83
|
+
const source = singleVariant(
|
|
84
|
+
value,
|
|
85
|
+
["rejected"],
|
|
86
|
+
"unknown_cancellation_outcome",
|
|
87
|
+
);
|
|
88
|
+
return { rejected: parseBridgeFailure(source.rejected, details) };
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function singleVariant(
|
|
92
|
+
value: unknown,
|
|
93
|
+
keys: readonly string[],
|
|
94
|
+
code: "unknown_job_outcome" | "unknown_cancellation_outcome",
|
|
95
|
+
): Record<string, unknown> {
|
|
96
|
+
if (typeof value !== "object" || value === null || Array.isArray(value)) {
|
|
97
|
+
incompatible(code, value);
|
|
98
|
+
}
|
|
99
|
+
const source = value as Record<string, unknown>;
|
|
100
|
+
const present = Object.keys(source);
|
|
101
|
+
if (present.length !== 1 || !keys.includes(present[0]!)) {
|
|
102
|
+
incompatible(code, value);
|
|
103
|
+
}
|
|
104
|
+
return source;
|
|
105
|
+
}
|