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

browserTargetResolver.ts

114 lines4.1 KB

1import type {
2 BrowserNavigationTarget,
3 EnvironmentId,
4 PreviewUrlResolution,
5} from "@t3tools/contracts";
6import { isLoopbackHost, normalizePreviewUrl } from "@t3tools/shared/preview";
7import { isLocalLoopbackHost, isPrivateNetworkHost } from "@t3tools/shared/hostClassification";
8​
9import type { ConnectionTarget } from "@t3tools/client-runtime/connection";
10​
11import { isDesktopLocalConnectionTarget } from "~/connection/desktopLocal";
12import { isElectron } from "~/env";
13import { readPreparedConnection } from "~/state/session";
14​
15export {
16 normalizeHostname,
17 isLocalLoopbackHost,
18 isPrivateNetworkHost,
19 isPublicFaviconHost,
20} from "@t3tools/shared/hostClassification";
21​
22const readEnvironmentUrl = (environmentId: EnvironmentId): URL => {
23 const connection = readPreparedConnection(environmentId);
24 if (!connection) throw new Error(`Environment ${environmentId} is not connected.`);
25 return new URL(connection.httpBaseUrl);
26};
27​
28const resolveEnvironmentPortTarget = (
29 environmentId: EnvironmentId,
30 target: Extract<BrowserNavigationTarget, { readonly kind: "environment-port" }>,
31 environmentUrl: URL,
32 requestedUrl?: string,
33 sourceUrl?: URL,
34): PreviewUrlResolution => {
35 if (!isPrivateNetworkHost(environmentUrl.hostname)) {
36 throw new Error(
37 "This environment port needs the planned authenticated preview gateway; its server address is not directly private-network reachable.",
38 );
39 }
40 const protocol = target.protocol ?? "http";
41 const path = target.path?.startsWith("/") ? target.path : `/${target.path ?? ""}`;
42 const normalizedEnvironmentHost = environmentUrl.hostname.replace(/^\[|\]$/g, "");
43 // Local loopback environments should advertise `localhost` so Chromium
44 // dual-stack lookup can reach a Vite server bound only to ::1 or 127.0.0.1.
45 const resolvedHost = isLocalLoopbackHost(normalizedEnvironmentHost)
46 ? "localhost"
47 : normalizedEnvironmentHost.includes(":")
48 ? `[${normalizedEnvironmentHost}]`
49 : normalizedEnvironmentHost;
50 const resolved = sourceUrl
51 ? new URL(sourceUrl)
52 : new URL(path, `${protocol}://${resolvedHost}:${target.port}`);
53 if (sourceUrl) {
54 resolved.hostname = resolvedHost;
55 resolved.port = String(target.port);
56 }
57 return {
58 requestedUrl: requestedUrl ?? `${protocol}://localhost:${target.port}${path}`,
59 resolvedUrl: resolved.toString(),
60 resolutionKind: isLocalLoopbackHost(normalizedEnvironmentHost)
61 ? "direct"
62 : "direct-private-network",
63 environmentId,
64 };
65};
66​
67export function resolveBrowserNavigationTarget(
68 environmentId: EnvironmentId,
69 target: BrowserNavigationTarget,
70): PreviewUrlResolution {
71 if (target.kind === "url") {
72 return {
73 requestedUrl: target.url,
74 resolvedUrl: target.url,
75 resolutionKind: "direct",
76 environmentId,
77 };
78 }
79 return resolveEnvironmentPortTarget(environmentId, target, readEnvironmentUrl(environmentId));
81​
82/**
83 * The desktop app draws tabs on the machine that runs its primary backend and
84 * any desktop-local backend (WSL). Its connection URL can still be a LAN
85 * address (WSL NAT mode binds the distro IP), but a dev server bound to
86 * loopback only answers on `localhost` there.
87 */
88const reachesEnvironmentLoopback = (target: ConnectionTarget): boolean =>
89 isElectron &&
90 (target._tag === "PrimaryConnectionTarget" || isDesktopLocalConnectionTarget(target));
91​
92export function resolveDiscoveredServerUrl(environmentId: EnvironmentId, rawUrl: string): string {
93 try {
94 const normalizedUrl = normalizePreviewUrl(rawUrl);
95 const parsed = new URL(normalizedUrl);
96 if (!isLoopbackHost(parsed.hostname)) return normalizedUrl;
97 const target = readPreparedConnection(environmentId)?.target;
98 if (target && reachesEnvironmentLoopback(target)) return normalizedUrl;
99 return resolveEnvironmentPortTarget(
100 environmentId,
101 {
102 kind: "environment-port",
103 port: Number(parsed.port || (parsed.protocol === "https:" ? 443 : 80)),
104 protocol: parsed.protocol === "https:" ? "https" : "http",
105 path: `${parsed.pathname}${parsed.search}${parsed.hash}`,
106 },
107 readEnvironmentUrl(environmentId),
108 rawUrl,
109 parsed,
110 ).resolvedUrl;
111 } catch {
112 return rawUrl;
113 }