-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
commander.ts
244 lines (221 loc) 路 6.17 KB
/
commander.ts
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
import { defaults } from "./utils/lodash";
import Command from "./command";
import Script from "./script";
import * as PromiseContainer from "./promiseContainer";
import asCallback from "standard-as-callback";
import {
executeWithAutoPipelining,
shouldUseAutoPipelining,
} from "./autoPipelining";
export interface ICommanderOptions {
showFriendlyErrorStack?: boolean;
}
const DROP_BUFFER_SUPPORT_ERROR =
"*Buffer methods are not available " +
'because "dropBufferSupport" option is enabled.' +
"Refer to https://github.com/luin/ioredis/wiki/Improve-Performance for more details.";
/**
* Commander
*
* This is the base class of Redis, Redis.Cluster and Pipeline
*
* @param {boolean} [options.showFriendlyErrorStack=false] - Whether to show a friendly error stack.
* Will decrease the performance significantly.
* @constructor
*/
export default function Commander() {
this.options = defaults({}, this.options || {}, {
showFriendlyErrorStack: false,
});
this.scriptsSet = {};
this.addedBuiltinSet = new Set();
}
const commands = require("redis-commands").list.filter(function (command) {
return command !== "monitor";
});
commands.push("sentinel");
/**
* Return supported builtin commands
*
* @return {string[]} command list
* @public
*/
Commander.prototype.getBuiltinCommands = function () {
return commands.slice(0);
};
/**
* Create a builtin command
*
* @param {string} commandName - command name
* @return {object} functions
* @public
*/
Commander.prototype.createBuiltinCommand = function (commandName) {
return {
string: generateFunction(null, commandName, "utf8"),
buffer: generateFunction(null, commandName, null),
};
};
/**
* Create add builtin command
*
* @param {string} commandName - command name
* @return {object} functions
* @public
*/
Commander.prototype.addBuiltinCommand = function (commandName) {
this.addedBuiltinSet.add(commandName);
this[commandName] = generateFunction(commandName, commandName, "utf8");
this[commandName + "Buffer"] = generateFunction(
commandName + "Buffer",
commandName,
null
);
};
commands.forEach(function (commandName) {
Commander.prototype[commandName] = generateFunction(
commandName,
commandName,
"utf8"
);
Commander.prototype[commandName + "Buffer"] = generateFunction(
commandName + "Buffer",
commandName,
null
);
});
Commander.prototype.call = generateFunction("call", "utf8");
Commander.prototype.callBuffer = generateFunction("callBuffer", null);
// eslint-disable-next-line @typescript-eslint/camelcase
Commander.prototype.send_command = Commander.prototype.call;
/**
* Define a custom command using lua script
*
* @param {string} name - the command name
* @param {object} definition
* @param {string} definition.lua - the lua code
* @param {number} [definition.numberOfKeys=null] - the number of keys.
* @param {boolean} [definition.readOnly=false] - force this script to be readonly so it executes on slaves as well.
* If omit, you have to pass the number of keys as the first argument every time you invoke the command
*/
Commander.prototype.defineCommand = function (name, definition) {
const script = new Script(
definition.lua,
definition.numberOfKeys,
this.options.keyPrefix,
definition.readOnly
);
this.scriptsSet[name] = script;
this[name] = generateScriptingFunction(name, name, script, "utf8");
this[name + "Buffer"] = generateScriptingFunction(
name + "Buffer",
name,
script,
null
);
};
/**
* Send a command
*
* @abstract
* @public
*/
Commander.prototype.sendCommand = function () {};
function generateFunction(functionName: string | null, _encoding: string);
function generateFunction(
functionName: string | null,
_commandName: string | void,
_encoding: string
);
function generateFunction(
functionName: string | null,
_commandName?: string,
_encoding?: string
) {
if (typeof _encoding === "undefined") {
_encoding = _commandName;
_commandName = null;
}
return function (...args) {
const commandName = _commandName || args.shift();
let callback = args[args.length - 1];
if (typeof callback === "function") {
args.pop();
} else {
callback = undefined;
}
const options = {
errorStack: this.options.showFriendlyErrorStack ? new Error() : undefined,
keyPrefix: this.options.keyPrefix,
replyEncoding: _encoding,
};
if (this.options.dropBufferSupport && !_encoding) {
return asCallback(
PromiseContainer.get().reject(new Error(DROP_BUFFER_SUPPORT_ERROR)),
callback
);
}
// No auto pipeline, use regular command sending
if (!shouldUseAutoPipelining(this, functionName, commandName)) {
return this.sendCommand(
new Command(commandName, args, options, callback)
);
}
// Create a new pipeline and make sure it's scheduled
return executeWithAutoPipelining(
this,
functionName,
commandName,
args,
callback
);
};
}
function generateScriptingFunction(
functionName,
commandName,
script,
encoding
) {
return function () {
let length = arguments.length;
const lastArgIndex = length - 1;
let callback = arguments[lastArgIndex];
if (typeof callback !== "function") {
callback = undefined;
} else {
length = lastArgIndex;
}
const args = new Array(length);
for (let i = 0; i < length; i++) {
args[i] = arguments[i];
}
let options;
if (this.options.dropBufferSupport) {
if (!encoding) {
return asCallback(
PromiseContainer.get().reject(new Error(DROP_BUFFER_SUPPORT_ERROR)),
callback
);
}
options = { replyEncoding: null };
} else {
options = { replyEncoding: encoding };
}
if (this.options.showFriendlyErrorStack) {
options.errorStack = new Error();
}
// No auto pipeline, use regular command sending
if (!shouldUseAutoPipelining(this, functionName, commandName)) {
return script.execute(this, args, options, callback);
}
// Create a new pipeline and make sure it's scheduled
return executeWithAutoPipelining(
this,
functionName,
commandName,
args,
callback
);
};
}