Skip to content

Commit 2309d09

Browse files
committed
fix socket type issues
1 parent cc85112 commit 2309d09

File tree

3 files changed

+68
-76
lines changed

3 files changed

+68
-76
lines changed

packages/client/lib/cluster/cluster-slots.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { EventEmitter } from 'node:stream';
55
import { ChannelListeners, PUBSUB_TYPE, PubSubTypeListeners } from '../client/pub-sub';
66
import { RedisArgument, RedisFunctions, RedisModules, RedisScripts, RespVersions, TypeMapping } from '../RESP/types';
77
import calculateSlot from 'cluster-key-slot';
8+
import { RedisSocketOptions } from '../client/socket';
89

910
interface NodeAddress {
1011
host: string;
@@ -102,8 +103,8 @@ export default class RedisClusterSlots<
102103
> {
103104
static #SLOTS = 16384;
104105

105-
readonly #options: RedisClusterOptions<M, F, S, RESP, TYPE_MAPPING>;
106-
readonly #clientFactory: ReturnType<typeof RedisClient.factory<M, F, S, RESP>>;
106+
readonly #options;
107+
readonly #clientFactory;
107108
readonly #emit: EventEmitter['emit'];
108109
slots = new Array<Shard<M, F, S, RESP, TYPE_MAPPING>>(RedisClusterSlots.#SLOTS);
109110
masters = new Array<MasterNode<M, F, S, RESP, TYPE_MAPPING>>();
@@ -271,7 +272,7 @@ export default class RedisClusterSlots<
271272
return {
272273
...this.#options.defaults,
273274
...options,
274-
socket
275+
socket: socket as RedisSocketOptions
275276
};
276277
}
277278

@@ -316,8 +317,7 @@ export default class RedisClusterSlots<
316317
host: node.host,
317318
port: node.port
318319
},
319-
readonly,
320-
RESP: this.#options.RESP
320+
readonly
321321
})
322322
).on('error', err => console.error(err));
323323
}

packages/client/lib/sentinel/index.ts

Lines changed: 55 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -649,19 +649,15 @@ class RedisSentinelInternal<
649649
}
650650

651651
#createClient(node: RedisNode, clientOptions: RedisClientOptions, reconnectStrategy?: undefined | false) {
652-
const options = { ...clientOptions } as RedisClientOptions;
653-
654-
if (clientOptions.socket) {
655-
options.socket = { ...clientOptions.socket };
656-
} else {
657-
options.socket = {};
658-
}
659-
660-
options.socket.host = node.host;
661-
options.socket.port = node.port;
662-
options.socket.reconnectStrategy = reconnectStrategy;
663-
664-
return RedisClient.create(options);
652+
return RedisClient.create({
653+
...clientOptions,
654+
socket: {
655+
...clientOptions.socket,
656+
host: node.host,
657+
port: node.port,
658+
reconnectStrategy
659+
}
660+
});
665661
}
666662

