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) {}
38
39export class SshConnectionProfile extends Schema.TaggedClass<SshConnectionProfile>()(
40 "SshConnectionProfile",
41 {
42 ...ConnectionProfileBase,
43 target: DesktopSshEnvironmentTargetSchema,
44 },
45) {}
46
47export const ConnectionProfile = Schema.Union([BearerConnectionProfile, SshConnectionProfile]);
48export type ConnectionProfile = typeof ConnectionProfile.Type;
49
50/** One way to reach an environment: T3 Connect, a direct URL, or SSH. */
51export interface ConnectionRoute {
52 readonly target: ConnectionTarget;
53 readonly profile: Option.Option<ConnectionProfile>;
54}
55
56/**
57 * A saved environment. `target` and `profile` are its preferred route;
58 * `alternateRoutes` holds the others in preference order. Read them together
59 * with `connectionRoutes`.
60 */
61export interface ConnectionCatalogEntry {
62 readonly target: ConnectionTarget;
63 readonly profile: Option.Option<ConnectionProfile>;
64 readonly alternateRoutes?: ReadonlyArray<ConnectionRoute>;
65 /** False when the user switched the environment off: saved, but never connects. */
66 readonly enabled: boolean;
67 /** Discovery rejection stays visible while the saved connection is switched off. */
68 readonly unsupportedReason?: string;
69 /** The rejection came from an outdated host, which can still be updated remotely. */
70 readonly serverUpdateRequired?: boolean;
71}
72
73export class BearerConnectionCredential extends Schema.TaggedClass<BearerConnectionCredential>()(
74 "BearerConnectionCredential",
75 {
76 token: Schema.String,
77 },
78) {}
79
80export const ConnectionCredential = Schema.Union([BearerConnectionCredential]);
81export type ConnectionCredential = typeof ConnectionCredential.Type;
82
83export class PrimaryConnectionRegistration extends Schema.TaggedClass<PrimaryConnectionRegistration>()(
84 "PrimaryConnectionRegistration",
85 {
86 target: PrimaryConnectionTarget,
87 },
88) {}
89
90export class RelayConnectionRegistration extends Schema.TaggedClass<RelayConnectionRegistration>()(
91 "RelayConnectionRegistration",
92 {
93 target: RelayConnectionTarget,
94 },
95) {}
96
97export class BearerConnectionRegistration extends Schema.TaggedClass<BearerConnectionRegistration>()(
98 "BearerConnectionRegistration",
99 {
100 target: BearerConnectionTarget,
101 profile: BearerConnectionProfile,
102 credential: BearerConnectionCredential,
103 },
104) {}
105
106export class SshConnectionRegistration extends Schema.TaggedClass<SshConnectionRegistration>()(
107 "SshConnectionRegistration",
108 {
109 target: SshConnectionTarget,
110 profile: SshConnectionProfile,
111 },
112) {}
113
114export const ConnectionRegistration = Schema.Union([
115 RelayConnectionRegistration,
116 BearerConnectionRegistration,
117 SshConnectionRegistration,
118]);
119export type ConnectionRegistration = typeof ConnectionRegistration.Type;
120
121/**
122 * Platform-managed registrations are reconciled from the host (the desktop
123 * bootstrap IPC) rather than persisted by the user. They cover the primary
124 * local environment plus any additional desktop-local backends running
125 * alongside it (e.g. a parallel WSL backend). The primary stays on same-origin
126 * cookie auth (`PrimaryConnectionRegistration`); secondary local backends live
127 * on a separate loopback origin and authenticate with a bearer token minted
128 * from their bootstrap credential (`BearerConnectionRegistration`).
129 */
130export const PlatformConnectionRegistration = Schema.Union([
131 PrimaryConnectionRegistration,
132 BearerConnectionRegistration,
133]);
134export type PlatformConnectionRegistration = typeof PlatformConnectionRegistration.Type;
135
136export function connectionRegistrationCatalogEntry(
137 registration: ConnectionRegistration | PrimaryConnectionRegistration,
138): ConnectionCatalogEntry {
139 switch (registration._tag) {
140 case "PrimaryConnectionRegistration":
141 case "RelayConnectionRegistration":
142 return {
143 target: registration.target,
144 profile: Option.none(),
145 enabled: true,
146 };
147 case "BearerConnectionRegistration":
148 case "SshConnectionRegistration":
149 return {
150 target: registration.target,
151 profile: Option.some(registration.profile),
152 enabled: true,
153 };
154 }
155}