|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +var utils = require('./utils'); |
| 4 | +var debug = require('./debug'); |
| 5 | +var Multi = require('./multi'); |
| 6 | +var no_password_is_set = /no password is set/; |
| 7 | +var RedisClient = require('../').RedisClient; |
| 8 | + |
| 9 | +/******************************** |
| 10 | +Replace built-in redis functions |
| 11 | +********************************/ |
| 12 | + |
| 13 | +RedisClient.prototype.multi = RedisClient.prototype.MULTI = function multi (args) { |
| 14 | + var multi = new Multi(this, args); |
| 15 | + multi.exec = multi.EXEC = multi.exec_transaction; |
| 16 | + return multi; |
| 17 | +}; |
| 18 | + |
| 19 | +// ATTENTION: This is not a native function but is still handled as a individual command as it behaves just the same as multi |
| 20 | +RedisClient.prototype.batch = RedisClient.prototype.BATCH = function batch (args) { |
| 21 | + return new Multi(this, args); |
| 22 | +}; |
| 23 | + |
| 24 | +// Store db in this.select_db to restore it on reconnect |
| 25 | +RedisClient.prototype.select = RedisClient.prototype.SELECT = function select (db, callback) { |
| 26 | + var self = this; |
| 27 | + return this.send_command('select', [db], function (err, res) { |
| 28 | + if (err === null) { |
| 29 | + self.selected_db = db; |
| 30 | + } |
| 31 | + utils.callback_or_emit(self, callback, err, res); |
| 32 | + }); |
| 33 | +}; |
| 34 | + |
| 35 | +// Store info in this.server_info after each call |
| 36 | +RedisClient.prototype.info = RedisClient.prototype.INFO = function info (callback) { |
| 37 | + var self = this; |
| 38 | + var ready = this.ready; |
| 39 | + this.ready = ready || this.offline_queue.length === 0; // keep the execution order intakt |
| 40 | + var tmp = this.send_command('info', [], function (err, res) { |
| 41 | + if (res) { |
| 42 | + var obj = {}; |
| 43 | + var lines = res.toString().split('\r\n'); |
| 44 | + var line, parts, sub_parts; |
| 45 | + |
| 46 | + for (var i = 0; i < lines.length; i++) { |
| 47 | + parts = lines[i].split(':'); |
| 48 | + if (parts[1]) { |
| 49 | + if (parts[0].indexOf('db') === 0) { |
| 50 | + sub_parts = parts[1].split(','); |
| 51 | + obj[parts[0]] = {}; |
| 52 | + while (line = sub_parts.pop()) { |
| 53 | + line = line.split('='); |
| 54 | + obj[parts[0]][line[0]] = +line[1]; |
| 55 | + } |
| 56 | + } else { |
| 57 | + obj[parts[0]] = parts[1]; |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | + obj.versions = []; |
| 62 | + /* istanbul ignore else: some redis servers do not send the version */ |
| 63 | + if (obj.redis_version) { |
| 64 | + obj.redis_version.split('.').forEach(function (num) { |
| 65 | + obj.versions.push(+num); |
| 66 | + }); |
| 67 | + } |
| 68 | + // Expose info key/vals to users |
| 69 | + self.server_info = obj; |
| 70 | + } else { |
| 71 | + self.server_info = {}; |
| 72 | + } |
| 73 | + utils.callback_or_emit(self, callback, err, res); |
| 74 | + }); |
| 75 | + this.ready = ready; |
| 76 | + return tmp; |
| 77 | +}; |
| 78 | + |
| 79 | +RedisClient.prototype.auth = RedisClient.prototype.AUTH = function auth (pass, callback) { |
| 80 | + var self = this; |
| 81 | + var ready = this.ready; |
| 82 | + debug('Sending auth to ' + self.address + ' id ' + self.connection_id); |
| 83 | + |
| 84 | + // Stash auth for connect and reconnect. |
| 85 | + this.auth_pass = pass; |
| 86 | + this.ready = this.offline_queue.length === 0; // keep the execution order intakt |
| 87 | + var tmp = this.send_command('auth', [pass], function (err, res) { |
| 88 | + if (err && no_password_is_set.test(err.message)) { |
| 89 | + self.warn('Warning: Redis server does not require a password, but a password was supplied.'); |
| 90 | + err = null; |
| 91 | + res = 'OK'; |
| 92 | + } |
| 93 | + |
| 94 | + utils.callback_or_emit(self, callback, err, res); |
| 95 | + }); |
| 96 | + this.ready = ready; |
| 97 | + return tmp; |
| 98 | +}; |
| 99 | + |
| 100 | +RedisClient.prototype.hmset = RedisClient.prototype.HMSET = function hmset () { |
| 101 | + var arr, |
| 102 | + len = arguments.length, |
| 103 | + callback, |
| 104 | + i = 0; |
| 105 | + if (Array.isArray(arguments[0])) { |
| 106 | + arr = arguments[0]; |
| 107 | + callback = arguments[1]; |
| 108 | + } else if (Array.isArray(arguments[1])) { |
| 109 | + if (len === 3) { |
| 110 | + callback = arguments[2]; |
| 111 | + } |
| 112 | + len = arguments[1].length; |
| 113 | + arr = new Array(len + 1); |
| 114 | + arr[0] = arguments[0]; |
| 115 | + for (; i < len; i += 1) { |
| 116 | + arr[i + 1] = arguments[1][i]; |
| 117 | + } |
| 118 | + } else if (typeof arguments[1] === 'object' && (arguments.length === 2 || arguments.length === 3 && typeof arguments[2] === 'function' || typeof arguments[2] === 'undefined')) { |
| 119 | + arr = [arguments[0]]; |
| 120 | + for (var field in arguments[1]) { // jshint ignore: line |
| 121 | + arr.push(field, arguments[1][field]); |
| 122 | + } |
| 123 | + callback = arguments[2]; |
| 124 | + } else { |
| 125 | + len = arguments.length; |
| 126 | + // The later should not be the average use case |
| 127 | + if (len !== 0 && (typeof arguments[len - 1] === 'function' || typeof arguments[len - 1] === 'undefined')) { |
| 128 | + len--; |
| 129 | + callback = arguments[len]; |
| 130 | + } |
| 131 | + arr = new Array(len); |
| 132 | + for (; i < len; i += 1) { |
| 133 | + arr[i] = arguments[i]; |
| 134 | + } |
| 135 | + } |
| 136 | + return this.send_command('hmset', arr, callback); |
| 137 | +}; |
0 commit comments