-
Notifications
You must be signed in to change notification settings - Fork 2
/
redis-client-abstract.ts
375 lines (315 loc) · 9.24 KB
/
redis-client-abstract.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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
/*
* Copyright (c)
* Weyoss <weyoss@protonmail.com>
* https://github.com/weyoss
*
* This source code is licensed under the MIT license found in the LICENSE file
* in the root directory of this source tree.
*/
import {
IRedisClient,
IRedisTransaction,
TRedisClientEvent,
} from '../types/index.js';
import { CallbackEmptyReplyError } from '../../errors/index.js';
import { EventEmitter } from '../../event/index.js';
import { RedisClientError } from '../errors/index.js';
import { ELuaScriptName, LuaScript } from '../lua-scripts/lua-script.js';
import { ICallback } from '../../common/index.js';
const minimalSupportedVersion: [number, number, number] = [4, 0, 0];
export abstract class RedisClientAbstract
extends EventEmitter<TRedisClientEvent>
implements IRedisClient
{
protected static redisServerVersion: number[] | null = null;
protected connectionClosed = true;
validateRedisVersion(major: number, feature = 0, minor = 0): boolean {
if (!RedisClientAbstract.redisServerVersion) {
this.emit('error', new RedisClientError('UNKNOWN_REDIS_SERVER_VERSION'));
return false;
}
return (
RedisClientAbstract.redisServerVersion[0] > major ||
(RedisClientAbstract.redisServerVersion[0] === major &&
RedisClientAbstract.redisServerVersion[1] >= feature &&
RedisClientAbstract.redisServerVersion[2] >= minor)
);
}
validateRedisServerSupport(cb: ICallback<void>): void {
const validate = (cb: ICallback<void>) => {
const [major, feature, minor] = minimalSupportedVersion;
if (!this.validateRedisVersion(major, feature, minor))
cb(new RedisClientError('UNSUPPORTED_REDIS_SERVER_VERSION'));
else cb();
};
if (!RedisClientAbstract.redisServerVersion) {
this.updateServerVersion((err) => {
if (err) cb(err);
else validate(cb);
});
} else validate(cb);
}
abstract set(
key: string,
value: string,
options: {
expire?: { mode: 'EX' | 'PX'; value: number };
exists?: 'NX' | 'XX';
},
cb: ICallback<string | null>,
): void;
abstract zadd(
key: string,
score: number,
member: string,
cb: ICallback<number | string>,
): void;
abstract multi(): IRedisTransaction;
abstract watch(args: string[], cb: ICallback<string>): void;
abstract unwatch(cb: ICallback<string>): void;
abstract sismember(key: string, member: string, cb: ICallback<number>): void;
abstract zcard(key: string, cb: ICallback<number>): void;
abstract zrange(
key: string,
min: number,
max: number,
cb: ICallback<string[]>,
): void;
abstract zrevrange(
key: string,
min: number,
max: number,
cb: ICallback<string[]>,
): void;
abstract psubscribe(pattern: string): void;
abstract punsubscribe(channel?: string): void;
abstract subscribe(channel: string): void;
abstract unsubscribe(channel?: string): void;
abstract zrangebyscore(
key: string,
min: number | string,
max: number | string,
offset: number,
count: number,
cb: ICallback<string[]>,
): void;
abstract smembers(key: string, cb: ICallback<string[]>): void;
abstract sscan(
key: string,
cursor: string,
options: { MATCH?: string; COUNT?: number },
cb: ICallback<{ cursor: string; items: string[] }>,
): void;
sscanAll(
key: string,
options: { MATCH?: string; COUNT?: number },
cb: ICallback<string[]>,
): void {
const result = new Set<string>();
const iterate = (cursor: string) => {
this.sscan(key, cursor, options, (err, reply) => {
if (err) cb(err);
else if (!reply) cb(new CallbackEmptyReplyError());
else {
reply.items.forEach((i) => result.add(i));
if (reply.cursor === '0') cb(null, [...result]);
else iterate(reply.cursor);
}
});
};
iterate('0');
}
abstract sadd(key: string, member: string, cb: ICallback<number>): void;
abstract srem(key: string, member: string, cb: ICallback<number>): void;
abstract hgetall(key: string, cb: ICallback<Record<string, string>>): void;
abstract hscan(
key: string,
cursor: string,
options: { MATCH?: string; COUNT?: number },
cb: ICallback<{ cursor: string; result: Record<string, string> }>,
): void;
hscanAll(
key: string,
options: { MATCH?: string; COUNT?: number },
cb: ICallback<Record<string, string>>,
): void {
const result: Record<string, string> = {};
const iterate = (cursor: string) => {
this.hscan(key, cursor, options, (err, reply) => {
if (err) cb(err);
else if (!reply) cb(new CallbackEmptyReplyError());
else {
Object.assign(result, reply.result);
if (reply.cursor === '0') cb(null, result);
else iterate(reply.cursor);
}
});
};
iterate('0');
}
abstract hget(key: string, field: string, cb: ICallback<string | null>): void;
abstract hset(
key: string,
field: string,
value: string | number,
cb: ICallback<number>,
): void;
abstract hdel(
key: string,
fields: string | string[],
cb: ICallback<number>,
): void;
abstract lrange(
key: string,
start: number,
stop: number,
cb: ICallback<string[]>,
): void;
abstract hkeys(key: string, cb: ICallback<string[]>): void;
abstract hlen(key: string, cb: ICallback<number>): void;
abstract brpoplpush(
source: string,
destination: string,
timeout: number,
cb: ICallback<string | null>,
): void;
abstract rpoplpush(
source: string,
destination: string,
cb: ICallback<string | null>,
): void;
zpoprpush(
source: string,
destination: string,
cb: ICallback<string | null>,
): void {
this.runScript(
ELuaScriptName.ZPOPRPUSH,
[source, destination],
[],
(err, res?: unknown) => {
if (err) cb(err);
else cb(null, typeof res === 'string' ? res : null);
},
);
}
abstract zscan(
key: string,
cursor: string,
options: { MATCH?: string; COUNT?: number },
cb: ICallback<{ cursor: string; items: string[] }>,
): void;
lpoprpush(
source: string,
destination: string,
cb: ICallback<string | null>,
): void {
if (this.validateRedisVersion(6, 2)) {
this.lmove(source, destination, 'LEFT', 'RIGHT', cb);
} else {
this.runScript(
ELuaScriptName.LPOPRPUSH,
[source, destination],
[],
(err, res?: unknown) => {
if (err) cb(err);
else cb(null, typeof res === 'string' ? res : null);
},
);
}
}
abstract zrangebyscorewithscores(
source: string,
min: number,
max: number,
cb: ICallback<Record<string, string>>,
): void;
abstract zrem(source: string, id: string, cb: ICallback<number>): void;
abstract rpop(key: string, cb: ICallback<string | null>): void;
abstract lrem(
key: string,
count: number,
element: string,
cb: ICallback<number>,
): void;
abstract publish(
channel: string,
message: string,
cb: ICallback<number>,
): void;
abstract flushall(cb: ICallback<string>): void;
abstract loadScript(script: string, cb: ICallback<string>): void;
abstract evalsha(
hash: string,
args: (string | number)[] | string | number,
cb: (err?: Error | null, res?: unknown) => void,
): void;
abstract get(key: string, cb: ICallback<string | null>): void;
abstract del(key: string | string[], cb: ICallback<number>): void;
abstract llen(key: string, cb: ICallback<number>): void;
abstract lmove(
source: string,
destination: string,
from: 'LEFT' | 'RIGHT',
to: 'LEFT' | 'RIGHT',
cb: ICallback<string | null>,
): void;
abstract zremrangebyscore(
source: string,
min: number | string,
max: number | string,
cb: ICallback<number>,
): void;
abstract hmget(
source: string,
keys: string[],
cb: ICallback<(string | null)[]>,
): void;
abstract halt(cb: ICallback<void>): void;
abstract end(flush: boolean): void;
abstract shutdown(cb: ICallback<void>): void;
abstract getInfo(cb: ICallback<string>): void;
updateServerVersion(cb: ICallback<void>): void {
if (!RedisClientAbstract.redisServerVersion) {
this.getInfo((err, res) => {
if (err) cb(err);
else if (!res) cb(new CallbackEmptyReplyError());
else {
RedisClientAbstract.redisServerVersion = res
.split('\r\n')[1]
.split(':')[1]
.split('.')
.map((i) => Number(i));
cb();
}
});
} else cb();
}
loadScripts(cb: ICallback<void>): void {
LuaScript.getInstance().loadScripts(this, cb);
}
runScript(
scriptName: string,
keys: (string | number)[],
args: (string | number)[],
cb: ICallback<unknown>,
): void {
const sha = LuaScript.getInstance().getScriptId(scriptName);
if (sha instanceof Error) cb(sha);
else {
this.evalsha(
sha,
[keys.length, ...keys, ...args],
(err, res?: unknown) => {
if (err) cb(err);
else cb(null, res);
},
);
}
}
static addScript(name: string, content: string): void {
if (!LuaScript.getInstance().addScript(name, content)) {
throw new RedisClientError(`A script with name [${name}] already exists`);
}
}
}