import progress from "cli-progress"; import fs from "fs"; import fse from "fs-extra"; import got, { type Progress, type Request } from "got"; import path from "path"; import ps from "promise-streams"; import { type Stream, type Writable } from "stream"; import { x as tarX } from "tar"; import unzipper from "unzipper"; export interface DownloadOptions { progress?: boolean; strip?: number; executable?: boolean; } const DEFAULT_OPTIONS: DownloadOptions = { progress: true, strip: 1, executable: false }; type Extractor = (stream: Request, dest: string, options: DownloadOptions) => Stream; const ZIP_EXTRACTOR: Extractor = (stream: Request, dest: string, options: DownloadOptions): Stream => { return stream.pipe(unzipper.Parse()).on("entry", (entry: unzipper.Entry) => { const filePath = entry.path.split("/").slice(options.strip).join(path.sep); if (filePath.length > 0) { if (entry.type === "Directory") { fse.ensureDirSync(path.join(dest, path.normalize(filePath))); } else if (entry.type === "File") { const fullFilePath = path.join(dest, path.normalize(filePath)); const dirPath = path.dirname(fullFilePath); if (!fs.existsSync(dirPath)) { fse.ensureDirSync(dirPath); } entry.pipe( fs.createWriteStream(path.join(dest, path.normalize(filePath)), { // TODO: zip entry file mode? mode: options.executable ? 0o755 : undefined, }), ); } } else { entry.autodrain(); } }); }; const QZ_EXTRACTOR: Extractor = (stream: Request, dest: string, options: DownloadOptions): Stream => { // TODO: cast tar.Unpack to Writable return stream.pipe(tarX({ C: dest, strip: options.strip }) as unknown as Writable); }; const DEFAULT_EXTRACTOR: Extractor = (stream: Request, dest: string, options: DownloadOptions): Stream => { const filename = String(stream.requestUrl as URL) .split("/") .pop()!; return stream.pipe(fs.createWriteStream(path.join(dest, filename))); }; const EXTRACTOR_MAP: Record = { ".zip": ZIP_EXTRACTOR, ".gz": QZ_EXTRACTOR, "": DEFAULT_EXTRACTOR, }; class ProgressBar { private multiBar: progress.MultiBar = new progress.MultiBar( { stopOnComplete: true, }, progress.Presets.shades_classic, ); handle(request: Request): Request { const bar = this.multiBar.create(0, 0); return request .on("request", (request: Request) => { bar.start(1, 0); }) .on("downloadProgress", (progress: Progress) => { if (progress.total) { bar.setTotal(progress.total); } bar.update(progress.transferred); if (progress.total === progress.transferred) { bar.render(); bar.stop(); this.multiBar.remove(bar); } }); } } const progressBar = new ProgressBar(); export const download = async (url: string, dest: string, options?: DownloadOptions): Promise => { options = { ...DEFAULT_OPTIONS, ...options }; fse.ensureDirSync(dest); let stream: Request = got.stream(url, { decompress: false }); if (options.progress) { stream = progressBar.handle(stream); } const ext = path.extname(url); const extractor = EXTRACTOR_MAP[ext] || EXTRACTOR_MAP[""]; return ps.wait(extractor(stream, dest, options)); }; export default download;