From df04dd8d87a44d3b64b385c86581915248554508 Mon Sep 17 00:00:00 2001 From: Zihua Li Date: Sat, 9 Apr 2022 11:04:12 +0800 Subject: [PATCH] fix: Expose ChainableCommander and other types (#1560) --- bin/template.ts | 4 +-- lib/cluster/ClusterOptions.ts | 4 +-- lib/index.ts | 12 ++++++- lib/redis/RedisOptions.ts | 59 +++++++++++++++++++++++++++++++++++ lib/utils/RedisCommander.ts | 4 +-- test/typing/options.test-d.ts | 14 +++++++-- 6 files changed, 88 insertions(+), 9 deletions(-) diff --git a/bin/template.ts b/bin/template.ts index 89e3e0e8..47d2a638 100644 --- a/bin/template.ts +++ b/bin/template.ts @@ -1,7 +1,7 @@ import { Callback } from "../types"; -type RedisKey = string | Buffer; -type RedisValue = string | Buffer | number; +export type RedisKey = string | Buffer; +export type RedisValue = string | Buffer | number; // Inspired by https://github.com/mmkal/handy-redis/blob/main/src/generated/interface.ts. // Should be fixed with https://github.com/Microsoft/TypeScript/issues/1213 diff --git a/lib/cluster/ClusterOptions.ts b/lib/cluster/ClusterOptions.ts index cceebb27..a829e6e9 100644 --- a/lib/cluster/ClusterOptions.ts +++ b/lib/cluster/ClusterOptions.ts @@ -6,7 +6,7 @@ import { NodeRole } from "./util"; export type DNSResolveSrvFunction = ( hostname: string, callback: ( - err: NodeJS.ErrnoException | undefined, + err: NodeJS.ErrnoException | null | undefined, records?: SrvRecord[] ) => void ) => void; @@ -14,7 +14,7 @@ export type DNSResolveSrvFunction = ( export type DNSLookupFunction = ( hostname: string, callback: ( - err: NodeJS.ErrnoException | undefined, + err: NodeJS.ErrnoException | null | undefined, address: string, family?: number ) => void diff --git a/lib/index.ts b/lib/index.ts index 766c9b1f..6fc5eeed 100644 --- a/lib/index.ts +++ b/lib/index.ts @@ -54,8 +54,18 @@ export { export { StandaloneConnectionOptions } from "./connectors/StandaloneConnector"; export { RedisOptions, CommonRedisOptions } from "./redis/RedisOptions"; export { ClusterNode } from "./cluster"; -export { ClusterOptions } from "./cluster/ClusterOptions"; +export { + ClusterOptions, + DNSLookupFunction, + DNSResolveSrvFunction, + NatMap, +} from "./cluster/ClusterOptions"; export { NodeRole } from "./cluster/util"; +export type { + RedisKey, + RedisValue, + ChainableCommander, +} from "./utils/RedisCommander"; // No TS typings export const ReplyError = require("redis-errors").ReplyError; diff --git a/lib/redis/RedisOptions.ts b/lib/redis/RedisOptions.ts index 8585b45e..e580d191 100644 --- a/lib/redis/RedisOptions.ts +++ b/lib/redis/RedisOptions.ts @@ -8,6 +8,11 @@ export type ReconnectOnError = (err: Error) => boolean | 1 | 2; export interface CommonRedisOptions extends CommanderOptions { Connector?: ConnectorConstructor; retryStrategy?: (times: number) => number | void | null; + + /** + * If a command does not return a reply within a set number of milliseconds, + * a "Command timed out" error will be thrown. + */ commandTimeout?: number; /** * Enable/disable keep-alive functionality. @@ -15,28 +20,46 @@ export interface CommonRedisOptions extends CommanderOptions { * @default 0 */ keepAlive?: number; + /** * Enable/disable the use of Nagle's algorithm. * @link https://nodejs.org/api/net.html#socketsetnodelaynodelay * @default true */ noDelay?: boolean; + /** * Set the name of the connection to make it easier to identity the connection * in client list. * @link https://redis.io/commands/client-setname */ connectionName?: string; + + /** + * If set, client will send AUTH command with the value of this option as the first argument when connected. + * This is supported since Redis 6. + */ username?: string; + + /** + * If set, client will send AUTH command with the value of this option when connected. + */ password?: string; + /** + * Database index to use. + * * @default 0 */ db?: number; + /** + * When the client reconnects, channels subscribed in the previous connection will be + * resubscribed automatically if `autoResubscribe` is `true`. * @default true */ autoResubscribe?: boolean; + /** * Whether or not to resend unfulfilled commands on reconnect. * Unfulfilled commands are most likely to be blocking commands such as `brpop` or `blpop`. @@ -65,6 +88,7 @@ export interface CommonRedisOptions extends CommanderOptions { * @default null */ reconnectOnError?: ReconnectOnError | null; + /** * @default false */ @@ -75,18 +99,33 @@ export interface CommonRedisOptions extends CommanderOptions { * @default false */ stringNumbers?: boolean; + /** + * How long the client will wait before killing a socket due to inactivity during initial connection. * @default 10000 */ connectTimeout?: number; + /** + * This option is used internally when you call `redis.monitor()` to tell Redis + * to enter the monitor mode when the connection is established. + * * @default false */ monitor?: boolean; + /** + * The commands that don't get a reply due to the connection to the server is lost are + * put into a queue and will be resent on reconnect (if allowed by the `retryStrategy` option). + * This option is used to configure how many reconnection attempts should be allowed before + * the queue is flushed with a `MaxRetriesPerRequestError` error. + * Set this options to `null` instead of a number to let commands wait forever + * until the connection is alive again. + * * @default 20 */ maxRetriesPerRequest?: number | null; + /** * @default 10000 */ @@ -101,18 +140,38 @@ export interface CommonRedisOptions extends CommanderOptions { autoPipeliningIgnoredCommands?: string[]; offlineQueue?: boolean; commandQueue?: boolean; + /** + * + * By default, if the connection to Redis server has not been established, commands are added to a queue + * and are executed once the connection is "ready" (when `enableReadyCheck` is true, "ready" means + * the Redis server has loaded the database from disk, otherwise means the connection to the Redis + * server has been established). If this option is false, when execute the command when the connection + * isn't ready, an error will be returned. + * * @default true */ enableOfflineQueue?: boolean; + /** + * The client will sent an INFO command to check whether the server is still loading data from the disk ( + * which happens when the server is just launched) when the connection is established, and only wait until + * the loading process is finished before emitting the `ready` event. + * * @default true */ enableReadyCheck?: boolean; + /** + * When a Redis instance is initialized, a connection to the server is immediately established. Set this to + * true will delay the connection to the server until the first command is sent or `redis.connect()` is called + * explicitly. + * * @default false */ + lazyConnect?: boolean; + /** * @default undefined */ diff --git a/lib/utils/RedisCommander.ts b/lib/utils/RedisCommander.ts index 0f6cbbad..8ee140c8 100644 --- a/lib/utils/RedisCommander.ts +++ b/lib/utils/RedisCommander.ts @@ -1,7 +1,7 @@ import { Callback } from "../types"; -type RedisKey = string | Buffer; -type RedisValue = string | Buffer | number; +export type RedisKey = string | Buffer; +export type RedisValue = string | Buffer | number; // Inspired by https://github.com/mmkal/handy-redis/blob/main/src/generated/interface.ts. // Should be fixed with https://github.com/Microsoft/TypeScript/issues/1213 diff --git a/test/typing/options.test-d.ts b/test/typing/options.test-d.ts index 80158dd6..cc02a605 100644 --- a/test/typing/options.test-d.ts +++ b/test/typing/options.test-d.ts @@ -1,5 +1,5 @@ -import { expectType } from "tsd"; -import Redis, { Cluster } from "../../built"; +import { expectAssignable, expectType } from "tsd"; +import Redis, { Cluster, NatMap, DNSLookupFunction } from "../../built"; expectType(new Redis()); @@ -39,3 +39,13 @@ expectType( enableAutoPipelining: true, }) ); + +expectAssignable({ + "10.0.1.230:30001": { host: "203.0.113.73", port: 30001 }, + "10.0.1.231:30001": { host: "203.0.113.73", port: 30002 }, + "10.0.1.232:30001": { host: "203.0.113.73", port: 30003 }, +}); + +expectAssignable((address, callback) => + callback(null, address) +);