( proxies: readonly Region[], latencies?: Record<string, ProxyLatencyReport>, )
| 243 | }; |
| 244 | |
| 245 | const selectByLatency = ( |
| 246 | proxies: readonly Region[], |
| 247 | latencies?: Record<string, ProxyLatencyReport>, |
| 248 | ): Region | undefined => { |
| 249 | if (!latencies) { |
| 250 | return undefined; |
| 251 | } |
| 252 | |
| 253 | const proxyMap = Object.fromEntries(proxies.map((it) => [it.id, it])); |
| 254 | const best = Object.entries(latencies) |
| 255 | .map(([proxyId, latency]) => ({ id: proxyId, ...latency })) |
| 256 | // If the proxy is not in our list, or it is unhealthy, ignore it. |
| 257 | .filter((latency) => proxyMap[latency.id]?.healthy) |
| 258 | .sort((a, b) => a.latencyMS - b.latencyMS) |
| 259 | .at(0); |
| 260 | |
| 261 | // Found a new best, use it! |
| 262 | if (best) { |
| 263 | const bestProxy = proxies.find((proxy) => proxy.id === best.id); |
| 264 | // Default to w/e it was before |
| 265 | return bestProxy; |
| 266 | } |
| 267 | |
| 268 | return undefined; |
| 269 | }; |
| 270 | |
| 271 | const computeUsableURLS = (proxy?: Region): PreferredProxy => { |
| 272 | if (!proxy) { |
no test coverage detected