@@ -54,6 +54,11 @@ ReplyParser.prototype._parseResult = function (type) {
54
54
throw new IncompleteReadBuffer ( "Wait for more data." ) ;
55
55
}
56
56
57
+ if ( type === 45 ) {
58
+ var result = this . _buffer . toString ( this . _encoding , start , end ) ;
59
+ return new Error ( result ) ;
60
+ }
61
+
57
62
if ( this . options . return_buffers ) {
58
63
return this . _buffer . slice ( start , end ) ;
59
64
} else {
@@ -76,12 +81,8 @@ ReplyParser.prototype._parseResult = function (type) {
76
81
throw new IncompleteReadBuffer ( "Wait for more data." ) ;
77
82
}
78
83
79
- if ( this . options . return_buffers ) {
80
- return this . _buffer . slice ( start , end ) ;
81
- }
82
-
83
84
// return the coerced numeric value
84
- return + small_toString ( this . _buffer , start , end ) ;
85
+ return + this . _buffer . toString ( 'ascii' , start , end ) ;
85
86
} else if ( type === 36 ) { // $
86
87
// set a rewind point, as the packet could be larger than the
87
88
// buffer in memory
@@ -123,7 +124,7 @@ ReplyParser.prototype._parseResult = function (type) {
123
124
throw new IncompleteReadBuffer ( "Wait for more data." ) ;
124
125
}
125
126
126
- var reply = [ ] ;
127
+ var reply = [ ] ;
127
128
var ntype , i , res ;
128
129
129
130
offset = this . _offset - 1 ;
@@ -152,36 +153,25 @@ ReplyParser.prototype.execute = function (buffer) {
152
153
153
154
while ( true ) {
154
155
offset = this . _offset ;
155
- try {
156
- // at least 4 bytes: :1\r\n
157
- if ( this . _bytesRemaining ( ) < 4 ) {
158
- break ;
159
- }
156
+ // at least 4 bytes: :1\r\n
157
+ if ( this . _bytesRemaining ( ) < 4 ) {
158
+ break ;
159
+ }
160
160
161
+ try {
161
162
type = this . _buffer [ this . _offset ++ ] ;
162
163
163
164
if ( type === 43 ) { // +
164
165
ret = this . _parseResult ( type ) ;
165
166
166
- if ( ret === null ) {
167
- break ;
168
- }
169
-
170
167
this . send_reply ( ret ) ;
171
168
} else if ( type === 45 ) { // -
172
169
ret = this . _parseResult ( type ) ;
173
170
174
- if ( ret === null ) {
175
- break ;
176
- }
177
- this . send_error ( new Error ( ret ) ) ;
171
+ this . send_error ( ret ) ;
178
172
} else if ( type === 58 ) { // :
179
173
ret = this . _parseResult ( type ) ;
180
174
181
- if ( ret === null ) {
182
- break ;
183
- }
184
-
185
175
this . send_reply ( ret ) ;
186
176
} else if ( type === 36 ) { // $
187
177
ret = this . _parseResult ( type ) ;
@@ -219,9 +209,6 @@ ReplyParser.prototype.execute = function (buffer) {
219
209
} ;
220
210
221
211
ReplyParser . prototype . append = function ( newBuffer ) {
222
- if ( ! newBuffer ) {
223
- return ;
224
- }
225
212
226
213
// first run
227
214
if ( this . _buffer === null ) {
@@ -238,27 +225,13 @@ ReplyParser.prototype.append = function (newBuffer) {
238
225
return ;
239
226
}
240
227
241
- // very large packet
242
- // check for concat, if we have it, use it
243
- if ( Buffer . concat !== undefined ) {
244
- this . _buffer = Buffer . concat ( [ this . _buffer . slice ( this . _offset ) , newBuffer ] ) ;
245
- } else {
246
- var remaining = this . _bytesRemaining ( ) ,
247
- newLength = remaining + newBuffer . length ,
248
- tmpBuffer = new Buffer ( newLength ) ;
249
-
250
- this . _buffer . copy ( tmpBuffer , 0 , this . _offset ) ;
251
- newBuffer . copy ( tmpBuffer , remaining , 0 ) ;
252
-
253
- this . _buffer = tmpBuffer ;
254
- }
255
-
228
+ this . _buffer = Buffer . concat ( [ this . _buffer . slice ( this . _offset ) , newBuffer ] ) ;
256
229
this . _offset = 0 ;
257
230
} ;
258
231
259
232
ReplyParser . prototype . parseHeader = function ( ) {
260
233
var end = this . _packetEndOffset ( ) ,
261
- value = small_toString ( this . _buffer , this . _offset , end - 1 ) ;
234
+ value = this . _buffer . toString ( 'ascii' , this . _offset , end - 1 ) ;
262
235
263
236
this . _offset = end + 1 ;
264
237
@@ -270,10 +243,6 @@ ReplyParser.prototype._packetEndOffset = function () {
270
243
271
244
while ( this . _buffer [ offset ] !== 0x0d && this . _buffer [ offset + 1 ] !== 0x0a ) {
272
245
offset ++ ;
273
-
274
- if ( offset >= this . _buffer . length ) {
275
- throw new IncompleteReadBuffer ( "didn't see LF after NL reading multi bulk count (" + offset + " => " + this . _buffer . length + ", " + this . _offset + ")" ) ;
276
- }
277
246
}
278
247
279
248
offset ++ ;
0 commit comments