Open History to see the last commit
1import type {
2 BrowserNavigationTarget,
3 EnvironmentId,
4 PreviewUrlResolution,
5} from "@t3tools/contracts";
6import { isLoopbackHost, normalizePreviewUrl } from "@t3tools/shared/preview";
7
8import { readPreparedConnection } from "~/state/session";
9
10const isPrivateNetworkHost = (host: string): boolean => {
11 const normalized = host.toLowerCase().replace(/^\[|\]$/g, "");
12 if (normalized === "localhost" || normalized === "::1" || normalized.endsWith(".local")) {
13 return true;
14 }
15 if (normalized.endsWith(".ts.net")) return true;
16 const parts = normalized.split(".").map(Number);
17 if (parts.length !== 4 || parts.some((part) => !Number.isInteger(part))) return false;
18 return (
19 parts[0] === 10 ||
20 (parts[0] === 172 && parts[1]! >= 16 && parts[1]! <= 31) ||
21 (parts[0] === 192 && parts[1] === 168) ||
22 parts[0] === 127 ||
23 (parts[0] === 169 && parts[1] === 254)
24 );
25};
26
27const isLocalLoopbackHost = (host: string): boolean => {
28 const normalized = host.toLowerCase().replace(/^\[|\]$/g, "");
29 return normalized === "localhost" || normalized === "127.0.0.1" || normalized === "::1";
30};
31
32export function resolveBrowserNavigationTarget(
33 environmentId: EnvironmentId,
34 target: BrowserNavigationTarget,
35): PreviewUrlResolution {
36 if (target.kind === "url") {
37 return {
38 requestedUrl: target.url,
39 resolvedUrl: target.url,
40 resolutionKind: "direct",
41 environmentId,
42 };
43 }
44 const connection = readPreparedConnection(environmentId);
45 if (!connection) throw new Error(`Environment ${environmentId} is not connected.`);
46 const environmentUrl = new URL(connection.httpBaseUrl);
47 if (!isPrivateNetworkHost(environmentUrl.hostname)) {
48 throw new Error(
49 "This environment port needs the planned authenticated preview gateway; its server address is not directly private-network reachable.",
50 );
51 }
52 const protocol = target.protocol ?? "http";
53 const path = target.path?.startsWith("/") ? target.path : `/${target.path ?? ""}`;
54 const requestedUrl = `${protocol}://localhost:${target.port}${path}`;
55 const normalizedEnvironmentHost = environmentUrl.hostname.replace(/^\[|\]$/g, "");
56 const resolvedHost = normalizedEnvironmentHost.includes(":")
57 ? `[${normalizedEnvironmentHost}]`
58 : normalizedEnvironmentHost;
59 const resolved = new URL(path, `${protocol}://${resolvedHost}:${target.port}`);
60 return {
61 requestedUrl,
62 resolvedUrl: resolved.toString(),
63 resolutionKind:
64 normalizedEnvironmentHost === "localhost" || normalizedEnvironmentHost === "127.0.0.1"
65 ? "direct"
66 : "direct-private-network",
67 environmentId,
68 };
69}
70
71export function resolveDiscoveredServerUrl(environmentId: EnvironmentId, rawUrl: string): string {
72 try {
73 const normalizedUrl = normalizePreviewUrl(rawUrl);
74 const parsed = new URL(normalizedUrl);
75 if (!isLoopbackHost(parsed.hostname)) return normalizedUrl;
76 const connection = readPreparedConnection(environmentId);
77 if (!connection) throw new Error(`Environment ${environmentId} is not connected.`);
78 const environmentUrl = new URL(connection.httpBaseUrl);
79 if (parsed.hostname !== "0.0.0.0" && isLocalLoopbackHost(environmentUrl.hostname)) {
80 return normalizedUrl;
81 }
82 const port = Number(parsed.port || (parsed.protocol === "https:" ? 443 : 80));
83 return resolveBrowserNavigationTarget(environmentId, {
84 kind: "environment-port",
85 port,
86 protocol: parsed.protocol === "https:" ? "https" : "http",
87 path: `${parsed.pathname}${parsed.search}${parsed.hash}`,
88 }).resolvedUrl;
89 } catch {
90 return rawUrl;
91 }
92}