@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,22 @@
|
|
|
1
|
+
import type { BridgeHelloRequest } from "./generated/protocol.ts";
|
|
2
|
+
import type { BridgeOperationDescriptor } from "./operation.ts";
|
|
3
|
+
import { BridgeHostRouter } from "./router.ts";
|
|
4
|
+
|
|
5
|
+
export class DirectBridgeAdapter {
|
|
6
|
+
readonly #router: BridgeHostRouter;
|
|
7
|
+
|
|
8
|
+
constructor(router: BridgeHostRouter) {
|
|
9
|
+
this.#router = router;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
negotiate(request: BridgeHelloRequest): Promise<unknown> {
|
|
13
|
+
return this.#router.negotiate(request);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
invoke<Request, Reply>(
|
|
17
|
+
operation: BridgeOperationDescriptor<Request, Reply>,
|
|
18
|
+
request: Request,
|
|
19
|
+
): Promise<unknown> {
|
|
20
|
+
return this.#router.invoke(operation.route, request);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
// @generated by `effigy generate:bridge`; do not edit.
|
|
2
|
+
// Allowed field names per protocol type, from the Rust structs.
|
|
3
|
+
// A `record()` given one of these rejects unknown and missing keys —
|
|
4
|
+
// contract 010's Boundary Validation Target.
|
|
5
|
+
|
|
6
|
+
export const BRIDGE_FIELDS: Record<string, readonly string[]> = {
|
|
7
|
+
"BridgeHostDescriptor": ["hostInstanceId", "form"],
|
|
8
|
+
"BridgeConnectionStatus": ["state", "reason"],
|
|
9
|
+
"DomainAuthorityDescriptor": ["domainId", "scopeId", "availability", "readAuthority", "writeAuthority", "executionAuthority", "authorityEpoch", "authoritativeRevision"],
|
|
10
|
+
"DomainCapabilityDescriptor": ["domainId", "capabilities"],
|
|
11
|
+
"BridgeDiagnostic": ["diagnosticId", "message"],
|
|
12
|
+
"BridgeHelloRequest": ["protocolVersion", "bridgeId", "requestedDomains"],
|
|
13
|
+
"BridgeNegotiationReceipt": ["protocolVersion", "host", "sessionId", "connection", "authentication", "transportFeatures", "domainCapabilities", "domainAuthorities", "diagnostics"],
|
|
14
|
+
"BridgeRequestContext": ["requestId", "sessionId", "domainId"],
|
|
15
|
+
"BridgeQueryEnvelope": ["context", "payload"],
|
|
16
|
+
"BridgeCommandEnvelope": ["context", "authorityEpoch", "expectedRevision", "idempotencyKey", "payload"],
|
|
17
|
+
"BridgeCancellationRequest": ["context", "targetRequestId", "jobId"],
|
|
18
|
+
"BridgeFailure": ["code", "message", "retryClass", "phase", "details"],
|
|
19
|
+
"BridgeQueryReply": ["requestId", "outcome"],
|
|
20
|
+
"BridgeCommandReply": ["requestId", "authoritativeRevision", "outcome"],
|
|
21
|
+
"BridgeReconnectSchedule": ["attempt", "retryClass", "notBefore"],
|
|
22
|
+
"BridgeConnectionTransitionReceipt": ["sequence", "at", "previous", "current", "sessionId", "reconnect"],
|
|
23
|
+
"BridgeAuthorityRequirement": ["domainId", "authority"],
|
|
24
|
+
"BridgeStreamCursor": ["sessionId", "domainId", "authorityEpoch", "sequence"],
|
|
25
|
+
"BridgeSnapshotEnvelope": ["cursor", "payload"],
|
|
26
|
+
"BridgeEventEnvelope": ["cursor", "payload"],
|
|
27
|
+
"BridgeProgressEvent": ["requestId", "jobId", "progress"],
|
|
28
|
+
"BridgeJobTerminalEvent": ["requestId", "jobId", "outcome"],
|
|
29
|
+
"BridgeCancellationReceipt": ["requestId", "targetRequestId", "jobId", "outcome"],
|
|
30
|
+
"BridgeServiceRequest": ["action", "credentialRef"],
|
|
31
|
+
"BridgeServiceTransitionReceipt": ["generation", "ownership", "action", "previous", "current", "outcome"],
|
|
32
|
+
};
|
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
// @generated by `effigy generate:bridge`; do not edit.
|
|
2
|
+
// Rust serde types are the wire authority.
|
|
3
|
+
|
|
4
|
+
export const BRIDGE_PROTOCOL_VERSION = 1 as const;
|
|
5
|
+
export const BRIDGE_MAXIMUM_OPAQUE_ID_BYTES = 128 as const;
|
|
6
|
+
// Wire-visible bounds. The Rust constants are the authority; a
|
|
7
|
+
// hand-copied literal in a validator is drift waiting to happen.
|
|
8
|
+
export const BRIDGE_MAXIMUM_REQUESTED_DOMAINS = 256 as const;
|
|
9
|
+
export const BRIDGE_MAXIMUM_CAPABILITY_DOMAINS = 256 as const;
|
|
10
|
+
export const BRIDGE_MAXIMUM_AUTHORITY_DOMAINS = 256 as const;
|
|
11
|
+
export const BRIDGE_MAXIMUM_CAPABILITIES_PER_DOMAIN = 128 as const;
|
|
12
|
+
export const BRIDGE_MAXIMUM_TRANSPORT_FEATURES = 128 as const;
|
|
13
|
+
export const BRIDGE_MAXIMUM_DIAGNOSTICS = 64 as const;
|
|
14
|
+
export const BRIDGE_MAXIMUM_DIAGNOSTIC_MESSAGE_BYTES = 4096 as const;
|
|
15
|
+
export const BRIDGE_MAXIMUM_FAILURE_MESSAGE_BYTES = 4096 as const;
|
|
16
|
+
export const BRIDGE_MAXIMUM_DEDUPLICATION_ENTRIES = 65536 as const;
|
|
17
|
+
export const BRIDGE_HOST_FORMS = ["direct","tauriLocal","localService","remote","localFirst"] as const;
|
|
18
|
+
export const BRIDGE_CONNECTION_STATES = ["idle","connecting","negotiating","ready","degraded","reconnecting","offline","incompatible","unauthorized","failed","closed"] as const;
|
|
19
|
+
export const BRIDGE_CONNECTION_REASONS = ["connectRequested","transportReady","negotiationAccepted","capabilityChanged","transportLost","retryScheduled","versionMismatch","authorizationRejected","hostFailure","shutdown"] as const;
|
|
20
|
+
export const BRIDGE_AUTHENTICATION_POSTURES = ["notRequired","required","authenticated","rejected"] as const;
|
|
21
|
+
export const BRIDGE_DOMAIN_AVAILABILITIES = ["available","degraded","offline"] as const;
|
|
22
|
+
export const BRIDGE_READ_AUTHORITIES = ["none","projection","authoritative"] as const;
|
|
23
|
+
export const BRIDGE_WRITE_AUTHORITIES = ["none","authoritative"] as const;
|
|
24
|
+
export const BRIDGE_EXECUTION_AUTHORITIES = ["none","executor"] as const;
|
|
25
|
+
export const BRIDGE_FAILURE_PHASES = ["admission","authorization","dispatch","execution","publication","response","transport"] as const;
|
|
26
|
+
export const BRIDGE_RETRY_CLASSES = ["never","afterBackoff","afterReconnect","afterAuthorityRefresh"] as const;
|
|
27
|
+
export const BRIDGE_COMMAND_DELIVERIES = ["notDispatched","uncertain"] as const;
|
|
28
|
+
export const BRIDGE_COMMAND_RETRY_DECISIONS = ["doNotRetry","retrySameRequest","indeterminate"] as const;
|
|
29
|
+
export const BRIDGE_QUERY_RETRY_DECISIONS = ["doNotRetry","retry"] as const;
|
|
30
|
+
export const BRIDGE_REQUIRED_AUTHORITIES = ["available","readable","authoritativeRead","writable","executable"] as const;
|
|
31
|
+
export const BRIDGE_AUTHORITY_CURSOR_DECISIONS = ["current","supersededSession","staleAuthority","refreshAuthority","unknownDomain"] as const;
|
|
32
|
+
export const BRIDGE_SERVICE_OWNERSHIPS = ["ownedLocal","externalLocal","externalRemote"] as const;
|
|
33
|
+
export const BRIDGE_SERVICE_STATES = ["absent","starting","attaching","awaitingReadiness","ready","restarting","reconnecting","stopped","failed"] as const;
|
|
34
|
+
export const BRIDGE_SERVICE_ACTIONS = ["spawn","attach","checkReadiness","restart","reconnect","shutdown"] as const;
|
|
35
|
+
export const BRIDGE_SERVICE_FAILURE_CODES = ["spawnFailed","attachFailed","readinessFailed","serviceExited","restartFailed","reconnectFailed","shutdownFailed"] as const;
|
|
36
|
+
export const BRIDGE_SNAPSHOT_DECISIONS = ["accepted","acceptedResnapshotRequired","supersededSession","wrongDomain"] as const;
|
|
37
|
+
export const BRIDGE_STREAM_DECISIONS = ["apply","ignoreDuplicate","ignoreStale","ignoreSupersededSession","ignoreWrongDomain","resnapshotGap","resnapshotNewEpoch","resnapshotRequired"] as const;
|
|
38
|
+
export const BRIDGE_PROGRESS_DECISIONS = ["accept","ignoreWrongCorrelation","ignoreAfterTerminal"] as const;
|
|
39
|
+
export const BRIDGE_TERMINAL_DECISIONS = ["accept","ignoreWrongCorrelation","ignoreAlreadyTerminal"] as const;
|
|
40
|
+
|
|
41
|
+
export type BridgeId = string;
|
|
42
|
+
|
|
43
|
+
export type BridgeSessionId = string;
|
|
44
|
+
|
|
45
|
+
export type HostInstanceId = string;
|
|
46
|
+
|
|
47
|
+
export type BridgeCapabilityId = string;
|
|
48
|
+
|
|
49
|
+
export type AuthorityScopeId = string;
|
|
50
|
+
|
|
51
|
+
export type TransportFeatureId = string;
|
|
52
|
+
|
|
53
|
+
export type BridgeDiagnosticId = string;
|
|
54
|
+
|
|
55
|
+
export type BridgeRequestId = string;
|
|
56
|
+
|
|
57
|
+
export type BridgeIdempotencyKey = string;
|
|
58
|
+
|
|
59
|
+
export type BridgeJobId = string;
|
|
60
|
+
|
|
61
|
+
export type BridgeErrorCode = string;
|
|
62
|
+
|
|
63
|
+
export type BridgeCredentialRef = string;
|
|
64
|
+
|
|
65
|
+
export type DomainId = string;
|
|
66
|
+
|
|
67
|
+
export type BridgeProtocolVersion = number;
|
|
68
|
+
|
|
69
|
+
export type BridgeHostForm = "direct" | "tauriLocal" | "localService" | "remote" | "localFirst";
|
|
70
|
+
|
|
71
|
+
export type BridgeHostDescriptor = {
|
|
72
|
+
/**
|
|
73
|
+
* Identity of one running host, independent of connection sessions.
|
|
74
|
+
*/
|
|
75
|
+
hostInstanceId: HostInstanceId,
|
|
76
|
+
/**
|
|
77
|
+
* Product-neutral form of this host.
|
|
78
|
+
*/
|
|
79
|
+
form: BridgeHostForm, };
|
|
80
|
+
|
|
81
|
+
export type BridgeConnectionState = "idle" | "connecting" | "negotiating" | "ready" | "degraded" | "reconnecting" | "offline" | "incompatible" | "unauthorized" | "failed" | "closed";
|
|
82
|
+
|
|
83
|
+
export type BridgeConnectionReason = "connectRequested" | "transportReady" | "negotiationAccepted" | "capabilityChanged" | "transportLost" | "retryScheduled" | "versionMismatch" | "authorizationRejected" | "hostFailure" | "shutdown";
|
|
84
|
+
|
|
85
|
+
export type BridgeConnectionStatus = { state: BridgeConnectionState, reason: BridgeConnectionReason | null, };
|
|
86
|
+
|
|
87
|
+
export type AuthenticationPosture = "notRequired" | "required" | "authenticated" | "rejected";
|
|
88
|
+
|
|
89
|
+
export type AuthorityEpoch = number;
|
|
90
|
+
|
|
91
|
+
export type AuthorityRevision = number;
|
|
92
|
+
|
|
93
|
+
export type DomainAvailability = "available" | "degraded" | "offline";
|
|
94
|
+
|
|
95
|
+
export type ReadAuthority = "none" | "projection" | "authoritative";
|
|
96
|
+
|
|
97
|
+
export type WriteAuthority = "none" | "authoritative";
|
|
98
|
+
|
|
99
|
+
export type ExecutionAuthority = "none" | "executor";
|
|
100
|
+
|
|
101
|
+
export type DomainAuthorityDescriptor = { domainId: DomainId, scopeId: AuthorityScopeId, availability: DomainAvailability, readAuthority: ReadAuthority, writeAuthority: WriteAuthority, executionAuthority: ExecutionAuthority, authorityEpoch: AuthorityEpoch, authoritativeRevision: AuthorityRevision | null, };
|
|
102
|
+
|
|
103
|
+
export type DomainCapabilityDescriptor = { domainId: DomainId, capabilities: Array<BridgeCapabilityId>, };
|
|
104
|
+
|
|
105
|
+
export type BridgeDiagnostic = { diagnosticId: BridgeDiagnosticId, message: string, };
|
|
106
|
+
|
|
107
|
+
export type BridgeHelloRequest = { protocolVersion: BridgeProtocolVersion, bridgeId: BridgeId, requestedDomains: Array<DomainId>, };
|
|
108
|
+
|
|
109
|
+
export type BridgeNegotiationReceipt = { protocolVersion: BridgeProtocolVersion, host: BridgeHostDescriptor, sessionId: BridgeSessionId, connection: BridgeConnectionStatus, authentication: AuthenticationPosture, transportFeatures: Array<TransportFeatureId>, domainCapabilities: Array<DomainCapabilityDescriptor>, domainAuthorities: Array<DomainAuthorityDescriptor>, diagnostics: Array<BridgeDiagnostic>, };
|
|
110
|
+
|
|
111
|
+
export type BridgeRequestContext = { requestId: BridgeRequestId, sessionId: BridgeSessionId, domainId: DomainId, };
|
|
112
|
+
|
|
113
|
+
export type BridgeQueryEnvelope<P> = { context: BridgeRequestContext, payload: P, };
|
|
114
|
+
|
|
115
|
+
export type BridgeCommandEnvelope<P> = { context: BridgeRequestContext, authorityEpoch: AuthorityEpoch, expectedRevision: AuthorityRevision | null, idempotencyKey: BridgeIdempotencyKey | null, payload: P, };
|
|
116
|
+
|
|
117
|
+
export type BridgeCancellationRequest = { context: BridgeRequestContext, targetRequestId: BridgeRequestId, jobId: BridgeJobId, };
|
|
118
|
+
|
|
119
|
+
export type BridgeFailureMessage = string;
|
|
120
|
+
|
|
121
|
+
export type BridgeFailurePhase = "admission" | "authorization" | "dispatch" | "execution" | "publication" | "response" | "transport";
|
|
122
|
+
|
|
123
|
+
export type BridgeRetryClass = "never" | "afterBackoff" | "afterReconnect" | "afterAuthorityRefresh";
|
|
124
|
+
|
|
125
|
+
export type BridgeFailure<D> = { code: BridgeErrorCode, message: BridgeFailureMessage, retryClass: BridgeRetryClass, phase: BridgeFailurePhase, details: D | null, };
|
|
126
|
+
|
|
127
|
+
export type BridgeQueryOutcome<S, D> = { "success": S } | { "rejected": BridgeFailure<D> };
|
|
128
|
+
|
|
129
|
+
export type BridgeQueryReply<S, D> = { requestId: BridgeRequestId, outcome: BridgeQueryOutcome<S, D>, };
|
|
130
|
+
|
|
131
|
+
export type BridgeCommandOutcome<S, D> = { "applied": S } | { "rejected": BridgeFailure<D> } | { "indeterminate": BridgeFailure<D> };
|
|
132
|
+
|
|
133
|
+
export type BridgeCommandReply<S, D> = { requestId: BridgeRequestId, authoritativeRevision: AuthorityRevision | null, outcome: BridgeCommandOutcome<S, D>, };
|
|
134
|
+
|
|
135
|
+
export type BridgeDeduplicationCapacity = number;
|
|
136
|
+
|
|
137
|
+
export type BridgeDeduplicationSupport = "unsupported" | { "finite": BridgeDeduplicationCapacity };
|
|
138
|
+
|
|
139
|
+
export type BridgeCommandDelivery = "notDispatched" | "uncertain";
|
|
140
|
+
|
|
141
|
+
export type BridgeCommandRetryDecision = "doNotRetry" | "retrySameRequest" | "indeterminate";
|
|
142
|
+
|
|
143
|
+
export type BridgeQueryRetryDecision = "doNotRetry" | "retry";
|
|
144
|
+
|
|
145
|
+
export type BridgeMonotonicMillis = number;
|
|
146
|
+
|
|
147
|
+
export type BridgeRetryAttempt = number;
|
|
148
|
+
|
|
149
|
+
export type BridgeReconnectSchedule = { attempt: BridgeRetryAttempt, retryClass: BridgeRetryClass, notBefore: BridgeMonotonicMillis, };
|
|
150
|
+
|
|
151
|
+
export type BridgeTransitionSequence = number;
|
|
152
|
+
|
|
153
|
+
export type BridgeConnectionTransitionReceipt = { sequence: BridgeTransitionSequence, at: BridgeMonotonicMillis, previous: BridgeConnectionStatus, current: BridgeConnectionStatus, sessionId: BridgeSessionId | null, reconnect: BridgeReconnectSchedule | null, };
|
|
154
|
+
|
|
155
|
+
export type BridgeRequiredAuthority = "available" | "readable" | "authoritativeRead" | "writable" | "executable";
|
|
156
|
+
|
|
157
|
+
export type BridgeAuthorityRequirement = { domainId: DomainId, authority: BridgeRequiredAuthority, };
|
|
158
|
+
|
|
159
|
+
export type BridgeAuthorityCursorDecision = "current" | "supersededSession" | "staleAuthority" | "refreshAuthority" | "unknownDomain";
|
|
160
|
+
|
|
161
|
+
export type BridgeStreamSequence = number;
|
|
162
|
+
|
|
163
|
+
export type BridgeStreamCursor = { sessionId: BridgeSessionId, domainId: DomainId, authorityEpoch: AuthorityEpoch, sequence: BridgeStreamSequence, };
|
|
164
|
+
|
|
165
|
+
export type BridgeSnapshotEnvelope<P> = { cursor: BridgeStreamCursor, payload: P, };
|
|
166
|
+
|
|
167
|
+
export type BridgeEventEnvelope<P> = { cursor: BridgeStreamCursor, payload: P, };
|
|
168
|
+
|
|
169
|
+
export type BridgeSnapshotDecision = "accepted" | "acceptedResnapshotRequired" | "supersededSession" | "wrongDomain";
|
|
170
|
+
|
|
171
|
+
export type BridgeStreamDecision = "apply" | "ignoreDuplicate" | "ignoreStale" | "ignoreSupersededSession" | "ignoreWrongDomain" | "resnapshotGap" | "resnapshotNewEpoch" | "resnapshotRequired";
|
|
172
|
+
|
|
173
|
+
export type BridgeProgressEvent<P> = { requestId: BridgeRequestId, jobId: BridgeJobId, progress: P, };
|
|
174
|
+
|
|
175
|
+
export type BridgeJobTerminalOutcome<S, D> = { "succeeded": S } | { "failed": BridgeFailure<D> } | "cancelled";
|
|
176
|
+
|
|
177
|
+
export type BridgeJobTerminalEvent<S, D> = { requestId: BridgeRequestId, jobId: BridgeJobId, outcome: BridgeJobTerminalOutcome<S, D>, };
|
|
178
|
+
|
|
179
|
+
export type BridgeCancellationOutcome<D> = "accepted" | "alreadyTerminal" | "unknown" | { "rejected": BridgeFailure<D> };
|
|
180
|
+
|
|
181
|
+
export type BridgeCancellationReceipt<D> = { requestId: BridgeRequestId, targetRequestId: BridgeRequestId, jobId: BridgeJobId, outcome: BridgeCancellationOutcome<D>, };
|
|
182
|
+
|
|
183
|
+
export type BridgeProgressDecision = "accept" | "ignoreWrongCorrelation" | "ignoreAfterTerminal";
|
|
184
|
+
|
|
185
|
+
export type BridgeJobTerminalDecision = "accept" | "ignoreWrongCorrelation" | "ignoreAlreadyTerminal";
|
|
186
|
+
|
|
187
|
+
export type BridgeServiceOwnership = "ownedLocal" | "externalLocal" | "externalRemote";
|
|
188
|
+
|
|
189
|
+
export type BridgeServiceState = "absent" | "starting" | "attaching" | "awaitingReadiness" | "ready" | "restarting" | "reconnecting" | "stopped" | "failed";
|
|
190
|
+
|
|
191
|
+
export type BridgeServiceAction = "spawn" | "attach" | "checkReadiness" | "restart" | "reconnect" | "shutdown";
|
|
192
|
+
|
|
193
|
+
export type BridgeServiceFailureCode = "spawnFailed" | "attachFailed" | "readinessFailed" | "serviceExited" | "restartFailed" | "reconnectFailed" | "shutdownFailed";
|
|
194
|
+
|
|
195
|
+
export type BridgeServiceOutcome = "accepted" | "ready" | "notReady" | "stopped" | { "failed": BridgeServiceFailureCode };
|
|
196
|
+
|
|
197
|
+
export type BridgeServiceRequest = { action: BridgeServiceAction, credentialRef: BridgeCredentialRef | null, };
|
|
198
|
+
|
|
199
|
+
export type BridgeServiceGeneration = number;
|
|
200
|
+
|
|
201
|
+
export type BridgeServiceTransitionReceipt = { generation: BridgeServiceGeneration, ownership: BridgeServiceOwnership, action: BridgeServiceAction, previous: BridgeServiceState, current: BridgeServiceState, outcome: BridgeServiceOutcome, };
|
|
202
|
+
export const BRIDGE_ADMITTED_CONNECTION_REASONS: Record<
|
|
203
|
+
BridgeConnectionState,
|
|
204
|
+
readonly (BridgeConnectionReason | null)[]
|
|
205
|
+
> = {
|
|
206
|
+
"idle": [null],
|
|
207
|
+
"connecting": ["connectRequested"],
|
|
208
|
+
"negotiating": ["transportReady"],
|
|
209
|
+
"ready": ["negotiationAccepted", "capabilityChanged"],
|
|
210
|
+
"degraded": ["capabilityChanged", "transportLost", "hostFailure"],
|
|
211
|
+
"reconnecting": ["retryScheduled", "transportLost"],
|
|
212
|
+
"offline": ["transportLost"],
|
|
213
|
+
"incompatible": ["versionMismatch"],
|
|
214
|
+
"unauthorized": ["authorizationRejected"],
|
|
215
|
+
"failed": ["hostFailure"],
|
|
216
|
+
"closed": ["shutdown"],
|
|
217
|
+
};
|
|
218
|
+
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
// @generated by `effigy generate:bridge`; do not edit.
|
|
2
|
+
// Allowed field names per tagged-union variant, from the Rust enums.
|
|
3
|
+
// Keyed by type, then by discriminant. The discriminant itself is in
|
|
4
|
+
// each list, because `exact()` compares every key.
|
|
5
|
+
|
|
6
|
+
export const BRIDGE_VARIANT_FIELDS: Record<string, Record<string, readonly string[]>> = {
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
export const BRIDGE_VARIANT_FIELDS_DISCRIMINANTS: Record<string, string> = {
|
|
10
|
+
};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export * from "./generated/protocol.ts";
|
|
2
|
+
// The generated field map is public API. A consumer that mirrors these
|
|
3
|
+
// types structurally -- Poodle does, because it may not depend on Longhorn --
|
|
4
|
+
// has no compile-time link to them, so a field added here is silent on their
|
|
5
|
+
// side. Whoever sees both can assert their shape against this map and fail
|
|
6
|
+
// when it drifts.
|
|
7
|
+
export * from "./generated/fields.ts";
|
|
8
|
+
export * from "./validation.ts";
|
|
9
|
+
export * from "./session.ts";
|
|
10
|
+
export * from "./operation.ts";
|
|
11
|
+
export * from "./runtime.ts";
|
|
12
|
+
export * from "./job.ts";
|
|
13
|
+
export * from "./router.ts";
|
|
14
|
+
export * from "./direct.ts";
|
|
15
|
+
export * from "./loopback.ts";
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
BridgeJobId,
|
|
3
|
+
BridgeJobTerminalDecision,
|
|
4
|
+
BridgeJobTerminalEvent,
|
|
5
|
+
BridgeProgressDecision,
|
|
6
|
+
BridgeProgressEvent,
|
|
7
|
+
BridgeRequestId,
|
|
8
|
+
} from "./generated/protocol.ts";
|
|
9
|
+
|
|
10
|
+
export class BridgeJobTracker {
|
|
11
|
+
readonly #requestId: BridgeRequestId;
|
|
12
|
+
readonly #jobId: BridgeJobId;
|
|
13
|
+
#terminal = false;
|
|
14
|
+
|
|
15
|
+
constructor(requestId: BridgeRequestId, jobId: BridgeJobId) {
|
|
16
|
+
this.#requestId = requestId;
|
|
17
|
+
this.#jobId = jobId;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
classifyProgress<P>(
|
|
21
|
+
event: BridgeProgressEvent<P>,
|
|
22
|
+
): BridgeProgressDecision {
|
|
23
|
+
if (
|
|
24
|
+
event.requestId !== this.#requestId ||
|
|
25
|
+
event.jobId !== this.#jobId
|
|
26
|
+
) {
|
|
27
|
+
return "ignoreWrongCorrelation";
|
|
28
|
+
}
|
|
29
|
+
return this.#terminal ? "ignoreAfterTerminal" : "accept";
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
classifyTerminal<S, D>(
|
|
33
|
+
event: BridgeJobTerminalEvent<S, D>,
|
|
34
|
+
): BridgeJobTerminalDecision {
|
|
35
|
+
if (
|
|
36
|
+
event.requestId !== this.#requestId ||
|
|
37
|
+
event.jobId !== this.#jobId
|
|
38
|
+
) {
|
|
39
|
+
return "ignoreWrongCorrelation";
|
|
40
|
+
}
|
|
41
|
+
if (this.#terminal) {
|
|
42
|
+
return "ignoreAlreadyTerminal";
|
|
43
|
+
}
|
|
44
|
+
this.#terminal = true;
|
|
45
|
+
return "accept";
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
isTerminal(): boolean {
|
|
49
|
+
return this.#terminal;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import type { BridgeHelloRequest } from "./generated/protocol.ts";
|
|
2
|
+
import type { BridgeOperationDescriptor } from "./operation.ts";
|
|
3
|
+
import { BridgeHostRouter } from "./router.ts";
|
|
4
|
+
|
|
5
|
+
export type BridgeSerializationPhase =
|
|
6
|
+
| "negotiation_request"
|
|
7
|
+
| "negotiation_reply"
|
|
8
|
+
| "operation_request"
|
|
9
|
+
| "operation_reply"
|
|
10
|
+
| "snapshot"
|
|
11
|
+
| "event";
|
|
12
|
+
|
|
13
|
+
export class BridgeSerializationError extends Error {
|
|
14
|
+
readonly phase: BridgeSerializationPhase;
|
|
15
|
+
override readonly cause: unknown;
|
|
16
|
+
|
|
17
|
+
constructor(phase: BridgeSerializationPhase, cause: unknown) {
|
|
18
|
+
super(`bridge serialization failed: ${phase}`, { cause });
|
|
19
|
+
this.name = "BridgeSerializationError";
|
|
20
|
+
this.phase = phase;
|
|
21
|
+
this.cause = cause;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export class SerializedLoopbackBridgeAdapter {
|
|
26
|
+
readonly #router: BridgeHostRouter;
|
|
27
|
+
|
|
28
|
+
constructor(router: BridgeHostRouter) {
|
|
29
|
+
this.#router = router;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
async negotiate(request: BridgeHelloRequest): Promise<unknown> {
|
|
33
|
+
const encodedRequest = serialize(
|
|
34
|
+
request,
|
|
35
|
+
"negotiation_request",
|
|
36
|
+
);
|
|
37
|
+
const reply = await this.#router.negotiate(JSON.parse(encodedRequest));
|
|
38
|
+
return deserialize(
|
|
39
|
+
serialize(reply, "negotiation_reply"),
|
|
40
|
+
"negotiation_reply",
|
|
41
|
+
);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
async invoke<Request, Reply>(
|
|
45
|
+
operation: BridgeOperationDescriptor<Request, Reply>,
|
|
46
|
+
request: Request,
|
|
47
|
+
): Promise<unknown> {
|
|
48
|
+
const encodedRequest = serialize(request, "operation_request");
|
|
49
|
+
const reply = await this.#router.invoke(
|
|
50
|
+
operation.route,
|
|
51
|
+
JSON.parse(encodedRequest),
|
|
52
|
+
);
|
|
53
|
+
return deserialize(
|
|
54
|
+
serialize(reply, "operation_reply"),
|
|
55
|
+
"operation_reply",
|
|
56
|
+
);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export function serialize(
|
|
61
|
+
value: unknown,
|
|
62
|
+
phase: BridgeSerializationPhase,
|
|
63
|
+
): string {
|
|
64
|
+
try {
|
|
65
|
+
const encoded = JSON.stringify(value);
|
|
66
|
+
if (encoded === undefined) {
|
|
67
|
+
throw new TypeError("value has no JSON representation");
|
|
68
|
+
}
|
|
69
|
+
return encoded;
|
|
70
|
+
} catch (error) {
|
|
71
|
+
throw new BridgeSerializationError(phase, error);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export function deserialize(
|
|
76
|
+
value: string,
|
|
77
|
+
phase: BridgeSerializationPhase,
|
|
78
|
+
): unknown {
|
|
79
|
+
try {
|
|
80
|
+
return JSON.parse(value);
|
|
81
|
+
} catch (error) {
|
|
82
|
+
throw new BridgeSerializationError(phase, error);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
BridgeCommandDelivery,
|
|
3
|
+
BridgeCommandEnvelope,
|
|
4
|
+
BridgeCommandRetryDecision,
|
|
5
|
+
BridgeDeduplicationSupport,
|
|
6
|
+
BridgeQueryEnvelope,
|
|
7
|
+
BridgeQueryRetryDecision,
|
|
8
|
+
BridgeRetryClass,
|
|
9
|
+
DomainId,
|
|
10
|
+
} from "./generated/protocol.ts";
|
|
11
|
+
import type { BridgeCodec } from "./validation.ts";
|
|
12
|
+
import { BridgeSession } from "./session.ts";
|
|
13
|
+
|
|
14
|
+
export type BridgeOperationKind = "query" | "command" | "cancellation";
|
|
15
|
+
|
|
16
|
+
export interface BridgeOperationDescriptor<Request, Reply> {
|
|
17
|
+
readonly route: string;
|
|
18
|
+
readonly domainId: DomainId;
|
|
19
|
+
readonly kind: BridgeOperationKind;
|
|
20
|
+
readonly requiredCapability: string;
|
|
21
|
+
readonly request: BridgeCodec<Request>;
|
|
22
|
+
readonly reply: BridgeCodec<Reply>;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export interface BridgeOperationAdapter {
|
|
26
|
+
invoke<Request, Reply>(
|
|
27
|
+
operation: BridgeOperationDescriptor<Request, Reply>,
|
|
28
|
+
request: Request,
|
|
29
|
+
): Promise<unknown>;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export class BridgeDomainClient {
|
|
33
|
+
readonly #session: BridgeSession;
|
|
34
|
+
readonly #adapter: BridgeOperationAdapter;
|
|
35
|
+
|
|
36
|
+
constructor(session: BridgeSession, adapter: BridgeOperationAdapter) {
|
|
37
|
+
this.#session = session;
|
|
38
|
+
this.#adapter = adapter;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
async execute<Request, Reply>(
|
|
42
|
+
operation: BridgeOperationDescriptor<Request, Reply>,
|
|
43
|
+
request: Request,
|
|
44
|
+
): Promise<Reply> {
|
|
45
|
+
const checkedRequest = operation.request.parse(request);
|
|
46
|
+
this.#assertAuthority(operation, checkedRequest);
|
|
47
|
+
return operation.reply.parse(
|
|
48
|
+
await this.#adapter.invoke(operation, checkedRequest),
|
|
49
|
+
);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
#assertAuthority<Request, Reply>(
|
|
53
|
+
operation: BridgeOperationDescriptor<Request, Reply>,
|
|
54
|
+
request: Request,
|
|
55
|
+
): void {
|
|
56
|
+
if (
|
|
57
|
+
!this.#session.supports(
|
|
58
|
+
operation.domainId,
|
|
59
|
+
operation.requiredCapability,
|
|
60
|
+
)
|
|
61
|
+
) {
|
|
62
|
+
throw new BridgeAuthorityError(
|
|
63
|
+
"capability_unavailable",
|
|
64
|
+
operation.domainId,
|
|
65
|
+
);
|
|
66
|
+
}
|
|
67
|
+
const authority = this.#session.authority(operation.domainId);
|
|
68
|
+
const context = (
|
|
69
|
+
request as {
|
|
70
|
+
readonly context?: {
|
|
71
|
+
readonly domainId?: unknown;
|
|
72
|
+
readonly sessionId?: unknown;
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
).context;
|
|
76
|
+
if (context?.domainId !== operation.domainId) {
|
|
77
|
+
throw new BridgeAuthorityError(
|
|
78
|
+
"request_domain_mismatch",
|
|
79
|
+
operation.domainId,
|
|
80
|
+
);
|
|
81
|
+
}
|
|
82
|
+
if (context.sessionId !== this.#session.receipt.sessionId) {
|
|
83
|
+
throw new BridgeAuthorityError(
|
|
84
|
+
"request_session_mismatch",
|
|
85
|
+
operation.domainId,
|
|
86
|
+
);
|
|
87
|
+
}
|
|
88
|
+
if (authority === undefined || authority.availability === "offline") {
|
|
89
|
+
throw new BridgeAuthorityError(
|
|
90
|
+
"authority_unavailable",
|
|
91
|
+
operation.domainId,
|
|
92
|
+
);
|
|
93
|
+
}
|
|
94
|
+
if (operation.kind === "query" && authority.readAuthority === "none") {
|
|
95
|
+
throw new BridgeAuthorityError("read_denied", operation.domainId);
|
|
96
|
+
}
|
|
97
|
+
if (operation.kind === "command") {
|
|
98
|
+
if (authority.writeAuthority !== "authoritative") {
|
|
99
|
+
throw new BridgeAuthorityError("write_denied", operation.domainId);
|
|
100
|
+
}
|
|
101
|
+
const envelope = request as BridgeCommandEnvelope<unknown>;
|
|
102
|
+
if (envelope.authorityEpoch !== authority.authorityEpoch) {
|
|
103
|
+
throw new BridgeAuthorityError("stale_authority", operation.domainId);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
if (
|
|
107
|
+
operation.kind === "cancellation" &&
|
|
108
|
+
authority.executionAuthority !== "executor"
|
|
109
|
+
) {
|
|
110
|
+
throw new BridgeAuthorityError("execution_denied", operation.domainId);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
export type BridgeAuthorityErrorCode =
|
|
116
|
+
| "capability_unavailable"
|
|
117
|
+
| "authority_unavailable"
|
|
118
|
+
| "read_denied"
|
|
119
|
+
| "write_denied"
|
|
120
|
+
| "execution_denied"
|
|
121
|
+
| "request_domain_mismatch"
|
|
122
|
+
| "request_session_mismatch"
|
|
123
|
+
| "stale_authority";
|
|
124
|
+
|
|
125
|
+
export class BridgeAuthorityError extends Error {
|
|
126
|
+
readonly code: BridgeAuthorityErrorCode;
|
|
127
|
+
readonly domainId: DomainId;
|
|
128
|
+
|
|
129
|
+
constructor(code: BridgeAuthorityErrorCode, domainId: DomainId) {
|
|
130
|
+
super(`bridge authority check failed: ${code} (${domainId})`);
|
|
131
|
+
this.name = "BridgeAuthorityError";
|
|
132
|
+
this.code = code;
|
|
133
|
+
this.domainId = domainId;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
export function classifyBridgeCommandRetry<P>(
|
|
138
|
+
command: BridgeCommandEnvelope<P>,
|
|
139
|
+
delivery: BridgeCommandDelivery,
|
|
140
|
+
retryClass: BridgeRetryClass,
|
|
141
|
+
deduplication: BridgeDeduplicationSupport,
|
|
142
|
+
): BridgeCommandRetryDecision {
|
|
143
|
+
if (delivery === "notDispatched") {
|
|
144
|
+
return "doNotRetry";
|
|
145
|
+
}
|
|
146
|
+
if (
|
|
147
|
+
retryClass !== "never" &&
|
|
148
|
+
command.idempotencyKey !== null &&
|
|
149
|
+
typeof deduplication === "object" &&
|
|
150
|
+
deduplication !== null &&
|
|
151
|
+
"finite" in deduplication
|
|
152
|
+
) {
|
|
153
|
+
return "retrySameRequest";
|
|
154
|
+
}
|
|
155
|
+
return "indeterminate";
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
export function classifyBridgeQueryRetry<P>(
|
|
159
|
+
_query: BridgeQueryEnvelope<P>,
|
|
160
|
+
retryClass: BridgeRetryClass,
|
|
161
|
+
adapterAllowsRetry: boolean,
|
|
162
|
+
): BridgeQueryRetryDecision {
|
|
163
|
+
return adapterAllowsRetry && retryClass !== "never"
|
|
164
|
+
? "retry"
|
|
165
|
+
: "doNotRetry";
|
|
166
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
BridgeHelloRequest,
|
|
3
|
+
BridgeNegotiationReceipt,
|
|
4
|
+
} from "./generated/protocol.ts";
|
|
5
|
+
import {
|
|
6
|
+
parseBridgeHelloRequest,
|
|
7
|
+
parseBridgeNegotiationReceipt,
|
|
8
|
+
} from "./validation.ts";
|
|
9
|
+
import type { BridgeOperationDescriptor } from "./operation.ts";
|
|
10
|
+
|
|
11
|
+
type BridgeOperationHandler<Request, Reply> = (
|
|
12
|
+
request: Request,
|
|
13
|
+
) => Reply | Promise<Reply>;
|
|
14
|
+
|
|
15
|
+
interface RegisteredOperation {
|
|
16
|
+
readonly descriptor: BridgeOperationDescriptor<unknown, unknown>;
|
|
17
|
+
readonly handler: BridgeOperationHandler<unknown, unknown>;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export class BridgeHostRouter {
|
|
21
|
+
readonly #negotiate: (
|
|
22
|
+
request: BridgeHelloRequest,
|
|
23
|
+
) => BridgeNegotiationReceipt | Promise<BridgeNegotiationReceipt>;
|
|
24
|
+
readonly #operations = new Map<string, RegisteredOperation>();
|
|
25
|
+
|
|
26
|
+
constructor(
|
|
27
|
+
negotiate: (
|
|
28
|
+
request: BridgeHelloRequest,
|
|
29
|
+
) => BridgeNegotiationReceipt | Promise<BridgeNegotiationReceipt>,
|
|
30
|
+
) {
|
|
31
|
+
this.#negotiate = negotiate;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
register<Request, Reply>(
|
|
35
|
+
descriptor: BridgeOperationDescriptor<Request, Reply>,
|
|
36
|
+
handler: BridgeOperationHandler<Request, Reply>,
|
|
37
|
+
): () => void {
|
|
38
|
+
if (this.#operations.has(descriptor.route)) {
|
|
39
|
+
throw new Error(`duplicate bridge operation route: ${descriptor.route}`);
|
|
40
|
+
}
|
|
41
|
+
const registration: RegisteredOperation = {
|
|
42
|
+
descriptor: descriptor as BridgeOperationDescriptor<unknown, unknown>,
|
|
43
|
+
handler: handler as BridgeOperationHandler<unknown, unknown>,
|
|
44
|
+
};
|
|
45
|
+
this.#operations.set(descriptor.route, registration);
|
|
46
|
+
return () => {
|
|
47
|
+
if (this.#operations.get(descriptor.route) === registration) {
|
|
48
|
+
this.#operations.delete(descriptor.route);
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
async negotiate(value: unknown): Promise<BridgeNegotiationReceipt> {
|
|
54
|
+
const request = parseBridgeHelloRequest(value);
|
|
55
|
+
return parseBridgeNegotiationReceipt(
|
|
56
|
+
await this.#negotiate(request),
|
|
57
|
+
request,
|
|
58
|
+
);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
async invoke(route: string, value: unknown): Promise<unknown> {
|
|
62
|
+
const registration = this.#operations.get(route);
|
|
63
|
+
if (registration === undefined) {
|
|
64
|
+
throw new Error(`unknown bridge operation route: ${route}`);
|
|
65
|
+
}
|
|
66
|
+
const request = registration.descriptor.request.parse(value);
|
|
67
|
+
return registration.descriptor.reply.parse(
|
|
68
|
+
await registration.handler(request),
|
|
69
|
+
);
|
|
70
|
+
}
|
|
71
|
+
}
|