add back HTTP_PROXY support

This commit is contained in:
Traines 2025-02-25 12:52:50 +00:00
parent 9fe4972d2b
commit 16829f839c

View file

@ -3,6 +3,20 @@ import {Request, fetch} from 'cross-fetch';
import {parse as parseContentType} from 'content-type';
import {HafasError} from './errors.js';
const proxyAddress = typeof process !== 'undefined' && (process.env.HTTPS_PROXY || process.env.HTTP_PROXY) || null;
let getAgent = () => undefined;
if (proxyAddress) {
import('https-proxy-agent').then(a => {
const agent = new a.default.HttpsProxyAgent(proxyAddress, {
keepAlive: true,
keepAliveMsecs: 10 * 1000, // 10s
});
getAgent = () => agent;
});
}
const randomBytesHexString = length => [...Array(length)].map(() => Math.floor(Math.random() * 16)
.toString(16))
.join('');
@ -66,6 +80,7 @@ const request = async (ctx, userAgent, reqData) => {
const rawReqBody = profile.transformReqBody(ctx, reqData.body);
const reqOptions = profile.transformReq(ctx, {
agent: getAgent(),
keepalive: true,
method: reqData.method,
// todo: CORS? referrer policy?