Feature: allow disable ipv6 in proxy, refactor cacheFetch to use proxy (#5011)

This commit is contained in:
shamoon 2025-03-16 20:09:34 -07:00 committed by GitHub
parent 934ad3a6f1
commit b4dc53c7c0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
10 changed files with 61 additions and 65 deletions

View file

@ -3,6 +3,7 @@
import { createUnzip, constants as zlibConstants } from "node:zlib";
import { http, https } from "follow-redirects";
import cache from "memory-cache";
import { addCookieToJar, setCookieHeader } from "./cookie-jar";
import { sanitizeErrorURL } from "./api-helpers";
@ -81,20 +82,46 @@ export function httpRequest(url, params) {
return handleRequest(http, url, params);
}
export async function cachedRequest(url, duration = 5, ua = "homepage") {
const cached = cache.get(url);
if (cached) {
return cached;
}
const options = {
headers: {
"User-Agent": ua,
Accept: "application/json",
},
};
let [, , data] = await httpProxy(url, options);
if (Buffer.isBuffer(data)) {
try {
data = JSON.parse(Buffer.from(data).toString());
} catch (e) {
logger.debug("Error parsing cachedRequest data for %s: %s %s", url, Buffer.from(data).toString(), e);
data = Buffer.from(data).toString();
}
}
cache.put(url, data, duration * 1000 * 60);
return data;
}
export async function httpProxy(url, params = {}) {
const constructedUrl = new URL(url);
const disableIpv6 = process.env.HOMEPAGE_PROXY_DISABLE_IPV6 === "true";
const agentOptions = disableIpv6 ? { family: 4, autoSelectFamily: false } : {};
let request = null;
if (constructedUrl.protocol === "https:") {
request = httpsRequest(constructedUrl, {
agent: new https.Agent({
rejectUnauthorized: false,
}),
agent: new https.Agent({ ...agentOptions, rejectUnauthorized: false }),
...params,
});
} else {
request = httpRequest(constructedUrl, {
agent: new http.Agent(),
agent: new http.Agent(agentOptions),
...params,
});
}