@@ -21,6 +21,7 @@ var debug = function(msg) {
2121} ;
2222
2323function noop ( ) { }
24+ function clone ( obj ) { return JSON . parse ( JSON . stringify ( obj || { } ) ) ; }
2425
2526exports . debug_mode = / \b r e d i s \b / i. test ( process . env . NODE_DEBUG ) ;
2627
@@ -34,31 +35,22 @@ try {
3435
3536parsers . push ( require ( './lib/parsers/javascript' ) ) ;
3637
37- function RedisClient ( stream , options ) {
38+ function RedisClient ( options ) {
3839 // Copy the options so they are not mutated
39- options = JSON . parse ( JSON . stringify ( options || { } ) ) ;
40+ options = clone ( options ) ;
41+ events . EventEmitter . call ( this ) ;
4042 var self = this ;
41-
42- this . pipeline = 0 ;
43- var cork ;
44- if ( ! stream . cork ) {
45- cork = function ( len ) {
46- self . pipeline = len ;
47- self . pipeline_queue = new Queue ( len ) ;
48- } ;
49- this . uncork = noop ;
43+ var cnx_options = { } ;
44+ if ( options . path ) {
45+ cnx_options . path = options . path ;
46+ this . address = options . path ;
5047 } else {
51- cork = function ( len ) {
52- self . pipeline = len ;
53- self . pipeline_queue = new Queue ( len ) ;
54- self . stream . cork ( ) ;
55- } ;
48+ cnx_options . port = options . port || default_port ;
49+ cnx_options . host = options . host || default_host ;
50+ cnx_options . family = options . family === 'IPv6' ? 6 : 4 ;
51+ this . address = cnx_options . host + ':' + cnx_options . port ;
5652 }
57- this . once ( 'ready' , function ( ) {
58- self . cork = cork ;
59- } ) ;
60-
61- this . stream = stream ;
53+ this . connection_option = cnx_options ;
6254 this . connection_id = ++ connection_id ;
6355 this . connected = false ;
6456 this . ready = false ;
@@ -69,10 +61,8 @@ function RedisClient(stream, options) {
6961 if ( options . socket_keepalive === undefined ) {
7062 options . socket_keepalive = true ;
7163 }
72- if ( options . rename_commands ) {
73- for ( var command in options . rename_commands ) { // jshint ignore: line
74- options . rename_commands [ command . toLowerCase ( ) ] = options . rename_commands [ command ] ;
75- }
64+ for ( var command in options . rename_commands ) { // jshint ignore: line
65+ options . rename_commands [ command . toLowerCase ( ) ] = options . rename_commands [ command ] ;
7666 }
7767 options . return_buffers = ! ! options . return_buffers ;
7868 options . detect_buffers = ! ! options . detect_buffers ;
@@ -98,14 +88,15 @@ function RedisClient(stream, options) {
9888 this . parser_module = null ;
9989 this . selected_db = null ; // Save the selected db here, used when reconnecting
10090 this . old_state = null ;
91+ this . pipeline = 0 ;
10192 this . options = options ;
10293
103- this . install_stream_listeners ( ) ;
104- events . EventEmitter . call ( this ) ;
94+ self . stream = net . createConnection ( cnx_options ) ;
95+ self . install_stream_listeners ( ) ;
10596}
10697util . inherits ( RedisClient , events . EventEmitter ) ;
10798
108- RedisClient . prototype . install_stream_listeners = function ( ) {
99+ RedisClient . prototype . install_stream_listeners = function ( ) {
109100 var self = this ;
110101
111102 if ( this . options . connect_timeout ) {
@@ -144,9 +135,7 @@ RedisClient.prototype.install_stream_listeners = function() {
144135} ;
145136
146137RedisClient . prototype . cork = noop ;
147- RedisClient . prototype . uncork = function ( ) {
148- this . stream . uncork ( ) ;
149- } ;
138+ RedisClient . prototype . uncork = noop ;
150139
151140RedisClient . prototype . initialize_retry_vars = function ( ) {
152141 this . retry_timer = null ;
@@ -332,6 +321,24 @@ RedisClient.prototype.on_ready = function () {
332321 this . old_state = null ;
333322 }
334323
324+ var cork ;
325+ if ( ! this . stream . cork ) {
326+ cork = function ( len ) {
327+ self . pipeline = len ;
328+ self . pipeline_queue = new Queue ( len ) ;
329+ } ;
330+ } else {
331+ cork = function ( len ) {
332+ self . pipeline = len ;
333+ self . pipeline_queue = new Queue ( len ) ;
334+ self . stream . cork ( ) ;
335+ } ;
336+ this . uncork = function ( ) {
337+ self . stream . uncork ( ) ;
338+ } ;
339+ }
340+ this . cork = cork ;
341+
335342 // magically restore any modal commands from a previous connection
336343 if ( this . selected_db !== null ) {
337344 // this trick works if and only if the following send_command
@@ -472,7 +479,7 @@ var retry_connection = function (self) {
472479 self . attempts += 1 ;
473480 self . retry_delay = Math . round ( self . retry_delay * self . retry_backoff ) ;
474481
475- self . stream = net . createConnection ( self . connectionOption ) ;
482+ self . stream = net . createConnection ( self . connection_option ) ;
476483 self . install_stream_listeners ( ) ;
477484
478485 self . retry_timer = null ;
@@ -488,6 +495,9 @@ RedisClient.prototype.connection_gone = function (why) {
488495 debug ( 'Redis connection is gone from ' + why + ' event.' ) ;
489496 this . connected = false ;
490497 this . ready = false ;
498+ // Deactivate cork to work with the offline queue
499+ this . cork = noop ;
500+ this . pipeline = 0 ;
491501
492502 if ( this . old_state === null ) {
493503 var state = {
@@ -1219,56 +1229,30 @@ Multi.prototype.exec = Multi.prototype.EXEC = Multi.prototype.exec_batch = funct
12191229 return this . _client . should_buffer ;
12201230} ;
12211231
1222- var createClient_unix = function ( path , options ) {
1223- var cnxOptions = {
1224- path : path
1225- } ;
1226- var net_client = net . createConnection ( cnxOptions ) ;
1227- var redis_client = new RedisClient ( net_client , options ) ;
1228-
1229- redis_client . connectionOption = cnxOptions ;
1230- redis_client . address = path ;
1231-
1232- return redis_client ;
1233- } ;
1234-
1235- var createClient_tcp = function ( port_arg , host_arg , options ) {
1236- var cnxOptions = {
1237- port : port_arg || default_port ,
1238- host : host_arg || default_host ,
1239- family : options . family === 'IPv6' ? 6 : 4
1240- } ;
1241- var net_client = net . createConnection ( cnxOptions ) ;
1242- var redis_client = new RedisClient ( net_client , options ) ;
1243-
1244- redis_client . connectionOption = cnxOptions ;
1245- redis_client . address = cnxOptions . host + ':' + cnxOptions . port ;
1246-
1247- return redis_client ;
1248- } ;
1249-
12501232var createClient = function ( port_arg , host_arg , options ) {
12511233 if ( typeof port_arg === 'object' || port_arg === undefined ) {
12521234 options = port_arg || options || { } ;
1253- return createClient_tcp ( + options . port , options . host , options ) ;
1254- }
1255- if ( typeof port_arg === 'number' || typeof port_arg === 'string' && / ^ \d + $ / . test ( port_arg ) ) {
1256- return createClient_tcp ( port_arg , host_arg , options || { } ) ;
1257- }
1258- if ( typeof port_arg === 'string' ) {
1259- options = host_arg || options || { } ;
1260-
1235+ } else if ( typeof port_arg === 'number' || typeof port_arg === 'string' && / ^ \d + $ / . test ( port_arg ) ) {
1236+ options = clone ( options ) ;
1237+ options . host = host_arg ;
1238+ options . port = port_arg ;
1239+ } else if ( typeof port_arg === 'string' ) {
1240+ options = clone ( host_arg || options ) ;
12611241 var parsed = URL . parse ( port_arg , true , true ) ;
12621242 if ( parsed . hostname ) {
12631243 if ( parsed . auth ) {
12641244 options . auth_pass = parsed . auth . split ( ':' ) [ 1 ] ;
12651245 }
1266- return createClient_tcp ( parsed . port , parsed . hostname , options ) ;
1246+ options . host = parsed . hostname ;
1247+ options . port = parsed . port ;
1248+ } else {
1249+ options . path = port_arg ;
12671250 }
1268-
1269- return createClient_unix ( port_arg , options ) ;
12701251 }
1271- throw new Error ( 'Unknown type of connection in createClient()' ) ;
1252+ if ( ! options ) {
1253+ throw new Error ( 'Unknown type of connection in createClient()' ) ;
1254+ }
1255+ return new RedisClient ( options ) ;
12721256} ;
12731257
12741258exports . createClient = createClient ;
0 commit comments