The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import type { DiscoveredLocalServer } from "@t3tools/contracts"; |
| 2 | import { isLoopbackHost } from "@t3tools/shared/preview"; |
| 3 | |
| 4 | import type { EnvironmentId } from "@t3tools/contracts"; |
| 5 | import { resolveDiscoveredServerUrl } from "~/browser/browserTargetResolver"; |
| 6 | import { useDiscoveredPortsState } from "~/portDiscoveryState"; |
| 7 | |
| 8 | export interface PreviewableServer extends DiscoveredLocalServer { |
| 9 | source: "scanner" | "configured"; |
| 10 | /** Pre-resolution loopback URL used as the history key; remote navigation URLs are volatile. */ |
| 11 | requestedUrl: string; |
| 12 | } |
| 13 | |
| 14 | interface UseDiscoveredLocalServersInput { |
| 15 | environmentId: EnvironmentId; |
| 16 | configuredUrls?: ReadonlyArray<string> | undefined; |
| 17 | } |
| 18 | |
| 19 | export function useDiscoveredLocalServers( |
| 20 | input: UseDiscoveredLocalServersInput, |
| 21 | ): ReadonlyArray<PreviewableServer> { |
| 22 | const scannerState = useDiscoveredPortsState(input.environmentId, input.configuredUrls); |
| 23 | |
| 24 | return mergeServers({ |
| 25 | scanner: scannerState.servers.map((server) => ({ |
| 26 | ...server, |
| 27 | url: resolveDiscoveredServerUrl(input.environmentId, server.url), |
| 28 | requestedUrl: server.url, |
| 29 | })), |
| 30 | configuredUrls: input.configuredUrls ?? [], |
| 31 | configuredUrlProbing: scannerState.configuredUrlProbing, |
| 32 | }); |
| 33 | } |
| 34 | |
| 35 | export function mergeServers(input: { |
| 36 | scanner: ReadonlyArray<DiscoveredLocalServer & { requestedUrl: string }>; |
| 37 | configuredUrls: ReadonlyArray<string>; |
| 38 | configuredUrlProbing?: boolean; |
| 39 | }): ReadonlyArray<PreviewableServer> { |
| 40 | const configuredByServer = new Map<string, { host: string; port: number; url: string }>(); |
| 41 | |
| 42 | for (const url of input.configuredUrls) { |
| 43 | const parsed = parseLocalUrl(url); |
| 44 | if (!parsed) continue; |
| 45 | const key = canonicalKey(parsed.host, parsed.port); |
| 46 | if (!configuredByServer.has(key)) configuredByServer.set(key, parsed); |
| 47 | } |
| 48 | |
| 49 | const live: PreviewableServer[] = []; |
| 50 | for (const server of input.scanner) { |
| 51 | const key = canonicalKey(server.host, server.port); |
| 52 | const configured = configuredByServer.get(key); |
| 53 | live.push({ |
| 54 | ...server, |
| 55 | requestedUrl: |
| 56 | configured && input.configuredUrlProbing === false ? configured.url : server.requestedUrl, |
| 57 | source: configured ? "configured" : "scanner", |
| 58 | }); |
| 59 | } |
| 60 | |
| 61 | return live.toSorted((a, b) => { |
| 62 | const sourceOrder: Record<PreviewableServer["source"], number> = { |
| 63 | configured: 0, |
| 64 | scanner: 1, |
| 65 | }; |
| 66 | if (sourceOrder[a.source] !== sourceOrder[b.source]) { |
| 67 | return sourceOrder[a.source] - sourceOrder[b.source]; |
| 68 | } |
| 69 | return a.port - b.port; |
| 70 | }); |
| 71 | } |
| 72 | |
| 73 | function canonicalKey(host: string, port: number): string { |
| 74 | const normalizedHost = host.toLowerCase(); |
| 75 | return `${isLoopbackHost(normalizedHost) ? "loopback" : normalizedHost}:${port}`; |
| 76 | } |
| 77 | |
| 78 | function parseLocalUrl(raw: string): { host: string; port: number; url: string } | null { |
| 79 | try { |
| 80 | const parsed = new URL(raw); |
| 81 | if (parsed.protocol !== "http:" && parsed.protocol !== "https:") return null; |
| 82 | if (!isLoopbackHost(parsed.hostname)) return null; |
| 83 | const port = parsed.port |
| 84 | ? Number.parseInt(parsed.port, 10) |
| 85 | : parsed.protocol === "http:" |
| 86 | ? 80 |
| 87 | : 443; |
| 88 | if (!Number.isFinite(port) || port <= 0) return null; |
| 89 | return { host: parsed.hostname, port, url: parsed.href }; |
| 90 | } catch { |
| 91 | return null; |
| 92 | } |
| 93 | } |