@@ -209,13 +209,36 @@ function parseArray (parser) {
209
209
return parseArrayElements ( parser , responses , 0 )
210
210
}
211
211
212
+ /**
213
+ * Push a partly parsed array to the stack
214
+ *
215
+ * @param parser
216
+ * @param elem
217
+ * @param i
218
+ * @returns {undefined }
219
+ */
220
+ function pushArrayCache ( parser , elem , pos ) {
221
+ parser . arrayCache . push ( elem )
222
+ parser . arrayPos . push ( pos )
223
+ }
224
+
212
225
/**
213
226
* Parse chunked redis array response
214
227
* @param parser
215
228
* @returns {* }
216
229
*/
217
230
function parseArrayChunks ( parser ) {
218
- return parseArrayElements ( parser , parser . arrayCache , parser . arrayPos )
231
+ var tmp = parser . arrayCache . pop ( )
232
+ var pos = parser . arrayPos . pop ( )
233
+ if ( parser . arrayCache . length ) {
234
+ var res = parseArrayChunks ( parser )
235
+ if ( ! res ) {
236
+ pushArrayCache ( parser , tmp , pos )
237
+ return
238
+ }
239
+ tmp [ pos ++ ] = res
240
+ }
241
+ return parseArrayElements ( parser , tmp , pos )
219
242
}
220
243
221
244
/**
@@ -230,16 +253,15 @@ function parseArrayElements (parser, responses, i) {
230
253
while ( i < responses . length ) {
231
254
var offset = parser . offset
232
255
if ( parser . offset >= bufferLength ) {
233
- parser . arrayCache = responses
234
- parser . arrayPos = i
235
- parser . offset = offset
256
+ pushArrayCache ( parser , responses , i )
236
257
return
237
258
}
238
259
var response = parseType ( parser , parser . buffer [ parser . offset ++ ] )
239
260
if ( response === undefined ) {
240
- parser . arrayCache = responses
241
- parser . arrayPos = i
242
- parser . offset = offset
261
+ if ( ! parser . arrayCache . length ) {
262
+ parser . offset = offset
263
+ }
264
+ pushArrayCache ( parser , responses , i )
243
265
return
244
266
}
245
267
responses [ i ] = response
@@ -324,8 +346,8 @@ function JavascriptRedisParser (options) {
324
346
this . bigOffset = 0
325
347
this . totalChunkSize = 0
326
348
this . bufferCache = [ ]
327
- this . arrayCache = null
328
- this . arrayPos = 0
349
+ this . arrayCache = [ ]
350
+ this . arrayPos = [ ]
329
351
}
330
352
331
353
/**
@@ -439,31 +461,29 @@ JavascriptRedisParser.prototype.execute = function execute (buffer) {
439
461
buffer . copy ( newBuffer , remainingLength , 0 , buffer . length )
440
462
this . buffer = newBuffer
441
463
this . offset = 0
442
- if ( this . arrayCache ) {
464
+ if ( this . arrayCache . length ) {
443
465
arr = parseArrayChunks ( this )
444
466
if ( ! arr ) {
445
467
return
446
468
}
447
469
this . returnReply ( arr )
448
- this . arrayCache = null
449
470
}
450
471
} else if ( this . totalChunkSize + buffer . length >= this . bigStrSize ) {
451
472
this . bufferCache . push ( buffer )
452
- if ( this . optionReturnBuffers === false && ! this . arrayCache ) {
473
+ if ( this . optionReturnBuffers === false && ! this . arrayCache . length ) {
453
474
this . returnReply ( concatBulkString ( this ) )
454
475
this . buffer = buffer
455
476
} else {
456
477
this . buffer = concatBuffer ( this , this . totalChunkSize + buffer . length )
457
478
this . offset = 0
458
- if ( this . arrayCache ) {
479
+ if ( this . arrayCache . length ) {
459
480
arr = parseArrayChunks ( this )
460
481
if ( ! arr ) {
461
482
this . bigStrSize = 0
462
483
this . bufferCache = [ ]
463
484
return
464
485
}
465
486
this . returnReply ( arr )
466
- this . arrayCache = null
467
487
}
468
488
}
469
489
this . bigStrSize = 0
@@ -479,7 +499,7 @@ JavascriptRedisParser.prototype.execute = function execute (buffer) {
479
499
var type = this . buffer [ this . offset ++ ]
480
500
var response = parseType ( this , type )
481
501
if ( response === undefined ) {
482
- if ( ! this . arrayCache ) {
502
+ if ( ! this . arrayCache . length ) {
483
503
this . offset = offset
484
504
}
485
505
return
0 commit comments