33var net = require ( "net" ) ,
44 URL = require ( "url" ) ,
55 util = require ( "util" ) ,
6+ utils = require ( "./lib/utils" ) ,
67 Queue = require ( "./lib/queue" ) ,
7- to_array = require ( "./lib/to_array" ) ,
88 events = require ( "events" ) ,
99 parsers = [ ] ,
1010 // This static list of commands is updated from time to time.
@@ -23,14 +23,13 @@ exports.debug_mode = /\bredis\b/i.test(process.env.NODE_DEBUG);
2323
2424// hiredis might not be installed
2525try {
26- require ( "./lib/parser/hiredis" ) ;
27- parsers . push ( require ( "./lib/parser/hiredis" ) ) ;
26+ parsers . push ( require ( "./lib/parsers/hiredis" ) ) ;
2827} catch ( err ) {
2928 /* istanbul ignore next: won't be reached with tests */
3029 debug ( "Hiredis parser not installed." ) ;
3130}
3231
33- parsers . push ( require ( "./lib/parser /javascript" ) ) ;
32+ parsers . push ( require ( "./lib/parsers /javascript" ) ) ;
3433
3534function RedisClient ( stream , options ) {
3635 options = options || { } ;
@@ -135,13 +134,15 @@ RedisClient.prototype.flush_and_error = function (error) {
135134
136135 while ( command_obj = this . offline_queue . shift ( ) ) {
137136 if ( typeof command_obj . callback === "function" ) {
137+ error . command = command_obj . command . toUpperCase ( ) ;
138138 command_obj . callback ( error ) ;
139139 }
140140 }
141141 this . offline_queue = new Queue ( ) ;
142142
143143 while ( command_obj = this . command_queue . shift ( ) ) {
144144 if ( typeof command_obj . callback === "function" ) {
145+ error . command = command_obj . command . toUpperCase ( ) ;
145146 command_obj . callback ( error ) ;
146147 }
147148 }
@@ -530,41 +531,6 @@ RedisClient.prototype.return_error = function (err) {
530531 }
531532} ;
532533
533- // hgetall converts its replies to an Object. If the reply is empty, null is returned.
534- function reply_to_object ( reply ) {
535- var obj = { } , j , jl , key , val ;
536-
537- if ( reply . length === 0 || ! Array . isArray ( reply ) ) {
538- return null ;
539- }
540-
541- for ( j = 0 , jl = reply . length ; j < jl ; j += 2 ) {
542- key = reply [ j ] . toString ( 'binary' ) ;
543- val = reply [ j + 1 ] ;
544- obj [ key ] = val ;
545- }
546-
547- return obj ;
548- }
549-
550- function reply_to_strings ( reply ) {
551- var i ;
552-
553- if ( Buffer . isBuffer ( reply ) ) {
554- return reply . toString ( ) ;
555- }
556-
557- if ( Array . isArray ( reply ) ) {
558- for ( i = 0 ; i < reply . length ; i ++ ) {
559- // Recusivly call the function as slowlog returns deep nested replies
560- reply [ i ] = reply_to_strings ( reply [ i ] ) ;
561- }
562- return reply ;
563- }
564-
565- return reply ;
566- }
567-
568534RedisClient . prototype . return_reply = function ( reply ) {
569535 var command_obj , len , type , timestamp , argindex , args , queue_len ;
570536
@@ -598,12 +564,12 @@ RedisClient.prototype.return_reply = function (reply) {
598564 if ( this . options . detect_buffers && command_obj . buffer_args === false ) {
599565 // If detect_buffers option was specified, then the reply from the parser will be Buffers.
600566 // If this command did not use Buffer arguments, then convert the reply to Strings here.
601- reply = reply_to_strings ( reply ) ;
567+ reply = utils . reply_to_strings ( reply ) ;
602568 }
603569
604570 // TODO - confusing and error-prone that hgetall is special cased in two places
605571 if ( reply && 'hgetall' === command_obj . command ) {
606- reply = reply_to_object ( reply ) ;
572+ reply = utils . reply_to_object ( reply ) ;
607573 }
608574 }
609575
@@ -614,7 +580,7 @@ RedisClient.prototype.return_reply = function (reply) {
614580 } else if ( this . pub_sub_mode || command_obj && command_obj . sub_command ) {
615581 if ( Array . isArray ( reply ) ) {
616582 if ( ! this . options . return_buffers && ( ! command_obj || this . options . detect_buffers && command_obj . buffer_args === false ) ) {
617- reply = reply_to_strings ( reply ) ;
583+ reply = utils . reply_to_strings ( reply ) ;
618584 }
619585 type = reply [ 0 ] . toString ( ) ;
620586
@@ -850,7 +816,7 @@ RedisClient.prototype.end = function (flush) {
850816
851817 // Flush queue if wanted
852818 if ( flush ) {
853- this . flush_and_error ( "Redis connection ended." ) ;
819+ this . flush_and_error ( new Error ( "The command can't be processed. The connection has already been closed." ) ) ;
854820 }
855821
856822 this . connected = false ;
@@ -894,7 +860,7 @@ commands.forEach(function (fullCommand) {
894860 arg = [ key ] . concat ( arg ) ;
895861 return this . send_command ( command , arg , callback ) ;
896862 }
897- return this . send_command ( command , to_array ( arguments ) ) ;
863+ return this . send_command ( command , utils . to_array ( arguments ) ) ;
898864 } ;
899865 RedisClient . prototype [ command . toUpperCase ( ) ] = RedisClient . prototype [ command ] ;
900866
@@ -910,7 +876,7 @@ commands.forEach(function (fullCommand) {
910876 }
911877 this . queue . push ( [ command , key ] . concat ( arg ) ) ;
912878 } else {
913- this . queue . push ( [ command ] . concat ( to_array ( arguments ) ) ) ;
879+ this . queue . push ( [ command ] . concat ( utils . to_array ( arguments ) ) ) ;
914880 }
915881 return this ;
916882 } ;
@@ -979,7 +945,7 @@ RedisClient.prototype.hmset = RedisClient.prototype.HMSET = function (key, args,
979945 }
980946 return this . send_command ( "hmset" , tmp_args , callback ) ;
981947 }
982- return this . send_command ( "hmset" , to_array ( arguments ) ) ;
948+ return this . send_command ( "hmset" , utils . to_array ( arguments ) ) ;
983949} ;
984950
985951Multi . prototype . hmset = Multi . prototype . HMSET = function ( key , args , callback ) {
@@ -1008,7 +974,7 @@ Multi.prototype.hmset = Multi.prototype.HMSET = function (key, args, callback) {
1008974 tmp_args . push ( callback ) ;
1009975 }
1010976 } else {
1011- tmp_args = to_array ( arguments ) ;
977+ tmp_args = utils . to_array ( arguments ) ;
1012978 tmp_args . unshift ( "hmset" ) ;
1013979 }
1014980 this . queue . push ( tmp_args ) ;
@@ -1087,11 +1053,11 @@ Multi.prototype.execute_callback = function (err, replies) {
10871053 replies [ i ] . command = args [ 0 ] . toUpperCase ( ) ;
10881054 } else if ( replies [ i ] ) {
10891055 if ( this . _client . options . detect_buffers && this . wants_buffers [ i + 1 ] === false ) {
1090- replies [ i ] = reply_to_strings ( replies [ i ] ) ;
1056+ replies [ i ] = utils . reply_to_strings ( replies [ i ] ) ;
10911057 }
10921058 if ( args [ 0 ] === "hgetall" ) {
10931059 // TODO - confusing and error-prone that hgetall is special cased in two places
1094- replies [ i ] = reply_to_object ( replies [ i ] ) ;
1060+ replies [ i ] = utils . reply_to_object ( replies [ i ] ) ;
10951061 }
10961062 }
10971063
0 commit comments