Open History to see the last commit
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;
13}
14
15function normalizeBaseUrl(rawValue: string): string {
16 return new URL(rawValue, window.location.origin).toString();
17}
18
19function normalizeHostname(hostname: string): string {
20 return hostname
21 .trim()
22 .toLowerCase()
23 .replace(/^\[(.*)\]$/, "$1");
24}
25
26export function isLoopbackHostname(hostname: string): boolean {
27 return LOOPBACK_HOSTNAMES.has(normalizeHostname(hostname));
28}
29
30function resolveHttpRequestBaseUrl(httpBaseUrl: string): string {
31 const configuredDevServerUrl = import.meta.env.VITE_DEV_SERVER_URL?.trim();
32 if (!configuredDevServerUrl) {
33 return httpBaseUrl;
34 }
35
36 const currentUrl = new URL(window.location.href);
37 const targetUrl = new URL(httpBaseUrl);
38 const devServerUrl = new URL(configuredDevServerUrl, currentUrl.origin);
39
40 const isCurrentOriginDevServer =
41 (currentUrl.protocol === "http:" || currentUrl.protocol === "https:") &&
42 currentUrl.origin === devServerUrl.origin;
43
44 if (
45 !isCurrentOriginDevServer ||
46 currentUrl.origin === targetUrl.origin ||
47 !isLoopbackHostname(currentUrl.hostname) ||
48 !isLoopbackHostname(targetUrl.hostname)
49 ) {
50 return httpBaseUrl;
51 }
52
53 return currentUrl.origin;
54}
55
56function resolveConfiguredPrimaryTarget(): PrimaryEnvironmentTarget | null {
57 const configuredHttpBaseUrl = import.meta.env.VITE_HTTP_URL?.trim();
58 const configuredWsBaseUrl = import.meta.env.VITE_WS_URL?.trim();
59
60 if (!configuredHttpBaseUrl && !configuredWsBaseUrl) {
61 return null;
62 }
63
64 if (!configuredHttpBaseUrl || !configuredWsBaseUrl) {
65 throw new Error("Configured primary environments require both VITE_HTTP_URL and VITE_WS_URL.");
66 }
67
68 return {
69 source: "configured",
70 target: {
71 httpBaseUrl: normalizeBaseUrl(configuredHttpBaseUrl),
72 wsBaseUrl: normalizeBaseUrl(configuredWsBaseUrl),
73 },
74 };
75}
76
77function resolveWindowOriginPrimaryTarget(): PrimaryEnvironmentTarget {
78 const httpBaseUrl = normalizeBaseUrl(window.location.origin);
79 const url = new URL(httpBaseUrl);
80 if (url.protocol === "http:") {
81 url.protocol = "ws:";
82 } else if (url.protocol === "https:") {
83 url.protocol = "wss:";
84 } else {
85 throw new Error(`Unsupported HTTP base URL protocol: ${url.protocol}`);
86 }
87 return {
88 source: "window-origin",
89 target: {
90 httpBaseUrl,
91 wsBaseUrl: url.toString(),
92 },
93 };
94}
95
96function resolveDesktopPrimaryTarget(): PrimaryEnvironmentTarget | null {
97 const desktopBootstrap = getDesktopLocalEnvironmentBootstrap();
98 if (!desktopBootstrap) {
99 return null;
100 }
101 if (!desktopBootstrap.httpBaseUrl && !desktopBootstrap.wsBaseUrl) {
102 return null;
103 }
104 if (!desktopBootstrap.httpBaseUrl || !desktopBootstrap.wsBaseUrl) {
105 throw new Error(
106 "Desktop bootstrap must provide both httpBaseUrl and wsBaseUrl for the local environment.",
107 );
108 }
109
110 return {
111 source: "desktop-managed",
112 target: {
113 httpBaseUrl: normalizeBaseUrl(desktopBootstrap.httpBaseUrl),
114 wsBaseUrl: normalizeBaseUrl(desktopBootstrap.wsBaseUrl),
115 },
116 };
117}
118
119export function resolvePrimaryEnvironmentHttpUrl(
120 pathname: string,
121 searchParams?: Record<string, string>,
122): string {
123 const primaryTarget = readPrimaryEnvironmentTarget();
124 if (!primaryTarget) {
125 throw new Error("Unable to resolve the primary environment HTTP base URL.");
126 }
127
128 const url = new URL(resolveHttpRequestBaseUrl(primaryTarget.target.httpBaseUrl));
129 url.pathname = pathname;
130 if (searchParams) {
131 url.search = new URLSearchParams(searchParams).toString();
132 }
133 return url.toString();
134}
135
136export function readPrimaryEnvironmentTarget(): PrimaryEnvironmentTarget | null {
137 return (
138 resolveDesktopPrimaryTarget() ??
139 resolveConfiguredPrimaryTarget() ??
140 resolveWindowOriginPrimaryTarget()
141 );
142}