667663
getClientLease(): ClientInfo | Promise<ClientInfo> {
@@ -1332,16 +1328,16 @@ export class RedisSentinelFactory extends EventEmitter {
13321328

13331329
async updateSentinelRootNodes() {
13341330
for (const node of this.#sentinelRootNodes) {
1335-
const options: RedisClientOptions = { ...this.options.sentinelClientOptions };
1336-
if (options.socket === undefined) {
1337-
options.socket = {};
1338-
}
1339-
options.socket.host = node.host;
1340-
options.socket.port = node.port;
1341-
options.socket.reconnectStrategy = false;
1342-
options.modules = RedisSentinelModule;
1343-
1344-
const client = RedisClient.create(options).on('error', (err) => this.emit(`updateSentinelRootNodes: ${err}`));
1331+
const client = RedisClient.create({
1332+
...this.options.sentinelClientOptions,
1333+
socket: {
1334+
...this.options.sentinelClientOptions?.socket,
1335+
host: node.host,
1336+
port: node.port,
1337+
reconnectStrategy: false
1338+
},
1339+
modules: RedisSentinelModule
1340+
}).on('error', (err) => this.emit(`updateSentinelRootNodes: ${err}`));
13451341
try {
13461342
await client.connect();
13471343
} catch {
@@ -1367,16 +1363,16 @@ export class RedisSentinelFactory extends EventEmitter {
13671363
let connected = false;
13681364

13691365
for (const node of this.#sentinelRootNodes) {
1370-
const options: RedisClientOptions = { ...this.options.sentinelClientOptions };
1371-
if (options.socket === undefined) {
1372-
options.socket = {};
1373-
}
1374-
options.socket.host = node.host;
1375-
options.socket.port = node.port;
1376-
options.socket.reconnectStrategy = false;
1377-
options.modules = RedisSentinelModule;
1378-
1379-
const client = RedisClient.create(options).on('error', err => this.emit(`getMasterNode: ${err}`));
1366+
const client = RedisClient.create({
1367+
...this.options.sentinelClientOptions,
1368+
socket: {
1369+
...this.options.sentinelClientOptions?.socket,
1370+
host: node.host,
1371+
port: node.port,
1372+
reconnectStrategy: false
1373+
},
1374+
modules: RedisSentinelModule
1375+
}).on('error', err => this.emit(`getMasterNode: ${err}`));
13801376

13811377
try {
13821378
await client.connect();
@@ -1412,30 +1408,30 @@ export class RedisSentinelFactory extends EventEmitter {
14121408

14131409
async getMasterClient() {
14141410
const master = await this.getMasterNode();
1415-
const options: RedisClientOptions = { ...this.options.nodeClientOptions };
1416-
if (options.socket === undefined) {
1417-
options.socket = {};
1418-
}
1419-
options.socket.host = master.host;
1420-
options.socket.port = master.port;
1421-
1422-
return RedisClient.create(options);;
1411+
return RedisClient.create({
1412+
...this.options.nodeClientOptions,
1413+
socket: {
1414+
...this.options.nodeClientOptions?.socket,
1415+
host: master.host,
1416+
port: master.port
1417+
}
1418+
});
14231419
}
14241420

14251421
async getReplicaNodes() {
14261422
let connected = false;
14271423

14281424
for (const node of this.#sentinelRootNodes) {
1429-
const options: RedisClientOptions = { ...this.options.sentinelClientOptions };
1430-
if (options.socket === undefined) {
1431-
options.socket = {};
1432-
}
1433-
options.socket.host = node.host;
1434-
options.socket.port = node.port;
1435-
options.socket.reconnectStrategy = false;
1436-
options.modules = RedisSentinelModule;
1437-
1438-
const client = RedisClient.create(options).on('error', err => this.emit(`getReplicaNodes: ${err}`));
1425+
const client = RedisClient.create({
1426+
...this.options.sentinelClientOptions,
1427+
socket: {
1428+
...this.options.sentinelClientOptions?.socket,
1429+
host: node.host,
1430+
port: node.port,
1431+
reconnectStrategy: false
1432+
},
1433+
modules: RedisSentinelModule
1434+
}).on('error', err => this.emit(`getReplicaNodes: ${err}`));
14391435

14401436
try {
14411437
await client.connect();
@@ -1480,13 +1476,13 @@ export class RedisSentinelFactory extends EventEmitter {
14801476
this.#replicaIdx = 0;
14811477
}
14821478

1483-
const options: RedisClientOptions = { ...this.options.nodeClientOptions };
1484-
if (options.socket === undefined) {
1485-
options.socket = {};
1486-
}
1487-
options.socket.host = replicas[this.#replicaIdx].host;
1488-
options.socket.port = replicas[this.#replicaIdx].port;
1489-
1490-
return RedisClient.create(options);
1479+
return RedisClient.create({
1480+
...this.options.nodeClientOptions,
1481+
socket: {
1482+
...this.options.nodeClientOptions?.socket,
1483+
host: replicas[this.#replicaIdx].host,
1484+
port: replicas[this.#replicaIdx].port
1485+
}
1486+
});
14911487
}
14921488
}

packages/client/lib/sentinel/pub-sub-proxy.ts

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -45,18 +45,14 @@ export class PubSubProxy extends EventEmitter {
4545
throw new Error("pubSubProxy: didn't define node to do pubsub against");
4646
}
4747

48-
const options = { ...this.#clientOptions };
49-
50-
if (this.#clientOptions.socket) {
51-
options.socket = { ...this.#clientOptions.socket };
52-
} else {
53-
options.socket = {};
54-
}
55-
56-
options.socket.host = this.#node.host;
57-
options.socket.port = this.#node.port;
58-
59-
return new RedisClient(options);
48+
return new RedisClient({
49+
...this.#clientOptions,
50+
socket: {
51+
...this.#clientOptions.socket,
52+
host: this.#node.host,
53+
port: this.#node.port
54+
}
55+
});
6056
}
6157

6258
async #initiatePubSubClient(withSubscriptions = false) {

0 commit comments

Comments
 (0)