@@ -140,6 +140,7 @@ function RedisClient (options, stream) {
140
140
this . pipeline = 0 ;
141
141
this . times_connected = 0 ;
142
142
this . options = options ;
143
+ this . buffers = options . return_buffers || options . detect_buffers ;
143
144
// Init parser
144
145
this . reply_parser = Parser ( {
145
146
returnReply : function ( data ) {
@@ -154,7 +155,7 @@ function RedisClient (options, stream) {
154
155
self . stream . destroy ( ) ;
155
156
self . return_error ( err ) ;
156
157
} ,
157
- returnBuffers : options . return_buffers || options . detect_buffers ,
158
+ returnBuffers : this . buffers ,
158
159
name : options . parser
159
160
} ) ;
160
161
this . create_stream ( ) ;
@@ -329,9 +330,7 @@ RedisClient.prototype.on_error = function (err) {
329
330
}
330
331
331
332
err . message = 'Redis connection to ' + this . address + ' failed - ' + err . message ;
332
-
333
333
debug ( err . message ) ;
334
-
335
334
this . connected = false ;
336
335
this . ready = false ;
337
336
@@ -369,12 +368,6 @@ RedisClient.prototype.on_ready = function () {
369
368
debug ( 'on_ready called ' + this . address + ' id ' + this . connection_id ) ;
370
369
this . ready = true ;
371
370
372
- if ( this . old_state !== null ) {
373
- this . monitoring = this . old_state . monitoring ;
374
- this . pub_sub_mode = this . old_state . pub_sub_mode ;
375
- this . old_state = null ;
376
- }
377
-
378
371
var cork ;
379
372
if ( ! this . stream . cork ) {
380
373
cork = function ( len ) {
@@ -393,16 +386,15 @@ RedisClient.prototype.on_ready = function () {
393
386
}
394
387
this . cork = cork ;
395
388
396
- // restore modal commands from previous connection
389
+ // restore modal commands from previous connection. The order of the commands is important
397
390
if ( this . selected_db !== undefined ) {
398
- // this trick works if and only if the following send_command
399
- // never goes into the offline queue
400
- var pub_sub_mode = this . pub_sub_mode ;
401
- this . pub_sub_mode = false ;
402
391
this . send_command ( 'select' , [ this . selected_db ] ) ;
403
- this . pub_sub_mode = pub_sub_mode ;
404
392
}
405
- if ( this . pub_sub_mode === true ) {
393
+ if ( this . old_state !== null ) {
394
+ this . monitoring = this . old_state . monitoring ;
395
+ this . pub_sub_mode = this . old_state . pub_sub_mode ;
396
+ }
397
+ if ( this . pub_sub_mode ) {
406
398
// only emit 'ready' when all subscriptions were made again
407
399
var callback_count = 0 ;
408
400
var callback = function ( ) {
@@ -424,12 +416,10 @@ RedisClient.prototype.on_ready = function () {
424
416
} ) ;
425
417
return ;
426
418
}
427
-
428
419
if ( this . monitoring ) {
429
420
this . send_command ( 'monitor' , [ ] ) ;
430
- } else {
431
- this . send_offline_queue ( ) ;
432
421
}
422
+ this . send_offline_queue ( ) ;
433
423
this . emit ( 'ready' ) ;
434
424
} ;
435
425
@@ -525,15 +515,13 @@ RedisClient.prototype.connection_gone = function (why, error) {
525
515
this . cork = noop ;
526
516
this . pipeline = 0 ;
527
517
528
- if ( this . old_state === null ) {
529
- var state = {
530
- monitoring : this . monitoring ,
531
- pub_sub_mode : this . pub_sub_mode
532
- } ;
533
- this . old_state = state ;
534
- this . monitoring = false ;
535
- this . pub_sub_mode = false ;
536
- }
518
+ var state = {
519
+ monitoring : this . monitoring ,
520
+ pub_sub_mode : this . pub_sub_mode
521
+ } ;
522
+ this . old_state = state ;
523
+ this . monitoring = false ;
524
+ this . pub_sub_mode = false ;
537
525
538
526
// since we are collapsing end and close, users don't expect to be called twice
539
527
if ( ! this . emitted_end ) {
@@ -604,9 +592,7 @@ RedisClient.prototype.connection_gone = function (why, error) {
604
592
} ;
605
593
606
594
RedisClient . prototype . return_error = function ( err ) {
607
- var command_obj = this . command_queue . shift ( ) ,
608
- queue_len = this . command_queue . length ;
609
-
595
+ var command_obj = this . command_queue . shift ( ) ;
610
596
if ( command_obj && command_obj . command && command_obj . command . toUpperCase ) {
611
597
err . command = command_obj . command . toUpperCase ( ) ;
612
598
}
@@ -617,8 +603,7 @@ RedisClient.prototype.return_error = function (err) {
617
603
err . code = match [ 1 ] ;
618
604
}
619
605
620
- this . emit_idle ( queue_len ) ;
621
-
606
+ this . emit_idle ( ) ;
622
607
utils . callback_or_emit ( this , command_obj && command_obj . callback , err ) ;
623
608
} ;
624
609
@@ -627,8 +612,8 @@ RedisClient.prototype.drain = function () {
627
612
this . should_buffer = false ;
628
613
} ;
629
614
630
- RedisClient . prototype . emit_idle = function ( queue_len ) {
631
- if ( queue_len === 0 && this . pub_sub_mode === false ) {
615
+ RedisClient . prototype . emit_idle = function ( ) {
616
+ if ( this . command_queue . length === 0 && this . pub_sub_mode === false ) {
632
617
this . emit ( 'idle' ) ;
633
618
}
634
619
} ;
@@ -640,20 +625,6 @@ function queue_state_error (self, command_obj) {
640
625
self . emit ( 'error' , err ) ;
641
626
}
642
627
643
- function monitor ( self , reply ) {
644
- if ( typeof reply !== 'string' ) {
645
- reply = reply . toString ( ) ;
646
- }
647
- // If in monitoring mode only two commands are valid ones: AUTH and MONITOR wich reply with OK
648
- var len = reply . indexOf ( ' ' ) ;
649
- var timestamp = reply . slice ( 0 , len ) ;
650
- var argindex = reply . indexOf ( '"' ) ;
651
- var args = reply . slice ( argindex + 1 , - 1 ) . split ( '" "' ) . map ( function ( elem ) {
652
- return elem . replace ( / \\ " / g, '"' ) ;
653
- } ) ;
654
- self . emit ( 'monitor' , timestamp , args ) ;
655
- }
656
-
657
628
function normal_reply ( self , reply , command_obj ) {
658
629
if ( typeof command_obj . callback === 'function' ) {
659
630
if ( 'exec' !== command_obj . command ) {
@@ -716,17 +687,15 @@ RedisClient.prototype.return_reply = function (reply) {
716
687
717
688
queue_len = this . command_queue . length ;
718
689
719
- this . emit_idle ( queue_len ) ;
690
+ this . emit_idle ( ) ;
720
691
721
692
if ( command_obj && ! command_obj . sub_command ) {
722
693
normal_reply ( this , reply , command_obj ) ;
723
694
} else if ( this . pub_sub_mode || command_obj && command_obj . sub_command ) {
724
695
return_pub_sub ( this , reply , command_obj ) ;
725
696
}
726
697
/* istanbul ignore else: this is a safety check that we should not be able to trigger */
727
- else if ( this . monitoring ) {
728
- monitor ( this , reply ) ;
729
- } else {
698
+ else if ( ! this . monitoring ) {
730
699
queue_state_error ( this , command_obj ) ;
731
700
}
732
701
} ;
@@ -837,8 +806,6 @@ RedisClient.prototype.send_command = function (command, args, callback) {
837
806
838
807
if ( command === 'subscribe' || command === 'psubscribe' || command === 'unsubscribe' || command === 'punsubscribe' ) {
839
808
this . pub_sub_command ( command_obj ) ; // TODO: This has to be moved to the result handler
840
- } else if ( command === 'monitor' ) {
841
- this . monitoring = true ;
842
809
} else if ( command === 'quit' ) {
843
810
this . closing = true ;
844
811
}
0 commit comments