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,57 +1,79 @@
|
||||
import {
|
||||
transformArguments as transformInfoArguments,
|
||||
InfoRawReply,
|
||||
InfoReply,
|
||||
transformReply as transformInfoReply
|
||||
} from './INFO';
|
||||
import { BlobStringReply, Command, NumberReply, SimpleStringReply, TypeMapping } from "@redis/client/lib/RESP/types";
|
||||
import INFO, { InfoRawReply, InfoRawReplyTypes, InfoReply } from "./INFO";
|
||||
import { ReplyUnion } from '@redis/client/lib/RESP/types';
|
||||
|
||||
export { IS_READ_ONLY, FIRST_KEY_INDEX } from './INFO';
|
||||
|
||||
export function transformArguments(key: string): Array<string> {
|
||||
const args = transformInfoArguments(key);
|
||||
args.push('DEBUG');
|
||||
return args;
|
||||
}
|
||||
type chunkType = Array<[
|
||||
'startTimestamp',
|
||||
NumberReply,
|
||||
'endTimestamp',
|
||||
NumberReply,
|
||||
'samples',
|
||||
NumberReply,
|
||||
'size',
|
||||
NumberReply,
|
||||
'bytesPerSample',
|
||||
SimpleStringReply
|
||||
]>;
|
||||
|
||||
type InfoDebugRawReply = [
|
||||
...InfoRawReply,
|
||||
'keySelfName',
|
||||
string,
|
||||
'chunks',
|
||||
Array<[
|
||||
'startTimestamp',
|
||||
number,
|
||||
'endTimestamp',
|
||||
number,
|
||||
'samples',
|
||||
number,
|
||||
'size',
|
||||
number,
|
||||
'bytesPerSample',
|
||||
string
|
||||
]>
|
||||
...InfoRawReply,
|
||||
'keySelfName',
|
||||
BlobStringReply,
|
||||
'Chunks',
|
||||
chunkType
|
||||
];
|
||||
|
||||
interface InfoDebugReply extends InfoReply {
|
||||
keySelfName: string;
|
||||
chunks: Array<{
|
||||
startTimestamp: number;
|
||||
endTimestamp: number;
|
||||
samples: number;
|
||||
size: number;
|
||||
bytesPerSample: string;
|
||||
}>;
|
||||
export type InfoDebugRawReplyType = InfoRawReplyTypes | chunkType
|
||||
|
||||
export interface InfoDebugReply extends InfoReply {
|
||||
keySelfName: BlobStringReply,
|
||||
chunks: Array<{
|
||||
startTimestamp: NumberReply;
|
||||
endTimestamp: NumberReply;
|
||||
samples: NumberReply;
|
||||
size: NumberReply;
|
||||
bytesPerSample: SimpleStringReply;
|
||||
}>;
|
||||
}
|
||||
|
||||
export function transformReply(rawReply: InfoDebugRawReply): InfoDebugReply {
|
||||
const reply = transformInfoReply(rawReply as unknown as InfoRawReply);
|
||||
(reply as InfoDebugReply).keySelfName = rawReply[25];
|
||||
(reply as InfoDebugReply).chunks = rawReply[27].map(chunk => ({
|
||||
startTimestamp: chunk[1],
|
||||
endTimestamp: chunk[3],
|
||||
samples: chunk[5],
|
||||
size: chunk[7],
|
||||
bytesPerSample: chunk[9]
|
||||
}));
|
||||
return reply as InfoDebugReply;
|
||||
}
|
||||
export default {
|
||||
FIRST_KEY_INDEX: INFO.FIRST_KEY_INDEX,
|
||||
IS_READ_ONLY: INFO.IS_READ_ONLY,
|
||||
transformArguments(key: string) {
|
||||
const args = INFO.transformArguments(key);
|
||||
args.push('DEBUG');
|
||||
return args;
|
||||
},
|
||||
transformReply: {
|
||||
2: (reply: InfoDebugRawReply, _, typeMapping?: TypeMapping): InfoDebugReply => {
|
||||
const ret = INFO.transformReply[2](reply as unknown as InfoRawReply, _, typeMapping) as any;
|
||||
|
||||
for (let i=0; i < reply.length; i += 2) {
|
||||
const key = (reply[i] as any).toString();
|
||||
|
||||
switch (key) {
|
||||
case 'keySelfName': {
|
||||
ret[key] = reply[i+1];
|
||||
break;
|
||||
}
|
||||
case 'Chunks': {
|
||||
ret['chunks'] = (reply[i+1] as chunkType).map(
|
||||
chunk => ({
|
||||
startTimestamp: chunk[1],
|
||||
endTimestamp: chunk[3],
|
||||
samples: chunk[5],
|
||||
size: chunk[7],
|
||||
bytesPerSample: chunk[9]
|
||||
})
|
||||
);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
},
|
||||
3: undefined as unknown as () => ReplyUnion
|
||||
},
|
||||
unstableResp3: true
|
||||
} as const satisfies Command;
|
Reference in New Issue
Block a user