index.js 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105
  1. 'use strict';
  2. var net = require('net');
  3. var tls = require('tls');
  4. var util = require('util');
  5. var utils = require('./lib/utils');
  6. var Command = require('./lib/command');
  7. var Queue = require('double-ended-queue');
  8. var errorClasses = require('./lib/customErrors');
  9. var EventEmitter = require('events');
  10. var Parser = require('redis-parser');
  11. var commands = require('redis-commands');
  12. var debug = require('./lib/debug');
  13. var unifyOptions = require('./lib/createClient');
  14. var SUBSCRIBE_COMMANDS = {
  15. subscribe: true,
  16. unsubscribe: true,
  17. psubscribe: true,
  18. punsubscribe: true
  19. };
  20. // Newer Node.js versions > 0.10 return the EventEmitter right away and using .EventEmitter was deprecated
  21. if (typeof EventEmitter !== 'function') {
  22. EventEmitter = EventEmitter.EventEmitter;
  23. }
  24. function noop () {}
  25. function handle_detect_buffers_reply (reply, command, buffer_args) {
  26. if (buffer_args === false || this.message_buffers) {
  27. // If detect_buffers option was specified, then the reply from the parser will be a buffer.
  28. // If this command did not use Buffer arguments, then convert the reply to Strings here.
  29. reply = utils.reply_to_strings(reply);
  30. }
  31. if (command === 'hgetall') {
  32. reply = utils.reply_to_object(reply);
  33. }
  34. return reply;
  35. }
  36. exports.debug_mode = /\bredis\b/i.test(process.env.NODE_DEBUG);
  37. // Attention: The second parameter might be removed at will and is not officially supported.
  38. // Do not rely on this
  39. function RedisClient (options, stream) {
  40. // Copy the options so they are not mutated
  41. options = utils.clone(options);
  42. EventEmitter.call(this);
  43. var cnx_options = {};
  44. var self = this;
  45. /* istanbul ignore next: travis does not work with stunnel atm. Therefore the tls tests are skipped on travis */
  46. for (var tls_option in options.tls) {
  47. cnx_options[tls_option] = options.tls[tls_option];
  48. // Copy the tls options into the general options to make sure the address is set right
  49. if (tls_option === 'port' || tls_option === 'host' || tls_option === 'path' || tls_option === 'family') {
  50. options[tls_option] = options.tls[tls_option];
  51. }
  52. }
  53. if (stream) {
  54. // The stream from the outside is used so no connection from this side is triggered but from the server this client should talk to
  55. // Reconnect etc won't work with this. This requires monkey patching to work, so it is not officially supported
  56. options.stream = stream;
  57. this.address = '"Private stream"';
  58. } else if (options.path) {
  59. cnx_options.path = options.path;
  60. this.address = options.path;
  61. } else {
  62. cnx_options.port = +options.port || 6379;
  63. cnx_options.host = options.host || '127.0.0.1';
  64. cnx_options.family = (!options.family && net.isIP(cnx_options.host)) || (options.family === 'IPv6' ? 6 : 4);
  65. this.address = cnx_options.host + ':' + cnx_options.port;
  66. }
  67. // Warn on misusing deprecated functions
  68. if (typeof options.retry_strategy === 'function') {
  69. if ('max_attempts' in options) {
  70. self.warn('WARNING: You activated the retry_strategy and max_attempts at the same time. This is not possible and max_attempts will be ignored.');
  71. // Do not print deprecation warnings twice
  72. delete options.max_attempts;
  73. }
  74. if ('retry_max_delay' in options) {
  75. self.warn('WARNING: You activated the retry_strategy and retry_max_delay at the same time. This is not possible and retry_max_delay will be ignored.');
  76. // Do not print deprecation warnings twice
  77. delete options.retry_max_delay;
  78. }
  79. }
  80. this.connection_options = cnx_options;
  81. this.connection_id = RedisClient.connection_id++;
  82. this.connected = false;
  83. this.ready = false;
  84. if (options.socket_nodelay === undefined) {
  85. options.socket_nodelay = true;
  86. } else if (!options.socket_nodelay) { // Only warn users with this set to false
  87. self.warn(
  88. 'socket_nodelay is deprecated and will be removed in v.3.0.0.\n' +
  89. 'Setting socket_nodelay to false likely results in a reduced throughput. Please use .batch for pipelining instead.\n' +
  90. 'If you are sure you rely on the NAGLE-algorithm you can activate it by calling client.stream.setNoDelay(false) instead.'
  91. );
  92. }
  93. if (options.socket_keepalive === undefined) {
  94. options.socket_keepalive = true;
  95. }
  96. for (var command in options.rename_commands) {
  97. options.rename_commands[command.toLowerCase()] = options.rename_commands[command];
  98. }
  99. options.return_buffers = !!options.return_buffers;
  100. options.detect_buffers = !!options.detect_buffers;
  101. // Override the detect_buffers setting if return_buffers is active and print a warning
  102. if (options.return_buffers && options.detect_buffers) {
  103. self.warn('WARNING: You activated return_buffers and detect_buffers at the same time. The return value is always going to be a buffer.');
  104. options.detect_buffers = false;
  105. }
  106. if (options.detect_buffers) {
  107. // We only need to look at the arguments if we do not know what we have to return
  108. this.handle_reply = handle_detect_buffers_reply;
  109. }
  110. this.should_buffer = false;
  111. this.max_attempts = options.max_attempts | 0;
  112. if ('max_attempts' in options) {
  113. self.warn(
  114. 'max_attempts is deprecated and will be removed in v.3.0.0.\n' +
  115. 'To reduce the number of options and to improve the reconnection handling please use the new `retry_strategy` option instead.\n' +
  116. 'This replaces the max_attempts and retry_max_delay option.'
  117. );
  118. }
  119. this.command_queue = new Queue(); // Holds sent commands to de-pipeline them
  120. this.offline_queue = new Queue(); // Holds commands issued but not able to be sent
  121. this.pipeline_queue = new Queue(); // Holds all pipelined commands
  122. // ATTENTION: connect_timeout should change in v.3.0 so it does not count towards ending reconnection attempts after x seconds
  123. // This should be done by the retry_strategy. Instead it should only be the timeout for connecting to redis
  124. this.connect_timeout = +options.connect_timeout || 3600000; // 60 * 60 * 1000 ms
  125. this.enable_offline_queue = options.enable_offline_queue === false ? false : true;
  126. this.retry_max_delay = +options.retry_max_delay || null;
  127. if ('retry_max_delay' in options) {
  128. self.warn(
  129. 'retry_max_delay is deprecated and will be removed in v.3.0.0.\n' +
  130. 'To reduce the amount of options and the improve the reconnection handling please use the new `retry_strategy` option instead.\n' +
  131. 'This replaces the max_attempts and retry_max_delay option.'
  132. );
  133. }
  134. this.initialize_retry_vars();
  135. this.pub_sub_mode = 0;
  136. this.subscription_set = {};
  137. this.monitoring = false;
  138. this.message_buffers = false;
  139. this.closing = false;
  140. this.server_info = {};
  141. this.auth_pass = options.auth_pass || options.password;
  142. this.selected_db = options.db; // Save the selected db here, used when reconnecting
  143. this.old_state = null;
  144. this.fire_strings = true; // Determine if strings or buffers should be written to the stream
  145. this.pipeline = false;
  146. this.sub_commands_left = 0;
  147. this.times_connected = 0;
  148. this.buffers = options.return_buffers || options.detect_buffers;
  149. this.options = options;
  150. this.reply = 'ON'; // Returning replies is the default
  151. this.create_stream();
  152. // The listeners will not be attached right away, so let's print the deprecation message while the listener is attached
  153. this.on('newListener', function (event) {
  154. if (event === 'idle') {
  155. this.warn(
  156. 'The idle event listener is deprecated and will likely be removed in v.3.0.0.\n' +
  157. 'If you rely on this feature please open a new ticket in node_redis with your use case'
  158. );
  159. } else if (event === 'drain') {
  160. this.warn(
  161. 'The drain event listener is deprecated and will be removed in v.3.0.0.\n' +
  162. 'If you want to keep on listening to this event please listen to the stream drain event directly.'
  163. );
  164. } else if ((event === 'message_buffer' || event === 'pmessage_buffer' || event === 'messageBuffer' || event === 'pmessageBuffer') && !this.buffers && !this.message_buffers) {
  165. if (this.reply_parser.name !== 'javascript') {
  166. return this.warn(
  167. 'You attached the "' + event + '" listener without the returnBuffers option set to true.\n' +
  168. 'Please use the JavaScript parser or set the returnBuffers option to true to return buffers.'
  169. );
  170. }
  171. this.reply_parser.optionReturnBuffers = true;
  172. this.message_buffers = true;
  173. this.handle_reply = handle_detect_buffers_reply;
  174. }
  175. });
  176. }
  177. util.inherits(RedisClient, EventEmitter);
  178. RedisClient.connection_id = 0;
  179. function create_parser (self) {
  180. return new Parser({
  181. returnReply: function (data) {
  182. self.return_reply(data);
  183. },
  184. returnError: function (err) {
  185. // Return a ReplyError to indicate Redis returned an error
  186. self.return_error(err);
  187. },
  188. returnFatalError: function (err) {
  189. // Error out all fired commands. Otherwise they might rely on faulty data. We have to reconnect to get in a working state again
  190. // Note: the execution order is important. First flush and emit, then create the stream
  191. err.message += '. Please report this.';
  192. self.ready = false;
  193. self.flush_and_error({
  194. message: 'Fatal error encountert. Command aborted.',
  195. code: 'NR_FATAL'
  196. }, {
  197. error: err,
  198. queues: ['command_queue']
  199. });
  200. self.emit('error', err);
  201. self.create_stream();
  202. },
  203. returnBuffers: self.buffers || self.message_buffers,
  204. name: self.options.parser || 'javascript',
  205. stringNumbers: self.options.string_numbers || false
  206. });
  207. }
  208. /******************************************************************************
  209. All functions in here are internal besides the RedisClient constructor
  210. and the exported functions. Don't rely on them as they will be private
  211. functions in node_redis v.3
  212. ******************************************************************************/
  213. // Attention: the function name "create_stream" should not be changed, as other libraries need this to mock the stream (e.g. fakeredis)
  214. RedisClient.prototype.create_stream = function () {
  215. var self = this;
  216. // Init parser
  217. this.reply_parser = create_parser(this);
  218. if (this.options.stream) {
  219. // Only add the listeners once in case of a reconnect try (that won't work)
  220. if (this.stream) {
  221. return;
  222. }
  223. this.stream = this.options.stream;
  224. } else {
  225. // On a reconnect destroy the former stream and retry
  226. if (this.stream) {
  227. this.stream.removeAllListeners();
  228. this.stream.destroy();
  229. }
  230. /* istanbul ignore if: travis does not work with stunnel atm. Therefore the tls tests are skipped on travis */
  231. if (this.options.tls) {
  232. this.stream = tls.connect(this.connection_options);
  233. } else {
  234. this.stream = net.createConnection(this.connection_options);
  235. }
  236. }
  237. if (this.options.connect_timeout) {
  238. this.stream.setTimeout(this.connect_timeout, function () {
  239. // Note: This is only tested if a internet connection is established
  240. self.retry_totaltime = self.connect_timeout;
  241. self.connection_gone('timeout');
  242. });
  243. }
  244. /* istanbul ignore next: travis does not work with stunnel atm. Therefore the tls tests are skipped on travis */
  245. var connect_event = this.options.tls ? 'secureConnect' : 'connect';
  246. this.stream.once(connect_event, function () {
  247. this.removeAllListeners('timeout');
  248. self.times_connected++;
  249. self.on_connect();
  250. });
  251. this.stream.on('data', function (buffer_from_socket) {
  252. // The buffer_from_socket.toString() has a significant impact on big chunks and therefore this should only be used if necessary
  253. debug('Net read ' + self.address + ' id ' + self.connection_id); // + ': ' + buffer_from_socket.toString());
  254. self.reply_parser.execute(buffer_from_socket);
  255. self.emit_idle();
  256. });
  257. this.stream.on('error', function (err) {
  258. self.on_error(err);
  259. });
  260. /* istanbul ignore next: difficult to test and not important as long as we keep this listener */
  261. this.stream.on('clientError', function (err) {
  262. debug('clientError occured');
  263. self.on_error(err);
  264. });
  265. this.stream.once('close', function (hadError) {
  266. self.connection_gone('close');
  267. });
  268. this.stream.once('end', function () {
  269. self.connection_gone('end');
  270. });
  271. this.stream.on('drain', function () {
  272. self.drain();
  273. });
  274. if (this.options.socket_nodelay) {
  275. this.stream.setNoDelay();
  276. }
  277. // Fire the command before redis is connected to be sure it's the first fired command
  278. if (this.auth_pass !== undefined) {
  279. this.ready = true;
  280. // Fail silently as we might not be able to connect
  281. this.auth(this.auth_pass, function (err) {
  282. if (err && err.code !== 'UNCERTAIN_STATE') {
  283. self.emit('error', err);
  284. }
  285. });
  286. this.ready = false;
  287. }
  288. };
  289. RedisClient.prototype.handle_reply = function (reply, command) {
  290. if (command === 'hgetall') {
  291. reply = utils.reply_to_object(reply);
  292. }
  293. return reply;
  294. };
  295. RedisClient.prototype.cork = noop;
  296. RedisClient.prototype.uncork = noop;
  297. RedisClient.prototype.initialize_retry_vars = function () {
  298. this.retry_timer = null;
  299. this.retry_totaltime = 0;
  300. this.retry_delay = 200;
  301. this.retry_backoff = 1.7;
  302. this.attempts = 1;
  303. };
  304. RedisClient.prototype.warn = function (msg) {
  305. var self = this;
  306. // Warn on the next tick. Otherwise no event listener can be added
  307. // for warnings that are emitted in the redis client constructor
  308. process.nextTick(function () {
  309. if (self.listeners('warning').length !== 0) {
  310. self.emit('warning', msg);
  311. } else {
  312. console.warn('node_redis:', msg);
  313. }
  314. });
  315. };
  316. // Flush provided queues, erroring any items with a callback first
  317. RedisClient.prototype.flush_and_error = function (error_attributes, options) {
  318. options = options || {};
  319. var aggregated_errors = [];
  320. var queue_names = options.queues || ['command_queue', 'offline_queue']; // Flush the command_queue first to keep the order intakt
  321. for (var i = 0; i < queue_names.length; i++) {
  322. // If the command was fired it might have been processed so far
  323. if (queue_names[i] === 'command_queue') {
  324. error_attributes.message += ' It might have been processed.';
  325. } else { // As the command_queue is flushed first, remove this for the offline queue
  326. error_attributes.message = error_attributes.message.replace(' It might have been processed.', '');
  327. }
  328. // Don't flush everything from the queue
  329. for (var command_obj = this[queue_names[i]].shift(); command_obj; command_obj = this[queue_names[i]].shift()) {
  330. var err = new errorClasses.AbortError(error_attributes);
  331. if (command_obj.error) {
  332. err.stack = err.stack + command_obj.error.stack.replace(/^Error.*?\n/, '\n');
  333. }
  334. err.command = command_obj.command.toUpperCase();
  335. if (command_obj.args && command_obj.args.length) {
  336. err.args = command_obj.args;
  337. }
  338. if (options.error) {
  339. err.origin = options.error;
  340. }
  341. if (typeof command_obj.callback === 'function') {
  342. command_obj.callback(err);
  343. } else {
  344. aggregated_errors.push(err);
  345. }
  346. }
  347. }
  348. // Currently this would be a breaking change, therefore it's only emitted in debug_mode
  349. if (exports.debug_mode && aggregated_errors.length) {
  350. var error;
  351. if (aggregated_errors.length === 1) {
  352. error = aggregated_errors[0];
  353. } else {
  354. error_attributes.message = error_attributes.message.replace('It', 'They').replace(/command/i, '$&s');
  355. error = new errorClasses.AggregateError(error_attributes);
  356. error.errors = aggregated_errors;
  357. }
  358. this.emit('error', error);
  359. }
  360. };
  361. RedisClient.prototype.on_error = function (err) {
  362. if (this.closing) {
  363. return;
  364. }
  365. err.message = 'Redis connection to ' + this.address + ' failed - ' + err.message;
  366. debug(err.message);
  367. this.connected = false;
  368. this.ready = false;
  369. // Only emit the error if the retry_stategy option is not set
  370. if (!this.options.retry_strategy) {
  371. this.emit('error', err);
  372. }
  373. // 'error' events get turned into exceptions if they aren't listened for. If the user handled this error
  374. // then we should try to reconnect.
  375. this.connection_gone('error', err);
  376. };
  377. RedisClient.prototype.on_connect = function () {
  378. debug('Stream connected ' + this.address + ' id ' + this.connection_id);
  379. this.connected = true;
  380. this.ready = false;
  381. this.emitted_end = false;
  382. this.stream.setKeepAlive(this.options.socket_keepalive);
  383. this.stream.setTimeout(0);
  384. this.emit('connect');
  385. this.initialize_retry_vars();
  386. if (this.options.no_ready_check) {
  387. this.on_ready();
  388. } else {
  389. this.ready_check();
  390. }
  391. };
  392. RedisClient.prototype.on_ready = function () {
  393. var self = this;
  394. debug('on_ready called ' + this.address + ' id ' + this.connection_id);
  395. this.ready = true;
  396. this.cork = function () {
  397. self.pipeline = true;
  398. if (self.stream.cork) {
  399. self.stream.cork();
  400. }
  401. };
  402. this.uncork = function () {
  403. if (self.fire_strings) {
  404. self.write_strings();
  405. } else {
  406. self.write_buffers();
  407. }
  408. self.pipeline = false;
  409. self.fire_strings = true;
  410. if (self.stream.uncork) {
  411. // TODO: Consider using next tick here. See https://github.com/NodeRedis/node_redis/issues/1033
  412. self.stream.uncork();
  413. }
  414. };
  415. // Restore modal commands from previous connection. The order of the commands is important
  416. if (this.selected_db !== undefined) {
  417. this.internal_send_command(new Command('select', [this.selected_db]));
  418. }
  419. if (this.monitoring) { // Monitor has to be fired before pub sub commands
  420. this.internal_send_command(new Command('monitor', []));
  421. }
  422. var callback_count = Object.keys(this.subscription_set).length;
  423. if (!this.options.disable_resubscribing && callback_count) {
  424. // only emit 'ready' when all subscriptions were made again
  425. // TODO: Remove the countdown for ready here. This is not coherent with all other modes and should therefore not be handled special
  426. // We know we are ready as soon as all commands were fired
  427. var callback = function () {
  428. callback_count--;
  429. if (callback_count === 0) {
  430. self.emit('ready');
  431. }
  432. };
  433. debug('Sending pub/sub on_ready commands');
  434. for (var key in this.subscription_set) {
  435. var command = key.slice(0, key.indexOf('_'));
  436. var args = this.subscription_set[key];
  437. this[command]([args], callback);
  438. }
  439. this.send_offline_queue();
  440. return;
  441. }
  442. this.send_offline_queue();
  443. this.emit('ready');
  444. };
  445. RedisClient.prototype.on_info_cmd = function (err, res) {
  446. if (err) {
  447. if (err.message === "ERR unknown command 'info'") {
  448. this.on_ready();
  449. return;
  450. }
  451. err.message = 'Ready check failed: ' + err.message;
  452. this.emit('error', err);
  453. return;
  454. }
  455. /* istanbul ignore if: some servers might not respond with any info data. This is just a safety check that is difficult to test */
  456. if (!res) {
  457. debug('The info command returned without any data.');
  458. this.on_ready();
  459. return;
  460. }
  461. if (!this.server_info.loading || this.server_info.loading === '0') {
  462. // If the master_link_status exists but the link is not up, try again after 50 ms
  463. if (this.server_info.master_link_status && this.server_info.master_link_status !== 'up') {
  464. this.server_info.loading_eta_seconds = 0.05;
  465. } else {
  466. // Eta loading should change
  467. debug('Redis server ready.');
  468. this.on_ready();
  469. return;
  470. }
  471. }
  472. var retry_time = +this.server_info.loading_eta_seconds * 1000;
  473. if (retry_time > 1000) {
  474. retry_time = 1000;
  475. }
  476. debug('Redis server still loading, trying again in ' + retry_time);
  477. setTimeout(function (self) {
  478. self.ready_check();
  479. }, retry_time, this);
  480. };
  481. RedisClient.prototype.ready_check = function () {
  482. var self = this;
  483. debug('Checking server ready state...');
  484. // Always fire this info command as first command even if other commands are already queued up
  485. this.ready = true;
  486. this.info(function (err, res) {
  487. self.on_info_cmd(err, res);
  488. });
  489. this.ready = false;
  490. };
  491. RedisClient.prototype.send_offline_queue = function () {
  492. for (var command_obj = this.offline_queue.shift(); command_obj; command_obj = this.offline_queue.shift()) {
  493. debug('Sending offline command: ' + command_obj.command);
  494. this.internal_send_command(command_obj);
  495. }
  496. this.drain();
  497. };
  498. var retry_connection = function (self, error) {
  499. debug('Retrying connection...');
  500. var reconnect_params = {
  501. delay: self.retry_delay,
  502. attempt: self.attempts,
  503. error: error
  504. };
  505. if (self.options.camel_case) {
  506. reconnect_params.totalRetryTime = self.retry_totaltime;
  507. reconnect_params.timesConnected = self.times_connected;
  508. } else {
  509. reconnect_params.total_retry_time = self.retry_totaltime;
  510. reconnect_params.times_connected = self.times_connected;
  511. }
  512. self.emit('reconnecting', reconnect_params);
  513. self.retry_totaltime += self.retry_delay;
  514. self.attempts += 1;
  515. self.retry_delay = Math.round(self.retry_delay * self.retry_backoff);
  516. self.create_stream();
  517. self.retry_timer = null;
  518. };
  519. RedisClient.prototype.connection_gone = function (why, error) {
  520. // If a retry is already in progress, just let that happen
  521. if (this.retry_timer) {
  522. return;
  523. }
  524. error = error || null;
  525. debug('Redis connection is gone from ' + why + ' event.');
  526. this.connected = false;
  527. this.ready = false;
  528. // Deactivate cork to work with the offline queue
  529. this.cork = noop;
  530. this.uncork = noop;
  531. this.pipeline = false;
  532. this.pub_sub_mode = 0;
  533. // since we are collapsing end and close, users don't expect to be called twice
  534. if (!this.emitted_end) {
  535. this.emit('end');
  536. this.emitted_end = true;
  537. }
  538. // If this is a requested shutdown, then don't retry
  539. if (this.closing) {
  540. debug('Connection ended by quit / end command, not retrying.');
  541. this.flush_and_error({
  542. message: 'Stream connection ended and command aborted.',
  543. code: 'NR_CLOSED'
  544. }, {
  545. error: error
  546. });
  547. return;
  548. }
  549. if (typeof this.options.retry_strategy === 'function') {
  550. var retry_params = {
  551. attempt: this.attempts,
  552. error: error
  553. };
  554. if (this.options.camel_case) {
  555. retry_params.totalRetryTime = this.retry_totaltime;
  556. retry_params.timesConnected = this.times_connected;
  557. } else {
  558. retry_params.total_retry_time = this.retry_totaltime;
  559. retry_params.times_connected = this.times_connected;
  560. }
  561. this.retry_delay = this.options.retry_strategy(retry_params);
  562. if (typeof this.retry_delay !== 'number') {
  563. // Pass individual error through
  564. if (this.retry_delay instanceof Error) {
  565. error = this.retry_delay;
  566. }
  567. this.flush_and_error({
  568. message: 'Stream connection ended and command aborted.',
  569. code: 'NR_CLOSED'
  570. }, {
  571. error: error
  572. });
  573. this.end(false);
  574. return;
  575. }
  576. }
  577. if (this.max_attempts !== 0 && this.attempts >= this.max_attempts || this.retry_totaltime >= this.connect_timeout) {
  578. var message = 'Redis connection in broken state: ';
  579. if (this.retry_totaltime >= this.connect_timeout) {
  580. message += 'connection timeout exceeded.';
  581. } else {
  582. message += 'maximum connection attempts exceeded.';
  583. }
  584. this.flush_and_error({
  585. message: message,
  586. code: 'CONNECTION_BROKEN',
  587. }, {
  588. error: error
  589. });
  590. var err = new Error(message);
  591. err.code = 'CONNECTION_BROKEN';
  592. if (error) {
  593. err.origin = error;
  594. }
  595. this.emit('error', err);
  596. this.end(false);
  597. return;
  598. }
  599. // Retry commands after a reconnect instead of throwing an error. Use this with caution
  600. if (this.options.retry_unfulfilled_commands) {
  601. this.offline_queue.unshift.apply(this.offline_queue, this.command_queue.toArray());
  602. this.command_queue.clear();
  603. } else if (this.command_queue.length !== 0) {
  604. this.flush_and_error({
  605. message: 'Redis connection lost and command aborted.',
  606. code: 'UNCERTAIN_STATE'
  607. }, {
  608. error: error,
  609. queues: ['command_queue']
  610. });
  611. }
  612. if (this.retry_max_delay !== null && this.retry_delay > this.retry_max_delay) {
  613. this.retry_delay = this.retry_max_delay;
  614. } else if (this.retry_totaltime + this.retry_delay > this.connect_timeout) {
  615. // Do not exceed the maximum
  616. this.retry_delay = this.connect_timeout - this.retry_totaltime;
  617. }
  618. debug('Retry connection in ' + this.retry_delay + ' ms');
  619. this.retry_timer = setTimeout(retry_connection, this.retry_delay, this, error);
  620. };
  621. RedisClient.prototype.return_error = function (err) {
  622. var command_obj = this.command_queue.shift();
  623. if (command_obj.error) {
  624. err.stack = command_obj.error.stack.replace(/^Error.*?\n/, 'ReplyError: ' + err.message + '\n');
  625. }
  626. err.command = command_obj.command.toUpperCase();
  627. if (command_obj.args && command_obj.args.length) {
  628. err.args = command_obj.args;
  629. }
  630. // Count down pub sub mode if in entering modus
  631. if (this.pub_sub_mode > 1) {
  632. this.pub_sub_mode--;
  633. }
  634. var match = err.message.match(utils.err_code);
  635. // LUA script could return user errors that don't behave like all other errors!
  636. if (match) {
  637. err.code = match[1];
  638. }
  639. utils.callback_or_emit(this, command_obj.callback, err);
  640. };
  641. RedisClient.prototype.drain = function () {
  642. this.emit('drain');
  643. this.should_buffer = false;
  644. };
  645. RedisClient.prototype.emit_idle = function () {
  646. if (this.command_queue.length === 0 && this.pub_sub_mode === 0) {
  647. this.emit('idle');
  648. }
  649. };
  650. function normal_reply (self, reply) {
  651. var command_obj = self.command_queue.shift();
  652. if (typeof command_obj.callback === 'function') {
  653. if (command_obj.command !== 'exec') {
  654. reply = self.handle_reply(reply, command_obj.command, command_obj.buffer_args);
  655. }
  656. command_obj.callback(null, reply);
  657. } else {
  658. debug('No callback for reply');
  659. }
  660. }
  661. function subscribe_unsubscribe (self, reply, type) {
  662. // Subscribe commands take an optional callback and also emit an event, but only the _last_ response is included in the callback
  663. // The pub sub commands return each argument in a separate return value and have to be handled that way
  664. var command_obj = self.command_queue.get(0);
  665. var buffer = self.options.return_buffers || self.options.detect_buffers && command_obj.buffer_args;
  666. var channel = (buffer || reply[1] === null) ? reply[1] : reply[1].toString();
  667. var count = +reply[2]; // Return the channel counter as number no matter if `string_numbers` is activated or not
  668. debug(type, channel);
  669. // Emit first, then return the callback
  670. if (channel !== null) { // Do not emit or "unsubscribe" something if there was no channel to unsubscribe from
  671. self.emit(type, channel, count);
  672. if (type === 'subscribe' || type === 'psubscribe') {
  673. self.subscription_set[type + '_' + channel] = channel;
  674. } else {
  675. type = type === 'unsubscribe' ? 'subscribe' : 'psubscribe'; // Make types consistent
  676. delete self.subscription_set[type + '_' + channel];
  677. }
  678. }
  679. if (command_obj.args.length === 1 || self.sub_commands_left === 1 || command_obj.args.length === 0 && (count === 0 || channel === null)) {
  680. if (count === 0) { // unsubscribed from all channels
  681. var running_command;
  682. var i = 1;
  683. self.pub_sub_mode = 0; // Deactivating pub sub mode
  684. // This should be a rare case and therefore handling it this way should be good performance wise for the general case
  685. while (running_command = self.command_queue.get(i)) {
  686. if (SUBSCRIBE_COMMANDS[running_command.command]) {
  687. self.pub_sub_mode = i; // Entering pub sub mode again
  688. break;
  689. }
  690. i++;
  691. }
  692. }
  693. self.command_queue.shift();
  694. if (typeof command_obj.callback === 'function') {
  695. // TODO: The current return value is pretty useless.
  696. // Evaluate to change this in v.3 to return all subscribed / unsubscribed channels in an array including the number of channels subscribed too
  697. command_obj.callback(null, channel);
  698. }
  699. self.sub_commands_left = 0;
  700. } else {
  701. if (self.sub_commands_left !== 0) {
  702. self.sub_commands_left--;
  703. } else {
  704. self.sub_commands_left = command_obj.args.length ? command_obj.args.length - 1 : count;
  705. }
  706. }
  707. }
  708. function return_pub_sub (self, reply) {
  709. var type = reply[0].toString();
  710. if (type === 'message') { // channel, message
  711. if (!self.options.return_buffers || self.message_buffers) { // backwards compatible. Refactor this in v.3 to always return a string on the normal emitter
  712. self.emit('message', reply[1].toString(), reply[2].toString());
  713. self.emit('message_buffer', reply[1], reply[2]);
  714. self.emit('messageBuffer', reply[1], reply[2]);
  715. } else {
  716. self.emit('message', reply[1], reply[2]);
  717. }
  718. } else if (type === 'pmessage') { // pattern, channel, message
  719. if (!self.options.return_buffers || self.message_buffers) { // backwards compatible. Refactor this in v.3 to always return a string on the normal emitter
  720. self.emit('pmessage', reply[1].toString(), reply[2].toString(), reply[3].toString());
  721. self.emit('pmessage_buffer', reply[1], reply[2], reply[3]);
  722. self.emit('pmessageBuffer', reply[1], reply[2], reply[3]);
  723. } else {
  724. self.emit('pmessage', reply[1], reply[2], reply[3]);
  725. }
  726. } else {
  727. subscribe_unsubscribe(self, reply, type);
  728. }
  729. }
  730. RedisClient.prototype.return_reply = function (reply) {
  731. if (this.monitoring) {
  732. var replyStr;
  733. if (this.buffers && Buffer.isBuffer(reply)) {
  734. replyStr = reply.toString();
  735. } else {
  736. replyStr = reply;
  737. }
  738. // If in monitor mode, all normal commands are still working and we only want to emit the streamlined commands
  739. if (typeof replyStr === 'string' && utils.monitor_regex.test(replyStr)) {
  740. var timestamp = replyStr.slice(0, replyStr.indexOf(' '));
  741. var args = replyStr.slice(replyStr.indexOf('"') + 1, -1).split('" "').map(function (elem) {
  742. return elem.replace(/\\"/g, '"');
  743. });
  744. this.emit('monitor', timestamp, args, replyStr);
  745. return;
  746. }
  747. }
  748. if (this.pub_sub_mode === 0) {
  749. normal_reply(this, reply);
  750. } else if (this.pub_sub_mode !== 1) {
  751. this.pub_sub_mode--;
  752. normal_reply(this, reply);
  753. } else if (!(reply instanceof Array) || reply.length <= 2) {
  754. // Only PING and QUIT are allowed in this context besides the pub sub commands
  755. // Ping replies with ['pong', null|value] and quit with 'OK'
  756. normal_reply(this, reply);
  757. } else {
  758. return_pub_sub(this, reply);
  759. }
  760. };
  761. function handle_offline_command (self, command_obj) {
  762. var command = command_obj.command;
  763. var err, msg;
  764. if (self.closing || !self.enable_offline_queue) {
  765. command = command.toUpperCase();
  766. if (!self.closing) {
  767. if (self.stream.writable) {
  768. msg = 'The connection is not yet established and the offline queue is deactivated.';
  769. } else {
  770. msg = 'Stream not writeable.';
  771. }
  772. } else {
  773. msg = 'The connection is already closed.';
  774. }
  775. err = new errorClasses.AbortError({
  776. message: command + " can't be processed. " + msg,
  777. code: 'NR_CLOSED',
  778. command: command
  779. });
  780. if (command_obj.args.length) {
  781. err.args = command_obj.args;
  782. }
  783. utils.reply_in_order(self, command_obj.callback, err);
  784. } else {
  785. debug('Queueing ' + command + ' for next server connection.');
  786. self.offline_queue.push(command_obj);
  787. }
  788. self.should_buffer = true;
  789. }
  790. // Do not call internal_send_command directly, if you are not absolutly certain it handles everything properly
  791. // e.g. monitor / info does not work with internal_send_command only
  792. RedisClient.prototype.internal_send_command = function (command_obj) {
  793. var arg, prefix_keys;
  794. var i = 0;
  795. var command_str = '';
  796. var args = command_obj.args;
  797. var command = command_obj.command;
  798. var len = args.length;
  799. var big_data = false;
  800. var args_copy = new Array(len);
  801. if (process.domain && command_obj.callback) {
  802. command_obj.callback = process.domain.bind(command_obj.callback);
  803. }
  804. if (this.ready === false || this.stream.writable === false) {
  805. // Handle offline commands right away
  806. handle_offline_command(this, command_obj);
  807. return false; // Indicate buffering
  808. }
  809. for (i = 0; i < len; i += 1) {
  810. if (typeof args[i] === 'string') {
  811. // 30000 seemed to be a good value to switch to buffers after testing and checking the pros and cons
  812. if (args[i].length > 30000) {
  813. big_data = true;
  814. args_copy[i] = new Buffer(args[i], 'utf8');
  815. } else {
  816. args_copy[i] = args[i];
  817. }
  818. } else if (typeof args[i] === 'object') { // Checking for object instead of Buffer.isBuffer helps us finding data types that we can't handle properly
  819. if (args[i] instanceof Date) { // Accept dates as valid input
  820. args_copy[i] = args[i].toString();
  821. } else if (args[i] === null) {
  822. this.warn(
  823. 'Deprecated: The ' + command.toUpperCase() + ' command contains a "null" argument.\n' +
  824. 'This is converted to a "null" string now and will return an error from v.3.0 on.\n' +
  825. 'Please handle this in your code to make sure everything works as you intended it to.'
  826. );
  827. args_copy[i] = 'null'; // Backwards compatible :/
  828. } else if (Buffer.isBuffer(args[i])) {
  829. args_copy[i] = args[i];
  830. command_obj.buffer_args = true;
  831. big_data = true;
  832. } else {
  833. this.warn(
  834. 'Deprecated: The ' + command.toUpperCase() + ' command contains a argument of type ' + args[i].constructor.name + '.\n' +
  835. 'This is converted to "' + args[i].toString() + '" by using .toString() now and will return an error from v.3.0 on.\n' +
  836. 'Please handle this in your code to make sure everything works as you intended it to.'
  837. );
  838. args_copy[i] = args[i].toString(); // Backwards compatible :/
  839. }
  840. } else if (typeof args[i] === 'undefined') {
  841. this.warn(
  842. 'Deprecated: The ' + command.toUpperCase() + ' command contains a "undefined" argument.\n' +
  843. 'This is converted to a "undefined" string now and will return an error from v.3.0 on.\n' +
  844. 'Please handle this in your code to make sure everything works as you intended it to.'
  845. );
  846. args_copy[i] = 'undefined'; // Backwards compatible :/
  847. } else {
  848. // Seems like numbers are converted fast using string concatenation
  849. args_copy[i] = '' + args[i];
  850. }
  851. }
  852. if (this.options.prefix) {
  853. prefix_keys = commands.getKeyIndexes(command, args_copy);
  854. for (i = prefix_keys.pop(); i !== undefined; i = prefix_keys.pop()) {
  855. args_copy[i] = this.options.prefix + args_copy[i];
  856. }
  857. }
  858. if (this.options.rename_commands && this.options.rename_commands[command]) {
  859. command = this.options.rename_commands[command];
  860. }
  861. // Always use 'Multi bulk commands', but if passed any Buffer args, then do multiple writes, one for each arg.
  862. // This means that using Buffers in commands is going to be slower, so use Strings if you don't already have a Buffer.
  863. command_str = '*' + (len + 1) + '\r\n$' + command.length + '\r\n' + command + '\r\n';
  864. if (big_data === false) { // Build up a string and send entire command in one write
  865. for (i = 0; i < len; i += 1) {
  866. arg = args_copy[i];
  867. command_str += '$' + Buffer.byteLength(arg) + '\r\n' + arg + '\r\n';
  868. }
  869. debug('Send ' + this.address + ' id ' + this.connection_id + ': ' + command_str);
  870. this.write(command_str);
  871. } else {
  872. debug('Send command (' + command_str + ') has Buffer arguments');
  873. this.fire_strings = false;
  874. this.write(command_str);
  875. for (i = 0; i < len; i += 1) {
  876. arg = args_copy[i];
  877. if (typeof arg === 'string') {
  878. this.write('$' + Buffer.byteLength(arg) + '\r\n' + arg + '\r\n');
  879. } else { // buffer
  880. this.write('$' + arg.length + '\r\n');
  881. this.write(arg);
  882. this.write('\r\n');
  883. }
  884. debug('send_command: buffer send ' + arg.length + ' bytes');
  885. }
  886. }
  887. if (command_obj.call_on_write) {
  888. command_obj.call_on_write();
  889. }
  890. // Handle `CLIENT REPLY ON|OFF|SKIP`
  891. // This has to be checked after call_on_write
  892. /* istanbul ignore else: TODO: Remove this as soon as we test Redis 3.2 on travis */
  893. if (this.reply === 'ON') {
  894. this.command_queue.push(command_obj);
  895. } else {
  896. // Do not expect a reply
  897. // Does this work in combination with the pub sub mode?
  898. if (command_obj.callback) {
  899. utils.reply_in_order(this, command_obj.callback, null, undefined, this.command_queue);
  900. }
  901. if (this.reply === 'SKIP') {
  902. this.reply = 'SKIP_ONE_MORE';
  903. } else if (this.reply === 'SKIP_ONE_MORE') {
  904. this.reply = 'ON';
  905. }
  906. }
  907. return !this.should_buffer;
  908. };
  909. RedisClient.prototype.write_strings = function () {
  910. var str = '';
  911. for (var command = this.pipeline_queue.shift(); command; command = this.pipeline_queue.shift()) {
  912. // Write to stream if the string is bigger than 4mb. The biggest string may be Math.pow(2, 28) - 15 chars long
  913. if (str.length + command.length > 4 * 1024 * 1024) {
  914. this.should_buffer = !this.stream.write(str);
  915. str = '';
  916. }
  917. str += command;
  918. }
  919. if (str !== '') {
  920. this.should_buffer = !this.stream.write(str);
  921. }
  922. };
  923. RedisClient.prototype.write_buffers = function () {
  924. for (var command = this.pipeline_queue.shift(); command; command = this.pipeline_queue.shift()) {
  925. this.should_buffer = !this.stream.write(command);
  926. }
  927. };
  928. RedisClient.prototype.write = function (data) {
  929. if (this.pipeline === false) {
  930. this.should_buffer = !this.stream.write(data);
  931. return;
  932. }
  933. this.pipeline_queue.push(data);
  934. };
  935. Object.defineProperty(exports, 'debugMode', {
  936. get: function () {
  937. return this.debug_mode;
  938. },
  939. set: function (val) {
  940. this.debug_mode = val;
  941. }
  942. });
  943. // Don't officially expose the command_queue directly but only the length as read only variable
  944. Object.defineProperty(RedisClient.prototype, 'command_queue_length', {
  945. get: function () {
  946. return this.command_queue.length;
  947. }
  948. });
  949. Object.defineProperty(RedisClient.prototype, 'offline_queue_length', {
  950. get: function () {
  951. return this.offline_queue.length;
  952. }
  953. });
  954. // Add support for camelCase by adding read only properties to the client
  955. // All known exposed snake_case variables are added here
  956. Object.defineProperty(RedisClient.prototype, 'retryDelay', {
  957. get: function () {
  958. return this.retry_delay;
  959. }
  960. });
  961. Object.defineProperty(RedisClient.prototype, 'retryBackoff', {
  962. get: function () {
  963. return this.retry_backoff;
  964. }
  965. });
  966. Object.defineProperty(RedisClient.prototype, 'commandQueueLength', {
  967. get: function () {
  968. return this.command_queue.length;
  969. }
  970. });
  971. Object.defineProperty(RedisClient.prototype, 'offlineQueueLength', {
  972. get: function () {
  973. return this.offline_queue.length;
  974. }
  975. });
  976. Object.defineProperty(RedisClient.prototype, 'shouldBuffer', {
  977. get: function () {
  978. return this.should_buffer;
  979. }
  980. });
  981. Object.defineProperty(RedisClient.prototype, 'connectionId', {
  982. get: function () {
  983. return this.connection_id;
  984. }
  985. });
  986. Object.defineProperty(RedisClient.prototype, 'serverInfo', {
  987. get: function () {
  988. return this.server_info;
  989. }
  990. });
  991. exports.createClient = function () {
  992. return new RedisClient(unifyOptions.apply(null, arguments));
  993. };
  994. exports.RedisClient = RedisClient;
  995. exports.print = utils.print;
  996. exports.Multi = require('./lib/multi');
  997. exports.AbortError = errorClasses.AbortError;
  998. exports.RedisError = Parser.RedisError;
  999. exports.ParserError = Parser.ParserError;
  1000. exports.ReplyError = Parser.ReplyError;
  1001. exports.AggregateError = errorClasses.AggregateError;
  1002. // Add all redis commands / node_redis api to the client
  1003. require('./lib/individualCommands');
  1004. require('./lib/extendedApi');
  1005. //enables adding new commands (for modules and new commands)
  1006. exports.addCommand = exports.add_command = require('./lib/commands');