mirror of
https://github.com/catdevnull/transicion-desordenada-diablo
synced 2024-11-29 20:46:19 +00:00
usar fetch
experimento
This commit is contained in:
parent
2f55749bcb
commit
31b58d373c
3 changed files with 9 additions and 17 deletions
|
@ -12,7 +12,7 @@ const zCkanPackageList = z.object({
|
||||||
*/
|
*/
|
||||||
async function getJson(url) {
|
async function getJson(url) {
|
||||||
const res = await customRequestWithLimitsAndRetries(new URL(url));
|
const res = await customRequestWithLimitsAndRetries(new URL(url));
|
||||||
const json = await res.body.json();
|
const json = await res.json();
|
||||||
return json;
|
return json;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -41,7 +41,7 @@ async function downloadFromData(target) {
|
||||||
const jsonRes = await customRequestWithLimitsAndRetries(
|
const jsonRes = await customRequestWithLimitsAndRetries(
|
||||||
new URL(target.url)
|
new URL(target.url)
|
||||||
);
|
);
|
||||||
json = await jsonRes.body.json();
|
json = await jsonRes.json();
|
||||||
}
|
}
|
||||||
|
|
||||||
const parsed = zData.parse(json);
|
const parsed = zData.parse(json);
|
||||||
|
|
|
@ -1,14 +1,6 @@
|
||||||
import { Dispatcher, request, Agent, setGlobalDispatcher } from "undici";
|
|
||||||
import pLimit from "p-limit";
|
import pLimit from "p-limit";
|
||||||
import { userAgent } from "./config.js";
|
import { userAgent } from "./config.js";
|
||||||
|
|
||||||
setGlobalDispatcher(
|
|
||||||
new Agent({
|
|
||||||
pipelining: 0,
|
|
||||||
bodyTimeout: 15 * 60 * 1000,
|
|
||||||
})
|
|
||||||
);
|
|
||||||
|
|
||||||
export class StatusCodeError extends Error {
|
export class StatusCodeError extends Error {
|
||||||
/**
|
/**
|
||||||
* @param {number} code
|
* @param {number} code
|
||||||
|
@ -30,7 +22,7 @@ const nConnections = process.env.N_THREADS
|
||||||
/**
|
/**
|
||||||
* @argument {URL} url
|
* @argument {URL} url
|
||||||
* @argument {number} attempts
|
* @argument {number} attempts
|
||||||
* @returns {Promise<Dispatcher.ResponseData>}
|
* @returns {Promise<import("undici").BodyMixin>}
|
||||||
*/
|
*/
|
||||||
export async function customRequestWithLimitsAndRetries(url, attempts = 0) {
|
export async function customRequestWithLimitsAndRetries(url, attempts = 0) {
|
||||||
try {
|
try {
|
||||||
|
@ -83,23 +75,23 @@ async function _customRequestWithLimits(url) {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {URL} url
|
* @param {URL} url
|
||||||
|
* @returns {Promise<import("undici").BodyMixin>}
|
||||||
*/
|
*/
|
||||||
async function _customRequest(url) {
|
async function _customRequest(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(), {
|
const res = await fetch(url.toString(), {
|
||||||
maxRedirections: 20,
|
// maxRedirections: 20,
|
||||||
headers: {
|
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,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
if (res.statusCode >= 300 && res.statusCode <= 399)
|
if (res.status >= 300 && res.status <= 399) throw new TooManyRedirectsError();
|
||||||
throw new TooManyRedirectsError();
|
if (res.status < 200 || res.status > 299)
|
||||||
if (res.statusCode < 200 || res.statusCode > 299)
|
throw new StatusCodeError(res.status);
|
||||||
throw new StatusCodeError(res.statusCode);
|
|
||||||
|
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue