( proxies: readonly Region[], selectedProxy?: Region, latencies?: Record<string, ProxyLatencyReport>, autoSelectBasedOnLatency = true, )
| 216 | * If not, `primary` is always the best default. |
| 217 | */ |
| 218 | export const getPreferredProxy = ( |
| 219 | proxies: readonly Region[], |
| 220 | selectedProxy?: Region, |
| 221 | latencies?: Record<string, ProxyLatencyReport>, |
| 222 | autoSelectBasedOnLatency = true, |
| 223 | ): PreferredProxy => { |
| 224 | // If a proxy is selected, make sure it is in the list of proxies. If it is not |
| 225 | // we should default to the primary. |
| 226 | selectedProxy = proxies.find( |
| 227 | (proxy) => selectedProxy && proxy.id === selectedProxy.id, |
| 228 | ); |
| 229 | |
| 230 | // If no proxy is selected, or the selected proxy is unhealthy default to the primary proxy. |
| 231 | if (!selectedProxy?.healthy) { |
| 232 | // Default to the primary proxy |
| 233 | selectedProxy = proxies.find((proxy) => proxy.name === "primary"); |
| 234 | |
| 235 | // If we have latencies, then attempt to use the best proxy by latency instead. |
| 236 | const best = selectByLatency(proxies, latencies); |
| 237 | if (autoSelectBasedOnLatency && best !== undefined) { |
| 238 | selectedProxy = best; |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | return computeUsableURLS(selectedProxy); |
| 243 | }; |
| 244 | |
| 245 | const selectByLatency = ( |
| 246 | proxies: readonly Region[], |
no test coverage detected