-
Notifications
You must be signed in to change notification settings - Fork 0
/
rcc-ioredis-adapter.js
183 lines (150 loc) · 5.56 KB
/
rcc-ioredis-adapter.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
'use strict';
const isInstanceOf = require('core-functions/objects').isInstanceOf;
const rccCore = require('rcc-core');
const IoRedisClient = require('ioredis');
const ReplyError = IoRedisClient.ReplyError;
exports.ReplyError = ReplyError;
const adaptee = 'ioredis';
exports.adaptee = adaptee;
const defaultHost = 'localhost';
const defaultPort = rccCore.DEFAULT_REDIS_PORT;
exports.defaultHost = defaultHost;
exports.defaultPort = defaultPort;
exports.createClient = createClient;
exports.getClientFunction = getClientFunction;
exports.setClientFunction = setClientFunction;
exports.deleteClientFunction = deleteClientFunction;
exports.isMovedError = isMovedError;
exports.resolveHostAndPortFromMovedError = resolveHostAndPortFromMovedError;
function fixRedisClientFunctions() {
const prototype = IoRedisClient.prototype;
// If client is missing an `end` method then use its `quit` method as an `end` method
if (!prototype.end) {
console.warn('Missing end function for `ioredis` client prototype');
// console.log('Adding missing end function to `ioredis` client prototype');
// prototype.end = endViaQuit;
}
}
function adaptRedisClient() {
const prototype = IoRedisClient.prototype;
if (!prototype.getAdapter) {
prototype.getAdapter = getAdapter;
}
if (!prototype.getOptions) {
prototype.getOptions = getOptions;
}
if (!prototype.isClosing) {
prototype.isClosing = isClosing;
}
if (!prototype.resolveHostAndPort) {
prototype.resolveHostAndPort = resolveHostAndPort;
}
if (!prototype.addEventListeners) {
prototype.addEventListeners = addEventListeners;
}
if (!prototype.getFunction) {
prototype.getFunction = getClientFunction;
}
if (!prototype.setFunction) {
prototype.setFunction = setClientFunction;
}
if (!prototype.deleteFunction) {
prototype.deleteFunction = deleteClientFunction;
}
}
fixRedisClientFunctions();
adaptRedisClient();
/**
* Creates a new RedisClient instance.
* @param {RedisClientOptions|undefined} [redisClientOptions] - the options to use to construct the new Redis client
* instance
* @return {RedisClient} returns the new RedisClient instance
*/
function createClient(redisClientOptions) {
return new IoRedisClient(redisClientOptions);
}
function getClientFunction(fnName) {
return IoRedisClient.prototype[fnName];
}
function setClientFunction(fnName, fn) {
IoRedisClient.prototype[fnName] = fn;
}
function deleteClientFunction(fnName) {
delete IoRedisClient.prototype[fnName];
}
/**
* Returns true if the given error indicates that the key attempted was moved to a new host and port; otherwise returns
* false.
* @param {Error|ReplyError} error - an error thrown by a RedisClient instance
* @return {boolean} true if moved; false otherwise
*/
function isMovedError(error) {
// Check if error message contains something like: "MOVED 14190 127.0.0.1:6379"
return !!isInstanceOf(error, ReplyError) && error.message && error.message.startsWith('MOVED ');
}
/**
* Extracts the new host and port from the given RedisClient "moved" ReplyError.
* @param {ReplyError|Error} movedError - a ReplyError thrown by a RedisClient instance that indicates the redis server
* has moved
* @return {[string, number|string]} the new host and port
*/
function resolveHostAndPortFromMovedError(movedError) {
// Attempt to resolve the new host & port from the error message
if (isMovedError(movedError)) {
return movedError.message.substring(movedError.message.lastIndexOf(' ') + 1).split(':');
}
throw new Error(`Unexpected ioredis client "moved" ReplyError - ${movedError}`);
}
// function endViaQuit(flush) {
// let args = arguments;
//
// // Drop the first flush argument (if any), since quit cannot use it
// if (typeof flush !== 'function') {
// const n = arguments.length;
// args = new Array(n - 1);
// for (let i = 1; i < n; ++i) {
// args[i - 1] = arguments[i];
// }
// }
// this.manuallyClosing = true; // simulate a "closing" flag
// return this.quit.apply(this, args);
// }
/**
* Returns true if this RedisClient instance's connection is closing or has closed.
* @return {boolean} true if closing or closed; false otherwise
*/
function getAdapter() {
return module.exports;
}
/**
* Returns the options with which this RedisClient instance was constructed.
* @returns {RedisClientOptions} the options used
*/
function getOptions() {
return this.options;
}
/**
* Returns true if this RedisClient instance's connection is closing or has closed.
* @return {boolean} true if closing or closed; false otherwise
*/
function isClosing() {
return this.manuallyClosing;
}
/**
* Resolves the host & port of this RedisClient instance.
* @return {[string, number|string]} an array containing the host and port
*/
function resolveHostAndPort() {
const connectionOptions = this.connection_options;
return connectionOptions ? [connectionOptions.host, connectionOptions.port] :
this.options ? [this.options.host, this.options.port] : [defaultHost, defaultPort];
}
function addEventListeners(onConnect, onReady, onReconnecting, onError, onClientError, onEnd, onClose) {
if (typeof onConnect === 'function') this.on('connect', onConnect);
if (typeof onReady === 'function') this.on('ready', onReady);
if (typeof onReconnecting === 'function') this.on('reconnecting', onReconnecting);
if (typeof onError === 'function') this.on('error', onError);
if (typeof onClientError === 'function') this.on('clientError', onClientError);
if (typeof onEnd === 'function') this.on('end', onEnd);
if (typeof onClose === 'function') this.on('close', onClose);
}