Skip to content

Commit 77ecdf2

Browse files
committed
fix #2679 - fix socket types, and clean some code
1 parent ec33759 commit 77ecdf2

File tree

4 files changed

+205
-163
lines changed

4 files changed

+205
-163
lines changed

docs/client-configuration.md

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,32 @@
11
# `createClient` configuration
22

3-
| Property | Default | Description |
4-
|--------------------------|------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
5-
| url | | `redis[s]://[[username][:password]@][host][:port][/db-number]` (see [`redis`](https://www.iana.org/assignments/uri-schemes/prov/redis) and [`rediss`](https://www.iana.org/assignments/uri-schemes/prov/rediss) IANA registration for more details) |
6-
| socket | | Socket connection properties. Unlisted [`net.connect`](https://nodejs.org/api/net.html#socketconnectoptions-connectlistener) properties (and [`tls.connect`](https://nodejs.org/api/tls.html#tlsconnectoptions-callback)) are also supported |
7-
| socket.port | `6379` | Redis server port |
8-
| socket.host | `'localhost'` | Redis server hostname |
9-
| socket.family | `0` | IP Stack version (one of `4 \| 6 \| 0`) |
10-
| socket.path | | Path to the UNIX Socket |
11-
| socket.connectTimeout | `5000` | Connection Timeout (in milliseconds) |
12-
| socket.noDelay | `true` | Toggle [`Nagle's algorithm`](https://nodejs.org/api/net.html#net_socket_setnodelay_nodelay) |
13-
| socket.keepAlive | `5000` | Toggle [`keep-alive`](https://nodejs.org/api/net.html#net_socket_setkeepalive_enable_initialdelay) functionality |
14-
| socket.tls | | See explanation and examples [below](#TLS) |
15-
| socket.reconnectStrategy | `retries => Math.min(retries * 50, 500)` | A function containing the [Reconnect Strategy](#reconnect-strategy) logic |
16-
| username | | ACL username ([see ACL guide](https://redis.io/topics/acl)) |
17-
| password | | ACL password or the old "--requirepass" password |
18-
| name | | Client name ([see `CLIENT SETNAME`](https://redis.io/commands/client-setname)) |
19-
| database | | Redis database number (see [`SELECT`](https://redis.io/commands/select) command) |
20-
| modules | | Included [Redis Modules](../README.md#packages) |
21-
| scripts | | Script definitions (see [Lua Scripts](../README.md#lua-scripts)) |
22-
| functions | | Function definitions (see [Functions](../README.md#functions)) |
23-
| commandsQueueMaxLength | | Maximum length of the client's internal command queue |
24-
| disableOfflineQueue | `false` | Disables offline queuing, see [FAQ](./FAQ.md#what-happens-when-the-network-goes-down) |
25-
| readonly | `false` | Connect in [`READONLY`](https://redis.io/commands/readonly) mode |
26-
| legacyMode | `false` | Maintain some backwards compatibility (see the [Migration Guide](./v3-to-v4.md)) |
27-
| isolationPoolOptions | | See the [Isolated Execution Guide](./isolated-execution.md) |
28-
| pingInterval | | Send `PING` command at interval (in ms). Useful with ["Azure Cache for Redis"](https://learn.microsoft.com/en-us/azure/azure-cache-for-redis/cache-best-practices-connection#idle-timeout) |
3+
| Property | Default | Description |
4+
|------------------------------|------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
5+
| url | | `redis[s]://[[username][:password]@][host][:port][/db-number]` (see [`redis`](https://www.iana.org/assignments/uri-schemes/prov/redis) and [`rediss`](https://www.iana.org/assignments/uri-schemes/prov/rediss) IANA registration for more details) |
6+
| socket | | Socket connection properties. Unlisted [`net.connect`](https://nodejs.org/api/net.html#socketconnectoptions-connectlistener) properties (and [`tls.connect`](https://nodejs.org/api/tls.html#tlsconnectoptions-callback)) are also supported |
7+
| socket.port | `6379` | Redis server port |
8+
| socket.host | `'localhost'` | Redis server hostname |
9+
| socket.family | `0` | IP Stack version (one of `4 \| 6 \| 0`) |
10+
| socket.path | | Path to the UNIX Socket |
11+
| socket.connectTimeout | `5000` | Connection timeout (in milliseconds) |
12+
| socket.noDelay | `true` | Toggle [`Nagle's algorithm`](https://nodejs.org/api/net.html#net_socket_setnodelay_nodelay) |
13+
| socket.keepAlive | `true` | Toggle [`keep-alive`](https://nodejs.org/api/net.html#socketsetkeepaliveenable-initialdelay) functionality |
14+
| socket.keepAliveInitialDelay | `5000` | If set to a positive number, it sets the initial delay before the first keepalive probe is sent on an idle socket |
15+
| socket.tls | | See explanation and examples [below](#TLS) |
16+
| socket.reconnectStrategy | `retries => Math.min(retries * 50, 500)` | A function containing the [Reconnect Strategy](#reconnect-strategy) logic |
17+
| username | | ACL username ([see ACL guide](https://redis.io/topics/acl)) |
18+
| password | | ACL password or the old "--requirepass" password |
19+
| name | | Client name ([see `CLIENT SETNAME`](https://redis.io/commands/client-setname)) |
20+
| database | | Redis database number (see [`SELECT`](https://redis.io/commands/select) command) |
21+
| modules | | Included [Redis Modules](../README.md#packages) |
22+
| scripts | | Script definitions (see [Lua Scripts](../README.md#lua-scripts)) |
23+
| functions | | Function definitions (see [Functions](../README.md#functions)) |
24+
| commandsQueueMaxLength | | Maximum length of the client's internal command queue |
25+
| disableOfflineQueue | `false` | Disables offline queuing, see [FAQ](./FAQ.md#what-happens-when-the-network-goes-down) |
26+
| readonly | `false` | Connect in [`READONLY`](https://redis.io/commands/readonly) mode |
27+
| legacyMode | `false` | Maintain some backwards compatibility (see the [Migration Guide](./v3-to-v4.md)) |
28+
| isolationPoolOptions | | See the [Isolated Execution Guide](./isolated-execution.md) |
29+
| pingInterval | | Send `PING` command at interval (in ms). Useful with ["Azure Cache for Redis"](https://learn.microsoft.com/en-us/azure/azure-cache-for-redis/cache-best-practices-connection#idle-timeout) |
2930

3031
## Reconnect Strategy
3132

docs/v4-to-v5.md

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,27 @@
11
# v4 to v5 migration guide
22

3+
## Client Configuration
4+
5+
### Keep Alive
6+
7+
To better align with Node.js build-in [`net`](https://nodejs.org/api/net.html) and [`tls`](https://nodejs.org/api/tls.html) modules, the `keepAlive` option has been split into 2 options: `keepAlive` (`boolean`) and `keepAliveInitialDelay` (`number`). The defaults remain `true` and `5000`.
8+
9+
### Legacy Mode
10+
11+
In the previous version, you could access "legacy" mode by creating a client and passing in `{ legacyMode: true }`. Now, you can create one off of an existing client by calling the `.legacy()` function. This allows easier access to both APIs and enables better TypeScript support.
12+
13+
```javascript
14+
// use `client` for the current API
15+
const client = createClient();
16+
await client.set('key', 'value');
17+
18+
// use `legacyClient` for the "legacy" API
19+
const legacyClient = client.legacy();
20+
legacyClient.set('key', 'value', (err, reply) => {
21+
// ...
22+
});
23+
```
24+
325
## Command Options
426

527
In v4, command options are passed as a first optional argument:
@@ -59,22 +81,6 @@ for await (const keys of client.scanIterator()) {
5981

6082
for more information, see the [Scan Iterators guide](./scan-iterators.md).
6183

62-
## Legacy Mode
63-
64-
In the previous version, you could access "legacy" mode by creating a client and passing in `{ legacyMode: true }`. Now, you can create one off of an existing client by calling the `.legacy()` function. This allows easier access to both APIs and enables better TypeScript support.
65-
66-
```javascript
67-
// use `client` for the current API
68-
const client = createClient();
69-
await client.set('key', 'value');
70-
71-
// use `legacyClient` for the "legacy" API
72-
const legacyClient = client.legacy();
73-
legacyClient.set('key', 'value', (err, reply) => {
74-
// ...
75-
});
76-
```
77-
7884
## Isolation Pool
7985

8086
[TODO](./blocking-commands.md).

packages/client/lib/client/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import COMMANDS from '../commands';
2-
import RedisSocket, { RedisSocketOptions, RedisTlsSocketOptions } from './socket';
2+
import RedisSocket, { RedisSocketOptions } from './socket';
33
import RedisCommandsQueue, { CommandOptions } from './commands-queue';
44
import { EventEmitter } from 'node:events';
55
import { attachConfig, functionArgumentsPrefix, getTransformReply, scriptArgumentsPrefix } from '../commander';
@@ -244,7 +244,7 @@ export default class RedisClient<
244244
};
245245

246246
if (protocol === 'rediss:') {
247-
(parsed.socket as RedisTlsSocketOptions).tls = true;
247+
parsed!.socket!.tls = true;
248248
} else if (protocol !== 'redis:') {
249249
throw new TypeError('Invalid protocol');
250250
}

0 commit comments

Comments
 (0)