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.TaggedError<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.TaggedError<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 const DPOP_ACCESS_TOKEN_REFRESH_SKEW_MS = 60_000;
107
108export type PreparedHttpAuthorization =
109 | {
110 readonly _tag: "Bearer";
111 readonly token: string;
112 }
113 | {
114 readonly _tag: "Dpop";
115 readonly accessToken: string;
116 readonly expiresAtEpochMs: number;
117 };
118
119export interface PreparedConnection {
120 readonly environmentId: EnvironmentId;
121 readonly label: string;
122 readonly httpBaseUrl: string;
123 readonly socketUrl: string;
124 readonly httpAuthorization: PreparedHttpAuthorization | null;
125 readonly target: ConnectionTarget;
126}
127
128export type SupervisorConnectionPhase =
129 | "available"
130 | "offline"
131 | "connecting"
132 | "backoff"
133 | "connected"
134 | "blocked";
135
136export type ConnectionAttemptStage = "preparing" | "opening" | "synchronizing";
137
138export interface SupervisorConnectionState {
139 readonly desired: boolean;
140 readonly network: NetworkStatus;
141 readonly phase: SupervisorConnectionPhase;
142 readonly stage: ConnectionAttemptStage | null;
143 readonly attempt: number;
144 readonly generation: number;
145 readonly lastFailure: ConnectionAttemptError | null;
146 readonly retryAt: number | null;
147}
148
149export type ConnectionProjectionPhase = "disconnected" | "synchronizing" | "ready";
150
151export function connectionProjectionPhase(
152 state: SupervisorConnectionState,
153): ConnectionProjectionPhase {
154 switch (state.phase) {
155 case "connecting":
156 return "synchronizing";
157 case "connected":
158 return "ready";
159 case "available":
160 case "offline":
161 case "backoff":
162 case "blocked":
163 return "disconnected";
164 }
165}
166
167export const AVAILABLE_CONNECTION_STATE: SupervisorConnectionState = Object.freeze({
168 desired: false,
169 network: "unknown",
170 phase: "available",
171 stage: null,
172 attempt: 0,
173 generation: 0,
174 lastFailure: null,
175 retryAt: null,
176});