(url: string)
| 902 | } |
| 903 | const baseUrl = getHMRWsUrl() || 'ws://localhost:5173/ns-hmr'; |
| 904 | const buildCandidates = (url: string): string[] => { |
| 905 | let candidates: string[] = []; |
| 906 | try { |
| 907 | const u = new URL(url); |
| 908 | const proto = u.protocol === 'wss:' ? ['wss'] : ['ws']; |
| 909 | const defaultPort = u.port || '5173'; |
| 910 | |
| 911 | // Build ordered host candidates with preference to the active HTTP origin |
| 912 | const orderedHosts: Array<{ host: string; port?: string }> = []; |
| 913 | try { |
| 914 | const g: any = globalThis as any; |
| 915 | const httpOrigin: string | undefined = g && typeof g.__NS_HTTP_ORIGIN__ === 'string' ? g.__NS_HTTP_ORIGIN__ : undefined; |
| 916 | if (httpOrigin) { |
| 917 | try { |
| 918 | const ho = new URL(httpOrigin); |
| 919 | orderedHosts.push({ |
| 920 | host: ho.hostname, |
| 921 | port: ho.port || defaultPort, |
| 922 | }); |
| 923 | } catch {} |
| 924 | } |
| 925 | } catch {} |
| 926 | |
| 927 | // From provided URL |
| 928 | orderedHosts.push({ host: u.hostname, port: defaultPort }); |
| 929 | // Explicit override |
| 930 | try { |
| 931 | const h = (globalThis as any).__NS_HMR_HOST; |
| 932 | if (h) orderedHosts.push({ host: String(h), port: defaultPort }); |
| 933 | } catch {} |
| 934 | // Common fallbacks |
| 935 | orderedHosts.push({ host: '10.0.2.2', port: defaultPort }); |
| 936 | orderedHosts.push({ host: 'localhost', port: defaultPort }); |
| 937 | |
| 938 | const seen = new Set<string>(); |
| 939 | for (const p of proto) { |
| 940 | for (const { host, port } of orderedHosts) { |
| 941 | const key = `${host}:${port}`; |
| 942 | if (seen.has(key)) continue; |
| 943 | seen.add(key); |
| 944 | const cand = `${p}://${host}:${port}${u.pathname || '/ns-hmr'}`; |
| 945 | candidates.push(cand); |
| 946 | } |
| 947 | } |
| 948 | } catch { |
| 949 | candidates.push(url); |
| 950 | } |
| 951 | // Deduplicate while preserving order |
| 952 | const seen = new Set<string>(); |
| 953 | return candidates.filter((c) => (seen.has(c) ? false : (seen.add(c), true))); |
| 954 | }; |
| 955 | |
| 956 | const candidates = buildCandidates(baseUrl); |
| 957 | let idx = 0; |
no test coverage detected