Skip to content

Commit

Permalink
Node: added echo command.
Browse files Browse the repository at this point in the history
  • Loading branch information
Adan committed Feb 22, 2024
1 parent 44ef9a9 commit e2a78cb
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 0 deletions.
11 changes: 11 additions & 0 deletions node/src/BaseClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
createDecr,
createDecrBy,
createDel,
createEcho,
createExists,
createExpire,
createExpireAt,
Expand Down Expand Up @@ -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<string> {
return this.createWritePromise(createEcho(message));
}

private readonly MAP_READ_FROM_STRATEGY: Record<
ReadFrom,
connection_request.ReadFrom
Expand Down
7 changes: 7 additions & 0 deletions node/src/Commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
}
12 changes: 12 additions & 0 deletions node/src/Transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
createDecr,
createDecrBy,
createDel,
createEcho,
createExists,
createExpire,
createExpireAt,
Expand Down Expand Up @@ -900,6 +901,17 @@ export class BaseTransaction<T extends BaseTransaction<T>> {
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.
*
Expand Down
11 changes: 11 additions & 0 deletions node/tests/SharedTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1532,6 +1532,17 @@ export function runBaseTests<Context>(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) => {
Expand Down
2 changes: 2 additions & 0 deletions node/tests/TestUtilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
Expand Down

0 comments on commit e2a78cb

Please sign in to comment.