-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathindex.ts
883 lines (763 loc) · 28.1 KB
/
index.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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
import COMMANDS from './commands';
import { RedisCommand, RedisCommandArgument, RedisCommandArguments, RedisCommandRawReply, RedisCommandReply, RedisFunctions, RedisModules, RedisExtensions, RedisScript, RedisScripts, RedisCommandSignature, ConvertArgumentType, RedisFunction, ExcludeMappedString, RedisCommands } from '../commands';
import RedisSocket, { RedisSocketOptions, RedisTlsSocketOptions } from './socket';
import RedisCommandsQueue, { QueueCommandOptions } from './commands-queue';
import RedisClientMultiCommand, { RedisClientMultiCommandType } from './multi-command';
import { RedisMultiQueuedCommand } from '../multi-command';
import { EventEmitter } from 'events';
import { CommandOptions, commandOptions, isCommandOptions } from '../command-options';
import { ScanOptions, ZMember } from '../commands/generic-transformers';
import { ScanCommandOptions } from '../commands/SCAN';
import { HScanTuple } from '../commands/HSCAN';
import { attachCommands, attachExtensions, fCallArguments, transformCommandArguments, transformCommandReply, transformLegacyCommandArguments } from '../commander';
import { Pool, Options as PoolOptions, createPool } from 'generic-pool';
import { ClientClosedError, ClientOfflineError, DisconnectsClientError, ErrorReply } from '../errors';
import { URL } from 'url';
import { TcpSocketConnectOpts } from 'net';
import { PubSubType, PubSubListener, PubSubTypeListeners, ChannelListeners } from './pub-sub';
import {version} from '../../package.json';
export interface RedisClientOptions<
M extends RedisModules = RedisModules,
F extends RedisFunctions = RedisFunctions,
S extends RedisScripts = RedisScripts
> extends RedisExtensions<M, F, S> {
/**
* `redis[s]://[[username][:password]@][host][:port][/db-number]`
* See [`redis`](https://www.iana.org/assignments/uri-schemes/prov/redis) and [`rediss`](https://www.iana.org/assignments/uri-schemes/prov/rediss) IANA registration for more details
*/
url?: string;
/**
* Socket connection properties
*/
socket?: RedisSocketOptions;
/**
* ACL username ([see ACL guide](https://redis.io/topics/acl))
*/
username?: string;
/**
* ACL password or the old "--requirepass" password
*/
password?: string;
/**
* Client name ([see `CLIENT SETNAME`](https://redis.io/commands/client-setname))
*/
name?: string;
/**
* Redis database number (see [`SELECT`](https://redis.io/commands/select) command)
*/
database?: number;
/**
* Maximum length of the client's internal command queue
*/
commandsQueueMaxLength?: number;
/**
* When `true`, commands are rejected when the client is reconnecting.
* When `false`, commands are queued for execution after reconnection.
*/
disableOfflineQueue?: boolean;
/**
* Connect in [`READONLY`](https://redis.io/commands/readonly) mode
*/
readonly?: boolean;
legacyMode?: boolean;
isolationPoolOptions?: PoolOptions;
/**
* Send `PING` command at interval (in ms).
* Useful with Redis deployments that do not use TCP Keep-Alive.
*/
pingInterval?: number;
/**
* If set to true, disables sending client identifier (user-agent like message) to the redis server
*/
disableClientInfo?: boolean;
/**
* Tag to append to library name that is sent to the Redis server
*/
clientInfoTag?: string;
}
type WithCommands = {
[P in keyof typeof COMMANDS]: RedisCommandSignature<(typeof COMMANDS)[P]>;
};
export type WithModules<M extends RedisModules> = {
[P in keyof M as ExcludeMappedString<P>]: {
[C in keyof M[P] as ExcludeMappedString<C>]: RedisCommandSignature<M[P][C]>;
};
};
export type WithFunctions<F extends RedisFunctions> = {
[P in keyof F as ExcludeMappedString<P>]: {
[FF in keyof F[P] as ExcludeMappedString<FF>]: RedisCommandSignature<F[P][FF]>;
};
};
export type WithScripts<S extends RedisScripts> = {
[P in keyof S as ExcludeMappedString<P>]: RedisCommandSignature<S[P]>;
};
export type RedisClientType<
M extends RedisModules = Record<string, never>,
F extends RedisFunctions = Record<string, never>,
S extends RedisScripts = Record<string, never>
> = RedisClient<M, F, S> & WithCommands & WithModules<M> & WithFunctions<F> & WithScripts<S>;
export type InstantiableRedisClient<
M extends RedisModules,
F extends RedisFunctions,
S extends RedisScripts
> = new (options?: RedisClientOptions<M, F, S>) => RedisClientType<M, F, S>;
export interface ClientCommandOptions extends QueueCommandOptions {
isolated?: boolean;
}
type ClientLegacyCallback = (err: Error | null, reply?: RedisCommandRawReply) => void;
export default class RedisClient<
M extends RedisModules,
F extends RedisFunctions,
S extends RedisScripts
> extends EventEmitter {
static commandOptions<T extends ClientCommandOptions>(options: T): CommandOptions<T> {
return commandOptions(options);
}
commandOptions = RedisClient.commandOptions;
static extend<
M extends RedisModules,
F extends RedisFunctions,
S extends RedisScripts
>(extensions?: RedisExtensions<M, F, S>): InstantiableRedisClient<M, F, S> {
const Client = attachExtensions({
BaseClass: RedisClient,
modulesExecutor: RedisClient.prototype.commandsExecutor,
modules: extensions?.modules,
functionsExecutor: RedisClient.prototype.functionsExecuter,
functions: extensions?.functions,
scriptsExecutor: RedisClient.prototype.scriptsExecuter,
scripts: extensions?.scripts
});
if (Client !== RedisClient) {
Client.prototype.Multi = RedisClientMultiCommand.extend(extensions);
}
return Client;
}
static create<
M extends RedisModules,
F extends RedisFunctions,
S extends RedisScripts
>(options?: RedisClientOptions<M, F, S>): RedisClientType<M, F, S> {
return new (RedisClient.extend(options))(options);
}
static parseURL(url: string): RedisClientOptions {
// https://www.iana.org/assignments/uri-schemes/prov/redis
const { hostname, port, protocol, username, password, pathname } = new URL(url),
parsed: RedisClientOptions = {
socket: {
host: hostname
}
};
if (protocol === 'rediss:') {
(parsed.socket as RedisTlsSocketOptions).tls = true;
} else if (protocol !== 'redis:') {
throw new TypeError('Invalid protocol');
}
if (port) {
(parsed.socket as TcpSocketConnectOpts).port = Number(port);
}
if (username) {
parsed.username = decodeURIComponent(username);
}
if (password) {
parsed.password = decodeURIComponent(password);
}
if (pathname.length > 1) {
const database = Number(pathname.substring(1));
if (isNaN(database)) {
throw new TypeError('Invalid pathname');
}
parsed.database = database;
}
return parsed;
}
readonly #options?: RedisClientOptions<M, F, S>;
readonly #socket: RedisSocket;
readonly #queue: RedisCommandsQueue;
#isolationPool?: Pool<RedisClientType<M, F, S>>;
readonly #v4: Record<string, any> = {};
#selectedDB = 0;
get options(): RedisClientOptions<M, F, S> | undefined {
return this.#options;
}
get isOpen(): boolean {
return this.#socket.isOpen;
}
get isReady(): boolean {
return this.#socket.isReady;
}
get isPubSubActive() {
return this.#queue.isPubSubActive;
}
get v4(): Record<string, any> {
if (!this.#options?.legacyMode) {
throw new Error('the client is not in "legacy mode"');
}
return this.#v4;
}
constructor(options?: RedisClientOptions<M, F, S>) {
super();
this.#options = this.#initiateOptions(options);
this.#queue = this.#initiateQueue();
this.#socket = this.#initiateSocket();
// should be initiated in connect, not here
// TODO: consider breaking in v5
this.#isolationPool = this.#initiateIsolationPool();
this.#legacyMode();
}
#initiateOptions(options?: RedisClientOptions<M, F, S>): RedisClientOptions<M, F, S> | undefined {
if (options?.url) {
const parsed = RedisClient.parseURL(options.url);
if (options.socket) {
parsed.socket = Object.assign(options.socket, parsed.socket);
}
Object.assign(options, parsed);
}
if (options?.database) {
this.#selectedDB = options.database;
}
return options;
}
#initiateQueue(): RedisCommandsQueue {
return new RedisCommandsQueue(
this.#options?.commandsQueueMaxLength,
(channel, listeners) => this.emit('sharded-channel-moved', channel, listeners)
);
}
#initiateSocket(): RedisSocket {
const socketInitiator = async (): Promise<void> => {
const promises = [];
if (this.#selectedDB !== 0) {
promises.push(
this.#queue.addCommand(
['SELECT', this.#selectedDB.toString()],
{ asap: true }
)
);
}
if (this.#options?.readonly) {
promises.push(
this.#queue.addCommand(
COMMANDS.READONLY.transformArguments(),
{ asap: true }
)
);
}
if (!this.#options?.disableClientInfo) {
promises.push(
this.#queue.addCommand(
[ 'CLIENT', 'SETINFO', 'LIB-VER', version],
{ asap: true }
).catch(err => {
if (!(err instanceof ErrorReply)) {
throw err;
}
})
);
promises.push(
this.#queue.addCommand(
[
'CLIENT', 'SETINFO', 'LIB-NAME',
this.#options?.clientInfoTag ? `node-redis(${this.#options.clientInfoTag})` : 'node-redis'
],
{ asap: true }
).catch(err => {
if (!(err instanceof ErrorReply)) {
throw err;
}
})
);
}
if (this.#options?.name) {
promises.push(
this.#queue.addCommand(
COMMANDS.CLIENT_SETNAME.transformArguments(this.#options.name),
{ asap: true }
)
);
}
if (this.#options?.username || this.#options?.password) {
promises.push(
this.#queue.addCommand(
COMMANDS.AUTH.transformArguments({
username: this.#options.username,
password: this.#options.password ?? ''
}),
{ asap: true }
)
);
}
const resubscribePromise = this.#queue.resubscribe();
if (resubscribePromise) {
promises.push(resubscribePromise);
}
if (promises.length) {
this.#tick(true);
await Promise.all(promises);
}
};
return new RedisSocket(socketInitiator, this.#options?.socket)
.on('data', chunk => this.#queue.onReplyChunk(chunk))
.on('error', err => {
this.emit('error', err);
if (this.#socket.isOpen && !this.#options?.disableOfflineQueue) {
this.#queue.flushWaitingForReply(err);
} else {
this.#queue.flushAll(err);
}
})
.on('connect', () => {
this.emit('connect');
})
.on('ready', () => {
this.emit('ready');
this.#setPingTimer();
this.#tick();
})
.on('reconnecting', () => this.emit('reconnecting'))
.on('drain', () => this.#tick())
.on('end', () => this.emit('end'));
}
#initiateIsolationPool() {
return createPool({
create: async () => {
const duplicate = this.duplicate({
isolationPoolOptions: undefined
}).on('error', err => this.emit('error', err));
await duplicate.connect();
return duplicate;
},
destroy: client => client.disconnect()
}, this.#options?.isolationPoolOptions);
}
#legacyMode(): void {
if (!this.#options?.legacyMode) return;
(this as any).#v4.sendCommand = this.#sendCommand.bind(this);
(this as any).sendCommand = (...args: Array<any>): void => {
const result = this.#legacySendCommand(...args);
if (result) {
result.promise
.then(reply => result.callback(null, reply))
.catch(err => result.callback(err));
}
};
for (const [ name, command ] of Object.entries(COMMANDS as RedisCommands)) {
this.#defineLegacyCommand(name, command);
(this as any)[name.toLowerCase()] ??= (this as any)[name];
}
// hard coded commands
this.#defineLegacyCommand('SELECT');
this.#defineLegacyCommand('select');
this.#defineLegacyCommand('SUBSCRIBE');
this.#defineLegacyCommand('subscribe');
this.#defineLegacyCommand('PSUBSCRIBE');
this.#defineLegacyCommand('pSubscribe');
this.#defineLegacyCommand('UNSUBSCRIBE');
this.#defineLegacyCommand('unsubscribe');
this.#defineLegacyCommand('PUNSUBSCRIBE');
this.#defineLegacyCommand('pUnsubscribe');
this.#defineLegacyCommand('QUIT');
this.#defineLegacyCommand('quit');
}
#legacySendCommand(...args: Array<any>) {
const callback = typeof args[args.length - 1] === 'function' ?
args.pop() as ClientLegacyCallback :
undefined;
const promise = this.#sendCommand(transformLegacyCommandArguments(args));
if (callback) return {
promise,
callback
};
promise.catch(err => this.emit('error', err));
}
#defineLegacyCommand(name: string, command?: RedisCommand): void {
this.#v4[name] = (this as any)[name].bind(this);
(this as any)[name] = command && command.TRANSFORM_LEGACY_REPLY && command.transformReply ?
(...args: Array<unknown>) => {
const result = this.#legacySendCommand(name, ...args);
if (result) {
result.promise
.then(reply => result.callback(null, command.transformReply!(reply)))
.catch(err => result.callback(err));
}
} :
(...args: Array<unknown>) => (this as any).sendCommand(name, ...args);
}
#pingTimer?: NodeJS.Timeout;
#setPingTimer(): void {
if (!this.#options?.pingInterval || !this.#socket.isReady) return;
clearTimeout(this.#pingTimer);
this.#pingTimer = setTimeout(() => {
if (!this.#socket.isReady) return;
// using #sendCommand to support legacy mode
this.#sendCommand(['PING'])
.then(reply => this.emit('ping-interval', reply))
.catch(err => this.emit('error', err))
.finally(() => this.#setPingTimer());
}, this.#options.pingInterval);
}
duplicate(overrides?: Partial<RedisClientOptions<M, F, S>>): RedisClientType<M, F, S> {
return new (Object.getPrototypeOf(this).constructor)({
...this.#options,
...overrides
});
}
async connect() {
// see comment in constructor
this.#isolationPool ??= this.#initiateIsolationPool();
await this.#socket.connect();
return this as unknown as RedisClientType<M, F, S>;
}
async commandsExecutor<C extends RedisCommand>(
command: C,
args: Array<unknown>
): Promise<RedisCommandReply<C>> {
const { args: redisArgs, options } = transformCommandArguments(command, args);
return transformCommandReply(
command,
await this.#sendCommand(redisArgs, options),
redisArgs.preserve
);
}
sendCommand<T = RedisCommandRawReply>(
args: RedisCommandArguments,
options?: ClientCommandOptions
): Promise<T> {
return this.#sendCommand(args, options);
}
// using `#sendCommand` cause `sendCommand` is overwritten in legacy mode
#sendCommand<T = RedisCommandRawReply>(
args: RedisCommandArguments,
options?: ClientCommandOptions
): Promise<T> {
if (!this.#socket.isOpen) {
return Promise.reject(new ClientClosedError());
} else if (options?.isolated) {
return this.executeIsolated(isolatedClient =>
isolatedClient.sendCommand(args, {
...options,
isolated: false
})
);
} else if (!this.#socket.isReady && this.#options?.disableOfflineQueue) {
return Promise.reject(new ClientOfflineError());
}
const promise = this.#queue.addCommand<T>(args, options);
this.#tick();
return promise;
}
async functionsExecuter<F extends RedisFunction>(
fn: F,
args: Array<unknown>,
name: string
): Promise<RedisCommandReply<F>> {
const { args: redisArgs, options } = transformCommandArguments(fn, args);
return transformCommandReply(
fn,
await this.executeFunction(name, fn, redisArgs, options),
redisArgs.preserve
);
}
executeFunction(
name: string,
fn: RedisFunction,
args: RedisCommandArguments,
options?: ClientCommandOptions
): Promise<RedisCommandRawReply> {
return this.#sendCommand(
fCallArguments(name, fn, args),
options
);
}
async scriptsExecuter<S extends RedisScript>(
script: S,
args: Array<unknown>
): Promise<RedisCommandReply<S>> {
const { args: redisArgs, options } = transformCommandArguments(script, args);
return transformCommandReply(
script,
await this.executeScript(script, redisArgs, options),
redisArgs.preserve
);
}
async executeScript(
script: RedisScript,
args: RedisCommandArguments,
options?: ClientCommandOptions
): Promise<RedisCommandRawReply> {
const redisArgs: RedisCommandArguments = ['EVALSHA', script.SHA1];
if (script.NUMBER_OF_KEYS !== undefined) {
redisArgs.push(script.NUMBER_OF_KEYS.toString());
}
redisArgs.push(...args);
try {
return await this.#sendCommand(redisArgs, options);
} catch (err: any) {
if (!err?.message?.startsWith?.('NOSCRIPT')) {
throw err;
}
redisArgs[0] = 'EVAL';
redisArgs[1] = script.SCRIPT;
return this.#sendCommand(redisArgs, options);
}
}
async SELECT(db: number): Promise<void>;
async SELECT(options: CommandOptions<ClientCommandOptions>, db: number): Promise<void>;
async SELECT(options?: any, db?: any): Promise<void> {
if (!isCommandOptions(options)) {
db = options;
options = null;
}
await this.#sendCommand(['SELECT', db.toString()], options);
this.#selectedDB = db;
}
select = this.SELECT;
#pubSubCommand(promise: Promise<void> | undefined) {
if (promise === undefined) return Promise.resolve();
this.#tick();
return promise;
}
SUBSCRIBE<T extends boolean = false>(
channels: string | Array<string>,
listener: PubSubListener<T>,
bufferMode?: T
): Promise<void> {
return this.#pubSubCommand(
this.#queue.subscribe(
PubSubType.CHANNELS,
channels,
listener,
bufferMode
)
);
}
subscribe = this.SUBSCRIBE;
UNSUBSCRIBE<T extends boolean = false>(
channels?: string | Array<string>,
listener?: PubSubListener<T>,
bufferMode?: T
): Promise<void> {
return this.#pubSubCommand(
this.#queue.unsubscribe(
PubSubType.CHANNELS,
channels,
listener,
bufferMode
)
);
}
unsubscribe = this.UNSUBSCRIBE;
PSUBSCRIBE<T extends boolean = false>(
patterns: string | Array<string>,
listener: PubSubListener<T>,
bufferMode?: T
): Promise<void> {
return this.#pubSubCommand(
this.#queue.subscribe(
PubSubType.PATTERNS,
patterns,
listener,
bufferMode
)
);
}
pSubscribe = this.PSUBSCRIBE;
PUNSUBSCRIBE<T extends boolean = false>(
patterns?: string | Array<string>,
listener?: PubSubListener<T>,
bufferMode?: T
): Promise<void> {
return this.#pubSubCommand(
this.#queue.unsubscribe(
PubSubType.PATTERNS,
patterns,
listener,
bufferMode
)
);
}
pUnsubscribe = this.PUNSUBSCRIBE;
SSUBSCRIBE<T extends boolean = false>(
channels: string | Array<string>,
listener: PubSubListener<T>,
bufferMode?: T
): Promise<void> {
return this.#pubSubCommand(
this.#queue.subscribe(
PubSubType.SHARDED,
channels,
listener,
bufferMode
)
);
}
sSubscribe = this.SSUBSCRIBE;
SUNSUBSCRIBE<T extends boolean = false>(
channels?: string | Array<string>,
listener?: PubSubListener<T>,
bufferMode?: T
): Promise<void> {
return this.#pubSubCommand(
this.#queue.unsubscribe(
PubSubType.SHARDED,
channels,
listener,
bufferMode
)
);
}
sUnsubscribe = this.SUNSUBSCRIBE;
getPubSubListeners(type: PubSubType) {
return this.#queue.getPubSubListeners(type);
}
extendPubSubChannelListeners(
type: PubSubType,
channel: string,
listeners: ChannelListeners
) {
return this.#pubSubCommand(
this.#queue.extendPubSubChannelListeners(type, channel, listeners)
);
}
extendPubSubListeners(type: PubSubType, listeners: PubSubTypeListeners) {
return this.#pubSubCommand(
this.#queue.extendPubSubListeners(type, listeners)
);
}
QUIT(): Promise<string> {
return this.#socket.quit(async () => {
if (this.#pingTimer) clearTimeout(this.#pingTimer);
const quitPromise = this.#queue.addCommand<string>(['QUIT']);
this.#tick();
const [reply] = await Promise.all([
quitPromise,
this.#destroyIsolationPool()
]);
return reply;
});
}
quit = this.QUIT;
#tick(force = false): void {
if (this.#socket.writableNeedDrain || (!force && !this.#socket.isReady)) {
return;
}
this.#socket.cork();
while (!this.#socket.writableNeedDrain) {
const args = this.#queue.getCommandToSend();
if (args === undefined) break;
this.#socket.writeCommand(args);
}
}
executeIsolated<T>(fn: (client: RedisClientType<M, F, S>) => T | Promise<T>): Promise<T> {
if (!this.#isolationPool) return Promise.reject(new ClientClosedError());
return this.#isolationPool.use(fn);
}
MULTI(): RedisClientMultiCommandType<M, F, S> {
return new (this as any).Multi(
this.multiExecutor.bind(this),
this.#options?.legacyMode
);
}
multi = this.MULTI;
async multiExecutor(
commands: Array<RedisMultiQueuedCommand>,
selectedDB?: number,
chainId?: symbol
): Promise<Array<RedisCommandRawReply>> {
if (!this.#socket.isOpen) {
return Promise.reject(new ClientClosedError());
}
const promise = chainId ?
// if `chainId` has a value, it's a `MULTI` (and not "pipeline") - need to add the `MULTI` and `EXEC` commands
Promise.all([
this.#queue.addCommand(['MULTI'], { chainId }),
this.#addMultiCommands(commands, chainId),
this.#queue.addCommand(['EXEC'], { chainId })
]) :
this.#addMultiCommands(commands);
this.#tick();
const results = await promise;
if (selectedDB !== undefined) {
this.#selectedDB = selectedDB;
}
return results;
}
#addMultiCommands(commands: Array<RedisMultiQueuedCommand>, chainId?: symbol) {
return Promise.all(
commands.map(({ args }) => this.#queue.addCommand(args, { chainId }))
);
}
async* scanIterator(options?: ScanCommandOptions): AsyncIterable<string> {
let cursor = 0;
do {
const reply = await (this as any).scan(cursor, options);
cursor = reply.cursor;
for (const key of reply.keys) {
yield key;
}
} while (cursor !== 0);
}
async* hScanIterator(key: string, options?: ScanOptions): AsyncIterable<ConvertArgumentType<HScanTuple, string>> {
let cursor = 0;
do {
const reply = await (this as any).hScan(key, cursor, options);
cursor = reply.cursor;
for (const tuple of reply.tuples) {
yield tuple;
}
} while (cursor !== 0);
}
async* hScanNoValuesIterator(key: string, options?: ScanOptions): AsyncIterable<ConvertArgumentType<RedisCommandArgument, string>> {
let cursor = 0;
do {
const reply = await (this as any).hScanNoValues(key, cursor, options);
cursor = reply.cursor;
for (const k of reply.keys) {
yield k;
}
} while (cursor !== 0);
}
async* sScanIterator(key: string, options?: ScanOptions): AsyncIterable<string> {
let cursor = 0;
do {
const reply = await (this as any).sScan(key, cursor, options);
cursor = reply.cursor;
for (const member of reply.members) {
yield member;
}
} while (cursor !== 0);
}
async* zScanIterator(key: string, options?: ScanOptions): AsyncIterable<ConvertArgumentType<ZMember, string>> {
let cursor = 0;
do {
const reply = await (this as any).zScan(key, cursor, options);
cursor = reply.cursor;
for (const member of reply.members) {
yield member;
}
} while (cursor !== 0);
}
async disconnect(): Promise<void> {
if (this.#pingTimer) clearTimeout(this.#pingTimer);
this.#queue.flushAll(new DisconnectsClientError());
this.#socket.disconnect();
await this.#destroyIsolationPool();
}
async #destroyIsolationPool(): Promise<void> {
await this.#isolationPool!.drain();
await this.#isolationPool!.clear();
this.#isolationPool = undefined;
}
ref(): void {
this.#socket.ref();
}
unref(): void {
this.#socket.unref();
}
}
attachCommands({
BaseClass: RedisClient,
commands: COMMANDS,
executor: RedisClient.prototype.commandsExecutor
});
(RedisClient.prototype as any).Multi = RedisClientMultiCommand;