react / wstein / node_modules / jest-cli / node_modules / jsdom / node_modules / request / node_modules / forever-agent / index.js
81145 viewsmodule.exports = ForeverAgent1ForeverAgent.SSL = ForeverAgentSSL23var util = require('util')4, Agent = require('http').Agent5, net = require('net')6, tls = require('tls')7, AgentSSL = require('https').Agent89function getConnectionName(host, port) {10var name = ''11if (typeof host === 'string') {12name = host + ':' + port13} else {14// For node.js v012.0 and iojs-v1.5.1, host is an object. And any existing localAddress is part of the connection name.15name = host.host + ':' + host.port + ':' + (host.localAddress ? (host.localAddress + ':') : ':')16}17return name18}1920function ForeverAgent(options) {21var self = this22self.options = options || {}23self.requests = {}24self.sockets = {}25self.freeSockets = {}26self.maxSockets = self.options.maxSockets || Agent.defaultMaxSockets27self.minSockets = self.options.minSockets || ForeverAgent.defaultMinSockets28self.on('free', function(socket, host, port) {29var name = getConnectionName(host, port)3031if (self.requests[name] && self.requests[name].length) {32self.requests[name].shift().onSocket(socket)33} else if (self.sockets[name].length < self.minSockets) {34if (!self.freeSockets[name]) self.freeSockets[name] = []35self.freeSockets[name].push(socket)3637// if an error happens while we don't use the socket anyway, meh, throw the socket away38var onIdleError = function() {39socket.destroy()40}41socket._onIdleError = onIdleError42socket.on('error', onIdleError)43} else {44// If there are no pending requests just destroy the45// socket and it will get removed from the pool. This46// gets us out of timeout issues and allows us to47// default to Connection:keep-alive.48socket.destroy()49}50})5152}53util.inherits(ForeverAgent, Agent)5455ForeverAgent.defaultMinSockets = 5565758ForeverAgent.prototype.createConnection = net.createConnection59ForeverAgent.prototype.addRequestNoreuse = Agent.prototype.addRequest60ForeverAgent.prototype.addRequest = function(req, host, port) {61var name = getConnectionName(host, port)6263if (typeof host !== 'string') {64var options = host65port = options.port66host = options.host67}6869if (this.freeSockets[name] && this.freeSockets[name].length > 0 && !req.useChunkedEncodingByDefault) {70var idleSocket = this.freeSockets[name].pop()71idleSocket.removeListener('error', idleSocket._onIdleError)72delete idleSocket._onIdleError73req._reusedSocket = true74req.onSocket(idleSocket)75} else {76this.addRequestNoreuse(req, host, port)77}78}7980ForeverAgent.prototype.removeSocket = function(s, name, host, port) {81if (this.sockets[name]) {82var index = this.sockets[name].indexOf(s)83if (index !== -1) {84this.sockets[name].splice(index, 1)85}86} else if (this.sockets[name] && this.sockets[name].length === 0) {87// don't leak88delete this.sockets[name]89delete this.requests[name]90}9192if (this.freeSockets[name]) {93var index = this.freeSockets[name].indexOf(s)94if (index !== -1) {95this.freeSockets[name].splice(index, 1)96if (this.freeSockets[name].length === 0) {97delete this.freeSockets[name]98}99}100}101102if (this.requests[name] && this.requests[name].length) {103// If we have pending requests and a socket gets closed a new one104// needs to be created to take over in the pool for the one that closed.105this.createSocket(name, host, port).emit('free')106}107}108109function ForeverAgentSSL (options) {110ForeverAgent.call(this, options)111}112util.inherits(ForeverAgentSSL, ForeverAgent)113114ForeverAgentSSL.prototype.createConnection = createConnectionSSL115ForeverAgentSSL.prototype.addRequestNoreuse = AgentSSL.prototype.addRequest116117function createConnectionSSL (port, host, options) {118if (typeof port === 'object') {119options = port;120} else if (typeof host === 'object') {121options = host;122} else if (typeof options === 'object') {123options = options;124} else {125options = {};126}127128if (typeof port === 'number') {129options.port = port;130}131132if (typeof host === 'string') {133options.host = host;134}135136return tls.connect(options);137}138139140