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:
114
packages/client/lib/sentinel/utils.ts
Normal file
114
packages/client/lib/sentinel/utils.ts
Normal file
@@ -0,0 +1,114 @@
|
||||
import { ArrayReply, Command, RedisFunction, RedisScript, RespVersions, UnwrapReply } from '../RESP/types';
|
||||
import { RedisSocketOptions, RedisTcpSocketOptions } from '../client/socket';
|
||||
import { functionArgumentsPrefix, getTransformReply, scriptArgumentsPrefix } from '../commander';
|
||||
import { NamespaceProxySentinel, NamespaceProxySentinelClient, ProxySentinel, ProxySentinelClient, RedisNode } from './types';
|
||||
|
||||
/* TODO: should use map interface, would need a transform reply probably? as resp2 is list form, which this depends on */
|
||||
export function parseNode(node: Record<string, string>): RedisNode | undefined{
|
||||
|
||||
if (node.flags.includes("s_down") || node.flags.includes("disconnected") || node.flags.includes("failover_in_progress")) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
return { host: node.ip, port: Number(node.port) };
|
||||
}
|
||||
|
||||
export function createNodeList(nodes: UnwrapReply<ArrayReply<Record<string, string>>>) {
|
||||
var nodeList: Array<RedisNode> = [];
|
||||
|
||||
for (const nodeData of nodes) {
|
||||
const node = parseNode(nodeData)
|
||||
if (node === undefined) {
|
||||
continue;
|
||||
}
|
||||
nodeList.push(node);
|
||||
}
|
||||
|
||||
return nodeList;
|
||||
}
|
||||
|
||||
export function clientSocketToNode(socket: RedisSocketOptions): RedisNode {
|
||||
const s = socket as RedisTcpSocketOptions;
|
||||
|
||||
return {
|
||||
host: s.host!,
|
||||
port: s.port!
|
||||
}
|
||||
}
|
||||
|
||||
export function createCommand<T extends ProxySentinel | ProxySentinelClient>(command: Command, resp: RespVersions) {
|
||||
const transformReply = getTransformReply(command, resp);
|
||||
return async function (this: T, ...args: Array<unknown>) {
|
||||
const redisArgs = command.transformArguments(...args);
|
||||
const typeMapping = this._self.commandOptions?.typeMapping;
|
||||
|
||||
const reply = await this._self.sendCommand(
|
||||
command.IS_READ_ONLY,
|
||||
redisArgs,
|
||||
this._self.commandOptions
|
||||
);
|
||||
|
||||
return transformReply ?
|
||||
transformReply(reply, redisArgs.preserve, typeMapping) :
|
||||
reply;
|
||||
};
|
||||
}
|
||||
|
||||
export function createFunctionCommand<T extends NamespaceProxySentinel | NamespaceProxySentinelClient>(name: string, fn: RedisFunction, resp: RespVersions) {
|
||||
const prefix = functionArgumentsPrefix(name, fn),
|
||||
transformReply = getTransformReply(fn, resp);
|
||||
return async function (this: T, ...args: Array<unknown>) {
|
||||
const fnArgs = fn.transformArguments(...args);
|
||||
const redisArgs = prefix.concat(fnArgs);
|
||||
const typeMapping = this._self._self.commandOptions?.typeMapping;
|
||||
|
||||
const reply = await this._self._self.sendCommand(
|
||||
fn.IS_READ_ONLY,
|
||||
redisArgs,
|
||||
this._self._self.commandOptions
|
||||
);
|
||||
|
||||
return transformReply ?
|
||||
transformReply(reply, fnArgs.preserve, typeMapping) :
|
||||
reply;
|
||||
}
|
||||
};
|
||||
|
||||
export function createModuleCommand<T extends NamespaceProxySentinel | NamespaceProxySentinelClient>(command: Command, resp: RespVersions) {
|
||||
const transformReply = getTransformReply(command, resp);
|
||||
return async function (this: T, ...args: Array<unknown>) {
|
||||
const redisArgs = command.transformArguments(...args);
|
||||
const typeMapping = this._self._self.commandOptions?.typeMapping;
|
||||
|
||||
const reply = await this._self._self.sendCommand(
|
||||
command.IS_READ_ONLY,
|
||||
redisArgs,
|
||||
this._self._self.commandOptions
|
||||
);
|
||||
|
||||
return transformReply ?
|
||||
transformReply(reply, redisArgs.preserve, typeMapping) :
|
||||
reply;
|
||||
}
|
||||
};
|
||||
|
||||
export function createScriptCommand<T extends ProxySentinel | ProxySentinelClient>(script: RedisScript, resp: RespVersions) {
|
||||
const prefix = scriptArgumentsPrefix(script),
|
||||
transformReply = getTransformReply(script, resp);
|
||||
return async function (this: T, ...args: Array<unknown>) {
|
||||
const scriptArgs = script.transformArguments(...args);
|
||||
const redisArgs = prefix.concat(scriptArgs);
|
||||
const typeMapping = this._self.commandOptions?.typeMapping;
|
||||
|
||||
const reply = await this._self.executeScript(
|
||||
script,
|
||||
script.IS_READ_ONLY,
|
||||
redisArgs,
|
||||
this._self.commandOptions
|
||||
);
|
||||
|
||||
return transformReply ?
|
||||
transformReply(reply, scriptArgs.preserve, typeMapping) :
|
||||
reply;
|
||||
};
|
||||
}
|
Reference in New Issue
Block a user