@@ -63,13 +63,15 @@ function parseStringNumbers (parser) {
63
63
/**
64
64
* Returns a string or buffer of the provided offset start and
65
65
* end ranges. Checks `optionReturnBuffers`.
66
+ *
67
+ * If returnBuffers is active, all return values are returned as buffers besides numbers and errors
68
+ *
66
69
* @param parser
67
70
* @param start
68
71
* @param end
69
72
* @returns {* }
70
73
*/
71
74
function convertBufferRange ( parser , start , end ) {
72
- // If returnBuffers is active, all return values are returned as buffers besides numbers and errors
73
75
parser . offset = end + 2
74
76
if ( parser . optionReturnBuffers === true ) {
75
77
return parser . buffer . slice ( start , end )
@@ -111,13 +113,15 @@ function parseLength (parser) {
111
113
112
114
/**
113
115
* Parse a ':' redis integer response
116
+ *
117
+ * If stringNumbers is activated the parser always returns numbers as string
118
+ * This is important for big numbers (number > Math.pow(2, 53)) as js numbers
119
+ * are 64bit floating point numbers with reduced precision
120
+ *
114
121
* @param parser
115
122
* @returns {* }
116
123
*/
117
124
function parseInteger ( parser ) {
118
- // If stringNumbers is activated the parser always returns numbers as string
119
- // This is important for big numbers (number > Math.pow(2, 53)) as js numbers
120
- // are 64bit floating point numbers with reduced precision
121
125
if ( parser . optionStringNumbers ) {
122
126
return parseStringNumbers ( parser )
123
127
}
@@ -223,7 +227,10 @@ function parseType (parser, type) {
223
227
case 45 : // -
224
228
return parseError ( parser )
225
229
default :
226
- return handleError ( parser , new ReplyError ( 'Protocol error, got ' + JSON . stringify ( String . fromCharCode ( type ) ) + ' as reply type byte' ) )
230
+ const err = new ReplyError ( 'Protocol error, got ' + JSON . stringify ( String . fromCharCode ( type ) ) + ' as reply type byte' , 20 )
231
+ err . offset = parser . offset
232
+ err . buffer = JSON . stringify ( parser . buffer )
233
+ return handleError ( parser , err )
227
234
}
228
235
}
229
236
@@ -250,7 +257,7 @@ function JavascriptRedisParser (options) {
250
257
throw new TypeError ( 'Please provide all return functions while initiating the parser' )
251
258
}
252
259
for ( var key in options ) {
253
- if ( typeof options [ key ] !== optionTypes [ key ] ) {
260
+ if ( optionTypes . hasOwnProperty ( key ) && typeof options [ key ] !== optionTypes [ key ] ) {
254
261
throw new TypeError ( 'The options argument contains the property "' + key + '" that is either unkown or of a wrong type' )
255
262
}
256
263
}
@@ -280,13 +287,17 @@ function JavascriptRedisParser (options) {
280
287
281
288
/**
282
289
* Concat a bulk string containing multiple chunks
290
+ *
291
+ * Notes:
292
+ * 1) The first chunk might contain the whole bulk string including the \r
293
+ * 2) We are only safe to fully add up elements that are neither the first nor any of the last two elements
294
+ *
283
295
* @param parser
284
296
* @param buffer
285
297
* @returns {String }
286
298
*/
287
299
function concatBulkString ( parser ) {
288
300
var list = parser . bufferCache
289
- // The first chunk might contain the whole bulk string including the \r
290
301
var chunks = list . length
291
302
var offset = parser . bigStrSize - parser . totalChunkSize
292
303
parser . offset = offset
@@ -299,7 +310,6 @@ function concatBulkString (parser) {
299
310
}
300
311
var res = decoder . write ( list [ 0 ] . slice ( parser . bigOffset ) )
301
312
for ( var i = 1 ; i < chunks - 2 ; i ++ ) {
302
- // We are only safe to fully add up elements that are neither the first nor any of the last two elements
303
313
res += decoder . write ( list [ i ] )
304
314
}
305
315
res += decoder . end ( list [ i ] . slice ( 0 , offset === 1 ? list [ i ] . length - 1 : offset - 2 ) )
@@ -314,8 +324,14 @@ function decreaseBufferPool () {
314
324
if ( bufferPool . length > 50 * 1024 ) {
315
325
// Balance between increasing and decreasing the bufferPool
316
326
if ( counter === 1 || notDecreased > counter * 2 ) {
317
- // Decrease the bufferPool by 16kb by removing the first 16kb of the current pool
318
- bufferPool = bufferPool . slice ( Math . floor ( bufferPool . length / 10 ) , bufferPool . length )
327
+ // Decrease the bufferPool by 10% by removing the first 10% of the current pool
328
+ var sliceLength = Math . floor ( bufferPool . length / 10 )
329
+ if ( bufferOffset <= sliceLength ) {
330
+ bufferOffset = 0
331
+ } else {
332
+ bufferOffset -= sliceLength
333
+ }
334
+ bufferPool = bufferPool . slice ( sliceLength , bufferPool . length )
319
335
} else {
320
336
notDecreased ++
321
337
counter --
@@ -339,18 +355,17 @@ function concatBuffer (parser, length) {
339
355
var pos = bufferOffset
340
356
length -= parser . offset
341
357
if ( bufferPool . length < length + bufferOffset ) {
342
- // Increase the bufferPool size by three times the current needed length
343
- var multiplier = 3
344
- if ( bufferOffset > 1024 * 1024 * 200 ) {
358
+ // Increase the bufferPool size
359
+ var multiplier = length > 1024 * 1024 * 75 ? 2 : 3
360
+ if ( bufferOffset > 1024 * 1024 * 120 ) {
345
361
bufferOffset = 1024 * 1024 * 50
346
- multiplier = 2
347
362
}
348
363
bufferPool = new Buffer ( length * multiplier + bufferOffset )
349
364
bufferOffset = 0
350
365
counter ++
351
366
pos = 0
352
367
if ( interval === null ) {
353
- interval = setInterval ( decreaseBufferPool , 50 )
368
+ interval = setInterval ( decreaseBufferPool , 50 ) . unref ( )
354
369
}
355
370
}
356
371
list [ 0 ] . copy ( bufferPool , pos , parser . offset , list [ 0 ] . length )
@@ -369,7 +384,7 @@ function concatBuffer (parser, length) {
369
384
* @param buffer
370
385
* @returns {undefined }
371
386
*/
372
- JavascriptRedisParser . prototype . execute = function ( buffer ) {
387
+ JavascriptRedisParser . prototype . execute = function execute ( buffer ) {
373
388
if ( this . buffer === null ) {
374
389
this . buffer = buffer
375
390
this . offset = 0
@@ -410,9 +425,9 @@ JavascriptRedisParser.prototype.execute = function (buffer) {
410
425
}
411
426
412
427
if ( type === 45 ) {
413
- this . returnError ( response ) // Errors -
428
+ this . returnError ( response )
414
429
} else {
415
- this . returnReply ( response ) // Strings + // Integers : // Bulk strings $ // Arrays *
430
+ this . returnReply ( response )
416
431
}
417
432
}
418
433
0 commit comments