@@ -27,7 +27,7 @@ if (typeof EventEmitter !== 'function') {
27
27
function noop ( ) { }
28
28
29
29
function handle_detect_buffers_reply ( reply , command , buffer_args ) {
30
- if ( buffer_args === false ) {
30
+ if ( buffer_args === false || this . message_buffers ) {
31
31
// If detect_buffers option was specified, then the reply from the parser will be a buffer.
32
32
// If this command did not use Buffer arguments, then convert the reply to Strings here.
33
33
reply = utils . reply_to_strings ( reply ) ;
@@ -138,6 +138,7 @@ function RedisClient (options, stream) {
138
138
this . pub_sub_mode = 0 ;
139
139
this . subscription_set = { } ;
140
140
this . monitoring = false ;
141
+ this . message_buffers = false ;
141
142
this . closing = false ;
142
143
this . server_info = { } ;
143
144
this . auth_pass = options . auth_pass || options . password ;
@@ -149,23 +150,7 @@ function RedisClient (options, stream) {
149
150
this . options = options ;
150
151
this . buffers = options . return_buffers || options . detect_buffers ;
151
152
// Init parser
152
- this . reply_parser = Parser ( {
153
- returnReply : function ( data ) {
154
- self . return_reply ( data ) ;
155
- } ,
156
- returnError : function ( err ) {
157
- self . return_error ( err ) ;
158
- } ,
159
- returnFatalError : function ( err ) {
160
- // Error out all fired commands. Otherwise they might rely on faulty data. We have to reconnect to get in a working state again
161
- self . flush_and_error ( err , [ 'command_queue' ] ) ;
162
- self . stream . destroy ( ) ;
163
- self . return_error ( err ) ;
164
- } ,
165
- returnBuffers : this . buffers ,
166
- name : options . parser ,
167
- stringNumbers : options . string_numbers
168
- } ) ;
153
+ this . reply_parser = create_parser ( this , options ) ;
169
154
this . create_stream ( ) ;
170
155
// The listeners will not be attached right away, so let's print the deprecation message while the listener is attached
171
156
this . on ( 'newListener' , function ( event ) {
@@ -179,13 +164,37 @@ function RedisClient (options, stream) {
179
164
'The drain event listener is deprecated and will be removed in v.3.0.0.\n' +
180
165
'If you want to keep on listening to this event please listen to the stream drain event directly.'
181
166
) ;
167
+ } else if ( event === 'message_buffer' || event === 'pmessage_buffer' || event === 'messageBuffer' || event === 'pmessageBuffer' && ! this . buffers ) {
168
+ this . message_buffers = true ;
169
+ this . handle_reply = handle_detect_buffers_reply ;
170
+ this . reply_parser = create_parser ( this ) ;
182
171
}
183
172
} ) ;
184
173
}
185
174
util . inherits ( RedisClient , EventEmitter ) ;
186
175
187
176
RedisClient . connection_id = 0 ;
188
177
178
+ function create_parser ( self ) {
179
+ return Parser ( {
180
+ returnReply : function ( data ) {
181
+ self . return_reply ( data ) ;
182
+ } ,
183
+ returnError : function ( err ) {
184
+ self . return_error ( err ) ;
185
+ } ,
186
+ returnFatalError : function ( err ) {
187
+ // Error out all fired commands. Otherwise they might rely on faulty data. We have to reconnect to get in a working state again
188
+ self . flush_and_error ( err , [ 'command_queue' ] ) ;
189
+ self . stream . destroy ( ) ;
190
+ self . return_error ( err ) ;
191
+ } ,
192
+ returnBuffers : self . buffers || self . message_buffers ,
193
+ name : self . options . parser ,
194
+ stringNumbers : self . options . string_numbers
195
+ } ) ;
196
+ }
197
+
189
198
/******************************************************************************
190
199
191
200
All functions in here are internal besides the RedisClient constructor
@@ -696,21 +705,18 @@ function subscribe_unsubscribe (self, reply, type, subscribe) {
696
705
function return_pub_sub ( self , reply ) {
697
706
var type = reply [ 0 ] . toString ( ) ;
698
707
if ( type === 'message' ) { // channel, message
699
- // TODO: Implement message_buffer
700
- // if (self.buffers) {
701
- // self.emit('message_buffer', reply[1], reply[2]);
702
- // }
703
- if ( ! self . options . return_buffers ) { // backwards compatible. Refactor this in v.3 to always return a string on the normal emitter
708
+ if ( ! self . options . return_buffers || self . message_buffers ) { // backwards compatible. Refactor this in v.3 to always return a string on the normal emitter
704
709
self . emit ( 'message' , reply [ 1 ] . toString ( ) , reply [ 2 ] . toString ( ) ) ;
710
+ self . emit ( 'message_buffer' , reply [ 1 ] , reply [ 2 ] ) ;
711
+ self . emit ( 'messageBuffer' , reply [ 1 ] , reply [ 2 ] ) ;
705
712
} else {
706
713
self . emit ( 'message' , reply [ 1 ] , reply [ 2 ] ) ;
707
714
}
708
715
} else if ( type === 'pmessage' ) { // pattern, channel, message
709
- // if (self.buffers) {
710
- // self.emit('pmessage_buffer', reply[1], reply[2], reply[3]);
711
- // }
712
- if ( ! self . options . return_buffers ) { // backwards compatible. Refactor this in v.3 to always return a string on the normal emitter
716
+ if ( ! self . options . return_buffers || self . message_buffers ) { // backwards compatible. Refactor this in v.3 to always return a string on the normal emitter
713
717
self . emit ( 'pmessage' , reply [ 1 ] . toString ( ) , reply [ 2 ] . toString ( ) , reply [ 3 ] . toString ( ) ) ;
718
+ self . emit ( 'pmessage_buffer' , reply [ 1 ] , reply [ 2 ] , reply [ 3 ] ) ;
719
+ self . emit ( 'pmessageBuffer' , reply [ 1 ] , reply [ 2 ] , reply [ 3 ] ) ;
714
720
} else {
715
721
self . emit ( 'pmessage' , reply [ 1 ] , reply [ 2 ] , reply [ 3 ] ) ;
716
722
}
0 commit comments