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

remote.ts

151 lines4.2 KB

1const PAIRING_TOKEN_PARAM = "token";
2const HOSTED_PAIRING_HOST_PARAM = "host";
3const HOSTED_PAIRING_LABEL_PARAM = "label";
4​
5const readHashParams = (url: URL): URLSearchParams =>
6 new URLSearchParams(url.hash.startsWith("#") ? url.hash.slice(1) : url.hash);
7​
8const normalizeRemoteBaseUrl = (rawValue: string): URL => {
9 const trimmed = rawValue.trim();
10 if (!trimmed) {
11 throw new Error("Enter a backend URL.");
12 }
13​
14 const normalizedInput =
15 /^[a-zA-Z][a-zA-Z\d+-]*:\/\//.test(trimmed) || trimmed.startsWith("//")
16 ? trimmed
17 : `https://${trimmed}`;
18 const url = new URL(normalizedInput);
19 url.pathname = "/";
20 url.search = "";
21 url.hash = "";
22 return url;
23};
24​
25const toHttpBaseUrl = (url: URL): string => {
26 const next = new URL(url.toString());
27 if (next.protocol === "ws:") {
28 next.protocol = "http:";
29 } else if (next.protocol === "wss:") {
30 next.protocol = "https:";
31 }
32 next.pathname = "/";
33 next.search = "";
34 next.hash = "";
35 return next.toString();
36};
37​
38const toWsBaseUrl = (url: URL): string => {
39 const next = new URL(url.toString());
40 if (next.protocol === "http:") {
41 next.protocol = "ws:";
42 } else if (next.protocol === "https:") {
43 next.protocol = "wss:";
44 }
45 next.pathname = "/";
46 next.search = "";
47 next.hash = "";
48 return next.toString();
49};
50​
51export interface ResolvedRemotePairingTarget {
52 readonly credential: string;
53 readonly httpBaseUrl: string;
54 readonly wsBaseUrl: string;
56​
57export interface HostedPairingRequest {
58 readonly host: string;
59 readonly token: string;
60 readonly label: string;
62​
63export const getPairingTokenFromUrl = (url: URL): string | null => {
64 const hashToken = readHashParams(url).get(PAIRING_TOKEN_PARAM)?.trim() ?? "";
65 if (hashToken.length > 0) {
66 return hashToken;
67 }
68​
69 const searchToken = url.searchParams.get(PAIRING_TOKEN_PARAM)?.trim() ?? "";
70 return searchToken.length > 0 ? searchToken : null;
71};
72​
73export const stripPairingTokenFromUrl = (url: URL): URL => {
74 const next = new URL(url.toString());
75 const hashParams = readHashParams(next);
76 if (hashParams.has(PAIRING_TOKEN_PARAM)) {
77 hashParams.delete(PAIRING_TOKEN_PARAM);
78 next.hash = hashParams.toString();
79 }
80 next.searchParams.delete(PAIRING_TOKEN_PARAM);
81 return next;
82};
83​
84export const setPairingTokenOnUrl = (url: URL, credential: string): URL => {
85 const next = new URL(url.toString());
86 next.searchParams.delete(PAIRING_TOKEN_PARAM);
87 next.hash = new URLSearchParams([[PAIRING_TOKEN_PARAM, credential]]).toString();
88 return next;
89};
90​
91export const readHostedPairingRequest = (url: URL): HostedPairingRequest | null => {
92 const host = url.searchParams.get(HOSTED_PAIRING_HOST_PARAM)?.trim() ?? "";
93 const token = getPairingTokenFromUrl(url)?.trim() ?? "";
94 const label = url.searchParams.get(HOSTED_PAIRING_LABEL_PARAM)?.trim() ?? "";
95​
96 if (!host || !token) {
97 return null;
98 }
99​
100 return {
101 host,
102 token,
103 label,
104 };
105};
106​
107export const resolveRemotePairingTarget = (input: {
108 readonly pairingUrl?: string;
109 readonly host?: string;
110 readonly pairingCode?: string;
111}): ResolvedRemotePairingTarget => {
112 const pairingUrl = input.pairingUrl?.trim() ?? "";
113 if (pairingUrl.length > 0) {
114 const url = new URL(pairingUrl);
115 const hostedPairingRequest = readHostedPairingRequest(url);
116 if (hostedPairingRequest) {
117 const hostedBackendUrl = normalizeRemoteBaseUrl(hostedPairingRequest.host);
118 return {
119 credential: hostedPairingRequest.token,
120 httpBaseUrl: toHttpBaseUrl(hostedBackendUrl),
121 wsBaseUrl: toWsBaseUrl(hostedBackendUrl),
122 };
123 }
124​
125 const credential = getPairingTokenFromUrl(url) ?? "";
126 if (!credential) {
127 throw new Error("Pairing URL is missing its token.");
128 }
129 return {
130 credential,
131 httpBaseUrl: toHttpBaseUrl(url),
132 wsBaseUrl: toWsBaseUrl(url),
133 };
134 }
135​
136 const host = input.host?.trim() ?? "";
137 const pairingCode = input.pairingCode?.trim() ?? "";
138 if (!host) {
139 throw new Error("Enter a backend URL.");
140 }
141 if (!pairingCode) {
142 throw new Error("Enter a pairing code.");
143 }
144​
145 const normalizedHost = normalizeRemoteBaseUrl(host);
146 return {
147 credential: pairingCode,
148 httpBaseUrl: toHttpBaseUrl(normalizedHost),
149 wsBaseUrl: toWsBaseUrl(normalizedHost),
150 };
151};