1 |
|
2 |
|
3 |
|
4 | var events = require("events"),
|
5 | util = require("util");
|
6 |
|
7 |
|
8 | var parseArguments = function(args, options) {
|
9 | var arr,
|
10 | len = args.length,
|
11 | callback,
|
12 | i = 0;
|
13 | if (Array.isArray(args[0])) {
|
14 |
|
15 |
|
16 | arr = args[0];
|
17 | callback = args[1];
|
18 | } else if (Array.isArray(args[1])) {
|
19 |
|
20 |
|
21 |
|
22 | if (len === 3) {
|
23 | callback = args[2];
|
24 | }
|
25 | len = args[1].length;
|
26 | arr = new Array(len + 1);
|
27 | arr[0] = args[0];
|
28 | for (; i < len; i += 1) {
|
29 | arr[i + 1] = args[1][i];
|
30 | }
|
31 | } else if (typeof args[1] === 'object' &&
|
32 | (args.length === 2 || args.length === 3 &&
|
33 | (typeof args[2] === 'function' || typeof args[2] === 'undefined'))) {
|
34 |
|
35 |
|
36 |
|
37 | arr = [args[0]];
|
38 | if(options && options.valueIsString) {
|
39 | arr.push(String(args[1]));
|
40 | } else if(options && options.valueIsBuffer) {
|
41 | arr.push(args[1]);
|
42 | } else {
|
43 | for (var field in args[1]) {
|
44 | arr.push(field, args[1][field]);
|
45 | }
|
46 | }
|
47 | callback = args[2];
|
48 | } else {
|
49 |
|
50 |
|
51 |
|
52 | len = args.length;
|
53 |
|
54 | if (len !== 0 && (typeof args[len - 1] === 'function' || typeof args[len - 1] === 'undefined')) {
|
55 | len--;
|
56 | callback = args[len];
|
57 | }
|
58 | arr = new Array(len);
|
59 | for (; i < len; i += 1) {
|
60 | arr[i] = args[i];
|
61 | }
|
62 | }
|
63 | if (callback) {
|
64 | arr.push(callback);
|
65 | }
|
66 |
|
67 | return arr;
|
68 | }
|
69 |
|
70 |
|
71 |
|
72 |
|
73 | function RedisMock() {
|
74 |
|
75 | this.storage = {};
|
76 |
|
77 | var self = this;
|
78 |
|
79 | |
80 |
|
81 |
|
82 |
|
83 | this._callCallback = function (callback, err, result) {
|
84 | if (callback && typeof callback === 'function') {
|
85 | process.nextTick(function () {
|
86 | callback(err, result);
|
87 | });
|
88 | }
|
89 | };
|
90 | }
|
91 |
|
92 |
|
93 |
|
94 |
|
95 | util.inherits(RedisMock, events.EventEmitter);
|
96 |
|
97 |
|
98 |
|
99 |
|
100 | var MockInstance = new RedisMock();
|
101 | module.exports = exports = MockInstance;
|
102 |
|
103 |
|
104 |
|
105 |
|
106 | function RedisClient(stream, options) {
|
107 |
|
108 | var self = this;
|
109 |
|
110 | this.connected = false;
|
111 | this.pub_sub_mode = false;
|
112 |
|
113 |
|
114 |
|
115 |
|
116 | this._message = function (ch, msg) {
|
117 |
|
118 | if (ch in self.subscriptions && self.subscriptions[ch] == true) {
|
119 | self.emit('message', ch, msg);
|
120 | }
|
121 |
|
122 | Object.keys(self.psubscriptions).some(function(key) {
|
123 | if(self.psubscriptions[key].test(ch)) {
|
124 | self.emit('pmessage', key, ch, msg);
|
125 | return true;
|
126 | }
|
127 | return false;
|
128 | });
|
129 | }
|
130 |
|
131 | MockInstance.on('message', this._message);
|
132 |
|
133 |
|
134 | this.subscriptions = {};
|
135 | this.psubscriptions = {};
|
136 |
|
137 |
|
138 | process.nextTick(function () {
|
139 | self.connected = true;
|
140 | self.emit("connect");
|
141 | self.emit("ready");
|
142 | });
|
143 | }
|
144 |
|
145 |
|
146 |
|
147 |
|
148 | util.inherits(RedisClient, events.EventEmitter);
|
149 |
|
150 |
|
151 |
|
152 |
|
153 | RedisMock.prototype.RedisClient = RedisClient;
|
154 |
|
155 |
|
156 |
|
157 |
|
158 | RedisClient.prototype.duplicate = function (_options, callback) {
|
159 | if (typeof callback !== 'undefined') {
|
160 | return callback(null, new RedisClient());
|
161 | } else {
|
162 | return new RedisClient();
|
163 | }
|
164 | };
|
165 |
|
166 |
|
167 |
|
168 |
|
169 | RedisClient.prototype.quit = function (callback) {
|
170 | var self = this;
|
171 |
|
172 |
|
173 | this.subscriptions = {};
|
174 |
|
175 | this.connected = false;
|
176 |
|
177 |
|
178 | MockInstance.removeListener('message', this._message);
|
179 |
|
180 |
|
181 |
|
182 | process.nextTick(function () {
|
183 | self.emit("end");
|
184 |
|
185 | if (callback) {
|
186 | return callback();
|
187 | }
|
188 | });
|
189 |
|
190 | }
|
191 |
|
192 |
|
193 |
|
194 |
|
195 | var end = RedisClient.prototype.end = function () {
|
196 | return this.quit();
|
197 | }
|
198 |
|
199 |
|
200 |
|
201 |
|
202 | var pubsub = require("./pubsub.js");
|
203 | RedisClient.prototype.subscribe = pubsub.subscribe;
|
204 | RedisClient.prototype.psubscribe = pubsub.psubscribe;
|
205 | RedisClient.prototype.unsubscribe = pubsub.unsubscribe;
|
206 | RedisClient.prototype.punsubscribe = pubsub.punsubscribe;
|
207 | RedisClient.prototype.publish = function (channel, msg, callback) {
|
208 | pubsub.publish.call(this, MockInstance, channel, msg);
|
209 |
|
210 | process.nextTick(function () {
|
211 | if (callback) {
|
212 | return callback();
|
213 | }
|
214 | });
|
215 | }
|
216 |
|
217 |
|
218 |
|
219 |
|
220 | var multi = require("./multi");
|
221 | RedisClient.prototype.multi = RedisClient.prototype.batch = function(commands) {
|
222 | return multi.multi(this, MockInstance, commands, false);
|
223 | }
|
224 | RedisClient.prototype.batch = function (commands) {
|
225 | return multi.multi(this, MockInstance, commands, true);
|
226 | }
|
227 |
|
228 | exports.Multi = multi.Multi;
|
229 |
|
230 |
|
231 |
|
232 |
|
233 |
|
234 | var keyfunctions = require("./keys.js");
|
235 |
|
236 | var getKeysVarArgs = function (args) {
|
237 | var keys = [];
|
238 | var hasCallback = typeof(args[args.length - 1]) === 'function';
|
239 | for (var i = 0; i < (hasCallback ? args.length - 1 : args.length); i++) {
|
240 | keys.push(args[i]);
|
241 | }
|
242 | var callback = hasCallback ? args[args.length - 1] : undefined;
|
243 | return {keys: keys, callback: callback};
|
244 | }
|
245 |
|
246 | RedisClient.prototype.del = RedisClient.prototype.DEL = function (keys, callback) {
|
247 |
|
248 | keyfunctions.del.call(this, MockInstance, keys, callback);
|
249 | };
|
250 |
|
251 | RedisClient.prototype.exists = RedisClient.prototype.EXISTS = function (keys, callback) {
|
252 | var args = getKeysVarArgs(arguments);
|
253 | keys = args.keys;
|
254 | callback = args.callback;
|
255 | keyfunctions.exists.call(this, MockInstance, keys, callback);
|
256 | };
|
257 |
|
258 | RedisClient.prototype.type = RedisClient.prototype.TYPE = function(key, callback) {
|
259 | keyfunctions.type.call(this, MockInstance, key, callback);
|
260 | }
|
261 |
|
262 | RedisClient.prototype.expire = RedisClient.prototype.EXPIRE = function (key, seconds, callback) {
|
263 |
|
264 | keyfunctions.expire.call(this, MockInstance, key, seconds, callback);
|
265 | };
|
266 |
|
267 | RedisClient.prototype.pexpire = RedisClient.prototype.PEXPIRE = function (key, ms, callback) {
|
268 |
|
269 | keyfunctions.pexpire.call(this, MockInstance, key, ms, callback);
|
270 | };
|
271 |
|
272 | RedisClient.prototype.persist = RedisClient.prototype.PERSIST = function (key, callback) {
|
273 | keyfunctions.persist.call(this, MockInstance, key, callback);
|
274 | };
|
275 |
|
276 | RedisClient.prototype.ttl = RedisClient.prototype.TTL = function (key, callback) {
|
277 |
|
278 | keyfunctions.ttl.call(this, MockInstance, key, callback);
|
279 | };
|
280 |
|
281 | RedisClient.prototype.pttl = RedisClient.prototype.PTTL = function (key, callback) {
|
282 | keyfunctions.pttl.call(this, MockInstance, key, callback);
|
283 | };
|
284 |
|
285 | RedisClient.prototype.keys = RedisClient.prototype.KEYS = function (pattern, callback) {
|
286 |
|
287 | keyfunctions.keys.call(this, MockInstance, pattern, callback);
|
288 | };
|
289 | RedisClient.prototype.scan = RedisClient.prototype.SCAN = function () {
|
290 | var args = parseArguments(arguments);
|
291 | var match = args[1];
|
292 | var count = args[2];
|
293 | if(args.length > 0) {
|
294 | for (var i = 0; i < args.length; i++) {
|
295 | if(typeof args[i] === 'string' && args[i].toLowerCase() === "match") {
|
296 | match = args[i+1];
|
297 | } else if(typeof args[i] === 'string' && args[i].toLowerCase() === "count") {
|
298 | count = args[i+1];
|
299 | }
|
300 | }
|
301 | }
|
302 | var index = args[0];
|
303 | var callback = args.pop();
|
304 | keyfunctions.scan.call(this, MockInstance, index, match, count, callback);
|
305 | };
|
306 |
|
307 | RedisClient.prototype.rename = RedisClient.prototype.RENAME = function (key, newKey, callback) {
|
308 | keyfunctions.rename.call(this, MockInstance, key, newKey, callback);
|
309 | };
|
310 |
|
311 | RedisClient.prototype.renamenx = RedisClient.prototype.RENAMENX = function (key, newKey, callback) {
|
312 | keyfunctions.renamenx.call(this, MockInstance, key, newKey, callback);
|
313 | };
|
314 |
|
315 | RedisClient.prototype.dbsize = RedisClient.prototype.DBSIZE = function (callback) {
|
316 | keyfunctions.dbsize.call(this, MockInstance, callback);
|
317 | };
|
318 |
|
319 | RedisClient.prototype.incr = RedisClient.prototype.INCR = function (key, callback) {
|
320 |
|
321 | stringfunctions.incr.call(this, MockInstance, key, callback);
|
322 | };
|
323 |
|
324 | RedisClient.prototype.incrby = RedisClient.prototype.INCRBY = function (key, value, callback) {
|
325 |
|
326 | stringfunctions.incrby.call(this, MockInstance, key, value, callback);
|
327 | };
|
328 |
|
329 | RedisClient.prototype.incrbyfloat = RedisClient.prototype.INCRBYFLOAT = function (key, value, callback) {
|
330 |
|
331 | stringfunctions.incrbyfloat.call(this, MockInstance, key, value, callback);
|
332 | };
|
333 |
|
334 | RedisClient.prototype.decr = RedisClient.prototype.DECR = function (key, callback) {
|
335 |
|
336 | stringfunctions.decr.call(this, MockInstance, key, callback);
|
337 | };
|
338 |
|
339 | RedisClient.prototype.decrby = RedisClient.prototype.DECRBY = function (key, value, callback) {
|
340 |
|
341 | stringfunctions.decrby.call(this, MockInstance, key, value, callback);
|
342 | };
|
343 |
|
344 |
|
345 |
|
346 |
|
347 |
|
348 | var stringfunctions = require("./strings.js");
|
349 | RedisClient.prototype.get = RedisClient.prototype.GET = function (key, callback) {
|
350 |
|
351 | stringfunctions.get.call(this, MockInstance, key, callback);
|
352 | };
|
353 |
|
354 | RedisClient.prototype.getset = RedisClient.prototype.GETSET = function (key, value, callback) {
|
355 |
|
356 | stringfunctions.getset.call(this, MockInstance, key, value, callback);
|
357 | };
|
358 |
|
359 |
|
360 | RedisClient.prototype.set = RedisClient.prototype.SET = function (key, value, callback) {
|
361 | var isBuffer = (value instanceof Buffer);
|
362 | var isString = !isBuffer;
|
363 | var args = parseArguments(arguments, { valueIsBuffer: isBuffer, valueIsString: isString });
|
364 |
|
365 | key = args.shift();
|
366 | value = args.shift();
|
367 | callback = args.pop();
|
368 |
|
369 | var isEx = false;
|
370 | var isPx = false;
|
371 | var isNx = false;
|
372 | var isXx = false;
|
373 | var expireTime = 0;
|
374 | var keyExists = false;
|
375 | if (key in MockInstance.storage) {
|
376 | keyExists = true;
|
377 | }
|
378 |
|
379 | if(args.length > 0) {
|
380 | for (var i = 0; i < args.length; i++) {
|
381 | if(typeof args[i] === 'string' && args[i].toLowerCase() === "ex") {
|
382 | isEx = true;
|
383 | } else if(typeof args[i] === 'string' && args[i].toLowerCase() === "px") {
|
384 | isPx = true;
|
385 | } else if(typeof args[i] === 'string' && args[i].toLowerCase() === "nx") {
|
386 | isNx = true;
|
387 | } else if(typeof args[i] === 'string' && args[i].toLowerCase() === "xx") {
|
388 | isXx = true;
|
389 | } else if(typeof args[i] === 'number' && args[i] % 1 === 0 ) {
|
390 | expireTime = args[i];
|
391 | }
|
392 | }
|
393 | }
|
394 | if(isPx === true) {
|
395 | expireTime = expireTime / 1000;
|
396 | isEx = true;
|
397 | }
|
398 |
|
399 | if(isEx === true) {
|
400 | if(isXx === true) {
|
401 | if(keyExists === true) {
|
402 | stringfunctions.set.call(this, MockInstance, key, value, function () {
|
403 | keyfunctions.expire.call(this, MockInstance, key, expireTime, function(err, result) {
|
404 | callback(err, "OK");
|
405 | });
|
406 | });
|
407 | } else {
|
408 | MockInstance._callCallback(callback, null, 0);
|
409 | }
|
410 | } else if(isNx === true) {
|
411 |
|
412 | if(keyExists === true) {
|
413 | MockInstance._callCallback(callback, null, null);
|
414 | } else {
|
415 | stringfunctions.set.call(this, MockInstance, key, value, function () {
|
416 | keyfunctions.expire.call(this, MockInstance, key, expireTime, function(err, result) {
|
417 | MockInstance._callCallback(callback, err, "OK");
|
418 | });
|
419 | });
|
420 | }
|
421 | } else {
|
422 | stringfunctions.set.call(this, MockInstance, key, value, function () {
|
423 | keyfunctions.expire.call(this, MockInstance, key, expireTime, function(err, result) {
|
424 | MockInstance._callCallback(callback, err, "OK");
|
425 | });
|
426 | });
|
427 | }
|
428 | } else {
|
429 |
|
430 | if(isXx === true) {
|
431 | if(keyExists === true) {
|
432 | stringfunctions.set.call(this, MockInstance, key, value, callback);
|
433 | } else {
|
434 | MockInstance._callCallback(callback, null, null);
|
435 | }
|
436 | } else if(isNx === true) {
|
437 | if(keyExists === true) {
|
438 | MockInstance._callCallback(callback, null, 0);
|
439 |
|
440 | } else {
|
441 | stringfunctions.set.call(this, MockInstance, key, value, callback);
|
442 | }
|
443 | } else {
|
444 | stringfunctions.set.call(this, MockInstance, key, value, callback);
|
445 | }
|
446 | }
|
447 |
|
448 | };
|
449 |
|
450 | RedisClient.prototype.ping = RedisClient.prototype.PING = function (callback) {
|
451 |
|
452 | stringfunctions.ping.call(this, MockInstance, callback);
|
453 | };
|
454 |
|
455 | RedisClient.prototype.setex = RedisClient.prototype.SETEX = function (key, seconds, value, callback) {
|
456 |
|
457 | stringfunctions.set.call(this, MockInstance, key, value, function () {
|
458 | keyfunctions.expire.call(this, MockInstance, key, seconds, function(err, result) {
|
459 | MockInstance._callCallback(callback, err, "OK");
|
460 | });
|
461 | });
|
462 | };
|
463 |
|
464 | RedisClient.prototype.setnx = RedisClient.prototype.SETNX = function (key, value, callback) {
|
465 | stringfunctions.setnx.call(this, MockInstance, key, value, callback);
|
466 | };
|
467 |
|
468 | RedisClient.prototype.mget = RedisClient.prototype.MGET = function () {
|
469 | var newArguments = [MockInstance];
|
470 | for (var i = 0; i < arguments.length; i++) {
|
471 | newArguments.push(arguments[i]);
|
472 | }
|
473 |
|
474 | stringfunctions.mget.apply(this, newArguments);
|
475 | };
|
476 |
|
477 | RedisClient.prototype.mset = RedisClient.prototype.MSET = function () {
|
478 | var newArguments = [MockInstance, false];
|
479 | for (var i = 0; i < arguments.length; i++) {
|
480 | newArguments.push(arguments[i]);
|
481 | }
|
482 |
|
483 | stringfunctions.mset.apply(this, newArguments);
|
484 | };
|
485 |
|
486 | RedisClient.prototype.msetnx = RedisClient.prototype.MSETNX = function () {
|
487 | var newArguments = [MockInstance, true];
|
488 | for (var i = 0; i < arguments.length; i++) {
|
489 | newArguments.push(arguments[i]);
|
490 | }
|
491 |
|
492 | stringfunctions.mset.apply(this, newArguments);
|
493 | };
|
494 |
|
495 |
|
496 |
|
497 |
|
498 | var hashing = require("./hash.js");
|
499 | RedisClient.prototype.hget = RedisClient.prototype.HGET = function (hash, key, callback) {
|
500 |
|
501 | hashing.hget.apply(this, [MockInstance].concat(parseArguments(arguments)));
|
502 | }
|
503 | RedisClient.prototype.hexists = RedisClient.prototype.HEXISTS = function (hash, key, callback) {
|
504 |
|
505 | hashing.hexists.apply(this, [MockInstance].concat(parseArguments(arguments)));
|
506 | }
|
507 | RedisClient.prototype.hdel = RedisClient.prototype.HDEL = function (hash, key, callback) {
|
508 |
|
509 | hashing.hdel.apply(this, [MockInstance].concat(parseArguments(arguments)));
|
510 | }
|
511 | RedisClient.prototype.hset = RedisClient.prototype.HSET = function (hash, key, value, callback) {
|
512 |
|
513 | hashing.hset.apply(this, [MockInstance].concat(parseArguments(arguments)));
|
514 | }
|
515 | RedisClient.prototype.hincrby = RedisClient.prototype.HINCRBY = function (hash, key, increment, callback) {
|
516 |
|
517 | hashing.hincrby.apply(this, [MockInstance].concat(parseArguments(arguments)));
|
518 | }
|
519 | RedisClient.prototype.hincrbyfloat = RedisClient.prototype.HINCRBYFLOAT = function (hash, key, increment, callback) {
|
520 |
|
521 | hashing.hincrbyfloat.apply(this, [MockInstance].concat(parseArguments(arguments)));
|
522 | }
|
523 |
|
524 | RedisClient.prototype.hsetnx = RedisClient.prototype.HSETNX = function (hash, key, value, callback) {
|
525 |
|
526 | hashing.hsetnx.apply(this, [MockInstance].concat(parseArguments(arguments)));
|
527 | }
|
528 | RedisClient.prototype.hlen = RedisClient.prototype.HLEN = function (hash, callback) {
|
529 |
|
530 | hashing.hlen.apply(this, [MockInstance].concat(parseArguments(arguments)));
|
531 | }
|
532 |
|
533 | RedisClient.prototype.hkeys = RedisClient.prototype.HKEYS = function (hash, callback) {
|
534 |
|
535 | hashing.hkeys.apply(this, [MockInstance].concat(parseArguments(arguments)));
|
536 | }
|
537 | RedisClient.prototype.hvals = RedisClient.prototype.HVALS = function (hash, callback) {
|
538 |
|
539 | hashing.hvals.apply(this, [MockInstance].concat(parseArguments(arguments)));
|
540 | }
|
541 | RedisClient.prototype.hmset = RedisClient.prototype.HMSET = function () {
|
542 |
|
543 | hashing.hmset.apply(this, [MockInstance].concat(parseArguments(arguments)));
|
544 | }
|
545 | RedisClient.prototype.hmget = RedisClient.prototype.HMGET = function () {
|
546 |
|
547 | hashing.hmget.apply(this, [MockInstance].concat(parseArguments(arguments)));
|
548 | }
|
549 | RedisClient.prototype.hgetall = RedisClient.prototype.HGETALL = function (hash, callback) {
|
550 |
|
551 | hashing.hgetall.apply(this, [MockInstance].concat(parseArguments(arguments)));
|
552 | }
|
553 |
|
554 | RedisClient.prototype.hscan = RedisClient.prototype.HSCAN = function () {
|
555 | var args = parseArguments(arguments);
|
556 | var hash = args[0];
|
557 | var index = args[1] || 0;
|
558 | var match = '*';
|
559 | var count = 10;
|
560 |
|
561 | if(args.length > 0) {
|
562 | for (var i = 0; i < args.length; i++) {
|
563 | if(typeof args[i] === 'string' && args[i].toLowerCase() === "match") {
|
564 | match = args[i+1];
|
565 | } else if(typeof args[i] === 'string' && args[i].toLowerCase() === "count") {
|
566 | count = args[i+1];
|
567 | }
|
568 | }
|
569 | }
|
570 | var callback = args.pop();
|
571 | hashing.hscan.call(this, MockInstance, hash, index, match, count, callback);
|
572 | };
|
573 |
|
574 |
|
575 |
|
576 |
|
577 |
|
578 | var listfunctions = require("./list.js");
|
579 | RedisClient.prototype.llen = RedisClient.prototype.LLEN = function (key, callback) {
|
580 | listfunctions.llen.call(this, MockInstance, key, callback);
|
581 | }
|
582 |
|
583 | RedisClient.prototype.lpush = RedisClient.prototype.LPUSH = function () {
|
584 | var args = parseArguments(arguments);
|
585 | listfunctions.lpush.apply(this, [MockInstance].concat(args));
|
586 | }
|
587 |
|
588 | RedisClient.prototype.rpush = RedisClient.prototype.RPUSH = function () {
|
589 | var args = parseArguments(arguments);
|
590 | listfunctions.rpush.apply(this, [MockInstance].concat(args));
|
591 | }
|
592 |
|
593 | RedisClient.prototype.lpushx = RedisClient.prototype.LPUSHX = function (key, value, callback) {
|
594 | listfunctions.lpushx.call(this, MockInstance, key, value, callback);
|
595 | }
|
596 |
|
597 | RedisClient.prototype.rpushx = RedisClient.prototype.RPUSHX = function (key, value, callback) {
|
598 | listfunctions.rpushx.call(this, MockInstance, key, value, callback);
|
599 | }
|
600 |
|
601 | RedisClient.prototype.lpop = RedisClient.prototype.LPOP = function (key, callback) {
|
602 | listfunctions.lpop.call(this, MockInstance, key, callback);
|
603 | }
|
604 |
|
605 | RedisClient.prototype.rpop = RedisClient.prototype.RPOP = function (key, callback) {
|
606 | listfunctions.rpop.call(this, MockInstance, key, callback);
|
607 | }
|
608 |
|
609 | RedisClient.prototype.rpoplpush = RedisClient.prototype.RPOPLPUSH = function (sourceKey, destinationKey, callback) {
|
610 | listfunctions.rpoplpush.call(this, MockInstance, sourceKey, destinationKey, callback);
|
611 | }
|
612 |
|
613 | var bpop = function (fn, key, timeout, callback) {
|
614 | var keys = [];
|
615 | var hasCallback = typeof(arguments[arguments.length - 1]) === "function";
|
616 | for (var i = 1; i < (hasCallback ? arguments.length - 2 : arguments.length - 1); i++) {
|
617 | keys.push(arguments[i]);
|
618 | }
|
619 | if (hasCallback) {
|
620 | fn.call(this, MockInstance, keys, arguments[arguments.length - 2], arguments[arguments.length - 1]);
|
621 | } else {
|
622 | fn.call(this, MockInstance, keys, arguments[arguments.length - 1]);
|
623 | }
|
624 | }
|
625 |
|
626 | RedisClient.prototype.blpop = RedisClient.prototype.BLPOP = function (key, timeout, callback) {
|
627 | var args = [listfunctions.blpop];
|
628 | for (var i = 0; i < arguments.length; i++) {
|
629 | args.push(arguments[i]);
|
630 | }
|
631 | bpop.apply(this, args);
|
632 | }
|
633 |
|
634 | RedisClient.prototype.brpop = RedisClient.prototype.BRPOP = function (key, timeout, callback) {
|
635 | var args = [listfunctions.brpop];
|
636 | for (var i = 0; i < arguments.length; i++) {
|
637 | args.push(arguments[i]);
|
638 | }
|
639 | bpop.apply(this, args);
|
640 | }
|
641 |
|
642 | RedisClient.prototype.lindex = RedisClient.prototype.LINDEX = function (key, index, callback) {
|
643 | listfunctions.lindex.call(this, MockInstance, key, index, callback);
|
644 | }
|
645 |
|
646 | RedisClient.prototype.lrange = RedisClient.prototype.LRANGE = function (key, index1, index2, callback) {
|
647 | listfunctions.lrange.call(this, MockInstance, key, index1, index2, callback);
|
648 | }
|
649 |
|
650 | RedisClient.prototype.lrem = RedisClient.prototype.LREM = function (key, index, value, callback) {
|
651 | listfunctions.lrem.call(this, MockInstance, key, index, value, callback);
|
652 | }
|
653 |
|
654 | RedisClient.prototype.lset = RedisClient.prototype.LSET = function (key, index, value, callback) {
|
655 | listfunctions.lset.call(this, MockInstance, key, index, value, callback);
|
656 | }
|
657 |
|
658 | RedisClient.prototype.ltrim = RedisClient.prototype.LTRIM = function (key, start, end, callback) {
|
659 | listfunctions.ltrim.call(this, MockInstance, key, start, end, callback);
|
660 | }
|
661 |
|
662 |
|
663 |
|
664 |
|
665 | var setfunctions = require("./set.js");
|
666 |
|
667 | var getVarargs = function (args) {
|
668 | var members = [];
|
669 | var hasCallback = typeof(args[args.length - 1]) === 'function';
|
670 | for (var i = 1; i < (hasCallback ? args.length - 1 : args.length); i++) {
|
671 | members.push(args[i]);
|
672 | }
|
673 | var callback = hasCallback ? args[args.length - 1] : undefined;
|
674 | return {members: members, callback: callback};
|
675 | }
|
676 |
|
677 | RedisClient.prototype.sadd = RedisClient.prototype.SADD = function () {
|
678 | setfunctions.sadd.apply(this, [MockInstance].concat(parseArguments(arguments)));
|
679 | }
|
680 |
|
681 | RedisClient.prototype.srem = RedisClient.prototype.SREM = function () {
|
682 | setfunctions.srem.apply(this, [MockInstance].concat(parseArguments(arguments)));
|
683 | }
|
684 |
|
685 | RedisClient.prototype.smembers = RedisClient.prototype.SMEMBERS = function (key, callback) {
|
686 | setfunctions.smembers.call(this, MockInstance, key, callback);
|
687 | }
|
688 |
|
689 | RedisClient.prototype.scard = RedisClient.prototype.SCARD = function (key, callback) {
|
690 | setfunctions.scard.call(this, MockInstance, key, callback);
|
691 | }
|
692 |
|
693 | RedisClient.prototype.sismember = RedisClient.prototype.SISMEMBER = function (key, member, callback) {
|
694 | setfunctions.sismember.call(this, MockInstance, key, member, callback);
|
695 | }
|
696 |
|
697 | RedisClient.prototype.smove = RedisClient.prototype.SMOVE = function (source, destination, member, callback) {
|
698 | setfunctions.smove.call(this, MockInstance, source, destination, member, callback);
|
699 | }
|
700 |
|
701 | RedisClient.prototype.srandmember = RedisClient.prototype.SRANDMEMBER = function (key, count, callback) {
|
702 | setfunctions.srandmember.call(this, MockInstance, key, count, callback);
|
703 | }
|
704 |
|
705 |
|
706 |
|
707 |
|
708 |
|
709 |
|
710 |
|
711 |
|
712 |
|
713 |
|
714 |
|
715 |
|
716 |
|
717 |
|
718 |
|
719 |
|
720 | var sortedset = require("./sortedset.js");
|
721 |
|
722 | RedisClient.prototype.zadd = RedisClient.prototype.ZADD = function () {
|
723 |
|
724 | var args = parseArguments(arguments);
|
725 | sortedset.zadd.apply(this, [MockInstance].concat(args));
|
726 | }
|
727 |
|
728 | RedisClient.prototype.zcard = RedisClient.prototype.ZCARD = function () {
|
729 |
|
730 | var args = parseArguments(arguments);
|
731 | sortedset.zcard.apply(this, [MockInstance].concat(args));
|
732 | }
|
733 |
|
734 | RedisClient.prototype.zcount = RedisClient.prototype.ZCOUNT = function () {
|
735 |
|
736 | var args = parseArguments(arguments);
|
737 | sortedset.zcount.apply(this, [MockInstance].concat(args));
|
738 | }
|
739 |
|
740 | RedisClient.prototype.zincrby = RedisClient.prototype.ZINCRBY = function () {
|
741 |
|
742 | var args = parseArguments(arguments);
|
743 | sortedset.zincrby.apply(this, [MockInstance].concat(args));
|
744 | }
|
745 |
|
746 | RedisClient.prototype.zrange = RedisClient.prototype.ZRANGE = function () {
|
747 |
|
748 | var args = parseArguments(arguments);
|
749 | sortedset.zrange.apply(this, [MockInstance].concat(args));
|
750 | }
|
751 |
|
752 | RedisClient.prototype.zrangebyscore = RedisClient.prototype.ZRANGEBYSCORE = function () {
|
753 |
|
754 | var args = parseArguments(arguments);
|
755 | sortedset.zrangebyscore.apply(this, [MockInstance].concat(args));
|
756 | }
|
757 |
|
758 | RedisClient.prototype.zrank = RedisClient.prototype.ZRANK = function () {
|
759 |
|
760 | var args = parseArguments(arguments);
|
761 | sortedset.zrank.apply(this, [MockInstance].concat(args));
|
762 | }
|
763 |
|
764 | RedisClient.prototype.zrem = RedisClient.prototype.ZREM = function () {
|
765 |
|
766 | var args = parseArguments(arguments);
|
767 | sortedset.zrem.apply(this, [MockInstance].concat(args));
|
768 | }
|
769 |
|
770 | RedisClient.prototype.zremrangebyrank = RedisClient.prototype.ZREMRANGEBYRANK = function () {
|
771 |
|
772 | var args = parseArguments(arguments);
|
773 | sortedset.zremrangebyrank.apply(this, [MockInstance].concat(args));
|
774 | }
|
775 |
|
776 | RedisClient.prototype.zremrangebyscore = RedisClient.prototype.ZREMRANGEBYSCORE = function () {
|
777 |
|
778 | var args = parseArguments(arguments);
|
779 | sortedset.zremrangebyscore.apply(this, [MockInstance].concat(args));
|
780 | }
|
781 |
|
782 | RedisClient.prototype.zrevrange = RedisClient.prototype.ZREVRANGE = function () {
|
783 |
|
784 | var args = parseArguments(arguments);
|
785 | sortedset.zrevrange.apply(this, [MockInstance].concat(args));
|
786 | }
|
787 |
|
788 | RedisClient.prototype.zrevrangebyscore = RedisClient.prototype.ZREVRANGEBYSCORE = function () {
|
789 |
|
790 | var args = parseArguments(arguments);
|
791 | sortedset.zrevrangebyscore.apply(this, [MockInstance].concat(args));
|
792 | }
|
793 |
|
794 | RedisClient.prototype.zrevrank = RedisClient.prototype.ZREVRANK = function () {
|
795 |
|
796 | var args = parseArguments(arguments);
|
797 | sortedset.zrevrank.apply(this, [MockInstance].concat(args));
|
798 | }
|
799 |
|
800 | RedisClient.prototype.zunionstore = RedisClient.prototype.ZUNIONSTORE = function() {
|
801 | var args = parseArguments(arguments);
|
802 | sortedset.zunionstore.apply(this, [MockInstance].concat(args))
|
803 | }
|
804 |
|
805 | RedisClient.prototype.zinterstore = RedisClient.prototype.ZINTERSTORE = function() {
|
806 | var args = parseArguments(arguments);
|
807 | sortedset.zinterstore.apply(this, [MockInstance].concat(args))
|
808 | }
|
809 |
|
810 | RedisClient.prototype.zscore = RedisClient.prototype.ZSCORE = function () {
|
811 |
|
812 | var args = parseArguments(arguments);
|
813 | sortedset.zscore.apply(this, [MockInstance].concat(args));
|
814 | }
|
815 |
|
816 |
|
817 |
|
818 |
|
819 |
|
820 | RedisClient.prototype.send_command = RedisClient.prototype.SEND_COMMAND = function (callback) {
|
821 | if (typeof(arguments[arguments.length - 1]) == 'function') {
|
822 | arguments[arguments.length - 1]();
|
823 | }
|
824 | }
|
825 |
|
826 | RedisClient.prototype.select = function (databaseIndex, callback) {
|
827 | var defaultMaxIndex = 15;
|
828 | if (!isNaN(databaseIndex) && (databaseIndex <= defaultMaxIndex)) {
|
829 | return MockInstance._callCallback( callback, null, "OK");
|
830 | } else {
|
831 | var error = new Error('ERR invalid DB index');
|
832 | return MockInstance._callCallback( callback, error, null);
|
833 | }
|
834 | }
|
835 |
|
836 |
|
837 |
|
838 |
|
839 | var serverfunctions = require("./server.js");
|
840 | RedisClient.prototype.flushdb = RedisClient.prototype.FLUSHDB = function (callback) {
|
841 |
|
842 | serverfunctions.flushdb.call(this, MockInstance, callback);
|
843 | }
|
844 | RedisClient.prototype.flushall = RedisClient.prototype.FLUSHALL = function (callback) {
|
845 |
|
846 | serverfunctions.flushall.call(this, MockInstance, callback);
|
847 | }
|
848 |
|
849 | RedisClient.prototype.auth = RedisClient.prototype.AUTH = function (password, callback) {
|
850 |
|
851 | serverfunctions.auth.call(this, MockInstance, password, callback);
|
852 | }
|
853 |
|
854 | RedisMock.prototype.createClient = function (port_arg, host_arg, options) {
|
855 |
|
856 | return new RedisClient();
|
857 | }
|