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