Open History to see the last commit
1import { EnvironmentId } from "@t3tools/contracts";
2import { RelayEnvironmentEndpointFailure } from "@t3tools/contracts/relay";
3import * as Schema from "effect/Schema";
4
5const ConnectionTargetBase = {
6 environmentId: EnvironmentId,
7 label: Schema.String,
8};
9
10export class PrimaryConnectionTarget extends Schema.TaggedClass<PrimaryConnectionTarget>()(
11 "PrimaryConnectionTarget",
12 {
13 ...ConnectionTargetBase,
14 httpBaseUrl: Schema.String,
15 wsBaseUrl: Schema.String,
16 },
17) {}
18
19export class BearerConnectionTarget extends Schema.TaggedClass<BearerConnectionTarget>()(
20 "BearerConnectionTarget",
21 {
22 ...ConnectionTargetBase,
23 connectionId: Schema.String,
24 },
25) {}
26
27export class RelayConnectionTarget extends Schema.TaggedClass<RelayConnectionTarget>()(
28 "RelayConnectionTarget",
29 {
30 ...ConnectionTargetBase,
31 },
32) {}
33
34export class SshConnectionTarget extends Schema.TaggedClass<SshConnectionTarget>()(
35 "SshConnectionTarget",
36 {
37 ...ConnectionTargetBase,
38 connectionId: Schema.String,
39 },
40) {}
41
42export const ConnectionTarget = Schema.Union([
43 PrimaryConnectionTarget,
44 BearerConnectionTarget,
45 RelayConnectionTarget,
46 SshConnectionTarget,
47]);
48export type ConnectionTarget = typeof ConnectionTarget.Type;
49
50export const PersistedConnectionTarget = Schema.Union([
51 BearerConnectionTarget,
52 RelayConnectionTarget,
53 SshConnectionTarget,
54]);
55export type PersistedConnectionTarget = typeof PersistedConnectionTarget.Type;
56
57export type ConnectionTargetKind = ConnectionTarget["_tag"];
58
59export type NetworkStatus = "unknown" | "offline" | "online";
60
61export const ConnectionTransientReason = Schema.Literals([
62 "network",
63 "timeout",
64 "transport",
65 "endpoint-unavailable",
66 "relay-unavailable",
67 "remote-unavailable",
68 "session-pending",
69]);
70export type ConnectionTransientReason = typeof ConnectionTransientReason.Type;
71
72export const ConnectionBlockedReason = Schema.Literals([
73 "authentication",
74 "configuration",
75 "permission",
76 "unsupported",
77]);
78export type ConnectionBlockedReason = typeof ConnectionBlockedReason.Type;
79
80/** A part of the path to an environment: this device, T3 Connect, the host's tunnel, or T3 Code on the host. */
81export const ConnectionHop = Schema.Literals(["device", "relay", "tunnel", "server"]);
82export type ConnectionHop = typeof ConnectionHop.Type;
83
84/** The relay's endpoint failures plus the causes the client can name from other errors. */
85export const ConnectionFailureCause = Schema.Literals([
86 ...RelayEnvironmentEndpointFailure.literals,
87 "clock_skew",
88 "not_linked",
89 "relay_error",
90 "tunnel_pending",
91 "tunnel_not_enabled",
92]);
93export type ConnectionFailureCause = typeof ConnectionFailureCause.Type;
94
95/**
96 * Where and why a failure happened, when the code that saw it knows. See
97 * `presentConnectionDiagnosis`. `via` names the route when the hop alone does
98 * not: T3 Code on the host can fail behind the tunnel or on a direct route.
99 */
100const ConnectionFailureFields = {
101 hop: Schema.optionalKey(ConnectionHop),
102 via: Schema.optionalKey(Schema.Literals(["relay", "direct"])),
103 failureCause: Schema.optionalKey(ConnectionFailureCause),
104 httpStatus: Schema.optionalKey(Schema.Number),
105};
106
107export class ConnectionTransientError extends Schema.TaggedError<ConnectionTransientError>()(
108 "ConnectionTransientError",
109 {
110 reason: ConnectionTransientReason,
111 detail: Schema.String,
112 traceId: Schema.optionalKey(Schema.String),
113 ...ConnectionFailureFields,
114 },
115) {
116 override get message(): string {
117 return this.detail;
118 }
119}
120
121export class ConnectionBlockedError extends Schema.TaggedError<ConnectionBlockedError>()(
122 "ConnectionBlockedError",
123 {
124 reason: ConnectionBlockedReason,
125 detail: Schema.String,
126 traceId: Schema.optionalKey(Schema.String),
127 ...ConnectionFailureFields,
128
129 serverUpdateRequired: Schema.optionalKey(Schema.Boolean),
130 },
131) {
132 override get message(): string {
133 return this.detail;
134 }
135}
136
137export type ConnectionAttemptError = ConnectionTransientError | ConnectionBlockedError;
138
139export const DPOP_ACCESS_TOKEN_REFRESH_SKEW_MS = 60_000;
140
141export type PreparedHttpAuthorization =
142 | {
143 readonly _tag: "Bearer";
144 readonly token: string;
145 }
146 | {
147 readonly _tag: "Dpop";
148 readonly accessToken: string;
149 readonly expiresAtEpochMs: number;
150 };
151
152export interface PreparedConnection {
153 readonly environmentId: EnvironmentId;
154 readonly label: string;
155 readonly httpBaseUrl: string;
156 readonly socketUrl: string;
157 readonly httpAuthorization: PreparedHttpAuthorization | null;
158 readonly target: ConnectionTarget;
159}
160
161export type SupervisorConnectionPhase =
162 | "available"
163 | "offline"
164 | "connecting"
165 | "backoff"
166 | "connected"
167 | "blocked";
168
169export type ConnectionAttemptStage = "preparing" | "opening" | "synchronizing";
170
171export interface SupervisorConnectionState {
172 readonly desired: boolean;
173 readonly network: NetworkStatus;
174 readonly phase: SupervisorConnectionPhase;
175 readonly stage: ConnectionAttemptStage | null;
176 readonly attempt: number;
177 readonly generation: number;
178 readonly lastFailure: ConnectionAttemptError | null;
179 readonly retryAt: number | null;
180}
181
182export type ConnectionProjectionPhase = "disconnected" | "synchronizing" | "ready";
183
184export function connectionProjectionPhase(
185 state: SupervisorConnectionState,
186): ConnectionProjectionPhase {
187 switch (state.phase) {
188 case "connecting":
189 return "synchronizing";
190 case "connected":
191 return "ready";
192 case "available":
193 case "offline":
194 case "backoff":
195 case "blocked":
196 return "disconnected";
197 }
198}
199
200export const AVAILABLE_CONNECTION_STATE: SupervisorConnectionState = Object.freeze({
201 desired: false,
202 network: "unknown",
203 phase: "available",
204 stage: null,
205 attempt: 0,
206 generation: 0,
207 lastFailure: null,
208 retryAt: null,
209});