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 learned: Schema.optionalKey(Schema.Literal(true)),
26 authorization: Schema.optionalKey(Schema.Literal("t3-connect")),
27 /**
28 * "tailscale" on a learned route the server found on its Tailscale
29 * interface. A bare 100.64.0.0/10 address could belong to any VPN.
30 */
31 network: Schema.optionalKey(Schema.Literal("tailscale")),
32 },
33) {}
34
35export class SshConnectionProfile extends Schema.TaggedClass<SshConnectionProfile>()(
36 "SshConnectionProfile",
37 {
38 ...ConnectionProfileBase,
39 target: DesktopSshEnvironmentTargetSchema,
40 },
41) {}
42
43export const ConnectionProfile = Schema.Union([BearerConnectionProfile, SshConnectionProfile]);
44export type ConnectionProfile = typeof ConnectionProfile.Type;
45
46export interface ConnectionRoute {
47 readonly target: ConnectionTarget;
48 readonly profile: Option.Option<ConnectionProfile>;
49}
50
51export interface ConnectionCatalogEntry {
52 readonly target: ConnectionTarget;
53 readonly profile: Option.Option<ConnectionProfile>;
54 readonly alternateRoutes?: ReadonlyArray<ConnectionRoute>;
55 readonly enabled: boolean;
56 readonly unsupportedReason?: string;
57 readonly serverUpdateRequired?: boolean;
58}
59
60export class BearerConnectionCredential extends Schema.TaggedClass<BearerConnectionCredential>()(
61 "BearerConnectionCredential",
62 {
63 token: Schema.String,
64 },
65) {}
66
67export const ConnectionCredential = Schema.Union([BearerConnectionCredential]);
68export type ConnectionCredential = typeof ConnectionCredential.Type;
69
70export class PrimaryConnectionRegistration extends Schema.TaggedClass<PrimaryConnectionRegistration>()(
71 "PrimaryConnectionRegistration",
72 {
73 target: PrimaryConnectionTarget,
74 },
75) {}
76
77export class RelayConnectionRegistration extends Schema.TaggedClass<RelayConnectionRegistration>()(
78 "RelayConnectionRegistration",
79 {
80 target: RelayConnectionTarget,
81 },
82) {}
83
84export class BearerConnectionRegistration extends Schema.TaggedClass<BearerConnectionRegistration>()(
85 "BearerConnectionRegistration",
86 {
87 target: BearerConnectionTarget,
88 profile: BearerConnectionProfile,
89 credential: BearerConnectionCredential,
90 },
91) {}
92
93export class SshConnectionRegistration extends Schema.TaggedClass<SshConnectionRegistration>()(
94 "SshConnectionRegistration",
95 {
96 target: SshConnectionTarget,
97 profile: SshConnectionProfile,
98 },
99) {}
100
101export const ConnectionRegistration = Schema.Union([
102 RelayConnectionRegistration,
103 BearerConnectionRegistration,
104 SshConnectionRegistration,
105]);
106export type ConnectionRegistration = typeof ConnectionRegistration.Type;
107
108/**
109 * The primary stays on same-origin cookie auth
110 * (`PrimaryConnectionRegistration`); secondary local backends live on a
111 * separate loopback origin and authenticate with a bearer token minted from
112 * their bootstrap credential (`BearerConnectionRegistration`).
113 */
114export const PlatformConnectionRegistration = Schema.Union([
115 PrimaryConnectionRegistration,
116 BearerConnectionRegistration,
117]);
118export type PlatformConnectionRegistration = typeof PlatformConnectionRegistration.Type;
119
120export function connectionRegistrationCatalogEntry(
121 registration: ConnectionRegistration | PrimaryConnectionRegistration,
122): ConnectionCatalogEntry {
123 switch (registration._tag) {
124 case "PrimaryConnectionRegistration":
125 case "RelayConnectionRegistration":
126 return {
127 target: registration.target,
128 profile: Option.none(),
129 enabled: true,
130 };
131 case "BearerConnectionRegistration":
132 case "SshConnectionRegistration":
133 return {
134 target: registration.target,
135 profile: Option.some(registration.profile),
136 enabled: true,
137 };
138 }
139}