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}
47
48export class BearerConnectionCredential extends Schema.TaggedClass<BearerConnectionCredential>()(
49 "BearerConnectionCredential",
50 {
51 token: Schema.String,
52 },
53) {}
54
55export const ConnectionCredential = Schema.Union([BearerConnectionCredential]);
56export type ConnectionCredential = typeof ConnectionCredential.Type;
57
58export class PrimaryConnectionRegistration extends Schema.TaggedClass<PrimaryConnectionRegistration>()(
59 "PrimaryConnectionRegistration",
60 {
61 target: PrimaryConnectionTarget,
62 },
63) {}
64
65export class RelayConnectionRegistration extends Schema.TaggedClass<RelayConnectionRegistration>()(
66 "RelayConnectionRegistration",
67 {
68 target: RelayConnectionTarget,
69 },
70) {}
71
72export class BearerConnectionRegistration extends Schema.TaggedClass<BearerConnectionRegistration>()(
73 "BearerConnectionRegistration",
74 {
75 target: BearerConnectionTarget,
76 profile: BearerConnectionProfile,
77 credential: BearerConnectionCredential,
78 },
79) {}
80
81export class SshConnectionRegistration extends Schema.TaggedClass<SshConnectionRegistration>()(
82 "SshConnectionRegistration",
83 {
84 target: SshConnectionTarget,
85 profile: SshConnectionProfile,
86 },
87) {}
88
89export const ConnectionRegistration = Schema.Union([
90 RelayConnectionRegistration,
91 BearerConnectionRegistration,
92 SshConnectionRegistration,
93]);
94export type ConnectionRegistration = typeof ConnectionRegistration.Type;
95
96/**
97 * Platform-managed registrations are reconciled from the host (the desktop
98 * bootstrap IPC) rather than persisted by the user. They cover the primary
99 * local environment plus any additional desktop-local backends running
100 * alongside it (e.g. a parallel WSL backend). The primary stays on same-origin
101 * cookie auth (`PrimaryConnectionRegistration`); secondary local backends live
102 * on a separate loopback origin and authenticate with a bearer token minted
103 * from their bootstrap credential (`BearerConnectionRegistration`).
104 */
105export const PlatformConnectionRegistration = Schema.Union([
106 PrimaryConnectionRegistration,
107 BearerConnectionRegistration,
108]);
109export type PlatformConnectionRegistration = typeof PlatformConnectionRegistration.Type;
110
111export function connectionRegistrationCatalogEntry(
112 registration: ConnectionRegistration | PrimaryConnectionRegistration,
113): ConnectionCatalogEntry {
114 switch (registration._tag) {
115 case "PrimaryConnectionRegistration":
116 case "RelayConnectionRegistration":
117 return {
118 target: registration.target,
119 profile: Option.none(),
120 enabled: true,
121 };
122 case "BearerConnectionRegistration":
123 case "SshConnectionRegistration":
124 return {
125 target: registration.target,
126 profile: Option.some(registration.profile),
127 enabled: true,
128 };
129 }
130}