@@ -178,7 +178,7 @@ RedisClient.prototype.flush_and_error = function (message) {
178178} ;
179179
180180RedisClient . prototype . on_error = function ( msg ) {
181- var message = "Redis connection to " + this . host + ":" + this . port + " failed - " + msg ;
181+ var message = "Redis connection to " + this . address + " failed - " + msg ;
182182
183183 if ( this . closing ) {
184184 return ;
@@ -203,7 +203,7 @@ RedisClient.prototype.do_auth = function () {
203203 var self = this ;
204204
205205 if ( exports . debug_mode ) {
206- console . log ( "Sending auth to " + self . host + ":" + self . port + " id " + self . connection_id ) ;
206+ console . log ( "Sending auth to " + self . address + " id " + self . connection_id ) ;
207207 }
208208 self . send_anyway = true ;
209209 self . send_command ( "auth" , [ this . auth_pass ] , function ( err , res ) {
@@ -227,7 +227,7 @@ RedisClient.prototype.do_auth = function () {
227227 return self . emit ( "error" , new Error ( "Auth failed: " + res . toString ( ) ) ) ;
228228 }
229229 if ( exports . debug_mode ) {
230- console . log ( "Auth succeeded " + self . host + ":" + self . port + " id " + self . connection_id ) ;
230+ console . log ( "Auth succeeded " + self . address + " id " + self . connection_id ) ;
231231 }
232232 if ( self . auth_callback ) {
233233 self . auth_callback ( err , res ) ;
@@ -249,7 +249,7 @@ RedisClient.prototype.do_auth = function () {
249249
250250RedisClient . prototype . on_connect = function ( ) {
251251 if ( exports . debug_mode ) {
252- console . log ( "Stream connected " + this . host + ":" + this . port + " id " + this . connection_id ) ;
252+ console . log ( "Stream connected " + this . address + " id " + this . connection_id ) ;
253253 }
254254
255255 this . connected = true ;
@@ -532,15 +532,15 @@ RedisClient.prototype.connection_gone = function (why) {
532532 return ;
533533 }
534534
535- self . stream = net . createConnection ( self . port , self . host ) ;
535+ self . stream = net . createConnection ( self . connectionOption ) ;
536536 self . install_stream_listeners ( ) ;
537537 self . retry_timer = null ;
538538 } , this . retry_delay ) ;
539539} ;
540540
541541RedisClient . prototype . on_data = function ( data ) {
542542 if ( exports . debug_mode ) {
543- console . log ( "net read " + this . host + ":" + this . port + " id " + this . connection_id + ": " + data . toString ( ) ) ;
543+ console . log ( "net read " + this . address + " id " + this . connection_id + ": " + data . toString ( ) ) ;
544544 }
545545
546546 try {
@@ -852,7 +852,7 @@ RedisClient.prototype.send_command = function (command, args, callback) {
852852 command_str += "$" + Buffer . byteLength ( arg ) + "\r\n" + arg + "\r\n" ;
853853 }
854854 if ( exports . debug_mode ) {
855- console . log ( "send " + this . host + ":" + this . port + " id " + this . connection_id + ": " + command_str ) ;
855+ console . log ( "send " + this . address + " id " + this . connection_id + ": " + command_str ) ;
856856 }
857857 buffered_writes += ! stream . write ( command_str ) ;
858858 } else {
@@ -1213,28 +1213,64 @@ RedisClient.prototype.eval = RedisClient.prototype.EVAL = function () {
12131213} ;
12141214
12151215
1216- exports . createClient = function ( port_arg , host_arg , options ) {
1216+ exports . createClient = function ( arg0 , arg1 , arg2 ) {
1217+ if ( arguments . length === 0 ) {
12171218
1218- var cnxFamily ;
1219-
1220- if ( options && options . family ) {
1221- cnxFamily = ( options . family == 'IPv6' ? 6 : 4 ) ;
1222- }
1223-
1219+ // createClient()
1220+ return createClient_tcp ( default_port , default_host , { } ) ;
1221+
1222+ } else if ( typeof arg0 === 'number' ||
1223+ typeof arg0 === 'string' && arg0 . match ( / ^ \d + $ / ) ) {
1224+
1225+ // createClient( 3000, host, options)
1226+ // createClient('3000', host, options)
1227+ return createClient_tcp ( arg0 , arg1 , arg2 ) ;
1228+
1229+ } else if ( typeof arg0 === 'string' ) {
1230+
1231+ // createClient( '/tmp/redis.sock', options)
1232+ return createClient_unix ( arg0 , arg1 ) ;
1233+
1234+ } else if ( arg0 !== null && typeof arg0 === 'object' ) {
1235+
1236+ // createClient(options)
1237+ return createClient_tcp ( default_port , default_host , arg0 ) ;
1238+
1239+ } else if ( arg0 === null && arg1 === null ) {
1240+
1241+ // for backward compatibility
1242+ // createClient(null,null,options)
1243+ return createClient_tcp ( default_port , default_host , arg2 ) ;
1244+
1245+ } else {
1246+ throw new Error ( 'unknown type of connection in createClient()' ) ;
1247+ }
1248+ }
1249+
1250+ var createClient_unix = function ( path , options ) {
12241251 var cnxOptions = {
1225- 'port' : port_arg || default_port ,
1226- 'host' : host_arg || default_host ,
1227- 'family' : cnxFamily || '4'
1252+ path : path
12281253 } ;
1254+ var net_client = net . createConnection ( cnxOptions ) ;
1255+ var redis_client = new RedisClient ( net_client , options || { } ) ;
12291256
1230- var redis_client , net_client ;
1257+ redis_client . connectionOption = cnxOptions ;
1258+ redis_client . address = path ;
12311259
1232- net_client = net . createConnection ( cnxOptions ) ;
1260+ return redis_client ;
1261+ }
12331262
1234- redis_client = new RedisClient ( net_client , options ) ;
1263+ var createClient_tcp = function ( port_arg , host_arg , options ) {
1264+ var cnxOptions = {
1265+ 'port' : port_arg || default_port ,
1266+ 'host' : host_arg || default_host ,
1267+ 'family' : ( options && options . family === 'IPv6' ) ? 'IPv6' : 'IPv4'
1268+ } ;
1269+ var net_client = net . createConnection ( cnxOptions ) ;
1270+ var redis_client = new RedisClient ( net_client , options || { } ) ;
12351271
1236- redis_client . port = cnxOptions . port ;
1237- redis_client . host = cnxOptions . host ;
1272+ redis_client . connectionOption = cnxOptions ;
1273+ redis_client . address = cnxOptions . host + ':' + cnxOptions . port ;
12381274
12391275 return redis_client ;
12401276} ;
0 commit comments