1
0
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:
leibale
2021-06-01 16:51:22 -04:00
parent 4cbcc90bbb
commit f62b68d672
4 changed files with 258 additions and 84 deletions

View File

@@ -5,6 +5,8 @@
* [`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`, ...)~~
* Key prefixing (?) (partially supported in v3)
* Support for RESP3
* client-side caching
## Client
* ~~Blocking Commands~~

View File

@@ -33,20 +33,18 @@ describe('Client', () => {
});
});
describe('callbackify', () => {
describe('legacyMode', () => {
const client = RedisClient.create({
socket: TEST_REDIS_SERVERS[TestRedisServers.OPEN],
callbackify: true
legacyMode: true
});
before(() => client.connect());
after(async () => {
await (client as any).flushAllAsync();
await client.disconnect();
});
afterEach(() => client.modern.flushAll());
after(() => client.disconnect());
it('client.{command} should call the callback', done => {
(client as any).ping((err: Error, reply: string) => {
it('client.sendCommand should call the callback', done => {
(client as any).sendCommand('PING', (err?: Error, reply?: string) => {
if (err) {
return done(err);
}
@@ -60,17 +58,95 @@ describe('Client', () => {
});
});
it('client.{command} should work without callback', async () => {
(client as any).ping();
await (client as any).pingAsync(); // make sure the first command was replied
it('client.sendCommand should work without callback', async () => {
(client as any).sendCommand('PING');
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(
await (client as any).pingAsync(),
await client.modern.sendCommand(['PING']),
'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', () => {

View File

@@ -13,7 +13,7 @@ export interface RedisClientOptions<M = RedisModules, S = RedisLuaScripts> {
scripts?: S;
commandsQueueMaxLength?: number;
readOnly?: boolean;
callbackify?: boolean;
legacyMode?: boolean;
}
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> {
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 #queue: RedisCommandsQueue;
readonly #Multi: typeof RedisMultiCommand & { new(): RedisMultiCommandType<M, S> };
readonly #modern: Record<string, Function> = {};
#selectedDB = 0;
get options(): RedisClientOptions<M> | null | undefined {
@@ -90,6 +73,14 @@ export default class RedisClient<M extends RedisModules = RedisModules, S extend
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>) {
super();
this.#options = options;
@@ -98,7 +89,7 @@ export default class RedisClient<M extends RedisModules = RedisModules, S extend
this.#Multi = this.#initiateMulti();
this.#initiateModules();
this.#initiateScripts();
this.#callbackify();
this.#legacyMode();
}
#initiateSocket(): RedisSocket {
@@ -159,7 +150,7 @@ export default class RedisClient<M extends RedisModules = RedisModules, S extend
const options = this.#options;
return <any>class extends RedisMultiCommand {
constructor() {
super(executor, options?.modules, options?.scripts);
super(executor, options);
}
};
}
@@ -203,24 +194,63 @@ export default class RedisClient<M extends RedisModules = RedisModules, S extend
}
}
#callbackify(): void {
if (!this.#options?.callbackify) return;
#legacyMode(): void {
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)) {
RedisClient.callbackifyCommand(this, name);
RedisClient.callbackifyCommand(this.#Multi.prototype, name);
this.#defineLegacyCommand(name);
}
if (!this.#options?.modules) return;
// hard coded commands
this.#defineLegacyCommand('SELECT');
this.#defineLegacyCommand('select');
if (this.#options?.modules) {
for (const m of this.#options.modules) {
for (const name of Object.keys(m)) {
RedisClient.callbackifyCommand(this, name);
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> {
return RedisClient.create(this.#options);
}

View File

@@ -2,6 +2,7 @@ import COMMANDS from './commands/client';
import { RedisCommand, RedisModules, RedisReply } from './commands';
import RedisCommandsQueue from './commands-queue';
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>;
@@ -29,12 +30,57 @@ export type RedisMultiExecutor = (queue: Array<MultiQueuedCommand>, chainId: Sym
export default class RedisMultiCommand<M extends RedisModules = RedisModules, S extends RedisLuaScripts = RedisLuaScripts> {
static defineCommand(on: any, name: string, command: RedisCommand): void {
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 {
on[name] = function (...args: Array<unknown>) {
static create<M extends RedisModules, S extends RedisLuaScripts>(executor: RedisMultiExecutor, clientOptions?: RedisClientOptions<M, S>): RedisMultiCommandType<M, S> {
return <any>new RedisMultiCommand<M, S>(executor, clientOptions);
}
readonly #executor: RedisMultiExecutor;
readonly #clientOptions: RedisClientOptions<M, S> | undefined;
readonly #queue: Array<MultiQueuedCommand> = [];
readonly #scriptsInUse = new Set<string>();
readonly #modern: Record<string, Function> = {};
get modern(): Record<string, Function> {
if (!this.#clientOptions?.legacyMode) {
throw new Error('client is not in "legacy mode"');
}
return this.#modern;
}
constructor(executor: RedisMultiExecutor, clientOptions?: RedisClientOptions<M, S>) {
this.#executor = executor;
this.#clientOptions = clientOptions;
this.#initiateModules();
this.#initiateScripts();
this.#legacyMode();
}
#initiateModules(): void {
if (!this.#clientOptions?.modules) return;
for (const m of this.#clientOptions.modules) {
for (const [name, command] of Object.entries(m)) {
RedisMultiCommand.defineCommand(this, name, command);
}
}
}
#initiateScripts(): void {
if (!this.#clientOptions?.scripts) return;
for (const [name, script] of Object.entries(this.#clientOptions.scripts)) {
(this as any)[name] = function (...args: Array<unknown>) {
let evalArgs;
if (this.#scriptsInUse.has(name)) {
evalArgs = [
@@ -59,40 +105,60 @@ export default class RedisMultiCommand<M extends RedisModules = RedisModules, S
);
};
}
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;
#legacyMode(): Record<string, Function> | undefined {
if (!this.#clientOptions?.legacyMode) return;
readonly #queue: Array<MultiQueuedCommand> = [];
this.#modern.exec = this.exec.bind(this);
this.#modern.addCommand = this.addCommand.bind(this);
readonly #scriptsInUse = new Set<string>();
(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;
constructor(executor: RedisMultiExecutor, modules?: RedisModules, scripts?: RedisLuaScripts) {
this.#executor = executor;
this.#initiateModules(modules);
this.#initiateScripts(scripts);
callback(null, reply);
})
.catch((err: Error) => {
if (!callback) {
// this.emit('error', err);
return;
}
#initiateModules(modules?: RedisModules): void {
if (!modules) return;
callback(err);
});
};
for (const m of modules) {
for (const [name, command] of Object.entries(m)) {
RedisMultiCommand.defineCommand(this, name, command);
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);
}
}
}
#initiateScripts(scripts?: RedisLuaScripts): void {
if (!scripts) return;
for (const [name, script] of Object.entries(scripts)) {
RedisMultiCommand.defineLuaScript(this, name, script);
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 {
this.#queue.push({