@@ -1118,10 +1118,12 @@ function Agent(options) {
11181118 this . options = options ;
11191119 this . host = options . host ;
11201120 this . port = options . port || this . defaultPort ;
1121+ this . socketPath = options . socketPath ;
11211122
11221123 this . queue = [ ] ;
11231124 this . sockets = [ ] ;
11241125 this . maxSockets = Agent . defaultMaxSockets ;
1126+
11251127}
11261128util . inherits ( Agent , EventEmitter ) ;
11271129exports . Agent = Agent ;
@@ -1161,7 +1163,7 @@ Agent.prototype._establishNewConnection = function() {
11611163
11621164 // Grab a new "socket". Depending on the implementation of _getConnection
11631165 // this could either be a raw TCP socket or a TLS stream.
1164- var socket = this . _getConnection ( this . host , this . port , function ( ) {
1166+ var socket = this . _getConnection ( self , function ( ) {
11651167 socket . _httpConnecting = false ;
11661168 self . emit ( 'connect' ) ; // mostly for the shim.
11671169 debug ( 'Agent _getConnection callback' ) ;
@@ -1342,9 +1344,18 @@ Agent.prototype._establishNewConnection = function() {
13421344
13431345// Sub-classes can overwrite this method with e.g. something that supplies
13441346// TLS streams.
1345- Agent . prototype . _getConnection = function ( host , port , cb ) {
1347+ Agent . prototype . _getConnection = function ( options , cb ) {
13461348 debug ( 'Agent connected!' ) ;
1347- var c = net . createConnection ( port , host ) ;
1349+
1350+ var c ;
1351+
1352+ if ( options . host ) {
1353+ c = net . createConnection ( options . port , options . host ) ;
1354+ } else if ( options . socketPath ) {
1355+ c = net . createConnection ( options . socketPath ) ;
1356+ } else {
1357+ c = net . createConnection ( options . port ) ;
1358+ }
13481359 c . on ( 'connect' , cb ) ;
13491360 return c ;
13501361} ;
@@ -1404,14 +1415,41 @@ Agent.prototype._cycle = function() {
14041415// to remove it?
14051416var agents = { } ;
14061417
1418+ // Backwards compatible with legacy getAgent(host, port);
1419+ function getAgent ( options ) {
1420+ var agent ;
1421+ var host ;
1422+ var id ;
1423+ var port ;
1424+
1425+ var _opts = { } ;
1426+
1427+ if ( options instanceof String ) {
1428+ port = arguments [ 1 ] || 80 ;
1429+ id = options + ':' + port ;
1430+ _opts . host = options ;
1431+ _opts . port = port ;
1432+ } else if ( options instanceof Object ) {
1433+ if ( options . port || options . host ) {
1434+ host = options . host || 'localhost' ;
1435+ port = options . port || 80 ;
1436+ id = host + port ;
1437+ _opts . host = host ;
1438+ _opts . port = port ;
1439+ } else if ( options . socketPath ) {
1440+ id = options . socketPath ;
1441+ _opts . socketPath = options . socketPath ;
1442+ } else {
1443+ throw new TypeError ( 'Invalid options specification to getAgent' ) ;
1444+ }
1445+ } else {
1446+ throw new TypeError ( 'Invalid argument to getAgent' ) ;
1447+ }
14071448
1408- function getAgent ( host , port ) {
1409- port = port || 80 ;
1410- var id = host + ':' + port ;
1411- var agent = agents [ id ] ;
1449+ agent = agents [ id ] ;
14121450
14131451 if ( ! agent ) {
1414- agent = agents [ id ] = new Agent ( { host : host , port : port } ) ;
1452+ agent = agents [ id ] = new Agent ( _opts ) ;
14151453 }
14161454
14171455 return agent ;
@@ -1429,7 +1467,7 @@ exports._requestFromAgent = function(options, cb) {
14291467
14301468exports . request = function ( options , cb ) {
14311469 if ( options . agent === undefined ) {
1432- options . agent = getAgent ( options . host , options . port ) ;
1470+ options . agent = getAgent ( options ) ;
14331471 } else if ( options . agent === false ) {
14341472 options . agent = new Agent ( options ) ;
14351473 }
0 commit comments