-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
66 lines (52 loc) · 1.71 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
var http = require('http'),
https = require('https'),
util = require('util');
function agentFactory(BaseAgent) {
function Agent(options) {
BaseAgent.call(this, options);
this.name = options.name;
}
util.inherits(Agent, BaseAgent);
Agent.prototype.removeSocket = function() {
var res = Agent.super_.prototype.removeSocket.apply(this, arguments);
if (
Object.keys(this.requests).length === 0
&& Object.keys(this.sockets).length === 0
&& (this.freeSockets === undefined || Object.keys(this.freeSockets).length === 0)
) {
delete Agent.pool[this.name];
}
this.emit('socketRemoved');
return res;
};
Agent.pool = {};
Agent.factory = function(options) {
if ( ! this.pool[options.name]) {
this.pool[options.name] = new Agent(options);
}
return this.pool[options.name];
};
return Agent;
}
exports = module.exports = AdvancedAgent;
exports.httpAgent = agentFactory(http.Agent);
exports.httpsAgent = agentFactory(https.Agent);
/**
* @param {Object} options
* @param {String} [options.name]
* @param {String} options.protocol
* @param {Number} options.maxSocket
* @constructor
*/
function AdvancedAgent(options) {
var protocol = options.protocol;
if (protocol[protocol.length - 1] === ':') {
protocol = protocol.substring(0, protocol.length - 1);
}
if ( ! options.name || options.name === 'globalAgent') {
return protocol === 'https' ? https.globalAgent : http.globalAgent;
} else {
var agentCls = protocol === 'https' ? exports.httpsAgent : exports.httpAgent;
return agentCls.factory(options);
}
}