Skip to content

Commit 27537b0

Browse files
authored
fix(cluster): replace native private with _ (#2971)
Private class fields (e.g., #execute) cause runtime errors when accessed from contexts where `this` is not the exact instance, triggering “Receiver must be an instance of class RedisCluster”. fixes #2967
1 parent 9ea260f commit 27537b0

File tree

1 file changed

+48
-48
lines changed

1 file changed

+48
-48
lines changed

packages/client/lib/cluster/index.ts

Lines changed: 48 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ export default class RedisCluster<
188188
const parser = new BasicCommandParser();
189189
command.parseCommand(parser, ...args);
190190

191-
return this._self.#execute(
191+
return this._self._execute(
192192
parser.firstKey,
193193
command.IS_READ_ONLY,
194194
this._commandOptions,
@@ -204,7 +204,7 @@ export default class RedisCluster<
204204
const parser = new BasicCommandParser();
205205
command.parseCommand(parser, ...args);
206206

207-
return this._self.#execute(
207+
return this._self._execute(
208208
parser.firstKey,
209209
command.IS_READ_ONLY,
210210
this._self._commandOptions,
@@ -222,7 +222,7 @@ export default class RedisCluster<
222222
parser.push(...prefix);
223223
fn.parseCommand(parser, ...args);
224224

225-
return this._self.#execute(
225+
return this._self._execute(
226226
parser.firstKey,
227227
fn.IS_READ_ONLY,
228228
this._self._commandOptions,
@@ -240,7 +240,7 @@ export default class RedisCluster<
240240
parser.push(...prefix);
241241
script.parseCommand(parser, ...args);
242242

243-
return this._self.#execute(
243+
return this._self._execute(
244244
parser.firstKey,
245245
script.IS_READ_ONLY,
246246
this._commandOptions,
@@ -293,9 +293,9 @@ export default class RedisCluster<
293293
return RedisCluster.factory(options)(options);
294294
}
295295

296-
readonly #options: RedisClusterOptions<M, F, S, RESP, TYPE_MAPPING/*, POLICIES*/>;
296+
readonly _options: RedisClusterOptions<M, F, S, RESP, TYPE_MAPPING/*, POLICIES*/>;
297297

298-
readonly #slots: RedisClusterSlots<M, F, S, RESP, TYPE_MAPPING>;
298+
readonly _slots: RedisClusterSlots<M, F, S, RESP, TYPE_MAPPING>;
299299

300300
private _self = this;
301301
private _commandOptions?: ClusterCommandOptions<TYPE_MAPPING/*, POLICIES*/>;
@@ -305,53 +305,53 @@ export default class RedisCluster<
305305
* Use with {@link RedisCluster.prototype.nodeClient} to get the client for a specific node (master or replica).
306306
*/
307307
get slots() {
308-
return this._self.#slots.slots;
308+
return this._self._slots.slots;
309309
}
310310

311311
get clientSideCache() {
312-
return this._self.#slots.clientSideCache;
312+
return this._self._slots.clientSideCache;
313313
}
314314

315315
/**
316316
* An array of the cluster masters.
317317
* Use with {@link RedisCluster.prototype.nodeClient} to get the client for a specific master node.
318318
*/
319319
get masters() {
320-
return this._self.#slots.masters;
320+
return this._self._slots.masters;
321321
}
322322

323323
/**
324324
* An array of the cluster replicas.
325325
* Use with {@link RedisCluster.prototype.nodeClient} to get the client for a specific replica node.
326326
*/
327327
get replicas() {
328-
return this._self.#slots.replicas;
328+
return this._self._slots.replicas;
329329
}
330330

331331
/**
332332
* A map form a node address (`<host>:<port>`) to its shard, each shard contain its `master` and `replicas`.
333333
* Use with {@link RedisCluster.prototype.nodeClient} to get the client for a specific node (master or replica).
334334
*/
335335
get nodeByAddress() {
336-
return this._self.#slots.nodeByAddress;
336+
return this._self._slots.nodeByAddress;
337337
}
338338

339339
/**
340340
* The current pub/sub node.
341341
*/
342342
get pubSubNode() {
343-
return this._self.#slots.pubSubNode;
343+
return this._self._slots.pubSubNode;
344344
}
345345

346346
get isOpen() {
347-
return this._self.#slots.isOpen;
347+
return this._self._slots.isOpen;
348348
}
349349

350350
constructor(options: RedisClusterOptions<M, F, S, RESP, TYPE_MAPPING/*, POLICIES*/>) {
351351
super();
352352

353-
this.#options = options;
354-
this.#slots = new RedisClusterSlots(options, this.emit.bind(this));
353+
this._options = options;
354+
this._slots = new RedisClusterSlots(options, this.emit.bind(this));
355355

356356
if (options?.commandOptions) {
357357
this._commandOptions = options.commandOptions;
@@ -366,14 +366,14 @@ export default class RedisCluster<
366366
_TYPE_MAPPING extends TypeMapping = TYPE_MAPPING
367367
>(overrides?: Partial<RedisClusterOptions<_M, _F, _S, _RESP, _TYPE_MAPPING>>) {
368368
return new (Object.getPrototypeOf(this).constructor)({
369-
...this._self.#options,
369+
...this._self._options,
370370
commandOptions: this._commandOptions,
371371
...overrides
372372
}) as RedisClusterType<_M, _F, _S, _RESP, _TYPE_MAPPING>;
373373
}
374374

375375
async connect() {
376-
await this._self.#slots.connect();
376+
await this._self._slots.connect();
377377
return this as unknown as RedisClusterType<M, F, S, RESP, TYPE_MAPPING>;
378378
}
379379

@@ -429,7 +429,7 @@ export default class RedisCluster<
429429
// return this._commandOptionsProxy('policies', policies);
430430
// }
431431

432-
#handleAsk<T>(
432+
_handleAsk<T>(
433433
fn: (client: RedisClientType<M, F, S, RESP, TYPE_MAPPING>, opts?: ClusterCommandOptions) => Promise<T>
434434
) {
435435
return async (client: RedisClientType<M, F, S, RESP, TYPE_MAPPING>, options?: ClusterCommandOptions) => {
@@ -450,14 +450,14 @@ export default class RedisCluster<
450450
};
451451
}
452452

453-
async #execute<T>(
453+
async _execute<T>(
454454
firstKey: RedisArgument | undefined,
455455
isReadonly: boolean | undefined,
456456
options: ClusterCommandOptions | undefined,
457457
fn: (client: RedisClientType<M, F, S, RESP, TYPE_MAPPING>, opts?: ClusterCommandOptions) => Promise<T>
458458
): Promise<T> {
459-
const maxCommandRedirections = this.#options.maxCommandRedirections ?? 16;
460-
let client = await this.#slots.getClient(firstKey, isReadonly);
459+
const maxCommandRedirections = this._options.maxCommandRedirections ?? 16;
460+
let client = await this._slots.getClient(firstKey, isReadonly);
461461
let i = 0;
462462

463463
let myFn = fn;
@@ -475,24 +475,24 @@ export default class RedisCluster<
475475

476476
if (err.message.startsWith('ASK')) {
477477
const address = err.message.substring(err.message.lastIndexOf(' ') + 1);
478-
let redirectTo = await this.#slots.getMasterByAddress(address);
478+
let redirectTo = await this._slots.getMasterByAddress(address);
479479
if (!redirectTo) {
480-
await this.#slots.rediscover(client);
481-
redirectTo = await this.#slots.getMasterByAddress(address);
480+
await this._slots.rediscover(client);
481+
redirectTo = await this._slots.getMasterByAddress(address);
482482
}
483483

484484
if (!redirectTo) {
485485
throw new Error(`Cannot find node ${address}`);
486486
}
487487

488488
client = redirectTo;
489-
myFn = this.#handleAsk(fn);
489+
myFn = this._handleAsk(fn);
490490
continue;
491491
}
492492

493493
if (err.message.startsWith('MOVED')) {
494-
await this.#slots.rediscover(client);
495-
client = await this.#slots.getClient(firstKey, isReadonly);
494+
await this._slots.rediscover(client);
495+
client = await this._slots.getClient(firstKey, isReadonly);
496496
continue;
497497
}
498498

@@ -508,7 +508,7 @@ export default class RedisCluster<
508508
options?: ClusterCommandOptions,
509509
// defaultPolicies?: CommandPolicies
510510
): Promise<T> {
511-
return this._self.#execute(
511+
return this._self._execute(
512512
firstKey,
513513
isReadonly,
514514
options,
@@ -520,11 +520,11 @@ export default class RedisCluster<
520520
type Multi = new (...args: ConstructorParameters<typeof RedisClusterMultiCommand>) => RedisClusterMultiCommandType<[], M, F, S, RESP, TYPE_MAPPING>;
521521
return new ((this as any).Multi as Multi)(
522522
async (firstKey, isReadonly, commands) => {
523-
const client = await this._self.#slots.getClient(firstKey, isReadonly);
523+
const client = await this._self._slots.getClient(firstKey, isReadonly);
524524
return client._executeMulti(commands);
525525
},
526526
async (firstKey, isReadonly, commands) => {
527-
const client = await this._self.#slots.getClient(firstKey, isReadonly);
527+
const client = await this._self._slots.getClient(firstKey, isReadonly);
528528
return client._executePipeline(commands);
529529
},
530530
routing,
@@ -539,7 +539,7 @@ export default class RedisCluster<
539539
listener: PubSubListener<T>,
540540
bufferMode?: T
541541
) {
542-
return (await this._self.#slots.getPubSubClient())
542+
return (await this._self._slots.getPubSubClient())
543543
.SUBSCRIBE(channels, listener, bufferMode);
544544
}
545545

@@ -550,7 +550,7 @@ export default class RedisCluster<
550550
listener?: PubSubListener<boolean>,
551551
bufferMode?: T
552552
) {
553-
return this._self.#slots.executeUnsubscribeCommand(client =>
553+
return this._self._slots.executeUnsubscribeCommand(client =>
554554
client.UNSUBSCRIBE(channels, listener, bufferMode)
555555
);
556556
}
@@ -562,7 +562,7 @@ export default class RedisCluster<
562562
listener: PubSubListener<T>,
563563
bufferMode?: T
564564
) {
565-
return (await this._self.#slots.getPubSubClient())
565+
return (await this._self._slots.getPubSubClient())
566566
.PSUBSCRIBE(patterns, listener, bufferMode);
567567
}
568568

@@ -573,7 +573,7 @@ export default class RedisCluster<
573573
listener?: PubSubListener<T>,
574574
bufferMode?: T
575575
) {
576-
return this._self.#slots.executeUnsubscribeCommand(client =>
576+
return this._self._slots.executeUnsubscribeCommand(client =>
577577
client.PUNSUBSCRIBE(patterns, listener, bufferMode)
578578
);
579579
}
@@ -585,9 +585,9 @@ export default class RedisCluster<
585585
listener: PubSubListener<T>,
586586
bufferMode?: T
587587
) {
588-
const maxCommandRedirections = this._self.#options.maxCommandRedirections ?? 16,
588+
const maxCommandRedirections = this._self._options.maxCommandRedirections ?? 16,
589589
firstChannel = Array.isArray(channels) ? channels[0] : channels;
590-
let client = await this._self.#slots.getShardedPubSubClient(firstChannel);
590+
let client = await this._self._slots.getShardedPubSubClient(firstChannel);
591591
for (let i = 0; ; i++) {
592592
try {
593593
return await client.SSUBSCRIBE(channels, listener, bufferMode);
@@ -597,8 +597,8 @@ export default class RedisCluster<
597597
}
598598

599599
if (err.message.startsWith('MOVED')) {
600-
await this._self.#slots.rediscover(client);
601-
client = await this._self.#slots.getShardedPubSubClient(firstChannel);
600+
await this._self._slots.rediscover(client);
601+
client = await this._self._slots.getShardedPubSubClient(firstChannel);
602602
continue;
603603
}
604604

@@ -614,7 +614,7 @@ export default class RedisCluster<
614614
listener?: PubSubListener<T>,
615615
bufferMode?: T
616616
) {
617-
return this._self.#slots.executeShardedUnsubscribeCommand(
617+
return this._self._slots.executeShardedUnsubscribeCommand(
618618
Array.isArray(channels) ? channels[0] : channels,
619619
client => client.SUNSUBSCRIBE(channels, listener, bufferMode)
620620
);
@@ -626,44 +626,44 @@ export default class RedisCluster<
626626
* @deprecated Use `close` instead.
627627
*/
628628
quit() {
629-
return this._self.#slots.quit();
629+
return this._self._slots.quit();
630630
}
631631

632632
/**
633633
* @deprecated Use `destroy` instead.
634634
*/
635635
disconnect() {
636-
return this._self.#slots.disconnect();
636+
return this._self._slots.disconnect();
637637
}
638638

639639
close() {
640-
this._self.#slots.clientSideCache?.onPoolClose();
641-
return this._self.#slots.close();
640+
this._self._slots.clientSideCache?.onPoolClose();
641+
return this._self._slots.close();
642642
}
643643

644644
destroy() {
645-
this._self.#slots.clientSideCache?.onPoolClose();
646-
return this._self.#slots.destroy();
645+
this._self._slots.clientSideCache?.onPoolClose();
646+
return this._self._slots.destroy();
647647
}
648648

649649
nodeClient(node: ShardNode<M, F, S, RESP, TYPE_MAPPING>) {
650-
return this._self.#slots.nodeClient(node);
650+
return this._self._slots.nodeClient(node);
651651
}
652652

653653
/**
654654
* Returns a random node from the cluster.
655655
* Userful for running "forward" commands (like PUBLISH) on a random node.
656656
*/
657657
getRandomNode() {
658-
return this._self.#slots.getRandomNode();
658+
return this._self._slots.getRandomNode();
659659
}
660660

661661
/**
662662
* Get a random node from a slot.
663663
* Useful for running readonly commands on a slot.
664664
*/
665665
getSlotRandomNode(slot: number) {
666-
return this._self.#slots.getSlotRandomNode(slot);
666+
return this._self._slots.getSlotRandomNode(slot);
667667
}
668668

669669
/**

0 commit comments

Comments
 (0)