You've already forked node-redis
mirror of
https://github.com/redis/node-redis.git
synced 2025-08-04 15:02:09 +03:00
* Parser support with all commands * remove "dist" from all imports for consistency * address most of my review comments * small tweak to multi type mapping handling * tweak multi commands / fix addScript cases * nits * addressed all in person review comments * revert addCommand/addScript changes to multi-commands addCommand needs to be there for sendCommand like ability within a multi. If its there, it might as well be used by createCommand() et al, to avoid repeating code. addScript is there (even though only used once), but now made private to keep the logic for bookkeeping near each other.
88 lines
2.0 KiB
TypeScript
88 lines
2.0 KiB
TypeScript
import { CommandParser } from '../client/parser';
|
|
import { SimpleStringReply, Command } from '../RESP/types';
|
|
import { RedisVariadicArgument } from './generic-transformers';
|
|
|
|
interface CommonOptions {
|
|
REDIRECT?: number;
|
|
NOLOOP?: boolean;
|
|
}
|
|
|
|
interface BroadcastOptions {
|
|
BCAST?: boolean;
|
|
PREFIX?: RedisVariadicArgument;
|
|
}
|
|
|
|
interface OptInOptions {
|
|
OPTIN?: boolean;
|
|
}
|
|
|
|
interface OptOutOptions {
|
|
OPTOUT?: boolean;
|
|
}
|
|
|
|
export type ClientTrackingOptions = CommonOptions & (
|
|
BroadcastOptions |
|
|
OptInOptions |
|
|
OptOutOptions
|
|
);
|
|
|
|
export default {
|
|
NOT_KEYED_COMMAND: true,
|
|
IS_READ_ONLY: true,
|
|
parseCommand<M extends boolean>(
|
|
parser: CommandParser,
|
|
mode: M,
|
|
options?: M extends true ? ClientTrackingOptions : never
|
|
) {
|
|
parser.push(
|
|
'CLIENT',
|
|
'TRACKING',
|
|
mode ? 'ON' : 'OFF'
|
|
);
|
|
|
|
if (mode) {
|
|
if (options?.REDIRECT) {
|
|
parser.push(
|
|
'REDIRECT',
|
|
options.REDIRECT.toString()
|
|
);
|
|
}
|
|
|
|
if (isBroadcast(options)) {
|
|
parser.push('BCAST');
|
|
|
|
if (options?.PREFIX) {
|
|
if (Array.isArray(options.PREFIX)) {
|
|
for (const prefix of options.PREFIX) {
|
|
parser.push('PREFIX', prefix);
|
|
}
|
|
} else {
|
|
parser.push('PREFIX', options.PREFIX);
|
|
}
|
|
}
|
|
} else if (isOptIn(options)) {
|
|
parser.push('OPTIN');
|
|
} else if (isOptOut(options)) {
|
|
parser.push('OPTOUT');
|
|
}
|
|
|
|
if (options?.NOLOOP) {
|
|
parser.push('NOLOOP');
|
|
}
|
|
}
|
|
},
|
|
transformReply: undefined as unknown as () => SimpleStringReply<'OK'>
|
|
} as const satisfies Command;
|
|
|
|
function isBroadcast(options?: ClientTrackingOptions): options is BroadcastOptions {
|
|
return (options as BroadcastOptions)?.BCAST === true;
|
|
}
|
|
|
|
function isOptIn(options?: ClientTrackingOptions): options is OptInOptions {
|
|
return (options as OptInOptions)?.OPTIN === true;
|
|
}
|
|
|
|
function isOptOut(options?: ClientTrackingOptions): options is OptOutOptions {
|
|
return (options as OptOutOptions)?.OPTOUT === true;
|
|
}
|