You've already forked node-redis
mirror of
https://github.com/redis/node-redis.git
synced 2025-08-13 10:02:24 +03:00
replace callbackify
with legacyMode
This commit is contained in:
2
TODO.md
2
TODO.md
@@ -5,6 +5,8 @@
|
|||||||
* [`return_buffers`](https://github.com/NodeRedis/node-redis#options-object-properties) (? supported in v3, but have performance drawbacks)
|
* [`return_buffers`](https://github.com/NodeRedis/node-redis#options-object-properties) (? supported in v3, but have performance drawbacks)
|
||||||
* ~~Support options in a command function (`.get`, `.set`, ...)~~
|
* ~~Support options in a command function (`.get`, `.set`, ...)~~
|
||||||
* Key prefixing (?) (partially supported in v3)
|
* Key prefixing (?) (partially supported in v3)
|
||||||
|
* Support for RESP3
|
||||||
|
* client-side caching
|
||||||
|
|
||||||
## Client
|
## Client
|
||||||
* ~~Blocking Commands~~
|
* ~~Blocking Commands~~
|
||||||
|
@@ -33,20 +33,18 @@ describe('Client', () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('callbackify', () => {
|
describe('legacyMode', () => {
|
||||||
const client = RedisClient.create({
|
const client = RedisClient.create({
|
||||||
socket: TEST_REDIS_SERVERS[TestRedisServers.OPEN],
|
socket: TEST_REDIS_SERVERS[TestRedisServers.OPEN],
|
||||||
callbackify: true
|
legacyMode: true
|
||||||
});
|
});
|
||||||
|
|
||||||
before(() => client.connect());
|
before(() => client.connect());
|
||||||
after(async () => {
|
afterEach(() => client.modern.flushAll());
|
||||||
await (client as any).flushAllAsync();
|
after(() => client.disconnect());
|
||||||
await client.disconnect();
|
|
||||||
});
|
|
||||||
|
|
||||||
it('client.{command} should call the callback', done => {
|
it('client.sendCommand should call the callback', done => {
|
||||||
(client as any).ping((err: Error, reply: string) => {
|
(client as any).sendCommand('PING', (err?: Error, reply?: string) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
return done(err);
|
return done(err);
|
||||||
}
|
}
|
||||||
@@ -60,17 +58,95 @@ describe('Client', () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('client.{command} should work without callback', async () => {
|
it('client.sendCommand should work without callback', async () => {
|
||||||
(client as any).ping();
|
(client as any).sendCommand('PING');
|
||||||
await (client as any).pingAsync(); // make sure the first command was replied
|
await client.modern.ping(); // make sure the first command was replied
|
||||||
});
|
});
|
||||||
|
|
||||||
it('client.{command}Async should return a promise', async () => {
|
it('client.modern.sendCommand should return a promise', async () => {
|
||||||
assert.equal(
|
assert.equal(
|
||||||
await (client as any).pingAsync(),
|
await client.modern.sendCommand(['PING']),
|
||||||
'PONG'
|
'PONG'
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('client.{command} should accept vardict arguments', done => {
|
||||||
|
(client as any).set('a', 'b', (err?: Error, reply?: string) => {
|
||||||
|
if (err) {
|
||||||
|
return done(err);
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
assert.equal(reply, 'OK');
|
||||||
|
done();
|
||||||
|
} catch (err) {
|
||||||
|
done(err);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('client.{command} should accept arguments array', done => {
|
||||||
|
(client as any).set(['a', 'b'], (err?: Error, reply?: string) => {
|
||||||
|
if (err) {
|
||||||
|
return done(err);
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
assert.equal(reply, 'OK');
|
||||||
|
done();
|
||||||
|
} catch (err) {
|
||||||
|
done(err);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('client.{command} should accept mix of strings and array of strings', done => {
|
||||||
|
(client as any).set(['a'], 'b', ['GET'], (err?: Error, reply?: string) => {
|
||||||
|
if (err) {
|
||||||
|
return done(err);
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
assert.equal(reply, null);
|
||||||
|
done();
|
||||||
|
} catch (err) {
|
||||||
|
done(err);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('client.multi.exec should call the callback', done => {
|
||||||
|
(client as any).multi()
|
||||||
|
.ping()
|
||||||
|
.exec((err?: Error, reply?: string) => {
|
||||||
|
if (err) {
|
||||||
|
return done(err);
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
assert.deepEqual(reply, ['PONG']);
|
||||||
|
done();
|
||||||
|
} catch (err) {
|
||||||
|
done(err);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('client.multi.exec should work without callback', async () => {
|
||||||
|
(client as any).multi()
|
||||||
|
.ping()
|
||||||
|
.exec();
|
||||||
|
await client.modern.ping(); // make sure the first command was replied
|
||||||
|
});
|
||||||
|
|
||||||
|
it('client.modern.exec should return a promise', async () => {
|
||||||
|
assert.deepEqual(
|
||||||
|
await ((client as any).multi().modern
|
||||||
|
.ping()
|
||||||
|
.exec()),
|
||||||
|
['PONG']
|
||||||
|
);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('events', () => {
|
describe('events', () => {
|
||||||
|
@@ -13,7 +13,7 @@ export interface RedisClientOptions<M = RedisModules, S = RedisLuaScripts> {
|
|||||||
scripts?: S;
|
scripts?: S;
|
||||||
commandsQueueMaxLength?: number;
|
commandsQueueMaxLength?: number;
|
||||||
readOnly?: boolean;
|
readOnly?: boolean;
|
||||||
callbackify?: boolean;
|
legacyMode?: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
export type RedisCommandSignature<C extends RedisCommand> =
|
export type RedisCommandSignature<C extends RedisCommand> =
|
||||||
@@ -50,24 +50,6 @@ export default class RedisClient<M extends RedisModules = RedisModules, S extend
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
static callbackifyCommand(on: any, name: string): void {
|
|
||||||
const originalFunction = on[name + 'Async'] = on[name];
|
|
||||||
on[name] = function (...args: Array<unknown>) {
|
|
||||||
const hasCallback = typeof args[args.length - 1] === 'function',
|
|
||||||
callback = (hasCallback && args.pop()) as Function;
|
|
||||||
|
|
||||||
const promise = originalFunction.apply(this, args);
|
|
||||||
if (hasCallback) {
|
|
||||||
promise
|
|
||||||
.then((reply: RedisReply) => callback(null, reply))
|
|
||||||
.catch((err: Error) => callback(err));
|
|
||||||
} else {
|
|
||||||
promise
|
|
||||||
.catch((err: Error) => this.emit('error', err));
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
static create<M extends RedisModules, S extends RedisLuaScripts>(options?: RedisClientOptions<M, S>): RedisClientType<M, S> {
|
static create<M extends RedisModules, S extends RedisLuaScripts>(options?: RedisClientOptions<M, S>): RedisClientType<M, S> {
|
||||||
return <any>new RedisClient<M, S>(options);
|
return <any>new RedisClient<M, S>(options);
|
||||||
}
|
}
|
||||||
@@ -80,6 +62,7 @@ export default class RedisClient<M extends RedisModules = RedisModules, S extend
|
|||||||
readonly #socket: RedisSocket;
|
readonly #socket: RedisSocket;
|
||||||
readonly #queue: RedisCommandsQueue;
|
readonly #queue: RedisCommandsQueue;
|
||||||
readonly #Multi: typeof RedisMultiCommand & { new(): RedisMultiCommandType<M, S> };
|
readonly #Multi: typeof RedisMultiCommand & { new(): RedisMultiCommandType<M, S> };
|
||||||
|
readonly #modern: Record<string, Function> = {};
|
||||||
#selectedDB = 0;
|
#selectedDB = 0;
|
||||||
|
|
||||||
get options(): RedisClientOptions<M> | null | undefined {
|
get options(): RedisClientOptions<M> | null | undefined {
|
||||||
@@ -90,6 +73,14 @@ export default class RedisClient<M extends RedisModules = RedisModules, S extend
|
|||||||
return this.#socket.isOpen;
|
return this.#socket.isOpen;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
get modern(): Record<string, Function> {
|
||||||
|
if (!this.#options?.legacyMode) {
|
||||||
|
throw new Error('the client is not in "legacy mode"');
|
||||||
|
}
|
||||||
|
|
||||||
|
return this.#modern;
|
||||||
|
}
|
||||||
|
|
||||||
constructor(options?: RedisClientOptions<M, S>) {
|
constructor(options?: RedisClientOptions<M, S>) {
|
||||||
super();
|
super();
|
||||||
this.#options = options;
|
this.#options = options;
|
||||||
@@ -98,7 +89,7 @@ export default class RedisClient<M extends RedisModules = RedisModules, S extend
|
|||||||
this.#Multi = this.#initiateMulti();
|
this.#Multi = this.#initiateMulti();
|
||||||
this.#initiateModules();
|
this.#initiateModules();
|
||||||
this.#initiateScripts();
|
this.#initiateScripts();
|
||||||
this.#callbackify();
|
this.#legacyMode();
|
||||||
}
|
}
|
||||||
|
|
||||||
#initiateSocket(): RedisSocket {
|
#initiateSocket(): RedisSocket {
|
||||||
@@ -159,7 +150,7 @@ export default class RedisClient<M extends RedisModules = RedisModules, S extend
|
|||||||
const options = this.#options;
|
const options = this.#options;
|
||||||
return <any>class extends RedisMultiCommand {
|
return <any>class extends RedisMultiCommand {
|
||||||
constructor() {
|
constructor() {
|
||||||
super(executor, options?.modules, options?.scripts);
|
super(executor, options);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@@ -203,22 +194,61 @@ export default class RedisClient<M extends RedisModules = RedisModules, S extend
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#callbackify(): void {
|
#legacyMode(): void {
|
||||||
if (!this.#options?.callbackify) return;
|
if (!this.#options?.legacyMode) return;
|
||||||
|
|
||||||
|
this.#modern.sendCommand = this.sendCommand.bind(this);
|
||||||
|
|
||||||
|
(this as any).sendCommand = (...args: Array<unknown>): void => {
|
||||||
|
const options = isCommandOptions(args[0]) && args.shift(),
|
||||||
|
callback = typeof args[args.length - 1] === 'function' && (args.pop() as Function);
|
||||||
|
|
||||||
|
this.#modern.sendCommand(args.flat(), options)
|
||||||
|
.then((reply: unknown) => {
|
||||||
|
if (!callback) return;
|
||||||
|
|
||||||
|
// https://github.com/NodeRedis/node-redis#commands:~:text=minimal%20parsing
|
||||||
|
|
||||||
|
callback(null, reply);
|
||||||
|
})
|
||||||
|
.catch((err: Error) => {
|
||||||
|
if (!callback) {
|
||||||
|
this.emit('error', err);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
callback(err);
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
for (const name of Object.keys(COMMANDS)) {
|
for (const name of Object.keys(COMMANDS)) {
|
||||||
RedisClient.callbackifyCommand(this, name);
|
this.#defineLegacyCommand(name);
|
||||||
RedisClient.callbackifyCommand(this.#Multi.prototype, name);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!this.#options?.modules) return;
|
// hard coded commands
|
||||||
|
this.#defineLegacyCommand('SELECT');
|
||||||
|
this.#defineLegacyCommand('select');
|
||||||
|
|
||||||
for (const m of this.#options.modules) {
|
if (this.#options?.modules) {
|
||||||
for (const name of Object.keys(m)) {
|
for (const m of this.#options.modules) {
|
||||||
RedisClient.callbackifyCommand(this, name);
|
for (const name of Object.keys(m)) {
|
||||||
RedisClient.callbackifyCommand(this.#Multi.prototype, name);
|
this.#defineLegacyCommand(name);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (this.#options?.scripts) {
|
||||||
|
for (const name of Object.keys(this.#options.scripts)) {
|
||||||
|
this.#defineLegacyCommand(name);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#defineLegacyCommand(name: string): void {
|
||||||
|
this.#modern[name] = (this as any)[name];
|
||||||
|
(this as any)[name] = function (...args: Array<unknown>): void {
|
||||||
|
this.sendCommand(name, ...args);
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
duplicate(): RedisClientType<M, S> {
|
duplicate(): RedisClientType<M, S> {
|
||||||
|
@@ -2,6 +2,7 @@ import COMMANDS from './commands/client';
|
|||||||
import { RedisCommand, RedisModules, RedisReply } from './commands';
|
import { RedisCommand, RedisModules, RedisReply } from './commands';
|
||||||
import RedisCommandsQueue from './commands-queue';
|
import RedisCommandsQueue from './commands-queue';
|
||||||
import { RedisLuaScript, RedisLuaScripts } from './lua-script';
|
import { RedisLuaScript, RedisLuaScripts } from './lua-script';
|
||||||
|
import { RedisClientOptions } from './client';
|
||||||
|
|
||||||
type RedisMultiCommandSignature<C extends RedisCommand, M extends RedisModules, S extends RedisLuaScripts> = (...args: Parameters<C['transformArguments']>) => RedisMultiCommandType<M, S>;
|
type RedisMultiCommandSignature<C extends RedisCommand, M extends RedisModules, S extends RedisLuaScripts> = (...args: Parameters<C['transformArguments']>) => RedisMultiCommandType<M, S>;
|
||||||
|
|
||||||
@@ -29,71 +30,136 @@ export type RedisMultiExecutor = (queue: Array<MultiQueuedCommand>, chainId: Sym
|
|||||||
export default class RedisMultiCommand<M extends RedisModules = RedisModules, S extends RedisLuaScripts = RedisLuaScripts> {
|
export default class RedisMultiCommand<M extends RedisModules = RedisModules, S extends RedisLuaScripts = RedisLuaScripts> {
|
||||||
static defineCommand(on: any, name: string, command: RedisCommand): void {
|
static defineCommand(on: any, name: string, command: RedisCommand): void {
|
||||||
on[name] = function (...args: Parameters<typeof command.transformArguments>) {
|
on[name] = function (...args: Parameters<typeof command.transformArguments>) {
|
||||||
return this.addCommand(command.transformArguments(...args), command.transformReply);
|
// do not return `this.addCommand` directly cause in legacy mode it's binded to the legacy version
|
||||||
|
this.addCommand(command.transformArguments(...args), command.transformReply);
|
||||||
|
return this;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
static defineLuaScript(on: any, name: string, script: RedisLuaScript): void {
|
static create<M extends RedisModules, S extends RedisLuaScripts>(executor: RedisMultiExecutor, clientOptions?: RedisClientOptions<M, S>): RedisMultiCommandType<M, S> {
|
||||||
on[name] = function (...args: Array<unknown>) {
|
return <any>new RedisMultiCommand<M, S>(executor, clientOptions);
|
||||||
let evalArgs;
|
|
||||||
if (this.#scriptsInUse.has(name)) {
|
|
||||||
evalArgs = [
|
|
||||||
'EVALSHA',
|
|
||||||
script.SHA
|
|
||||||
];
|
|
||||||
} else {
|
|
||||||
this.#scriptsInUse.add(name);
|
|
||||||
evalArgs = [
|
|
||||||
'EVAL',
|
|
||||||
script.SCRIPT
|
|
||||||
];
|
|
||||||
}
|
|
||||||
|
|
||||||
return this.addCommand(
|
|
||||||
[
|
|
||||||
...evalArgs,
|
|
||||||
script.NUMBER_OF_KEYS,
|
|
||||||
...script.transformArguments(...args)
|
|
||||||
],
|
|
||||||
script.transformReply
|
|
||||||
);
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
static create<M extends RedisModules, S extends RedisLuaScripts>(executor: RedisMultiExecutor, modules?: M, scripts?: S): RedisMultiCommandType<M, S> {
|
|
||||||
return <any>new RedisMultiCommand<M, S>(executor, modules, scripts);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
readonly #executor: RedisMultiExecutor;
|
readonly #executor: RedisMultiExecutor;
|
||||||
|
|
||||||
|
readonly #clientOptions: RedisClientOptions<M, S> | undefined;
|
||||||
|
|
||||||
readonly #queue: Array<MultiQueuedCommand> = [];
|
readonly #queue: Array<MultiQueuedCommand> = [];
|
||||||
|
|
||||||
readonly #scriptsInUse = new Set<string>();
|
readonly #scriptsInUse = new Set<string>();
|
||||||
|
|
||||||
constructor(executor: RedisMultiExecutor, modules?: RedisModules, scripts?: RedisLuaScripts) {
|
readonly #modern: Record<string, Function> = {};
|
||||||
this.#executor = executor;
|
|
||||||
this.#initiateModules(modules);
|
get modern(): Record<string, Function> {
|
||||||
this.#initiateScripts(scripts);
|
if (!this.#clientOptions?.legacyMode) {
|
||||||
|
throw new Error('client is not in "legacy mode"');
|
||||||
|
}
|
||||||
|
|
||||||
|
return this.#modern;
|
||||||
}
|
}
|
||||||
|
|
||||||
#initiateModules(modules?: RedisModules): void {
|
constructor(executor: RedisMultiExecutor, clientOptions?: RedisClientOptions<M, S>) {
|
||||||
if (!modules) return;
|
this.#executor = executor;
|
||||||
|
this.#clientOptions = clientOptions;
|
||||||
|
this.#initiateModules();
|
||||||
|
this.#initiateScripts();
|
||||||
|
this.#legacyMode();
|
||||||
|
}
|
||||||
|
|
||||||
for (const m of modules) {
|
#initiateModules(): void {
|
||||||
|
if (!this.#clientOptions?.modules) return;
|
||||||
|
|
||||||
|
for (const m of this.#clientOptions.modules) {
|
||||||
for (const [name, command] of Object.entries(m)) {
|
for (const [name, command] of Object.entries(m)) {
|
||||||
RedisMultiCommand.defineCommand(this, name, command);
|
RedisMultiCommand.defineCommand(this, name, command);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#initiateScripts(scripts?: RedisLuaScripts): void {
|
#initiateScripts(): void {
|
||||||
if (!scripts) return;
|
if (!this.#clientOptions?.scripts) return;
|
||||||
|
|
||||||
for (const [name, script] of Object.entries(scripts)) {
|
for (const [name, script] of Object.entries(this.#clientOptions.scripts)) {
|
||||||
RedisMultiCommand.defineLuaScript(this, name, script);
|
(this as any)[name] = function (...args: Array<unknown>) {
|
||||||
|
let evalArgs;
|
||||||
|
if (this.#scriptsInUse.has(name)) {
|
||||||
|
evalArgs = [
|
||||||
|
'EVALSHA',
|
||||||
|
script.SHA
|
||||||
|
];
|
||||||
|
} else {
|
||||||
|
this.#scriptsInUse.add(name);
|
||||||
|
evalArgs = [
|
||||||
|
'EVAL',
|
||||||
|
script.SCRIPT
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
return this.addCommand(
|
||||||
|
[
|
||||||
|
...evalArgs,
|
||||||
|
script.NUMBER_OF_KEYS,
|
||||||
|
...script.transformArguments(...args)
|
||||||
|
],
|
||||||
|
script.transformReply
|
||||||
|
);
|
||||||
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#legacyMode(): Record<string, Function> | undefined {
|
||||||
|
if (!this.#clientOptions?.legacyMode) return;
|
||||||
|
|
||||||
|
this.#modern.exec = this.exec.bind(this);
|
||||||
|
this.#modern.addCommand = this.addCommand.bind(this);
|
||||||
|
|
||||||
|
(this as any).exec = function (...args: Array<unknown>): void {
|
||||||
|
const callback = typeof args[args.length - 1] === 'function' && args.pop() as Function;
|
||||||
|
this.#modern.exec()
|
||||||
|
.then((reply: unknown) => {
|
||||||
|
if (!callback) return;
|
||||||
|
|
||||||
|
callback(null, reply);
|
||||||
|
})
|
||||||
|
.catch((err: Error) => {
|
||||||
|
if (!callback) {
|
||||||
|
// this.emit('error', err);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
callback(err);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
for (const name of Object.keys(COMMANDS)) {
|
||||||
|
this.#defineLegacyCommand(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (this.#clientOptions.modules) {
|
||||||
|
for (const m of this.#clientOptions.modules) {
|
||||||
|
for (const name of Object.keys(m)) {
|
||||||
|
this.#defineLegacyCommand(name);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (this.#clientOptions.scripts) {
|
||||||
|
for (const name of Object.keys(this.#clientOptions.scripts)) {
|
||||||
|
this.#defineLegacyCommand(name);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#defineLegacyCommand(name: string): void {
|
||||||
|
this.#modern[name] = (this as any)[name];
|
||||||
|
|
||||||
|
// TODO: https://github.com/NodeRedis/node-redis#commands:~:text=minimal%20parsing
|
||||||
|
(this as any)[name] = function (...args: Array<unknown>) {
|
||||||
|
return this.addCommand([name, ...args.flat()]);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
addCommand(args: Array<string>, transformReply?: RedisCommand['transformReply']): this {
|
addCommand(args: Array<string>, transformReply?: RedisCommand['transformReply']): this {
|
||||||
this.#queue.push({
|
this.#queue.push({
|
||||||
encodedCommand: RedisCommandsQueue.encodeCommand(args),
|
encodedCommand: RedisCommandsQueue.encodeCommand(args),
|
||||||
|
Reference in New Issue
Block a user