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

browserTargetResolver.ts

98 lines3.4 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 { readPreparedConnection } from "~/state/session";
10​
11export {
12 normalizeHostname,
13 isLocalLoopbackHost,
14 isPrivateNetworkHost,
15 isPublicFaviconHost,
16} from "@t3tools/shared/hostClassification";
17​
18const readEnvironmentUrl = (environmentId: EnvironmentId): URL => {
19 const connection = readPreparedConnection(environmentId);
20 if (!connection) throw new Error(`Environment ${environmentId} is not connected.`);
21 return new URL(connection.httpBaseUrl);
22};
23​
24const resolveEnvironmentPortTarget = (
25 environmentId: EnvironmentId,
26 target: Extract<BrowserNavigationTarget, { readonly kind: "environment-port" }>,
27 environmentUrl: URL,
28 requestedUrl?: string,
29 sourceUrl?: URL,
30): PreviewUrlResolution => {
31 if (!isPrivateNetworkHost(environmentUrl.hostname)) {
32 throw new Error(
33 "This environment port needs the planned authenticated preview gateway; its server address is not directly private-network reachable.",
34 );
35 }
36 const protocol = target.protocol ?? "http";
37 const path = target.path?.startsWith("/") ? target.path : `/${target.path ?? ""}`;
38 const normalizedEnvironmentHost = environmentUrl.hostname.replace(/^\[|\]$/g, "");
39 // Local loopback environments should advertise `localhost` so Chromium
40 // dual-stack lookup can reach a Vite server bound only to ::1 or 127.0.0.1.
41 const resolvedHost = isLocalLoopbackHost(normalizedEnvironmentHost)
42 ? "localhost"
43 : normalizedEnvironmentHost.includes(":")
44 ? `[${normalizedEnvironmentHost}]`
45 : normalizedEnvironmentHost;
46 const resolved = sourceUrl
47 ? new URL(sourceUrl)
48 : new URL(path, `${protocol}://${resolvedHost}:${target.port}`);
49 if (sourceUrl) {
50 resolved.hostname = resolvedHost;
51 resolved.port = String(target.port);
52 }
53 return {
54 requestedUrl: requestedUrl ?? `${protocol}://localhost:${target.port}${path}`,
55 resolvedUrl: resolved.toString(),
56 resolutionKind: isLocalLoopbackHost(normalizedEnvironmentHost)
57 ? "direct"
58 : "direct-private-network",
59 environmentId,
60 };
61};
62​
63export function resolveBrowserNavigationTarget(
64 environmentId: EnvironmentId,
65 target: BrowserNavigationTarget,
66): PreviewUrlResolution {
67 if (target.kind === "url") {
68 return {
69 requestedUrl: target.url,
70 resolvedUrl: target.url,
71 resolutionKind: "direct",
72 environmentId,
73 };
74 }
75 return resolveEnvironmentPortTarget(environmentId, target, readEnvironmentUrl(environmentId));
77​
78export function resolveDiscoveredServerUrl(environmentId: EnvironmentId, rawUrl: string): string {
79 try {
80 const normalizedUrl = normalizePreviewUrl(rawUrl);
81 const parsed = new URL(normalizedUrl);
82 if (!isLoopbackHost(parsed.hostname)) return normalizedUrl;
83 return resolveEnvironmentPortTarget(
84 environmentId,
85 {
86 kind: "environment-port",
87 port: Number(parsed.port || (parsed.protocol === "https:" ? 443 : 80)),
88 protocol: parsed.protocol === "https:" ? "https" : "http",
89 path: `${parsed.pathname}${parsed.search}${parsed.hash}`,
90 },
91 readEnvironmentUrl(environmentId),
92 rawUrl,
93 parsed,
94 ).resolvedUrl;
95 } catch {
96 return rawUrl;
97 }