Skip to content

Commit 06782e6

Browse files
committed
feat: improve typings for cluster
1 parent 756e1a7 commit 06782e6

File tree

24 files changed

+10310
-1511
lines changed

24 files changed

+10310
-1511
lines changed

bin/generateRedisCommander/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ async function main() {
3232
"latency",
3333
"lolwut",
3434
"memory",
35+
"cluster",
3536
],
3637
});
3738

bin/generateRedisCommander/returnTypes.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,51 @@ module.exports = {
3131
return "string";
3232
}
3333
},
34+
cluster: (types) => {
35+
if (matchSubcommand(types, "SLOTS")) {
36+
return "[startSlotRange: number, endSlotRange: number, ...nodes: [host: string, port: number, nodeId: string, info: unknown[]][]][]";
37+
}
38+
if (
39+
matchSubcommand(types, [
40+
"ADDSLOTS",
41+
"ADDSLOTSRANGE",
42+
"DELSLOTS",
43+
"DELSLOTSRANGE",
44+
"FAILOVER",
45+
"FLUSHSLOTS",
46+
"FORGET",
47+
"MEET",
48+
"REPLICATE",
49+
"RESET",
50+
"SAVECONFIG",
51+
"SET-CONFIG-EPOCH",
52+
"SETSLOT",
53+
])
54+
) {
55+
return "'OK'";
56+
}
57+
if (matchSubcommand(types, "BUMPEPOCH")) {
58+
return "'BUMPED' | 'STILL'";
59+
}
60+
if (
61+
matchSubcommand(types, [
62+
"COUNT-FAILURE-REPORTS",
63+
"COUNTKEYSINSLOT",
64+
"KEYSLOT",
65+
])
66+
) {
67+
return "number";
68+
}
69+
if (matchSubcommand(types, "GETKEYSINSLOT")) {
70+
return "string[]";
71+
}
72+
if (matchSubcommand(types, ["INFO", "MYID"])) {
73+
return "string";
74+
}
75+
if (matchSubcommand(types, "LINKS")) {
76+
return "unknown[]";
77+
}
78+
},
3479
append: "number",
3580
asking: "'OK'",
3681
auth: "'OK'",

