Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

catalog.ts

141 lines4.3 KB

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>;
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;
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 /** The token from pairing over SSH, which later connects reuse. */
99 credential: BearerConnectionCredential,
100 },
101) {}
102​
103export const ConnectionRegistration = Schema.Union([
104 RelayConnectionRegistration,
105 BearerConnectionRegistration,
106 SshConnectionRegistration,
107]);
108export type ConnectionRegistration = typeof ConnectionRegistration.Type;
109​
110/**
111 * The primary stays on same-origin cookie auth
112 * (`PrimaryConnectionRegistration`); secondary local backends live on a
113 * separate loopback origin and authenticate with a bearer token minted from
114 * their bootstrap credential (`BearerConnectionRegistration`).
115 */
116export const PlatformConnectionRegistration = Schema.Union([
117 PrimaryConnectionRegistration,
118 BearerConnectionRegistration,
119]);
120export type PlatformConnectionRegistration = typeof PlatformConnectionRegistration.Type;
121​
122export function connectionRegistrationCatalogEntry(
123 registration: ConnectionRegistration | PrimaryConnectionRegistration,
124): ConnectionCatalogEntry {
125 switch (registration._tag) {
126 case "PrimaryConnectionRegistration":
127 case "RelayConnectionRegistration":
128 return {
129 target: registration.target,
130 profile: Option.none(),
131 enabled: true,
132 };
133 case "BearerConnectionRegistration":
134 case "SshConnectionRegistration":
135 return {
136 target: registration.target,
137 profile: Option.some(registration.profile),
138 enabled: true,
139 };
140 }