superagent icon indicating copy to clipboard operation
superagent copied to clipboard

Support different agent for different protocols

Open Tom910 opened this issue 6 years ago • 0 comments

Problem

const customAgent = new https.Agent({ keepAlive: true })
function request(url, method) {
	return new Promise(resolve => {
	  request[method](url)
	      .agent(customAgent)
	      .end(function(){
	       ....
	       })
	}
}

request('https://github.com') // > work fine
request('http://github.com') // > dosen't work

The current api does not allow to transfer different HTTP agents for different protocols. And it is necessary to parse the URL in the request function. And expose different agents.

Possible Solution

got way - https://github.com/sindresorhus/got#agent

got('https://sindresorhus.com', {
	agent: {
		http: new HttpAgent(),
		https: new HttpsAgent()
	}
});

node-fetch way - https://github.com/bitinn/node-fetch#custom-agent

const customAgent = function(_parsedURL) {
    if (_parsedURL.protocol == 'http:') {
        return httpAgent;
    } else {
        return httpsAgent;
    }
}

Tom910 avatar Jun 18 '19 08:06 Tom910