-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathindex.js
84 lines (68 loc) · 1.84 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/**
* Module dependencies.
*/
var promisify = require('es6-promisify');
var EventEmitter = require('events').EventEmitter;
/**
* List of API functions that do not take a callback as an argument
* since they are not redis commands.
*
* @constant
* @type {Array}
*/
var API_FUNCTIONS = ['end', 'unref'];
/**
* Wrap `client`.
*
* @param {Redis} client
* @return {Object}
*/
module.exports = function (client) {
var wrap = {};
wrap.multi = function (cmds) {
var multi = client.multi(cmds);
multi.exec = promisify(multi.exec, multi);
return multi;
};
wrap.batch = function (cmds) {
var batch = client.batch(cmds);
batch.exec = promisify(batch.exec, batch);
return batch;
};
wrap.pipeline = function(){
var pipeline = client.pipeline();
pipeline.exec = promisify(pipeline.exec, pipeline);
return pipeline;
};
Object.keys(client).forEach(function (key) {
wrap[key] = client[key];
});
Object.keys(EventEmitter.prototype).forEach(function (key) {
if (typeof client[key] != 'function') return;
wrap[key] = client[key].bind(client);
});
Object.defineProperty(wrap, 'connected', {
get: function () { return client.connected }
});
Object.defineProperty(wrap, 'retry_delay', {
get: function () { return client.retry_delay }
});
Object.defineProperty(wrap, 'retry_backoff', {
get: function () { return client.retry_backoff }
});
var nowrap = {
'multi': true,
'batch': true,
'pipeline': true
};
Object.keys(Object.getPrototypeOf(client)).forEach(function (key) {
var protoFunction = client[key].bind(client);
var isCommand = API_FUNCTIONS.indexOf(key) === -1;
if (nowrap[key]) return;
if (isCommand) {
protoFunction = promisify(protoFunction, client);
}
wrap[key] = protoFunction;
});
return wrap;
};