1
0
mirror of https://github.com/redis/node-redis.git synced 2025-08-04 15:02:09 +03:00

fix: fix various command import issues (#2944)

* fix: fix various command import issues

there was some sort of a circular dependency
in <module>/lib/commands/index.ts for various modules

fixes #2937 fixes #2941

* remove redundant definition
This commit is contained in:
Nikolay Karadzhov
2025-05-07 16:10:35 +03:00
committed by GitHub
parent bc4b2101ee
commit 7b737821b2
42 changed files with 416 additions and 391 deletions

View File

@@ -1,7 +1,7 @@
import { strict as assert } from 'node:assert';
import testUtils, { GLOBAL } from '../test-utils';
import ADD from './ADD';
import { TIME_SERIES_ENCODING, TIME_SERIES_DUPLICATE_POLICIES } from '.';
import { TIME_SERIES_ENCODING, TIME_SERIES_DUPLICATE_POLICIES } from './helpers';
import { parseArgs } from '@redis/client/lib/commands/generic-transformers';
describe('TS.ADD', () => {

View File

@@ -11,7 +11,7 @@ import {
parseLabelsArgument,
Timestamp,
parseIgnoreArgument
} from '.';
} from './helpers';
export interface TsIgnoreOptions {
maxTimeDiff: number;

View File

@@ -1,7 +1,7 @@
import { strict as assert } from 'node:assert';
import testUtils, { GLOBAL } from '../test-utils';
import ALTER from './ALTER';
import { TIME_SERIES_DUPLICATE_POLICIES } from '.';
import { TIME_SERIES_DUPLICATE_POLICIES } from './helpers';
import { parseArgs } from '@redis/client/lib/commands/generic-transformers';
describe('TS.ALTER', () => {

View File

@@ -1,7 +1,8 @@
import { CommandParser } from '@redis/client/dist/lib/client/parser';
import { RedisArgument, SimpleStringReply, Command } from '@redis/client/dist/lib/RESP/types';
import { TsCreateOptions } from './CREATE';
import { parseRetentionArgument, parseChunkSizeArgument, parseDuplicatePolicy, parseLabelsArgument, parseIgnoreArgument } from '.';
import { parseRetentionArgument, parseChunkSizeArgument, parseDuplicatePolicy, parseLabelsArgument, parseIgnoreArgument } from './helpers';
export type TsAlterOptions = Pick<TsCreateOptions, 'RETENTION' | 'CHUNK_SIZE' | 'DUPLICATE_POLICY' | 'LABELS' | 'IGNORE'>;

View File

@@ -1,7 +1,7 @@
import { strict as assert } from 'node:assert';
import testUtils, { GLOBAL } from '../test-utils';
import CREATE from './CREATE';
import { TIME_SERIES_ENCODING, TIME_SERIES_DUPLICATE_POLICIES } from '.';
import { TIME_SERIES_ENCODING, TIME_SERIES_DUPLICATE_POLICIES } from './helpers';
import { parseArgs } from '@redis/client/lib/commands/generic-transformers';
describe('TS.CREATE', () => {

View File

@@ -10,7 +10,7 @@ import {
Labels,
parseLabelsArgument,
parseIgnoreArgument
} from '.';
} from './helpers';
import { TsIgnoreOptions } from './ADD';
export interface TsCreateOptions {

View File

@@ -1,5 +1,5 @@
import { CommandParser } from '@redis/client/dist/lib/client/parser';
import { Timestamp, transformTimestampArgument } from '.';
import { Timestamp, transformTimestampArgument } from './helpers';
import { RedisArgument, NumberReply, Command, } from '@redis/client/dist/lib/RESP/types';
export default {

View File

@@ -1,6 +1,6 @@
import { CommandParser } from '@redis/client/dist/lib/client/parser';
import { RedisArgument, NumberReply, Command } from '@redis/client/dist/lib/RESP/types';
import { Timestamp, transformTimestampArgument, parseRetentionArgument, parseChunkSizeArgument, Labels, parseLabelsArgument, parseIgnoreArgument } from '.';
import { Timestamp, transformTimestampArgument, parseRetentionArgument, parseChunkSizeArgument, Labels, parseLabelsArgument, parseIgnoreArgument } from './helpers';
import { TsIgnoreOptions } from './ADD';
export interface TsIncrByOptions {

View File

@@ -1,5 +1,5 @@
import { strict as assert } from 'node:assert';
import { TIME_SERIES_DUPLICATE_POLICIES } from '.';
import { TIME_SERIES_DUPLICATE_POLICIES } from './helpers';
import testUtils, { GLOBAL } from '../test-utils';
import INFO, { InfoReply } from './INFO';
import { TIME_SERIES_AGGREGATION_TYPE } from './CREATERULE';

View File

@@ -1,6 +1,6 @@
import { CommandParser } from '@redis/client/dist/lib/client/parser';
import { ArrayReply, BlobStringReply, Command, DoubleReply, NumberReply, ReplyUnion, SimpleStringReply, TypeMapping } from "@redis/client/dist/lib/RESP/types";
import { TimeSeriesDuplicatePolicies } from ".";
import { TimeSeriesDuplicatePolicies } from "./helpers";
import { TimeSeriesAggregationType } from "./CREATERULE";
import { transformDoubleReply } from '@redis/client/dist/lib/commands/generic-transformers';

View File

@@ -1,5 +1,5 @@
import { strict as assert } from 'node:assert';
import { TIME_SERIES_DUPLICATE_POLICIES } from '.';
import { TIME_SERIES_DUPLICATE_POLICIES } from './helpers';
import testUtils, { GLOBAL } from '../test-utils';
import { assertInfo } from './INFO.spec';
import INFO_DEBUG from './INFO_DEBUG';

View File

@@ -1,5 +1,5 @@
import { CommandParser } from '@redis/client/dist/lib/client/parser';
import { Timestamp, transformTimestampArgument } from '.';
import { Timestamp, transformTimestampArgument } from './helpers';
import { ArrayReply, NumberReply, SimpleErrorReply, Command } from '@redis/client/dist/lib/RESP/types';
export interface TsMAddSample {

View File

@@ -1,6 +1,6 @@
import { CommandParser } from '@redis/client/dist/lib/client/parser';
import { Command, BlobStringReply, ArrayReply, Resp2Reply, MapReply, TuplesReply, TypeMapping } from '@redis/client/dist/lib/RESP/types';
import { resp2MapToValue, resp3MapToValue, SampleRawReply, transformSampleReply } from '.';
import { resp2MapToValue, resp3MapToValue, SampleRawReply, transformSampleReply } from './helpers';
import { RedisVariadicArgument } from '@redis/client/dist/lib/commands/generic-transformers';
export interface TsMGetOptions {

View File

@@ -2,7 +2,7 @@ import { CommandParser } from '@redis/client/dist/lib/client/parser';
import { Command, BlobStringReply, NullReply } from '@redis/client/dist/lib/RESP/types';
import { RedisVariadicArgument } from '@redis/client/dist/lib/commands/generic-transformers';
import { TsMGetOptions, parseLatestArgument, parseFilterArgument } from './MGET';
import { parseSelectedLabelsArguments } from '.';
import { parseSelectedLabelsArguments } from './helpers';
import { createTransformMGetLabelsReply } from './MGET_WITHLABELS';
export default {

View File

@@ -2,7 +2,7 @@ import { CommandParser } from '@redis/client/dist/lib/client/parser';
import { Command, BlobStringReply, ArrayReply, Resp2Reply, MapReply, TuplesReply, TypeMapping } from '@redis/client/dist/lib/RESP/types';
import { RedisVariadicArgument } from '@redis/client/dist/lib/commands/generic-transformers';
import { TsMGetOptions, parseLatestArgument, parseFilterArgument } from './MGET';
import { RawLabelValue, resp2MapToValue, resp3MapToValue, SampleRawReply, transformRESP2Labels, transformSampleReply } from '.';
import { RawLabelValue, resp2MapToValue, resp3MapToValue, SampleRawReply, transformRESP2Labels, transformSampleReply } from './helpers';
export interface TsMGetWithLabelsOptions extends TsMGetOptions {
SELECTED_LABELS?: RedisVariadicArgument;

View File

@@ -1,7 +1,7 @@
import { CommandParser } from '@redis/client/dist/lib/client/parser';
import { Command, ArrayReply, BlobStringReply, Resp2Reply, MapReply, TuplesReply, TypeMapping, RedisArgument } from '@redis/client/dist/lib/RESP/types';
import { RedisVariadicArgument } from '@redis/client/dist/lib/commands/generic-transformers';
import { resp2MapToValue, resp3MapToValue, SampleRawReply, Timestamp, transformSamplesReply } from '.';
import { resp2MapToValue, resp3MapToValue, SampleRawReply, Timestamp, transformSamplesReply } from './helpers';
import { TsRangeOptions, parseRangeArguments } from './RANGE';
import { parseFilterArgument } from './MGET';

View File

@@ -1,7 +1,7 @@
import { CommandParser } from '@redis/client/dist/lib/client/parser';
import { Command, ArrayReply, BlobStringReply, Resp2Reply, MapReply, TuplesReply, TypeMapping, RedisArgument, TuplesToMapReply, UnwrapReply } from '@redis/client/dist/lib/RESP/types';
import { RedisVariadicArgument } from '@redis/client/dist/lib/commands/generic-transformers';
import { resp2MapToValue, resp3MapToValue, SampleRawReply, Timestamp, transformSamplesReply } from '.';
import { resp2MapToValue, resp3MapToValue, SampleRawReply, Timestamp, transformSamplesReply } from './helpers';
import { TsRangeOptions, parseRangeArguments } from './RANGE';
import { parseFilterArgument } from './MGET';

View File

@@ -1,7 +1,7 @@
import { CommandParser } from '@redis/client/dist/lib/client/parser';
import { Command, ArrayReply, BlobStringReply, Resp2Reply, MapReply, TuplesReply, TypeMapping, NullReply, RedisArgument } from '@redis/client/dist/lib/RESP/types';
import { RedisVariadicArgument } from '@redis/client/dist/lib/commands/generic-transformers';
import { parseSelectedLabelsArguments, resp2MapToValue, resp3MapToValue, SampleRawReply, Timestamp, transformRESP2Labels, transformSamplesReply } from '.';
import { parseSelectedLabelsArguments, resp2MapToValue, resp3MapToValue, SampleRawReply, Timestamp, transformRESP2Labels, transformSamplesReply } from './helpers';
import { TsRangeOptions, parseRangeArguments } from './RANGE';
import { parseFilterArgument } from './MGET';

View File

@@ -1,7 +1,7 @@
import { CommandParser } from '@redis/client/dist/lib/client/parser';
import { Command, ArrayReply, BlobStringReply, MapReply, TuplesReply, RedisArgument, NullReply } from '@redis/client/dist/lib/RESP/types';
import { RedisVariadicArgument } from '@redis/client/dist/lib/commands/generic-transformers';
import { parseSelectedLabelsArguments, resp3MapToValue, SampleRawReply, Timestamp, transformSamplesReply } from '.';
import { parseSelectedLabelsArguments, resp3MapToValue, SampleRawReply, Timestamp, transformSamplesReply } from './helpers';
import { TsRangeOptions, parseRangeArguments } from './RANGE';
import { extractResp3MRangeSources, parseGroupByArguments, TsMRangeGroupBy, TsMRangeGroupByRawMetadataReply3 } from './MRANGE_GROUPBY';
import { parseFilterArgument } from './MGET';

View File

@@ -1,7 +1,7 @@
import { CommandParser } from '@redis/client/dist/lib/client/parser';
import { Command, UnwrapReply, ArrayReply, BlobStringReply, Resp2Reply, MapReply, TuplesReply, TypeMapping, RedisArgument } from '@redis/client/dist/lib/RESP/types';
import { RedisVariadicArgument } from '@redis/client/dist/lib/commands/generic-transformers';
import { resp2MapToValue, resp3MapToValue, SampleRawReply, Timestamp, transformSamplesReply } from '.';
import { resp2MapToValue, resp3MapToValue, SampleRawReply, Timestamp, transformSamplesReply } from './helpers';
import { TsRangeOptions, parseRangeArguments } from './RANGE';
import { parseFilterArgument } from './MGET';

View File

@@ -1,7 +1,7 @@
import { CommandParser } from '@redis/client/dist/lib/client/parser';
import { Command, ArrayReply, BlobStringReply, Resp2Reply, MapReply, TuplesReply, TypeMapping, RedisArgument } from '@redis/client/dist/lib/RESP/types';
import { RedisVariadicArgument } from '@redis/client/dist/lib/commands/generic-transformers';
import { resp2MapToValue, resp3MapToValue, SampleRawReply, Timestamp, transformRESP2LabelsWithSources, transformSamplesReply } from '.';
import { resp2MapToValue, resp3MapToValue, SampleRawReply, Timestamp, transformRESP2LabelsWithSources, transformSamplesReply } from './helpers';
import { TsRangeOptions, parseRangeArguments } from './RANGE';
import { extractResp3MRangeSources, parseGroupByArguments, TsMRangeGroupBy, TsMRangeGroupByRawMetadataReply3 } from './MRANGE_GROUPBY';
import { parseFilterArgument } from './MGET';

View File

@@ -1,6 +1,6 @@
import { CommandParser } from '@redis/client/dist/lib/client/parser';
import { RedisArgument, Command } from '@redis/client/dist/lib/RESP/types';
import { Timestamp, transformTimestampArgument, SamplesRawReply, transformSamplesReply } from '.';
import { Timestamp, transformTimestampArgument, SamplesRawReply, transformSamplesReply } from './helpers';
import { TimeSeriesAggregationType } from './CREATERULE';
import { Resp2Reply } from '@redis/client/dist/lib/RESP/types';

View File

@@ -0,0 +1,306 @@
import { CommandParser } from "@redis/client/dist/lib/client/parser";
import { TsIgnoreOptions } from "./ADD";
import { ArrayReply, BlobStringReply, DoubleReply, MapReply, NullReply, NumberReply, ReplyUnion, Resp2Reply, RespType, TuplesReply, TypeMapping, UnwrapReply } from "@redis/client/dist/lib/RESP/types";
import { RESP_TYPES } from "@redis/client";
import { RedisVariadicArgument } from "@redis/client/dist/lib/commands/generic-transformers";
export function parseIgnoreArgument(parser: CommandParser, ignore?: TsIgnoreOptions) {
if (ignore !== undefined) {
parser.push('IGNORE', ignore.maxTimeDiff.toString(), ignore.maxValDiff.toString());
}
}
export function parseRetentionArgument(parser: CommandParser, retention?: number) {
if (retention !== undefined) {
parser.push('RETENTION', retention.toString());
}
}
export const TIME_SERIES_ENCODING = {
COMPRESSED: 'COMPRESSED',
UNCOMPRESSED: 'UNCOMPRESSED'
} as const;
export type TimeSeriesEncoding = typeof TIME_SERIES_ENCODING[keyof typeof TIME_SERIES_ENCODING];
export function parseEncodingArgument(parser: CommandParser, encoding?: TimeSeriesEncoding) {
if (encoding !== undefined) {
parser.push('ENCODING', encoding);
}
}
export function parseChunkSizeArgument(parser: CommandParser, chunkSize?: number) {
if (chunkSize !== undefined) {
parser.push('CHUNK_SIZE', chunkSize.toString());
}
}
export const TIME_SERIES_DUPLICATE_POLICIES = {
BLOCK: 'BLOCK',
FIRST: 'FIRST',
LAST: 'LAST',
MIN: 'MIN',
MAX: 'MAX',
SUM: 'SUM'
} as const;
export type TimeSeriesDuplicatePolicies = typeof TIME_SERIES_DUPLICATE_POLICIES[keyof typeof TIME_SERIES_DUPLICATE_POLICIES];
export function parseDuplicatePolicy(parser: CommandParser, duplicatePolicy?: TimeSeriesDuplicatePolicies) {
if (duplicatePolicy !== undefined) {
parser.push('DUPLICATE_POLICY', duplicatePolicy);
}
}
export type Timestamp = number | Date | string;
export function transformTimestampArgument(timestamp: Timestamp): string {
if (typeof timestamp === 'string') return timestamp;
return (
typeof timestamp === 'number' ?
timestamp :
timestamp.getTime()
).toString();
}
export type Labels = {
[label: string]: string;
};
export function parseLabelsArgument(parser: CommandParser, labels?: Labels) {
if (labels) {
parser.push('LABELS');
for (const [label, value] of Object.entries(labels)) {
parser.push(label, value);
}
}
}
export type SampleRawReply = TuplesReply<[timestamp: NumberReply, value: DoubleReply]>;
export const transformSampleReply = {
2(reply: Resp2Reply<SampleRawReply>) {
const [ timestamp, value ] = reply as unknown as UnwrapReply<typeof reply>;
return {
timestamp,
value: Number(value) // TODO: use double type mapping instead
};
},
3(reply: SampleRawReply) {
const [ timestamp, value ] = reply as unknown as UnwrapReply<typeof reply>;
return {
timestamp,
value
};
}
};
export type SamplesRawReply = ArrayReply<SampleRawReply>;
export const transformSamplesReply = {
2(reply: Resp2Reply<SamplesRawReply>) {
return (reply as unknown as UnwrapReply<typeof reply>)
.map(sample => transformSampleReply[2](sample));
},
3(reply: SamplesRawReply) {
return (reply as unknown as UnwrapReply<typeof reply>)
.map(sample => transformSampleReply[3](sample));
}
};
// TODO: move to @redis/client?
export function resp2MapToValue<
RAW_VALUE extends TuplesReply<[key: BlobStringReply, ...rest: Array<ReplyUnion>]>,
TRANSFORMED
>(
wrappedReply: ArrayReply<RAW_VALUE>,
parseFunc: (rawValue: UnwrapReply<RAW_VALUE>) => TRANSFORMED,
typeMapping?: TypeMapping
): MapReply<BlobStringReply, TRANSFORMED> {
const reply = wrappedReply as unknown as UnwrapReply<typeof wrappedReply>;
switch (typeMapping?.[RESP_TYPES.MAP]) {
case Map: {
const ret = new Map<string, TRANSFORMED>();
for (const wrappedTuple of reply) {
const tuple = wrappedTuple as unknown as UnwrapReply<typeof wrappedTuple>;
const key = tuple[0] as unknown as UnwrapReply<typeof tuple[0]>;
ret.set(key.toString(), parseFunc(tuple));
}
return ret as never;
}
case Array: {
for (const wrappedTuple of reply) {
const tuple = wrappedTuple as unknown as UnwrapReply<typeof wrappedTuple>;
(tuple[1] as unknown as TRANSFORMED) = parseFunc(tuple);
}
return reply as never;
}
default: {
const ret: Record<string, TRANSFORMED> = Object.create(null);
for (const wrappedTuple of reply) {
const tuple = wrappedTuple as unknown as UnwrapReply<typeof wrappedTuple>;
const key = tuple[0] as unknown as UnwrapReply<typeof tuple[0]>;
ret[key.toString()] = parseFunc(tuple);
}
return ret as never;
}
}
}
export function resp3MapToValue<
RAW_VALUE extends RespType<any, any, any, any>, // TODO: simplify types
TRANSFORMED
>(
wrappedReply: MapReply<BlobStringReply, RAW_VALUE>,
parseFunc: (rawValue: UnwrapReply<RAW_VALUE>) => TRANSFORMED
): MapReply<BlobStringReply, TRANSFORMED> {
const reply = wrappedReply as unknown as UnwrapReply<typeof wrappedReply>;
if (reply instanceof Array) {
for (let i = 1; i < reply.length; i += 2) {
(reply[i] as unknown as TRANSFORMED) = parseFunc(reply[i] as unknown as UnwrapReply<RAW_VALUE>);
}
} else if (reply instanceof Map) {
for (const [key, value] of reply.entries()) {
(reply as unknown as Map<BlobStringReply, TRANSFORMED>).set(
key,
parseFunc(value as unknown as UnwrapReply<typeof value>)
);
}
} else {
for (const [key, value] of Object.entries(reply)) {
(reply[key] as unknown as TRANSFORMED) = parseFunc(value as unknown as UnwrapReply<typeof value>);
}
}
return reply as never;
}
export function parseSelectedLabelsArguments(
parser: CommandParser,
selectedLabels: RedisVariadicArgument
) {
parser.push('SELECTED_LABELS');
parser.pushVariadic(selectedLabels);
}
export type RawLabelValue = BlobStringReply | NullReply;
export type RawLabels<T extends RawLabelValue> = ArrayReply<TuplesReply<[
label: BlobStringReply,
value: T
]>>;
export function transformRESP2Labels<T extends RawLabelValue>(
labels: RawLabels<T>,
typeMapping?: TypeMapping
): MapReply<BlobStringReply, T> {
const unwrappedLabels = labels as unknown as UnwrapReply<typeof labels>;
switch (typeMapping?.[RESP_TYPES.MAP]) {
case Map:
const map = new Map<string, T>();
for (const tuple of unwrappedLabels) {
const [key, value] = tuple as unknown as UnwrapReply<typeof tuple>;
const unwrappedKey = key as unknown as UnwrapReply<typeof key>;
map.set(unwrappedKey.toString(), value);
}
return map as never;
case Array:
return unwrappedLabels.flat() as never;
case Object:
default:
const labelsObject: Record<string, T> = Object.create(null);
for (const tuple of unwrappedLabels) {
const [key, value] = tuple as unknown as UnwrapReply<typeof tuple>;
const unwrappedKey = key as unknown as UnwrapReply<typeof key>;
labelsObject[unwrappedKey.toString()] = value;
}
return labelsObject as never;
}
}
export function transformRESP2LabelsWithSources<T extends RawLabelValue>(
labels: RawLabels<T>,
typeMapping?: TypeMapping
) {
const unwrappedLabels = labels as unknown as UnwrapReply<typeof labels>;
const to = unwrappedLabels.length - 2; // ignore __reducer__ and __source__
let transformedLabels: MapReply<BlobStringReply, T>;
switch (typeMapping?.[RESP_TYPES.MAP]) {
case Map:
const map = new Map<string, T>();
for (let i = 0; i < to; i++) {
const [key, value] = unwrappedLabels[i] as unknown as UnwrapReply<typeof unwrappedLabels[number]>;
const unwrappedKey = key as unknown as UnwrapReply<typeof key>;
map.set(unwrappedKey.toString(), value);
}
transformedLabels = map as never;
break;
case Array:
transformedLabels = unwrappedLabels.slice(0, to).flat() as never;
break;
case Object:
default:
const labelsObject: Record<string, T> = Object.create(null);
for (let i = 0; i < to; i++) {
const [key, value] = unwrappedLabels[i] as unknown as UnwrapReply<typeof unwrappedLabels[number]>;
const unwrappedKey = key as unknown as UnwrapReply<typeof key>;
labelsObject[unwrappedKey.toString()] = value;
}
transformedLabels = labelsObject as never;
break;
}
const sourcesTuple = unwrappedLabels[unwrappedLabels.length - 1];
const unwrappedSourcesTuple = sourcesTuple as unknown as UnwrapReply<typeof sourcesTuple>;
// the __source__ label will never be null
const transformedSources = transformRESP2Sources(unwrappedSourcesTuple[1] as BlobStringReply);
return {
labels: transformedLabels,
sources: transformedSources
};
}
function transformRESP2Sources(sourcesRaw: BlobStringReply) {
// if a label contains "," this function will produce incorrcet results..
// there is not much we can do about it, and we assume most users won't be using "," in their labels..
const unwrappedSources = sourcesRaw as unknown as UnwrapReply<typeof sourcesRaw>;
if (typeof unwrappedSources === 'string') {
return unwrappedSources.split(',');
}
const indexOfComma = unwrappedSources.indexOf(',');
if (indexOfComma === -1) {
return [unwrappedSources];
}
const sourcesArray = [
unwrappedSources.subarray(0, indexOfComma)
];
let previousComma = indexOfComma + 1;
while (true) {
const indexOf = unwrappedSources.indexOf(',', previousComma);
if (indexOf === -1) {
sourcesArray.push(
unwrappedSources.subarray(previousComma)
);
break;
}
const source = unwrappedSources.subarray(
previousComma,
indexOf
);
sourcesArray.push(source);
previousComma = indexOf + 1;
}
return sourcesArray;
}

View File

@@ -24,7 +24,7 @@
// TimeSeriesDuplicatePolicies,
// pushLatestArgument,
// TimeSeriesBucketTimestamp
// } from '.';
// } from './helpers';
// describe('transformTimestampArgument', () => {
// it('number', () => {

View File

@@ -1,5 +1,4 @@
import type { DoubleReply, NumberReply, RedisCommands, TuplesReply, UnwrapReply, Resp2Reply, ArrayReply, BlobStringReply, MapReply, NullReply, TypeMapping, ReplyUnion, RespType } from '@redis/client/dist/lib/RESP/types';
import ADD, { TsIgnoreOptions } from './ADD';
import ADD from './ADD';
import ALTER from './ALTER';
import CREATE from './CREATE';
import CREATERULE from './CREATERULE';
@@ -29,9 +28,9 @@ import MREVRANGE from './MREVRANGE';
import QUERYINDEX from './QUERYINDEX';
import RANGE from './RANGE';
import REVRANGE from './REVRANGE';
import { RedisVariadicArgument } from '@redis/client/dist/lib/commands/generic-transformers';
import { CommandParser } from '@redis/client/dist/lib/client/parser';
import { RESP_TYPES } from '@redis/client/dist/lib/RESP/decoder';
import { RedisCommands } from '@redis/client/dist/lib/RESP/types';
export * from './helpers';
export default {
ADD,
@@ -96,303 +95,3 @@ export default {
revRange: REVRANGE
} as const satisfies RedisCommands;
export function parseIgnoreArgument(parser: CommandParser, ignore?: TsIgnoreOptions) {
if (ignore !== undefined) {
parser.push('IGNORE', ignore.maxTimeDiff.toString(), ignore.maxValDiff.toString());
}
}
export function parseRetentionArgument(parser: CommandParser, retention?: number) {
if (retention !== undefined) {
parser.push('RETENTION', retention.toString());
}
}
export const TIME_SERIES_ENCODING = {
COMPRESSED: 'COMPRESSED',
UNCOMPRESSED: 'UNCOMPRESSED'
} as const;
export type TimeSeriesEncoding = typeof TIME_SERIES_ENCODING[keyof typeof TIME_SERIES_ENCODING];
export function parseEncodingArgument(parser: CommandParser, encoding?: TimeSeriesEncoding) {
if (encoding !== undefined) {
parser.push('ENCODING', encoding);
}
}
export function parseChunkSizeArgument(parser: CommandParser, chunkSize?: number) {
if (chunkSize !== undefined) {
parser.push('CHUNK_SIZE', chunkSize.toString());
}
}
export const TIME_SERIES_DUPLICATE_POLICIES = {
BLOCK: 'BLOCK',
FIRST: 'FIRST',
LAST: 'LAST',
MIN: 'MIN',
MAX: 'MAX',
SUM: 'SUM'
} as const;
export type TimeSeriesDuplicatePolicies = typeof TIME_SERIES_DUPLICATE_POLICIES[keyof typeof TIME_SERIES_DUPLICATE_POLICIES];
export function parseDuplicatePolicy(parser: CommandParser, duplicatePolicy?: TimeSeriesDuplicatePolicies) {
if (duplicatePolicy !== undefined) {
parser.push('DUPLICATE_POLICY', duplicatePolicy);
}
}
export type Timestamp = number | Date | string;
export function transformTimestampArgument(timestamp: Timestamp): string {
if (typeof timestamp === 'string') return timestamp;
return (
typeof timestamp === 'number' ?
timestamp :
timestamp.getTime()
).toString();
}
export type Labels = {
[label: string]: string;
};
export function parseLabelsArgument(parser: CommandParser, labels?: Labels) {
if (labels) {
parser.push('LABELS');
for (const [label, value] of Object.entries(labels)) {
parser.push(label, value);
}
}
}
export type SampleRawReply = TuplesReply<[timestamp: NumberReply, value: DoubleReply]>;
export const transformSampleReply = {
2(reply: Resp2Reply<SampleRawReply>) {
const [ timestamp, value ] = reply as unknown as UnwrapReply<typeof reply>;
return {
timestamp,
value: Number(value) // TODO: use double type mapping instead
};
},
3(reply: SampleRawReply) {
const [ timestamp, value ] = reply as unknown as UnwrapReply<typeof reply>;
return {
timestamp,
value
};
}
};
export type SamplesRawReply = ArrayReply<SampleRawReply>;
export const transformSamplesReply = {
2(reply: Resp2Reply<SamplesRawReply>) {
return (reply as unknown as UnwrapReply<typeof reply>)
.map(sample => transformSampleReply[2](sample));
},
3(reply: SamplesRawReply) {
return (reply as unknown as UnwrapReply<typeof reply>)
.map(sample => transformSampleReply[3](sample));
}
};
// TODO: move to @redis/client?
export function resp2MapToValue<
RAW_VALUE extends TuplesReply<[key: BlobStringReply, ...rest: Array<ReplyUnion>]>,
TRANSFORMED
>(
wrappedReply: ArrayReply<RAW_VALUE>,
parseFunc: (rawValue: UnwrapReply<RAW_VALUE>) => TRANSFORMED,
typeMapping?: TypeMapping
): MapReply<BlobStringReply, TRANSFORMED> {
const reply = wrappedReply as unknown as UnwrapReply<typeof wrappedReply>;
switch (typeMapping?.[RESP_TYPES.MAP]) {
case Map: {
const ret = new Map<string, TRANSFORMED>();
for (const wrappedTuple of reply) {
const tuple = wrappedTuple as unknown as UnwrapReply<typeof wrappedTuple>;
const key = tuple[0] as unknown as UnwrapReply<typeof tuple[0]>;
ret.set(key.toString(), parseFunc(tuple));
}
return ret as never;
}
case Array: {
for (const wrappedTuple of reply) {
const tuple = wrappedTuple as unknown as UnwrapReply<typeof wrappedTuple>;
(tuple[1] as unknown as TRANSFORMED) = parseFunc(tuple);
}
return reply as never;
}
default: {
const ret: Record<string, TRANSFORMED> = Object.create(null);
for (const wrappedTuple of reply) {
const tuple = wrappedTuple as unknown as UnwrapReply<typeof wrappedTuple>;
const key = tuple[0] as unknown as UnwrapReply<typeof tuple[0]>;
ret[key.toString()] = parseFunc(tuple);
}
return ret as never;
}
}
}
export function resp3MapToValue<
RAW_VALUE extends RespType<any, any, any, any>, // TODO: simplify types
TRANSFORMED
>(
wrappedReply: MapReply<BlobStringReply, RAW_VALUE>,
parseFunc: (rawValue: UnwrapReply<RAW_VALUE>) => TRANSFORMED
): MapReply<BlobStringReply, TRANSFORMED> {
const reply = wrappedReply as unknown as UnwrapReply<typeof wrappedReply>;
if (reply instanceof Array) {
for (let i = 1; i < reply.length; i += 2) {
(reply[i] as unknown as TRANSFORMED) = parseFunc(reply[i] as unknown as UnwrapReply<RAW_VALUE>);
}
} else if (reply instanceof Map) {
for (const [key, value] of reply.entries()) {
(reply as unknown as Map<BlobStringReply, TRANSFORMED>).set(
key,
parseFunc(value as unknown as UnwrapReply<typeof value>)
);
}
} else {
for (const [key, value] of Object.entries(reply)) {
(reply[key] as unknown as TRANSFORMED) = parseFunc(value as unknown as UnwrapReply<typeof value>);
}
}
return reply as never;
}
export function parseSelectedLabelsArguments(
parser: CommandParser,
selectedLabels: RedisVariadicArgument
) {
parser.push('SELECTED_LABELS');
parser.pushVariadic(selectedLabels);
}
export type RawLabelValue = BlobStringReply | NullReply;
export type RawLabels<T extends RawLabelValue> = ArrayReply<TuplesReply<[
label: BlobStringReply,
value: T
]>>;
export function transformRESP2Labels<T extends RawLabelValue>(
labels: RawLabels<T>,
typeMapping?: TypeMapping
): MapReply<BlobStringReply, T> {
const unwrappedLabels = labels as unknown as UnwrapReply<typeof labels>;
switch (typeMapping?.[RESP_TYPES.MAP]) {
case Map:
const map = new Map<string, T>();
for (const tuple of unwrappedLabels) {
const [key, value] = tuple as unknown as UnwrapReply<typeof tuple>;
const unwrappedKey = key as unknown as UnwrapReply<typeof key>;
map.set(unwrappedKey.toString(), value);
}
return map as never;
case Array:
return unwrappedLabels.flat() as never;
case Object:
default:
const labelsObject: Record<string, T> = Object.create(null);
for (const tuple of unwrappedLabels) {
const [key, value] = tuple as unknown as UnwrapReply<typeof tuple>;
const unwrappedKey = key as unknown as UnwrapReply<typeof key>;
labelsObject[unwrappedKey.toString()] = value;
}
return labelsObject as never;
}
}
export function transformRESP2LabelsWithSources<T extends RawLabelValue>(
labels: RawLabels<T>,
typeMapping?: TypeMapping
) {
const unwrappedLabels = labels as unknown as UnwrapReply<typeof labels>;
const to = unwrappedLabels.length - 2; // ignore __reducer__ and __source__
let transformedLabels: MapReply<BlobStringReply, T>;
switch (typeMapping?.[RESP_TYPES.MAP]) {
case Map:
const map = new Map<string, T>();
for (let i = 0; i < to; i++) {
const [key, value] = unwrappedLabels[i] as unknown as UnwrapReply<typeof unwrappedLabels[number]>;
const unwrappedKey = key as unknown as UnwrapReply<typeof key>;
map.set(unwrappedKey.toString(), value);
}
transformedLabels = map as never;
break;
case Array:
transformedLabels = unwrappedLabels.slice(0, to).flat() as never;
break;
case Object:
default:
const labelsObject: Record<string, T> = Object.create(null);
for (let i = 0; i < to; i++) {
const [key, value] = unwrappedLabels[i] as unknown as UnwrapReply<typeof unwrappedLabels[number]>;
const unwrappedKey = key as unknown as UnwrapReply<typeof key>;
labelsObject[unwrappedKey.toString()] = value;
}
transformedLabels = labelsObject as never;
break;
}
const sourcesTuple = unwrappedLabels[unwrappedLabels.length - 1];
const unwrappedSourcesTuple = sourcesTuple as unknown as UnwrapReply<typeof sourcesTuple>;
// the __source__ label will never be null
const transformedSources = transformRESP2Sources(unwrappedSourcesTuple[1] as BlobStringReply);
return {
labels: transformedLabels,
sources: transformedSources
};
}
function transformRESP2Sources(sourcesRaw: BlobStringReply) {
// if a label contains "," this function will produce incorrcet results..
// there is not much we can do about it, and we assume most users won't be using "," in their labels..
const unwrappedSources = sourcesRaw as unknown as UnwrapReply<typeof sourcesRaw>;
if (typeof unwrappedSources === 'string') {
return unwrappedSources.split(',');
}
const indexOfComma = unwrappedSources.indexOf(',');
if (indexOfComma === -1) {
return [unwrappedSources];
}
const sourcesArray = [
unwrappedSources.subarray(0, indexOfComma)
];
let previousComma = indexOfComma + 1;
while (true) {
const indexOf = unwrappedSources.indexOf(',', previousComma);
if (indexOf === -1) {
sourcesArray.push(
unwrappedSources.subarray(previousComma)
);
break;
}
const source = unwrappedSources.subarray(
previousComma,
indexOf
);
sourcesArray.push(source);
previousComma = indexOf + 1;
}
return sourcesArray;
}