You've already forked node-redis
mirror of
https://github.com/redis/node-redis.git
synced 2025-08-06 02:15:48 +03:00
V5 bringing RESP3, Sentinel and TypeMapping to node-redis
RESP3 Support - Some commands responses in RESP3 aren't stable yet and therefore return an "untyped" ReplyUnion. Sentinel TypeMapping Correctly types Multi commands Note: some API changes to be further documented in v4-to-v5.md
This commit is contained in:
@@ -1,46 +1,41 @@
|
||||
import { RedisCommandArguments } from '.';
|
||||
import { TuplesReply, BlobStringReply, NumberReply, ArrayReply, UnwrapReply, Command } from '../RESP/types';
|
||||
|
||||
export function transformArguments(): RedisCommandArguments {
|
||||
return ['CLUSTER', 'SLOTS'];
|
||||
}
|
||||
|
||||
type ClusterSlotsRawNode = [ip: string, port: number, id: string];
|
||||
|
||||
type ClusterSlotsRawReply = Array<[
|
||||
from: number,
|
||||
to: number,
|
||||
master: ClusterSlotsRawNode,
|
||||
...replicas: Array<ClusterSlotsRawNode>
|
||||
type RawNode = TuplesReply<[
|
||||
host: BlobStringReply,
|
||||
port: NumberReply,
|
||||
id: BlobStringReply
|
||||
]>;
|
||||
|
||||
export interface ClusterSlotsNode {
|
||||
ip: string;
|
||||
port: number;
|
||||
id: string;
|
||||
};
|
||||
type ClusterSlotsRawReply = ArrayReply<[
|
||||
from: NumberReply,
|
||||
to: NumberReply,
|
||||
master: RawNode,
|
||||
...replicas: Array<RawNode>
|
||||
]>;
|
||||
|
||||
export type ClusterSlotsReply = Array<{
|
||||
from: number;
|
||||
to: number;
|
||||
master: ClusterSlotsNode;
|
||||
replicas: Array<ClusterSlotsNode>;
|
||||
}>;
|
||||
export type ClusterSlotsNode = ReturnType<typeof transformNode>;
|
||||
|
||||
export function transformReply(reply: ClusterSlotsRawReply): ClusterSlotsReply {
|
||||
return reply.map(([from, to, master, ...replicas]) => {
|
||||
return {
|
||||
from,
|
||||
to,
|
||||
master: transformNode(master),
|
||||
replicas: replicas.map(transformNode)
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
function transformNode([ip, port, id]: ClusterSlotsRawNode): ClusterSlotsNode {
|
||||
return {
|
||||
ip,
|
||||
port,
|
||||
id
|
||||
};
|
||||
export default {
|
||||
FIRST_KEY_INDEX: undefined,
|
||||
IS_READ_ONLY: true,
|
||||
transformArguments() {
|
||||
return ['CLUSTER', 'SLOTS'];
|
||||
},
|
||||
transformReply(reply: UnwrapReply<ClusterSlotsRawReply>) {
|
||||
return reply.map(([from, to, master, ...replicas]) => ({
|
||||
from,
|
||||
to,
|
||||
master: transformNode(master),
|
||||
replicas: replicas.map(transformNode)
|
||||
}));
|
||||
}
|
||||
} as const satisfies Command;
|
||||
|
||||
function transformNode(node: RawNode) {
|
||||
const [host, port, id] = node as unknown as UnwrapReply<typeof node>;
|
||||
return {
|
||||
host,
|
||||
port,
|
||||
id
|
||||
};
|
||||
}
|
||||
|
Reference in New Issue
Block a user