@@ -13,9 +13,9 @@ var counter = 0
1313var notDecreased = 0
1414
1515/**
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 }
1919 */
2020function parseSimpleNumbers ( parser ) {
2121 const length = parser . buffer . length - 1
@@ -41,11 +41,11 @@ function parseSimpleNumbers (parser) {
4141/**
4242 * Used for integer numbers in case of the returnNumbers option
4343 *
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.
4646 *
47- * @param parser
48- * @returns {* }
47+ * @param { JavascriptRedisParser } parser
48+ * @returns {undefined|string }
4949 */
5050function parseStringNumbers ( parser ) {
5151 const length = parser . buffer . length - 1
@@ -80,8 +80,8 @@ function parseStringNumbers (parser) {
8080/**
8181 * Parse a '+' redis simple string response but forward the offsets
8282 * onto convertBufferRange to generate a string.
83- * @param parser
84- * @returns {* }
83+ * @param { JavascriptRedisParser } parser
84+ * @returns {undefined|string|Buffer }
8585 */
8686function parseSimpleString ( parser ) {
8787 const start = parser . offset
@@ -102,8 +102,8 @@ function parseSimpleString (parser) {
102102
103103/**
104104 * Returns the read length
105- * @param parser
106- * @returns {* }
105+ * @param { JavascriptRedisParser } parser
106+ * @returns {undefined|number }
107107 */
108108function parseLength ( parser ) {
109109 const length = parser . buffer . length - 1
@@ -127,8 +127,8 @@ function parseLength (parser) {
127127 * This is important for big numbers (number > Math.pow(2, 53)) as js numbers
128128 * are 64bit floating point numbers with reduced precision
129129 *
130- * @param parser
131- * @returns {* }
130+ * @param { JavascriptRedisParser } parser
131+ * @returns {undefined|number|string }
132132 */
133133function parseInteger ( parser ) {
134134 if ( parser . optionStringNumbers === true ) {
@@ -139,8 +139,8 @@ function parseInteger (parser) {
139139
140140/**
141141 * Parse a '$' redis bulk string response
142- * @param parser
143- * @returns {* }
142+ * @param { JavascriptRedisParser } parser
143+ * @returns {undefined|null|string }
144144 */
145145function parseBulkString ( parser ) {
146146 const length = parseLength ( parser )
@@ -150,25 +150,25 @@ function parseBulkString (parser) {
150150 if ( length < 0 ) {
151151 return null
152152 }
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
156156 parser . totalChunkSize = parser . buffer . length
157157 parser . bufferCache . push ( parser . buffer )
158158 return
159159 }
160160 const start = parser . offset
161- parser . offset = offsetEnd + 2
161+ parser . offset = offset + 2
162162 if ( parser . optionReturnBuffers === true ) {
163- return parser . buffer . slice ( start , offsetEnd )
163+ return parser . buffer . slice ( start , offset )
164164 }
165- return parser . buffer . toString ( 'utf8' , start , offsetEnd )
165+ return parser . buffer . toString ( 'utf8' , start , offset )
166166}
167167
168168/**
169169 * Parse a '-' redis error response
170- * @param parser
171- * @returns {Error }
170+ * @param { JavascriptRedisParser } parser
171+ * @returns {ReplyError }
172172 */
173173function parseError ( parser ) {
174174 var string = parseSimpleString ( parser )
@@ -182,8 +182,9 @@ function parseError (parser) {
182182
183183/**
184184 * Parsing error handler, resets parser buffer
185- * @param parser
186- * @param error
185+ * @param {JavascriptRedisParser } parser
186+ * @param {number } type
187+ * @returns {undefined }
187188 */
188189function handleError ( parser , error ) {
189190 parser . buffer = null
@@ -192,8 +193,8 @@ function handleError (parser, error) {
192193
193194/**
194195 * Parse a '*' redis array response
195- * @param parser
196- * @returns {* }
196+ * @param { JavascriptRedisParser } parser
197+ * @returns {undefined|null|any[] }
197198 */
198199function parseArray ( parser ) {
199200 const length = parseLength ( parser )
@@ -210,20 +211,20 @@ function parseArray (parser) {
210211/**
211212 * Push a partly parsed array to the stack
212213 *
213- * @param parser
214- * @param elem
215- * @param i
214+ * @param { JavascriptRedisParser } parser
215+ * @param { any[] } array
216+ * @param { number } pos
216217 * @returns {undefined }
217218 */
218- function pushArrayCache ( parser , elem , pos ) {
219- parser . arrayCache . push ( elem )
219+ function pushArrayCache ( parser , array , pos ) {
220+ parser . arrayCache . push ( array )
220221 parser . arrayPos . push ( pos )
221222}
222223
223224/**
224225 * Parse chunked redis array response
225- * @param parser
226- * @returns {* }
226+ * @param { JavascriptRedisParser } parser
227+ * @returns {undefined|any[] }
227228 */
228229function parseArrayChunks ( parser ) {
229230 const tmp = parser . arrayCache . pop ( )
@@ -241,10 +242,10 @@ function parseArrayChunks (parser) {
241242
242243/**
243244 * 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[] }
248249 */
249250function parseArrayElements ( parser , responses , i ) {
250251 const bufferLength = parser . buffer . length
@@ -256,7 +257,7 @@ function parseArrayElements (parser, responses, i) {
256257 }
257258 const response = parseType ( parser , parser . buffer [ parser . offset ++ ] )
258259 if ( response === undefined ) {
259- if ( ! parser . arrayCache . length && ! parser . bufferCache . length ) {
260+ if ( ! ( parser . arrayCache . length || parser . bufferCache . length ) ) {
260261 parser . offset = offset
261262 }
262263 pushArrayCache ( parser , responses , i )
@@ -278,8 +279,8 @@ function parseArrayElements (parser, responses, i) {
278279 * 58: :
279280 * 45: -
280281 *
281- * @param parser
282- * @param type
282+ * @param { JavascriptRedisParser } parser
283+ * @param { number } type
283284 * @returns {* }
284285 */
285286function parseType ( parser , type ) {
@@ -335,7 +336,7 @@ function decreaseBufferPool () {
335336 * Check if the requested size fits in the current bufferPool.
336337 * If it does not, reset and increase the bufferPool accordingly.
337338 *
338- * @param length
339+ * @param { number } length
339340 * @returns {undefined }
340341 */
341342function resizeBuffer ( length ) {
@@ -360,7 +361,7 @@ function resizeBuffer (length) {
360361 * 1) The first chunk might contain the whole bulk string including the \r
361362 * 2) We are only safe to fully add up elements that are neither the first nor any of the last two elements
362363 *
363- * @param parser
364+ * @param { JavascriptRedisParser } parser
364365 * @returns {String }
365366 */
366367function concatBulkString ( parser ) {
@@ -389,7 +390,7 @@ function concatBulkString (parser) {
389390 *
390391 * Increases the bufferPool size beforehand if necessary.
391392 *
392- * @param parser
393+ * @param { JavascriptRedisParser } parser
393394 * @returns {Buffer }
394395 */
395396function concatBulkBuffer ( parser ) {
@@ -419,12 +420,12 @@ function concatBulkBuffer (parser) {
419420 return bufferPool . slice ( start , bufferOffset )
420421}
421422
422- /**
423- * Javascript Redis Parser
424- * @param options
425- * @constructor
426- */
427423class JavascriptRedisParser {
424+ /**
425+ * Javascript Redis Parser constructor
426+ * @param {{returnError: Function, returnReply: Function, returnFatalError?: Function, returnBuffers: boolean, stringNumbers: boolean } } options
427+ * @constructor
428+ */
428429 constructor ( options ) {
429430 if ( ! options ) {
430431 throw new TypeError ( 'Options are mandatory.' )
@@ -458,7 +459,7 @@ class JavascriptRedisParser {
458459 /**
459460 * Set the returnBuffers option
460461 *
461- * @param returnBuffers
462+ * @param { boolean } returnBuffers
462463 * @returns {undefined }
463464 */
464465 setReturnBuffers ( returnBuffers ) {
@@ -471,7 +472,7 @@ class JavascriptRedisParser {
471472 /**
472473 * Set the stringNumbers option
473474 *
474- * @param stringNumbers
475+ * @param { boolean } stringNumbers
475476 * @returns {undefined }
476477 */
477478 setStringNumbers ( stringNumbers ) {
@@ -483,7 +484,7 @@ class JavascriptRedisParser {
483484
484485 /**
485486 * Parse the redis buffer
486- * @param buffer
487+ * @param { Buffer } buffer
487488 * @returns {undefined }
488489 */
489490 execute ( buffer ) {
@@ -530,7 +531,7 @@ class JavascriptRedisParser {
530531 const type = this . buffer [ this . offset ++ ]
531532 const response = parseType ( this , type )
532533 if ( response === undefined ) {
533- if ( ! this . arrayCache . length && ! this . bufferCache . length ) {
534+ if ( ! ( this . arrayCache . length || this . bufferCache . length ) ) {
534535 this . offset = offset
535536 }
536537 return
0 commit comments