From 454beec14042e174de5179af0c54bfe28cc094f5 Mon Sep 17 00:00:00 2001 From: Nick Roberts Date: Fri, 22 Feb 2019 10:31:41 -0500 Subject: [PATCH 1/4] Update ioredis cluster types --- types/ioredis/index.d.ts | 282 +++++++++++++++++++++++++++------ types/ioredis/ioredis-tests.ts | 143 ++++++++++------- 2 files changed, 321 insertions(+), 104 deletions(-) diff --git a/types/ioredis/index.d.ts b/types/ioredis/index.d.ts index cbe5b0f8e2..7158e9fb31 100644 --- a/types/ioredis/index.d.ts +++ b/types/ioredis/index.d.ts @@ -24,13 +24,13 @@ import tls = require('tls'); interface RedisStatic { - new(port?: number, host?: string, options?: IORedis.RedisOptions): IORedis.Redis; - new(host?: string, options?: IORedis.RedisOptions): IORedis.Redis; - new(options?: IORedis.RedisOptions): IORedis.Redis; + new (port?: number, host?: string, options?: IORedis.RedisOptions): IORedis.Redis; + new (host?: string, options?: IORedis.RedisOptions): IORedis.Redis; + new (options?: IORedis.RedisOptions): IORedis.Redis; (port?: number, host?: string, options?: IORedis.RedisOptions): IORedis.Redis; (host?: string, options?: IORedis.RedisOptions): IORedis.Redis; (options?: IORedis.RedisOptions): IORedis.Redis; - Cluster: IORedis.Cluster; + Cluster: IORedis.ClusterStatic; Command: IORedis.Command; } @@ -40,10 +40,13 @@ export = IORedis; declare class Commander { getBuiltinCommands(): string[]; createBuiltinCommand(commandName: string): {}; - defineCommand(name: string, definition: { - numberOfKeys?: number; - lua?: string; - }): any; + defineCommand( + name: string, + definition: { + numberOfKeys?: number; + lua?: string; + } + ): any; sendCommand(): void; } @@ -75,19 +78,57 @@ declare namespace IORedis { getBuffer(key: KeyType, callback: (err: Error, res: Buffer) => void): void; getBuffer(key: KeyType): Promise; - set(key: KeyType, value: any, expiryMode?: string | any[], time?: number | string, setMode?: number | string): Promise; + set( + key: KeyType, + value: any, + expiryMode?: string | any[], + time?: number | string, + setMode?: number | string + ): Promise; set(key: KeyType, value: any, callback: (err: Error, res: string) => void): void; set(key: KeyType, value: any, setMode: string | any[], callback: (err: Error, res: string) => void): void; - set(key: KeyType, value: any, expiryMode: string, time: number | string, callback: (err: Error, res: string) => void): void; - set(key: KeyType, value: any, expiryMode: string, time: number | string, setMode: number | string, callback: (err: Error, res: string) => void): void; + set( + key: KeyType, + value: any, + expiryMode: string, + time: number | string, + callback: (err: Error, res: string) => void + ): void; + set( + key: KeyType, + value: any, + expiryMode: string, + time: number | string, + setMode: number | string, + callback: (err: Error, res: string) => void + ): void; - setBuffer(key: KeyType, value: any, expiryMode?: string | any[], time?: number | string, setMode?: number | string): Promise; + setBuffer( + key: KeyType, + value: any, + expiryMode?: string | any[], + time?: number | string, + setMode?: number | string + ): Promise; setBuffer(key: KeyType, value: any, callback: (err: Error, res: Buffer) => void): void; setBuffer(key: KeyType, value: any, setMode: string, callback: (err: Error, res: Buffer) => void): void; - setBuffer(key: KeyType, value: any, expiryMode: string, time: number, callback: (err: Error, res: Buffer) => void): void; - setBuffer(key: KeyType, value: any, expiryMode: string, time: number | string, setMode: number | string, callback: (err: Error, res: Buffer) => void): void; + setBuffer( + key: KeyType, + value: any, + expiryMode: string, + time: number, + callback: (err: Error, res: Buffer) => void + ): void; + setBuffer( + key: KeyType, + value: any, + expiryMode: string, + time: number | string, + setMode: number | string, + callback: (err: Error, res: Buffer) => void + ): void; setnx(key: KeyType, value: any, callback: (err: Error, res: any) => void): void; setnx(key: KeyType, value: any): Promise; @@ -142,8 +183,14 @@ declare namespace IORedis { lpushx(key: KeyType, value: any, callback: (err: Error, res: number) => void): void; lpushx(key: KeyType, value: any): Promise; - linsert(key: KeyType, direction: "BEFORE" | "AFTER", pivot: string, value: any, callback: (err: Error, res: number) => void): void; - linsert(key: KeyType, direction: "BEFORE" | "AFTER", pivot: string, value: any): Promise; + linsert( + key: KeyType, + direction: 'BEFORE' | 'AFTER', + pivot: string, + value: any, + callback: (err: Error, res: number) => void + ): void; + linsert(key: KeyType, direction: 'BEFORE' | 'AFTER', pivot: string, value: any): Promise; rpop(key: KeyType, callback: (err: Error, res: string) => void): void; rpop(key: KeyType): Promise; @@ -155,7 +202,12 @@ declare namespace IORedis { blpop(...keys: KeyType[]): any; - brpoplpush(source: string, destination: string, timeout: number, callback: (err: Error, res: any) => void): void; + brpoplpush( + source: string, + destination: string, + timeout: number, + callback: (err: Error, res: any) => void + ): void; brpoplpush(source: string, destination: string, timeout: number): Promise; llen(key: KeyType, callback: (err: Error, res: number) => void): void; @@ -222,7 +274,12 @@ declare namespace IORedis { zrem(key: KeyType, ...members: any[]): any; - zremrangebyscore(key: KeyType, min: number | string, max: number | string, callback: (err: Error, res: any) => void): void; + zremrangebyscore( + key: KeyType, + min: number | string, + max: number | string, + callback: (err: Error, res: any) => void + ): void; zremrangebyscore(key: KeyType, min: number | string, max: number | string): Promise; zremrangebyrank(key: KeyType, start: number, stop: number, callback: (err: Error, res: any) => void): void; @@ -233,18 +290,35 @@ declare namespace IORedis { zinterstore(destination: string, numkeys: number, key: KeyType, ...args: string[]): any; zrange(key: KeyType, start: number, stop: number, callback: (err: Error, res: any) => void): void; - zrange(key: KeyType, start: number, stop: number, withScores: "WITHSCORES", callback: (err: Error, res: any) => void): void; - zrange(key: KeyType, start: number, stop: number, withScores?: "WITHSCORES"): Promise; + zrange( + key: KeyType, + start: number, + stop: number, + withScores: 'WITHSCORES', + callback: (err: Error, res: any) => void + ): void; + zrange(key: KeyType, start: number, stop: number, withScores?: 'WITHSCORES'): Promise; zrevrange(key: KeyType, start: number, stop: number, callback: (err: Error, res: any) => void): void; - zrevrange(key: KeyType, start: number, stop: number, withScores: "WITHSCORES", callback: (err: Error, res: any) => void): void; - zrevrange(key: KeyType, start: number, stop: number, withScores?: "WITHSCORES"): Promise; + zrevrange( + key: KeyType, + start: number, + stop: number, + withScores: 'WITHSCORES', + callback: (err: Error, res: any) => void + ): void; + zrevrange(key: KeyType, start: number, stop: number, withScores?: 'WITHSCORES'): Promise; zrangebyscore(key: KeyType, min: number | string, max: number | string, ...args: string[]): any; zrevrangebyscore(key: KeyType, max: number | string, min: number | string, ...args: string[]): any; - zcount(key: KeyType, min: number | string, max: number | string, callback: (err: Error, res: number) => void): void; + zcount( + key: KeyType, + min: number | string, + max: number | string, + callback: (err: Error, res: number) => void + ): void; zcount(key: KeyType, min: number | string, max: number | string): Promise; zcard(key: KeyType, callback: (err: Error, res: number) => void): void; @@ -373,8 +447,8 @@ declare namespace IORedis { bgrewriteaof(callback: (err: Error, res: string) => void): void; bgrewriteaof(): Promise; - shutdown(save: "SAVE" | "NOSAVE", callback: (err: Error, res: any) => void): void; - shutdown(save: "SAVE" | "NOSAVE"): Promise; + shutdown(save: 'SAVE' | 'NOSAVE', callback: (err: Error, res: any) => void): void; + shutdown(save: 'SAVE' | 'NOSAVE'): Promise; lastsave(callback: (err: Error, res: number) => void): void; lastsave(): Promise; @@ -468,8 +542,20 @@ declare namespace IORedis { scan(cursor: number, matchOption: 'match' | 'MATCH', pattern: string): Promise<[string, string[]]>; scan(cursor: number, countOption: 'count' | 'COUNT', count: number): Promise<[string, string[]]>; - scan(cursor: number, matchOption: 'match' | 'MATCH', pattern: string, countOption: 'count' | 'COUNT', count: number): Promise<[string, string[]]>; - scan(cursor: number, countOption: 'count' | 'COUNT', count: number, matchOption: 'match' | 'MATCH', pattern: string): Promise<[string, string[]]>; + scan( + cursor: number, + matchOption: 'match' | 'MATCH', + pattern: string, + countOption: 'count' | 'COUNT', + count: number + ): Promise<[string, string[]]>; + scan( + cursor: number, + countOption: 'count' | 'COUNT', + count: number, + matchOption: 'match' | 'MATCH', + pattern: string + ): Promise<[string, string[]]>; sscan(key: KeyType, cursor: number, ...args: any[]): any; @@ -512,7 +598,7 @@ declare namespace IORedis { xread(...args: any[]): any; - xreadgroup(groupOption: 'GROUP' | 'group', group: string, consumer: string, ...args: any[]): any; + xreadgroup(groupOption: 'GROUP' | 'group', group: string, consumer: string, ...args: any[]): any; xrevrange(key: KeyType, end: string, start: string, ...args: any[]): any; @@ -535,13 +621,39 @@ declare namespace IORedis { set(key: KeyType, value: any, callback?: (err: Error, res: string) => void): Pipeline; set(key: KeyType, value: any, setMode: string, callback?: (err: Error, res: string) => void): Pipeline; - set(key: KeyType, value: any, expiryMode: string, time: number, callback?: (err: Error, res: string) => void): Pipeline; - set(key: KeyType, value: any, expiryMode: string, time: number, setMode: string, callback?: (err: Error, res: string) => void): Pipeline; + set( + key: KeyType, + value: any, + expiryMode: string, + time: number, + callback?: (err: Error, res: string) => void + ): Pipeline; + set( + key: KeyType, + value: any, + expiryMode: string, + time: number, + setMode: string, + callback?: (err: Error, res: string) => void + ): Pipeline; setBuffer(key: KeyType, value: any, callback?: (err: Error, res: Buffer) => void): Pipeline; setBuffer(key: KeyType, value: any, setMode: string, callback?: (err: Error, res: Buffer) => void): Pipeline; - setBuffer(key: KeyType, value: any, expiryMode: string, time: number, callback?: (err: Error, res: Buffer) => void): Pipeline; - setBuffer(key: KeyType, value: any, expiryMode: string, time: number, setMode: string, callback?: (err: Error, res: Buffer) => void): Pipeline; + setBuffer( + key: KeyType, + value: any, + expiryMode: string, + time: number, + callback?: (err: Error, res: Buffer) => void + ): Pipeline; + setBuffer( + key: KeyType, + value: any, + expiryMode: string, + time: number, + setMode: string, + callback?: (err: Error, res: Buffer) => void + ): Pipeline; setnx(key: KeyType, value: any, callback?: (err: Error, res: any) => void): Pipeline; @@ -581,7 +693,13 @@ declare namespace IORedis { lpushx(key: KeyType, value: any, callback?: (err: Error, res: number) => void): Pipeline; - linsert(key: KeyType, direction: "BEFORE" | "AFTER", pivot: string, value: any, callback?: (err: Error, res: number) => void): Pipeline; + linsert( + key: KeyType, + direction: 'BEFORE' | 'AFTER', + pivot: string, + value: any, + callback?: (err: Error, res: number) => void + ): Pipeline; rpop(key: KeyType, callback?: (err: Error, res: string) => void): Pipeline; @@ -591,7 +709,12 @@ declare namespace IORedis { blpop(...keys: KeyType[]): Pipeline; - brpoplpush(source: string, destination: string, timeout: number, callback?: (err: Error, res: any) => void): Pipeline; + brpoplpush( + source: string, + destination: string, + timeout: number, + callback?: (err: Error, res: any) => void + ): Pipeline; llen(key: KeyType, callback?: (err: Error, res: number) => void): Pipeline; @@ -611,7 +734,12 @@ declare namespace IORedis { srem(key: KeyType, ...members: any[]): Pipeline; - smove(source: string, destination: string, member: string, callback?: (err: Error, res: string) => void): Pipeline; + smove( + source: string, + destination: string, + member: string, + callback?: (err: Error, res: string) => void + ): Pipeline; sismember(key: KeyType, member: string, callback?: (err: Error, res: 1 | 0) => void): Pipeline; @@ -643,7 +771,12 @@ declare namespace IORedis { zrem(key: KeyType, ...members: any[]): Pipeline; - zremrangebyscore(key: KeyType, min: number | string, max: number | string, callback?: (err: Error, res: any) => void): Pipeline; + zremrangebyscore( + key: KeyType, + min: number | string, + max: number | string, + callback?: (err: Error, res: any) => void + ): Pipeline; zremrangebyrank(key: KeyType, start: number, stop: number, callback?: (err: Error, res: any) => void): Pipeline; @@ -652,16 +785,33 @@ declare namespace IORedis { zinterstore(destination: string, numkeys: number, key: KeyType, ...args: string[]): Pipeline; zrange(key: KeyType, start: number, stop: number, callback?: (err: Error, res: any) => void): Pipeline; - zrange(key: KeyType, start: number, stop: number, withScores: "WITHSCORES", callback?: (err: Error, res: any) => void): Pipeline; + zrange( + key: KeyType, + start: number, + stop: number, + withScores: 'WITHSCORES', + callback?: (err: Error, res: any) => void + ): Pipeline; zrevrange(key: KeyType, start: number, stop: number, callback?: (err: Error, res: any) => void): Pipeline; - zrevrange(key: KeyType, start: number, stop: number, withScores: "WITHSCORES", callback?: (err: Error, res: any) => void): Pipeline; + zrevrange( + key: KeyType, + start: number, + stop: number, + withScores: 'WITHSCORES', + callback?: (err: Error, res: any) => void + ): Pipeline; zrangebyscore(key: KeyType, min: number | string, max: number | string, ...args: string[]): Pipeline; zrevrangebyscore(key: KeyType, max: number | string, min: number | string, ...args: string[]): Pipeline; - zcount(key: KeyType, min: number | string, max: number | string, callback?: (err: Error, res: number) => void): Pipeline; + zcount( + key: KeyType, + min: number | string, + max: number | string, + callback?: (err: Error, res: number) => void + ): Pipeline; zcard(key: KeyType, callback?: (err: Error, res: number) => void): Pipeline; @@ -686,7 +836,12 @@ declare namespace IORedis { hincrby(key: KeyType, field: string, increment: number, callback?: (err: Error, res: number) => void): Pipeline; - hincrbyfloat(key: KeyType, field: string, increment: number, callback?: (err: Error, res: number) => void): Pipeline; + hincrbyfloat( + key: KeyType, + field: string, + increment: number, + callback?: (err: Error, res: number) => void + ): Pipeline; hdel(key: KeyType, ...fields: string[]): Pipeline; @@ -749,7 +904,7 @@ declare namespace IORedis { bgrewriteaof(callback?: (err: Error, res: string) => void): Pipeline; - shutdown(save: "SAVE" | "NOSAVE", callback?: (err: Error, res: any) => void): Pipeline; + shutdown(save: 'SAVE' | 'NOSAVE', callback?: (err: Error, res: any) => void): Pipeline; lastsave(callback?: (err: Error, res: number) => void): Pipeline; @@ -825,8 +980,20 @@ declare namespace IORedis { scan(cursor: number, matchOption: 'match' | 'MATCH', pattern: string): Pipeline; scan(cursor: number, countOption: 'count' | 'COUNT', count: number): Pipeline; - scan(cursor: number, matchOption: 'match' | 'MATCH', pattern: string, countOption: 'count' | 'COUNT', count: number): Pipeline; - scan(cursor: number, countOption: 'count' | 'COUNT', count: number, matchOption: 'match' | 'MATCH', pattern: string): Pipeline; + scan( + cursor: number, + matchOption: 'match' | 'MATCH', + pattern: string, + countOption: 'count' | 'COUNT', + count: number + ): Pipeline; + scan( + cursor: number, + countOption: 'count' | 'COUNT', + count: number, + matchOption: 'match' | 'MATCH', + pattern: string + ): Pipeline; sscan(key: KeyType, cursor: number, ...args: any[]): Pipeline; hscan(key: KeyType, cursor: number, ...args: any[]): Pipeline; @@ -843,7 +1010,14 @@ declare namespace IORedis { xadd(key: KeyType, id: string, ...args: string[]): Pipeline; - xclaim(key: KeyType, group: string, consumer: string, minIdleTime: number, id: string, ...args: any[]): Pipeline; + xclaim( + key: KeyType, + group: string, + consumer: string, + minIdleTime: number, + id: string, + ...args: any[] + ): Pipeline; xdel(key: KeyType, ...ids: string[]): Pipeline; @@ -859,7 +1033,7 @@ declare namespace IORedis { xread(...args: any[]): Pipeline; - xreadgroup(command: 'GROUP' | 'group', group: string, consumer: string, ...args: any[]): Pipeline; + xreadgroup(command: 'GROUP' | 'group', group: string, consumer: string, ...args: any[]): Pipeline; xrevrange(key: KeyType, end: string, start: string, ...args: any[]): Pipeline; @@ -873,11 +1047,14 @@ declare namespace IORedis { type ClusterNode = string | number | NodeConfiguration; - interface Cluster extends NodeJS.EventEmitter, Commander { - new(nodes: ClusterNode[], options?: ClusterOptions): Redis; + interface Cluster { connect(callback: () => void): Promise; disconnect(): void; - nodes(role: string): Redis[]; + nodes: Redis[]; + } + + interface ClusterStatic extends NodeJS.EventEmitter, Commander { + new (nodes: ClusterNode[], options?: ClusterOptions): Cluster; } interface RedisOptions { @@ -958,7 +1135,7 @@ declare namespace IORedis { autoResendUnfulfilledCommands?: boolean; lazyConnect?: boolean; tls?: tls.ConnectionOptions; - sentinels?: Array<{ host: string; port: number; }>; + sentinels?: Array<{ host: string; port: number }>; name?: string; /** * Enable READONLY mode for the connection. Only available for cluster mode. @@ -981,9 +1158,12 @@ declare namespace IORedis { count?: number; } - type DNSLookupFunction = (hostname: string, callback: (err: NodeJS.ErrnoException, address: string, family: number) => void) => void; + type DNSLookupFunction = ( + hostname: string, + callback: (err: NodeJS.ErrnoException, address: string, family: number) => void + ) => void; interface NatMap { - [key: string]: {host: string, port: number}; + [key: string]: { host: string; port: number }; } interface ClusterOptions { diff --git a/types/ioredis/ioredis-tests.ts b/types/ioredis/ioredis-tests.ts index fdb1fafcc1..0428fe3086 100644 --- a/types/ioredis/ioredis-tests.ts +++ b/types/ioredis/ioredis-tests.ts @@ -1,4 +1,4 @@ -import Redis = require("ioredis"); +import Redis = require('ioredis'); const redis = new Redis(); @@ -38,23 +38,25 @@ redis.set('key', '100', ['EX', 10, 'NX'], (err, data) => {}); redis.setBuffer('key', '100', 'NX', 'EX', 10, (err, data) => {}); redis.exists('foo').then(result => result * 1); -redis.exists('foo', ((err, data) => data * 1)); +redis.exists('foo', (err, data) => data * 1); // Should support usage of Buffer redis.set(Buffer.from('key'), '100'); redis.setBuffer(Buffer.from('key'), '100', 'NX', 'EX', 10); -new Redis(); // Connect to 127.0.0.1:6379 -new Redis(6380); // 127.0.0.1:6380 -new Redis(6379, '192.168.1.1'); // 192.168.1.1:6379 +new Redis(); // Connect to 127.0.0.1:6379 +new Redis(6380); // 127.0.0.1:6380 +new Redis(6379, '192.168.1.1'); // 192.168.1.1:6379 new Redis('/tmp/redis.sock'); new Redis({ - port: 6379, // Redis port - host: '127.0.0.1', // Redis host - family: 4, // 4 (IPv4) or 6 (IPv6) + port: 6379, // Redis port + host: '127.0.0.1', // Redis host + family: 4, // 4 (IPv4) or 6 (IPv6) password: 'auth', db: 0, - retryStrategy() { return false; }, + retryStrategy() { + return false; + }, maxRetriesPerRequest: 20, showFriendlyErrorStack: true, tls: { @@ -97,25 +99,35 @@ pipeline.exec((err, results) => { }); // You can even chain the commands: -redis.pipeline().set('foo', 'bar').del('cc').exec((err, results) => { -}); +redis + .pipeline() + .set('foo', 'bar') + .del('cc') + .exec((err, results) => {}); // `exec` also returns a Promise: -const promise = redis.pipeline().set('foo', 'bar').get('foo').exec(); -promise.then((result) => { +const promise = redis + .pipeline() + .set('foo', 'bar') + .get('foo') + .exec(); +promise.then(result => { // result === [[null, 'OK'], [null, 'bar']] }); -redis.pipeline().set('foo', 'bar').get('foo', (err, result) => { - // result === 'bar' -}).exec((err, result) => { - // result[1][1] === 'bar' -}); +redis + .pipeline() + .set('foo', 'bar') + .get('foo', (err, result) => { + // result === 'bar' + }) + .exec((err, result) => { + // result[1][1] === 'bar' + }); -redis.pipeline([ - ['set', 'foo', 'bar'], - ['get', 'foo'] -]).exec(() => { /* ... */ }); +redis.pipeline([['set', 'foo', 'bar'], ['get', 'foo']]).exec(() => { + /* ... */ +}); Redis.Command.setArgumentTransformer('set', args => { return args; @@ -126,28 +138,33 @@ Redis.Command.setReplyTransformer('get', (result: any) => { }); redis.scan(0, 'match', '*foo*', 'count', 20).then(([nextCursor, keys]) => { - // nextCursor is always a string - if (nextCursor === '0') { - // keys is always an array of strings and it might be empty - return keys.map(key => key.trim()); - } + // nextCursor is always a string + if (nextCursor === '0') { + // keys is always an array of strings and it might be empty + return keys.map(key => key.trim()); + } }); -redis.pipeline().scan(0, 'count', 20, 'match', '*foo*').exec((err, result) => { - // result = [[null, [nextCursor, keys]]] -}); +redis + .pipeline() + .scan(0, 'count', 20, 'match', '*foo*') + .exec((err, result) => { + // result = [[null, [nextCursor, keys]]] + }); // multi -redis.multi().set('foo', 'bar').set('foo', 'baz').get('foo', (err, result) => { - // result === 'QUEUED' -}).exec((err, results) => { - // results = [[null, 'OK'], [null, 'OK'], [null, 'baz']] -}); +redis + .multi() + .set('foo', 'bar') + .set('foo', 'baz') + .get('foo', (err, result) => { + // result === 'QUEUED' + }) + .exec((err, results) => { + // results = [[null, 'OK'], [null, 'OK'], [null, 'baz']] + }); -redis.multi([ - ['set', 'foo', 'bar'], - ['get', 'foo'] -]).exec((err, results) => { +redis.multi([['set', 'foo', 'bar'], ['get', 'foo']]).exec((err, results) => { // results = [[null, 'OK'], [null, 'bar']] }); @@ -157,26 +174,28 @@ redis.mget(...keys); redis.mset(...['foo', 'bar']); redis.mset({ foo: 'bar' }); +new Redis.Cluster(['localhost']); + +new Redis.Cluster([6379]); + new Redis.Cluster([ - 'localhost' + { + host: 'localhost' + } ]); new Redis.Cluster([ - 6379 + { + port: 6379 + } ]); -new Redis.Cluster([{ - host: 'localhost' -}]); - -new Redis.Cluster([{ - port: 6379 -}]); - -new Redis.Cluster([{ - host: 'localhost', - port: 6379 -}]); +new Redis.Cluster([ + { + host: 'localhost', + port: 6379 + } +]); redis.xack('streamName', 'groupName', 'id'); redis.xadd('streamName', '*', 'field', 'name'); @@ -207,3 +226,21 @@ new Redis.Cluster([], { new Redis.Cluster([], { clusterRetryStrategy: (times: number, reason?: Error) => 1 }); + +// Cluster types +const clusterOptions: Redis.ClusterOptions = {}; +const cluster = new Redis.Cluster( + [ + { + host: 'localhost', + port: 6379 + } + ], + clusterOptions +); +cluster.nodes.map(node => { + node.pipeline() + .flushdb() + .exec() + .then(result => console.log(result)); +}); From 1ab5816356530184e9a7937989b1c34cec5b5e8f Mon Sep 17 00:00:00 2001 From: Nick Roberts Date: Fri, 22 Feb 2019 20:01:24 -0500 Subject: [PATCH 2/4] :art: Remove excess format changes --- types/ioredis/index.d.ts | 271 ++++++--------------------------- types/ioredis/ioredis-tests.ts | 125 +++++++-------- 2 files changed, 100 insertions(+), 296 deletions(-) diff --git a/types/ioredis/index.d.ts b/types/ioredis/index.d.ts index 7158e9fb31..b36cf485cf 100644 --- a/types/ioredis/index.d.ts +++ b/types/ioredis/index.d.ts @@ -24,9 +24,9 @@ import tls = require('tls'); interface RedisStatic { - new (port?: number, host?: string, options?: IORedis.RedisOptions): IORedis.Redis; - new (host?: string, options?: IORedis.RedisOptions): IORedis.Redis; - new (options?: IORedis.RedisOptions): IORedis.Redis; + new(port?: number, host?: string, options?: IORedis.RedisOptions): IORedis.Redis; + new(host?: string, options?: IORedis.RedisOptions): IORedis.Redis; + new(options?: IORedis.RedisOptions): IORedis.Redis; (port?: number, host?: string, options?: IORedis.RedisOptions): IORedis.Redis; (host?: string, options?: IORedis.RedisOptions): IORedis.Redis; (options?: IORedis.RedisOptions): IORedis.Redis; @@ -40,13 +40,10 @@ export = IORedis; declare class Commander { getBuiltinCommands(): string[]; createBuiltinCommand(commandName: string): {}; - defineCommand( - name: string, - definition: { - numberOfKeys?: number; - lua?: string; - } - ): any; + defineCommand(name: string, definition: { + numberOfKeys?: number; + lua?: string; + }): any; sendCommand(): void; } @@ -78,57 +75,19 @@ declare namespace IORedis { getBuffer(key: KeyType, callback: (err: Error, res: Buffer) => void): void; getBuffer(key: KeyType): Promise; - set( - key: KeyType, - value: any, - expiryMode?: string | any[], - time?: number | string, - setMode?: number | string - ): Promise; + set(key: KeyType, value: any, expiryMode?: string | any[], time?: number | string, setMode?: number | string): Promise; set(key: KeyType, value: any, callback: (err: Error, res: string) => void): void; set(key: KeyType, value: any, setMode: string | any[], callback: (err: Error, res: string) => void): void; - set( - key: KeyType, - value: any, - expiryMode: string, - time: number | string, - callback: (err: Error, res: string) => void - ): void; - set( - key: KeyType, - value: any, - expiryMode: string, - time: number | string, - setMode: number | string, - callback: (err: Error, res: string) => void - ): void; + set(key: KeyType, value: any, expiryMode: string, time: number | string, callback: (err: Error, res: string) => void): void; + set(key: KeyType, value: any, expiryMode: string, time: number | string, setMode: number | string, callback: (err: Error, res: string) => void): void; - setBuffer( - key: KeyType, - value: any, - expiryMode?: string | any[], - time?: number | string, - setMode?: number | string - ): Promise; + setBuffer(key: KeyType, value: any, expiryMode?: string | any[], time?: number | string, setMode?: number | string): Promise; setBuffer(key: KeyType, value: any, callback: (err: Error, res: Buffer) => void): void; setBuffer(key: KeyType, value: any, setMode: string, callback: (err: Error, res: Buffer) => void): void; - setBuffer( - key: KeyType, - value: any, - expiryMode: string, - time: number, - callback: (err: Error, res: Buffer) => void - ): void; - setBuffer( - key: KeyType, - value: any, - expiryMode: string, - time: number | string, - setMode: number | string, - callback: (err: Error, res: Buffer) => void - ): void; + setBuffer(key: KeyType, value: any, expiryMode: string, time: number, callback: (err: Error, res: Buffer) => void): void; + setBuffer(key: KeyType, value: any, expiryMode: string, time: number | string, setMode: number | string, callback: (err: Error, res: Buffer) => void): void; setnx(key: KeyType, value: any, callback: (err: Error, res: any) => void): void; setnx(key: KeyType, value: any): Promise; @@ -183,14 +142,8 @@ declare namespace IORedis { lpushx(key: KeyType, value: any, callback: (err: Error, res: number) => void): void; lpushx(key: KeyType, value: any): Promise; - linsert( - key: KeyType, - direction: 'BEFORE' | 'AFTER', - pivot: string, - value: any, - callback: (err: Error, res: number) => void - ): void; - linsert(key: KeyType, direction: 'BEFORE' | 'AFTER', pivot: string, value: any): Promise; + linsert(key: KeyType, direction: "BEFORE" | "AFTER", pivot: string, value: any, callback: (err: Error, res: number) => void): void; + linsert(key: KeyType, direction: "BEFORE" | "AFTER", pivot: string, value: any): Promise; rpop(key: KeyType, callback: (err: Error, res: string) => void): void; rpop(key: KeyType): Promise; @@ -202,12 +155,7 @@ declare namespace IORedis { blpop(...keys: KeyType[]): any; - brpoplpush( - source: string, - destination: string, - timeout: number, - callback: (err: Error, res: any) => void - ): void; + brpoplpush(source: string, destination: string, timeout: number, callback: (err: Error, res: any) => void): void; brpoplpush(source: string, destination: string, timeout: number): Promise; llen(key: KeyType, callback: (err: Error, res: number) => void): void; @@ -274,12 +222,7 @@ declare namespace IORedis { zrem(key: KeyType, ...members: any[]): any; - zremrangebyscore( - key: KeyType, - min: number | string, - max: number | string, - callback: (err: Error, res: any) => void - ): void; + zremrangebyscore(key: KeyType, min: number | string, max: number | string, callback: (err: Error, res: any) => void): void; zremrangebyscore(key: KeyType, min: number | string, max: number | string): Promise; zremrangebyrank(key: KeyType, start: number, stop: number, callback: (err: Error, res: any) => void): void; @@ -290,35 +233,18 @@ declare namespace IORedis { zinterstore(destination: string, numkeys: number, key: KeyType, ...args: string[]): any; zrange(key: KeyType, start: number, stop: number, callback: (err: Error, res: any) => void): void; - zrange( - key: KeyType, - start: number, - stop: number, - withScores: 'WITHSCORES', - callback: (err: Error, res: any) => void - ): void; - zrange(key: KeyType, start: number, stop: number, withScores?: 'WITHSCORES'): Promise; + zrange(key: KeyType, start: number, stop: number, withScores: "WITHSCORES", callback: (err: Error, res: any) => void): void; + zrange(key: KeyType, start: number, stop: number, withScores?: "WITHSCORES"): Promise; zrevrange(key: KeyType, start: number, stop: number, callback: (err: Error, res: any) => void): void; - zrevrange( - key: KeyType, - start: number, - stop: number, - withScores: 'WITHSCORES', - callback: (err: Error, res: any) => void - ): void; - zrevrange(key: KeyType, start: number, stop: number, withScores?: 'WITHSCORES'): Promise; + zrevrange(key: KeyType, start: number, stop: number, withScores: "WITHSCORES", callback: (err: Error, res: any) => void): void; + zrevrange(key: KeyType, start: number, stop: number, withScores?: "WITHSCORES"): Promise; zrangebyscore(key: KeyType, min: number | string, max: number | string, ...args: string[]): any; zrevrangebyscore(key: KeyType, max: number | string, min: number | string, ...args: string[]): any; - zcount( - key: KeyType, - min: number | string, - max: number | string, - callback: (err: Error, res: number) => void - ): void; + zcount(key: KeyType, min: number | string, max: number | string, callback: (err: Error, res: number) => void): void; zcount(key: KeyType, min: number | string, max: number | string): Promise; zcard(key: KeyType, callback: (err: Error, res: number) => void): void; @@ -447,8 +373,8 @@ declare namespace IORedis { bgrewriteaof(callback: (err: Error, res: string) => void): void; bgrewriteaof(): Promise; - shutdown(save: 'SAVE' | 'NOSAVE', callback: (err: Error, res: any) => void): void; - shutdown(save: 'SAVE' | 'NOSAVE'): Promise; + shutdown(save: "SAVE" | "NOSAVE", callback: (err: Error, res: any) => void): void; + shutdown(save: "SAVE" | "NOSAVE"): Promise; lastsave(callback: (err: Error, res: number) => void): void; lastsave(): Promise; @@ -542,20 +468,8 @@ declare namespace IORedis { scan(cursor: number, matchOption: 'match' | 'MATCH', pattern: string): Promise<[string, string[]]>; scan(cursor: number, countOption: 'count' | 'COUNT', count: number): Promise<[string, string[]]>; - scan( - cursor: number, - matchOption: 'match' | 'MATCH', - pattern: string, - countOption: 'count' | 'COUNT', - count: number - ): Promise<[string, string[]]>; - scan( - cursor: number, - countOption: 'count' | 'COUNT', - count: number, - matchOption: 'match' | 'MATCH', - pattern: string - ): Promise<[string, string[]]>; + scan(cursor: number, matchOption: 'match' | 'MATCH', pattern: string, countOption: 'count' | 'COUNT', count: number): Promise<[string, string[]]>; + scan(cursor: number, countOption: 'count' | 'COUNT', count: number, matchOption: 'match' | 'MATCH', pattern: string): Promise<[string, string[]]>; sscan(key: KeyType, cursor: number, ...args: any[]): any; @@ -598,7 +512,7 @@ declare namespace IORedis { xread(...args: any[]): any; - xreadgroup(groupOption: 'GROUP' | 'group', group: string, consumer: string, ...args: any[]): any; + xreadgroup(groupOption: 'GROUP' | 'group', group: string, consumer: string, ...args: any[]): any; xrevrange(key: KeyType, end: string, start: string, ...args: any[]): any; @@ -621,39 +535,13 @@ declare namespace IORedis { set(key: KeyType, value: any, callback?: (err: Error, res: string) => void): Pipeline; set(key: KeyType, value: any, setMode: string, callback?: (err: Error, res: string) => void): Pipeline; - set( - key: KeyType, - value: any, - expiryMode: string, - time: number, - callback?: (err: Error, res: string) => void - ): Pipeline; - set( - key: KeyType, - value: any, - expiryMode: string, - time: number, - setMode: string, - callback?: (err: Error, res: string) => void - ): Pipeline; + set(key: KeyType, value: any, expiryMode: string, time: number, callback?: (err: Error, res: string) => void): Pipeline; + set(key: KeyType, value: any, expiryMode: string, time: number, setMode: string, callback?: (err: Error, res: string) => void): Pipeline; setBuffer(key: KeyType, value: any, callback?: (err: Error, res: Buffer) => void): Pipeline; setBuffer(key: KeyType, value: any, setMode: string, callback?: (err: Error, res: Buffer) => void): Pipeline; - setBuffer( - key: KeyType, - value: any, - expiryMode: string, - time: number, - callback?: (err: Error, res: Buffer) => void - ): Pipeline; - setBuffer( - key: KeyType, - value: any, - expiryMode: string, - time: number, - setMode: string, - callback?: (err: Error, res: Buffer) => void - ): Pipeline; + setBuffer(key: KeyType, value: any, expiryMode: string, time: number, callback?: (err: Error, res: Buffer) => void): Pipeline; + setBuffer(key: KeyType, value: any, expiryMode: string, time: number, setMode: string, callback?: (err: Error, res: Buffer) => void): Pipeline; setnx(key: KeyType, value: any, callback?: (err: Error, res: any) => void): Pipeline; @@ -693,13 +581,7 @@ declare namespace IORedis { lpushx(key: KeyType, value: any, callback?: (err: Error, res: number) => void): Pipeline; - linsert( - key: KeyType, - direction: 'BEFORE' | 'AFTER', - pivot: string, - value: any, - callback?: (err: Error, res: number) => void - ): Pipeline; + linsert(key: KeyType, direction: "BEFORE" | "AFTER", pivot: string, value: any, callback?: (err: Error, res: number) => void): Pipeline; rpop(key: KeyType, callback?: (err: Error, res: string) => void): Pipeline; @@ -709,12 +591,7 @@ declare namespace IORedis { blpop(...keys: KeyType[]): Pipeline; - brpoplpush( - source: string, - destination: string, - timeout: number, - callback?: (err: Error, res: any) => void - ): Pipeline; + brpoplpush(source: string, destination: string, timeout: number, callback?: (err: Error, res: any) => void): Pipeline; llen(key: KeyType, callback?: (err: Error, res: number) => void): Pipeline; @@ -734,12 +611,7 @@ declare namespace IORedis { srem(key: KeyType, ...members: any[]): Pipeline; - smove( - source: string, - destination: string, - member: string, - callback?: (err: Error, res: string) => void - ): Pipeline; + smove(source: string, destination: string, member: string, callback?: (err: Error, res: string) => void): Pipeline; sismember(key: KeyType, member: string, callback?: (err: Error, res: 1 | 0) => void): Pipeline; @@ -771,12 +643,7 @@ declare namespace IORedis { zrem(key: KeyType, ...members: any[]): Pipeline; - zremrangebyscore( - key: KeyType, - min: number | string, - max: number | string, - callback?: (err: Error, res: any) => void - ): Pipeline; + zremrangebyscore(key: KeyType, min: number | string, max: number | string, callback?: (err: Error, res: any) => void): Pipeline; zremrangebyrank(key: KeyType, start: number, stop: number, callback?: (err: Error, res: any) => void): Pipeline; @@ -785,33 +652,16 @@ declare namespace IORedis { zinterstore(destination: string, numkeys: number, key: KeyType, ...args: string[]): Pipeline; zrange(key: KeyType, start: number, stop: number, callback?: (err: Error, res: any) => void): Pipeline; - zrange( - key: KeyType, - start: number, - stop: number, - withScores: 'WITHSCORES', - callback?: (err: Error, res: any) => void - ): Pipeline; + zrange(key: KeyType, start: number, stop: number, withScores: "WITHSCORES", callback?: (err: Error, res: any) => void): Pipeline; zrevrange(key: KeyType, start: number, stop: number, callback?: (err: Error, res: any) => void): Pipeline; - zrevrange( - key: KeyType, - start: number, - stop: number, - withScores: 'WITHSCORES', - callback?: (err: Error, res: any) => void - ): Pipeline; + zrevrange(key: KeyType, start: number, stop: number, withScores: "WITHSCORES", callback?: (err: Error, res: any) => void): Pipeline; zrangebyscore(key: KeyType, min: number | string, max: number | string, ...args: string[]): Pipeline; zrevrangebyscore(key: KeyType, max: number | string, min: number | string, ...args: string[]): Pipeline; - zcount( - key: KeyType, - min: number | string, - max: number | string, - callback?: (err: Error, res: number) => void - ): Pipeline; + zcount(key: KeyType, min: number | string, max: number | string, callback?: (err: Error, res: number) => void): Pipeline; zcard(key: KeyType, callback?: (err: Error, res: number) => void): Pipeline; @@ -836,12 +686,7 @@ declare namespace IORedis { hincrby(key: KeyType, field: string, increment: number, callback?: (err: Error, res: number) => void): Pipeline; - hincrbyfloat( - key: KeyType, - field: string, - increment: number, - callback?: (err: Error, res: number) => void - ): Pipeline; + hincrbyfloat(key: KeyType, field: string, increment: number, callback?: (err: Error, res: number) => void): Pipeline; hdel(key: KeyType, ...fields: string[]): Pipeline; @@ -904,7 +749,7 @@ declare namespace IORedis { bgrewriteaof(callback?: (err: Error, res: string) => void): Pipeline; - shutdown(save: 'SAVE' | 'NOSAVE', callback?: (err: Error, res: any) => void): Pipeline; + shutdown(save: "SAVE" | "NOSAVE", callback?: (err: Error, res: any) => void): Pipeline; lastsave(callback?: (err: Error, res: number) => void): Pipeline; @@ -980,20 +825,8 @@ declare namespace IORedis { scan(cursor: number, matchOption: 'match' | 'MATCH', pattern: string): Pipeline; scan(cursor: number, countOption: 'count' | 'COUNT', count: number): Pipeline; - scan( - cursor: number, - matchOption: 'match' | 'MATCH', - pattern: string, - countOption: 'count' | 'COUNT', - count: number - ): Pipeline; - scan( - cursor: number, - countOption: 'count' | 'COUNT', - count: number, - matchOption: 'match' | 'MATCH', - pattern: string - ): Pipeline; + scan(cursor: number, matchOption: 'match' | 'MATCH', pattern: string, countOption: 'count' | 'COUNT', count: number): Pipeline; + scan(cursor: number, countOption: 'count' | 'COUNT', count: number, matchOption: 'match' | 'MATCH', pattern: string): Pipeline; sscan(key: KeyType, cursor: number, ...args: any[]): Pipeline; hscan(key: KeyType, cursor: number, ...args: any[]): Pipeline; @@ -1010,14 +843,7 @@ declare namespace IORedis { xadd(key: KeyType, id: string, ...args: string[]): Pipeline; - xclaim( - key: KeyType, - group: string, - consumer: string, - minIdleTime: number, - id: string, - ...args: any[] - ): Pipeline; + xclaim(key: KeyType, group: string, consumer: string, minIdleTime: number, id: string, ...args: any[]): Pipeline; xdel(key: KeyType, ...ids: string[]): Pipeline; @@ -1033,7 +859,7 @@ declare namespace IORedis { xread(...args: any[]): Pipeline; - xreadgroup(command: 'GROUP' | 'group', group: string, consumer: string, ...args: any[]): Pipeline; + xreadgroup(command: 'GROUP' | 'group', group: string, consumer: string, ...args: any[]): Pipeline; xrevrange(key: KeyType, end: string, start: string, ...args: any[]): Pipeline; @@ -1135,7 +961,7 @@ declare namespace IORedis { autoResendUnfulfilledCommands?: boolean; lazyConnect?: boolean; tls?: tls.ConnectionOptions; - sentinels?: Array<{ host: string; port: number }>; + sentinels?: Array<{ host: string; port: number; }>; name?: string; /** * Enable READONLY mode for the connection. Only available for cluster mode. @@ -1158,12 +984,9 @@ declare namespace IORedis { count?: number; } - type DNSLookupFunction = ( - hostname: string, - callback: (err: NodeJS.ErrnoException, address: string, family: number) => void - ) => void; + type DNSLookupFunction = (hostname: string, callback: (err: NodeJS.ErrnoException, address: string, family: number) => void) => void; interface NatMap { - [key: string]: { host: string; port: number }; + [key: string]: {host: string, port: number}; } interface ClusterOptions { diff --git a/types/ioredis/ioredis-tests.ts b/types/ioredis/ioredis-tests.ts index 0428fe3086..284084cf93 100644 --- a/types/ioredis/ioredis-tests.ts +++ b/types/ioredis/ioredis-tests.ts @@ -1,4 +1,4 @@ -import Redis = require('ioredis'); +import Redis = require("ioredis"); const redis = new Redis(); @@ -38,25 +38,23 @@ redis.set('key', '100', ['EX', 10, 'NX'], (err, data) => {}); redis.setBuffer('key', '100', 'NX', 'EX', 10, (err, data) => {}); redis.exists('foo').then(result => result * 1); -redis.exists('foo', (err, data) => data * 1); +redis.exists('foo', ((err, data) => data * 1)); // Should support usage of Buffer redis.set(Buffer.from('key'), '100'); redis.setBuffer(Buffer.from('key'), '100', 'NX', 'EX', 10); -new Redis(); // Connect to 127.0.0.1:6379 -new Redis(6380); // 127.0.0.1:6380 -new Redis(6379, '192.168.1.1'); // 192.168.1.1:6379 +new Redis(); // Connect to 127.0.0.1:6379 +new Redis(6380); // 127.0.0.1:6380 +new Redis(6379, '192.168.1.1'); // 192.168.1.1:6379 new Redis('/tmp/redis.sock'); new Redis({ - port: 6379, // Redis port - host: '127.0.0.1', // Redis host - family: 4, // 4 (IPv4) or 6 (IPv6) + port: 6379, // Redis port + host: '127.0.0.1', // Redis host + family: 4, // 4 (IPv4) or 6 (IPv6) password: 'auth', db: 0, - retryStrategy() { - return false; - }, + retryStrategy() { return false; }, maxRetriesPerRequest: 20, showFriendlyErrorStack: true, tls: { @@ -99,36 +97,26 @@ pipeline.exec((err, results) => { }); // You can even chain the commands: -redis - .pipeline() - .set('foo', 'bar') - .del('cc') - .exec((err, results) => {}); +redis.pipeline().set('foo', 'bar').del('cc').exec((err, results) => { +}); // `exec` also returns a Promise: -const promise = redis - .pipeline() - .set('foo', 'bar') - .get('foo') - .exec(); -promise.then(result => { +const promise = redis.pipeline().set('foo', 'bar').get('foo').exec(); +promise.then((result) => { // result === [[null, 'OK'], [null, 'bar']] }); -redis - .pipeline() - .set('foo', 'bar') - .get('foo', (err, result) => { - // result === 'bar' - }) - .exec((err, result) => { - // result[1][1] === 'bar' - }); - -redis.pipeline([['set', 'foo', 'bar'], ['get', 'foo']]).exec(() => { - /* ... */ +redis.pipeline().set('foo', 'bar').get('foo', (err, result) => { + // result === 'bar' +}).exec((err, result) => { + // result[1][1] === 'bar' }); +redis.pipeline([ + ['set', 'foo', 'bar'], + ['get', 'foo'] +]).exec(() => { /* ... */ }); + Redis.Command.setArgumentTransformer('set', args => { return args; }); @@ -138,33 +126,28 @@ Redis.Command.setReplyTransformer('get', (result: any) => { }); redis.scan(0, 'match', '*foo*', 'count', 20).then(([nextCursor, keys]) => { - // nextCursor is always a string - if (nextCursor === '0') { - // keys is always an array of strings and it might be empty - return keys.map(key => key.trim()); - } + // nextCursor is always a string + if (nextCursor === '0') { + // keys is always an array of strings and it might be empty + return keys.map(key => key.trim()); + } }); -redis - .pipeline() - .scan(0, 'count', 20, 'match', '*foo*') - .exec((err, result) => { - // result = [[null, [nextCursor, keys]]] - }); +redis.pipeline().scan(0, 'count', 20, 'match', '*foo*').exec((err, result) => { + // result = [[null, [nextCursor, keys]]] +}); // multi -redis - .multi() - .set('foo', 'bar') - .set('foo', 'baz') - .get('foo', (err, result) => { - // result === 'QUEUED' - }) - .exec((err, results) => { - // results = [[null, 'OK'], [null, 'OK'], [null, 'baz']] - }); +redis.multi().set('foo', 'bar').set('foo', 'baz').get('foo', (err, result) => { + // result === 'QUEUED' +}).exec((err, results) => { + // results = [[null, 'OK'], [null, 'OK'], [null, 'baz']] +}); -redis.multi([['set', 'foo', 'bar'], ['get', 'foo']]).exec((err, results) => { +redis.multi([ + ['set', 'foo', 'bar'], + ['get', 'foo'] +]).exec((err, results) => { // results = [[null, 'OK'], [null, 'bar']] }); @@ -174,28 +157,26 @@ redis.mget(...keys); redis.mset(...['foo', 'bar']); redis.mset({ foo: 'bar' }); -new Redis.Cluster(['localhost']); - -new Redis.Cluster([6379]); - new Redis.Cluster([ - { - host: 'localhost' - } + 'localhost' ]); new Redis.Cluster([ - { - port: 6379 - } + 6379 ]); -new Redis.Cluster([ - { - host: 'localhost', - port: 6379 - } -]); +new Redis.Cluster([{ + host: 'localhost' +}]); + +new Redis.Cluster([{ + port: 6379 +}]); + +new Redis.Cluster([{ + host: 'localhost', + port: 6379 +}]); redis.xack('streamName', 'groupName', 'id'); redis.xadd('streamName', '*', 'field', 'name'); From 9669d70829404e6164d6e0313d62a659ce29a75a Mon Sep 17 00:00:00 2001 From: Nick Roberts Date: Sat, 23 Feb 2019 00:48:00 -0500 Subject: [PATCH 3/4] :zap: Have Cluster extend EventEmitter and Command --- types/ioredis/index.d.ts | 2 +- types/ioredis/ioredis-tests.ts | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/types/ioredis/index.d.ts b/types/ioredis/index.d.ts index b36cf485cf..f8ddf5fd43 100644 --- a/types/ioredis/index.d.ts +++ b/types/ioredis/index.d.ts @@ -873,7 +873,7 @@ declare namespace IORedis { type ClusterNode = string | number | NodeConfiguration; - interface Cluster { + interface Cluster extends NodeJS.EventEmitter, Commander { connect(callback: () => void): Promise; disconnect(): void; nodes: Redis[]; diff --git a/types/ioredis/ioredis-tests.ts b/types/ioredis/ioredis-tests.ts index 284084cf93..cefb150109 100644 --- a/types/ioredis/ioredis-tests.ts +++ b/types/ioredis/ioredis-tests.ts @@ -219,6 +219,7 @@ const cluster = new Redis.Cluster( ], clusterOptions ); +cluster.on('end', () => console.log('on end')); cluster.nodes.map(node => { node.pipeline() .flushdb() From 97203af8d9973b3402a343ff135f20357531577b Mon Sep 17 00:00:00 2001 From: Nick Roberts Date: Thu, 28 Feb 2019 07:33:21 -0500 Subject: [PATCH 4/4] :zap: Update nodes property to be a function --- types/ioredis/index.d.ts | 4 +++- types/ioredis/ioredis-tests.ts | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/types/ioredis/index.d.ts b/types/ioredis/index.d.ts index f8ddf5fd43..99c99dcf04 100644 --- a/types/ioredis/index.d.ts +++ b/types/ioredis/index.d.ts @@ -873,10 +873,12 @@ declare namespace IORedis { type ClusterNode = string | number | NodeConfiguration; + type NodeRole = 'master' | 'slave' | 'all'; + interface Cluster extends NodeJS.EventEmitter, Commander { connect(callback: () => void): Promise; disconnect(): void; - nodes: Redis[]; + nodes(role?: NodeRole): Redis[]; } interface ClusterStatic extends NodeJS.EventEmitter, Commander { diff --git a/types/ioredis/ioredis-tests.ts b/types/ioredis/ioredis-tests.ts index cefb150109..f3cafe1f18 100644 --- a/types/ioredis/ioredis-tests.ts +++ b/types/ioredis/ioredis-tests.ts @@ -220,7 +220,7 @@ const cluster = new Redis.Cluster( clusterOptions ); cluster.on('end', () => console.log('on end')); -cluster.nodes.map(node => { +cluster.nodes().map(node => { node.pipeline() .flushdb() .exec()