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