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

target.ts

158 lines4.7 KB

1import type { DesktopEnvironmentBootstrap } from "@t3tools/contracts";
2import type { KnownEnvironment } from "@t3tools/client-runtime";
3​
4export interface PrimaryEnvironmentTarget {
5 readonly source: KnownEnvironment["source"];
6 readonly target: KnownEnvironment["target"];
7}
8​
9const LOOPBACK_HOSTNAMES = new Set(["127.0.0.1", "::1", "localhost"]);
10​
11function getDesktopLocalEnvironmentBootstrap(): DesktopEnvironmentBootstrap | null {
12 return window.desktopBridge?.getLocalEnvironmentBootstrap() ?? null;
14​
15function normalizeBaseUrl(rawValue: string): string {
16 return new URL(rawValue, window.location.origin).toString();
18​
19function swapBaseUrlProtocol(
20 rawValue: string,
21 nextProtocol: "http:" | "https:" | "ws:" | "wss:",
22): string {
23 const url = new URL(normalizeBaseUrl(rawValue));
24 url.protocol = nextProtocol;
25 return url.toString();
27​
28function normalizeHostname(hostname: string): string {
29 return hostname
30 .trim()
31 .toLowerCase()
32 .replace(/^\[(.*)\]$/, "$1");
34​
35export function isLoopbackHostname(hostname: string): boolean {
36 return LOOPBACK_HOSTNAMES.has(normalizeHostname(hostname));
38​
39function resolveHttpRequestBaseUrl(httpBaseUrl: string): string {
40 const configuredDevServerUrl = import.meta.env.VITE_DEV_SERVER_URL?.trim();
41 if (!configuredDevServerUrl) {
42 return httpBaseUrl;
43 }
44​
45 const currentUrl = new URL(window.location.href);
46 const targetUrl = new URL(httpBaseUrl);
47 const devServerUrl = new URL(configuredDevServerUrl, currentUrl.origin);
48​
49 const isCurrentOriginDevServer =
50 (currentUrl.protocol === "http:" || currentUrl.protocol === "https:") &&
51 currentUrl.origin === devServerUrl.origin;
52​
53 if (
54 !isCurrentOriginDevServer ||
55 currentUrl.origin === targetUrl.origin ||
56 !isLoopbackHostname(currentUrl.hostname) ||
57 !isLoopbackHostname(targetUrl.hostname)
58 ) {
59 return httpBaseUrl;
60 }
61​
62 return currentUrl.origin;
64​
65function resolveConfiguredPrimaryTarget(): PrimaryEnvironmentTarget | null {
66 const configuredHttpBaseUrl = import.meta.env.VITE_HTTP_URL?.trim() || undefined;
67 const configuredWsBaseUrl = import.meta.env.VITE_WS_URL?.trim() || undefined;
68​
69 if (!configuredHttpBaseUrl && !configuredWsBaseUrl) {
70 return null;
71 }
72​
73 const resolvedHttpBaseUrl =
74 configuredHttpBaseUrl ??
75 (configuredWsBaseUrl?.startsWith("wss:")
76 ? swapBaseUrlProtocol(configuredWsBaseUrl, "https:")
77 : swapBaseUrlProtocol(configuredWsBaseUrl!, "http:"));
78 const resolvedWsBaseUrl =
79 configuredWsBaseUrl ??
80 (configuredHttpBaseUrl?.startsWith("https:")
81 ? swapBaseUrlProtocol(configuredHttpBaseUrl, "wss:")
82 : swapBaseUrlProtocol(configuredHttpBaseUrl!, "ws:"));
83​
84 return {
85 source: "configured",
86 target: {
87 httpBaseUrl: normalizeBaseUrl(resolvedHttpBaseUrl),
88 wsBaseUrl: normalizeBaseUrl(resolvedWsBaseUrl),
89 },
90 };
92​
93function resolveWindowOriginPrimaryTarget(): PrimaryEnvironmentTarget {
94 const httpBaseUrl = normalizeBaseUrl(window.location.origin);
95 const url = new URL(httpBaseUrl);
96 if (url.protocol === "http:") {
97 url.protocol = "ws:";
98 } else if (url.protocol === "https:") {
99 url.protocol = "wss:";
100 } else {
101 throw new Error(`Unsupported HTTP base URL protocol: ${url.protocol}`);
102 }
103 return {
104 source: "window-origin",
105 target: {
106 httpBaseUrl,
107 wsBaseUrl: url.toString(),
108 },
109 };
111​
112function resolveDesktopPrimaryTarget(): PrimaryEnvironmentTarget | null {
113 const desktopBootstrap = getDesktopLocalEnvironmentBootstrap();
114 if (!desktopBootstrap) {
115 return null;
116 }
117 if (!desktopBootstrap.httpBaseUrl && !desktopBootstrap.wsBaseUrl) {
118 return null;
119 }
120 if (!desktopBootstrap.httpBaseUrl || !desktopBootstrap.wsBaseUrl) {
121 throw new Error(
122 "Desktop bootstrap must provide both httpBaseUrl and wsBaseUrl for the local environment.",
123 );
124 }
125​
126 return {
127 source: "desktop-managed",
128 target: {
129 httpBaseUrl: normalizeBaseUrl(desktopBootstrap.httpBaseUrl),
130 wsBaseUrl: normalizeBaseUrl(desktopBootstrap.wsBaseUrl),
131 },
132 };
134​
135export function resolvePrimaryEnvironmentHttpUrl(
136 pathname: string,
137 searchParams?: Record<string, string>,
138): string {
139 const primaryTarget = readPrimaryEnvironmentTarget();
140 if (!primaryTarget) {
141 throw new Error("Unable to resolve the primary environment HTTP base URL.");
142 }
143​
144 const url = new URL(resolveHttpRequestBaseUrl(primaryTarget.target.httpBaseUrl));
145 url.pathname = pathname;
146 if (searchParams) {
147 url.search = new URLSearchParams(searchParams).toString();
148 }
149 return url.toString();
151​
152export function readPrimaryEnvironmentTarget(): PrimaryEnvironmentTarget | null {
153 return (
154 resolveDesktopPrimaryTarget() ??
155 resolveConfiguredPrimaryTarget() ??
156 resolveWindowOriginPrimaryTarget()
157 );