Refine Sealed Sender behaviors

This commit is contained in:
Scott Nonnenberg 2018-11-07 11:20:43 -08:00
parent 8391f6ec4e
commit 2d48daa7b6
8 changed files with 59 additions and 32 deletions

View file

@ -1,6 +1,7 @@
const WebSocket = require('websocket').w3cwebsocket;
const fetch = require('node-fetch');
const ProxyAgent = require('proxy-agent');
const { Agent } = require('https');
const is = require('@sindresorhus/is');
@ -159,6 +160,11 @@ function _createSocket(url, { certificateAuthority, proxyUrl }) {
return new WebSocket(url, null, null, null, requestOptions);
}
const agents = {
unauth: null,
auth: null,
};
function _promiseAjax(providedUrl, options) {
return new Promise((resolve, reject) => {
const url = providedUrl || `${options.host}/${options.path}`;
@ -169,10 +175,16 @@ function _promiseAjax(providedUrl, options) {
typeof options.timeout !== 'undefined' ? options.timeout : 10000;
const { proxyUrl } = options;
let agent;
if (proxyUrl) {
agent = new ProxyAgent(proxyUrl);
const agentType = options.unathenticated ? 'unauth' : 'auth';
if (!agents[agentType]) {
if (proxyUrl) {
agents[agentType] = new ProxyAgent(proxyUrl);
} else {
agents[agentType] = new Agent();
}
}
const agent = agents[agentType];
const fetchOptions = {
method: options.type,