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