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
|
||||
|
||||
@@ -22,6 +22,7 @@ from deploy_tool.installer import (
|
||||
MYSQL_INSTALLER_SHA256,
|
||||
MYSQL_INSTALLER_URL,
|
||||
NODE_DIST_BASE,
|
||||
NODE_DIST_MIRROR_BASE,
|
||||
build_mysql_installer_command,
|
||||
build_mysql_server_install_command,
|
||||
build_mysql_service_start_script,
|
||||
@@ -290,6 +291,42 @@ class InstallerTests(unittest.TestCase):
|
||||
self.assertEqual(insecure_fallback.call_count, 3)
|
||||
self.assertTrue(any("跳过 HTTPS 证书校验" in message for message in messages))
|
||||
|
||||
def test_node_download_switches_to_mirror_when_official_connection_is_closed(self):
|
||||
version = "16.20.2"
|
||||
directory = f"node-v{version}-win-x64"
|
||||
archive_name = f"{directory}.zip"
|
||||
archive_buffer = io.BytesIO()
|
||||
with zipfile.ZipFile(archive_buffer, "w") as archive:
|
||||
archive.writestr(f"{directory}/node.exe", "node")
|
||||
archive.writestr(f"{directory}/npm.cmd", "npm")
|
||||
archive_bytes = archive_buffer.getvalue()
|
||||
checksum = hashlib.sha256(archive_bytes).hexdigest()
|
||||
index_bytes = json.dumps(
|
||||
[{"version": f"v{version}", "lts": "Gallium", "files": ["win-x64-zip"]}]
|
||||
).encode("utf-8")
|
||||
requested_urls = []
|
||||
|
||||
def opener(url, timeout):
|
||||
requested_urls.append(url)
|
||||
if url.startswith(NODE_DIST_BASE):
|
||||
raise ConnectionResetError("remote end closed connection")
|
||||
self.assertTrue(url.startswith(NODE_DIST_MIRROR_BASE))
|
||||
if url.endswith("index.json"):
|
||||
return io.BytesIO(index_bytes)
|
||||
if url.endswith("SHASUMS256.txt"):
|
||||
return io.BytesIO(f"{checksum} {archive_name}\n".encode("ascii"))
|
||||
return io.BytesIO(archive_bytes)
|
||||
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
messages = []
|
||||
node, npm, installed_version = install_node(("16",), messages.append, Path(tmp), opener)
|
||||
|
||||
self.assertEqual(installed_version, version)
|
||||
self.assertTrue(node.exists())
|
||||
self.assertTrue(npm.exists())
|
||||
self.assertTrue(any(url.startswith(NODE_DIST_MIRROR_BASE) for url in requested_urls))
|
||||
self.assertTrue(any("切换国内镜像" in message for message in messages))
|
||||
|
||||
def test_builds_unattended_mysql_msi_install_command(self):
|
||||
command = build_mysql_installer_command(Path("mysql-installer.msi"), Path("mysql-install.log"))
|
||||
|
||||
|
||||
Reference in New Issue
Block a user