2023-12-18 15:13:09 +00:00
|
|
|
import { Dispatcher, request, Agent } from "undici";
|
2023-12-16 14:15:27 +00:00
|
|
|
import { userAgent } from "./config.js";
|
|
|
|
|
2023-12-18 15:13:09 +00:00
|
|
|
const dispatcher = new Agent({
|
2023-12-18 15:46:34 +00:00
|
|
|
connect: { timeout: 60 * 1000 },
|
2023-12-18 15:13:09 +00:00
|
|
|
bodyTimeout: 15 * 60 * 1000,
|
|
|
|
maxRedirections: 20,
|
|
|
|
});
|
2023-12-29 13:01:59 +00:00
|
|
|
const ignoreTlsDispatcher = new Agent({
|
|
|
|
connect: { timeout: 60 * 1000, rejectUnauthorized: false },
|
|
|
|
bodyTimeout: 15 * 60 * 1000,
|
|
|
|
maxRedirections: 20,
|
|
|
|
});
|
2023-12-16 14:27:51 +00:00
|
|
|
|
2023-12-16 14:15:27 +00:00
|
|
|
export class StatusCodeError extends Error {
|
|
|
|
/**
|
|
|
|
* @param {number} code
|
|
|
|
*/
|
|
|
|
constructor(code) {
|
|
|
|
super(`Status code: ${code}`);
|
|
|
|
this.code = code;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
export class TooManyRedirectsError extends Error {}
|
|
|
|
|
2023-12-18 16:43:17 +00:00
|
|
|
const REPORT_RETRIES = process.env.REPORT_RETRIES === "true" || false;
|
|
|
|
|
2023-12-16 14:15:27 +00:00
|
|
|
/**
|
|
|
|
* @argument {URL} url
|
|
|
|
* @argument {number} attempts
|
2023-12-16 14:27:51 +00:00
|
|
|
* @returns {Promise<Dispatcher.ResponseData>}
|
2023-12-16 14:15:27 +00:00
|
|
|
*/
|
2023-12-19 15:01:37 +00:00
|
|
|
export async function customRequestWithRetries(url, attempts = 0) {
|
2023-12-16 14:15:27 +00:00
|
|
|
try {
|
2023-12-19 15:01:37 +00:00
|
|
|
return await customRequest(url);
|
2023-12-16 14:15:27 +00:00
|
|
|
} catch (error) {
|
|
|
|
// algunos servidores usan 403 como coso para decir "calmate"
|
|
|
|
// intentar hasta 15 veces con 15 segundos de por medio
|
|
|
|
if (
|
|
|
|
error instanceof StatusCodeError &&
|
|
|
|
((error.code === 403 && url.host === "minsegar-my.sharepoint.com") ||
|
2023-12-29 13:01:59 +00:00
|
|
|
(error.code === 503 && url.host === "cdn.buenosaires.gob.ar") ||
|
|
|
|
(error.code === 502 && url.host === "datos.jus.gob.ar")) &&
|
2023-12-16 14:15:27 +00:00
|
|
|
attempts < 15
|
|
|
|
) {
|
2023-12-18 16:43:17 +00:00
|
|
|
if (REPORT_RETRIES)
|
|
|
|
console.debug(`reintentando(status)[${attempts}] ${url.toString()}`);
|
2023-12-18 17:12:25 +00:00
|
|
|
await wait(1000 * (attempts + 1) ** 2 + Math.random() * 10000);
|
2023-12-19 15:01:37 +00:00
|
|
|
return await customRequestWithRetries(url, attempts + 1);
|
2023-12-16 14:15:27 +00:00
|
|
|
}
|
|
|
|
// si no fue un error de http, reintentar hasta 3 veces con ~10 segundos de por medio
|
|
|
|
else if (
|
|
|
|
!(error instanceof StatusCodeError) &&
|
|
|
|
!(error instanceof TooManyRedirectsError) &&
|
|
|
|
attempts < 7
|
|
|
|
) {
|
2023-12-18 16:43:17 +00:00
|
|
|
if (REPORT_RETRIES)
|
|
|
|
console.debug(`reintentando[${attempts}] ${url.toString()}`);
|
2023-12-16 14:15:27 +00:00
|
|
|
await wait(5000 + Math.random() * 10000);
|
2023-12-19 15:01:37 +00:00
|
|
|
return await customRequestWithRetries(url, attempts + 1);
|
2023-12-16 14:15:27 +00:00
|
|
|
} else throw error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @argument {number} ms */
|
|
|
|
function wait(ms) {
|
|
|
|
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-12-18 15:13:09 +00:00
|
|
|
* genera los headers para hacer un pedido dependiendo de la url
|
2023-12-16 14:15:27 +00:00
|
|
|
* @param {URL} url
|
|
|
|
*/
|
2023-12-18 15:13:09 +00:00
|
|
|
function getHeaders(url) {
|
2023-12-16 14:15:27 +00:00
|
|
|
// sharepoint no le gusta compartir a bots lol
|
|
|
|
const spoofUserAgent = url.host.endsWith("sharepoint.com");
|
|
|
|
|
2023-12-18 15:13:09 +00:00
|
|
|
return {
|
|
|
|
"User-Agent": spoofUserAgent
|
|
|
|
? "Mozilla/5.0 (X11; Linux x86_64; rv:120.0) Gecko/20100101 Firefox/120.0"
|
|
|
|
: userAgent,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {URL} url
|
|
|
|
*/
|
2023-12-19 15:01:37 +00:00
|
|
|
export async function customRequest(url) {
|
2023-12-29 13:01:59 +00:00
|
|
|
let d = dispatcher;
|
|
|
|
if (url.hostname === "datos.agroindustria.gob.ar") {
|
|
|
|
d = ignoreTlsDispatcher;
|
|
|
|
}
|
2023-12-16 14:27:51 +00:00
|
|
|
const res = await request(url.toString(), {
|
2023-12-18 15:13:09 +00:00
|
|
|
headers: getHeaders(url),
|
|
|
|
dispatcher,
|
2023-12-16 14:15:27 +00:00
|
|
|
});
|
2023-12-16 14:27:51 +00:00
|
|
|
if (res.statusCode >= 300 && res.statusCode <= 399)
|
|
|
|
throw new TooManyRedirectsError();
|
|
|
|
if (res.statusCode < 200 || res.statusCode > 299)
|
|
|
|
throw new StatusCodeError(res.statusCode);
|
2023-12-16 14:15:27 +00:00
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|