Skip to content

Commit cc83cee

Browse files
committed
fix #1671 - add support for all client configurations in cluster
1 parent 833416c commit cc83cee

File tree

7 files changed

+41
-44
lines changed

7 files changed

+41
-44
lines changed

README.md

+15-19
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ The above code connects to localhost on port 6379. To connect to a different hos
5353

5454
```typescript
5555
createClient({
56-
url: 'redis://alice:foobared@awesome.redis.server:6380',
56+
url: 'redis://alice:foobared@awesome.redis.server:6380'
5757
});
5858
```
5959

@@ -78,7 +78,7 @@ Modifiers to commands are specified using a JavaScript object:
7878
```typescript
7979
await client.set('key', 'value', {
8080
EX: 10,
81-
NX: true,
81+
NX: true
8282
});
8383
```
8484

@@ -181,12 +181,9 @@ for await (const key of client.scanIterator()) {
181181
This works with `HSCAN`, `SSCAN`, and `ZSCAN` too:
182182

183183
```typescript
184-
for await (const member of client.hScanIterator('hash')) {
185-
}
186-
for await (const { field, value } of client.sScanIterator('set')) {
187-
}
188-
for await (const { member, score } of client.zScanIterator('sorted-set')) {
189-
}
184+
for await (const member of client.hScanIterator('hash')) {}
185+
for await (const { field, value } of client.sScanIterator('set')) {}
186+
for await (const { member, score } of client.zScanIterator('sorted-set')) {}
190187
```
191188

192189
You can override the default options by providing a configuration object:
@@ -204,7 +201,8 @@ client.scanIterator({
204201
Define new functions using [Lua scripts](https://redis.io/commands/eval) which execute on the Redis server:
205202

206203
```typescript
207-
import { createClient, defineScript } from 'redis';
204+
import { createClient } from 'redis';
205+
import { defineScript } from 'redis/lua-script';
208206

209207
(async () => {
210208
const client = createClient({
@@ -218,9 +216,9 @@ import { createClient, defineScript } from 'redis';
218216
},
219217
transformReply(reply: number): number {
220218
return reply;
221-
},
222-
}),
223-
},
219+
}
220+
})
221+
}
224222
});
225223

226224
await client.connect();
@@ -241,14 +239,12 @@ import { createCluster } from 'redis';
241239
const cluster = createCluster({
242240
rootNodes: [
243241
{
244-
host: '10.0.0.1',
245-
port: 30001,
242+
url: 'redis://10.0.0.1:30001'
246243
},
247244
{
248-
host: '10.0.0.2',
249-
port: 30002,
250-
},
251-
],
245+
url: 'redis://10.0.0.2:30002'
246+
}
247+
]
252248
});
253249

254250
cluster.on('error', (err) => console.log('Redis Cluster Error', err));
@@ -274,7 +270,7 @@ Of course, if you don't do something with your Promises you're certain to get [u
274270
```typescript
275271
await Promise.all([
276272
client.set('Tm9kZSBSZWRpcw==', 'users:1'),
277-
client.sAdd('users:1:tokens', 'Tm9kZSBSZWRpcw=='),
273+
client.sAdd('users:1:tokens', 'Tm9kZSBSZWRpcw==')
278274
]);
279275
```
280276

index.ts

-2
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,3 @@ export const createClient = RedisClient.create;
66
export const commandOptions = RedisClient.commandOptions;
77

88
export const createCluster = RedisCluster.create;
9-
10-
export { defineScript } from './lib/lua-script';

lib/cluster-slots.ts

+5-7
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import calculateSlot from 'cluster-key-slot';
22
import RedisClient, { RedisClientType } from './client';
33
import { RedisSocketOptions } from './socket';
44
import { RedisClusterMasterNode, RedisClusterReplicaNode } from './commands/CLUSTER_NODES';
5-
import { RedisClusterOptions } from './cluster';
5+
import { RedisClusterClientOptions, RedisClusterOptions } from './cluster';
66
import { RedisModules } from './commands';
77
import { RedisLuaScripts } from './lua-script';
88

@@ -39,21 +39,19 @@ export default class RedisClusterSlots<M extends RedisModules, S extends RedisLu
3939
}
4040

4141
async discover(startWith: RedisClientType<M, S>): Promise<void> {
42-
if (await this.#discoverNodes(startWith.options?.socket)) return;
42+
if (await this.#discoverNodes(startWith.options)) return;
4343

4444
for (const { client } of this.#nodeByUrl.values()) {
4545
if (client === startWith) continue;
4646

47-
if (await this.#discoverNodes(client.options?.socket)) return;
47+
if (await this.#discoverNodes(client.options)) return;
4848
}
4949

5050
throw new Error('None of the cluster nodes is available');
5151
}
5252

53-
async #discoverNodes(socketOptions?: RedisSocketOptions): Promise<boolean> {
54-
const client = RedisClient.create({
55-
socket: socketOptions
56-
});
53+
async #discoverNodes(clientOptions?: RedisClusterClientOptions): Promise<boolean> {
54+
const client = RedisClient.create(clientOptions);
5755

5856
await client.connect();
5957

lib/cluster.spec.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { ClusterSlotStates } from './commands/CLUSTER_SETSLOT';
88
describe('Cluster', () => {
99
it('sendCommand', async () => {
1010
const cluster = RedisCluster.create({
11-
rootNodes: TEST_REDIS_CLUSTERES[TestRedisClusters.OPEN],
11+
...TEST_REDIS_CLUSTERES[TestRedisClusters.OPEN],
1212
useReplicas: true
1313
});
1414

@@ -42,7 +42,7 @@ describe('Cluster', () => {
4242

4343
it('scripts', async () => {
4444
const cluster = RedisCluster.create({
45-
rootNodes: TEST_REDIS_CLUSTERES[TestRedisClusters.OPEN],
45+
...TEST_REDIS_CLUSTERES[TestRedisClusters.OPEN],
4646
scripts: {
4747
add: defineScript({
4848
NUMBER_OF_KEYS: 0,

lib/cluster.ts

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
import { RedisCommand, RedisCommandReply, RedisModules, TransformArgumentsReply } from './commands';
2-
import RedisClient, { ClientCommandOptions, RedisClientType, WithPlugins } from './client';
3-
import { RedisSocketOptions } from './socket';
2+
import RedisClient, { ClientCommandOptions, RedisClientOptions, RedisClientType, WithPlugins } from './client';
43
import RedisClusterSlots, { ClusterNode } from './cluster-slots';
54
import { RedisLuaScript, RedisLuaScripts } from './lua-script';
65
import { extendWithModulesAndScripts, extendWithDefaultCommands, transformCommandArguments, transformCommandReply } from './commander';
76
import RedisMultiCommand, { MultiQueuedCommand, RedisMultiCommandType } from './multi-command';
87
import { EventEmitter } from 'events';
98

10-
export interface RedisClusterOptions<M = RedisModules, S = RedisLuaScripts> {
11-
rootNodes: Array<RedisSocketOptions>;
9+
export type RedisClusterClientOptions = Omit<RedisClientOptions<{}, {}>, 'modules' | 'scripts'>;
10+
11+
export interface RedisClusterOptions<M = {}, S = {}> {
12+
rootNodes: Array<RedisClusterClientOptions>;
13+
defaults?: RedisClusterClientOptions;
1214
modules?: M;
1315
scripts?: S;
1416
useReplicas?: boolean;

lib/commands/BZPOPMAX.spec.ts

-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { strict as assert } from 'assert';
22
import { TestRedisServers, itWithClient } from '../test-utils';
33
import { transformArguments, transformReply } from './BZPOPMAX';
44
import { commandOptions } from '../../index';
5-
import { describe } from 'mocha';
65

76
describe('BZPOPMAX', () => {
87
describe('transformArguments', () => {

lib/test-utils.ts

+13-9
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { once } from 'events';
55
import { RedisSocketOptions } from './socket';
66
import which from 'which';
77
import { SinonSpy } from 'sinon';
8-
import RedisCluster, { RedisClusterType } from './cluster';
8+
import RedisCluster, { RedisClusterOptions, RedisClusterType } from './cluster';
99
import { promises as fs } from 'fs';
1010
import { Context as MochaContext } from 'mocha';
1111
import { promiseTimeout } from './utils';
@@ -60,7 +60,7 @@ export enum TestRedisClusters {
6060
OPEN
6161
}
6262

63-
export const TEST_REDIS_CLUSTERES: Record<TestRedisClusters, Array<RedisSocketOptions>> = <any>{};
63+
export const TEST_REDIS_CLUSTERES: Record<TestRedisClusters, RedisClusterOptions<RedisModules, RedisLuaScripts>> = <any>{};
6464

6565
let port = 6379;
6666

@@ -248,9 +248,13 @@ async function spawnPasswordServer(): Promise<void> {
248248
}
249249

250250
async function spawnOpenCluster(): Promise<void> {
251-
TEST_REDIS_CLUSTERES[TestRedisClusters.OPEN] = (await spawnGlobalRedisCluster(TestRedisClusters.OPEN, 3)).map(port => ({
252-
port
253-
}));
251+
TEST_REDIS_CLUSTERES[TestRedisClusters.OPEN] = {
252+
rootNodes: (await spawnGlobalRedisCluster(TestRedisClusters.OPEN, 3)).map(port => ({
253+
socket: {
254+
port
255+
}
256+
}))
257+
};
254258
}
255259

256260
before(function () {
@@ -314,9 +318,7 @@ export function itWithCluster(
314318
it(title, async function () {
315319
if (handleMinimumRedisVersion(this, options?.minimumRedisVersion)) return;
316320

317-
const cluster = RedisCluster.create({
318-
rootNodes: TEST_REDIS_CLUSTERES[type]
319-
});
321+
const cluster = RedisCluster.create(TEST_REDIS_CLUSTERES[type]);
320322

321323
await cluster.connect();
322324

@@ -337,7 +339,9 @@ export function itWithDedicatedCluster(title: string, fn: (cluster: RedisCluster
337339
const spawnResults = await spawnRedisCluster(null, 3),
338340
cluster = RedisCluster.create({
339341
rootNodes: [{
340-
port: spawnResults[0].port
342+
socket: {
343+
port: spawnResults[0].port
344+
}
341345
}]
342346
});
343347

0 commit comments

Comments
 (0)