refactor(download): refactor download module
test / test (push) Failing after 5m35s

This commit is contained in:
2026-10-27 01:04:16 +03:00
parent 188fcc8e4c
commit cf3a707a47
7 changed files with 105 additions and 79 deletions
+91 -69
View File
@@ -1,84 +1,106 @@
import progress from "cli-progress";
import fs from "fs";
import fse from "fs-extra";
import got from "got";
import got, { type Progress, type Request } from "got";
import path from "path";
import ps from "promise-streams";
import type { Stream, Writable } from "stream";
import * as tar from "tar";
import { x as tarX } from "tar";
import unzip from "unzip-stream";
export class Downloader {
static bar: progress.MultiBar | null;
export interface DownloadOptions {
progress?: boolean;
strip?: number;
executable?: boolean;
}
static createProgressBar() {
if (Downloader.bar == null) {
Downloader.bar = new progress.MultiBar(
{
clearOnComplete: true,
},
progress.Presets.shades_grey,
);
}
return Downloader.bar.create(0, 0);
}
const DEFAULT_OPTIONS: DownloadOptions = { progress: true, strip: 1, executable: false };
static releaseProgressBar(bar: progress.SingleBar) {
bar.stop();
if (Downloader.bar != null) {
Downloader.bar.remove(bar);
if (!Downloader.bar.isActive) {
Downloader.bar.stop();
Downloader.bar = null;
type Extractor = (stream: Request, dest: string, options: DownloadOptions) => Stream;
const ZIP_EXTRACTOR: Extractor = (stream: Request, dest: string, options: DownloadOptions): Stream => {
return stream.pipe(unzip.Parse()).on("entry", (entry: any) => {
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,
}),
);
}
}
}
static download(url: string, dest: string, strip = 1, executable = false) {
fse.ensureDirSync(dest);
const bar = Downloader.createProgressBar();
let stream: Stream = got.stream(url);
stream = stream
.on("request", (r: any) => {
if (bar) bar.start(1, 0);
})
.on("downloadProgress", (p: any) => {
if (bar) {
bar.setTotal(p.total);
bar.update(p.transferred);
if (p.total === p.transferred) {
Downloader.releaseProgressBar(bar);
}
}
});
if (url.endsWith(".zip")) {
stream = stream.pipe(unzip.Parse()).on("entry", (entry: any) => {
const filePath = entry.path.split("/").slice(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: executable ? 0o755 : undefined,
}),
);
}
} else {
entry.autodrain();
}
});
} else if (url.endsWith("tar.gz")) {
stream = stream.pipe(tar.x({ C: dest, strip }) as unknown as Writable);
} else {
stream = stream.pipe(fs.createWriteStream(path.join(dest, url.split("/").pop()!)));
entry.autodrain();
}
return ps.wait(stream);
});
};
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<string, Extractor> = {
".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<void> => {
options = Object.assign({}, DEFAULT_OPTIONS, options);
fse.ensureDirSync(dest);
let stream: Request = got.stream(url);
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;
+3 -1
View File
@@ -52,4 +52,6 @@ export class OutputLogger extends Logger {
export const OUTPUT_LOGGER = new OutputLogger();
export default (line: string) => OUTPUT_LOGGER.log(line);
export const output = (line: string) => OUTPUT_LOGGER.log(line);
export default output;