diff --git a/lib/request.js b/lib/request.js index 4ff979b1..386eee6b 100644 --- a/lib/request.js +++ b/lib/request.js @@ -7,15 +7,16 @@ const proxyAddress = typeof process !== 'undefined' && (process.env.HTTPS_PROXY let getAgent = () => undefined; -if (proxyAddress) { - import('https-proxy-agent').then(a => { +const setupProxy = async () => { + if (proxyAddress && !getAgent()) { + const a = await import('https-proxy-agent'); 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)) @@ -74,6 +75,7 @@ const checkIfResponseIsOk = (_) => { const request = async (ctx, userAgent, reqData) => { const {profile, opt} = ctx; + await setupProxy(); const endpoint = reqData.endpoint; delete reqData.endpoint;