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