UNPKG

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