From 013a78807772d31ad37c3d4c69bc3761c8a867da Mon Sep 17 00:00:00 2001 From: yunyingk Date: Tue, 25 Aug 2026 19:08:39 +0800 Subject: [PATCH] Refine update command with real-time stderr progress streaming --- src/cli/commands/update.ts | 73 ++++++++++++++++++++++++++++++++------ 1 file changed, 63 insertions(+), 10 deletions(-) diff --git a/src/cli/commands/update.ts b/src/cli/commands/update.ts index 5af7daf..a212d3f 100644 --- a/src/cli/commands/update.ts +++ b/src/cli/commands/update.ts @@ -459,6 +459,15 @@ function matchAssetForPlatform( return archive || matched[0]; } +function logProgress(msg: string) { + try { + process.stderr.write(`${msg}\n`); + } catch { + // ignore + } + console.error(msg); +} + async function downloadFile(url: string, destPath: string, tagName?: string, assetName?: string) { const candidates: string[] = []; @@ -466,7 +475,6 @@ async function downloadFile(url: string, destPath: string, tagName?: string, ass if (tagName && assetName) { const tag = tagName.startsWith("v") ? tagName : `v${tagName}`; candidates.push(`https://git.ying-qing.cn/gitea/seal-home/releases/download/${tag}/${assetName}`); - candidates.push(`https://git.ying-qing.cn/gitea/seal-home/releases/download/latest/${assetName}`); } // 2. Direct url if it's not already Gitea @@ -488,23 +496,68 @@ async function downloadFile(url: string, destPath: string, tagName?: string, ass const uniqueCandidates = Array.from(new Set(candidates)); let lastError: Error | undefined; - for (const candidate of uniqueCandidates) { + for (let i = 0; i < uniqueCandidates.length; i++) { + const candidate = uniqueCandidates[i]!; try { - console.error(`==> Trying download mirror: ${candidate}`); - const res = await fetch(candidate, { signal: AbortSignal.timeout(30000) }); + logProgress(`==> [${i + 1}/${uniqueCandidates.length}] Downloading via: ${candidate}`); + const res = await fetch(candidate, { signal: AbortSignal.timeout(10000) }); if (!res.ok) { throw new Error(`HTTP ${res.status} ${res.statusText}`); } - const buffer = Buffer.from(await res.arrayBuffer()); - if (buffer.length < 1000) { - throw new Error(`Downloaded content is suspiciously small (${buffer.length} bytes)`); + + const totalBytes = Number(res.headers.get("content-length")) || 0; + const reader = res.body?.getReader(); + if (!reader) { + const buffer = Buffer.from(await res.arrayBuffer()); + if (buffer.length < 1000) { + throw new Error(`Downloaded content is suspiciously small (${buffer.length} bytes)`); + } + writeFileSync(destPath, buffer); + logProgress(`✓ Download succeeded (${(buffer.length / 1024 / 1024).toFixed(1)} MB)`); + return; } - writeFileSync(destPath, buffer); - console.error(`✓ Download succeeded from: ${candidate} (${buffer.length} bytes)`); + + const chunks: Uint8Array[] = []; + let receivedBytes = 0; + let lastReport = 0; + + while (true) { + const { done, value } = await reader.read(); + if (done) break; + if (value) { + chunks.push(value); + receivedBytes += value.length; + const now = Date.now(); + if (now - lastReport > 200) { + lastReport = now; + const percent = totalBytes ? Math.round((receivedBytes / totalBytes) * 100) : 0; + const receivedMb = (receivedBytes / (1024 * 1024)).toFixed(1); + const totalMb = totalBytes ? (totalBytes / (1024 * 1024)).toFixed(1) : "?"; + try { + process.stderr.write(`\r ↳ Progress: ${percent}% (${receivedMb} / ${totalMb} MB)...`); + } catch { + // ignore + } + } + } + } + + try { + process.stderr.write("\n"); + } catch { + // ignore + } + + const completeBuffer = Buffer.concat(chunks); + if (completeBuffer.length < 1000) { + throw new Error(`Downloaded content is suspiciously small (${completeBuffer.length} bytes)`); + } + writeFileSync(destPath, completeBuffer); + logProgress(`✓ Download completed successfully (${(completeBuffer.length / 1024 / 1024).toFixed(1)} MB)`); return; } catch (err) { lastError = err instanceof Error ? err : new Error(String(err)); - console.error(` ↳ Mirror failed: ${lastError.message}`); + logProgress(` ↳ Mirror failed: ${lastError.message}`); } }