lib/Redis.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,15 +125,14 @@ class Redis extends Commander {
125125
this.resetCommandQueue();
126126
this.resetOfflineQueue();
127127

128-
if ("Connector" in this.options && this.options.Connector) {
128+
if (this.options.Connector) {
129129
this.connector = new this.options.Connector(this.options);
130-
} else if ("sentinels" in this.options && this.options.sentinels) {
130+
} else if (this.options.sentinels) {
131131
const sentinelConnector = new SentinelConnector(this.options);
132132
sentinelConnector.emitter = this;
133133

134134
this.connector = sentinelConnector;
135135
} else {
136-
// @ts-expect-error
137136
this.connector = new StandaloneConnector(this.options);
138137
}
139138

lib/cluster/ConnectionPool.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ type NODE_TYPE = "all" | "master" | "slave";
99

1010
export default class ConnectionPool extends EventEmitter {
1111
// master + slave = all
12-
private nodes: { [key in NODE_TYPE]: { [key: string]: any } } = {
12+
private nodes: { [key in NODE_TYPE]: { [key: string]: Redis } } = {
1313
all: {},
1414
master: {},
1515
slave: {},
@@ -21,16 +21,16 @@ export default class ConnectionPool extends EventEmitter {
2121
super();
2222
}
2323

24-
getNodes(role: NodeRole = "all"): any[] {
24+
getNodes(role: NodeRole = "all"): Redis[] {
2525
const nodes = this.nodes[role];
2626
return Object.keys(nodes).map((key) => nodes[key]);
2727
}
2828

29-
getInstanceByKey(key: NodeKey): any {
29+
getInstanceByKey(key: NodeKey): Redis {
3030
return this.nodes.all[key];
3131
}
3232

33-
getSampleInstance(role: NodeRole): any {
33+
getSampleInstance(role: NodeRole): Redis {
3434
const keys = Object.keys(this.nodes[role]);
3535
const sampleKey = sample(keys);
3636
return this.nodes[role][sampleKey];
@@ -39,7 +39,7 @@ export default class ConnectionPool extends EventEmitter {
3939
/**
4040
* Find or create a connection to the node
4141
*/
42-
findOrCreate(node: RedisOptions, readOnly = false): any {
42+
findOrCreate(node: RedisOptions, readOnly = false): Redis {
4343
const key = getNodeKey(node);
4444
readOnly = Boolean(readOnly);
4545

@@ -49,7 +49,7 @@ export default class ConnectionPool extends EventEmitter {
4949
this.specifiedOptions[key] = node;
5050
}
5151

52-
let redis;
52+
let redis: Redis;
5353
if (this.nodes.all[key]) {
5454
redis = this.nodes.all[key];
5555
if (redis.options.readOnly !== readOnly) {

lib/cluster/index.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ class Cluster extends Commander {
346346
/**
347347
* Get nodes with the specified role
348348
*/
349-
nodes(role: NodeRole = "all"): any[] {
349+
nodes(role: NodeRole = "all"): Redis[] {
350350
if (role !== "all" && role !== "master" && role !== "slave") {
351351
throw new Error(
352352
'Invalid role "' + role + '". Expected "all", "master" or "slave"'
@@ -696,7 +696,6 @@ class Cluster extends Commander {
696696
nextRound();
697697
}
698698

699-
700699
/**
701700
* Change cluster instance's status
702701
*/
@@ -785,7 +784,7 @@ class Cluster extends Commander {
785784
: nodeKey;
786785
}
787786

788-
private getInfoFromNode(redis, callback) {
787+
private getInfoFromNode(redis: Redis, callback) {
789788
if (!redis) {
790789
return callback(new Error("Node is disconnected"));
791790
}
@@ -808,7 +807,7 @@ class Cluster extends Commander {
808807
duplicatedConnection.on("error", noop);
809808

810809
duplicatedConnection.cluster(
811-
"slots",
810+
"SLOTS",
812811
timeout((err, result) => {
813812
duplicatedConnection.disconnect();
814813
if (err) {
@@ -827,7 +826,7 @@ class Cluster extends Commander {
827826
callback();
828827
return;
829828
}
830-
const nodes = [];
829+
const nodes: RedisOptions[] = [];
831830

832831
debug("cluster slots result count: %d", result.length);
833832

@@ -841,10 +840,13 @@ class Cluster extends Commander {
841840
if (!items[j][0]) {
842841
continue;
843842
}
844-
items[j] = this.natMapper({ host: items[j][0], port: items[j][1] });
845-
items[j].readOnly = j !== 2;
846-
nodes.push(items[j]);
847-
keys.push(items[j].host + ":" + items[j].port);
843+
const node = this.natMapper({
844+
host: items[j][0],
845+
port: items[j][1],
846+
});
847+
node.readOnly = j !== 2;
848+
nodes.push(node);
849+
keys.push(node.host + ":" + node.port);
848850
}
849851

850852
debug(
@@ -896,7 +898,7 @@ class Cluster extends Commander {
896898
* Check whether Cluster is able to process commands
897899
*/
898900
private readyCheck(callback: Callback<void | "fail">): void {
899-
(this as any).cluster("info", function (err, res) {
901+
this.cluster("INFO", function (err, res) {
900902
if (err) {
901903
return callback(err);
902904
}

lib/connectors/StandaloneConnector.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1-
import { createConnection, TcpNetConnectOpts, IpcNetConnectOpts } from "net";
1+
import { createConnection, IpcNetConnectOpts, TcpNetConnectOpts } from "net";
22
import { connect as createTLSConnection, ConnectionOptions } from "tls";
3+
import { NetStream } from "../types";
34
import { CONNECTION_CLOSED_ERROR_MSG } from "../utils";
45
import AbstractConnector, { ErrorEmitter } from "./AbstractConnector";
5-
import { NetStream } from "../types";
66

7-
export type StandaloneConnectionOptions = (
8-
| TcpNetConnectOpts
9-
| IpcNetConnectOpts
10-
) & { disconnectTimeout: number; tls?: ConnectionOptions };
7+
export type StandaloneConnectionOptions = (Partial<TcpNetConnectOpts> &
8+
Partial<IpcNetConnectOpts>) & {
9+
disconnectTimeout?: number;
10+
tls?: ConnectionOptions;
11+
};
1112

1213
export default class StandaloneConnector extends AbstractConnector {
1314
constructor(protected options: StandaloneConnectionOptions) {

lib/constants/TLSProfiles.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export default {
1+
const TLSProfiles = {
22
/**
33
* TLS settings for Redis.com Cloud Fixed plan. Updated on 2021-10-06.
44
*/
@@ -100,4 +100,6 @@ export default {
100100
"UhWfa/HQYOAPDdEjNgnVwLI23b8t0TozyCWw7q8h\n" +
101101
"-----END CERTIFICATE-----\n",
102102
},
103-
};
103+
} as const;
104+
105+
export default TLSProfiles;

lib/redis/RedisOptions.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ interface CommonRedisOptions extends CommanderOptions {
3535
lazyConnect?: boolean;
3636
}
3737

38-
export type RedisOptions =
39-
| (CommonRedisOptions & SentinelConnectionOptions)
40-
| (CommonRedisOptions & StandaloneConnectionOptions);
38+
export type RedisOptions = CommonRedisOptions &
39+
SentinelConnectionOptions &
40+
StandaloneConnectionOptions;
4141

4242
export const DEFAULT_REDIS_OPTIONS: RedisOptions = {
4343
// Connection

0 commit comments

Comments
 (0)