Skip to content

Commit e81bf64

Browse files
committed
ref #1741 - fix socket options type
1 parent ac37827 commit e81bf64

File tree

2 files changed

+9
-19
lines changed

2 files changed

+9
-19
lines changed

packages/client/lib/client/index.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import COMMANDS from './commands';
22
import { RedisCommand, RedisCommandArguments, RedisCommandRawReply, RedisCommandReply, RedisModules, RedisPlugins, RedisScript, RedisScripts } from '../commands';
3-
import RedisSocket, { RedisSocketOptions, RedisNetSocketOptions, RedisTlsSocketOptions } from './socket';
3+
import RedisSocket, { RedisSocketOptions, RedisTlsSocketOptions } from './socket';
44
import RedisCommandsQueue, { PubSubListener, PubSubSubscribeCommands, PubSubUnsubscribeCommands, QueueCommandOptions } from './commands-queue';
55
import RedisClientMultiCommand, { RedisClientMultiCommandType } from './multi-command';
66
import { RedisMultiQueuedCommand } from '../multi-command';
@@ -13,6 +13,7 @@ import { extendWithCommands, extendWithModulesAndScripts, LegacyCommandArguments
1313
import { Pool, Options as PoolOptions, createPool } from 'generic-pool';
1414
import { ClientClosedError, DisconnectsClientError } from '../errors';
1515
import { URL } from 'url';
16+
import { TcpSocketConnectOpts } from 'net';
1617

1718
export interface RedisClientOptions<M extends RedisModules, S extends RedisScripts> extends RedisPlugins<M, S> {
1819
url?: string;
@@ -97,7 +98,7 @@ export default class RedisClient<M extends RedisModules, S extends RedisScripts>
9798
}
9899

99100
if (port) {
100-
(parsed.socket as RedisNetSocketOptions).port = Number(port);
101+
(parsed.socket as TcpSocketConnectOpts).port = Number(port);
101102
}
102103

103104
if (username) {

packages/client/lib/client/socket.ts

+6-17
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,13 @@ export interface RedisSocketCommonOptions {
1313
reconnectStrategy?(retries: number): number | Error;
1414
}
1515

16-
export interface RedisNetSocketOptions extends RedisSocketCommonOptions {
17-
port?: number;
18-
host?: string;
19-
}
20-
21-
export interface RedisUnixSocketOptions extends RedisSocketCommonOptions {
22-
path: string;
23-
}
16+
export type RedisNetSocketOptions = Partial<net.SocketConnectOpts>;
2417

25-
export interface RedisTlsSocketOptions extends RedisNetSocketOptions, tls.SecureContextOptions, tls.CommonConnectionOptions {
18+
export interface RedisTlsSocketOptions extends RedisSocketCommonOptions, tls.ConnectionOptions {
2619
tls: true;
2720
}
2821

29-
export type RedisSocketOptions = RedisNetSocketOptions | RedisUnixSocketOptions | RedisTlsSocketOptions;
22+
export type RedisSocketOptions = RedisSocketCommonOptions & (RedisNetSocketOptions | RedisTlsSocketOptions);
3023

3124
interface CreateSocketReturn<T> {
3225
connectEvent: string;
@@ -38,9 +31,9 @@ export type RedisSocketInitiator = () => Promise<void>;
3831
export default class RedisSocket extends EventEmitter {
3932
static #initiateOptions(options?: RedisSocketOptions): RedisSocketOptions {
4033
options ??= {};
41-
if (!RedisSocket.#isUnixSocket(options)) {
42-
(options as RedisNetSocketOptions).port ??= 6379;
43-
(options as RedisNetSocketOptions).host ??= '127.0.0.1';
34+
if (!(options as net.IpcSocketConnectOpts).path) {
35+
(options as net.TcpSocketConnectOpts).port ??= 6379;
36+
(options as net.TcpSocketConnectOpts).host ??= '127.0.0.1';
4437
}
4538

4639
options.connectTimeout ??= 5000;
@@ -54,10 +47,6 @@ export default class RedisSocket extends EventEmitter {
5447
return Math.min(retries * 50, 500);
5548
}
5649

57-
static #isUnixSocket(options: RedisSocketOptions): options is RedisUnixSocketOptions {
58-
return Object.prototype.hasOwnProperty.call(options, 'path');
59-
}
60-
6150
static #isTlsSocket(options: RedisSocketOptions): options is RedisTlsSocketOptions {
6251
return (options as RedisTlsSocketOptions).tls === true;
6352
}

0 commit comments

Comments
 (0)