@@ -21,6 +21,7 @@ var debug = function(msg) {
21
21
} ;
22
22
23
23
function noop ( ) { }
24
+ function clone ( obj ) { return JSON . parse ( JSON . stringify ( obj || { } ) ) ; }
24
25
25
26
exports . debug_mode = / \b r e d i s \b / i. test ( process . env . NODE_DEBUG ) ;
26
27
@@ -34,31 +35,22 @@ try {
34
35
35
36
parsers . push ( require ( './lib/parsers/javascript' ) ) ;
36
37
37
- function RedisClient ( stream , options ) {
38
+ function RedisClient ( options ) {
38
39
// Copy the options so they are not mutated
39
- options = JSON . parse ( JSON . stringify ( options || { } ) ) ;
40
+ options = clone ( options ) ;
41
+ events . EventEmitter . call ( this ) ;
40
42
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 ;
50
47
} 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 ;
56
52
}
57
- this . once ( 'ready' , function ( ) {
58
- self . cork = cork ;
59
- } ) ;
60
-
61
- this . stream = stream ;
53
+ this . connection_option = cnx_options ;
62
54
this . connection_id = ++ connection_id ;
63
55
this . connected = false ;
64
56
this . ready = false ;
@@ -69,10 +61,8 @@ function RedisClient(stream, options) {
69
61
if ( options . socket_keepalive === undefined ) {
70
62
options . socket_keepalive = true ;
71
63
}
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 ] ;
76
66
}
77
67
options . return_buffers = ! ! options . return_buffers ;
78
68
options . detect_buffers = ! ! options . detect_buffers ;
@@ -98,14 +88,15 @@ function RedisClient(stream, options) {
98
88
this . parser_module = null ;
99
89
this . selected_db = null ; // Save the selected db here, used when reconnecting
100
90
this . old_state = null ;
91
+ this . pipeline = 0 ;
101
92
this . options = options ;
102
93
103
- this . install_stream_listeners ( ) ;
104
- events . EventEmitter . call ( this ) ;
94
+ self . stream = net . createConnection ( cnx_options ) ;
95
+ self . install_stream_listeners ( ) ;
105
96
}
106
97
util . inherits ( RedisClient , events . EventEmitter ) ;
107
98
108
- RedisClient . prototype . install_stream_listeners = function ( ) {
99
+ RedisClient . prototype . install_stream_listeners = function ( ) {
109
100
var self = this ;
110
101
111
102
if ( this . options . connect_timeout ) {
@@ -144,9 +135,7 @@ RedisClient.prototype.install_stream_listeners = function() {
144
135
} ;
145
136
146
137
RedisClient . prototype . cork = noop ;
147
- RedisClient . prototype . uncork = function ( ) {
148
- this . stream . uncork ( ) ;
149
- } ;
138
+ RedisClient . prototype . uncork = noop ;
150
139
151
140
RedisClient . prototype . initialize_retry_vars = function ( ) {
152
141
this . retry_timer = null ;
@@ -332,6 +321,24 @@ RedisClient.prototype.on_ready = function () {
332
321
this . old_state = null ;
333
322
}
334
323
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
+
335
342
// magically restore any modal commands from a previous connection
336
343
if ( this . selected_db !== null ) {
337
344
// this trick works if and only if the following send_command
@@ -472,7 +479,7 @@ var retry_connection = function (self) {
472
479
self . attempts += 1 ;
473
480
self . retry_delay = Math . round ( self . retry_delay * self . retry_backoff ) ;
474
481
475
- self . stream = net . createConnection ( self . connectionOption ) ;
482
+ self . stream = net . createConnection ( self . connection_option ) ;
476
483
self . install_stream_listeners ( ) ;
477
484
478
485
self . retry_timer = null ;
@@ -488,6 +495,9 @@ RedisClient.prototype.connection_gone = function (why) {
488
495
debug ( 'Redis connection is gone from ' + why + ' event.' ) ;
489
496
this . connected = false ;
490
497
this . ready = false ;
498
+ // Deactivate cork to work with the offline queue
499
+ this . cork = noop ;
500
+ this . pipeline = 0 ;
491
501
492
502
if ( this . old_state === null ) {
493
503
var state = {
@@ -1219,56 +1229,30 @@ Multi.prototype.exec = Multi.prototype.EXEC = Multi.prototype.exec_batch = funct
1219
1229
return this . _client . should_buffer ;
1220
1230
} ;
1221
1231
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
-
1250
1232
var createClient = function ( port_arg , host_arg , options ) {
1251
1233
if ( typeof port_arg === 'object' || port_arg === undefined ) {
1252
1234
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 ) ;
1261
1241
var parsed = URL . parse ( port_arg , true , true ) ;
1262
1242
if ( parsed . hostname ) {
1263
1243
if ( parsed . auth ) {
1264
1244
options . auth_pass = parsed . auth . split ( ':' ) [ 1 ] ;
1265
1245
}
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 ;
1267
1250
}
1268
-
1269
- return createClient_unix ( port_arg , options ) ;
1270
1251
}
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 ) ;
1272
1256
} ;
1273
1257
1274
1258
exports . createClient = createClient ;
0 commit comments