Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

First use environment variable as npm registry #4082

Merged
merged 2 commits into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions reflex/utils/prerequisites.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
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

Expand Down Expand Up @@ -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"""
Expand Down
20 changes: 17 additions & 3 deletions reflex/utils/registry.py
Original file line number Diff line number Diff line change
@@ -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:
Expand All @@ -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
Expand All @@ -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:
Expand All @@ -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()
Loading