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

catalog.ts

153 lines4.6 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​
67/** A token bound to this device's DPoP key, which the host renews while the device uses it. */
68export class DpopConnectionCredential extends Schema.TaggedClass<DpopConnectionCredential>()(
69 "DpopConnectionCredential",
70 {
71 accessToken: Schema.String,
72 expiresAtEpochMs: Schema.Number,
73 },
74) {}
75​
76export const ConnectionCredential = Schema.Union([
77 BearerConnectionCredential,
78 DpopConnectionCredential,
79]);
80export type ConnectionCredential = typeof ConnectionCredential.Type;
81​
82export class PrimaryConnectionRegistration extends Schema.TaggedClass<PrimaryConnectionRegistration>()(
83 "PrimaryConnectionRegistration",
84 {
85 target: PrimaryConnectionTarget,
86 },
87) {}
88​
89export class RelayConnectionRegistration extends Schema.TaggedClass<RelayConnectionRegistration>()(
90 "RelayConnectionRegistration",
91 {
92 target: RelayConnectionTarget,
93 },
94) {}
95​
96export class BearerConnectionRegistration extends Schema.TaggedClass<BearerConnectionRegistration>()(
97 "BearerConnectionRegistration",
98 {
99 target: BearerConnectionTarget,
100 profile: BearerConnectionProfile,
101 credential: ConnectionCredential,
102 },
103) {}
104​
105export class SshConnectionRegistration extends Schema.TaggedClass<SshConnectionRegistration>()(
106 "SshConnectionRegistration",
107 {
108 target: SshConnectionTarget,
109 profile: SshConnectionProfile,
110 /** The token from pairing over SSH, which later connects reuse. */
111 credential: BearerConnectionCredential,
112 },
113) {}
114​
115export const ConnectionRegistration = Schema.Union([
116 RelayConnectionRegistration,
117 BearerConnectionRegistration,
118 SshConnectionRegistration,
119]);
120export type ConnectionRegistration = typeof ConnectionRegistration.Type;
121​
122/**
123 * The primary stays on same-origin cookie auth
124 * (`PrimaryConnectionRegistration`); secondary local backends live on a
125 * separate loopback origin and authenticate with a bearer token minted from
126 * their bootstrap credential (`BearerConnectionRegistration`).
127 */
128export const PlatformConnectionRegistration = Schema.Union([
129 PrimaryConnectionRegistration,
130 BearerConnectionRegistration,
131]);
132export type PlatformConnectionRegistration = typeof PlatformConnectionRegistration.Type;
133​
134export function connectionRegistrationCatalogEntry(
135 registration: ConnectionRegistration | PrimaryConnectionRegistration,
136): ConnectionCatalogEntry {
137 switch (registration._tag) {
138 case "PrimaryConnectionRegistration":
139 case "RelayConnectionRegistration":
140 return {
141 target: registration.target,
142 profile: Option.none(),
143 enabled: true,
144 };
145 case "BearerConnectionRegistration":
146 case "SshConnectionRegistration":
147 return {
148 target: registration.target,
149 profile: Option.some(registration.profile),
150 enabled: true,
151 };
152 }