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]);
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
78export class ConnectionTransientError extends Schema.TaggedErrorClass<ConnectionTransientError>()(
79 "ConnectionTransientError",
80 {
81 reason: ConnectionTransientReason,
82 detail: Schema.String,
83 traceId: Schema.optionalKey(Schema.String),
84 },
85) {
86 override get message(): string {
87 return this.detail;
88 }
89}
90
91export class ConnectionBlockedError extends Schema.TaggedErrorClass<ConnectionBlockedError>()(
92 "ConnectionBlockedError",
93 {
94 reason: ConnectionBlockedReason,
95 detail: Schema.String,
96 traceId: Schema.optionalKey(Schema.String),
97 },
98) {
99 override get message(): string {
100 return this.detail;
101 }
102}
103
104export type ConnectionAttemptError = ConnectionTransientError | ConnectionBlockedError;
105
106export type PreparedHttpAuthorization =
107 | {
108 readonly _tag: "Bearer";
109 readonly token: string;
110 }
111 | {
112 readonly _tag: "Dpop";
113 readonly accessToken: string;
114 };
115
116export interface PreparedConnection {
117 readonly environmentId: EnvironmentId;
118 readonly label: string;
119 readonly httpBaseUrl: string;
120 readonly socketUrl: string;
121 readonly httpAuthorization: PreparedHttpAuthorization | null;
122 readonly target: ConnectionTarget;
123}
124
125export type SupervisorConnectionPhase =
126 | "available"
127 | "offline"
128 | "connecting"
129 | "backoff"
130 | "connected"
131 | "blocked";
132
133export type ConnectionAttemptStage = "preparing" | "opening" | "synchronizing";
134
135export interface SupervisorConnectionState {
136 readonly desired: boolean;
137 readonly network: NetworkStatus;
138 readonly phase: SupervisorConnectionPhase;
139 readonly stage: ConnectionAttemptStage | null;
140 readonly attempt: number;
141 readonly generation: number;
142 readonly lastFailure: ConnectionAttemptError | null;
143 readonly retryAt: number | null;
144}
145
146export type ConnectionProjectionPhase = "disconnected" | "synchronizing" | "ready";
147
148export function connectionProjectionPhase(
149 state: SupervisorConnectionState,
150): ConnectionProjectionPhase {
151 switch (state.phase) {
152 case "connecting":
153 return "synchronizing";
154 case "connected":
155 return "ready";
156 case "available":
157 case "offline":
158 case "backoff":
159 case "blocked":
160 return "disconnected";
161 }
162}
163
164export const AVAILABLE_CONNECTION_STATE: SupervisorConnectionState = Object.freeze({
165 desired: false,
166 network: "unknown",
167 phase: "available",
168 stage: null,
169 attempt: 0,
170 generation: 0,
171 lastFailure: null,
172 retryAt: null,
173});