From e2a78cb60773a7a17f3a204e9826b3507d20c4d4 Mon Sep 17 00:00:00 2001 From: Adan Date: Wed, 21 Feb 2024 16:22:07 +0000 Subject: [PATCH] Node: added echo command. --- node/src/BaseClient.ts | 11 +++++++++++ node/src/Commands.ts | 7 +++++++ node/src/Transaction.ts | 12 ++++++++++++ node/tests/SharedTests.ts | 11 +++++++++++ node/tests/TestUtilities.ts | 2 ++ 5 files changed, 43 insertions(+) diff --git a/node/src/BaseClient.ts b/node/src/BaseClient.ts index a73c40f47e..ff86a54263 100644 --- a/node/src/BaseClient.ts +++ b/node/src/BaseClient.ts @@ -18,6 +18,7 @@ import { createDecr, createDecrBy, createDel, + createEcho, createExists, createExpire, createExpireAt, @@ -1138,6 +1139,16 @@ export class BaseClient { return this.createWritePromise(createZpopmax(key, count)); } + /** Echoes the provided `message` back. + * See https://redis.io/commands/echo for more details. + * + * @param message - The message to be echoed back. + * @returns The provided `message`. + */ + public echo(message: string): Promise { + return this.createWritePromise(createEcho(message)); + } + private readonly MAP_READ_FROM_STRATEGY: Record< ReadFrom, connection_request.ReadFrom diff --git a/node/src/Commands.ts b/node/src/Commands.ts index ffd24f4731..9b856cfdf3 100644 --- a/node/src/Commands.ts +++ b/node/src/Commands.ts @@ -863,3 +863,10 @@ export function createZpopmax(key: string, count?: number): redis_request.Comman const args: string[] = count == undefined ? [key] : [key, count.toString()]; return createCommand(RequestType.ZPopMax, args); } + +/** + * @internal + */ +export function createEcho(message: string): redis_request.Command { + return createCommand(RequestType.Echo, [message]); +} diff --git a/node/src/Transaction.ts b/node/src/Transaction.ts index cf6f3826b9..296f63c400 100644 --- a/node/src/Transaction.ts +++ b/node/src/Transaction.ts @@ -18,6 +18,7 @@ import { createDecr, createDecrBy, createDel, + createEcho, createExists, createExpire, createExpireAt, @@ -900,6 +901,17 @@ export class BaseTransaction> { return this.addAndReturn(createZpopmax(key, count)); } + /** Echoes the provided `message` back. + * See https://redis.io/commands/echo for more details. + * + * @param message - The message to be echoed back. + * + * Command Response - The provided `message`. + */ + public echo(message: string): T { + return this.addAndReturn(createEcho(message)); + } + /** Executes a single command, without checking inputs. Every part of the command, including subcommands, * should be added as a separate value in args. * diff --git a/node/tests/SharedTests.ts b/node/tests/SharedTests.ts index 2ed87bedbb..cb23636dbc 100644 --- a/node/tests/SharedTests.ts +++ b/node/tests/SharedTests.ts @@ -1532,6 +1532,17 @@ export function runBaseTests(config: { config.timeout ); + it.each([ProtocolVersion.RESP2, ProtocolVersion.RESP3])( + `echo test_%p`, + async (protocol) => { + await runTest(async (client: BaseClient) => { + const message = uuidv4(); + expect(await client.echo(message)).toEqual(message); + }, protocol); + }, + config.timeout + ); + it.each([ProtocolVersion.RESP2, ProtocolVersion.RESP3])( `strlen test_%p`, async (protocol) => { diff --git a/node/tests/TestUtilities.ts b/node/tests/TestUtilities.ts index fadf6e5fde..553b3b3224 100644 --- a/node/tests/TestUtilities.ts +++ b/node/tests/TestUtilities.ts @@ -68,6 +68,8 @@ export function transactionTest( args.push("OK"); baseTransaction.type(key1); args.push("string"); + baseTransaction.echo(value); + args.push(value); baseTransaction.set(key2, "baz", { returnOldValue: true, });