You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

617 lines
22 KiB

4 years ago
  1. 'use strict';
  2. var utils = require('./utils');
  3. var debug = require('./debug');
  4. var Multi = require('./multi');
  5. var Command = require('./command');
  6. var no_password_is_set = /no password is set/;
  7. var loading = /LOADING/;
  8. var RedisClient = require('../').RedisClient;
  9. /********************************************************************************************
  10. Replace built-in redis functions
  11. The callback may be hooked as needed. The same does not apply to the rest of the function.
  12. State should not be set outside of the callback if not absolutly necessary.
  13. This is important to make sure it works the same as single command or in a multi context.
  14. To make sure everything works with the offline queue use the "call_on_write" function.
  15. This is going to be executed while writing to the stream.
  16. TODO: Implement individal command generation as soon as possible to prevent divergent code
  17. on single and multi calls!
  18. ********************************************************************************************/
  19. RedisClient.prototype.multi = RedisClient.prototype.MULTI = function multi (args) {
  20. var multi = new Multi(this, args);
  21. multi.exec = multi.EXEC = multi.exec_transaction;
  22. return multi;
  23. };
  24. // ATTENTION: This is not a native function but is still handled as a individual command as it behaves just the same as multi
  25. RedisClient.prototype.batch = RedisClient.prototype.BATCH = function batch (args) {
  26. return new Multi(this, args);
  27. };
  28. function select_callback (self, db, callback) {
  29. return function (err, res) {
  30. if (err === null) {
  31. // Store db in this.select_db to restore it on reconnect
  32. self.selected_db = db;
  33. }
  34. utils.callback_or_emit(self, callback, err, res);
  35. };
  36. }
  37. RedisClient.prototype.select = RedisClient.prototype.SELECT = function select (db, callback) {
  38. return this.internal_send_command(new Command('select', [db], select_callback(this, db, callback)));
  39. };
  40. Multi.prototype.select = Multi.prototype.SELECT = function select (db, callback) {
  41. this.queue.push(new Command('select', [db], select_callback(this._client, db, callback)));
  42. return this;
  43. };
  44. RedisClient.prototype.monitor = RedisClient.prototype.MONITOR = function monitor (callback) {
  45. // Use a individual command, as this is a special case that does not has to be checked for any other command
  46. var self = this;
  47. var call_on_write = function () {
  48. // Activating monitor mode has to happen before Redis returned the callback. The monitor result is returned first.
  49. // Therefore we expect the command to be properly processed. If this is not the case, it's not an issue either.
  50. self.monitoring = true;
  51. };
  52. return this.internal_send_command(new Command('monitor', [], callback, call_on_write));
  53. };
  54. // Only works with batch, not in a transaction
  55. Multi.prototype.monitor = Multi.prototype.MONITOR = function monitor (callback) {
  56. // Use a individual command, as this is a special case that does not has to be checked for any other command
  57. if (this.exec !== this.exec_transaction) {
  58. var self = this;
  59. var call_on_write = function () {
  60. self._client.monitoring = true;
  61. };
  62. this.queue.push(new Command('monitor', [], callback, call_on_write));
  63. return this;
  64. }
  65. // Set multi monitoring to indicate the exec that it should abort
  66. // Remove this "hack" as soon as Redis might fix this
  67. this.monitoring = true;
  68. return this;
  69. };
  70. function quit_callback (self, callback) {
  71. return function (err, res) {
  72. if (err && err.code === 'NR_CLOSED') {
  73. // Pretent the quit command worked properly in this case.
  74. // Either the quit landed in the offline queue and was flushed at the reconnect
  75. // or the offline queue is deactivated and the command was rejected right away
  76. // or the stream is not writable
  77. // or while sending the quit, the connection ended / closed
  78. err = null;
  79. res = 'OK';
  80. }
  81. utils.callback_or_emit(self, callback, err, res);
  82. if (self.stream.writable) {
  83. // If the socket is still alive, kill it. This could happen if quit got a NR_CLOSED error code
  84. self.stream.destroy();
  85. }
  86. };
  87. }
  88. RedisClient.prototype.QUIT = RedisClient.prototype.quit = function quit (callback) {
  89. // TODO: Consider this for v.3
  90. // Allow the quit command to be fired as soon as possible to prevent it landing in the offline queue.
  91. // this.ready = this.offline_queue.length === 0;
  92. var backpressure_indicator = this.internal_send_command(new Command('quit', [], quit_callback(this, callback)));
  93. // Calling quit should always end the connection, no matter if there's a connection or not
  94. this.closing = true;
  95. this.ready = false;
  96. return backpressure_indicator;
  97. };
  98. // Only works with batch, not in a transaction
  99. Multi.prototype.QUIT = Multi.prototype.quit = function quit (callback) {
  100. var self = this._client;
  101. var call_on_write = function () {
  102. // If called in a multi context, we expect redis is available
  103. self.closing = true;
  104. self.ready = false;
  105. };
  106. this.queue.push(new Command('quit', [], quit_callback(self, callback), call_on_write));
  107. return this;
  108. };
  109. function info_callback (self, callback) {
  110. return function (err, res) {
  111. if (res) {
  112. var obj = {};
  113. var lines = res.toString().split('\r\n');
  114. var line, parts, sub_parts;
  115. for (var i = 0; i < lines.length; i++) {
  116. parts = lines[i].split(':');
  117. if (parts[1]) {
  118. if (parts[0].indexOf('db') === 0) {
  119. sub_parts = parts[1].split(',');
  120. obj[parts[0]] = {};
  121. while (line = sub_parts.pop()) {
  122. line = line.split('=');
  123. obj[parts[0]][line[0]] = +line[1];
  124. }
  125. } else {
  126. obj[parts[0]] = parts[1];
  127. }
  128. }
  129. }
  130. obj.versions = [];
  131. if (obj.redis_version) {
  132. obj.redis_version.split('.').forEach(function (num) {
  133. obj.versions.push(+num);
  134. });
  135. }
  136. // Expose info key/vals to users
  137. self.server_info = obj;
  138. } else {
  139. self.server_info = {};
  140. }
  141. utils.callback_or_emit(self, callback, err, res);
  142. };
  143. }
  144. // Store info in this.server_info after each call
  145. RedisClient.prototype.info = RedisClient.prototype.INFO = function info (section, callback) {
  146. var args = [];
  147. if (typeof section === 'function') {
  148. callback = section;
  149. } else if (section !== undefined) {
  150. args = Array.isArray(section) ? section : [section];
  151. }
  152. return this.internal_send_command(new Command('info', args, info_callback(this, callback)));
  153. };
  154. Multi.prototype.info = Multi.prototype.INFO = function info (section, callback) {
  155. var args = [];
  156. if (typeof section === 'function') {
  157. callback = section;
  158. } else if (section !== undefined) {
  159. args = Array.isArray(section) ? section : [section];
  160. }
  161. this.queue.push(new Command('info', args, info_callback(this._client, callback)));
  162. return this;
  163. };
  164. function auth_callback (self, pass, callback) {
  165. return function (err, res) {
  166. if (err) {
  167. if (no_password_is_set.test(err.message)) {
  168. self.warn('Warning: Redis server does not require a password, but a password was supplied.');
  169. err = null;
  170. res = 'OK';
  171. } else if (loading.test(err.message)) {
  172. // If redis is still loading the db, it will not authenticate and everything else will fail
  173. debug('Redis still loading, trying to authenticate later');
  174. setTimeout(function () {
  175. self.auth(pass, callback);
  176. }, 100);
  177. return;
  178. }
  179. }
  180. utils.callback_or_emit(self, callback, err, res);
  181. };
  182. }
  183. RedisClient.prototype.auth = RedisClient.prototype.AUTH = function auth (pass, callback) {
  184. debug('Sending auth to ' + this.address + ' id ' + this.connection_id);
  185. // Stash auth for connect and reconnect.
  186. this.auth_pass = pass;
  187. var ready = this.ready;
  188. this.ready = ready || this.offline_queue.length === 0;
  189. var tmp = this.internal_send_command(new Command('auth', [pass], auth_callback(this, pass, callback)));
  190. this.ready = ready;
  191. return tmp;
  192. };
  193. // Only works with batch, not in a transaction
  194. Multi.prototype.auth = Multi.prototype.AUTH = function auth (pass, callback) {
  195. debug('Sending auth to ' + this.address + ' id ' + this.connection_id);
  196. // Stash auth for connect and reconnect.
  197. this.auth_pass = pass;
  198. this.queue.push(new Command('auth', [pass], auth_callback(this._client, callback)));
  199. return this;
  200. };
  201. RedisClient.prototype.client = RedisClient.prototype.CLIENT = function client () {
  202. var arr,
  203. len = arguments.length,
  204. callback,
  205. i = 0;
  206. if (Array.isArray(arguments[0])) {
  207. arr = arguments[0];
  208. callback = arguments[1];
  209. } else if (Array.isArray(arguments[1])) {
  210. if (len === 3) {
  211. callback = arguments[2];
  212. }
  213. len = arguments[1].length;
  214. arr = new Array(len + 1);
  215. arr[0] = arguments[0];
  216. for (; i < len; i += 1) {
  217. arr[i + 1] = arguments[1][i];
  218. }
  219. } else {
  220. len = arguments.length;
  221. // The later should not be the average use case
  222. if (len !== 0 && (typeof arguments[len - 1] === 'function' || typeof arguments[len - 1] === 'undefined')) {
  223. len--;
  224. callback = arguments[len];
  225. }
  226. arr = new Array(len);
  227. for (; i < len; i += 1) {
  228. arr[i] = arguments[i];
  229. }
  230. }
  231. var self = this;
  232. var call_on_write = undefined;
  233. // CLIENT REPLY ON|OFF|SKIP
  234. /* istanbul ignore next: TODO: Remove this as soon as Travis runs Redis 3.2 */
  235. if (arr.length === 2 && arr[0].toString().toUpperCase() === 'REPLY') {
  236. var reply_on_off = arr[1].toString().toUpperCase();
  237. if (reply_on_off === 'ON' || reply_on_off === 'OFF' || reply_on_off === 'SKIP') {
  238. call_on_write = function () {
  239. self.reply = reply_on_off;
  240. };
  241. }
  242. }
  243. return this.internal_send_command(new Command('client', arr, callback, call_on_write));
  244. };
  245. Multi.prototype.client = Multi.prototype.CLIENT = function client () {
  246. var arr,
  247. len = arguments.length,
  248. callback,
  249. i = 0;
  250. if (Array.isArray(arguments[0])) {
  251. arr = arguments[0];
  252. callback = arguments[1];
  253. } else if (Array.isArray(arguments[1])) {
  254. if (len === 3) {
  255. callback = arguments[2];
  256. }
  257. len = arguments[1].length;
  258. arr = new Array(len + 1);
  259. arr[0] = arguments[0];
  260. for (; i < len; i += 1) {
  261. arr[i + 1] = arguments[1][i];
  262. }
  263. } else {
  264. len = arguments.length;
  265. // The later should not be the average use case
  266. if (len !== 0 && (typeof arguments[len - 1] === 'function' || typeof arguments[len - 1] === 'undefined')) {
  267. len--;
  268. callback = arguments[len];
  269. }
  270. arr = new Array(len);
  271. for (; i < len; i += 1) {
  272. arr[i] = arguments[i];
  273. }
  274. }
  275. var self = this._client;
  276. var call_on_write = undefined;
  277. // CLIENT REPLY ON|OFF|SKIP
  278. /* istanbul ignore next: TODO: Remove this as soon as Travis runs Redis 3.2 */
  279. if (arr.length === 2 && arr[0].toString().toUpperCase() === 'REPLY') {
  280. var reply_on_off = arr[1].toString().toUpperCase();
  281. if (reply_on_off === 'ON' || reply_on_off === 'OFF' || reply_on_off === 'SKIP') {
  282. call_on_write = function () {
  283. self.reply = reply_on_off;
  284. };
  285. }
  286. }
  287. this.queue.push(new Command('client', arr, callback, call_on_write));
  288. return this;
  289. };
  290. RedisClient.prototype.hmset = RedisClient.prototype.HMSET = function hmset () {
  291. var arr,
  292. len = arguments.length,
  293. callback,
  294. i = 0;
  295. if (Array.isArray(arguments[0])) {
  296. arr = arguments[0];
  297. callback = arguments[1];
  298. } else if (Array.isArray(arguments[1])) {
  299. if (len === 3) {
  300. callback = arguments[2];
  301. }
  302. len = arguments[1].length;
  303. arr = new Array(len + 1);
  304. arr[0] = arguments[0];
  305. for (; i < len; i += 1) {
  306. arr[i + 1] = arguments[1][i];
  307. }
  308. } else if (typeof arguments[1] === 'object' && (arguments.length === 2 || arguments.length === 3 && (typeof arguments[2] === 'function' || typeof arguments[2] === 'undefined'))) {
  309. arr = [arguments[0]];
  310. for (var field in arguments[1]) {
  311. arr.push(field, arguments[1][field]);
  312. }
  313. callback = arguments[2];
  314. } else {
  315. len = arguments.length;
  316. // The later should not be the average use case
  317. if (len !== 0 && (typeof arguments[len - 1] === 'function' || typeof arguments[len - 1] === 'undefined')) {
  318. len--;
  319. callback = arguments[len];
  320. }
  321. arr = new Array(len);
  322. for (; i < len; i += 1) {
  323. arr[i] = arguments[i];
  324. }
  325. }
  326. return this.internal_send_command(new Command('hmset', arr, callback));
  327. };
  328. Multi.prototype.hmset = Multi.prototype.HMSET = function hmset () {
  329. var arr,
  330. len = arguments.length,
  331. callback,
  332. i = 0;
  333. if (Array.isArray(arguments[0])) {
  334. arr = arguments[0];
  335. callback = arguments[1];
  336. } else if (Array.isArray(arguments[1])) {
  337. if (len === 3) {
  338. callback = arguments[2];
  339. }
  340. len = arguments[1].length;
  341. arr = new Array(len + 1);
  342. arr[0] = arguments[0];
  343. for (; i < len; i += 1) {
  344. arr[i + 1] = arguments[1][i];
  345. }
  346. } else if (typeof arguments[1] === 'object' && (arguments.length === 2 || arguments.length === 3 && (typeof arguments[2] === 'function' || typeof arguments[2] === 'undefined'))) {
  347. arr = [arguments[0]];
  348. for (var field in arguments[1]) {
  349. arr.push(field, arguments[1][field]);
  350. }
  351. callback = arguments[2];
  352. } else {
  353. len = arguments.length;
  354. // The later should not be the average use case
  355. if (len !== 0 && (typeof arguments[len - 1] === 'function' || typeof arguments[len - 1] === 'undefined')) {
  356. len--;
  357. callback = arguments[len];
  358. }
  359. arr = new Array(len);
  360. for (; i < len; i += 1) {
  361. arr[i] = arguments[i];
  362. }
  363. }
  364. this.queue.push(new Command('hmset', arr, callback));
  365. return this;
  366. };
  367. RedisClient.prototype.subscribe = RedisClient.prototype.SUBSCRIBE = function subscribe () {
  368. var arr,
  369. len = arguments.length,
  370. callback,
  371. i = 0;
  372. if (Array.isArray(arguments[0])) {
  373. arr = arguments[0].slice(0);
  374. callback = arguments[1];
  375. } else {
  376. len = arguments.length;
  377. // The later should not be the average use case
  378. if (len !== 0 && (typeof arguments[len - 1] === 'function' || typeof arguments[len - 1] === 'undefined')) {
  379. len--;
  380. callback = arguments[len];
  381. }
  382. arr = new Array(len);
  383. for (; i < len; i += 1) {
  384. arr[i] = arguments[i];
  385. }
  386. }
  387. var self = this;
  388. var call_on_write = function () {
  389. self.pub_sub_mode = self.pub_sub_mode || self.command_queue.length + 1;
  390. };
  391. return this.internal_send_command(new Command('subscribe', arr, callback, call_on_write));
  392. };
  393. Multi.prototype.subscribe = Multi.prototype.SUBSCRIBE = function subscribe () {
  394. var arr,
  395. len = arguments.length,
  396. callback,
  397. i = 0;
  398. if (Array.isArray(arguments[0])) {
  399. arr = arguments[0].slice(0);
  400. callback = arguments[1];
  401. } else {
  402. len = arguments.length;
  403. // The later should not be the average use case
  404. if (len !== 0 && (typeof arguments[len - 1] === 'function' || typeof arguments[len - 1] === 'undefined')) {
  405. len--;
  406. callback = arguments[len];
  407. }
  408. arr = new Array(len);
  409. for (; i < len; i += 1) {
  410. arr[i] = arguments[i];
  411. }
  412. }
  413. var self = this._client;
  414. var call_on_write = function () {
  415. self.pub_sub_mode = self.pub_sub_mode || self.command_queue.length + 1;
  416. };
  417. this.queue.push(new Command('subscribe', arr, callback, call_on_write));
  418. return this;
  419. };
  420. RedisClient.prototype.unsubscribe = RedisClient.prototype.UNSUBSCRIBE = function unsubscribe () {
  421. var arr,
  422. len = arguments.length,
  423. callback,
  424. i = 0;
  425. if (Array.isArray(arguments[0])) {
  426. arr = arguments[0].slice(0);
  427. callback = arguments[1];
  428. } else {
  429. len = arguments.length;
  430. // The later should not be the average use case
  431. if (len !== 0 && (typeof arguments[len - 1] === 'function' || typeof arguments[len - 1] === 'undefined')) {
  432. len--;
  433. callback = arguments[len];
  434. }
  435. arr = new Array(len);
  436. for (; i < len; i += 1) {
  437. arr[i] = arguments[i];
  438. }
  439. }
  440. var self = this;
  441. var call_on_write = function () {
  442. // Pub sub has to be activated even if not in pub sub mode, as the return value is manipulated in the callback
  443. self.pub_sub_mode = self.pub_sub_mode || self.command_queue.length + 1;
  444. };
  445. return this.internal_send_command(new Command('unsubscribe', arr, callback, call_on_write));
  446. };
  447. Multi.prototype.unsubscribe = Multi.prototype.UNSUBSCRIBE = function unsubscribe () {
  448. var arr,
  449. len = arguments.length,
  450. callback,
  451. i = 0;
  452. if (Array.isArray(arguments[0])) {
  453. arr = arguments[0].slice(0);
  454. callback = arguments[1];
  455. } else {
  456. len = arguments.length;
  457. // The later should not be the average use case
  458. if (len !== 0 && (typeof arguments[len - 1] === 'function' || typeof arguments[len - 1] === 'undefined')) {
  459. len--;
  460. callback = arguments[len];
  461. }
  462. arr = new Array(len);
  463. for (; i < len; i += 1) {
  464. arr[i] = arguments[i];
  465. }
  466. }
  467. var self = this._client;
  468. var call_on_write = function () {
  469. // Pub sub has to be activated even if not in pub sub mode, as the return value is manipulated in the callback
  470. self.pub_sub_mode = self.pub_sub_mode || self.command_queue.length + 1;
  471. };
  472. this.queue.push(new Command('unsubscribe', arr, callback, call_on_write));
  473. return this;
  474. };
  475. RedisClient.prototype.psubscribe = RedisClient.prototype.PSUBSCRIBE = function psubscribe () {
  476. var arr,
  477. len = arguments.length,
  478. callback,
  479. i = 0;
  480. if (Array.isArray(arguments[0])) {
  481. arr = arguments[0].slice(0);
  482. callback = arguments[1];
  483. } else {
  484. len = arguments.length;
  485. // The later should not be the average use case
  486. if (len !== 0 && (typeof arguments[len - 1] === 'function' || typeof arguments[len - 1] === 'undefined')) {
  487. len--;
  488. callback = arguments[len];
  489. }
  490. arr = new Array(len);
  491. for (; i < len; i += 1) {
  492. arr[i] = arguments[i];
  493. }
  494. }
  495. var self = this;
  496. var call_on_write = function () {
  497. self.pub_sub_mode = self.pub_sub_mode || self.command_queue.length + 1;
  498. };
  499. return this.internal_send_command(new Command('psubscribe', arr, callback, call_on_write));
  500. };
  501. Multi.prototype.psubscribe = Multi.prototype.PSUBSCRIBE = function psubscribe () {
  502. var arr,
  503. len = arguments.length,
  504. callback,
  505. i = 0;
  506. if (Array.isArray(arguments[0])) {
  507. arr = arguments[0].slice(0);
  508. callback = arguments[1];
  509. } else {
  510. len = arguments.length;
  511. // The later should not be the average use case
  512. if (len !== 0 && (typeof arguments[len - 1] === 'function' || typeof arguments[len - 1] === 'undefined')) {
  513. len--;
  514. callback = arguments[len];
  515. }
  516. arr = new Array(len);
  517. for (; i < len; i += 1) {
  518. arr[i] = arguments[i];
  519. }
  520. }
  521. var self = this._client;
  522. var call_on_write = function () {
  523. self.pub_sub_mode = self.pub_sub_mode || self.command_queue.length + 1;
  524. };
  525. this.queue.push(new Command('psubscribe', arr, callback, call_on_write));
  526. return this;
  527. };
  528. RedisClient.prototype.punsubscribe = RedisClient.prototype.PUNSUBSCRIBE = function punsubscribe () {
  529. var arr,
  530. len = arguments.length,
  531. callback,
  532. i = 0;
  533. if (Array.isArray(arguments[0])) {
  534. arr = arguments[0].slice(0);
  535. callback = arguments[1];
  536. } else {
  537. len = arguments.length;
  538. // The later should not be the average use case
  539. if (len !== 0 && (typeof arguments[len - 1] === 'function' || typeof arguments[len - 1] === 'undefined')) {
  540. len--;
  541. callback = arguments[len];
  542. }
  543. arr = new Array(len);
  544. for (; i < len; i += 1) {
  545. arr[i] = arguments[i];
  546. }
  547. }
  548. var self = this;
  549. var call_on_write = function () {
  550. // Pub sub has to be activated even if not in pub sub mode, as the return value is manipulated in the callback
  551. self.pub_sub_mode = self.pub_sub_mode || self.command_queue.length + 1;
  552. };
  553. return this.internal_send_command(new Command('punsubscribe', arr, callback, call_on_write));
  554. };
  555. Multi.prototype.punsubscribe = Multi.prototype.PUNSUBSCRIBE = function punsubscribe () {
  556. var arr,
  557. len = arguments.length,
  558. callback,
  559. i = 0;
  560. if (Array.isArray(arguments[0])) {
  561. arr = arguments[0].slice(0);
  562. callback = arguments[1];
  563. } else {
  564. len = arguments.length;
  565. // The later should not be the average use case
  566. if (len !== 0 && (typeof arguments[len - 1] === 'function' || typeof arguments[len - 1] === 'undefined')) {
  567. len--;
  568. callback = arguments[len];
  569. }
  570. arr = new Array(len);
  571. for (; i < len; i += 1) {
  572. arr[i] = arguments[i];
  573. }
  574. }
  575. var self = this._client;
  576. var call_on_write = function () {
  577. // Pub sub has to be activated even if not in pub sub mode, as the return value is manipulated in the callback
  578. self.pub_sub_mode = self.pub_sub_mode || self.command_queue.length + 1;
  579. };
  580. this.queue.push(new Command('punsubscribe', arr, callback, call_on_write));
  581. return this;
  582. };