1
0
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:
Shaya Potter
2024-10-15 17:46:52 +03:00
committed by GitHub
parent 2fc79bdfb3
commit b2d35c5286
1174 changed files with 45931 additions and 36274 deletions

View File

@@ -1,56 +1,70 @@
import { RedisCommandArguments } from '.';
import { Command, TuplesToMapReply, BlobStringReply, NullReply, NumberReply, MapReply, Resp2Reply, UnwrapReply } from '../RESP/types';
import { isNullReply } from './generic-transformers';
export function transformArguments(): RedisCommandArguments {
type RunningScript = NullReply | TuplesToMapReply<[
[BlobStringReply<'name'>, BlobStringReply],
[BlobStringReply<'command'>, BlobStringReply],
[BlobStringReply<'duration_ms'>, NumberReply]
]>;
type Engine = TuplesToMapReply<[
[BlobStringReply<'libraries_count'>, NumberReply],
[BlobStringReply<'functions_count'>, NumberReply]
]>;
type Engines = MapReply<BlobStringReply, Engine>;
type FunctionStatsReply = TuplesToMapReply<[
[BlobStringReply<'running_script'>, RunningScript],
[BlobStringReply<'engines'>, Engines]
]>;
export default {
IS_READ_ONLY: true,
FIRST_KEY_INDEX: undefined,
transformArguments() {
return ['FUNCTION', 'STATS'];
},
transformReply: {
2: (reply: UnwrapReply<Resp2Reply<FunctionStatsReply>>) => {
return {
running_script: transformRunningScript(reply[1]),
engines: transformEngines(reply[3])
};
},
3: undefined as unknown as () => FunctionStatsReply
}
} as const satisfies Command;
function transformRunningScript(reply: Resp2Reply<RunningScript>) {
if (isNullReply(reply)) {
return null;
}
const unwraped = reply as unknown as UnwrapReply<typeof reply>;
return {
name: unwraped[1],
command: unwraped[3],
duration_ms: unwraped[5]
};
}
type FunctionStatsRawReply = [
'running_script',
null | [
'name',
string,
'command',
string,
'duration_ms',
number
],
'engines',
Array<any> // "flat tuples" (there is no way to type that)
// ...[string, [
// 'libraries_count',
// number,
// 'functions_count',
// number
// ]]
];
function transformEngines(reply: Resp2Reply<Engines>) {
const unwraped = reply as unknown as UnwrapReply<typeof reply>;
interface FunctionStatsReply {
runningScript: null | {
name: string;
command: string;
durationMs: number;
const engines: Record<string, {
libraries_count: NumberReply;
functions_count: NumberReply;
}> = Object.create(null);
for (let i = 0; i < unwraped.length; i++) {
const name = unwraped[i] as BlobStringReply,
stats = unwraped[++i] as Resp2Reply<Engine>,
unwrapedStats = stats as unknown as UnwrapReply<typeof stats>;
engines[name.toString()] = {
libraries_count: unwrapedStats[1],
functions_count: unwrapedStats[3]
};
engines: Record<string, {
librariesCount: number;
functionsCount: number;
}>;
}
}
export function transformReply(reply: FunctionStatsRawReply): FunctionStatsReply {
const engines = Object.create(null);
for (let i = 0; i < reply[3].length; i++) {
engines[reply[3][i]] = {
librariesCount: reply[3][++i][1],
functionsCount: reply[3][i][3]
};
}
return {
runningScript: reply[1] === null ? null : {
name: reply[1][1],
command: reply[1][3],
durationMs: reply[1][5]
},
engines
};
return engines;
}