fix: 1/2 axios migration

This commit is contained in:
Artemy 2023-06-21 16:52:41 +03:00
parent f760263f7f
commit 6cce71b998

View file

@ -4,15 +4,21 @@ const request = require("request");
const checksum = require("checksum"); const checksum = require("checksum");
const Zip = require("adm-zip"); const Zip = require("adm-zip");
const child = require("child_process"); const child = require("child_process");
const axios = require("axios");
const { Agent } = require("https");
let counter = 0; let counter = 0;
class Handler { class Handler {
constructor(client) { constructor(client) {
this.client = client; this.client = client;
this.options = client.options; this.options = client.options;
this.baseRequest = request.defaults({
pool: { maxSockets: this.options.overrides.maxSockets || 2 }, this.baseRequest = axios.create({
timeout: this.options.timeout || 10000, timeout: this.options.timeout || 10000,
httpsAgent: new Agent({
maxSockets: 8,
}),
}); });
} }
@ -40,10 +46,14 @@ class Handler {
} }
downloadAsync(url, directory, name, retry, type) { downloadAsync(url, directory, name, retry, type) {
return new Promise((resolve) => { return new Promise(async (resolve) => {
fs.mkdirSync(directory, { recursive: true }); fs.mkdirSync(directory, { recursive: true });
const _request = this.baseRequest(url); const _request = (
await this.baseRequest.get(url, {
responseType: "stream",
})
).data;
let receivedBytes = 0; let receivedBytes = 0;
let totalBytes = 0; let totalBytes = 0;
@ -636,17 +646,19 @@ class Handler {
}/${name}`; }/${name}`;
// Checking if the file still exists on Forge's server, if not, replace it with the fallback. // Checking if the file still exists on Forge's server, if not, replace it with the fallback.
// Not checking for sucess, only if it 404s. // Not checking for sucess, only if it 404s.
this.baseRequest(downloadLink, (error, response, body) => { try {
if (error) { await this.baseRequest.get(downloadLink);
this.client.emit( } catch (error) {
"debug", if (error.response) {
`[PiMi]: Failed checking request for ${downloadLink}` if (error.response.status === 404)
);
} else {
if (response.statusCode === 404)
library.url = this.options.overrides.url.fallbackMaven; library.url = this.options.overrides.url.fallbackMaven;
else
this.client.emit(
"debug",
`[PiMi]: Failed checking request for ${downloadLink}`
);
} }
}); }
}) })
); );
} }