Open History to see the last commit
1import { DesktopSshEnvironmentTargetSchema, EnvironmentId } from "@t3tools/contracts";
2import * as Option from "effect/Option";
3import * as Schema from "effect/Schema";
4
5import {
6 BearerConnectionTarget,
7 PrimaryConnectionTarget,
8 RelayConnectionTarget,
9 SshConnectionTarget,
10 type ConnectionTarget,
11} from "./model.ts";
12
13const ConnectionProfileBase = {
14 connectionId: Schema.String,
15 environmentId: EnvironmentId,
16 label: Schema.String,
17};
18
19export class BearerConnectionProfile extends Schema.TaggedClass<BearerConnectionProfile>()(
20 "BearerConnectionProfile",
21 {
22 ...ConnectionProfileBase,
23 httpBaseUrl: Schema.String,
24 wsBaseUrl: Schema.String,
25 /**
26 * Set on a route the server reported while this client was connected,
27 * rather than one the user paired. Learned routes are replaced when the
28 * server reports a different address, for example after a DHCP change.
29 */
30 learned: Schema.optionalKey(Schema.Literal(true)),
31 /**
32 * "t3-connect" when the route authenticates with the environment's T3
33 * Connect credential instead of a stored bearer token.
34 */
35 authorization: Schema.optionalKey(Schema.Literal("t3-connect")),
36 /**
37 * "tailscale" on a learned route the server found on its Tailscale
38 * interface. A bare 100.64.0.0/10 address could belong to any VPN.
39 */
40 network: Schema.optionalKey(Schema.Literal("tailscale")),
41 },
42) {}
43
44export class SshConnectionProfile extends Schema.TaggedClass<SshConnectionProfile>()(
45 "SshConnectionProfile",
46 {
47 ...ConnectionProfileBase,
48 target: DesktopSshEnvironmentTargetSchema,
49 },
50) {}
51
52export const ConnectionProfile = Schema.Union([BearerConnectionProfile, SshConnectionProfile]);
53export type ConnectionProfile = typeof ConnectionProfile.Type;
54
55/** One way to reach an environment: T3 Connect, a direct URL, or SSH. */
56export interface ConnectionRoute {
57 readonly target: ConnectionTarget;
58 readonly profile: Option.Option<ConnectionProfile>;
59}
60
61/**
62 * A saved environment. `target` and `profile` are its preferred route;
63 * `alternateRoutes` holds the others in preference order. Read them together
64 * with `connectionRoutes`.
65 */
66export interface ConnectionCatalogEntry {
67 readonly target: ConnectionTarget;
68 readonly profile: Option.Option<ConnectionProfile>;
69 readonly alternateRoutes?: ReadonlyArray<ConnectionRoute>;
70 /** False when the user switched the environment off: saved, but never connects. */
71 readonly enabled: boolean;
72 /** Discovery rejection stays visible while the saved connection is switched off. */
73 readonly unsupportedReason?: string;
74 /** The rejection came from an outdated host, which can still be updated remotely. */
75 readonly serverUpdateRequired?: boolean;
76}
77
78export class BearerConnectionCredential extends Schema.TaggedClass<BearerConnectionCredential>()(
79 "BearerConnectionCredential",
80 {
81 token: Schema.String,
82 },
83) {}
84
85export const ConnectionCredential = Schema.Union([BearerConnectionCredential]);
86export type ConnectionCredential = typeof ConnectionCredential.Type;
87
88export class PrimaryConnectionRegistration extends Schema.TaggedClass<PrimaryConnectionRegistration>()(
89 "PrimaryConnectionRegistration",
90 {
91 target: PrimaryConnectionTarget,
92 },
93) {}
94
95export class RelayConnectionRegistration extends Schema.TaggedClass<RelayConnectionRegistration>()(
96 "RelayConnectionRegistration",
97 {
98 target: RelayConnectionTarget,
99 },
100) {}
101
102export class BearerConnectionRegistration extends Schema.TaggedClass<BearerConnectionRegistration>()(
103 "BearerConnectionRegistration",
104 {
105 target: BearerConnectionTarget,
106 profile: BearerConnectionProfile,
107 credential: BearerConnectionCredential,
108 },
109) {}
110
111export class SshConnectionRegistration extends Schema.TaggedClass<SshConnectionRegistration>()(
112 "SshConnectionRegistration",
113 {
114 target: SshConnectionTarget,
115 profile: SshConnectionProfile,
116 },
117) {}
118
119export const ConnectionRegistration = Schema.Union([
120 RelayConnectionRegistration,
121 BearerConnectionRegistration,
122 SshConnectionRegistration,
123]);
124export type ConnectionRegistration = typeof ConnectionRegistration.Type;
125
126/**
127 * Platform-managed registrations are reconciled from the host (the desktop
128 * bootstrap IPC) rather than persisted by the user. They cover the primary
129 * local environment plus any additional desktop-local backends running
130 * alongside it (e.g. a parallel WSL backend). The primary stays on same-origin
131 * cookie auth (`PrimaryConnectionRegistration`); secondary local backends live
132 * on a separate loopback origin and authenticate with a bearer token minted
133 * from their bootstrap credential (`BearerConnectionRegistration`).
134 */
135export const PlatformConnectionRegistration = Schema.Union([
136 PrimaryConnectionRegistration,
137 BearerConnectionRegistration,
138]);
139export type PlatformConnectionRegistration = typeof PlatformConnectionRegistration.Type;
140
141export function connectionRegistrationCatalogEntry(
142 registration: ConnectionRegistration | PrimaryConnectionRegistration,
143): ConnectionCatalogEntry {
144 switch (registration._tag) {
145 case "PrimaryConnectionRegistration":
146 case "RelayConnectionRegistration":
147 return {
148 target: registration.target,
149 profile: Option.none(),
150 enabled: true,
151 };
152 case "BearerConnectionRegistration":
153 case "SshConnectionRegistration":
154 return {
155 target: registration.target,
156 profile: Option.some(registration.profile),
157 enabled: true,
158 };
159 }
160}