Open History to see the last commit
1import { EnvironmentId } from "@t3tools/contracts";
2import * as Schema from "effect/Schema";
3
4const ConnectionTargetBase = {
5 environmentId: EnvironmentId,
6 label: Schema.String,
7};
8
9export class PrimaryConnectionTarget extends Schema.TaggedClass<PrimaryConnectionTarget>()(
10 "PrimaryConnectionTarget",
11 {
12 ...ConnectionTargetBase,
13 httpBaseUrl: Schema.String,
14 wsBaseUrl: Schema.String,
15 },
16) {}
17
18export class BearerConnectionTarget extends Schema.TaggedClass<BearerConnectionTarget>()(
19 "BearerConnectionTarget",
20 {
21 ...ConnectionTargetBase,
22 connectionId: Schema.String,
23 },
24) {}
25
26export class RelayConnectionTarget extends Schema.TaggedClass<RelayConnectionTarget>()(
27 "RelayConnectionTarget",
28 {
29 ...ConnectionTargetBase,
30 },
31) {}
32
33export class SshConnectionTarget extends Schema.TaggedClass<SshConnectionTarget>()(
34 "SshConnectionTarget",
35 {
36 ...ConnectionTargetBase,
37 connectionId: Schema.String,
38 },
39) {}
40
41export const ConnectionTarget = Schema.Union([
42 PrimaryConnectionTarget,
43 BearerConnectionTarget,
44 RelayConnectionTarget,
45 SshConnectionTarget,
46]);
47export type ConnectionTarget = typeof ConnectionTarget.Type;
48
49export const PersistedConnectionTarget = Schema.Union([
50 BearerConnectionTarget,
51 RelayConnectionTarget,
52 SshConnectionTarget,
53]);
54export type PersistedConnectionTarget = typeof PersistedConnectionTarget.Type;
55
56export type ConnectionTargetKind = ConnectionTarget["_tag"];
57
58export type NetworkStatus = "unknown" | "offline" | "online";
59
60export const ConnectionTransientReason = Schema.Literals([
61 "network",
62 "timeout",
63 "transport",
64 "endpoint-unavailable",
65 "relay-unavailable",
66 "remote-unavailable",
67 "session-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
79export class ConnectionTransientError extends Schema.TaggedError<ConnectionTransientError>()(
80 "ConnectionTransientError",
81 {
82 reason: ConnectionTransientReason,
83 detail: Schema.String,
84 traceId: Schema.optionalKey(Schema.String),
85 },
86) {
87 override get message(): string {
88 return this.detail;
89 }
90}
91
92export class ConnectionBlockedError extends Schema.TaggedError<ConnectionBlockedError>()(
93 "ConnectionBlockedError",
94 {
95 reason: ConnectionBlockedReason,
96 detail: Schema.String,
97 traceId: Schema.optionalKey(Schema.String),
98
99 serverUpdateRequired: Schema.optionalKey(Schema.Boolean),
100 },
101) {
102 override get message(): string {
103 return this.detail;
104 }
105}
106
107export type ConnectionAttemptError = ConnectionTransientError | ConnectionBlockedError;
108
109export const DPOP_ACCESS_TOKEN_REFRESH_SKEW_MS = 60_000;
110
111export type PreparedHttpAuthorization =
112 | {
113 readonly _tag: "Bearer";
114 readonly token: string;
115 }
116 | {
117 readonly _tag: "Dpop";
118 readonly accessToken: string;
119 readonly expiresAtEpochMs: number;
120 };
121
122export interface PreparedConnection {
123 readonly environmentId: EnvironmentId;
124 readonly label: string;
125 readonly httpBaseUrl: string;
126 readonly socketUrl: string;
127 readonly httpAuthorization: PreparedHttpAuthorization | null;
128 readonly target: ConnectionTarget;
129}
130
131export type SupervisorConnectionPhase =
132 | "available"
133 | "offline"
134 | "connecting"
135 | "backoff"
136 | "connected"
137 | "blocked";
138
139export type ConnectionAttemptStage = "preparing" | "opening" | "synchronizing";
140
141export interface SupervisorConnectionState {
142 readonly desired: boolean;
143 readonly network: NetworkStatus;
144 readonly phase: SupervisorConnectionPhase;
145 readonly stage: ConnectionAttemptStage | null;
146 readonly attempt: number;
147 readonly generation: number;
148 readonly lastFailure: ConnectionAttemptError | null;
149 readonly retryAt: number | null;
150}
151
152export type ConnectionProjectionPhase = "disconnected" | "synchronizing" | "ready";
153
154export function connectionProjectionPhase(
155 state: SupervisorConnectionState,
156): ConnectionProjectionPhase {
157 switch (state.phase) {
158 case "connecting":
159 return "synchronizing";
160 case "connected":
161 return "ready";
162 case "available":
163 case "offline":
164 case "backoff":
165 case "blocked":
166 return "disconnected";
167 }
168}
169
170export const AVAILABLE_CONNECTION_STATE: SupervisorConnectionState = Object.freeze({
171 desired: false,
172 network: "unknown",
173 phase: "available",
174 stage: null,
175 attempt: 0,
176 generation: 0,
177 lastFailure: null,
178 retryAt: null,
179});