diff --git a/API.md b/API.md index 53c5d4ae..217e9352 100644 --- a/API.md +++ b/API.md @@ -12,75 +12,80 @@ ## Redis ⇐ [EventEmitter](http://nodejs.org/api/events.html#events_class_events_eventemitter) + **Kind**: global class -**Extends**: [EventEmitter](http://nodejs.org/api/events.html#events_class_events_eventemitter), [Commander](#Commander) - -* [Redis](#Redis) ⇐ [EventEmitter](http://nodejs.org/api/events.html#events_class_events_eventemitter) - * [new Redis([port], [host], [options])](#new_Redis_new) - * _instance_ - * [.connect([callback])](#Redis+connect) ⇒ Promise.<void> - * [.disconnect()](#Redis+disconnect) - * ~~[.end()](#Redis+end)~~ - * [.duplicate()](#Redis+duplicate) - * [.monitor([callback])](#Redis+monitor) - * [.getBuiltinCommands()](#Commander+getBuiltinCommands) ⇒ Array.<string> - * [.createBuiltinCommand(commandName)](#Commander+createBuiltinCommand) ⇒ object - * [.defineCommand(name, definition)](#Commander+defineCommand) - * _static_ - * ~~[.createClient()](#Redis.createClient)~~ +**Extends**: [EventEmitter](http://nodejs.org/api/events.html#events_class_events_eventemitter), [Commander](#Commander) + +- [Redis](#Redis) ⇐ [EventEmitter](http://nodejs.org/api/events.html#events_class_events_eventemitter) + - [new Redis([port], [host], [options])](#new_Redis_new) + - _instance_ + - [.connect([callback])](#Redis+connect) ⇒ Promise.<void> + - [.disconnect()](#Redis+disconnect) + - ~~[.end()](#Redis+end)~~ + - [.duplicate()](#Redis+duplicate) + - [.monitor([callback])](#Redis+monitor) + - [.getBuiltinCommands()](#Commander+getBuiltinCommands) ⇒ Array.<string> + - [.createBuiltinCommand(commandName)](#Commander+createBuiltinCommand) ⇒ object + - [.defineCommand(name, definition)](#Commander+defineCommand) + - _static_ + - ~~[.createClient()](#Redis.createClient)~~ ### new Redis([port], [host], [options]) + Creates a Redis instance +| Param | Type | Default | Description | +| --------------------------------------- | ----------------------------------------------------------------- | ------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| [port] | number \| string \| Object | 6379 | Port of the Redis server, or a URL string(see the examples below), or the `options` object(see the third argument). | +| [host] | string \| Object | "localhost" | Host of the Redis server, when the first argument is a URL string, this argument is an object represents the options. | +| [options] | Object | | Other options. | +| [options.port] | number | 6379 | Port of the Redis server. | +| [options.host] | string | "localhost" | Host of the Redis server. | +| [options.family] | string | 4 | Version of IP stack. Defaults to 4. | +| [options.path] | string | null | Local domain socket path. If set the `port`, `host` and `family` will be ignored. | +| [options.keepAlive] | number | 0 | TCP KeepAlive on the socket with a X ms delay before start. Set to a non-number value to disable keepAlive. | +| [options.noDelay] | boolean | true | Whether to disable the Nagle's Algorithm. By default we disable it to reduce the latency. | +| [options.connectionName] | string | null | Connection name. | +| [options.db] | number | 0 | Database index to use. | +| [options.password] | string | null | If set, client will send AUTH command with the value of this option when connected. | +| [options.dropBufferSupport] | boolean | false | Drop the buffer support for better performance. This option is recommended to be enabled when handling large array response and you don't need the buffer support. | +| [options.enableReadyCheck] | boolean | true | When a connection is established to the Redis server, the server might still be loading the database from disk. While loading, the server not respond to any commands. To work around this, when this option is `true`, ioredis will check the status of the Redis server, and when the Redis server is able to process commands, a `ready` event will be emitted. | +| [options.enableOfflineQueue] | boolean | true | By default, if there is no active connection to the Redis server, 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. | +| [options.connectTimeout] | number | 10000 | The milliseconds before a timeout occurs during the initial connection to the Redis server. | +| [options.autoResubscribe] | boolean | true | After reconnected, if the previous connection was in the subscriber mode, client will auto re-subscribe these channels. | +| [options.autoResendUnfulfilledCommands] | boolean | true | If true, client will resend unfulfilled commands(e.g. block commands) in the previous connection when reconnected. | +| [options.lazyConnect] | boolean | false | By default, When a new `Redis` instance is created, it will connect to Redis server automatically. If you want to keep the instance disconnected until a command is called, you can pass the `lazyConnect` option to the constructor: `javascript var redis = new Redis({ lazyConnect: true }); // No attempting to connect to the Redis server here. // Now let's connect to the Redis server redis.get('foo', function () { });` | +| [options.tls] | Object | | TLS connection support. See https://github.com/luin/ioredis#tls-options | +| [options.tlsSni] | boolean | | If set to true server name indication will be enabled. The name is guessed based on the hostname used when opening the stream. | +| [options.keyPrefix] | string | "''" | The prefix to prepend to all keys in a command. | +| [options.retryStrategy] | function | | See "Quick Start" section | +| [options.maxRetriesPerRequest] | number | | See "Quick Start" section | +| [options.reconnectOnError] | function | | See "Quick Start" section | +| [options.readOnly] | boolean | false | Enable READONLY mode for the connection. Only available for cluster mode. | +| [options.stringNumbers] | boolean | false | Force numbers to be always returned as JavaScript strings. This option is necessary when dealing with big numbers (exceed the [-2^53, +2^53] range). | + +**Example** -| Param | Type | Default | Description | -| --- | --- | --- | --- | -| [port] | number \| string \| Object | 6379 | Port of the Redis server, or a URL string(see the examples below), or the `options` object(see the third argument). | -| [host] | string \| Object | "localhost" | Host of the Redis server, when the first argument is a URL string, this argument is an object represents the options. | -| [options] | Object | | Other options. | -| [options.port] | number | 6379 | Port of the Redis server. | -| [options.host] | string | "localhost" | Host of the Redis server. | -| [options.family] | string | 4 | Version of IP stack. Defaults to 4. | -| [options.path] | string | null | Local domain socket path. If set the `port`, `host` and `family` will be ignored. | -| [options.keepAlive] | number | 0 | TCP KeepAlive on the socket with a X ms delay before start. Set to a non-number value to disable keepAlive. | -| [options.noDelay] | boolean | true | Whether to disable the Nagle's Algorithm. By default we disable it to reduce the latency. | -| [options.connectionName] | string | null | Connection name. | -| [options.db] | number | 0 | Database index to use. | -| [options.password] | string | null | If set, client will send AUTH command with the value of this option when connected. | -| [options.dropBufferSupport] | boolean | false | Drop the buffer support for better performance. This option is recommended to be enabled when handling large array response and you don't need the buffer support. | -| [options.enableReadyCheck] | boolean | true | When a connection is established to the Redis server, the server might still be loading the database from disk. While loading, the server not respond to any commands. To work around this, when this option is `true`, ioredis will check the status of the Redis server, and when the Redis server is able to process commands, a `ready` event will be emitted. | -| [options.enableOfflineQueue] | boolean | true | By default, if there is no active connection to the Redis server, 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. | -| [options.connectTimeout] | number | 10000 | The milliseconds before a timeout occurs during the initial connection to the Redis server. | -| [options.autoResubscribe] | boolean | true | After reconnected, if the previous connection was in the subscriber mode, client will auto re-subscribe these channels. | -| [options.autoResendUnfulfilledCommands] | boolean | true | If true, client will resend unfulfilled commands(e.g. block commands) in the previous connection when reconnected. | -| [options.lazyConnect] | boolean | false | By default, When a new `Redis` instance is created, it will connect to Redis server automatically. If you want to keep the instance disconnected until a command is called, you can pass the `lazyConnect` option to the constructor: ```javascript var redis = new Redis({ lazyConnect: true }); // No attempting to connect to the Redis server here. // Now let's connect to the Redis server redis.get('foo', function () { }); ``` | -| [options.tls] | Object | | TLS connection support. See https://github.com/luin/ioredis#tls-options | -| [options.keyPrefix] | string | "''" | The prefix to prepend to all keys in a command. | -| [options.retryStrategy] | function | | See "Quick Start" section | -| [options.maxRetriesPerRequest] | number | | See "Quick Start" section | -| [options.reconnectOnError] | function | | See "Quick Start" section | -| [options.readOnly] | boolean | false | Enable READONLY mode for the connection. Only available for cluster mode. | -| [options.stringNumbers] | boolean | false | Force numbers to be always returned as JavaScript strings. This option is necessary when dealing with big numbers (exceed the [-2^53, +2^53] range). | - -**Example** ```js -var Redis = require('ioredis'); +var Redis = require("ioredis"); var redis = new Redis(); var redisOnPort6380 = new Redis(6380); -var anotherRedis = new Redis(6380, '192.168.100.1'); -var unixSocketRedis = new Redis({ path: '/tmp/echo.sock' }); -var unixSocketRedis2 = new Redis('/tmp/echo.sock'); -var urlRedis = new Redis('redis://user:password@redis-service.com:6379/'); -var urlRedis2 = new Redis('//localhost:6379'); -var authedRedis = new Redis(6380, '192.168.100.1', { password: 'password' }); +var anotherRedis = new Redis(6380, "192.168.100.1"); +var unixSocketRedis = new Redis({ path: "/tmp/echo.sock" }); +var unixSocketRedis2 = new Redis("/tmp/echo.sock"); +var urlRedis = new Redis("redis://user:password@redis-service.com:6379/"); +var urlRedis2 = new Redis("//localhost:6379"); +var authedRedis = new Redis(6380, "192.168.100.1", { password: "password" }); ``` + ### redis.connect([callback]) ⇒ Promise.<void> + Create a connection to Redis. This method will be invoked automatically when creating a new Redis instance unless `lazyConnect: true` is passed. @@ -89,15 +94,16 @@ When calling this method manually, a Promise is returned, which will be resolved when the connection status is ready. **Kind**: instance method of [Redis](#Redis) -**Access**: public +**Access**: public -| Param | Type | -| --- | --- | -| [callback] | function | +| Param | Type | +| ---------- | --------------------- | +| [callback] | function | ### redis.disconnect() + Disconnect from Redis. This method closes the connection immediately, @@ -109,7 +115,8 @@ If you want to wait for the pending replies, use Redis#quit instead. ### ~~redis.end()~~ -***Deprecated*** + +**_Deprecated_** Disconnect from Redis. @@ -117,18 +124,22 @@ Disconnect from Redis. ### redis.duplicate() + Create a new instance with the same options as the current one. **Kind**: instance method of [Redis](#Redis) **Access**: public -**Example** +**Example** + ```js var redis = new Redis(6380); var anotherRedis = redis.duplicate(); ``` + ### redis.monitor([callback]) + Listen for all requests received by the server in real time. This command will create a new connection to Redis and send a @@ -136,32 +147,35 @@ MONITOR command via the new connection in order to avoid disturbing the current connection. **Kind**: instance method of [Redis](#Redis) -**Access**: public +**Access**: public -| Param | Type | Description | -| --- | --- | --- | +| Param | Type | Description | +| ---------- | --------------------- | ----------------------------------------------------------- | | [callback] | function | The callback function. If omit, a promise will be returned. | -**Example** +**Example** + ```js var redis = new Redis(); redis.monitor(function (err, monitor) { // Entering monitoring mode. - monitor.on('monitor', function (time, args, source, database) { + monitor.on("monitor", function (time, args, source, database) { console.log(time + ": " + util.inspect(args)); }); }); // supports promise as well as other commands redis.monitor().then(function (monitor) { - monitor.on('monitor', function (time, args, source, database) { + monitor.on("monitor", function (time, args, source, database) { console.log(time + ": " + util.inspect(args)); }); }); ``` + ### redis.getBuiltinCommands() ⇒ Array.<string> + Return supported builtin commands **Kind**: instance method of [Redis](#Redis) @@ -170,34 +184,37 @@ Return supported builtin commands ### redis.createBuiltinCommand(commandName) ⇒ object + Create a builtin command **Kind**: instance method of [Redis](#Redis) **Returns**: object - functions -**Access**: public +**Access**: public -| Param | Type | Description | -| --- | --- | --- | +| Param | Type | Description | +| ----------- | ------------------- | ------------ | | commandName | string | command name | ### redis.defineCommand(name, definition) + Define a custom command using lua script -**Kind**: instance method of [Redis](#Redis) +**Kind**: instance method of [Redis](#Redis) -| Param | Type | Default | Description | -| --- | --- | --- | --- | -| name | string | | the command name | -| definition | object | | | -| definition.lua | string | | the lua code | +| Param | Type | Default | Description | +| ------------------------- | ------------------- | ------------- | ------------------------------------------------------------------------------------------------------------------------ | +| name | string | | the command name | +| definition | object | | | +| definition.lua | string | | the lua code | | [definition.numberOfKeys] | number | | the number of keys. If omit, you have to pass the number of keys as the first argument every time you invoke the command | ### ~~Redis.createClient()~~ -***Deprecated*** + +**_Deprecated_** Create a Redis instance @@ -205,46 +222,48 @@ Create a Redis instance ## Cluster ⇐ [EventEmitter](http://nodejs.org/api/events.html#events_class_events_eventemitter) + **Kind**: global class -**Extends**: [EventEmitter](http://nodejs.org/api/events.html#events_class_events_eventemitter), [Commander](#Commander) - -* [Cluster](#Cluster) ⇐ [EventEmitter](http://nodejs.org/api/events.html#events_class_events_eventemitter) - * [new Cluster(startupNodes, options)](#new_Cluster_new) - * [.connect()](#Cluster+connect) ⇒ Promise - * [.disconnect([reconnect])](#Cluster+disconnect) - * [.quit([callback])](#Cluster+quit) ⇒ Promise - * [.nodes([role])](#Cluster+nodes) ⇒ [Array.<Redis>](#Redis) - * [.getBuiltinCommands()](#Commander+getBuiltinCommands) ⇒ Array.<string> - * [.createBuiltinCommand(commandName)](#Commander+createBuiltinCommand) ⇒ object - * [.defineCommand(name, definition)](#Commander+defineCommand) - * *[.sendCommand()](#Commander+sendCommand)* +**Extends**: [EventEmitter](http://nodejs.org/api/events.html#events_class_events_eventemitter), [Commander](#Commander) + +- [Cluster](#Cluster) ⇐ [EventEmitter](http://nodejs.org/api/events.html#events_class_events_eventemitter) + - [new Cluster(startupNodes, options)](#new_Cluster_new) + - [.connect()](#Cluster+connect) ⇒ Promise + - [.disconnect([reconnect])](#Cluster+disconnect) + - [.quit([callback])](#Cluster+quit) ⇒ Promise + - [.nodes([role])](#Cluster+nodes) ⇒ [Array.<Redis>](#Redis) + - [.getBuiltinCommands()](#Commander+getBuiltinCommands) ⇒ Array.<string> + - [.createBuiltinCommand(commandName)](#Commander+createBuiltinCommand) ⇒ object + - [.defineCommand(name, definition)](#Commander+defineCommand) + - _[.sendCommand()](#Commander+sendCommand)_ ### new Cluster(startupNodes, options) -Creates a Redis Cluster instance +Creates a Redis Cluster instance -| Param | Type | Default | Description | -| --- | --- | --- | --- | -| startupNodes | Array.<Object> | | An array of nodes in the cluster, [{ port: number, host: string }] | -| options | Object | | | -| [options.clusterRetryStrategy] | function | | See "Quick Start" section | -| [options.dnsLookup] | function(hostname, function(err, addr, family)) | [dns.lookup](https://nodejs.org/api/dns.html#dns_dns_lookup_hostname_options_callback) | Function used to resolve DNS hostnames of Redis cluster members. | -| [options.enableOfflineQueue] | boolean | true | See Redis class | -| [options.enableReadyCheck] | boolean | true | When enabled, ioredis only emits "ready" event when `CLUSTER INFO` command reporting the cluster is ready for handling commands. | -| [options.scaleReads] | string | "master" | Scale reads to the node with the specified role. Available values are "master", "slave" and "all". | -| [options.maxRedirections] | number | 16 | When a MOVED or ASK error is received, client will redirect the command to another node. This option limits the max redirections allowed to send a command. | -| [options.retryDelayOnFailover] | number | 100 | When an error is received when sending a command(e.g. "Connection is closed." when the target Redis node is down), | -| [options.retryDelayOnClusterDown] | number | 100 | When a CLUSTERDOWN error is received, client will retry if `retryDelayOnClusterDown` is valid delay time. | -| [options.retryDelayOnTryAgain] | number | 100 | When a TRYAGAIN error is received, client will retry if `retryDelayOnTryAgain` is valid delay time. | -| [options.slotsRefreshTimeout] | number | 1000 | The milliseconds before a timeout occurs while refreshing slots from the cluster. | -| [options.slotsRefreshInterval] | number | 5000 | The milliseconds between every automatic slots refresh. | -| [options.redisOptions] | Object | | Passed to the constructor of `Redis`. | +| Param | Type | Default | Description | +| --------------------------------- | ----------------------------------------------------------- | --------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------- | +| startupNodes | Array.<Object> | | An array of nodes in the cluster, [{ port: number, host: string }] | +| options | Object | | | +| [options.clusterRetryStrategy] | function | | See "Quick Start" section | +| [options.dnsLookup] | function(hostname, function(err, addr, family)) | [dns.lookup](https://nodejs.org/api/dns.html#dns_dns_lookup_hostname_options_callback) | Function used to resolve DNS hostnames of Redis cluster members. | +| [options.enableOfflineQueue] | boolean | true | See Redis class | +| [options.enableReadyCheck] | boolean | true | When enabled, ioredis only emits "ready" event when `CLUSTER INFO` command reporting the cluster is ready for handling commands. | +| [options.scaleReads] | string | "master" | Scale reads to the node with the specified role. Available values are "master", "slave" and "all". | +| [options.maxRedirections] | number | 16 | When a MOVED or ASK error is received, client will redirect the command to another node. This option limits the max redirections allowed to send a command. | +| [options.retryDelayOnFailover] | number | 100 | When an error is received when sending a command(e.g. "Connection is closed." when the target Redis node is down), | +| [options.retryDelayOnClusterDown] | number | 100 | When a CLUSTERDOWN error is received, client will retry if `retryDelayOnClusterDown` is valid delay time. | +| [options.retryDelayOnTryAgain] | number | 100 | When a TRYAGAIN error is received, client will retry if `retryDelayOnTryAgain` is valid delay time. | +| [options.slotsRefreshTimeout] | number | 1000 | The milliseconds before a timeout occurs while refreshing slots from the cluster. | +| [options.slotsRefreshInterval] | number | 5000 | The milliseconds between every automatic slots refresh. | +| [options.redisOptions] | Object | | Passed to the constructor of `Redis`. | ### cluster.connect() ⇒ Promise + Connect to a cluster **Kind**: instance method of [Cluster](#Cluster) @@ -252,44 +271,48 @@ Connect to a cluster ### cluster.disconnect([reconnect]) + Disconnect from every node in the cluster. **Kind**: instance method of [Cluster](#Cluster) -**Access**: public +**Access**: public -| Param | Type | -| --- | --- | -| [reconnect] | boolean | +| Param | Type | +| ----------- | -------------------- | +| [reconnect] | boolean | ### cluster.quit([callback]) ⇒ Promise + Quit the cluster gracefully. **Kind**: instance method of [Cluster](#Cluster) **Returns**: Promise - return 'OK' if successfully -**Access**: public +**Access**: public -| Param | Type | -| --- | --- | -| [callback] | function | +| Param | Type | +| ---------- | --------------------- | +| [callback] | function | ### cluster.nodes([role]) ⇒ [Array.<Redis>](#Redis) + Get nodes with the specified role **Kind**: instance method of [Cluster](#Cluster) **Returns**: [Array.<Redis>](#Redis) - array of nodes -**Access**: public +**Access**: public -| Param | Type | Default | Description | -| --- | --- | --- | --- | +| Param | Type | Default | Description | +| ------ | ------------------- | ---------------------------- | -------------------------------- | | [role] | string | "all" | role, "master", "slave" or "all" | ### cluster.getBuiltinCommands() ⇒ Array.<string> + Return supported builtin commands **Kind**: instance method of [Cluster](#Cluster) @@ -298,33 +321,36 @@ Return supported builtin commands ### cluster.createBuiltinCommand(commandName) ⇒ object + Create a builtin command **Kind**: instance method of [Cluster](#Cluster) **Returns**: object - functions -**Access**: public +**Access**: public -| Param | Type | Description | -| --- | --- | --- | +| Param | Type | Description | +| ----------- | ------------------- | ------------ | | commandName | string | command name | ### cluster.defineCommand(name, definition) + Define a custom command using lua script -**Kind**: instance method of [Cluster](#Cluster) +**Kind**: instance method of [Cluster](#Cluster) -| Param | Type | Default | Description | -| --- | --- | --- | --- | -| name | string | | the command name | -| definition | object | | | -| definition.lua | string | | the lua code | +| Param | Type | Default | Description | +| ------------------------- | ------------------- | ------------- | ------------------------------------------------------------------------------------------------------------------------ | +| name | string | | the command name | +| definition | object | | | +| definition.lua | string | | the lua code | | [definition.numberOfKeys] | number | | the number of keys. If omit, you have to pass the number of keys as the first argument every time you invoke the command | -### *cluster.sendCommand()* +### _cluster.sendCommand()_ + Send a command **Kind**: instance abstract method of [Cluster](#Cluster) @@ -333,30 +359,32 @@ Send a command ## Commander -**Kind**: global class -* [Commander](#Commander) - * [new Commander()](#new_Commander_new) - * [.getBuiltinCommands()](#Commander+getBuiltinCommands) ⇒ Array.<string> - * [.createBuiltinCommand(commandName)](#Commander+createBuiltinCommand) ⇒ object - * [.defineCommand(name, definition)](#Commander+defineCommand) - * *[.sendCommand()](#Commander+sendCommand)* +**Kind**: global class + +- [Commander](#Commander) + - [new Commander()](#new_Commander_new) + - [.getBuiltinCommands()](#Commander+getBuiltinCommands) ⇒ Array.<string> + - [.createBuiltinCommand(commandName)](#Commander+createBuiltinCommand) ⇒ object + - [.defineCommand(name, definition)](#Commander+defineCommand) + - _[.sendCommand()](#Commander+sendCommand)_ ### new Commander() + Commander This is the base class of Redis, Redis.Cluster and Pipeline - -| Param | Type | Default | Description | -| --- | --- | --- | --- | +| Param | Type | Default | Description | +| -------------------------------- | -------------------- | ------------------ | ------------------------------------------------------------------------------------ | | [options.showFriendlyErrorStack] | boolean | false | Whether to show a friendly error stack. Will decrease the performance significantly. | ### commander.getBuiltinCommands() ⇒ Array.<string> + Return supported builtin commands **Kind**: instance method of [Commander](#Commander) @@ -365,34 +393,37 @@ Return supported builtin commands ### commander.createBuiltinCommand(commandName) ⇒ object + Create a builtin command **Kind**: instance method of [Commander](#Commander) **Returns**: object - functions -**Access**: public +**Access**: public -| Param | Type | Description | -| --- | --- | --- | +| Param | Type | Description | +| ----------- | ------------------- | ------------ | | commandName | string | command name | ### commander.defineCommand(name, definition) + Define a custom command using lua script -**Kind**: instance method of [Commander](#Commander) +**Kind**: instance method of [Commander](#Commander) -| Param | Type | Default | Description | -| --- | --- | --- | --- | -| name | string | | the command name | -| definition | object | | | -| definition.lua | string | | the lua code | +| Param | Type | Default | Description | +| ------------------------- | ------------------- | ------------- | ------------------------------------------------------------------------------------------------------------------------ | +| name | string | | the command name | +| definition | object | | | +| definition.lua | string | | the lua code | | [definition.numberOfKeys] | number | | the number of keys. If omit, you have to pass the number of keys as the first argument every time you invoke the command | -### *commander.sendCommand()* +### _commander.sendCommand()_ + Send a command **Kind**: instance abstract method of [Commander](#Commander) -**Access**: public +**Access**: public diff --git a/README.md b/README.md index 99879bf2..db8755b0 100644 --- a/README.md +++ b/README.md @@ -732,6 +732,21 @@ Alternatively, specify the connection through a [`rediss://` URL](https://www.ia var redis = new Redis("rediss://redis.my-service.com"); ``` +To enable the Server Name Indication TLS extension (SNI) set the `tlsSni` option to `true`. +The name is guessed based on the hostname used when opening the stream. + +```javascript +var redis = new Redis({ + host: "localhost", + // optional TLS options (see above) + tls: {...}, + tlsSni: true, +}); +``` + +This will basically copy the Redis hostname to `tls.servername` before the connection initialization, +allowing each node of a Redis Cluster to possess its own SNI. +
## Sentinel diff --git a/lib/cluster/ClusterSubscriber.ts b/lib/cluster/ClusterSubscriber.ts index 1a459013..612330ab 100644 --- a/lib/cluster/ClusterSubscriber.ts +++ b/lib/cluster/ClusterSubscriber.ts @@ -74,12 +74,14 @@ export default class ClusterSubscriber { this.subscriber = new Redis({ port: options.port, host: options.host, + hostOriginal: options.hostOriginal, username: options.username, password: options.password, enableReadyCheck: true, connectionName: SUBSCRIBER_CONNECTION_NAME, lazyConnect: true, tls: options.tls, + tlsSni: options.tlsSni, }); // Ignore the errors since they're handled in the connection pool. diff --git a/lib/cluster/util.ts b/lib/cluster/util.ts index d6e82037..30b32caa 100644 --- a/lib/cluster/util.ts +++ b/lib/cluster/util.ts @@ -55,6 +55,8 @@ export function normalizeNodeOptions( } if (!options.host) { options.host = "127.0.0.1"; + } else { + options.hostOriginal = options.host; } return options; diff --git a/lib/connectors/StandaloneConnector.ts b/lib/connectors/StandaloneConnector.ts index 71a36fdd..68bd83aa 100644 --- a/lib/connectors/StandaloneConnector.ts +++ b/lib/connectors/StandaloneConnector.ts @@ -12,6 +12,8 @@ export function isIIpcConnectionOptions( export interface ITcpConnectionOptions extends TcpNetConnectOpts { tls?: SecureContextOptions; + tlsSni?: boolean; + hostOriginal?: string; } export interface IIpcConnectionOptions extends IpcNetConnectOpts { @@ -29,6 +31,7 @@ export default class StandaloneConnector extends AbstractConnector { const { options } = this; this.connecting = true; + let isTls = false; let connectionOptions: any; if (isIIpcConnectionOptions(options)) { connectionOptions = { @@ -42,12 +45,17 @@ export default class StandaloneConnector extends AbstractConnector { if (options.host != null) { connectionOptions.host = options.host; } + if (options.tlsSni && (options.hostOriginal || options.host)) { + isTls = true; + connectionOptions.servername = options.hostOriginal || options.host; + } if (options.family != null) { connectionOptions.family = options.family; } } if (options.tls) { + isTls = true; Object.assign(connectionOptions, options.tls); } @@ -66,7 +74,7 @@ export default class StandaloneConnector extends AbstractConnector { } try { - if (options.tls) { + if (isTls) { this.stream = createTLSConnection(connectionOptions); } else { this.stream = createConnection(connectionOptions); diff --git a/lib/redis/RedisOptions.ts b/lib/redis/RedisOptions.ts index 78b6e6fe..3ecad1d2 100644 --- a/lib/redis/RedisOptions.ts +++ b/lib/redis/RedisOptions.ts @@ -26,6 +26,7 @@ export interface IRedisOptions stringNumbers?: boolean; maxRetriesPerRequest?: number; maxLoadingRetryTime?: number; + tlsSni?: boolean; } export const DEFAULT_REDIS_OPTIONS: IRedisOptions = { diff --git a/test/functional/cluster/nat.ts b/test/functional/cluster/nat.ts index 504de35a..07aea60e 100644 --- a/test/functional/cluster/nat.ts +++ b/test/functional/cluster/nat.ts @@ -199,7 +199,12 @@ describe("NAT", () => { cluster.on("ready", () => { expect(reset.secondCall.args[0]).to.deep.equal([ - { host: "127.0.0.1", port: 30001, readOnly: false }, + { + host: "127.0.0.1", + hostOriginal: "127.0.0.1", + port: 30001, + readOnly: false, + }, ]); cluster.disconnect(); done(); diff --git a/test/functional/cluster/tls.ts b/test/functional/cluster/tls.ts index e3ec7178..ad525ebe 100644 --- a/test/functional/cluster/tls.ts +++ b/test/functional/cluster/tls.ts @@ -27,7 +27,7 @@ describe("cluster:tls option", () => { // @ts-ignore expect(op.ca).to.eql("123"); // @ts-ignore - expect(op.port).to.be.oneOf([30001, 30003, 30003]); + expect(op.port).to.be.oneOf([30001, 30002, 30003]); const stream = net.createConnection(op); stream.on("connect", (data) => { stream.emit("secureConnect", data); @@ -56,4 +56,54 @@ describe("cluster:tls option", () => { cluster.on("end", () => done()); }); }); + + it("supports tls sni", (done) => { + const slotTable = [ + [0, 5460, ["127.0.0.1", 30001]], + [5461, 10922, ["127.0.0.1", 30002]], + [10923, 16383, ["127.0.0.1", 30003]], + ]; + const argvHandler = function (argv) { + if (argv[0] === "cluster" && argv[1] === "slots") { + return slotTable; + } + }; + + new MockServer(30001, argvHandler); + new MockServer(30002, argvHandler); + new MockServer(30003, argvHandler); + + // @ts-ignore + const stub = sinon.stub(tls, "connect").callsFake((op) => { + // @ts-ignore + expect(op.host).to.eql("127.0.0.1"); + // @ts-ignore + expect(op.servername).to.eql("localhost"); + // @ts-ignore + expect(op.port).to.be.oneOf([30001, 30002, 30003]); + const stream = net.createConnection(op); + stream.on("connect", (data) => { + stream.emit("secureConnect", data); + }); + return stream; + }); + + const cluster = new Cluster( + [ + { host: "localhost", port: "30001" }, + { host: "localhost", port: "30002" }, + { host: "localhost", port: "30003" }, + ], + { + redisOptions: { tlsSni: true }, + } + ); + + cluster.on("ready", () => { + expect(cluster.subscriber.subscriber.options.tlsSni).to.equal(true); + cluster.disconnect(); + stub.restore(); + cluster.on("end", () => done()); + }); + }); }); diff --git a/test/functional/tls.ts b/test/functional/tls.ts index 6dc3ba14..b4820aab 100644 --- a/test/functional/tls.ts +++ b/test/functional/tls.ts @@ -30,6 +30,32 @@ describe("tls option", () => { redis.on("end", () => done()); }); }); + + it("supports tls sni", (done) => { + let redis; + + // @ts-ignore + const stub = sinon.stub(tls, "connect").callsFake((op) => { + // @ts-ignore + expect(op.host).to.eql("localhost"); + // @ts-ignore + expect(op.servername).to.eql("localhost"); + // @ts-ignore + expect(op.port).to.eql(6379); + const stream = net.createConnection(op); + stream.on("connect", (data) => { + stream.emit("secureConnect", data); + }); + return stream; + }); + + redis = new Redis({ tlsSni: true }); + redis.on("ready", () => { + redis.disconnect(); + stub.restore(); + redis.on("end", () => done()); + }); + }); }); describe("Sentinel", () => { diff --git a/test/unit/connectors/connector.ts b/test/unit/connectors/connector.ts index 45a19731..fd9c10a5 100644 --- a/test/unit/connectors/connector.ts +++ b/test/unit/connectors/connector.ts @@ -39,5 +39,22 @@ describe("StandaloneConnector", () => { expect(spy.firstCall.args[0]).to.eql({ port: 6379, ca: "on" }); connector.disconnect(); }); + + it("supports tls sni", async () => { + const spy = sinon.stub(tls, "connect"); + const connector = new StandaloneConnector({ + host: "localhost", + port: 6379, + tlsSni: true, + }); + await connector.connect(() => {}); + expect(spy.calledOnce).to.eql(true); + expect(spy.firstCall.args[0]).to.eql({ + host: "localhost", + port: 6379, + servername: "localhost", + }); + connector.disconnect(); + }); }); });