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

use different registry when in china, fixes #3700 #3702

Merged
merged 7 commits into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
2 changes: 2 additions & 0 deletions reflex/constants/installer.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ class Bun(SimpleNamespace):
WINDOWS_INSTALL_URL = (
"https://raw.githubusercontent.com/reflex-dev/reflex/main/scripts/install.ps1"
)
# Path of the bunfig file
CONFIG_PATH = "bunfig.toml"


# FNM config.
Expand Down
10 changes: 10 additions & 0 deletions reflex/utils/prerequisites.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
from reflex.config import Config, get_config
from reflex.utils import console, path_ops, processes
from reflex.utils.format import format_library_name
from reflex.utils.registry import _get_best_registry

CURRENTLY_INSTALLING_NODE = False

Expand Down Expand Up @@ -577,6 +578,15 @@ def initialize_package_json():
code = _compile_package_json()
output_path.write_text(code)

best_registry = _get_best_registry()
bun_config_path = get_web_dir() / constants.Bun.CONFIG_PATH
bun_config_path.write_text(
f"""
[install]
registry = "{best_registry}"
"""
)


def init_reflex_json(project_hash: int | None):
"""Write the hash of the Reflex project to a REFLEX_JSON.
Expand Down
40 changes: 40 additions & 0 deletions reflex/utils/registry.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import httpx


def latency(registry: str) -> int:
"""Get the latency of a registry.

Args:
registry (str): The URL of the registry.

Returns:
int: The latency of the registry in microseconds.
"""
return httpx.get(registry).elapsed.microseconds


def average_latency(registry, attempts: int = 3) -> int:
"""Get the average latency of a registry.

Args:
registry (str): The URL of the registry.
attempts (int, optional): The number of attempts to make. Defaults to 10.

Returns:
int: The average latency of the registry in microseconds.
"""
return sum(latency(registry) for _ in range(attempts)) // attempts


def _get_best_registry() -> str:
"""Get the best registry based on latency.

Returns:
str: The best registry.
"""
registries = [
"https://registry.npmjs.org",
"https://r.cnpmjs.org",
]

return min(registries, key=average_latency)
Loading