A few small bugfixes

This commit is contained in:
Scott Nonnenberg 2018-11-08 17:23:07 -08:00
parent 59d048ca06
commit c5f9fae444
2 changed files with 36 additions and 23 deletions

View file

@ -160,6 +160,7 @@ function _createSocket(url, { certificateAuthority, proxyUrl }) {
return new WebSocket(url, null, null, null, requestOptions);
}
const FIVE_MINUTES = 1000 * 60 * 5;
const agents = {
unauth: null,
auth: null,
@ -175,16 +176,21 @@ function _promiseAjax(providedUrl, options) {
typeof options.timeout !== 'undefined' ? options.timeout : 10000;
const { proxyUrl } = options;
const agentType = options.unathenticated ? 'unauth' : 'auth';
const agentType = options.unauthenticated ? 'unauth' : 'auth';
if (!agents[agentType]) {
if (proxyUrl) {
agents[agentType] = new ProxyAgent(proxyUrl);
} else {
agents[agentType] = new Agent();
const { timestamp } = agents[agentType] || {};
if (!timestamp || timestamp + FIVE_MINUTES < Date.now()) {
if (timestamp) {
log.info(`Cycling agent for type ${agentType}`);
}
agents[agentType] = {
agent: proxyUrl
? new ProxyAgent(proxyUrl)
: new Agent({ keepAlive: true }),
timestamp: Date.now(),
};
}
const agent = agents[agentType];
const { agent } = agents[agentType];
const fetchOptions = {
method: options.type,
@ -436,7 +442,7 @@ function initialize({ url, cdnUrl, certificateAuthority, proxyUrl }) {
message =
'The server rejected our query, please file a bug report.';
}
e.message = message;
e.message = `${message} (original: ${e.message})`;
throw e;
});
}