in src/core/func-core-tools.ts [157:233]
async function downloadAndUnzipPackage(release: CoreToolsRelease, dest: string) {
const progressBar = new cliProgress.Bar(
{
format: "{bar} {percentage}% | ETA: {eta}s",
},
cliProgress.Presets.shades_classic,
);
try {
const response = await fetch(release.url);
const totalSize = Number(response.headers.get("content-length"));
let downloadedSize = 0;
let since = Date.now();
progressBar.start(totalSize, downloadedSize);
const bodyStream1 = response?.body?.pipe(new PassThrough());
const bodyStream2 = response?.body?.pipe(new PassThrough());
bodyStream2?.on("data", (chunk) => {
downloadedSize += chunk.length;
const now = Date.now();
if (now - since > 100) {
progressBar.update(downloadedSize);
since = now;
}
});
const downloadFilename = release.url.substring(release.url.lastIndexOf("/") + 1);
const outputFile = path.join(dest, downloadFilename);
const writableStream = fs.createWriteStream(outputFile);
bodyStream2?.pipe(writableStream);
const unzipPromise = new Promise((resolve, reject) => {
const timeout = 30;
let timer = setTimeout(() => {
if (downloadedSize === 0) {
writableStream.close();
reject(new Error(`Download failed after ${timeout} seconds. Please check your network or retry.`));
}
}, timeout * 1000);
writableStream.on("finish", () => {
clearTimeout(timer);
resolve(outputFile);
});
writableStream.on("error", (err) => {
reject(err);
});
})
.then(() => {
const zip = new AdmZip(outputFile);
zip.extractAllTo(dest, true);
fs.unlinkSync(outputFile);
})
.catch((err) => {
throw new Error(`Extract failed: ${err.message}`);
});
const hash = await new Promise((resolve) => {
const hash = crypto.createHash("sha256");
hash.setEncoding("hex");
bodyStream1?.on("end", () => {
hash.end();
resolve(hash.read());
});
bodyStream1?.pipe(hash);
});
await unzipPromise;
if (hash !== release.sha2) {
throw new Error(`Downloaded Core Tools SHA2 mismatch: expected ${hash}, got ${release.sha2}`);
}
} finally {
progressBar.stop();
}
}