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 { readEnvironmentConnection } from "~/environments/runtime";
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
27export function resolveBrowserNavigationTarget(
28 environmentId: EnvironmentId,
29 target: BrowserNavigationTarget,
30): PreviewUrlResolution {
31 if (target.kind === "url") {
32 return {
33 requestedUrl: target.url,
34 resolvedUrl: target.url,
35 resolutionKind: "direct",
36 environmentId,
37 };
38 }
39 const connection = readEnvironmentConnection(environmentId);
40 if (!connection) throw new Error(`Environment ${environmentId} is not connected.`);
41 const environmentUrl = new URL(connection.knownEnvironment.target.httpBaseUrl);
42 if (!isPrivateNetworkHost(environmentUrl.hostname)) {
43 throw new Error(
44 "This environment port needs the planned authenticated preview gateway; its server address is not directly private-network reachable.",
45 );
46 }
47 const protocol = target.protocol ?? "http";
48 const path = target.path?.startsWith("/") ? target.path : `/${target.path ?? ""}`;
49 const requestedUrl = `${protocol}://localhost:${target.port}${path}`;
50 const normalizedEnvironmentHost = environmentUrl.hostname.replace(/^\[|\]$/g, "");
51 const resolvedHost = normalizedEnvironmentHost.includes(":")
52 ? `[${normalizedEnvironmentHost}]`
53 : normalizedEnvironmentHost;
54 const resolved = new URL(path, `${protocol}://${resolvedHost}:${target.port}`);
55 return {
56 requestedUrl,
57 resolvedUrl: resolved.toString(),
58 resolutionKind:
59 normalizedEnvironmentHost === "localhost" || normalizedEnvironmentHost === "127.0.0.1"
60 ? "direct"
61 : "direct-private-network",
62 environmentId,
63 };
64}
65
66export function resolveDiscoveredServerUrl(environmentId: EnvironmentId, rawUrl: string): string {
67 try {
68 const normalizedUrl = normalizePreviewUrl(rawUrl);
69 const parsed = new URL(normalizedUrl);
70 if (!isLoopbackHost(parsed.hostname)) return normalizedUrl;
71 const port = Number(parsed.port || (parsed.protocol === "https:" ? 443 : 80));
72 return resolveBrowserNavigationTarget(environmentId, {
73 kind: "environment-port",
74 port,
75 protocol: parsed.protocol === "https:" ? "https" : "http",
76 path: `${parsed.pathname}${parsed.search}${parsed.hash}`,
77 }).resolvedUrl;
78 } catch {
79 return rawUrl;
80 }
81}