Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

model.ts

207 lines5.6 KB

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 NetworkStatus = "unknown" | "offline" | "online";
58​
59export const ConnectionTransientReason = Schema.Literals([
60 "network",
61 "timeout",
62 "transport",
63 "endpoint-unavailable",
64 "relay-unavailable",
65 "remote-unavailable",
66 "session-pending",
67]);
68export type ConnectionTransientReason = typeof ConnectionTransientReason.Type;
69​
70export const ConnectionBlockedReason = Schema.Literals([
71 "authentication",
72 "configuration",
73 "permission",
74 "unsupported",
75]);
76export type ConnectionBlockedReason = typeof ConnectionBlockedReason.Type;
77​
78/** A part of the path to an environment: this device, T3 Connect, the host's tunnel, or T3 Code on the host. */
79export const ConnectionHop = Schema.Literals(["device", "relay", "tunnel", "server"]);
80export type ConnectionHop = typeof ConnectionHop.Type;
81​
82/** The relay's endpoint failures plus the causes the client can name from other errors. */
83export const ConnectionFailureCause = Schema.Literals([
84 ...RelayEnvironmentEndpointFailure.literals,
85 "clock_skew",
86 "not_linked",
87 "relay_error",
88 "tunnel_pending",
89 "tunnel_not_enabled",
90]);
91export type ConnectionFailureCause = typeof ConnectionFailureCause.Type;
92​
93/**
94 * Where and why a failure happened, when the code that saw it knows. See
95 * `presentConnectionDiagnosis`. `via` names the route when the hop alone does
96 * not: T3 Code on the host can fail behind the tunnel or on a direct route.
97 */
98const ConnectionFailureFields = {
99 hop: Schema.optionalKey(ConnectionHop),
100 via: Schema.optionalKey(Schema.Literals(["relay", "direct"])),
101 failureCause: Schema.optionalKey(ConnectionFailureCause),
102 httpStatus: Schema.optionalKey(Schema.Number),
103};
104​
105export class ConnectionTransientError extends Schema.TaggedError<ConnectionTransientError>()(
106 "ConnectionTransientError",
107 {
108 reason: ConnectionTransientReason,
109 detail: Schema.String,
110 traceId: Schema.optionalKey(Schema.String),
111 ...ConnectionFailureFields,
112 },
113) {
114 override get message(): string {
115 return this.detail;
116 }
118​
119export class ConnectionBlockedError extends Schema.TaggedError<ConnectionBlockedError>()(
120 "ConnectionBlockedError",
121 {
122 reason: ConnectionBlockedReason,
123 detail: Schema.String,
124 traceId: Schema.optionalKey(Schema.String),
125 ...ConnectionFailureFields,
126​
127 serverUpdateRequired: Schema.optionalKey(Schema.Boolean),
128 },
129) {
130 override get message(): string {
131 return this.detail;
132 }
134​
135export type ConnectionAttemptError = ConnectionTransientError | ConnectionBlockedError;
136​
137export const DPOP_ACCESS_TOKEN_REFRESH_SKEW_MS = 60_000;
138​
139export type PreparedHttpAuthorization =
140 | {
141 readonly _tag: "Bearer";
142 readonly token: string;
143 }
144 | {
145 readonly _tag: "Dpop";
146 readonly accessToken: string;
147 readonly expiresAtEpochMs: number;
148 };
149​
150export interface PreparedConnection {
151 readonly environmentId: EnvironmentId;
152 readonly label: string;
153 readonly httpBaseUrl: string;
154 readonly socketUrl: string;
155 readonly httpAuthorization: PreparedHttpAuthorization | null;
156 readonly target: ConnectionTarget;
158​
159export type SupervisorConnectionPhase =
160 | "available"
161 | "offline"
162 | "connecting"
163 | "backoff"
164 | "connected"
165 | "blocked";
166​
167export type ConnectionAttemptStage = "preparing" | "opening" | "synchronizing";
168​
169export interface SupervisorConnectionState {
170 readonly desired: boolean;
171 readonly network: NetworkStatus;
172 readonly phase: SupervisorConnectionPhase;
173 readonly stage: ConnectionAttemptStage | null;
174 readonly attempt: number;
175 readonly generation: number;
176 readonly lastFailure: ConnectionAttemptError | null;
177 readonly retryAt: number | null;
179​
180export type ConnectionProjectionPhase = "disconnected" | "synchronizing" | "ready";
181​
182export function connectionProjectionPhase(
183 state: SupervisorConnectionState,
184): ConnectionProjectionPhase {
185 switch (state.phase) {
186 case "connecting":
187 return "synchronizing";
188 case "connected":
189 return "ready";
190 case "available":
191 case "offline":
192 case "backoff":
193 case "blocked":
194 return "disconnected";
195 }
197​
198export const AVAILABLE_CONNECTION_STATE: SupervisorConnectionState = Object.freeze({
199 desired: false,
200 network: "unknown",
201 phase: "available",
202 stage: null,
203 attempt: 0,
204 generation: 0,
205 lastFailure: null,
206 retryAt: null,
207});