UNPKG

26.8 kBJavaScriptView Raw
1/**
2 * Module dependencies
3 */
4var events = require("events"),
5 util = require("util");
6
7
8var parseArguments = function(args, options) { // eslint-disable-line complexity
9 var arr,
10 len = args.length,
11 callback,
12 i = 0;
13 if (Array.isArray(args[0])) {
14 // arg0 = [hash, k1, v1, k2, v2,]
15 // arg1 = callback
16 arr = args[0];
17 callback = args[1];
18 } else if (Array.isArray(args[1])) {
19 // arg0 = hash
20 // arg1 = [k1, v1, k2, v2,]
21 // arg2 = callback
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 // arg0 = hash
35 // arg1 = {k1: v1, k2: v2,}
36 // arg2 = callback
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 // arg0 = hash
50 // arg1..N-1 = k1,v1,k2,v2,...N-1
51 // argN = callback
52 len = args.length;
53 // The later should not be the average use case
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 * RedisMock constructor
72 */
73function RedisMock() {
74
75 this.storage = {};
76
77 var self = this;
78
79 /**
80 * Helper function to launch the callback(err, reply)
81 * on the next process tick
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 * RedisMock inherits from EventEmitter to be mock pub/sub
94 */
95util.inherits(RedisMock, events.EventEmitter);
96
97/*
98 * Create RedisMock instance and export
99 */
100var MockInstance = new RedisMock();
101module.exports = exports = MockInstance;
102
103/**
104 * RedisClient constructor
105 */
106function RedisClient(stream, options) {
107
108 var self = this;
109
110 this.connected = false;
111 this.pub_sub_mode = false;
112
113
114 // We always listen for 'message', even if this is not a subscription client.
115 // We will only act on it, however, if the channel is in this.subscriptions, which is populated through subscribe
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 // Pub/sub subscriptions
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 * RedisClient inherits from EventEmitter
147 */
148util.inherits(RedisClient, events.EventEmitter);
149
150/**
151 * Export the RedisClient constructor
152 */
153RedisMock.prototype.RedisClient = RedisClient;
154
155/**
156 * Duplicate
157 */
158RedisClient.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 * Quit
168 */
169RedisClient.prototype.quit = function (callback) {
170 var self = this;
171
172 // Remove all subscriptions (pub/sub)
173 this.subscriptions = {};
174
175 this.connected = false;
176
177 //Remove listener from MockInstance to avoid 'too many subscribers errors'
178 MockInstance.removeListener('message', this._message);
179
180 // TODO: Anything else we need to clear?
181
182 process.nextTick(function () {
183 self.emit("end");
184
185 if (callback) {
186 return callback();
187 }
188 });
189
190}
191
192/**
193 * End
194 */
195var end = RedisClient.prototype.end = function () {
196 return this.quit();
197}
198
199/**
200 * Publish / subscribe / unsubscribe
201 */
202var pubsub = require("./pubsub.js");
203RedisClient.prototype.subscribe = pubsub.subscribe;
204RedisClient.prototype.psubscribe = pubsub.psubscribe;
205RedisClient.prototype.unsubscribe = pubsub.unsubscribe;
206RedisClient.prototype.punsubscribe = pubsub.punsubscribe;
207RedisClient.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 * multi
219 */
220var multi = require("./multi");
221RedisClient.prototype.multi = RedisClient.prototype.batch = function(commands) {
222 return multi.multi(this, MockInstance, commands, false);
223}
224RedisClient.prototype.batch = function (commands) {
225 return multi.multi(this, MockInstance, commands, true);
226}
227
228exports.Multi = multi.Multi;
229
230/**
231 * Keys function
232 */
233
234var keyfunctions = require("./keys.js");
235
236var 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
246RedisClient.prototype.del = RedisClient.prototype.DEL = function (keys, callback) {
247
248 keyfunctions.del.call(this, MockInstance, keys, callback);
249};
250
251RedisClient.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
258RedisClient.prototype.type = RedisClient.prototype.TYPE = function(key, callback) {
259 keyfunctions.type.call(this, MockInstance, key, callback);
260}
261
262RedisClient.prototype.expire = RedisClient.prototype.EXPIRE = function (key, seconds, callback) {
263
264 keyfunctions.expire.call(this, MockInstance, key, seconds, callback);
265};
266
267RedisClient.prototype.pexpire = RedisClient.prototype.PEXPIRE = function (key, ms, callback) {
268
269 keyfunctions.pexpire.call(this, MockInstance, key, ms, callback);
270};
271
272RedisClient.prototype.persist = RedisClient.prototype.PERSIST = function (key, callback) {
273 keyfunctions.persist.call(this, MockInstance, key, callback);
274};
275
276RedisClient.prototype.ttl = RedisClient.prototype.TTL = function (key, callback) {
277
278 keyfunctions.ttl.call(this, MockInstance, key, callback);
279};
280
281RedisClient.prototype.pttl = RedisClient.prototype.PTTL = function (key, callback) {
282 keyfunctions.pttl.call(this, MockInstance, key, callback);
283};
284
285RedisClient.prototype.keys = RedisClient.prototype.KEYS = function (pattern, callback) {
286
287 keyfunctions.keys.call(this, MockInstance, pattern, callback);
288};
289RedisClient.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
307RedisClient.prototype.rename = RedisClient.prototype.RENAME = function (key, newKey, callback) {
308 keyfunctions.rename.call(this, MockInstance, key, newKey, callback);
309};
310
311RedisClient.prototype.renamenx = RedisClient.prototype.RENAMENX = function (key, newKey, callback) {
312 keyfunctions.renamenx.call(this, MockInstance, key, newKey, callback);
313};
314
315RedisClient.prototype.dbsize = RedisClient.prototype.DBSIZE = function (callback) {
316 keyfunctions.dbsize.call(this, MockInstance, callback);
317};
318
319RedisClient.prototype.incr = RedisClient.prototype.INCR = function (key, callback) {
320
321 stringfunctions.incr.call(this, MockInstance, key, callback);
322};
323
324RedisClient.prototype.incrby = RedisClient.prototype.INCRBY = function (key, value, callback) {
325
326 stringfunctions.incrby.call(this, MockInstance, key, value, callback);
327};
328
329RedisClient.prototype.incrbyfloat = RedisClient.prototype.INCRBYFLOAT = function (key, value, callback) {
330
331 stringfunctions.incrbyfloat.call(this, MockInstance, key, value, callback);
332};
333
334RedisClient.prototype.decr = RedisClient.prototype.DECR = function (key, callback) {
335
336 stringfunctions.decr.call(this, MockInstance, key, callback);
337};
338
339RedisClient.prototype.decrby = RedisClient.prototype.DECRBY = function (key, value, callback) {
340
341 stringfunctions.decrby.call(this, MockInstance, key, value, callback);
342};
343
344/**
345 * String function
346 */
347
348var stringfunctions = require("./strings.js");
349RedisClient.prototype.get = RedisClient.prototype.GET = function (key, callback) {
350
351 stringfunctions.get.call(this, MockInstance, key, callback);
352};
353
354RedisClient.prototype.getset = RedisClient.prototype.GETSET = function (key, value, callback) {
355
356 stringfunctions.getset.call(this, MockInstance, key, value, callback);
357};
358
359//SET key value [EX seconds] [PX milliseconds] [NX|XX]
360RedisClient.prototype.set = RedisClient.prototype.SET = function (key, value, callback) { // eslint-disable-line complexity
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
450RedisClient.prototype.ping = RedisClient.prototype.PING = function (callback) {
451
452 stringfunctions.ping.call(this, MockInstance, callback);
453};
454
455RedisClient.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
464RedisClient.prototype.setnx = RedisClient.prototype.SETNX = function (key, value, callback) {
465 stringfunctions.setnx.call(this, MockInstance, key, value, callback);
466};
467
468RedisClient.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
477RedisClient.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
486RedisClient.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 * Hashing functions
497 */
498var hashing = require("./hash.js");
499RedisClient.prototype.hget = RedisClient.prototype.HGET = function (hash, key, callback) {
500
501 hashing.hget.apply(this, [MockInstance].concat(parseArguments(arguments)));
502}
503RedisClient.prototype.hexists = RedisClient.prototype.HEXISTS = function (hash, key, callback) {
504
505 hashing.hexists.apply(this, [MockInstance].concat(parseArguments(arguments)));
506}
507RedisClient.prototype.hdel = RedisClient.prototype.HDEL = function (hash, key, callback) {
508
509 hashing.hdel.apply(this, [MockInstance].concat(parseArguments(arguments)));
510}
511RedisClient.prototype.hset = RedisClient.prototype.HSET = function (hash, key, value, callback) {
512
513 hashing.hset.apply(this, [MockInstance].concat(parseArguments(arguments)));
514}
515RedisClient.prototype.hincrby = RedisClient.prototype.HINCRBY = function (hash, key, increment, callback) {
516
517 hashing.hincrby.apply(this, [MockInstance].concat(parseArguments(arguments)));
518}
519RedisClient.prototype.hincrbyfloat = RedisClient.prototype.HINCRBYFLOAT = function (hash, key, increment, callback) {
520
521 hashing.hincrbyfloat.apply(this, [MockInstance].concat(parseArguments(arguments)));
522}
523
524RedisClient.prototype.hsetnx = RedisClient.prototype.HSETNX = function (hash, key, value, callback) {
525
526 hashing.hsetnx.apply(this, [MockInstance].concat(parseArguments(arguments)));
527}
528RedisClient.prototype.hlen = RedisClient.prototype.HLEN = function (hash, callback) {
529
530 hashing.hlen.apply(this, [MockInstance].concat(parseArguments(arguments)));
531}
532
533RedisClient.prototype.hkeys = RedisClient.prototype.HKEYS = function (hash, callback) {
534
535 hashing.hkeys.apply(this, [MockInstance].concat(parseArguments(arguments)));
536}
537RedisClient.prototype.hvals = RedisClient.prototype.HVALS = function (hash, callback) {
538
539 hashing.hvals.apply(this, [MockInstance].concat(parseArguments(arguments)));
540}
541RedisClient.prototype.hmset = RedisClient.prototype.HMSET = function () {
542
543 hashing.hmset.apply(this, [MockInstance].concat(parseArguments(arguments)));
544}
545RedisClient.prototype.hmget = RedisClient.prototype.HMGET = function () {
546
547 hashing.hmget.apply(this, [MockInstance].concat(parseArguments(arguments)));
548}
549RedisClient.prototype.hgetall = RedisClient.prototype.HGETALL = function (hash, callback) {
550
551 hashing.hgetall.apply(this, [MockInstance].concat(parseArguments(arguments)));
552}
553
554RedisClient.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 * List functions
577 */
578var listfunctions = require("./list.js");
579RedisClient.prototype.llen = RedisClient.prototype.LLEN = function (key, callback) {
580 listfunctions.llen.call(this, MockInstance, key, callback);
581}
582
583RedisClient.prototype.lpush = RedisClient.prototype.LPUSH = function () {
584 var args = parseArguments(arguments);
585 listfunctions.lpush.apply(this, [MockInstance].concat(args));
586}
587
588RedisClient.prototype.rpush = RedisClient.prototype.RPUSH = function () {
589 var args = parseArguments(arguments);
590 listfunctions.rpush.apply(this, [MockInstance].concat(args));
591}
592
593RedisClient.prototype.lpushx = RedisClient.prototype.LPUSHX = function (key, value, callback) {
594 listfunctions.lpushx.call(this, MockInstance, key, value, callback);
595}
596
597RedisClient.prototype.rpushx = RedisClient.prototype.RPUSHX = function (key, value, callback) {
598 listfunctions.rpushx.call(this, MockInstance, key, value, callback);
599}
600
601RedisClient.prototype.lpop = RedisClient.prototype.LPOP = function (key, callback) {
602 listfunctions.lpop.call(this, MockInstance, key, callback);
603}
604
605RedisClient.prototype.rpop = RedisClient.prototype.RPOP = function (key, callback) {
606 listfunctions.rpop.call(this, MockInstance, key, callback);
607}
608
609RedisClient.prototype.rpoplpush = RedisClient.prototype.RPOPLPUSH = function (sourceKey, destinationKey, callback) {
610 listfunctions.rpoplpush.call(this, MockInstance, sourceKey, destinationKey, callback);
611}
612
613var 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
626RedisClient.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
634RedisClient.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
642RedisClient.prototype.lindex = RedisClient.prototype.LINDEX = function (key, index, callback) {
643 listfunctions.lindex.call(this, MockInstance, key, index, callback);
644}
645
646RedisClient.prototype.lrange = RedisClient.prototype.LRANGE = function (key, index1, index2, callback) {
647 listfunctions.lrange.call(this, MockInstance, key, index1, index2, callback);
648}
649
650RedisClient.prototype.lrem = RedisClient.prototype.LREM = function (key, index, value, callback) {
651 listfunctions.lrem.call(this, MockInstance, key, index, value, callback);
652}
653
654RedisClient.prototype.lset = RedisClient.prototype.LSET = function (key, index, value, callback) {
655 listfunctions.lset.call(this, MockInstance, key, index, value, callback);
656}
657
658RedisClient.prototype.ltrim = RedisClient.prototype.LTRIM = function (key, start, end, callback) {
659 listfunctions.ltrim.call(this, MockInstance, key, start, end, callback);
660}
661
662/**
663 * Set functions
664 */
665var setfunctions = require("./set.js");
666
667var 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
677RedisClient.prototype.sadd = RedisClient.prototype.SADD = function () {
678 setfunctions.sadd.apply(this, [MockInstance].concat(parseArguments(arguments)));
679}
680
681RedisClient.prototype.srem = RedisClient.prototype.SREM = function () {
682 setfunctions.srem.apply(this, [MockInstance].concat(parseArguments(arguments)));
683}
684
685RedisClient.prototype.smembers = RedisClient.prototype.SMEMBERS = function (key, callback) {
686 setfunctions.smembers.call(this, MockInstance, key, callback);
687}
688
689RedisClient.prototype.scard = RedisClient.prototype.SCARD = function (key, callback) {
690 setfunctions.scard.call(this, MockInstance, key, callback);
691}
692
693RedisClient.prototype.sismember = RedisClient.prototype.SISMEMBER = function (key, member, callback) {
694 setfunctions.sismember.call(this, MockInstance, key, member, callback);
695}
696
697RedisClient.prototype.smove = RedisClient.prototype.SMOVE = function (source, destination, member, callback) {
698 setfunctions.smove.call(this, MockInstance, source, destination, member, callback);
699}
700
701RedisClient.prototype.srandmember = RedisClient.prototype.SRANDMEMBER = function (key, count, callback) {
702 setfunctions.srandmember.call(this, MockInstance, key, count, callback);
703}
704
705/**
706 * SortedSet functions
707
708 *** NOT IMPLEMENTED ***
709 ZLEXCOUNT key min max
710 ZRANGEBYLEX key min max [LIMIT offset count]
711 ZREVRANGEBYLEX key max min [LIMIT offset count]
712 ZREMRANGEBYLEX key min max
713 ZSCAN key cursor [MATCH pattern] [COUNT count]
714
715
716 *** PARTIALLY IMPLEMENTED ***
717 ZINTERSTORE - needs [WEIGHTS weight [weight ...]] [AGGREGATE SUM|MIN|MAX]
718 ZUNIONSTORE - needs [WEIGHTS weight [weight ...]] [AGGREGATE SUM|MIN|MAX]
719*/
720var sortedset = require("./sortedset.js");
721
722RedisClient.prototype.zadd = RedisClient.prototype.ZADD = function () {
723
724 var args = parseArguments(arguments);
725 sortedset.zadd.apply(this, [MockInstance].concat(args));
726}
727
728RedisClient.prototype.zcard = RedisClient.prototype.ZCARD = function () {
729
730 var args = parseArguments(arguments);
731 sortedset.zcard.apply(this, [MockInstance].concat(args));
732}
733
734RedisClient.prototype.zcount = RedisClient.prototype.ZCOUNT = function () {
735
736 var args = parseArguments(arguments);
737 sortedset.zcount.apply(this, [MockInstance].concat(args));
738}
739
740RedisClient.prototype.zincrby = RedisClient.prototype.ZINCRBY = function () {
741
742 var args = parseArguments(arguments);
743 sortedset.zincrby.apply(this, [MockInstance].concat(args));
744}
745
746RedisClient.prototype.zrange = RedisClient.prototype.ZRANGE = function () {
747
748 var args = parseArguments(arguments);
749 sortedset.zrange.apply(this, [MockInstance].concat(args));
750}
751
752RedisClient.prototype.zrangebyscore = RedisClient.prototype.ZRANGEBYSCORE = function () {
753
754 var args = parseArguments(arguments);
755 sortedset.zrangebyscore.apply(this, [MockInstance].concat(args));
756}
757
758RedisClient.prototype.zrank = RedisClient.prototype.ZRANK = function () {
759
760 var args = parseArguments(arguments);
761 sortedset.zrank.apply(this, [MockInstance].concat(args));
762}
763
764RedisClient.prototype.zrem = RedisClient.prototype.ZREM = function () {
765
766 var args = parseArguments(arguments);
767 sortedset.zrem.apply(this, [MockInstance].concat(args));
768}
769
770RedisClient.prototype.zremrangebyrank = RedisClient.prototype.ZREMRANGEBYRANK = function () {
771
772 var args = parseArguments(arguments);
773 sortedset.zremrangebyrank.apply(this, [MockInstance].concat(args));
774}
775
776RedisClient.prototype.zremrangebyscore = RedisClient.prototype.ZREMRANGEBYSCORE = function () {
777
778 var args = parseArguments(arguments);
779 sortedset.zremrangebyscore.apply(this, [MockInstance].concat(args));
780}
781
782RedisClient.prototype.zrevrange = RedisClient.prototype.ZREVRANGE = function () {
783
784 var args = parseArguments(arguments);
785 sortedset.zrevrange.apply(this, [MockInstance].concat(args));
786}
787
788RedisClient.prototype.zrevrangebyscore = RedisClient.prototype.ZREVRANGEBYSCORE = function () {
789
790 var args = parseArguments(arguments);
791 sortedset.zrevrangebyscore.apply(this, [MockInstance].concat(args));
792}
793
794RedisClient.prototype.zrevrank = RedisClient.prototype.ZREVRANK = function () {
795
796 var args = parseArguments(arguments);
797 sortedset.zrevrank.apply(this, [MockInstance].concat(args));
798}
799
800RedisClient.prototype.zunionstore = RedisClient.prototype.ZUNIONSTORE = function() {
801 var args = parseArguments(arguments);
802 sortedset.zunionstore.apply(this, [MockInstance].concat(args))
803}
804
805RedisClient.prototype.zinterstore = RedisClient.prototype.ZINTERSTORE = function() {
806 var args = parseArguments(arguments);
807 sortedset.zinterstore.apply(this, [MockInstance].concat(args))
808}
809
810RedisClient.prototype.zscore = RedisClient.prototype.ZSCORE = function () {
811
812 var args = parseArguments(arguments);
813 sortedset.zscore.apply(this, [MockInstance].concat(args));
814}
815
816/**
817 * Other commands (Lua scripts)
818 */
819
820RedisClient.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
826RedisClient.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 * Server functions
838 */
839var serverfunctions = require("./server.js");
840RedisClient.prototype.flushdb = RedisClient.prototype.FLUSHDB = function (callback) {
841
842 serverfunctions.flushdb.call(this, MockInstance, callback);
843}
844RedisClient.prototype.flushall = RedisClient.prototype.FLUSHALL = function (callback) {
845
846 serverfunctions.flushall.call(this, MockInstance, callback);
847}
848
849RedisClient.prototype.auth = RedisClient.prototype.AUTH = function (password, callback) {
850
851 serverfunctions.auth.call(this, MockInstance, password, callback);
852}
853
854RedisMock.prototype.createClient = function (port_arg, host_arg, options) {
855
856 return new RedisClient();
857}