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

"forward port" changes from 4.6.9

This commit is contained in:
Leibale
2023-09-19 19:23:24 -04:00
parent 819ca71538
commit 099f16e45f
16 changed files with 306 additions and 260 deletions

View File

@@ -1,22 +1,26 @@
// This example demonstrates the use of the DUMP and RESTORE commands
import { commandOptions, createClient } from 'redis';
import { createClient, RESP_TYPES } from 'redis';
const client = createClient();
await client.connect();
const client = await createClient({
commandOptions: {
typeMapping: {
[RESP_TYPES.BLOB_STRING]: Buffer
}
}
}).on('error', err => {
console.log('Redis Client Error', err);
}).connect();
// DUMP a specific key into a local variable
const dump = await client.dump(
commandOptions({ returnBuffers: true }),
'source'
);
const dump = await client.dump('source');
// RESTORE into a new key
await client.restore('destination', 0, dump);
// RESTORE and REPLACE an existing key
await client.restore('destination', 0, dump, {
REPLACE: true
REPLACE: true
});
await client.quit();
await client.close();

View File

@@ -1,30 +1,30 @@
import { strict as assert } from 'assert';
import testUtils, { GLOBAL } from '../test-utils';
import { transformArguments } from './CLIENT_NO-TOUCH';
import CLIENT_NO_TOUCH from './CLIENT_NO-TOUCH';
describe('CLIENT NO-TOUCH', () => {
testUtils.isVersionGreaterThanHook([7, 2]);
testUtils.isVersionGreaterThanHook([7, 2]);
describe('transformArguments', () => {
it('true', () => {
assert.deepEqual(
transformArguments(true),
['CLIENT', 'NO-TOUCH', 'ON']
);
});
it('false', () => {
assert.deepEqual(
transformArguments(false),
['CLIENT', 'NO-TOUCH', 'OFF']
);
});
describe('transformArguments', () => {
it('true', () => {
assert.deepEqual(
CLIENT_NO_TOUCH.transformArguments(true),
['CLIENT', 'NO-TOUCH', 'ON']
);
});
testUtils.testWithClient('client.clientNoTouch', async client => {
assert.equal(
await client.clientNoTouch(true),
'OK'
);
}, GLOBAL.SERVERS.OPEN);
it('false', () => {
assert.deepEqual(
CLIENT_NO_TOUCH.transformArguments(false),
['CLIENT', 'NO-TOUCH', 'OFF']
);
});
});
testUtils.testWithClient('client.clientNoTouch', async client => {
assert.equal(
await client.clientNoTouch(true),
'OK'
);
}, GLOBAL.SERVERS.OPEN);
});

View File

@@ -1,11 +1,15 @@
import { RedisCommandArguments } from '.';
import { SimpleStringReply, Command } from '@redis/client/dist/lib/RESP/types';
export function transformArguments(value: boolean): RedisCommandArguments {
export default {
FIRST_KEY_INDEX: undefined,
IS_READ_ONLY: true,
transformArguments(value: boolean) {
return [
'CLIENT',
'NO-TOUCH',
value ? 'ON' : 'OFF'
'CLIENT',
'NO-TOUCH',
value ? 'ON' : 'OFF'
];
}
},
transformReply: undefined as unknown as () => SimpleStringReply<'OK'>
} as const satisfies Command;
export declare function transformReply(): 'OK' | Buffer;

View File

@@ -1,22 +1,22 @@
import { strict as assert } from 'assert';
import testUtils, { GLOBAL } from '../test-utils';
import { transformArguments } from './CLUSTER_MYSHARDID';
import CLUSTER_MYSHARDID from './CLUSTER_MYSHARDID';
describe('CLUSTER MYSHARDID', () => {
testUtils.isVersionGreaterThanHook([7, 2]);
testUtils.isVersionGreaterThanHook([7, 2]);
it('transformArguments', () => {
assert.deepEqual(
transformArguments(),
['CLUSTER', 'MYSHARDID']
);
});
it('transformArguments', () => {
assert.deepEqual(
CLUSTER_MYSHARDID.transformArguments(),
['CLUSTER', 'MYSHARDID']
);
});
testUtils.testWithCluster('clusterNode.clusterMyShardId', async cluster => {
const client = await cluster.nodeClient(cluster.masters[0]);
assert.equal(
typeof await client.clusterMyShardId(),
'string'
);
}, GLOBAL.CLUSTERS.OPEN);
testUtils.testWithCluster('clusterNode.clusterMyShardId', async cluster => {
const client = await cluster.nodeClient(cluster.masters[0]);
assert.equal(
typeof await client.clusterMyShardId(),
'string'
);
}, GLOBAL.CLUSTERS.OPEN);
});

View File

@@ -1,7 +1,11 @@
export const IS_READ_ONLY = true;
import { BlobStringReply, Command } from '@redis/client/dist/lib/RESP/types';
export function transformArguments() {
export default {
FIRST_KEY_INDEX: undefined,
IS_READ_ONLY: true,
transformArguments() {
return ['CLUSTER', 'MYSHARDID'];
}
},
transformReply: undefined as unknown as () => BlobStringReply
} as const satisfies Command;
export declare function transformReply(): string | Buffer;

View File

@@ -15,14 +15,12 @@ describe('LATENCY GRAPH', () => {
});
testUtils.testWithClient('client.latencyGraph', async client => {
await Promise.all([
const [,, reply] = await Promise.all([
client.configSet('latency-monitor-threshold', '1'),
client.sendCommand(['DEBUG', 'SLEEP', '0.001'])
client.sendCommand(['DEBUG', 'SLEEP', '0.001']),
client.latencyGraph('command')
]);
assert.equal(
typeof await client.latencyGraph('command'),
'string'
);
assert.equal(typeof reply, 'string');
}, GLOBAL.SERVERS.OPEN);
});

View File

@@ -1,4 +1,4 @@
import { SimpleStringReply, Command, BlobStringReply } from '../RESP/types';
import { BlobStringReply, Command } from '../RESP/types';
export const LATENCY_EVENTS = {
ACTIVE_DEFRAG_CYCLE: 'active-defrag-cycle',

View File

@@ -1,26 +1,26 @@
import {strict as assert} from 'assert';
import testUtils, {GLOBAL} from '../test-utils';
import { transformArguments } from './LATENCY_HISTORY';
import { strict as assert } from 'assert';
import testUtils, { GLOBAL } from '../test-utils';
import LATENCY_HISTORY from './LATENCY_HISTORY';
describe('LATENCY HISTORY', () => {
it('transformArguments', () => {
assert.deepEqual(
transformArguments('command'),
['LATENCY', 'HISTORY', 'command']
);
});
it('transformArguments', () => {
assert.deepEqual(
LATENCY_HISTORY.transformArguments('command'),
['LATENCY', 'HISTORY', 'command']
);
});
testUtils.testWithClient('client.latencyHistory', async client => {
await Promise.all([
client.configSet('latency-monitor-threshold', '100'),
client.sendCommand(['DEBUG', 'SLEEP', '1'])
]);
testUtils.testWithClient('client.latencyHistory', async client => {
const [,, reply] = await Promise.all([
client.configSet('latency-monitor-threshold', '100'),
client.sendCommand(['DEBUG', 'SLEEP', '1']),
client.latencyHistory('command')
]);
const latencyHisRes = await client.latencyHistory('command');
assert.ok(Array.isArray(latencyHisRes));
for (const [timestamp, latency] of latencyHisRes) {
assert.equal(typeof timestamp, 'number');
assert.equal(typeof latency, 'number');
}
}, GLOBAL.SERVERS.OPEN);
assert.ok(Array.isArray(reply));
for (const [timestamp, latency] of reply) {
assert.equal(typeof timestamp, 'number');
assert.equal(typeof latency, 'number');
}
}, GLOBAL.SERVERS.OPEN);
});

View File

@@ -1,27 +1,33 @@
export type EventType = (
'active-defrag-cycle' |
'aof-fsync-always' |
'aof-stat' |
'aof-rewrite-diff-write' |
'aof-rename' |
'aof-write' |
'aof-write-active-child' |
'aof-write-alone' |
'aof-write-pending-fsync' |
'command' |
'expire-cycle' |
'eviction-cycle' |
'eviction-del' |
'fast-command' |
'fork' |
'rdb-unlink-temp-file'
import { ArrayReply, TuplesReply, NumberReply, Command } from '../RESP/types';
export type LatencyEventType = (
'active-defrag-cycle' |
'aof-fsync-always' |
'aof-stat' |
'aof-rewrite-diff-write' |
'aof-rename' |
'aof-write' |
'aof-write-active-child' |
'aof-write-alone' |
'aof-write-pending-fsync' |
'command' |
'expire-cycle' |
'eviction-cycle' |
'eviction-del' |
'fast-command' |
'fork' |
'rdb-unlink-temp-file'
);
export function transformArguments(event: EventType) {
export default {
FIRST_KEY_INDEX: undefined,
IS_READ_ONLY: true,
transformArguments(event: LatencyEventType) {
return ['LATENCY', 'HISTORY', event];
}
},
transformReply: undefined as unknown as () => ArrayReply<TuplesReply<[
timestamp: NumberReply,
latency: NumberReply
]>>
} as const satisfies Command;
export declare function transformReply(): Array<[
timestamp: number,
latency: number,
]>;

View File

@@ -11,13 +11,13 @@ describe('LATENCY LATEST', () => {
});
testUtils.testWithClient('client.latencyLatest', async client => {
await Promise.all([
const [,, reply] = await Promise.all([
client.configSet('latency-monitor-threshold', '100'),
client.sendCommand(['DEBUG', 'SLEEP', '1'])
client.sendCommand(['DEBUG', 'SLEEP', '1']),
client.latencyLatest()
]);
const latency = await client.latencyLatest();
assert.ok(Array.isArray(latency));
for (const [name, timestamp, latestLatency, allTimeLatency] of latency) {
assert.ok(Array.isArray(reply));
for (const [name, timestamp, latestLatency, allTimeLatency] of reply) {
assert.equal(typeof name, 'string');
assert.equal(typeof timestamp, 'number');
assert.equal(typeof latestLatency, 'number');

View File

@@ -1,48 +1,48 @@
import { strict as assert } from 'assert';
import testUtils, { GLOBAL } from '../test-utils';
import { transformArguments } from './PUBSUB_SHARDNUMSUB';
import PUBSUB_SHARDNUMSUB from './PUBSUB_SHARDNUMSUB';
describe('PUBSUB SHARDNUMSUB', () => {
testUtils.isVersionGreaterThanHook([7]);
testUtils.isVersionGreaterThanHook([7]);
describe('transformArguments', () => {
it('simple', () => {
assert.deepEqual(
transformArguments(),
['PUBSUB', 'SHARDNUMSUB']
);
});
it('string', () => {
assert.deepEqual(
transformArguments('channel'),
['PUBSUB', 'SHARDNUMSUB', 'channel']
);
});
it('array', () => {
assert.deepEqual(
transformArguments(['1', '2']),
['PUBSUB', 'SHARDNUMSUB', '1', '2']
);
});
describe('transformArguments', () => {
it('simple', () => {
assert.deepEqual(
PUBSUB_SHARDNUMSUB.transformArguments(),
['PUBSUB', 'SHARDNUMSUB']
);
});
testUtils.testWithClient('client.pubSubShardNumSub', async client => {
assert.deepEqual(
await client.pubSubShardNumSub(['foo', 'bar']),
Object.create(null, {
foo: {
value: 0,
configurable: true,
enumerable: true
},
bar: {
value: 0,
configurable: true,
enumerable: true
}
})
);
}, GLOBAL.SERVERS.OPEN);
it('string', () => {
assert.deepEqual(
PUBSUB_SHARDNUMSUB.transformArguments('channel'),
['PUBSUB', 'SHARDNUMSUB', 'channel']
);
});
it('array', () => {
assert.deepEqual(
PUBSUB_SHARDNUMSUB.transformArguments(['1', '2']),
['PUBSUB', 'SHARDNUMSUB', '1', '2']
);
});
});
testUtils.testWithClient('client.pubSubShardNumSub', async client => {
assert.deepEqual(
await client.pubSubShardNumSub(['foo', 'bar']),
Object.create(null, {
foo: {
value: 0,
configurable: true,
enumerable: true
},
bar: {
value: 0,
configurable: true,
enumerable: true
}
})
);
}, GLOBAL.SERVERS.OPEN);
});

View File

@@ -1,24 +1,24 @@
import { pushVerdictArguments } from './generic-transformers';
import { RedisCommandArgument, RedisCommandArguments } from '.';
import { ArrayReply, BlobStringReply, NumberReply, UnwrapReply, Command } from '../RESP/types';
import { RedisVariadicArgument, pushVariadicArguments } from './generic-transformers';
export const IS_READ_ONLY = true;
export function transformArguments(
channels?: Array<RedisCommandArgument> | RedisCommandArgument
): RedisCommandArguments {
export default {
FIRST_KEY_INDEX: undefined,
IS_READ_ONLY: true,
transformArguments(channels?: RedisVariadicArgument) {
const args = ['PUBSUB', 'SHARDNUMSUB'];
if (channels) return pushVerdictArguments(args, channels);
if (channels) return pushVariadicArguments(args, channels);
return args;
}
},
transformReply(reply: UnwrapReply<ArrayReply<BlobStringReply | NumberReply>>) {
const transformedReply: Record<string, NumberReply> = Object.create(null);
export function transformReply(rawReply: Array<string | number>): Record<string, number> {
const transformedReply = Object.create(null);
for (let i = 0; i < rawReply.length; i += 2) {
transformedReply[rawReply[i]] = rawReply[i + 1];
for (let i = 0; i < reply.length; i += 2) {
transformedReply[(reply[i] as BlobStringReply).toString()] = reply[i + 1] as NumberReply;
}
return transformedReply;
}
}
} as const satisfies Command;

View File

@@ -1,74 +1,84 @@
import { strict as assert } from 'assert';
import testUtils, { GLOBAL } from '../test-utils';
import { transformArguments } from './RESTORE';
import RESTORE from './RESTORE';
import { RESP_TYPES } from '../RESP/decoder';
describe('RESTORE', () => {
describe('transformArguments', () => {
it('simple', () => {
assert.deepEqual(
transformArguments('key', 0, 'value'),
['RESTORE', 'key', '0', 'value']
);
});
it('with REPLACE', () => {
assert.deepEqual(
transformArguments('key', 0, 'value', {
REPLACE: true
}),
['RESTORE', 'key', '0', 'value', 'REPLACE']
);
});
it('with ABSTTL', () => {
assert.deepEqual(
transformArguments('key', 0, 'value', {
ABSTTL: true
}),
['RESTORE', 'key', '0', 'value', 'ABSTTL']
);
});
it('with IDLETIME', () => {
assert.deepEqual(
transformArguments('key', 0, 'value', {
IDLETIME: 1
}),
['RESTORE', 'key', '0', 'value', 'IDLETIME', '1']
);
});
it('with FREQ', () => {
assert.deepEqual(
transformArguments('key', 0, 'value', {
FREQ: 1
}),
['RESTORE', 'key', '0', 'value', 'FREQ', '1']
);
});
it('with REPLACE, ABSTTL, IDLETIME and FREQ', () => {
assert.deepEqual(
transformArguments('key', 0, 'value', {
REPLACE: true,
ABSTTL: true,
IDLETIME: 1,
FREQ: 2
}),
['RESTORE', 'key', '0', 'value', 'REPLACE', 'ABSTTL', 'IDLETIME', '1', 'FREQ', '2']
);
});
describe('transformArguments', () => {
it('simple', () => {
assert.deepEqual(
RESTORE.transformArguments('key', 0, 'value'),
['RESTORE', 'key', '0', 'value']
);
});
testUtils.testWithClient('client.restore', async client => {
const [, dump] = await Promise.all([
client.set('source', 'value'),
client.dump(client.commandOptions({ returnBuffers: true }), 'source')
]);
it('with REPLACE', () => {
assert.deepEqual(
RESTORE.transformArguments('key', 0, 'value', {
REPLACE: true
}),
['RESTORE', 'key', '0', 'value', 'REPLACE']
);
});
assert.equal(
await client.restore('destination', 0, dump),
'OK'
);
}, GLOBAL.SERVERS.OPEN);
it('with ABSTTL', () => {
assert.deepEqual(
RESTORE.transformArguments('key', 0, 'value', {
ABSTTL: true
}),
['RESTORE', 'key', '0', 'value', 'ABSTTL']
);
});
it('with IDLETIME', () => {
assert.deepEqual(
RESTORE.transformArguments('key', 0, 'value', {
IDLETIME: 1
}),
['RESTORE', 'key', '0', 'value', 'IDLETIME', '1']
);
});
it('with FREQ', () => {
assert.deepEqual(
RESTORE.transformArguments('key', 0, 'value', {
FREQ: 1
}),
['RESTORE', 'key', '0', 'value', 'FREQ', '1']
);
});
it('with REPLACE, ABSTTL, IDLETIME and FREQ', () => {
assert.deepEqual(
RESTORE.transformArguments('key', 0, 'value', {
REPLACE: true,
ABSTTL: true,
IDLETIME: 1,
FREQ: 2
}),
['RESTORE', 'key', '0', 'value', 'REPLACE', 'ABSTTL', 'IDLETIME', '1', 'FREQ', '2']
);
});
});
testUtils.testWithClient('client.restore', async client => {
const [, dump] = await Promise.all([
client.set('source', 'value'),
client.dump('source')
]);
assert.equal(
await client.restore('destination', 0, dump),
'OK'
);
}, {
...GLOBAL.SERVERS.OPEN,
clientOptions: {
commandOptions: {
typeMapping: {
[RESP_TYPES.BLOB_STRING]: Buffer
}
}
}
});
});

View File

@@ -1,39 +1,40 @@
import { RedisCommandArgument, RedisCommandArguments } from '.';
import { RedisArgument, SimpleStringReply, Command } from '../RESP/types';
export const FIRST_KEY_INDEX = 1;
interface RestoreOptions {
REPLACE?: true;
ABSTTL?: true;
IDLETIME?: number;
FREQ?: number;
export interface RestoreOptions {
REPLACE?: boolean;
ABSTTL?: boolean;
IDLETIME?: number;
FREQ?: number;
}
export function transformArguments(
key: RedisCommandArgument,
export default {
FIRST_KEY_INDEX: 1,
IS_READ_ONLY: false,
transformArguments(
key: RedisArgument,
ttl: number,
serializedValue: RedisCommandArgument,
serializedValue: RedisArgument,
options?: RestoreOptions
): RedisCommandArguments {
) {
const args = ['RESTORE', key, ttl.toString(), serializedValue];
if (options?.REPLACE) {
args.push('REPLACE');
args.push('REPLACE');
}
if (options?.ABSTTL) {
args.push('ABSTTL');
args.push('ABSTTL');
}
if (options?.IDLETIME) {
args.push('IDLETIME', options.IDLETIME.toString());
args.push('IDLETIME', options.IDLETIME.toString());
}
if (options?.FREQ) {
args.push('FREQ', options.FREQ.toString());
args.push('FREQ', options.FREQ.toString());
}
return args;
}
export declare function transformReply(): 'OK';
},
transformReply: undefined as unknown as () => SimpleStringReply<'OK'>
} as const satisfies Command;

View File

@@ -38,6 +38,7 @@ import CLIENT_INFO from './CLIENT_INFO';
import CLIENT_KILL from './CLIENT_KILL';
import CLIENT_LIST from './CLIENT_LIST';
import CLIENT_NO_EVICT from './CLIENT_NO-EVICT';
import CLIENT_NO_TOUCH from './CLIENT_NO-TOUCH';
import CLIENT_PAUSE from './CLIENT_PAUSE';
import CLIENT_SETNAME from './CLIENT_SETNAME';
import CLIENT_TRACKING from './CLIENT_TRACKING';
@@ -59,6 +60,7 @@ import CLUSTER_KEYSLOT from './CLUSTER_KEYSLOT';
import CLUSTER_LINKS from './CLUSTER_LINKS';
import CLUSTER_MEET from './CLUSTER_MEET';
import CLUSTER_MYID from './CLUSTER_MYID';
import CLUSTER_MYSHARDID from './CLUSTER_MYSHARDID';
import CLUSTER_NODES from './CLUSTER_NODES';
import CLUSTER_REPLICAS from './CLUSTER_REPLICAS';
import CLUSTER_REPLICATE from './CLUSTER_REPLICATE';
@@ -154,6 +156,7 @@ import KEYS from './KEYS';
import LASTSAVE from './LASTSAVE';
import LATENCY_DOCTOR from './LATENCY_DOCTOR';
import LATENCY_GRAPH from './LATENCY_GRAPH';
import LATENCY_HISTORY from './LATENCY_HISTORY';
import LATENCY_LATEST from './LATENCY_LATEST';
import LCS_IDX_WITHMATCHLEN from './LCS_IDX_WITHMATCHLEN';
import LCS_IDX from './LCS_IDX';
@@ -205,11 +208,15 @@ import PUBLISH from './PUBLISH';
import PUBSUB_CHANNELS from './PUBSUB_CHANNELS';
import PUBSUB_NUMPAT from './PUBSUB_NUMPAT';
import PUBSUB_NUMSUB from './PUBSUB_NUMSUB';
import PUBSUB_SHARDNUMSUB from './PUBSUB_SHARDNUMSUB';
import PUBSUB_SHARDCHANNELS from './PUBSUB_SHARDCHANNELS';
import RANDOMKEY from './RANDOMKEY';
import READONLY from './READONLY';
import RENAME from './RENAME';
import RENAMENX from './RENAMENX';
import REPLICAOF from './REPLICAOF';
import RESTORE_ASKING from './RESTORE-ASKING';
import RESTORE from './RESTORE';
import ROLE from './ROLE';
import RPOP_COUNT from './RPOP_COUNT';
import RPOP from './RPOP';
@@ -405,6 +412,8 @@ export default {
clientList: CLIENT_LIST,
'CLIENT_NO-EVICT': CLIENT_NO_EVICT,
clientNoEvict: CLIENT_NO_EVICT,
'CLIENT_NO-TOUCH': CLIENT_NO_TOUCH,
clientNoTouch: CLIENT_NO_TOUCH,
CLIENT_PAUSE,
clientPause: CLIENT_PAUSE,
CLIENT_SETNAME,
@@ -447,6 +456,8 @@ export default {
clusterMeet: CLUSTER_MEET,
CLUSTER_MYID,
clusterMyId: CLUSTER_MYID,
CLUSTER_MYSHARDID,
clusterMyShardId: CLUSTER_MYSHARDID,
CLUSTER_NODES,
clusterNodes: CLUSTER_NODES,
CLUSTER_REPLICAS,
@@ -637,6 +648,8 @@ export default {
latencyDoctor: LATENCY_DOCTOR,
LATENCY_GRAPH,
latencyGraph: LATENCY_GRAPH,
LATENCY_HISTORY,
latencyHistory: LATENCY_HISTORY,
LATENCY_LATEST,
latencyLatest: LATENCY_LATEST,
LCS_IDX_WITHMATCHLEN,
@@ -741,6 +754,8 @@ export default {
pubSubNumPat: PUBSUB_NUMPAT,
PUBSUB_NUMSUB,
pubSubNumSub: PUBSUB_NUMSUB,
PUBSUB_SHARDNUMSUB,
pubSubShardNumSub: PUBSUB_SHARDNUMSUB,
PUBSUB_SHARDCHANNELS,
pubSubShardChannels: PUBSUB_SHARDCHANNELS,
RANDOMKEY,
@@ -751,6 +766,12 @@ export default {
rename: RENAME,
RENAMENX,
renameNX: RENAMENX,
REPLICAOF,
replicaOf: REPLICAOF,
'RESTORE-ASKING': RESTORE_ASKING,
restoreAsking: RESTORE_ASKING,
RESTORE,
restore: RESTORE,
RPOP_COUNT,
rPopCount: RPOP_COUNT,
ROLE,

View File

@@ -34,11 +34,9 @@ Looking for a high-level library to handle object mapping? See [redis-om-node](h
```javascript
import { createClient } from 'redis';
const client = createClient();
client.on('error', err => console.log('Redis Client Error', err));
await client.connect();
const client = await createClient()
.on('error', err => console.log('Redis Client Error', err))
.connect();
await client.set('key', 'value');
const value = await client.get('key');