|
@@ -1,8 +1,10 @@
|
|
|
"""Utilities for working with registries."""
|
|
|
|
|
|
+import os
|
|
|
+
|
|
|
import httpx
|
|
|
|
|
|
-from reflex.utils import console
|
|
|
+from reflex.utils import console, net
|
|
|
|
|
|
|
|
|
def latency(registry: str) -> int:
|
|
@@ -15,7 +17,7 @@ def latency(registry: str) -> int:
|
|
|
int: The latency of the registry in microseconds.
|
|
|
"""
|
|
|
try:
|
|
|
- return httpx.get(registry).elapsed.microseconds
|
|
|
+ return net.get(registry).elapsed.microseconds
|
|
|
except httpx.HTTPError:
|
|
|
console.info(f"Failed to connect to {registry}.")
|
|
|
return 10_000_000
|
|
@@ -34,7 +36,7 @@ def average_latency(registry, attempts: int = 3) -> int:
|
|
|
return sum(latency(registry) for _ in range(attempts)) // attempts
|
|
|
|
|
|
|
|
|
-def _get_best_registry() -> str:
|
|
|
+def get_best_registry() -> str:
|
|
|
"""Get the best registry based on latency.
|
|
|
|
|
|
Returns:
|
|
@@ -46,3 +48,15 @@ def _get_best_registry() -> str:
|
|
|
]
|
|
|
|
|
|
return min(registries, key=average_latency)
|
|
|
+
|
|
|
+
|
|
|
+def _get_npm_registry() -> str:
|
|
|
+ """Get npm registry. If environment variable is set, use it first.
|
|
|
+
|
|
|
+ Returns:
|
|
|
+ str:
|
|
|
+ """
|
|
|
+ if npm_registry := os.environ.get("NPM_CONFIG_REGISTRY", ""):
|
|
|
+ return npm_registry
|
|
|
+ else:
|
|
|
+ return get_best_registry()
|