The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as Schema from "effect/Schema"; |
| 2 | |
| 3 | import { parseUrl } from "./url.ts"; |
| 4 | |
| 5 | const PAIRING_TOKEN_PARAM = "token"; |
| 6 | const HOSTED_PAIRING_HOST_PARAM = "host"; |
| 7 | const HOSTED_PAIRING_LABEL_PARAM = "label"; |
| 8 | const PAIRING_APP_LINK_PARAM = "pairingUrl"; |
| 9 | const APP_LINK_PROTOCOL = /^t3code(?:-[a-z]+)?:$/; |
| 10 | const SUPPORTED_REMOTE_BACKEND_PROTOCOLS = new Set(["http:", "https:", "ws:", "wss:"]); |
| 11 | |
| 12 | export const readHashParams = (url: URL): URLSearchParams => |
| 13 | new URLSearchParams(url.hash.startsWith("#") ? url.hash.slice(1) : url.hash); |
| 14 | |
| 15 | export class RemoteBackendUrlMissingError extends Schema.TaggedError<RemoteBackendUrlMissingError>()( |
| 16 | "RemoteBackendUrlMissingError", |
| 17 | {}, |
| 18 | ) { |
| 19 | override get message(): string { |
| 20 | return "Enter a backend URL."; |
| 21 | } |
| 22 | } |
| 23 | |
| 24 | export class RemotePairingUrlInvalidError extends Schema.TaggedError<RemotePairingUrlInvalidError>()( |
| 25 | "RemotePairingUrlInvalidError", |
| 26 | { |
| 27 | cause: Schema.optional(Schema.Defect()), |
| 28 | protocol: Schema.optional(Schema.String), |
| 29 | }, |
| 30 | ) { |
| 31 | override get message(): string { |
| 32 | return "Pairing URL is invalid."; |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | export class RemoteBackendUrlInvalidError extends Schema.TaggedError<RemoteBackendUrlInvalidError>()( |
| 37 | "RemoteBackendUrlInvalidError", |
| 38 | { |
| 39 | source: Schema.Literals(["direct-host", "hosted-pairing-host"]), |
| 40 | cause: Schema.optional(Schema.Defect()), |
| 41 | protocol: Schema.optional(Schema.String), |
| 42 | }, |
| 43 | ) { |
| 44 | override get message(): string { |
| 45 | return "Backend URL is invalid."; |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | export class RemotePairingTokenMissingError extends Schema.TaggedError<RemotePairingTokenMissingError>()( |
| 50 | "RemotePairingTokenMissingError", |
| 51 | { host: Schema.String }, |
| 52 | ) { |
| 53 | override get message(): string { |
| 54 | return "Pairing URL is missing its token."; |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | export class RemotePairingCodeMissingError extends Schema.TaggedError<RemotePairingCodeMissingError>()( |
| 59 | "RemotePairingCodeMissingError", |
| 60 | { host: Schema.String }, |
| 61 | ) { |
| 62 | override get message(): string { |
| 63 | return "Enter a pairing code."; |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | export const RemotePairingTargetError = Schema.Union([ |
| 68 | RemoteBackendUrlMissingError, |
| 69 | RemotePairingUrlInvalidError, |
| 70 | RemoteBackendUrlInvalidError, |
| 71 | RemotePairingTokenMissingError, |
| 72 | RemotePairingCodeMissingError, |
| 73 | ]); |
| 74 | export type RemotePairingTargetError = typeof RemotePairingTargetError.Type; |
| 75 | |
| 76 | const hasSupportedRemoteBackendProtocol = (url: URL): boolean => |
| 77 | SUPPORTED_REMOTE_BACKEND_PROTOCOLS.has(url.protocol); |
| 78 | |
| 79 | const normalizeRemoteBaseUrl = ( |
| 80 | rawValue: string, |
| 81 | source: RemoteBackendUrlInvalidError["source"], |
| 82 | ): URL => { |
| 83 | const trimmed = rawValue.trim(); |
| 84 | if (!trimmed) { |
| 85 | throw new RemoteBackendUrlMissingError(); |
| 86 | } |
| 87 | |
| 88 | const withoutLeadingSlashes = trimmed.replace(/^\/+/, ""); |
| 89 | const normalizedInput = /^[a-zA-Z][a-zA-Z\d+-]*:\/\//.test(withoutLeadingSlashes) |
| 90 | ? withoutLeadingSlashes |
| 91 | : `https://${withoutLeadingSlashes}`; |
| 92 | let url: URL; |
| 93 | try { |
| 94 | url = new URL(normalizedInput); |
| 95 | } catch (cause) { |
| 96 | throw new RemoteBackendUrlInvalidError({ source, cause }); |
| 97 | } |
| 98 | if (!hasSupportedRemoteBackendProtocol(url)) { |
| 99 | throw new RemoteBackendUrlInvalidError({ |
| 100 | source, |
| 101 | protocol: url.protocol, |
| 102 | }); |
| 103 | } |
| 104 | url.pathname = "/"; |
| 105 | url.search = ""; |
| 106 | url.hash = ""; |
| 107 | return url; |
| 108 | }; |
| 109 | |
| 110 | const toHttpBaseUrl = (url: URL): string => { |
| 111 | const next = new URL(url.toString()); |
| 112 | if (next.protocol === "ws:") { |
| 113 | next.protocol = "http:"; |
| 114 | } else if (next.protocol === "wss:") { |
| 115 | next.protocol = "https:"; |
| 116 | } |
| 117 | next.pathname = "/"; |
| 118 | next.search = ""; |
| 119 | next.hash = ""; |
| 120 | return next.toString(); |
| 121 | }; |
| 122 | |
| 123 | const toWsBaseUrl = (url: URL): string => { |
| 124 | const next = new URL(url.toString()); |
| 125 | if (next.protocol === "http:") { |
| 126 | next.protocol = "ws:"; |
| 127 | } else if (next.protocol === "https:") { |
| 128 | next.protocol = "wss:"; |
| 129 | } |
| 130 | next.pathname = "/"; |
| 131 | next.search = ""; |
| 132 | next.hash = ""; |
| 133 | return next.toString(); |
| 134 | }; |
| 135 | |
| 136 | export interface ResolvedRemotePairingTarget { |
| 137 | readonly credential: string; |
| 138 | readonly httpBaseUrl: string; |
| 139 | readonly wsBaseUrl: string; |
| 140 | } |
| 141 | |
| 142 | export interface HostedPairingRequest { |
| 143 | readonly host: string; |
| 144 | readonly token: string; |
| 145 | readonly label: string; |
| 146 | } |
| 147 | |
| 148 | export const getPairingTokenFromUrl = (url: URL): string | null => { |
| 149 | const hashToken = readHashParams(url).get(PAIRING_TOKEN_PARAM)?.trim() ?? ""; |
| 150 | if (hashToken.length > 0) { |
| 151 | return hashToken; |
| 152 | } |
| 153 | |
| 154 | const searchToken = url.searchParams.get(PAIRING_TOKEN_PARAM)?.trim() ?? ""; |
| 155 | return searchToken.length > 0 ? searchToken : null; |
| 156 | }; |
| 157 | |
| 158 | export const stripPairingTokenFromUrl = (url: URL): URL => { |
| 159 | const next = new URL(url.toString()); |
| 160 | const hashParams = readHashParams(next); |
| 161 | if (hashParams.has(PAIRING_TOKEN_PARAM)) { |
| 162 | hashParams.delete(PAIRING_TOKEN_PARAM); |
| 163 | next.hash = hashParams.toString(); |
| 164 | } |
| 165 | next.searchParams.delete(PAIRING_TOKEN_PARAM); |
| 166 | return next; |
| 167 | }; |
| 168 | |
| 169 | export const setPairingTokenOnUrl = (url: URL, credential: string): URL => { |
| 170 | const next = new URL(url.toString()); |
| 171 | next.searchParams.delete(PAIRING_TOKEN_PARAM); |
| 172 | next.hash = new URLSearchParams([[PAIRING_TOKEN_PARAM, credential]]).toString(); |
| 173 | return next; |
| 174 | }; |
| 175 | |
| 176 | export const readHostedPairingRequest = (url: URL): HostedPairingRequest | null => { |
| 177 | const host = url.searchParams.get(HOSTED_PAIRING_HOST_PARAM)?.trim() ?? ""; |
| 178 | const token = getPairingTokenFromUrl(url)?.trim() ?? ""; |
| 179 | const label = url.searchParams.get(HOSTED_PAIRING_LABEL_PARAM)?.trim() ?? ""; |
| 180 | |
| 181 | if (!host || !token) { |
| 182 | return null; |
| 183 | } |
| 184 | |
| 185 | return { |
| 186 | host, |
| 187 | token, |
| 188 | label, |
| 189 | }; |
| 190 | }; |
| 191 | |
| 192 | /** Wraps a pairing URL in the `t3code://pair` link that desktop opens and mobile scans. */ |
| 193 | export const buildPairingAppLink = (pairingUrl: string): string => |
| 194 | `t3code://pair?${new URLSearchParams([[PAIRING_APP_LINK_PARAM, pairingUrl]])}`; |
| 195 | |
| 196 | /** Reads the pairing URL from a `t3code://pair` link on any app channel scheme. */ |
| 197 | export const readPairingAppLink = (value: string): string | null => { |
| 198 | const url = parseUrl(value); |
| 199 | if (!url) return null; |
| 200 | if (!APP_LINK_PROTOCOL.test(url.protocol) || url.host !== "pair") return null; |
| 201 | if (url.pathname !== "" && url.pathname !== "/") return null; |
| 202 | return url.searchParams.get(PAIRING_APP_LINK_PARAM)?.trim() || null; |
| 203 | }; |
| 204 | |
| 205 | export const resolveRemotePairingTarget = (input: { |
| 206 | readonly pairingUrl?: string; |
| 207 | readonly host?: string; |
| 208 | readonly pairingCode?: string; |
| 209 | }): ResolvedRemotePairingTarget => { |
| 210 | const pairingUrl = input.pairingUrl?.trim() ?? ""; |
| 211 | if (pairingUrl.length > 0) { |
| 212 | let url: URL; |
| 213 | try { |
| 214 | url = new URL(pairingUrl); |
| 215 | } catch (cause) { |
| 216 | throw new RemotePairingUrlInvalidError({ cause }); |
| 217 | } |
| 218 | if (!hasSupportedRemoteBackendProtocol(url)) { |
| 219 | throw new RemotePairingUrlInvalidError({ |
| 220 | protocol: url.protocol, |
| 221 | }); |
| 222 | } |
| 223 | const hostedPairingRequest = readHostedPairingRequest(url); |
| 224 | if (hostedPairingRequest) { |
| 225 | const hostedBackendUrl = normalizeRemoteBaseUrl( |
| 226 | hostedPairingRequest.host, |
| 227 | "hosted-pairing-host", |
| 228 | ); |
| 229 | return { |
| 230 | credential: hostedPairingRequest.token, |
| 231 | httpBaseUrl: toHttpBaseUrl(hostedBackendUrl), |
| 232 | wsBaseUrl: toWsBaseUrl(hostedBackendUrl), |
| 233 | }; |
| 234 | } |
| 235 | |
| 236 | const credential = getPairingTokenFromUrl(url) ?? ""; |
| 237 | if (!credential) { |
| 238 | throw new RemotePairingTokenMissingError({ host: url.host }); |
| 239 | } |
| 240 | return { |
| 241 | credential, |
| 242 | httpBaseUrl: toHttpBaseUrl(url), |
| 243 | wsBaseUrl: toWsBaseUrl(url), |
| 244 | }; |
| 245 | } |
| 246 | |
| 247 | const host = input.host?.trim() ?? ""; |
| 248 | const pairingCode = input.pairingCode?.trim() ?? ""; |
| 249 | if (!host) { |
| 250 | throw new RemoteBackendUrlMissingError(); |
| 251 | } |
| 252 | const normalizedHost = normalizeRemoteBaseUrl(host, "direct-host"); |
| 253 | if (!pairingCode) { |
| 254 | throw new RemotePairingCodeMissingError({ host: normalizedHost.host }); |
| 255 | } |
| 256 | |
| 257 | return { |
| 258 | credential: pairingCode, |
| 259 | httpBaseUrl: toHttpBaseUrl(normalizedHost), |
| 260 | wsBaseUrl: toWsBaseUrl(normalizedHost), |
| 261 | }; |
| 262 | }; |