1 | 'use strict';
|
2 |
|
3 | var utils = require('./utils');
|
4 | var debug = require('./debug');
|
5 | var RedisClient = require('../').RedisClient;
|
6 | var Command = require('./command');
|
7 | var noop = function () {};
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 | RedisClient.prototype.send_command = RedisClient.prototype.sendCommand = function (command, args, callback) {
|
15 |
|
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 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
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]);
|
44 | }
|
45 | return this[command].apply(this, args);
|
46 | };
|
47 |
|
48 | RedisClient.prototype.end = function (flush) {
|
49 |
|
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 |
|
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 |
|
74 | RedisClient.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 |
|
86 | RedisClient.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 | };
|