refactor: network

This commit is contained in:
Cat /dev/Nulo 2023-12-18 12:13:09 -03:00
parent 8d401e6ca3
commit 697eb887cf

View file

@ -1,13 +1,12 @@
import { Dispatcher, request, Agent, setGlobalDispatcher } from "undici"; import { Dispatcher, request, Agent } from "undici";
import pLimit from "p-limit"; import pLimit from "p-limit";
import { userAgent } from "./config.js"; import { userAgent } from "./config.js";
setGlobalDispatcher( const dispatcher = new Agent({
new Agent({
pipelining: 0, pipelining: 0,
bodyTimeout: 15 * 60 * 1000, bodyTimeout: 15 * 60 * 1000,
}) maxRedirections: 20,
); });
export class StatusCodeError extends Error { export class StatusCodeError extends Error {
/** /**
@ -82,19 +81,27 @@ async function _customRequestWithLimits(url) {
} }
/** /**
* genera los headers para hacer un pedido dependiendo de la url
* @param {URL} url * @param {URL} url
*/ */
async function _customRequest(url) { function getHeaders(url) {
// sharepoint no le gusta compartir a bots lol // sharepoint no le gusta compartir a bots lol
const spoofUserAgent = url.host.endsWith("sharepoint.com"); const spoofUserAgent = url.host.endsWith("sharepoint.com");
const res = await request(url.toString(), { return {
maxRedirections: 20,
headers: {
"User-Agent": spoofUserAgent "User-Agent": spoofUserAgent
? "Mozilla/5.0 (X11; Linux x86_64; rv:120.0) Gecko/20100101 Firefox/120.0" ? "Mozilla/5.0 (X11; Linux x86_64; rv:120.0) Gecko/20100101 Firefox/120.0"
: userAgent, : userAgent,
}, };
}
/**
* @param {URL} url
*/
async function _customRequest(url) {
const res = await request(url.toString(), {
headers: getHeaders(url),
dispatcher,
}); });
if (res.statusCode >= 300 && res.statusCode <= 399) if (res.statusCode >= 300 && res.statusCode <= 399)
throw new TooManyRedirectsError(); throw new TooManyRedirectsError();