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

browserTargetResolver.ts

108 lines3.9 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 // localhost lets Chromium reach loopback servers bound to either IPv4 or IPv6.
44 const resolvedHost = isLocalLoopbackHost(normalizedEnvironmentHost)
45 ? "localhost"
46 : normalizedEnvironmentHost.includes(":")
47 ? `[${normalizedEnvironmentHost}]`
48 : normalizedEnvironmentHost;
49 const resolved = sourceUrl
50 ? new URL(sourceUrl)
51 : new URL(path, `${protocol}://${resolvedHost}:${target.port}`);
52 if (sourceUrl) {
53 resolved.hostname = resolvedHost;
54 resolved.port = String(target.port);
55 }
56 return {
57 requestedUrl: requestedUrl ?? `${protocol}://localhost:${target.port}${path}`,
58 resolvedUrl: resolved.toString(),
59 resolutionKind: isLocalLoopbackHost(normalizedEnvironmentHost)
60 ? "direct"
61 : "direct-private-network",
62 environmentId,
63 };
64};
65​
66export function resolveBrowserNavigationTarget(
67 environmentId: EnvironmentId,
68 target: BrowserNavigationTarget,
69): PreviewUrlResolution {
70 if (target.kind === "url") {
71 return {
72 requestedUrl: target.url,
73 resolvedUrl: target.url,
74 resolutionKind: "direct",
75 environmentId,
76 };
77 }
78 return resolveEnvironmentPortTarget(environmentId, target, readEnvironmentUrl(environmentId));
80​
81// Desktop renders primary and WSL tabs locally; loopback servers require localhost even for LAN backends.
82const reachesEnvironmentLoopback = (target: ConnectionTarget): boolean =>
83 isElectron &&
84 (target._tag === "PrimaryConnectionTarget" || isDesktopLocalConnectionTarget(target));
85​
86export function resolveDiscoveredServerUrl(environmentId: EnvironmentId, rawUrl: string): string {
87 try {
88 const normalizedUrl = normalizePreviewUrl(rawUrl);
89 const parsed = new URL(normalizedUrl);
90 if (!isLoopbackHost(parsed.hostname)) return normalizedUrl;
91 const target = readPreparedConnection(environmentId)?.target;
92 if (target && reachesEnvironmentLoopback(target)) return normalizedUrl;
93 return resolveEnvironmentPortTarget(
94 environmentId,
95 {
96 kind: "environment-port",
97 port: Number(parsed.port || (parsed.protocol === "https:" ? 443 : 80)),
98 protocol: parsed.protocol === "https:" ? "https" : "http",
99 path: `${parsed.pathname}${parsed.search}${parsed.hash}`,
100 },
101 readEnvironmentUrl(environmentId),
102 rawUrl,
103 parsed,
104 ).resolvedUrl;
105 } catch {
106 return rawUrl;
107 }