@@ -13,9 +13,9 @@ var counter = 0
13
13
var notDecreased = 0
14
14
15
15
/**
16
- * Used for lengths and numbers only, faster perf on arrays / bulks
17
- * @param parser
18
- * @returns {* }
16
+ * Used for integer numbers only
17
+ * @param { JavascriptRedisParser } parser
18
+ * @returns {undefined|number }
19
19
*/
20
20
function parseSimpleNumbers ( parser ) {
21
21
const length = parser . buffer . length - 1
@@ -41,11 +41,11 @@ function parseSimpleNumbers (parser) {
41
41
/**
42
42
* Used for integer numbers in case of the returnNumbers option
43
43
*
44
- * The maximum possible integer to use is: Math.floor(Number.MAX_SAFE_INTEGER / 10)
45
- * Staying in a SMI Math.floor((Math.pow(2, 32) / 10) - 1) is even more efficient though
44
+ * Reading the string as parts of n SMI is more efficient than
45
+ * using a string directly.
46
46
*
47
- * @param parser
48
- * @returns {* }
47
+ * @param { JavascriptRedisParser } parser
48
+ * @returns {undefined|string }
49
49
*/
50
50
function parseStringNumbers ( parser ) {
51
51
const length = parser . buffer . length - 1
@@ -80,8 +80,8 @@ function parseStringNumbers (parser) {
80
80
/**
81
81
* Parse a '+' redis simple string response but forward the offsets
82
82
* onto convertBufferRange to generate a string.
83
- * @param parser
84
- * @returns {* }
83
+ * @param { JavascriptRedisParser } parser
84
+ * @returns {undefined|string|Buffer }
85
85
*/
86
86
function parseSimpleString ( parser ) {
87
87
const start = parser . offset
@@ -102,8 +102,8 @@ function parseSimpleString (parser) {
102
102
103
103
/**
104
104
* Returns the read length
105
- * @param parser
106
- * @returns {* }
105
+ * @param { JavascriptRedisParser } parser
106
+ * @returns {undefined|number }
107
107
*/
108
108
function parseLength ( parser ) {
109
109
const length = parser . buffer . length - 1
@@ -127,8 +127,8 @@ function parseLength (parser) {
127
127
* This is important for big numbers (number > Math.pow(2, 53)) as js numbers
128
128
* are 64bit floating point numbers with reduced precision
129
129
*
130
- * @param parser
131
- * @returns {* }
130
+ * @param { JavascriptRedisParser } parser
131
+ * @returns {undefined|number|string }
132
132
*/
133
133
function parseInteger ( parser ) {
134
134
if ( parser . optionStringNumbers === true ) {
@@ -139,8 +139,8 @@ function parseInteger (parser) {
139
139
140
140
/**
141
141
* Parse a '$' redis bulk string response
142
- * @param parser
143
- * @returns {* }
142
+ * @param { JavascriptRedisParser } parser
143
+ * @returns {undefined|null|string }
144
144
*/
145
145
function parseBulkString ( parser ) {
146
146
const length = parseLength ( parser )
@@ -150,25 +150,25 @@ function parseBulkString (parser) {
150
150
if ( length < 0 ) {
151
151
return null
152
152
}
153
- const offsetEnd = parser . offset + length
154
- if ( offsetEnd + 2 > parser . buffer . length ) {
155
- parser . bigStrSize = offsetEnd + 2
153
+ const offset = parser . offset + length
154
+ if ( offset + 2 > parser . buffer . length ) {
155
+ parser . bigStrSize = offset + 2
156
156
parser . totalChunkSize = parser . buffer . length
157
157
parser . bufferCache . push ( parser . buffer )
158
158
return
159
159
}
160
160
const start = parser . offset
161
- parser . offset = offsetEnd + 2
161
+ parser . offset = offset + 2
162
162
if ( parser . optionReturnBuffers === true ) {
163
- return parser . buffer . slice ( start , offsetEnd )
163
+ return parser . buffer . slice ( start , offset )
164
164
}
165
- return parser . buffer . toString ( 'utf8' , start , offsetEnd )
165
+ return parser . buffer . toString ( 'utf8' , start , offset )
166
166
}
167
167
168
168
/**
169
169
* Parse a '-' redis error response
170
- * @param parser
171
- * @returns {Error }
170
+ * @param { JavascriptRedisParser } parser
171
+ * @returns {ReplyError }
172
172
*/
173
173
function parseError ( parser ) {
174
174
var string = parseSimpleString ( parser )
@@ -182,8 +182,9 @@ function parseError (parser) {
182
182
183
183
/**
184
184
* Parsing error handler, resets parser buffer
185
- * @param parser
186
- * @param error
185
+ * @param {JavascriptRedisParser } parser
186
+ * @param {number } type
187
+ * @returns {undefined }
187
188
*/
188
189
function handleError ( parser , error ) {
189
190
parser . buffer = null
@@ -192,8 +193,8 @@ function handleError (parser, error) {
192
193
193
194
/**
194
195
* Parse a '*' redis array response
195
- * @param parser
196
- * @returns {* }
196
+ * @param { JavascriptRedisParser } parser
197
+ * @returns {undefined|null|any[] }
197
198
*/
198
199
function parseArray ( parser ) {
199
200
const length = parseLength ( parser )
@@ -210,20 +211,20 @@ function parseArray (parser) {
210
211
/**
211
212
* Push a partly parsed array to the stack
212
213
*
213
- * @param parser
214
- * @param elem
215
- * @param i
214
+ * @param { JavascriptRedisParser } parser
215
+ * @param { any[] } array
216
+ * @param { number } pos
216
217
* @returns {undefined }
217
218
*/
218
- function pushArrayCache ( parser , elem , pos ) {
219
- parser . arrayCache . push ( elem )
219
+ function pushArrayCache ( parser , array , pos ) {
220
+ parser . arrayCache . push ( array )
220
221
parser . arrayPos . push ( pos )
221
222
}
222
223
223
224
/**
224
225
* Parse chunked redis array response
225
- * @param parser
226
- * @returns {* }
226
+ * @param { JavascriptRedisParser } parser
227
+ * @returns {undefined|any[] }
227
228
*/
228
229
function parseArrayChunks ( parser ) {
229
230
const tmp = parser . arrayCache . pop ( )
@@ -241,10 +242,10 @@ function parseArrayChunks (parser) {
241
242
242
243
/**
243
244
* Parse redis array response elements
244
- * @param parser
245
- * @param responses
246
- * @param i
247
- * @returns {* }
245
+ * @param { JavascriptRedisParser } parser
246
+ * @param { Array } responses
247
+ * @param { number } i
248
+ * @returns {undefined|null|any[] }
248
249
*/
249
250
function parseArrayElements ( parser , responses , i ) {
250
251
const bufferLength = parser . buffer . length
@@ -256,7 +257,7 @@ function parseArrayElements (parser, responses, i) {
256
257
}
257
258
const response = parseType ( parser , parser . buffer [ parser . offset ++ ] )
258
259
if ( response === undefined ) {
259
- if ( ! parser . arrayCache . length && ! parser . bufferCache . length ) {
260
+ if ( ! ( parser . arrayCache . length || parser . bufferCache . length ) ) {
260
261
parser . offset = offset
261
262
}
262
263
pushArrayCache ( parser , responses , i )
@@ -278,8 +279,8 @@ function parseArrayElements (parser, responses, i) {
278
279
* 58: :
279
280
* 45: -
280
281
*
281
- * @param parser
282
- * @param type
282
+ * @param { JavascriptRedisParser } parser
283
+ * @param { number } type
283
284
* @returns {* }
284
285
*/
285
286
function parseType ( parser , type ) {
@@ -335,7 +336,7 @@ function decreaseBufferPool () {
335
336
* Check if the requested size fits in the current bufferPool.
336
337
* If it does not, reset and increase the bufferPool accordingly.
337
338
*
338
- * @param length
339
+ * @param { number } length
339
340
* @returns {undefined }
340
341
*/
341
342
function resizeBuffer ( length ) {
@@ -360,7 +361,7 @@ function resizeBuffer (length) {
360
361
* 1) The first chunk might contain the whole bulk string including the \r
361
362
* 2) We are only safe to fully add up elements that are neither the first nor any of the last two elements
362
363
*
363
- * @param parser
364
+ * @param { JavascriptRedisParser } parser
364
365
* @returns {String }
365
366
*/
366
367
function concatBulkString ( parser ) {
@@ -389,7 +390,7 @@ function concatBulkString (parser) {
389
390
*
390
391
* Increases the bufferPool size beforehand if necessary.
391
392
*
392
- * @param parser
393
+ * @param { JavascriptRedisParser } parser
393
394
* @returns {Buffer }
394
395
*/
395
396
function concatBulkBuffer ( parser ) {
@@ -419,12 +420,12 @@ function concatBulkBuffer (parser) {
419
420
return bufferPool . slice ( start , bufferOffset )
420
421
}
421
422
422
- /**
423
- * Javascript Redis Parser
424
- * @param options
425
- * @constructor
426
- */
427
423
class JavascriptRedisParser {
424
+ /**
425
+ * Javascript Redis Parser constructor
426
+ * @param {{returnError: Function, returnReply: Function, returnFatalError?: Function, returnBuffers: boolean, stringNumbers: boolean } } options
427
+ * @constructor
428
+ */
428
429
constructor ( options ) {
429
430
if ( ! options ) {
430
431
throw new TypeError ( 'Options are mandatory.' )
@@ -458,7 +459,7 @@ class JavascriptRedisParser {
458
459
/**
459
460
* Set the returnBuffers option
460
461
*
461
- * @param returnBuffers
462
+ * @param { boolean } returnBuffers
462
463
* @returns {undefined }
463
464
*/
464
465
setReturnBuffers ( returnBuffers ) {
@@ -471,7 +472,7 @@ class JavascriptRedisParser {
471
472
/**
472
473
* Set the stringNumbers option
473
474
*
474
- * @param stringNumbers
475
+ * @param { boolean } stringNumbers
475
476
* @returns {undefined }
476
477
*/
477
478
setStringNumbers ( stringNumbers ) {
@@ -483,7 +484,7 @@ class JavascriptRedisParser {
483
484
484
485
/**
485
486
* Parse the redis buffer
486
- * @param buffer
487
+ * @param { Buffer } buffer
487
488
* @returns {undefined }
488
489
*/
489
490
execute ( buffer ) {
@@ -530,7 +531,7 @@ class JavascriptRedisParser {
530
531
const type = this . buffer [ this . offset ++ ]
531
532
const response = parseType ( this , type )
532
533
if ( response === undefined ) {
533
- if ( ! this . arrayCache . length && ! this . bufferCache . length ) {
534
+ if ( ! ( this . arrayCache . length || this . bufferCache . length ) ) {
534
535
this . offset = offset
535
536
}
536
537
return
0 commit comments