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

model.ts

213 lines6.0 KB

1import { EnvironmentId, type ExecutionEnvironmentDescriptor } 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 "approval-pending",
68]);
69export type ConnectionTransientReason = typeof ConnectionTransientReason.Type;
70​
71export const ConnectionBlockedReason = Schema.Literals([
72 "authentication",
73 "configuration",
74 "permission",
75 "unsupported",
76]);
77export type ConnectionBlockedReason = typeof ConnectionBlockedReason.Type;
78​
79/** A part of the path to an environment: this device, T3 Connect, the host's tunnel, or T3 Code on the host. */
80export const ConnectionHop = Schema.Literals(["device", "relay", "tunnel", "server"]);
81export type ConnectionHop = typeof ConnectionHop.Type;
82​
83/** The relay's endpoint failures plus the causes the client can name from other errors. */
84export const ConnectionFailureCause = Schema.Literals([
85 ...RelayEnvironmentEndpointFailure.literals,
86 "clock_skew",
87 "not_linked",
88 "relay_error",
89 "tunnel_pending",
90 "tunnel_not_enabled",
91 "approval_pending",
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 /** The code this device shows while the host decides whether to trust it. */
114 verificationCode: Schema.optionalKey(Schema.String),
115 ...ConnectionFailureFields,
116 },
117) {
118 override get message(): string {
119 return this.detail;
120 }
122​
123export class ConnectionBlockedError extends Schema.TaggedError<ConnectionBlockedError>()(
124 "ConnectionBlockedError",
125 {
126 reason: ConnectionBlockedReason,
127 detail: Schema.String,
128 traceId: Schema.optionalKey(Schema.String),
129 ...ConnectionFailureFields,
130​
131 serverUpdateRequired: Schema.optionalKey(Schema.Boolean),
132 },
133) {
134 override get message(): string {
135 return this.detail;
136 }
138​
139export type ConnectionAttemptError = ConnectionTransientError | ConnectionBlockedError;
140​
141export const DPOP_ACCESS_TOKEN_REFRESH_SKEW_MS = 60_000;
142​
143export type PreparedHttpAuthorization =
144 | {
145 readonly _tag: "Bearer";
146 readonly token: string;
147 }
148 | {
149 readonly _tag: "Dpop";
150 readonly accessToken: string;
151 readonly expiresAtEpochMs: number;
152 };
153​
154export interface PreparedConnection {
155 readonly environmentId: EnvironmentId;
156 readonly label: string;
157 readonly httpBaseUrl: string;
158 readonly socketUrl: string;
159 readonly httpAuthorization: PreparedHttpAuthorization | null;
160 readonly target: ConnectionTarget;
161 /** What the server said about itself while this connection was prepared. */
162 readonly descriptor?: ExecutionEnvironmentDescriptor;
164​
165export type SupervisorConnectionPhase =
166 | "available"
167 | "offline"
168 | "connecting"
169 | "backoff"
170 | "connected"
171 | "blocked";
172​
173export type ConnectionAttemptStage = "preparing" | "opening" | "synchronizing";
174​
175export interface SupervisorConnectionState {
176 readonly desired: boolean;
177 readonly network: NetworkStatus;
178 readonly phase: SupervisorConnectionPhase;
179 readonly stage: ConnectionAttemptStage | null;
180 readonly attempt: number;
181 readonly generation: number;
182 readonly lastFailure: ConnectionAttemptError | null;
183 readonly retryAt: number | null;
185​
186export type ConnectionProjectionPhase = "disconnected" | "synchronizing" | "ready";
187​
188export function connectionProjectionPhase(
189 state: SupervisorConnectionState,
190): ConnectionProjectionPhase {
191 switch (state.phase) {
192 case "connecting":
193 return "synchronizing";
194 case "connected":
195 return "ready";
196 case "available":
197 case "offline":
198 case "backoff":
199 case "blocked":
200 return "disconnected";
201 }
203​
204export const AVAILABLE_CONNECTION_STATE: SupervisorConnectionState = Object.freeze({
205 desired: false,
206 network: "unknown",
207 phase: "available",
208 stage: null,
209 attempt: 0,
210 generation: 0,
211 lastFailure: null,
212 retryAt: null,
213});