@electron/get
Version:
Utility for downloading artifacts from different versions of Electron
62 lines • 2.35 kB
JavaScript
import got, { HTTPError } from 'got';
import fs from 'graceful-fs';
import path from 'node:path';
import ProgressBar from 'progress';
import { pipeline } from 'node:stream/promises';
const PROGRESS_BAR_DELAY_IN_SECONDS = 30;
/**
* Default {@link Downloader} implemented with {@link https://npmjs.com/package/got | `got`}.
* @category Downloader
*/
export class GotDownloader {
async download(url, targetFilePath, options) {
if (!options) {
options = {};
}
const { quiet, getProgressCallback, ...gotOptions } = options;
let downloadCompleted = false;
let bar;
let progressPercent;
let timeout = undefined;
await fs.promises.mkdir(path.dirname(targetFilePath), { recursive: true });
const writeStream = fs.createWriteStream(targetFilePath);
if (!quiet || !process.env.ELECTRON_GET_NO_PROGRESS) {
const start = new Date();
timeout = setTimeout(() => {
if (!downloadCompleted) {
bar = new ProgressBar(`Downloading ${path.basename(url)}: [:bar] :percent ETA: :eta seconds `, {
curr: progressPercent,
total: 100,
});
// https://github.com/visionmedia/node-progress/issues/159
// eslint-disable-next-line @typescript-eslint/no-explicit-any
bar.start = start;
}
}, PROGRESS_BAR_DELAY_IN_SECONDS * 1000);
}
const downloadStream = got.stream(url, gotOptions);
downloadStream.on('downloadProgress', async (progress) => {
progressPercent = progress.percent;
if (bar) {
bar.update(progress.percent);
}
if (getProgressCallback) {
await getProgressCallback(progress);
}
});
try {
await pipeline(downloadStream, writeStream);
}
catch (error) {
if (error instanceof HTTPError && error.response.statusCode === 404) {
error.message += ` for ${error.response.url}`;
}
throw error;
}
downloadCompleted = true;
if (timeout) {
clearTimeout(timeout);
}
}
}
//# sourceMappingURL=GotDownloader.js.map