UNPKG

4.33 kBJavaScriptView Raw
1'use strict';
2
3var utils = require('./utils');
4var debug = require('./debug');
5var RedisClient = require('../').RedisClient;
6var Command = require('./command');
7var noop = function () {};
8
9/**********************************************
10All documented and exposed API belongs in here
11**********************************************/
12
13// Redirect calls to the appropriate function and use to send arbitrary / not supported commands
14RedisClient.prototype.send_command = RedisClient.prototype.sendCommand = function (command, args, callback) {
15 // Throw to fail early instead of relying in order in this case
16 if (typeof command !== 'string') {
17 throw new TypeError('Wrong input type "' + (command !== null && command !== undefined ? command.constructor.name : command) + '" for command name');
18 }
19 command = command.toLowerCase();
20 if (!Array.isArray(args)) {
21 if (args === undefined || args === null) {
22 args = [];
23 } else if (typeof args === 'function' && callback === undefined) {
24 callback = args;
25 args = [];
26 } else {
27 throw new TypeError('Wrong input type "' + args.constructor.name + '" for args');
28 }
29 }
30 if (typeof callback !== 'function' && callback !== undefined) {
31 throw new TypeError('Wrong input type "' + (callback !== null ? callback.constructor.name : 'null') + '" for callback function');
32 }
33
34 // Using the raw multi command is only possible with this function
35 // If the command is not yet added to the client, the internal function should be called right away
36 // Otherwise we need to redirect the calls to make sure the internal functions don't get skipped
37 // The internal functions could actually be used for any non hooked function
38 // but this might change from time to time and at the moment there's no good way to distinguish them
39 // from each other, so let's just do it do it this way for the time being
40 if (command === 'multi' || typeof this[command] !== 'function') {
41 return this.internal_send_command(new Command(command, args, callback));
42 }
43 if (typeof callback === 'function') {
44 args = args.concat([callback]); // Prevent manipulating the input array
45 }
46 return this[command].apply(this, args);
47};
48
49RedisClient.prototype.end = function (flush) {
50 // Flush queue if wanted
51 if (flush) {
52 this.flush_and_error({
53 message: 'Connection forcefully ended and command aborted.',
54 code: 'NR_CLOSED'
55 });
56 } else if (arguments.length === 0) {
57 this.warn(
58 'Using .end() without the flush parameter is deprecated and throws from v.3.0.0 on.\n' +
59 'Please check the doku (https://github.com/NodeRedis/node_redis) and explictly use flush.'
60 );
61 }
62 // Clear retry_timer
63 if (this.retry_timer) {
64 clearTimeout(this.retry_timer);
65 this.retry_timer = null;
66 }
67 this.stream.removeAllListeners();
68 this.stream.on('error', noop);
69 this.connected = false;
70 this.ready = false;
71 this.closing = true;
72 return this.stream.destroySoon();
73};
74
75RedisClient.prototype.unref = function () {
76 if (this.connected) {
77 debug("Unref'ing the socket connection");
78 this.stream.unref();
79 } else {
80 debug('Not connected yet, will unref later');
81 this.once('connect', function () {
82 this.unref();
83 });
84 }
85};
86
87RedisClient.prototype.duplicate = function (options, callback) {
88 if (typeof options === 'function') {
89 callback = options;
90 options = null;
91 }
92 var existing_options = utils.clone(this.options);
93 options = utils.clone(options);
94 for (var elem in options) {
95 existing_options[elem] = options[elem];
96 }
97 var client = new RedisClient(existing_options);
98 client.selected_db = options.db || this.selected_db;
99 if (typeof callback === 'function') {
100 var ready_listener = function () {
101 callback(null, client);
102 client.removeAllListeners(error_listener);
103 };
104 var error_listener = function (err) {
105 callback(err);
106 client.end(true);
107 };
108 client.once('ready', ready_listener);
109 client.once('error', error_listener);
110 return;
111 }
112 return client;
113};