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 | 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 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
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]);
|
45 | }
|
46 | return this[command].apply(this, args);
|
47 | };
|
48 |
|
49 | RedisClient.prototype.end = function (flush) {
|
50 |
|
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 |
|
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 |
|
75 | RedisClient.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 |
|
87 | RedisClient.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 | };
|