From 5835887ac8689c0c103e9eb25b345bff54ade6f0 Mon Sep 17 00:00:00 2001 From: Khaleel Al-Adhami Date: Wed, 24 Jul 2024 16:16:59 -0700 Subject: [PATCH 1/7] use different registry when in china, fixes #3700 --- reflex/constants/installer.py | 2 ++ reflex/utils/china.py | 17 +++++++++++++++++ reflex/utils/prerequisites.py | 10 ++++++++++ 3 files changed, 29 insertions(+) create mode 100644 reflex/utils/china.py diff --git a/reflex/constants/installer.py b/reflex/constants/installer.py index fbee4cd5ac1..4a7027ee825 100644 --- a/reflex/constants/installer.py +++ b/reflex/constants/installer.py @@ -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. diff --git a/reflex/utils/china.py b/reflex/utils/china.py new file mode 100644 index 00000000000..93442391016 --- /dev/null +++ b/reflex/utils/china.py @@ -0,0 +1,17 @@ +import requests + + +def _is_in_china(): + try: + response = requests.get(f"http://ip-api.com/json") + data = response.json() + + if data["status"] == "success": + return data["country"] == "China" + else: + print(f"Error: {data['message']}") + return None + + except requests.RequestException as e: + print(f"Request error: {e}") + return None diff --git a/reflex/utils/prerequisites.py b/reflex/utils/prerequisites.py index 40a2338d327..2efc62e1ca3 100644 --- a/reflex/utils/prerequisites.py +++ b/reflex/utils/prerequisites.py @@ -25,6 +25,7 @@ from typing import Callable, List, Optional import httpx +from reflex.utils.china import _is_in_china import typer from alembic.util.exc import CommandError from packaging import version @@ -577,6 +578,15 @@ def initialize_package_json(): code = _compile_package_json() output_path.write_text(code) + if _is_in_china() or True: + bun_config_path = get_web_dir() / constants.Bun.CONFIG_PATH + bun_config_path.write_text( + """ +[install] +registry = "https://r.cnpmjs.org" +""" + ) + def init_reflex_json(project_hash: int | None): """Write the hash of the Reflex project to a REFLEX_JSON. From ed66a3b49a66d51c66d8d1c351df584c383a287c Mon Sep 17 00:00:00 2001 From: Khaleel Al-Adhami Date: Wed, 24 Jul 2024 16:18:02 -0700 Subject: [PATCH 2/7] remove or True --- reflex/utils/prerequisites.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/reflex/utils/prerequisites.py b/reflex/utils/prerequisites.py index 2efc62e1ca3..327733965b0 100644 --- a/reflex/utils/prerequisites.py +++ b/reflex/utils/prerequisites.py @@ -578,7 +578,7 @@ def initialize_package_json(): code = _compile_package_json() output_path.write_text(code) - if _is_in_china() or True: + if _is_in_china(): bun_config_path = get_web_dir() / constants.Bun.CONFIG_PATH bun_config_path.write_text( """ From 3d93c4caf770756edba984713d884b69cbf71cdb Mon Sep 17 00:00:00 2001 From: Khaleel Al-Adhami Date: Wed, 24 Jul 2024 16:20:47 -0700 Subject: [PATCH 3/7] add docs --- reflex/utils/china.py | 12 +++++++++++- reflex/utils/prerequisites.py | 2 +- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/reflex/utils/china.py b/reflex/utils/china.py index 93442391016..9bafee36185 100644 --- a/reflex/utils/china.py +++ b/reflex/utils/china.py @@ -1,7 +1,17 @@ +"""This module contains utility functions for checking if the current IP address is in China.""" + +from typing import Union + import requests -def _is_in_china(): +def _is_in_china() -> Union[bool, None]: + """Check if the current IP address is in China. + + Returns: + bool: True if the IP address is in China, False otherwise. + None: If an error occurred. + """ try: response = requests.get(f"http://ip-api.com/json") data = response.json() diff --git a/reflex/utils/prerequisites.py b/reflex/utils/prerequisites.py index 327733965b0..0d7fcf1f9f0 100644 --- a/reflex/utils/prerequisites.py +++ b/reflex/utils/prerequisites.py @@ -25,7 +25,6 @@ from typing import Callable, List, Optional import httpx -from reflex.utils.china import _is_in_china import typer from alembic.util.exc import CommandError from packaging import version @@ -38,6 +37,7 @@ from reflex.compiler import templates from reflex.config import Config, get_config from reflex.utils import console, path_ops, processes +from reflex.utils.china import _is_in_china from reflex.utils.format import format_library_name CURRENTLY_INSTALLING_NODE = False From 9aedf5ffb88175847865608abf32a467f713b468 Mon Sep 17 00:00:00 2001 From: Khaleel Al-Adhami Date: Thu, 25 Jul 2024 09:55:02 -0700 Subject: [PATCH 4/7] use best of registries instead --- reflex/utils/china.py | 27 ----------------------- reflex/utils/prerequisites.py | 14 ++++++------ reflex/utils/registry.py | 40 +++++++++++++++++++++++++++++++++++ 3 files changed, 47 insertions(+), 34 deletions(-) delete mode 100644 reflex/utils/china.py create mode 100644 reflex/utils/registry.py diff --git a/reflex/utils/china.py b/reflex/utils/china.py deleted file mode 100644 index 9bafee36185..00000000000 --- a/reflex/utils/china.py +++ /dev/null @@ -1,27 +0,0 @@ -"""This module contains utility functions for checking if the current IP address is in China.""" - -from typing import Union - -import requests - - -def _is_in_china() -> Union[bool, None]: - """Check if the current IP address is in China. - - Returns: - bool: True if the IP address is in China, False otherwise. - None: If an error occurred. - """ - try: - response = requests.get(f"http://ip-api.com/json") - data = response.json() - - if data["status"] == "success": - return data["country"] == "China" - else: - print(f"Error: {data['message']}") - return None - - except requests.RequestException as e: - print(f"Request error: {e}") - return None diff --git a/reflex/utils/prerequisites.py b/reflex/utils/prerequisites.py index 0d7fcf1f9f0..2c9dcf20a7a 100644 --- a/reflex/utils/prerequisites.py +++ b/reflex/utils/prerequisites.py @@ -37,8 +37,8 @@ from reflex.compiler import templates from reflex.config import Config, get_config from reflex.utils import console, path_ops, processes -from reflex.utils.china import _is_in_china from reflex.utils.format import format_library_name +from reflex.utils.registry import _get_best_registry CURRENTLY_INSTALLING_NODE = False @@ -578,14 +578,14 @@ def initialize_package_json(): code = _compile_package_json() output_path.write_text(code) - if _is_in_china(): - bun_config_path = get_web_dir() / constants.Bun.CONFIG_PATH - bun_config_path.write_text( - """ + best_registry = _get_best_registry() + bun_config_path = get_web_dir() / constants.Bun.CONFIG_PATH + bun_config_path.write_text( + f""" [install] -registry = "https://r.cnpmjs.org" +registry = "{best_registry}" """ - ) + ) def init_reflex_json(project_hash: int | None): diff --git a/reflex/utils/registry.py b/reflex/utils/registry.py new file mode 100644 index 00000000000..a9d014a82bc --- /dev/null +++ b/reflex/utils/registry.py @@ -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) From 56544acfcfdd646eae789509cf0cd41cf9030dc4 Mon Sep 17 00:00:00 2001 From: Khaleel Al-Adhami Date: Thu, 25 Jul 2024 09:58:06 -0700 Subject: [PATCH 5/7] put it in try except --- reflex/utils/registry.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/reflex/utils/registry.py b/reflex/utils/registry.py index a9d014a82bc..aa4e205672f 100644 --- a/reflex/utils/registry.py +++ b/reflex/utils/registry.py @@ -1,3 +1,5 @@ +"""Utilities for working with registries.""" + import httpx @@ -10,7 +12,10 @@ def latency(registry: str) -> int: Returns: int: The latency of the registry in microseconds. """ - return httpx.get(registry).elapsed.microseconds + try: + return httpx.get(registry).elapsed.microseconds + except httpx.HTTPError: + return 10_000_000 def average_latency(registry, attempts: int = 3) -> int: From 0409ad51d3bbf0b7ae1e6b1de38e2851707c8c8d Mon Sep 17 00:00:00 2001 From: Khaleel Al-Adhami Date: Thu, 25 Jul 2024 09:59:00 -0700 Subject: [PATCH 6/7] add info message --- reflex/utils/registry.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/reflex/utils/registry.py b/reflex/utils/registry.py index aa4e205672f..e53de1a6b2c 100644 --- a/reflex/utils/registry.py +++ b/reflex/utils/registry.py @@ -2,6 +2,8 @@ import httpx +from reflex.utils import console + def latency(registry: str) -> int: """Get the latency of a registry. @@ -15,6 +17,7 @@ def latency(registry: str) -> int: try: return httpx.get(registry).elapsed.microseconds except httpx.HTTPError: + console.info(f"Failed to connect to {registry}.") return 10_000_000 From acc6f00c0774bbc4e913c9f3f9e97841b18e6c9b Mon Sep 17 00:00:00 2001 From: Khaleel Al-Adhami Date: Thu, 25 Jul 2024 10:22:30 -0700 Subject: [PATCH 7/7] dang it darglint --- reflex/utils/registry.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/reflex/utils/registry.py b/reflex/utils/registry.py index e53de1a6b2c..551292f2d44 100644 --- a/reflex/utils/registry.py +++ b/reflex/utils/registry.py @@ -26,7 +26,7 @@ def average_latency(registry, attempts: int = 3) -> int: Args: registry (str): The URL of the registry. - attempts (int, optional): The number of attempts to make. Defaults to 10. + attempts (int): The number of attempts to make. Defaults to 10. Returns: int: The average latency of the registry in microseconds.