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,41 +1,42 @@
import { strict as assert } from 'assert';
import { strict as assert } from 'node:assert';
import testUtils, { GLOBAL } from '../../test-utils';
import { transformArguments } from './INCRBY';
import INCRBY from './INCRBY';
describe('CMS INCRBY', () => {
describe('transformArguments', () => {
it('single item', () => {
assert.deepEqual(
transformArguments('key', {
item: 'item',
incrementBy: 1
}),
['CMS.INCRBY', 'key', 'item', '1']
);
});
it('multiple items', () => {
assert.deepEqual(
transformArguments('key', [{
item: 'a',
incrementBy: 1
}, {
item: 'b',
incrementBy: 2
}]),
['CMS.INCRBY', 'key', 'a', '1', 'b', '2']
);
});
describe('CMS.INCRBY', () => {
describe('transformArguments', () => {
it('single item', () => {
assert.deepEqual(
INCRBY.transformArguments('key', {
item: 'item',
incrementBy: 1
}),
['CMS.INCRBY', 'key', 'item', '1']
);
});
testUtils.testWithClient('client.cms.incrBy', async client => {
await client.cms.initByDim('key', 1000, 5);
assert.deepEqual(
await client.cms.incrBy('key', {
item: 'item',
incrementBy: 1
}),
[1]
);
}, GLOBAL.SERVERS.OPEN);
it('multiple items', () => {
assert.deepEqual(
INCRBY.transformArguments('key', [{
item: 'a',
incrementBy: 1
}, {
item: 'b',
incrementBy: 2
}]),
['CMS.INCRBY', 'key', 'a', '1', 'b', '2']
);
});
});
testUtils.testWithClient('client.cms.incrBy', async client => {
const [, reply] = await Promise.all([
client.cms.initByDim('key', 1000, 5),
client.cms.incrBy('key', {
item: 'item',
incrementBy: 1
})
]);
assert.deepEqual(reply, [1]);
}, GLOBAL.SERVERS.OPEN);
});

View File

@@ -1,29 +1,32 @@
export const FIRST_KEY_INDEX = 1;
import { RedisArgument, ArrayReply, NumberReply, Command } from '@redis/client/dist/lib/RESP/types';
interface IncrByItem {
item: string;
incrementBy: number;
export interface BfIncrByItem {
item: RedisArgument;
incrementBy: number;
}
export function transformArguments(
key: string,
items: IncrByItem | Array<IncrByItem>
): Array<string> {
export default {
FIRST_KEY_INDEX: 1,
IS_READ_ONLY: false,
transformArguments(
key: RedisArgument,
items: BfIncrByItem | Array<BfIncrByItem>
) {
const args = ['CMS.INCRBY', key];
if (Array.isArray(items)) {
for (const item of items) {
pushIncrByItem(args, item);
}
for (const item of items) {
pushIncrByItem(args, item);
}
} else {
pushIncrByItem(args, items);
pushIncrByItem(args, items);
}
return args;
}
},
transformReply: undefined as unknown as () => ArrayReply<NumberReply>
} as const satisfies Command;
function pushIncrByItem(args: Array<string>, { item, incrementBy }: IncrByItem): void {
args.push(item, incrementBy.toString());
function pushIncrByItem(args: Array<RedisArgument>, { item, incrementBy }: BfIncrByItem): void {
args.push(item, incrementBy.toString());
}
export declare function transformReply(): Array<number>;

View File

@@ -1,25 +1,28 @@
import { strict as assert } from 'assert';
import { strict as assert } from 'node:assert';
import testUtils, { GLOBAL } from '../../test-utils';
import { transformArguments } from './INFO';
import INFO from './INFO';
describe('CMS INFO', () => {
it('transformArguments', () => {
assert.deepEqual(
transformArguments('key'),
['CMS.INFO', 'key']
);
});
describe('CMS.INFO', () => {
it('transformArguments', () => {
assert.deepEqual(
INFO.transformArguments('key'),
['CMS.INFO', 'key']
);
});
testUtils.testWithClient('client.cms.info', async client => {
await client.cms.initByDim('key', 1000, 5);
testUtils.testWithClient('client.cms.info', async client => {
const width = 1000,
depth = 5,
[, reply] = await Promise.all([
client.cms.initByDim('key', width, depth),
client.cms.info('key')
]);
assert.deepEqual(
await client.cms.info('key'),
{
width: 1000,
depth: 5,
count: 0
}
);
}, GLOBAL.SERVERS.OPEN);
const expected = Object.create(null);
expected['width'] = width;
expected['depth'] = depth;
expected['count'] = 0;
assert.deepEqual(reply, expected);
}, GLOBAL.SERVERS.OPEN);
});

View File

@@ -1,30 +1,28 @@
export const FIRST_KEY_INDEX = 1;
import { RedisArgument, TuplesToMapReply, NumberReply, UnwrapReply, Resp2Reply, Command, SimpleStringReply, TypeMapping } from '@redis/client/dist/lib/RESP/types';
import { transformInfoV2Reply } from '../bloom';
export const IS_READ_ONLY = true;
export type CmsInfoReplyMap = TuplesToMapReply<[
[SimpleStringReply<'width'>, NumberReply],
[SimpleStringReply<'depth'>, NumberReply],
[SimpleStringReply<'count'>, NumberReply]
]>;
export function transformArguments(key: string): Array<string> {
export interface CmsInfoReply {
width: NumberReply;
depth: NumberReply;
count: NumberReply;
}
export default {
FIRST_KEY_INDEX: 1,
IS_READ_ONLY: true,
transformArguments(key: RedisArgument) {
return ['CMS.INFO', key];
}
export type InfoRawReply = [
_: string,
width: number,
_: string,
depth: number,
_: string,
count: number
];
export interface InfoReply {
width: number;
depth: number;
count: number;
}
export function transformReply(reply: InfoRawReply): InfoReply {
return {
width: reply[1],
depth: reply[3],
count: reply[5]
};
}
},
transformReply: {
2: (reply: UnwrapReply<Resp2Reply<CmsInfoReplyMap>>, _, typeMapping?: TypeMapping): CmsInfoReply => {
return transformInfoV2Reply<CmsInfoReply>(reply, typeMapping);
},
3: undefined as unknown as () => CmsInfoReply
}
} as const satisfies Command;

View File

@@ -1,19 +1,19 @@
import { strict as assert } from 'assert';
import { strict as assert } from 'node:assert';
import testUtils, { GLOBAL } from '../../test-utils';
import { transformArguments } from './INITBYDIM';
import INITBYDIM from './INITBYDIM';
describe('CMS INITBYDIM', () => {
it('transformArguments', () => {
assert.deepEqual(
transformArguments('key', 1000, 5),
['CMS.INITBYDIM', 'key', '1000', '5']
);
});
describe('CMS.INITBYDIM', () => {
it('transformArguments', () => {
assert.deepEqual(
INITBYDIM.transformArguments('key', 1000, 5),
['CMS.INITBYDIM', 'key', '1000', '5']
);
});
testUtils.testWithClient('client.cms.initByDim', async client => {
assert.equal(
await client.cms.initByDim('key', 1000, 5),
'OK'
);
}, GLOBAL.SERVERS.OPEN);
testUtils.testWithClient('client.cms.initByDim', async client => {
assert.equal(
await client.cms.initByDim('key', 1000, 5),
'OK'
);
}, GLOBAL.SERVERS.OPEN);
});

View File

@@ -1,7 +1,10 @@
export const FIRST_KEY_INDEX = 1;
import { RedisArgument, SimpleStringReply, Command } from '@redis/client/dist/lib/RESP/types';
export function transformArguments(key: string, width: number, depth: number): Array<string> {
export default {
FIRST_KEY_INDEX: 1,
IS_READ_ONLY: false,
transformArguments(key: RedisArgument, width: number, depth: number) {
return ['CMS.INITBYDIM', key, width.toString(), depth.toString()];
}
export declare function transformReply(): 'OK';
},
transformReply: undefined as unknown as () => SimpleStringReply<'OK'>
} as const satisfies Command;

View File

@@ -1,19 +1,19 @@
import { strict as assert } from 'assert';
import { strict as assert } from 'node:assert';
import testUtils, { GLOBAL } from '../../test-utils';
import { transformArguments } from './INITBYPROB';
import INITBYPROB from './INITBYPROB';
describe('CMS INITBYPROB', () => {
it('transformArguments', () => {
assert.deepEqual(
transformArguments('key', 0.001, 0.01),
['CMS.INITBYPROB', 'key', '0.001', '0.01']
);
});
describe('CMS.INITBYPROB', () => {
it('transformArguments', () => {
assert.deepEqual(
INITBYPROB.transformArguments('key', 0.001, 0.01),
['CMS.INITBYPROB', 'key', '0.001', '0.01']
);
});
testUtils.testWithClient('client.cms.initByProb', async client => {
assert.equal(
await client.cms.initByProb('key', 0.001, 0.01),
'OK'
);
}, GLOBAL.SERVERS.OPEN);
testUtils.testWithClient('client.cms.initByProb', async client => {
assert.equal(
await client.cms.initByProb('key', 0.001, 0.01),
'OK'
);
}, GLOBAL.SERVERS.OPEN);
});

View File

@@ -1,7 +1,10 @@
export const FIRST_KEY_INDEX = 1;
import { RedisArgument, SimpleStringReply, Command } from '@redis/client/dist/lib/RESP/types';
export function transformArguments(key: string, error: number, probability: number): Array<string> {
export default {
FIRST_KEY_INDEX: 1,
IS_READ_ONLY: false,
transformArguments(key: RedisArgument, error: number, probability: number) {
return ['CMS.INITBYPROB', key, error.toString(), probability.toString()];
}
export declare function transformReply(): 'OK';
},
transformReply: undefined as unknown as () => SimpleStringReply<'OK'>
} as const satisfies Command;

View File

@@ -1,36 +1,34 @@
import { strict as assert } from 'assert';
import { strict as assert } from 'node:assert';
import testUtils, { GLOBAL } from '../../test-utils';
import { transformArguments } from './MERGE';
import MERGE from './MERGE';
describe('CMS MERGE', () => {
describe('transformArguments', () => {
it('without WEIGHTS', () => {
assert.deepEqual(
transformArguments('dest', ['src']),
['CMS.MERGE', 'dest', '1', 'src']
);
});
it('with WEIGHTS', () => {
assert.deepEqual(
transformArguments('dest', [{
name: 'src',
weight: 1
}]),
['CMS.MERGE', 'dest', '1', 'src', 'WEIGHTS', '1']
);
});
describe('CMS.MERGE', () => {
describe('transformArguments', () => {
it('without WEIGHTS', () => {
assert.deepEqual(
MERGE.transformArguments('destination', ['source']),
['CMS.MERGE', 'destination', '1', 'source']
);
});
testUtils.testWithClient('client.cms.merge', async client => {
await Promise.all([
client.cms.initByDim('src', 1000, 5),
client.cms.initByDim('dest', 1000, 5),
]);
it('with WEIGHTS', () => {
assert.deepEqual(
MERGE.transformArguments('destination', [{
name: 'source',
weight: 1
}]),
['CMS.MERGE', 'destination', '1', 'source', 'WEIGHTS', '1']
);
});
});
assert.equal(
await client.cms.merge('dest', ['src']),
'OK'
);
}, GLOBAL.SERVERS.OPEN);
testUtils.testWithClient('client.cms.merge', async client => {
const [, , reply] = await Promise.all([
client.cms.initByDim('source', 1000, 5),
client.cms.initByDim('destination', 1000, 5),
client.cms.merge('destination', ['source'])
]);
assert.equal(reply, 'OK');
}, GLOBAL.SERVERS.OPEN);
});

View File

@@ -1,37 +1,37 @@
export const FIRST_KEY_INDEX = 1;
import { RedisArgument, SimpleStringReply, Command } from '@redis/client/dist/lib/RESP/types';
interface Sketch {
name: string;
weight: number;
interface BfMergeSketch {
name: RedisArgument;
weight: number;
}
type Sketches = Array<string> | Array<Sketch>;
export type BfMergeSketches = Array<RedisArgument> | Array<BfMergeSketch>;
export function transformArguments(dest: string, src: Sketches): Array<string> {
const args = [
'CMS.MERGE',
dest,
src.length.toString()
];
export default {
FIRST_KEY_INDEX: 1,
IS_READ_ONLY: false,
transformArguments(
destination: RedisArgument,
source: BfMergeSketches
) {
let args = ['CMS.MERGE', destination, source.length.toString()];
if (isStringSketches(src)) {
args.push(...src);
if (isPlainSketches(source)) {
args = args.concat(source);
} else {
for (const sketch of src) {
args.push(sketch.name);
}
args.push('WEIGHTS');
for (const sketch of src) {
args.push(sketch.weight.toString());
}
const { length } = args;
args[length + source.length] = 'WEIGHTS';
for (let i = 0; i < source.length; i++) {
args[length + i] = source[i].name;
args[length + source.length + i + 1] = source[i].weight.toString();
}
}
return args;
}
},
transformReply: undefined as unknown as () => SimpleStringReply<'OK'>
} as const satisfies Command;
function isStringSketches(src: Sketches): src is Array<string> {
return typeof src[0] === 'string';
function isPlainSketches(src: BfMergeSketches): src is Array<RedisArgument> {
return typeof src[0] === 'string' || src[0] instanceof Buffer;
}
export declare function transformReply(): 'OK';

View File

@@ -1,22 +1,21 @@
import { strict as assert } from 'assert';
import { strict as assert } from 'node:assert';
import testUtils, { GLOBAL } from '../../test-utils';
import { transformArguments } from './QUERY';
import QUERY from './QUERY';
describe('CMS QUERY', () => {
it('transformArguments', () => {
assert.deepEqual(
transformArguments('key', 'item'),
['CMS.QUERY', 'key', 'item']
);
});
describe('CMS.QUERY', () => {
it('transformArguments', () => {
assert.deepEqual(
QUERY.transformArguments('key', 'item'),
['CMS.QUERY', 'key', 'item']
);
});
testUtils.testWithClient('client.cms.query', async client => {
await client.cms.initByDim('key', 1000, 5);
testUtils.testWithClient('client.cms.query', async client => {
const [, reply] = await Promise.all([
client.cms.initByDim('key', 1000, 5),
client.cms.query('key', 'item')
]);
assert.deepEqual(
await client.cms.query('key', 'item'),
[0]
);
}, GLOBAL.SERVERS.OPEN);
assert.deepEqual(reply, [0]);
}, GLOBAL.SERVERS.OPEN);
});

View File

@@ -1,15 +1,11 @@
import { RedisCommandArguments } from '@redis/client/dist/lib/commands';
import { pushVerdictArguments } from '@redis/client/dist/lib/commands/generic-transformers';
import { ArrayReply, NumberReply, Command, RedisArgument } from '@redis/client/dist/lib/RESP/types';
import { RedisVariadicArgument, pushVariadicArguments } from '@redis/client/dist/lib/commands/generic-transformers';
export const FIRST_KEY_INDEX = 1;
export const IS_READ_ONLY = true;
export function transformArguments(
key: string,
items: string | Array<string>
): RedisCommandArguments {
return pushVerdictArguments(['CMS.QUERY', key], items);
}
export declare function transformReply(): Array<number>;
export default {
FIRST_KEY_INDEX: 1,
IS_READ_ONLY: true,
transformArguments(key: RedisArgument, items: RedisVariadicArgument) {
return pushVariadicArguments(['CMS.QUERY', key], items);
},
transformReply: undefined as unknown as () => ArrayReply<NumberReply>
} as const satisfies Command;

View File

@@ -1,21 +1,22 @@
import * as INCRBY from './INCRBY';
import * as INFO from './INFO';
import * as INITBYDIM from './INITBYDIM';
import * as INITBYPROB from './INITBYPROB';
import * as MERGE from './MERGE';
import * as QUERY from './QUERY';
import type { RedisCommands } from '@redis/client/dist/lib/RESP/types';
import INCRBY from './INCRBY';
import INFO from './INFO';
import INITBYDIM from './INITBYDIM';
import INITBYPROB from './INITBYPROB';
import MERGE from './MERGE';
import QUERY from './QUERY';
export default {
INCRBY,
incrBy: INCRBY,
INFO,
info: INFO,
INITBYDIM,
initByDim: INITBYDIM,
INITBYPROB,
initByProb: INITBYPROB,
MERGE,
merge: MERGE,
QUERY,
query: QUERY
};
INCRBY,
incrBy: INCRBY,
INFO,
info: INFO,
INITBYDIM,
initByDim: INITBYDIM,
INITBYPROB,
initByProb: INITBYPROB,
MERGE,
merge: MERGE,
QUERY,
query: QUERY
} as const satisfies RedisCommands;