Fall back to Node download mirror
This commit is contained in:
@@ -50,6 +50,7 @@ MYSQL_INSTALLER_URL = (
|
||||
)
|
||||
MYSQL_INSTALLER_SHA256 = "F60502EB1EE10A9C1D0A26706A627D224DFAE466D82EF3EB9D76A8A718DB9D80"
|
||||
NODE_DIST_BASE = "https://nodejs.org/dist"
|
||||
NODE_DIST_MIRROR_BASE = "https://npmmirror.com/mirrors/node"
|
||||
|
||||
ANSI_PATTERN = re.compile(r"\x1b\[[0-?]*[ -/]*[@-~]")
|
||||
PERCENT_PATTERN = re.compile(r"(\d{1,3})\s*%")
|
||||
@@ -912,6 +913,43 @@ def _read_url_bytes(
|
||||
on_output: Callable[[str], None],
|
||||
*,
|
||||
allow_windows_fallback: bool,
|
||||
) -> bytes:
|
||||
try:
|
||||
return _read_url_bytes_from_source(
|
||||
url,
|
||||
opener,
|
||||
timeout,
|
||||
on_output,
|
||||
allow_windows_fallback=allow_windows_fallback,
|
||||
)
|
||||
except Exception as official_error:
|
||||
mirror_url = _node_mirror_url(url)
|
||||
if mirror_url is None:
|
||||
raise
|
||||
on_output("Node.js 官方下载连接失败,正在切换国内镜像重试...")
|
||||
try:
|
||||
return _read_url_bytes_from_source(
|
||||
mirror_url,
|
||||
opener,
|
||||
timeout,
|
||||
on_output,
|
||||
allow_windows_fallback=allow_windows_fallback,
|
||||
)
|
||||
except Exception as mirror_error:
|
||||
raise RuntimeError(
|
||||
"Node.js 官方地址和国内镜像均下载失败。"
|
||||
f"\n官方地址: {official_error}"
|
||||
f"\n国内镜像: {mirror_error}"
|
||||
) from mirror_error
|
||||
|
||||
|
||||
def _read_url_bytes_from_source(
|
||||
url: str,
|
||||
opener: Callable[..., BinaryIO],
|
||||
timeout: int,
|
||||
on_output: Callable[[str], None],
|
||||
*,
|
||||
allow_windows_fallback: bool,
|
||||
) -> bytes:
|
||||
try:
|
||||
with opener(url, timeout=timeout) as response:
|
||||
@@ -940,6 +978,44 @@ def _download_to_file_with_windows_fallback(
|
||||
on_output: Callable[[str], None],
|
||||
*,
|
||||
allow_windows_fallback: bool,
|
||||
) -> None:
|
||||
try:
|
||||
_download_to_file_from_source(
|
||||
url,
|
||||
destination,
|
||||
opener,
|
||||
on_output,
|
||||
allow_windows_fallback=allow_windows_fallback,
|
||||
)
|
||||
except Exception as official_error:
|
||||
mirror_url = _node_mirror_url(url)
|
||||
if mirror_url is None:
|
||||
raise
|
||||
destination.unlink(missing_ok=True)
|
||||
on_output("Node.js 官方下载连接失败,正在切换国内镜像重试...")
|
||||
try:
|
||||
_download_to_file_from_source(
|
||||
mirror_url,
|
||||
destination,
|
||||
opener,
|
||||
on_output,
|
||||
allow_windows_fallback=allow_windows_fallback,
|
||||
)
|
||||
except Exception as mirror_error:
|
||||
raise RuntimeError(
|
||||
"Node.js 官方地址和国内镜像均下载失败。"
|
||||
f"\n官方地址: {official_error}"
|
||||
f"\n国内镜像: {mirror_error}"
|
||||
) from mirror_error
|
||||
|
||||
|
||||
def _download_to_file_from_source(
|
||||
url: str,
|
||||
destination: Path,
|
||||
opener: Callable[..., BinaryIO],
|
||||
on_output: Callable[[str], None],
|
||||
*,
|
||||
allow_windows_fallback: bool,
|
||||
) -> None:
|
||||
try:
|
||||
_download_to_file(url, destination, opener, on_output)
|
||||
@@ -963,6 +1039,14 @@ def _download_to_file_with_windows_fallback(
|
||||
)
|
||||
|
||||
|
||||
def _node_mirror_url(url: str) -> str | None:
|
||||
if url == NODE_DIST_BASE:
|
||||
return NODE_DIST_MIRROR_BASE
|
||||
if url.startswith(f"{NODE_DIST_BASE}/"):
|
||||
return f"{NODE_DIST_MIRROR_BASE}{url[len(NODE_DIST_BASE):]}"
|
||||
return None
|
||||
|
||||
|
||||
def _open_url_without_certificate_validation(url: str, timeout: int):
|
||||
context = ssl.create_default_context()
|
||||
context.check_hostname = False
|
||||
|
||||
Reference in New Issue
Block a user