First use environment variable as npm registry (#4082)

* First use environment variable as npm registry

* use NPM_CONFIG_REGISTRY as env variable

---------

Co-authored-by: 류형주/인공지능팀 <hyungju.ryu@ahnlab.com>
This commit is contained in:
ruhz3 2024-10-10 03:36:33 +09:00 committed by GitHub
parent 0e7627d1c4
commit f3b8b2e336
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 19 additions and 5 deletions

View File

@ -37,7 +37,7 @@ from reflex.config import Config, get_config
from reflex.utils import console, net, path_ops, processes
from reflex.utils.exceptions import GeneratedCodeHasNoFunctionDefs
from reflex.utils.format import format_library_name
from reflex.utils.registry import _get_best_registry
from reflex.utils.registry import _get_npm_registry
CURRENTLY_INSTALLING_NODE = False
@ -620,7 +620,7 @@ def initialize_package_json():
code = _compile_package_json()
output_path.write_text(code)
best_registry = _get_best_registry()
best_registry = _get_npm_registry()
bun_config_path = get_web_dir() / constants.Bun.CONFIG_PATH
bun_config_path.write_text(
f"""

View File

@ -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()