diff --git a/lib/request.js b/lib/request.js index f6643a7a..81d9b67c 100644 --- a/lib/request.js +++ b/lib/request.js @@ -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?