@@ -140,6 +140,7 @@ function RedisClient (options, stream) {
140140 this . pipeline = 0 ;
141141 this . times_connected = 0 ;
142142 this . options = options ;
143+ this . buffers = options . return_buffers || options . detect_buffers ;
143144 // Init parser
144145 this . reply_parser = Parser ( {
145146 returnReply : function ( data ) {
@@ -154,7 +155,7 @@ function RedisClient (options, stream) {
154155 self . stream . destroy ( ) ;
155156 self . return_error ( err ) ;
156157 } ,
157- returnBuffers : options . return_buffers || options . detect_buffers ,
158+ returnBuffers : this . buffers ,
158159 name : options . parser
159160 } ) ;
160161 this . create_stream ( ) ;
@@ -329,9 +330,7 @@ RedisClient.prototype.on_error = function (err) {
329330 }
330331
331332 err . message = 'Redis connection to ' + this . address + ' failed - ' + err . message ;
332-
333333 debug ( err . message ) ;
334-
335334 this . connected = false ;
336335 this . ready = false ;
337336
@@ -369,12 +368,6 @@ RedisClient.prototype.on_ready = function () {
369368 debug ( 'on_ready called ' + this . address + ' id ' + this . connection_id ) ;
370369 this . ready = true ;
371370
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-
378371 var cork ;
379372 if ( ! this . stream . cork ) {
380373 cork = function ( len ) {
@@ -393,16 +386,15 @@ RedisClient.prototype.on_ready = function () {
393386 }
394387 this . cork = cork ;
395388
396- // restore modal commands from previous connection
389+ // restore modal commands from previous connection. The order of the commands is important
397390 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 ;
402391 this . send_command ( 'select' , [ this . selected_db ] ) ;
403- this . pub_sub_mode = pub_sub_mode ;
404392 }
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 ) {
406398 // only emit 'ready' when all subscriptions were made again
407399 var callback_count = 0 ;
408400 var callback = function ( ) {
@@ -424,12 +416,10 @@ RedisClient.prototype.on_ready = function () {
424416 } ) ;
425417 return ;
426418 }
427-
428419 if ( this . monitoring ) {
429420 this . send_command ( 'monitor' , [ ] ) ;
430- } else {
431- this . send_offline_queue ( ) ;
432421 }
422+ this . send_offline_queue ( ) ;
433423 this . emit ( 'ready' ) ;
434424} ;
435425
@@ -525,15 +515,13 @@ RedisClient.prototype.connection_gone = function (why, error) {
525515 this . cork = noop ;
526516 this . pipeline = 0 ;
527517
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 ;
537525
538526 // since we are collapsing end and close, users don't expect to be called twice
539527 if ( ! this . emitted_end ) {
@@ -604,9 +592,7 @@ RedisClient.prototype.connection_gone = function (why, error) {
604592} ;
605593
606594RedisClient . 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 ( ) ;
610596 if ( command_obj && command_obj . command && command_obj . command . toUpperCase ) {
611597 err . command = command_obj . command . toUpperCase ( ) ;
612598 }
@@ -617,8 +603,7 @@ RedisClient.prototype.return_error = function (err) {
617603 err . code = match [ 1 ] ;
618604 }
619605
620- this . emit_idle ( queue_len ) ;
621-
606+ this . emit_idle ( ) ;
622607 utils . callback_or_emit ( this , command_obj && command_obj . callback , err ) ;
623608} ;
624609
@@ -627,8 +612,8 @@ RedisClient.prototype.drain = function () {
627612 this . should_buffer = false ;
628613} ;
629614
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 ) {
632617 this . emit ( 'idle' ) ;
633618 }
634619} ;
@@ -640,20 +625,6 @@ function queue_state_error (self, command_obj) {
640625 self . emit ( 'error' , err ) ;
641626}
642627
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-
657628function normal_reply ( self , reply , command_obj ) {
658629 if ( typeof command_obj . callback === 'function' ) {
659630 if ( 'exec' !== command_obj . command ) {
@@ -716,17 +687,15 @@ RedisClient.prototype.return_reply = function (reply) {
716687
717688 queue_len = this . command_queue . length ;
718689
719- this . emit_idle ( queue_len ) ;
690+ this . emit_idle ( ) ;
720691
721692 if ( command_obj && ! command_obj . sub_command ) {
722693 normal_reply ( this , reply , command_obj ) ;
723694 } else if ( this . pub_sub_mode || command_obj && command_obj . sub_command ) {
724695 return_pub_sub ( this , reply , command_obj ) ;
725696 }
726697 /* 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 ) {
730699 queue_state_error ( this , command_obj ) ;
731700 }
732701} ;
@@ -837,8 +806,6 @@ RedisClient.prototype.send_command = function (command, args, callback) {
837806
838807 if ( command === 'subscribe' || command === 'psubscribe' || command === 'unsubscribe' || command === 'punsubscribe' ) {
839808 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 ;
842809 } else if ( command === 'quit' ) {
843810 this . closing = true ;
844811 }
0 commit comments