redis 3.0.0 → 3.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.deepsource.toml +9 -0
- package/CHANGELOG.md +1 -0
- package/LICENSE +1 -1
- package/README.md +25 -21
- package/heroku/index.js +14 -0
- package/heroku/node_modules/.package-lock.json +57 -0
- package/heroku/node_modules/denque/CHANGELOG.md +4 -0
- package/heroku/node_modules/denque/LICENSE +13 -0
- package/heroku/node_modules/denque/README.md +362 -0
- package/heroku/node_modules/denque/index.d.ts +31 -0
- package/heroku/node_modules/denque/index.js +443 -0
- package/heroku/node_modules/denque/package.json +55 -0
- package/heroku/node_modules/redis/.deepsource.toml +9 -0
- package/heroku/node_modules/redis/CHANGELOG.md +880 -0
- package/heroku/node_modules/redis/LICENSE +24 -0
- package/heroku/node_modules/redis/README.md +1009 -0
- package/heroku/node_modules/redis/a.js +12 -0
- package/heroku/node_modules/redis/index.js +1039 -0
- package/heroku/node_modules/redis/lib/command.js +16 -0
- package/heroku/node_modules/redis/lib/commands.js +105 -0
- package/heroku/node_modules/redis/lib/createClient.js +88 -0
- package/heroku/node_modules/redis/lib/customErrors.js +58 -0
- package/heroku/node_modules/redis/lib/debug.js +13 -0
- package/heroku/node_modules/redis/lib/extendedApi.js +113 -0
- package/heroku/node_modules/redis/lib/individualCommands.js +629 -0
- package/heroku/node_modules/redis/lib/multi.js +187 -0
- package/heroku/node_modules/redis/lib/utils.js +134 -0
- package/heroku/node_modules/redis/npm +0 -0
- package/heroku/node_modules/redis/package.json +77 -0
- package/heroku/node_modules/redis-commands/LICENSE +22 -0
- package/heroku/node_modules/redis-commands/README.md +51 -0
- package/heroku/node_modules/redis-commands/changelog.md +83 -0
- package/heroku/node_modules/redis-commands/commands.json +2334 -0
- package/heroku/node_modules/redis-commands/index.js +168 -0
- package/heroku/node_modules/redis-commands/package.json +41 -0
- package/heroku/node_modules/redis-commands/tools/build.js +62 -0
- package/heroku/node_modules/redis-errors/LICENSE +22 -0
- package/heroku/node_modules/redis-errors/README.md +116 -0
- package/heroku/node_modules/redis-errors/index.js +7 -0
- package/heroku/node_modules/redis-errors/lib/modern.js +59 -0
- package/heroku/node_modules/redis-errors/lib/old.js +119 -0
- package/heroku/node_modules/redis-errors/package.json +41 -0
- package/heroku/node_modules/redis-parser/LICENSE +22 -0
- package/heroku/node_modules/redis-parser/README.md +166 -0
- package/heroku/node_modules/redis-parser/changelog.md +156 -0
- package/heroku/node_modules/redis-parser/index.js +3 -0
- package/heroku/node_modules/redis-parser/lib/parser.js +552 -0
- package/heroku/node_modules/redis-parser/package.json +53 -0
- package/heroku/package.json +9 -0
- package/index.js +8 -19
- package/lib/createClient.js +5 -1
- package/lib/individualCommands.js +19 -7
- package/lib/utils.js +1 -1
- package/package.json +15 -13
|
@@ -0,0 +1,552 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const Buffer = require('buffer').Buffer
|
|
4
|
+
const StringDecoder = require('string_decoder').StringDecoder
|
|
5
|
+
const decoder = new StringDecoder()
|
|
6
|
+
const errors = require('redis-errors')
|
|
7
|
+
const ReplyError = errors.ReplyError
|
|
8
|
+
const ParserError = errors.ParserError
|
|
9
|
+
var bufferPool = Buffer.allocUnsafe(32 * 1024)
|
|
10
|
+
var bufferOffset = 0
|
|
11
|
+
var interval = null
|
|
12
|
+
var counter = 0
|
|
13
|
+
var notDecreased = 0
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Used for integer numbers only
|
|
17
|
+
* @param {JavascriptRedisParser} parser
|
|
18
|
+
* @returns {undefined|number}
|
|
19
|
+
*/
|
|
20
|
+
function parseSimpleNumbers (parser) {
|
|
21
|
+
const length = parser.buffer.length - 1
|
|
22
|
+
var offset = parser.offset
|
|
23
|
+
var number = 0
|
|
24
|
+
var sign = 1
|
|
25
|
+
|
|
26
|
+
if (parser.buffer[offset] === 45) {
|
|
27
|
+
sign = -1
|
|
28
|
+
offset++
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
while (offset < length) {
|
|
32
|
+
const c1 = parser.buffer[offset++]
|
|
33
|
+
if (c1 === 13) { // \r\n
|
|
34
|
+
parser.offset = offset + 1
|
|
35
|
+
return sign * number
|
|
36
|
+
}
|
|
37
|
+
number = (number * 10) + (c1 - 48)
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Used for integer numbers in case of the returnNumbers option
|
|
43
|
+
*
|
|
44
|
+
* Reading the string as parts of n SMI is more efficient than
|
|
45
|
+
* using a string directly.
|
|
46
|
+
*
|
|
47
|
+
* @param {JavascriptRedisParser} parser
|
|
48
|
+
* @returns {undefined|string}
|
|
49
|
+
*/
|
|
50
|
+
function parseStringNumbers (parser) {
|
|
51
|
+
const length = parser.buffer.length - 1
|
|
52
|
+
var offset = parser.offset
|
|
53
|
+
var number = 0
|
|
54
|
+
var res = ''
|
|
55
|
+
|
|
56
|
+
if (parser.buffer[offset] === 45) {
|
|
57
|
+
res += '-'
|
|
58
|
+
offset++
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
while (offset < length) {
|
|
62
|
+
var c1 = parser.buffer[offset++]
|
|
63
|
+
if (c1 === 13) { // \r\n
|
|
64
|
+
parser.offset = offset + 1
|
|
65
|
+
if (number !== 0) {
|
|
66
|
+
res += number
|
|
67
|
+
}
|
|
68
|
+
return res
|
|
69
|
+
} else if (number > 429496728) {
|
|
70
|
+
res += (number * 10) + (c1 - 48)
|
|
71
|
+
number = 0
|
|
72
|
+
} else if (c1 === 48 && number === 0) {
|
|
73
|
+
res += 0
|
|
74
|
+
} else {
|
|
75
|
+
number = (number * 10) + (c1 - 48)
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Parse a '+' redis simple string response but forward the offsets
|
|
82
|
+
* onto convertBufferRange to generate a string.
|
|
83
|
+
* @param {JavascriptRedisParser} parser
|
|
84
|
+
* @returns {undefined|string|Buffer}
|
|
85
|
+
*/
|
|
86
|
+
function parseSimpleString (parser) {
|
|
87
|
+
const start = parser.offset
|
|
88
|
+
const buffer = parser.buffer
|
|
89
|
+
const length = buffer.length - 1
|
|
90
|
+
var offset = start
|
|
91
|
+
|
|
92
|
+
while (offset < length) {
|
|
93
|
+
if (buffer[offset++] === 13) { // \r\n
|
|
94
|
+
parser.offset = offset + 1
|
|
95
|
+
if (parser.optionReturnBuffers === true) {
|
|
96
|
+
return parser.buffer.slice(start, offset - 1)
|
|
97
|
+
}
|
|
98
|
+
return parser.buffer.toString('utf8', start, offset - 1)
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Returns the read length
|
|
105
|
+
* @param {JavascriptRedisParser} parser
|
|
106
|
+
* @returns {undefined|number}
|
|
107
|
+
*/
|
|
108
|
+
function parseLength (parser) {
|
|
109
|
+
const length = parser.buffer.length - 1
|
|
110
|
+
var offset = parser.offset
|
|
111
|
+
var number = 0
|
|
112
|
+
|
|
113
|
+
while (offset < length) {
|
|
114
|
+
const c1 = parser.buffer[offset++]
|
|
115
|
+
if (c1 === 13) {
|
|
116
|
+
parser.offset = offset + 1
|
|
117
|
+
return number
|
|
118
|
+
}
|
|
119
|
+
number = (number * 10) + (c1 - 48)
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Parse a ':' redis integer response
|
|
125
|
+
*
|
|
126
|
+
* If stringNumbers is activated the parser always returns numbers as string
|
|
127
|
+
* This is important for big numbers (number > Math.pow(2, 53)) as js numbers
|
|
128
|
+
* are 64bit floating point numbers with reduced precision
|
|
129
|
+
*
|
|
130
|
+
* @param {JavascriptRedisParser} parser
|
|
131
|
+
* @returns {undefined|number|string}
|
|
132
|
+
*/
|
|
133
|
+
function parseInteger (parser) {
|
|
134
|
+
if (parser.optionStringNumbers === true) {
|
|
135
|
+
return parseStringNumbers(parser)
|
|
136
|
+
}
|
|
137
|
+
return parseSimpleNumbers(parser)
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* Parse a '$' redis bulk string response
|
|
142
|
+
* @param {JavascriptRedisParser} parser
|
|
143
|
+
* @returns {undefined|null|string}
|
|
144
|
+
*/
|
|
145
|
+
function parseBulkString (parser) {
|
|
146
|
+
const length = parseLength(parser)
|
|
147
|
+
if (length === undefined) {
|
|
148
|
+
return
|
|
149
|
+
}
|
|
150
|
+
if (length < 0) {
|
|
151
|
+
return null
|
|
152
|
+
}
|
|
153
|
+
const offset = parser.offset + length
|
|
154
|
+
if (offset + 2 > parser.buffer.length) {
|
|
155
|
+
parser.bigStrSize = offset + 2
|
|
156
|
+
parser.totalChunkSize = parser.buffer.length
|
|
157
|
+
parser.bufferCache.push(parser.buffer)
|
|
158
|
+
return
|
|
159
|
+
}
|
|
160
|
+
const start = parser.offset
|
|
161
|
+
parser.offset = offset + 2
|
|
162
|
+
if (parser.optionReturnBuffers === true) {
|
|
163
|
+
return parser.buffer.slice(start, offset)
|
|
164
|
+
}
|
|
165
|
+
return parser.buffer.toString('utf8', start, offset)
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* Parse a '-' redis error response
|
|
170
|
+
* @param {JavascriptRedisParser} parser
|
|
171
|
+
* @returns {ReplyError}
|
|
172
|
+
*/
|
|
173
|
+
function parseError (parser) {
|
|
174
|
+
var string = parseSimpleString(parser)
|
|
175
|
+
if (string !== undefined) {
|
|
176
|
+
if (parser.optionReturnBuffers === true) {
|
|
177
|
+
string = string.toString()
|
|
178
|
+
}
|
|
179
|
+
return new ReplyError(string)
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* Parsing error handler, resets parser buffer
|
|
185
|
+
* @param {JavascriptRedisParser} parser
|
|
186
|
+
* @param {number} type
|
|
187
|
+
* @returns {undefined}
|
|
188
|
+
*/
|
|
189
|
+
function handleError (parser, type) {
|
|
190
|
+
const err = new ParserError(
|
|
191
|
+
'Protocol error, got ' + JSON.stringify(String.fromCharCode(type)) + ' as reply type byte',
|
|
192
|
+
JSON.stringify(parser.buffer),
|
|
193
|
+
parser.offset
|
|
194
|
+
)
|
|
195
|
+
parser.buffer = null
|
|
196
|
+
parser.returnFatalError(err)
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/**
|
|
200
|
+
* Parse a '*' redis array response
|
|
201
|
+
* @param {JavascriptRedisParser} parser
|
|
202
|
+
* @returns {undefined|null|any[]}
|
|
203
|
+
*/
|
|
204
|
+
function parseArray (parser) {
|
|
205
|
+
const length = parseLength(parser)
|
|
206
|
+
if (length === undefined) {
|
|
207
|
+
return
|
|
208
|
+
}
|
|
209
|
+
if (length < 0) {
|
|
210
|
+
return null
|
|
211
|
+
}
|
|
212
|
+
const responses = new Array(length)
|
|
213
|
+
return parseArrayElements(parser, responses, 0)
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/**
|
|
217
|
+
* Push a partly parsed array to the stack
|
|
218
|
+
*
|
|
219
|
+
* @param {JavascriptRedisParser} parser
|
|
220
|
+
* @param {any[]} array
|
|
221
|
+
* @param {number} pos
|
|
222
|
+
* @returns {undefined}
|
|
223
|
+
*/
|
|
224
|
+
function pushArrayCache (parser, array, pos) {
|
|
225
|
+
parser.arrayCache.push(array)
|
|
226
|
+
parser.arrayPos.push(pos)
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
/**
|
|
230
|
+
* Parse chunked redis array response
|
|
231
|
+
* @param {JavascriptRedisParser} parser
|
|
232
|
+
* @returns {undefined|any[]}
|
|
233
|
+
*/
|
|
234
|
+
function parseArrayChunks (parser) {
|
|
235
|
+
const tmp = parser.arrayCache.pop()
|
|
236
|
+
var pos = parser.arrayPos.pop()
|
|
237
|
+
if (parser.arrayCache.length) {
|
|
238
|
+
const res = parseArrayChunks(parser)
|
|
239
|
+
if (res === undefined) {
|
|
240
|
+
pushArrayCache(parser, tmp, pos)
|
|
241
|
+
return
|
|
242
|
+
}
|
|
243
|
+
tmp[pos++] = res
|
|
244
|
+
}
|
|
245
|
+
return parseArrayElements(parser, tmp, pos)
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
/**
|
|
249
|
+
* Parse redis array response elements
|
|
250
|
+
* @param {JavascriptRedisParser} parser
|
|
251
|
+
* @param {Array} responses
|
|
252
|
+
* @param {number} i
|
|
253
|
+
* @returns {undefined|null|any[]}
|
|
254
|
+
*/
|
|
255
|
+
function parseArrayElements (parser, responses, i) {
|
|
256
|
+
const bufferLength = parser.buffer.length
|
|
257
|
+
while (i < responses.length) {
|
|
258
|
+
const offset = parser.offset
|
|
259
|
+
if (parser.offset >= bufferLength) {
|
|
260
|
+
pushArrayCache(parser, responses, i)
|
|
261
|
+
return
|
|
262
|
+
}
|
|
263
|
+
const response = parseType(parser, parser.buffer[parser.offset++])
|
|
264
|
+
if (response === undefined) {
|
|
265
|
+
if (!(parser.arrayCache.length || parser.bufferCache.length)) {
|
|
266
|
+
parser.offset = offset
|
|
267
|
+
}
|
|
268
|
+
pushArrayCache(parser, responses, i)
|
|
269
|
+
return
|
|
270
|
+
}
|
|
271
|
+
responses[i] = response
|
|
272
|
+
i++
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
return responses
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
/**
|
|
279
|
+
* Called the appropriate parser for the specified type.
|
|
280
|
+
*
|
|
281
|
+
* 36: $
|
|
282
|
+
* 43: +
|
|
283
|
+
* 42: *
|
|
284
|
+
* 58: :
|
|
285
|
+
* 45: -
|
|
286
|
+
*
|
|
287
|
+
* @param {JavascriptRedisParser} parser
|
|
288
|
+
* @param {number} type
|
|
289
|
+
* @returns {*}
|
|
290
|
+
*/
|
|
291
|
+
function parseType (parser, type) {
|
|
292
|
+
switch (type) {
|
|
293
|
+
case 36:
|
|
294
|
+
return parseBulkString(parser)
|
|
295
|
+
case 43:
|
|
296
|
+
return parseSimpleString(parser)
|
|
297
|
+
case 42:
|
|
298
|
+
return parseArray(parser)
|
|
299
|
+
case 58:
|
|
300
|
+
return parseInteger(parser)
|
|
301
|
+
case 45:
|
|
302
|
+
return parseError(parser)
|
|
303
|
+
default:
|
|
304
|
+
return handleError(parser, type)
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
/**
|
|
309
|
+
* Decrease the bufferPool size over time
|
|
310
|
+
*
|
|
311
|
+
* Balance between increasing and decreasing the bufferPool.
|
|
312
|
+
* Decrease the bufferPool by 10% by removing the first 10% of the current pool.
|
|
313
|
+
* @returns {undefined}
|
|
314
|
+
*/
|
|
315
|
+
function decreaseBufferPool () {
|
|
316
|
+
if (bufferPool.length > 50 * 1024) {
|
|
317
|
+
if (counter === 1 || notDecreased > counter * 2) {
|
|
318
|
+
const minSliceLen = Math.floor(bufferPool.length / 10)
|
|
319
|
+
const sliceLength = minSliceLen < bufferOffset
|
|
320
|
+
? bufferOffset
|
|
321
|
+
: minSliceLen
|
|
322
|
+
bufferOffset = 0
|
|
323
|
+
bufferPool = bufferPool.slice(sliceLength, bufferPool.length)
|
|
324
|
+
} else {
|
|
325
|
+
notDecreased++
|
|
326
|
+
counter--
|
|
327
|
+
}
|
|
328
|
+
} else {
|
|
329
|
+
clearInterval(interval)
|
|
330
|
+
counter = 0
|
|
331
|
+
notDecreased = 0
|
|
332
|
+
interval = null
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
/**
|
|
337
|
+
* Check if the requested size fits in the current bufferPool.
|
|
338
|
+
* If it does not, reset and increase the bufferPool accordingly.
|
|
339
|
+
*
|
|
340
|
+
* @param {number} length
|
|
341
|
+
* @returns {undefined}
|
|
342
|
+
*/
|
|
343
|
+
function resizeBuffer (length) {
|
|
344
|
+
if (bufferPool.length < length + bufferOffset) {
|
|
345
|
+
const multiplier = length > 1024 * 1024 * 75 ? 2 : 3
|
|
346
|
+
if (bufferOffset > 1024 * 1024 * 111) {
|
|
347
|
+
bufferOffset = 1024 * 1024 * 50
|
|
348
|
+
}
|
|
349
|
+
bufferPool = Buffer.allocUnsafe(length * multiplier + bufferOffset)
|
|
350
|
+
bufferOffset = 0
|
|
351
|
+
counter++
|
|
352
|
+
if (interval === null) {
|
|
353
|
+
interval = setInterval(decreaseBufferPool, 50)
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
/**
|
|
359
|
+
* Concat a bulk string containing multiple chunks
|
|
360
|
+
*
|
|
361
|
+
* Notes:
|
|
362
|
+
* 1) The first chunk might contain the whole bulk string including the \r
|
|
363
|
+
* 2) We are only safe to fully add up elements that are neither the first nor any of the last two elements
|
|
364
|
+
*
|
|
365
|
+
* @param {JavascriptRedisParser} parser
|
|
366
|
+
* @returns {String}
|
|
367
|
+
*/
|
|
368
|
+
function concatBulkString (parser) {
|
|
369
|
+
const list = parser.bufferCache
|
|
370
|
+
const oldOffset = parser.offset
|
|
371
|
+
var chunks = list.length
|
|
372
|
+
var offset = parser.bigStrSize - parser.totalChunkSize
|
|
373
|
+
parser.offset = offset
|
|
374
|
+
if (offset <= 2) {
|
|
375
|
+
if (chunks === 2) {
|
|
376
|
+
return list[0].toString('utf8', oldOffset, list[0].length + offset - 2)
|
|
377
|
+
}
|
|
378
|
+
chunks--
|
|
379
|
+
offset = list[list.length - 2].length + offset
|
|
380
|
+
}
|
|
381
|
+
var res = decoder.write(list[0].slice(oldOffset))
|
|
382
|
+
for (var i = 1; i < chunks - 1; i++) {
|
|
383
|
+
res += decoder.write(list[i])
|
|
384
|
+
}
|
|
385
|
+
res += decoder.end(list[i].slice(0, offset - 2))
|
|
386
|
+
return res
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
/**
|
|
390
|
+
* Concat the collected chunks from parser.bufferCache.
|
|
391
|
+
*
|
|
392
|
+
* Increases the bufferPool size beforehand if necessary.
|
|
393
|
+
*
|
|
394
|
+
* @param {JavascriptRedisParser} parser
|
|
395
|
+
* @returns {Buffer}
|
|
396
|
+
*/
|
|
397
|
+
function concatBulkBuffer (parser) {
|
|
398
|
+
const list = parser.bufferCache
|
|
399
|
+
const oldOffset = parser.offset
|
|
400
|
+
const length = parser.bigStrSize - oldOffset - 2
|
|
401
|
+
var chunks = list.length
|
|
402
|
+
var offset = parser.bigStrSize - parser.totalChunkSize
|
|
403
|
+
parser.offset = offset
|
|
404
|
+
if (offset <= 2) {
|
|
405
|
+
if (chunks === 2) {
|
|
406
|
+
return list[0].slice(oldOffset, list[0].length + offset - 2)
|
|
407
|
+
}
|
|
408
|
+
chunks--
|
|
409
|
+
offset = list[list.length - 2].length + offset
|
|
410
|
+
}
|
|
411
|
+
resizeBuffer(length)
|
|
412
|
+
const start = bufferOffset
|
|
413
|
+
list[0].copy(bufferPool, start, oldOffset, list[0].length)
|
|
414
|
+
bufferOffset += list[0].length - oldOffset
|
|
415
|
+
for (var i = 1; i < chunks - 1; i++) {
|
|
416
|
+
list[i].copy(bufferPool, bufferOffset)
|
|
417
|
+
bufferOffset += list[i].length
|
|
418
|
+
}
|
|
419
|
+
list[i].copy(bufferPool, bufferOffset, 0, offset - 2)
|
|
420
|
+
bufferOffset += offset - 2
|
|
421
|
+
return bufferPool.slice(start, bufferOffset)
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
class JavascriptRedisParser {
|
|
425
|
+
/**
|
|
426
|
+
* Javascript Redis Parser constructor
|
|
427
|
+
* @param {{returnError: Function, returnReply: Function, returnFatalError?: Function, returnBuffers: boolean, stringNumbers: boolean }} options
|
|
428
|
+
* @constructor
|
|
429
|
+
*/
|
|
430
|
+
constructor (options) {
|
|
431
|
+
if (!options) {
|
|
432
|
+
throw new TypeError('Options are mandatory.')
|
|
433
|
+
}
|
|
434
|
+
if (typeof options.returnError !== 'function' || typeof options.returnReply !== 'function') {
|
|
435
|
+
throw new TypeError('The returnReply and returnError options have to be functions.')
|
|
436
|
+
}
|
|
437
|
+
this.setReturnBuffers(!!options.returnBuffers)
|
|
438
|
+
this.setStringNumbers(!!options.stringNumbers)
|
|
439
|
+
this.returnError = options.returnError
|
|
440
|
+
this.returnFatalError = options.returnFatalError || options.returnError
|
|
441
|
+
this.returnReply = options.returnReply
|
|
442
|
+
this.reset()
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
/**
|
|
446
|
+
* Reset the parser values to the initial state
|
|
447
|
+
*
|
|
448
|
+
* @returns {undefined}
|
|
449
|
+
*/
|
|
450
|
+
reset () {
|
|
451
|
+
this.offset = 0
|
|
452
|
+
this.buffer = null
|
|
453
|
+
this.bigStrSize = 0
|
|
454
|
+
this.totalChunkSize = 0
|
|
455
|
+
this.bufferCache = []
|
|
456
|
+
this.arrayCache = []
|
|
457
|
+
this.arrayPos = []
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
/**
|
|
461
|
+
* Set the returnBuffers option
|
|
462
|
+
*
|
|
463
|
+
* @param {boolean} returnBuffers
|
|
464
|
+
* @returns {undefined}
|
|
465
|
+
*/
|
|
466
|
+
setReturnBuffers (returnBuffers) {
|
|
467
|
+
if (typeof returnBuffers !== 'boolean') {
|
|
468
|
+
throw new TypeError('The returnBuffers argument has to be a boolean')
|
|
469
|
+
}
|
|
470
|
+
this.optionReturnBuffers = returnBuffers
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
/**
|
|
474
|
+
* Set the stringNumbers option
|
|
475
|
+
*
|
|
476
|
+
* @param {boolean} stringNumbers
|
|
477
|
+
* @returns {undefined}
|
|
478
|
+
*/
|
|
479
|
+
setStringNumbers (stringNumbers) {
|
|
480
|
+
if (typeof stringNumbers !== 'boolean') {
|
|
481
|
+
throw new TypeError('The stringNumbers argument has to be a boolean')
|
|
482
|
+
}
|
|
483
|
+
this.optionStringNumbers = stringNumbers
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
/**
|
|
487
|
+
* Parse the redis buffer
|
|
488
|
+
* @param {Buffer} buffer
|
|
489
|
+
* @returns {undefined}
|
|
490
|
+
*/
|
|
491
|
+
execute (buffer) {
|
|
492
|
+
if (this.buffer === null) {
|
|
493
|
+
this.buffer = buffer
|
|
494
|
+
this.offset = 0
|
|
495
|
+
} else if (this.bigStrSize === 0) {
|
|
496
|
+
const oldLength = this.buffer.length
|
|
497
|
+
const remainingLength = oldLength - this.offset
|
|
498
|
+
const newBuffer = Buffer.allocUnsafe(remainingLength + buffer.length)
|
|
499
|
+
this.buffer.copy(newBuffer, 0, this.offset, oldLength)
|
|
500
|
+
buffer.copy(newBuffer, remainingLength, 0, buffer.length)
|
|
501
|
+
this.buffer = newBuffer
|
|
502
|
+
this.offset = 0
|
|
503
|
+
if (this.arrayCache.length) {
|
|
504
|
+
const arr = parseArrayChunks(this)
|
|
505
|
+
if (arr === undefined) {
|
|
506
|
+
return
|
|
507
|
+
}
|
|
508
|
+
this.returnReply(arr)
|
|
509
|
+
}
|
|
510
|
+
} else if (this.totalChunkSize + buffer.length >= this.bigStrSize) {
|
|
511
|
+
this.bufferCache.push(buffer)
|
|
512
|
+
var tmp = this.optionReturnBuffers ? concatBulkBuffer(this) : concatBulkString(this)
|
|
513
|
+
this.bigStrSize = 0
|
|
514
|
+
this.bufferCache = []
|
|
515
|
+
this.buffer = buffer
|
|
516
|
+
if (this.arrayCache.length) {
|
|
517
|
+
this.arrayCache[0][this.arrayPos[0]++] = tmp
|
|
518
|
+
tmp = parseArrayChunks(this)
|
|
519
|
+
if (tmp === undefined) {
|
|
520
|
+
return
|
|
521
|
+
}
|
|
522
|
+
}
|
|
523
|
+
this.returnReply(tmp)
|
|
524
|
+
} else {
|
|
525
|
+
this.bufferCache.push(buffer)
|
|
526
|
+
this.totalChunkSize += buffer.length
|
|
527
|
+
return
|
|
528
|
+
}
|
|
529
|
+
|
|
530
|
+
while (this.offset < this.buffer.length) {
|
|
531
|
+
const offset = this.offset
|
|
532
|
+
const type = this.buffer[this.offset++]
|
|
533
|
+
const response = parseType(this, type)
|
|
534
|
+
if (response === undefined) {
|
|
535
|
+
if (!(this.arrayCache.length || this.bufferCache.length)) {
|
|
536
|
+
this.offset = offset
|
|
537
|
+
}
|
|
538
|
+
return
|
|
539
|
+
}
|
|
540
|
+
|
|
541
|
+
if (type === 45) {
|
|
542
|
+
this.returnError(response)
|
|
543
|
+
} else {
|
|
544
|
+
this.returnReply(response)
|
|
545
|
+
}
|
|
546
|
+
}
|
|
547
|
+
|
|
548
|
+
this.buffer = null
|
|
549
|
+
}
|
|
550
|
+
}
|
|
551
|
+
|
|
552
|
+
module.exports = JavascriptRedisParser
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "redis-parser",
|
|
3
|
+
"version": "3.0.0",
|
|
4
|
+
"description": "Javascript Redis protocol (RESP) parser",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"test": "npm run coverage",
|
|
8
|
+
"benchmark": "node ./benchmark",
|
|
9
|
+
"lint": "standard --fix",
|
|
10
|
+
"posttest": "npm run lint && npm run coverage:check",
|
|
11
|
+
"coverage": "node ./node_modules/istanbul/lib/cli.js cover --preserve-comments ./node_modules/mocha/bin/_mocha -- -R spec",
|
|
12
|
+
"coverage:check": "node ./node_modules/istanbul/lib/cli.js check-coverage --branch 100 --statement 100"
|
|
13
|
+
},
|
|
14
|
+
"repository": {
|
|
15
|
+
"type": "git",
|
|
16
|
+
"url": "git+https://github.com/NodeRedis/node-redis-parser.git"
|
|
17
|
+
},
|
|
18
|
+
"keywords": [
|
|
19
|
+
"redis",
|
|
20
|
+
"protocol",
|
|
21
|
+
"parser",
|
|
22
|
+
"database",
|
|
23
|
+
"javascript",
|
|
24
|
+
"node",
|
|
25
|
+
"nodejs",
|
|
26
|
+
"resp",
|
|
27
|
+
"hiredis"
|
|
28
|
+
],
|
|
29
|
+
"engines": {
|
|
30
|
+
"node": ">=4"
|
|
31
|
+
},
|
|
32
|
+
"dependencies": {
|
|
33
|
+
"redis-errors": "^1.0.0"
|
|
34
|
+
},
|
|
35
|
+
"devDependencies": {
|
|
36
|
+
"benchmark": "^2.1.0",
|
|
37
|
+
"codeclimate-test-reporter": "^0.4.0",
|
|
38
|
+
"hiredis": "^0.5.0",
|
|
39
|
+
"istanbul": "^0.4.0",
|
|
40
|
+
"mocha": "^3.1.2",
|
|
41
|
+
"standard": "^10.0.0"
|
|
42
|
+
},
|
|
43
|
+
"author": "Ruben Bridgewater",
|
|
44
|
+
"license": "MIT",
|
|
45
|
+
"bugs": {
|
|
46
|
+
"url": "https://github.com/NodeRedis/node-redis-parser/issues"
|
|
47
|
+
},
|
|
48
|
+
"homepage": "https://github.com/NodeRedis/node-redis-parser#readme",
|
|
49
|
+
"directories": {
|
|
50
|
+
"test": "test",
|
|
51
|
+
"lib": "lib"
|
|
52
|
+
}
|
|
53
|
+
}
|
package/index.js
CHANGED
|
@@ -109,6 +109,7 @@ function RedisClient (options, stream) {
|
|
|
109
109
|
this.closing = false;
|
|
110
110
|
this.server_info = {};
|
|
111
111
|
this.auth_pass = options.auth_pass || options.password;
|
|
112
|
+
this.auth_user = options.auth_user || options.user;
|
|
112
113
|
this.selected_db = options.db; // Save the selected db here, used when reconnecting
|
|
113
114
|
this.fire_strings = true; // Determine if strings or buffers should be written to the stream
|
|
114
115
|
this.pipeline = false;
|
|
@@ -240,7 +241,7 @@ RedisClient.prototype.create_stream = function () {
|
|
|
240
241
|
if (this.auth_pass !== undefined) {
|
|
241
242
|
this.ready = true;
|
|
242
243
|
// Fail silently as we might not be able to connect
|
|
243
|
-
this.auth(this.auth_pass, function (err) {
|
|
244
|
+
this.auth(this.auth_pass, this.auth_user, function (err) {
|
|
244
245
|
if (err && err.code !== 'UNCERTAIN_STATE') {
|
|
245
246
|
self.emit('error', err);
|
|
246
247
|
}
|
|
@@ -556,12 +557,8 @@ RedisClient.prototype.connection_gone = function (why, error) {
|
|
|
556
557
|
if (this.retry_delay instanceof Error) {
|
|
557
558
|
error = this.retry_delay;
|
|
558
559
|
}
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
errorMessage += 'connection timeout exceeded.';
|
|
562
|
-
} else {
|
|
563
|
-
errorMessage += 'maximum connection attempts exceeded.';
|
|
564
|
-
}
|
|
560
|
+
|
|
561
|
+
var errorMessage = 'Redis connection in broken state: retry aborted.';
|
|
565
562
|
|
|
566
563
|
this.flush_and_error({
|
|
567
564
|
message: errorMessage,
|
|
@@ -581,13 +578,7 @@ RedisClient.prototype.connection_gone = function (why, error) {
|
|
|
581
578
|
}
|
|
582
579
|
|
|
583
580
|
if (this.retry_totaltime >= this.connect_timeout) {
|
|
584
|
-
var message = 'Redis connection in broken state: ';
|
|
585
|
-
if (this.retry_totaltime >= this.connect_timeout) {
|
|
586
|
-
message += 'connection timeout exceeded.';
|
|
587
|
-
} else {
|
|
588
|
-
message += 'maximum connection attempts exceeded.';
|
|
589
|
-
}
|
|
590
|
-
|
|
581
|
+
var message = 'Redis connection in broken state: connection timeout exceeded.';
|
|
591
582
|
this.flush_and_error({
|
|
592
583
|
message: message,
|
|
593
584
|
code: 'CONNECTION_BROKEN',
|
|
@@ -864,11 +855,9 @@ RedisClient.prototype.internal_send_command = function (command_obj) {
|
|
|
864
855
|
if (command_obj.args && command_obj.args.length) {
|
|
865
856
|
undefinedArgError.args = command_obj.args;
|
|
866
857
|
}
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
}
|
|
871
|
-
throw undefinedArgError;
|
|
858
|
+
// there is always a callback in this scenario
|
|
859
|
+
command_obj.callback(undefinedArgError);
|
|
860
|
+
return false;
|
|
872
861
|
} else {
|
|
873
862
|
// Seems like numbers are converted fast using string concatenation
|
|
874
863
|
args_copy[i] = '' + args[i];
|
package/lib/createClient.js
CHANGED
|
@@ -29,7 +29,11 @@ module.exports = function createClient (port_arg, host_arg, options) {
|
|
|
29
29
|
// [redis:]//[[user][:password]@][host][:port][/db-number][?db=db-number[&password=bar[&option=value]]]
|
|
30
30
|
if (parsed.slashes) { // We require slashes
|
|
31
31
|
if (parsed.auth) {
|
|
32
|
-
|
|
32
|
+
var columnIndex = parsed.auth.indexOf(':');
|
|
33
|
+
options.password = parsed.auth.slice(columnIndex + 1);
|
|
34
|
+
if (columnIndex > 0) {
|
|
35
|
+
options.user = parsed.auth.slice(0, columnIndex);
|
|
36
|
+
}
|
|
33
37
|
}
|
|
34
38
|
if (parsed.protocol) {
|
|
35
39
|
if (parsed.protocol === 'rediss:') {
|