@@ -29,14 +29,17 @@ try {
29
29
require ( "./lib/parser/hiredis" ) ;
30
30
parsers . push ( require ( "./lib/parser/hiredis" ) ) ;
31
31
} catch ( err ) {
32
+ /* istanbul ignore next: won't be reached with tests */
32
33
debug ( "hiredis parser not installed." ) ;
33
34
}
34
35
35
36
parsers . push ( require ( "./lib/parser/javascript" ) ) ;
36
37
37
38
function RedisClient ( stream , options ) {
39
+ options = options || { } ;
40
+
38
41
this . stream = stream ;
39
- this . options = options = options || { } ;
42
+ this . options = options ;
40
43
41
44
this . connection_id = ++ connection_id ;
42
45
this . connected = false ;
@@ -51,26 +54,23 @@ function RedisClient(stream, options) {
51
54
this . should_buffer = false ;
52
55
this . command_queue_high_water = this . options . command_queue_high_water || 1000 ;
53
56
this . command_queue_low_water = this . options . command_queue_low_water || 0 ;
54
- this . max_attempts = null ;
55
- if ( options . max_attempts && ! isNaN ( options . max_attempts ) && options . max_attempts > 0 ) {
57
+ if ( options . max_attempts && options . max_attempts > 0 ) {
56
58
this . max_attempts = + options . max_attempts ;
57
59
}
58
60
this . command_queue = new Queue ( ) ; // holds sent commands to de-pipeline them
59
61
this . offline_queue = new Queue ( ) ; // holds commands issued but not able to be sent
60
62
this . commands_sent = 0 ;
61
- this . connect_timeout = false ;
62
- if ( options . connect_timeout && ! isNaN ( options . connect_timeout ) && options . connect_timeout > 0 ) {
63
+ if ( options . connect_timeout && options . connect_timeout > 0 ) {
63
64
this . connect_timeout = + options . connect_timeout ;
64
65
}
65
66
this . enable_offline_queue = true ;
66
- if ( typeof this . options . enable_offline_queue === "boolean" ) {
67
- this . enable_offline_queue = this . options . enable_offline_queue ;
67
+ if ( this . options . enable_offline_queue === false ) {
68
+ this . enable_offline_queue = false ;
68
69
}
69
70
this . retry_max_delay = null ;
70
- if ( options . retry_max_delay !== undefined && ! isNaN ( options . retry_max_delay ) && options . retry_max_delay > 0 ) {
71
- this . retry_max_delay = options . retry_max_delay ;
71
+ if ( options . retry_max_delay && options . retry_max_delay > 0 ) {
72
+ this . retry_max_delay = + options . retry_max_delay ;
72
73
}
73
-
74
74
this . initialize_retry_vars ( ) ;
75
75
this . pub_sub_mode = false ;
76
76
this . subscription_set = { } ;
@@ -131,12 +131,10 @@ RedisClient.prototype.initialize_retry_vars = function () {
131
131
} ;
132
132
133
133
RedisClient . prototype . unref = function ( ) {
134
- debug ( "User requesting to unref the connection" ) ;
135
134
if ( this . connected ) {
136
135
debug ( "unref'ing the socket connection" ) ;
137
136
this . stream . unref ( ) ;
138
- }
139
- else {
137
+ } else {
140
138
debug ( "Not connected yet, will unref later" ) ;
141
139
this . once ( "connect" , function ( ) {
142
140
this . unref ( ) ;
@@ -210,23 +208,26 @@ RedisClient.prototype.do_auth = function () {
210
208
self . send_anyway = true ;
211
209
self . send_command ( "auth" , [ this . auth_pass ] , function ( err , res ) {
212
210
if ( err ) {
211
+ /* istanbul ignore if: this is almost impossible to test */
213
212
if ( loading . test ( err . message ) ) {
214
213
// if redis is still loading the db, it will not authenticate and everything else will fail
215
- console . log ( "Redis still loading, trying to authenticate later" ) ;
214
+ debug ( "Redis still loading, trying to authenticate later" ) ;
216
215
setTimeout ( function ( ) {
217
216
self . do_auth ( ) ;
218
217
} , 2000 ) ; // TODO - magic number alert
219
218
return ;
220
219
} else if ( noPasswordIsSet . test ( err . message ) ) {
221
- console . log ( "Warning: Redis server does not require a password, but a password was supplied." ) ;
220
+ debug ( "Warning: Redis server does not require a password, but a password was supplied." ) ;
222
221
err = null ;
223
222
res = "OK" ;
224
223
} else {
225
224
return self . emit ( "error" , new Error ( "Auth error: " + err . message ) ) ;
226
225
}
227
226
}
228
- if ( res . toString ( ) !== "OK" ) {
229
- return self . emit ( "error" , new Error ( "Auth failed: " + res . toString ( ) ) ) ;
227
+
228
+ res = res . toString ( ) ;
229
+ if ( res !== "OK" ) {
230
+ return self . emit ( "error" , new Error ( "Auth failed: " + res ) ) ;
230
231
}
231
232
232
233
debug ( "Auth succeeded " + self . address + " id " + self . connection_id ) ;
@@ -283,7 +284,7 @@ RedisClient.prototype.init_parser = function () {
283
284
var self = this ;
284
285
285
286
if ( this . options . parser ) {
286
- if ( ! parsers . some ( function ( parser ) {
287
+ if ( ! parsers . some ( function ( parser ) {
287
288
if ( parser . name === self . options . parser ) {
288
289
self . parser_module = parser ;
289
290
debug ( "Using parser module: " + self . parser_module . name ) ;
@@ -353,7 +354,9 @@ RedisClient.prototype.on_ready = function () {
353
354
self . send_command ( parts [ 0 ] + "scribe" , [ parts [ 1 ] ] , callback ) ;
354
355
} ) ;
355
356
return ;
356
- } else if ( this . monitoring ) {
357
+ }
358
+
359
+ if ( this . monitoring ) {
357
360
this . send_command ( "monitor" , [ ] ) ;
358
361
} else {
359
362
this . send_offline_queue ( ) ;
@@ -378,7 +381,7 @@ RedisClient.prototype.on_info_cmd = function (err, res) {
378
381
} ) ;
379
382
380
383
obj . versions = [ ] ;
381
- if ( obj . redis_version ) {
384
+ if ( obj . redis_version ) {
382
385
obj . redis_version . split ( '.' ) . forEach ( function ( num ) {
383
386
obj . versions . push ( + num ) ;
384
387
} ) ;
@@ -483,7 +486,7 @@ RedisClient.prototype.connection_gone = function (why) {
483
486
this . retry_timer = null ;
484
487
// TODO - some people need a "Redis is Broken mode" for future commands that errors immediately, and others
485
488
// want the program to exit. Right now, we just log, which doesn't really help in either case.
486
- console . error ( "node_redis: Couldn't get Redis connection after " + this . max_attempts + " attempts." ) ;
489
+ debug ( "node_redis: Couldn't get Redis connection after " + this . max_attempts + " attempts." ) ;
487
490
return ;
488
491
}
489
492
@@ -500,7 +503,7 @@ RedisClient.prototype.connection_gone = function (why) {
500
503
if ( self . connect_timeout && self . retry_totaltime >= self . connect_timeout ) {
501
504
self . retry_timer = null ;
502
505
// TODO - engage Redis is Broken mode for future commands, or whatever
503
- console . error ( "node_redis: Couldn't get Redis connection after " + self . retry_totaltime + "ms." ) ;
506
+ debug ( "node_redis: Couldn't get Redis connection after " + self . retry_totaltime + "ms." ) ;
504
507
return ;
505
508
}
506
509
@@ -525,7 +528,7 @@ RedisClient.prototype.on_data = function (data) {
525
528
} ;
526
529
527
530
RedisClient . prototype . return_error = function ( err ) {
528
- var command_obj = this . command_queue . shift ( ) , queue_len = this . command_queue . getLength ( ) ;
531
+ var command_obj = this . command_queue . shift ( ) , queue_len = this . command_queue . length ;
529
532
530
533
if ( this . pub_sub_mode === false && queue_len === 0 ) {
531
534
this . command_queue = new Queue ( ) ;
@@ -536,20 +539,12 @@ RedisClient.prototype.return_error = function (err) {
536
539
this . should_buffer = false ;
537
540
}
538
541
539
- if ( command_obj && typeof command_obj . callback === "function" ) {
540
- try {
541
- command_obj . callback ( err ) ;
542
- } catch ( callback_err ) {
543
- // if a callback throws an exception, re-throw it on a new stack so the parser can keep going
544
- process . nextTick ( function ( ) {
545
- throw callback_err ;
546
- } ) ;
547
- }
548
- } else {
549
- console . log ( "node_redis: no callback to send error: " + err . message ) ;
550
- // this will probably not make it anywhere useful, but we might as well throw
542
+ try {
543
+ command_obj . callback ( err ) ;
544
+ } catch ( callback_err ) {
545
+ // if a callback throws an exception, re-throw it on a new stack so the parser can keep going
551
546
process . nextTick ( function ( ) {
552
- throw err ;
547
+ throw callback_err ;
553
548
} ) ;
554
549
}
555
550
} ;
@@ -628,7 +623,7 @@ RedisClient.prototype.return_reply = function (reply) {
628
623
command_obj = this . command_queue . shift ( ) ;
629
624
}
630
625
631
- queue_len = this . command_queue . getLength ( ) ;
626
+ queue_len = this . command_queue . length ;
632
627
633
628
if ( this . pub_sub_mode === false && queue_len === 0 ) {
634
629
this . command_queue = new Queue ( ) ; // explicitly reclaim storage from old Queue
@@ -720,7 +715,7 @@ RedisClient.prototype.send_command = function (command, args, callback) {
720
715
// probably the fastest way:
721
716
// client.command([arg1, arg2], cb); (straight passthrough)
722
717
// send_command(command, [arg1, arg2], cb);
723
- } else if ( ! callback ) {
718
+ } else if ( ! callback ) {
724
719
// most people find this variable argument length form more convenient, but it uses arguments, which is slower
725
720
// client.command(arg1, arg2, cb); (wraps up arguments into an array)
726
721
// send_command(command, [arg1, arg2, cb]);
@@ -739,7 +734,9 @@ RedisClient.prototype.send_command = function (command, args, callback) {
739
734
throw new Error ( "send_command: second argument must be an array" ) ;
740
735
}
741
736
742
- if ( callback && process . domain ) callback = process . domain . bind ( callback ) ;
737
+ if ( callback && process . domain ) {
738
+ callback = process . domain . bind ( callback ) ;
739
+ }
743
740
744
741
// if the last argument is an array and command is sadd or srem, expand it out:
745
742
// client.sadd(arg1, [arg2, arg3, arg4], cb);
@@ -844,7 +841,7 @@ RedisClient.prototype.send_command = function (command, args, callback) {
844
841
}
845
842
}
846
843
debug ( "send_command buffered_writes: " + buffered_writes , " should_buffer: " + this . should_buffer ) ;
847
- if ( buffered_writes || this . command_queue . getLength ( ) >= this . command_queue_high_water ) {
844
+ if ( buffered_writes || this . command_queue . length >= this . command_queue_high_water ) {
848
845
this . should_buffer = true ;
849
846
}
850
847
return ! this . should_buffer ;
@@ -1141,9 +1138,7 @@ Multi.prototype.EXEC = Multi.prototype.exec;
1141
1138
RedisClient . prototype . multi = function ( args ) {
1142
1139
return new Multi ( this , args ) ;
1143
1140
} ;
1144
- RedisClient . prototype . MULTI = function ( args ) {
1145
- return new Multi ( this , args ) ;
1146
- } ;
1141
+ RedisClient . prototype . MULTI = RedisClient . prototype . multi ;
1147
1142
1148
1143
1149
1144
// stash original eval method
@@ -1177,26 +1172,26 @@ RedisClient.prototype.eval = RedisClient.prototype.EVAL = function () {
1177
1172
} ) ;
1178
1173
} ;
1179
1174
1180
- exports . createClient = function ( arg0 , arg1 , options ) {
1181
- if ( typeof arg0 === 'object' || arg0 === undefined ) {
1182
- options = arg0 || options ;
1175
+ exports . createClient = function ( port_arg , host_arg , options ) {
1176
+ if ( typeof port_arg === 'object' || port_arg === undefined ) {
1177
+ options = port_arg || options ;
1183
1178
return createClient_tcp ( default_port , default_host , options ) ;
1184
1179
}
1185
- if ( typeof arg0 === 'number' || typeof arg0 === 'string' && arg0 . match ( / ^ \d + $ / ) ) {
1186
- return createClient_tcp ( arg0 , arg1 , options ) ;
1180
+ if ( typeof port_arg === 'number' || typeof port_arg === 'string' && / ^ \d + $ / . test ( port_arg ) ) {
1181
+ return createClient_tcp ( port_arg , host_arg , options ) ;
1187
1182
}
1188
- if ( typeof arg0 === 'string' ) {
1189
- options = arg1 || { } ;
1183
+ if ( typeof port_arg === 'string' ) {
1184
+ options = host_arg || { } ;
1190
1185
1191
- var parsed = URL . parse ( arg0 , true , true ) ;
1186
+ var parsed = URL . parse ( port_arg , true , true ) ;
1192
1187
if ( parsed . hostname ) {
1193
1188
if ( parsed . auth ) {
1194
1189
options . auth_pass = parsed . auth . split ( ':' ) [ 1 ] ;
1195
1190
}
1196
- return createClient_tcp ( ( parsed . port || default_port ) , parsed . hostname , options ) ;
1191
+ return createClient_tcp ( parsed . port , parsed . hostname , options ) ;
1197
1192
}
1198
1193
1199
- return createClient_unix ( arg0 , options ) ;
1194
+ return createClient_unix ( port_arg , options ) ;
1200
1195
}
1201
1196
throw new Error ( 'unknown type of connection in createClient()' ) ;
1202
1197
} ;
@@ -1206,7 +1201,7 @@ var createClient_unix = function(path, options){
1206
1201
path : path
1207
1202
} ;
1208
1203
var net_client = net . createConnection ( cnxOptions ) ;
1209
- var redis_client = new RedisClient ( net_client , options || { } ) ;
1204
+ var redis_client = new RedisClient ( net_client , options ) ;
1210
1205
1211
1206
redis_client . connectionOption = cnxOptions ;
1212
1207
redis_client . address = path ;
@@ -1218,10 +1213,10 @@ var createClient_tcp = function (port_arg, host_arg, options) {
1218
1213
var cnxOptions = {
1219
1214
'port' : port_arg || default_port ,
1220
1215
'host' : host_arg || default_host ,
1221
- 'family' : ( options && options . family === 'IPv6' ) ? 6 : 4
1216
+ 'family' : options && options . family === 'IPv6' ? 6 : 4
1222
1217
} ;
1223
1218
var net_client = net . createConnection ( cnxOptions ) ;
1224
- var redis_client = new RedisClient ( net_client , options || { } ) ;
1219
+ var redis_client = new RedisClient ( net_client , options ) ;
1225
1220
1226
1221
redis_client . connectionOption = cnxOptions ;
1227
1222
redis_client . address = cnxOptions . host + ':' + cnxOptions . port ;
0 commit comments