1
0
mirror of https://github.com/redis/node-redis.git synced 2025-08-06 02:15:48 +03:00

ref #1741 - fix socket options type

This commit is contained in:
leibale
2021-11-27 22:36:18 -05:00
parent ac378275ed
commit e81bf64914
2 changed files with 9 additions and 19 deletions

View File

@@ -1,6 +1,6 @@
import COMMANDS from './commands'; import COMMANDS from './commands';
import { RedisCommand, RedisCommandArguments, RedisCommandRawReply, RedisCommandReply, RedisModules, RedisPlugins, RedisScript, RedisScripts } from '../commands'; import { RedisCommand, RedisCommandArguments, RedisCommandRawReply, RedisCommandReply, RedisModules, RedisPlugins, RedisScript, RedisScripts } from '../commands';
import RedisSocket, { RedisSocketOptions, RedisNetSocketOptions, RedisTlsSocketOptions } from './socket'; import RedisSocket, { RedisSocketOptions, RedisTlsSocketOptions } from './socket';
import RedisCommandsQueue, { PubSubListener, PubSubSubscribeCommands, PubSubUnsubscribeCommands, QueueCommandOptions } from './commands-queue'; import RedisCommandsQueue, { PubSubListener, PubSubSubscribeCommands, PubSubUnsubscribeCommands, QueueCommandOptions } from './commands-queue';
import RedisClientMultiCommand, { RedisClientMultiCommandType } from './multi-command'; import RedisClientMultiCommand, { RedisClientMultiCommandType } from './multi-command';
import { RedisMultiQueuedCommand } from '../multi-command'; import { RedisMultiQueuedCommand } from '../multi-command';
@@ -13,6 +13,7 @@ import { extendWithCommands, extendWithModulesAndScripts, LegacyCommandArguments
import { Pool, Options as PoolOptions, createPool } from 'generic-pool'; import { Pool, Options as PoolOptions, createPool } from 'generic-pool';
import { ClientClosedError, DisconnectsClientError } from '../errors'; import { ClientClosedError, DisconnectsClientError } from '../errors';
import { URL } from 'url'; import { URL } from 'url';
import { TcpSocketConnectOpts } from 'net';
export interface RedisClientOptions<M extends RedisModules, S extends RedisScripts> extends RedisPlugins<M, S> { export interface RedisClientOptions<M extends RedisModules, S extends RedisScripts> extends RedisPlugins<M, S> {
url?: string; url?: string;
@@ -97,7 +98,7 @@ export default class RedisClient<M extends RedisModules, S extends RedisScripts>
} }
if (port) { if (port) {
(parsed.socket as RedisNetSocketOptions).port = Number(port); (parsed.socket as TcpSocketConnectOpts).port = Number(port);
} }
if (username) { if (username) {

View File

@@ -13,20 +13,13 @@ export interface RedisSocketCommonOptions {
reconnectStrategy?(retries: number): number | Error; reconnectStrategy?(retries: number): number | Error;
} }
export interface RedisNetSocketOptions extends RedisSocketCommonOptions { export type RedisNetSocketOptions = Partial<net.SocketConnectOpts>;
port?: number;
host?: string;
}
export interface RedisUnixSocketOptions extends RedisSocketCommonOptions { export interface RedisTlsSocketOptions extends RedisSocketCommonOptions, tls.ConnectionOptions {
path: string;
}
export interface RedisTlsSocketOptions extends RedisNetSocketOptions, tls.SecureContextOptions, tls.CommonConnectionOptions {
tls: true; tls: true;
} }
export type RedisSocketOptions = RedisNetSocketOptions | RedisUnixSocketOptions | RedisTlsSocketOptions; export type RedisSocketOptions = RedisSocketCommonOptions & (RedisNetSocketOptions | RedisTlsSocketOptions);
interface CreateSocketReturn<T> { interface CreateSocketReturn<T> {
connectEvent: string; connectEvent: string;
@@ -38,9 +31,9 @@ export type RedisSocketInitiator = () => Promise<void>;
export default class RedisSocket extends EventEmitter { export default class RedisSocket extends EventEmitter {
static #initiateOptions(options?: RedisSocketOptions): RedisSocketOptions { static #initiateOptions(options?: RedisSocketOptions): RedisSocketOptions {
options ??= {}; options ??= {};
if (!RedisSocket.#isUnixSocket(options)) { if (!(options as net.IpcSocketConnectOpts).path) {
(options as RedisNetSocketOptions).port ??= 6379; (options as net.TcpSocketConnectOpts).port ??= 6379;
(options as RedisNetSocketOptions).host ??= '127.0.0.1'; (options as net.TcpSocketConnectOpts).host ??= '127.0.0.1';
} }
options.connectTimeout ??= 5000; options.connectTimeout ??= 5000;
@@ -54,10 +47,6 @@ export default class RedisSocket extends EventEmitter {
return Math.min(retries * 50, 500); return Math.min(retries * 50, 500);
} }
static #isUnixSocket(options: RedisSocketOptions): options is RedisUnixSocketOptions {
return Object.prototype.hasOwnProperty.call(options, 'path');
}
static #isTlsSocket(options: RedisSocketOptions): options is RedisTlsSocketOptions { static #isTlsSocket(options: RedisSocketOptions): options is RedisTlsSocketOptions {
return (options as RedisTlsSocketOptions).tls === true; return (options as RedisTlsSocketOptions).tls === true;
} }