animepaste
Version:
Paste your favourite anime online
96 lines (89 loc) • 2.79 kB
JavaScript
;
const fs = require('fs-extra');
const path = require('node:path');
const stream = require('stream');
const axios = require('axios');
const onDeath = require('death');
const createDebug = require('debug');
const index = require('./animepaste.04f55d05.cjs');
function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e.default : e; }
function _interopNamespaceCompat(e) {
if (e && typeof e === 'object' && 'default' in e) return e;
const n = Object.create(null);
if (e) {
for (const k in e) {
n[k] = e[k];
}
}
n.default = e;
return n;
}
const fs__default = /*#__PURE__*/_interopDefaultCompat(fs);
const path__namespace = /*#__PURE__*/_interopNamespaceCompat(path);
const axios__default = /*#__PURE__*/_interopDefaultCompat(axios);
const onDeath__default = /*#__PURE__*/_interopDefaultCompat(onDeath);
const createDebug__default = /*#__PURE__*/_interopDefaultCompat(createDebug);
const debug = createDebug__default("anime:download");
async function download(...payloads) {
const formatSize = (size) => (size / 1024 / 1024).toFixed(1) + " MB";
const multibar = index.createProgressBar({
suffix(value, total, payload) {
const progress = `${formatSize(value)} / ${formatSize(total)}`;
const speed = payload.speed ? " | Speed: " + formatSize(payload.speed) + "/s" : "";
return progress + speed;
}
});
const down = async (payload) => {
if (fs__default.existsSync(payload.filepath)) {
return;
}
const fetch = async () => {
for (let i = 0; i < 5; i++) {
try {
return await axios__default.get(payload.url, {
responseType: "stream"
});
} catch (error) {
debug(error);
}
}
};
const resp = await fetch();
if (!resp)
return;
const { data, headers } = resp;
const bar = multibar.create(
path__namespace.basename(payload.filepath),
+headers["content-length"]
);
const writeStream = fs__default.createWriteStream(payload.filepath);
const cancel = onDeath__default(() => {
fs__default.removeSync(payload.filepath);
});
return new Promise((res, rej) => {
stream.pipeline(
data,
new stream.Transform({
transform(chunk, _encoding, callback) {
bar.increment(chunk.length);
this.push(chunk);
callback();
}
}),
writeStream,
(err) => {
if (err) {
fs__default.removeSync(payload.filepath);
rej(err);
} else {
cancel();
res();
}
}
);
});
};
await Promise.all(payloads.map((p) => down(p)));
multibar.finish();
}
exports.download = download;