Refine update command with real-time stderr progress streaming
Binaries / Build & Release Binaries (push) Has been cancelled

This commit is contained in:
gitea committed 2026-08-25 19:08:39 +08:00
1 parent 3c085456b0
commit 013a788077
1 file changed
+63 -10
+63 -10
View File
@@ -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}`);
}
}