You've already forked node-redis
mirror of
https://github.com/redis/node-redis.git
synced 2025-08-04 15:02:09 +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,75 +1,70 @@
|
||||
export const IS_READ_ONLY = true;
|
||||
import { BlobStringReply, NumberReply, ArrayReply, TuplesReply, UnwrapReply, Command } from '../RESP/types';
|
||||
|
||||
export function transformArguments(): Array<string> {
|
||||
type MasterRole = [
|
||||
role: BlobStringReply<'master'>,
|
||||
replicationOffest: NumberReply,
|
||||
replicas: ArrayReply<TuplesReply<[host: BlobStringReply, port: BlobStringReply, replicationOffest: BlobStringReply]>>
|
||||
];
|
||||
|
||||
type SlaveRole = [
|
||||
role: BlobStringReply<'slave'>,
|
||||
masterHost: BlobStringReply,
|
||||
masterPort: NumberReply,
|
||||
state: BlobStringReply<'connect' | 'connecting' | 'sync' | 'connected'>,
|
||||
dataReceived: NumberReply
|
||||
];
|
||||
|
||||
type SentinelRole = [
|
||||
role: BlobStringReply<'sentinel'>,
|
||||
masterNames: ArrayReply<BlobStringReply>
|
||||
];
|
||||
|
||||
type Role = TuplesReply<MasterRole | SlaveRole | SentinelRole>;
|
||||
|
||||
export default {
|
||||
FIRST_KEY_INDEX: undefined,
|
||||
IS_READ_ONLY: true,
|
||||
transformArguments() {
|
||||
return ['ROLE'];
|
||||
}
|
||||
|
||||
interface RoleReplyInterface<T extends string> {
|
||||
role: T;
|
||||
}
|
||||
|
||||
type RoleMasterRawReply = ['master', number, Array<[string, string, string]>];
|
||||
|
||||
interface RoleMasterReply extends RoleReplyInterface<'master'> {
|
||||
replicationOffest: number;
|
||||
replicas: Array<{
|
||||
ip: string;
|
||||
port: number;
|
||||
replicationOffest: number;
|
||||
}>;
|
||||
}
|
||||
|
||||
type RoleReplicaState = 'connect' | 'connecting' | 'sync' | 'connected';
|
||||
|
||||
type RoleReplicaRawReply = ['slave', string, number, RoleReplicaState, number];
|
||||
|
||||
interface RoleReplicaReply extends RoleReplyInterface<'slave'> {
|
||||
master: {
|
||||
ip: string;
|
||||
port: number;
|
||||
};
|
||||
state: RoleReplicaState;
|
||||
dataReceived: number;
|
||||
}
|
||||
|
||||
type RoleSentinelRawReply = ['sentinel', Array<string>];
|
||||
|
||||
interface RoleSentinelReply extends RoleReplyInterface<'sentinel'> {
|
||||
masterNames: Array<string>;
|
||||
}
|
||||
|
||||
type RoleRawReply = RoleMasterRawReply | RoleReplicaRawReply | RoleSentinelRawReply;
|
||||
|
||||
type RoleReply = RoleMasterReply | RoleReplicaReply | RoleSentinelReply;
|
||||
|
||||
export function transformReply(reply: RoleRawReply): RoleReply {
|
||||
switch (reply[0]) {
|
||||
case 'master':
|
||||
},
|
||||
transformReply(reply: UnwrapReply<Role>) {
|
||||
switch (reply[0] as unknown as UnwrapReply<typeof reply[0]>) {
|
||||
case 'master': {
|
||||
const [role, replicationOffest, replicas] = reply as MasterRole;
|
||||
return {
|
||||
role,
|
||||
replicationOffest,
|
||||
replicas: (replicas as unknown as UnwrapReply<typeof replicas>).map(replica => {
|
||||
const [host, port, replicationOffest] = replica as unknown as UnwrapReply<typeof replica>;
|
||||
return {
|
||||
role: 'master',
|
||||
replicationOffest: reply[1],
|
||||
replicas: reply[2].map(([ip, port, replicationOffest]) => ({
|
||||
ip,
|
||||
port: Number(port),
|
||||
replicationOffest: Number(replicationOffest)
|
||||
}))
|
||||
host,
|
||||
port: Number(port),
|
||||
replicationOffest: Number(replicationOffest)
|
||||
};
|
||||
})
|
||||
};
|
||||
}
|
||||
|
||||
case 'slave':
|
||||
return {
|
||||
role: 'slave',
|
||||
master: {
|
||||
ip: reply[1],
|
||||
port: reply[2]
|
||||
},
|
||||
state: reply[3],
|
||||
dataReceived: reply[4]
|
||||
};
|
||||
case 'slave': {
|
||||
const [role, masterHost, masterPort, state, dataReceived] = reply as SlaveRole;
|
||||
return {
|
||||
role,
|
||||
master: {
|
||||
host: masterHost,
|
||||
port: masterPort
|
||||
},
|
||||
state,
|
||||
dataReceived,
|
||||
};
|
||||
}
|
||||
|
||||
case 'sentinel':
|
||||
return {
|
||||
role: 'sentinel',
|
||||
masterNames: reply[1]
|
||||
};
|
||||
case 'sentinel': {
|
||||
const [role, masterNames] = reply as SentinelRole;
|
||||
return {
|
||||
role,
|
||||
masterNames
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} as const satisfies Command;
|
||||
|
Reference in New Issue
Block a user