Skip to content

Commit

Permalink
added clientGetName command in node.
Browse files Browse the repository at this point in the history
  • Loading branch information
Adan committed Oct 1, 2023
1 parent 33a0cb4 commit eb3d9bc
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 0 deletions.
4 changes: 4 additions & 0 deletions node/src/Commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,10 @@ export function createSelect(index: number): redis_request.Command {
return createCommand(RequestType.Select, [index.toString()]);
}

export function createClientGetName(): redis_request.Command {
return createCommand(RequestType.ClientGetName, []);
}

export function createConfigRewrite(): redis_request.Command {
return createCommand(RequestType.ConfigRewrite, []);
}
Expand Down
11 changes: 11 additions & 0 deletions node/src/RedisClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as net from "net";
import { BaseClient, ConnectionOptions, ReturnType } from "./BaseClient";
import {
InfoOptions,
createClientGetName,
createConfigGet,
createConfigResetStat,
createConfigRewrite,
Expand Down Expand Up @@ -127,6 +128,16 @@ export class RedisClient extends BaseClient {
return this.createWritePromise(createSelect(index));
}

/** Get the name of the current connection.
* See https://redis.io/commands/client-getname/ for more details.
*
* @returns the name of the client connection as a string if a name is set,
* or null if no name is assigned.
*/
public clientGetName(): Promise<string | null> {
return this.createWritePromise(createClientGetName());
}

/** Rewrite the configuration file with the current configuration.
* See https://redis.io/commands/config-rewrite/ for details.
*
Expand Down
25 changes: 25 additions & 0 deletions node/src/RedisClusterClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as net from "net";
import { BaseClient, ConnectionOptions, ReturnType } from "./BaseClient";
import {
InfoOptions,
createClientGetName,
createConfigGet,
createConfigResetStat,
createConfigRewrite,
Expand Down Expand Up @@ -233,6 +234,30 @@ export class RedisClusterClient extends BaseClient {
});
}

/** Get the name of the current connection.
* See https://redis.io/commands/client-getname/ for more details.
*
* @param route - The command will be routed automatically, unless `route` is provided, in which
* case the client will initially try to route the command to the nodes defined by `route`.
*
* @returns - the name of the client connection as a string if a name is set, or null if no name is assigned.
* When specifying a route other than a single node, it will return a dictionary of Address: nodeResponse.
*/
public clientGetName(
route?: Routes
): Promise<ClusterResponse<string | null>> {
const result = this.createWritePromise<string | null>(
createClientGetName(),
toProtobufRoute(route)
);
return result.then((res) => {
return convertMultiNodeResponseToDict<string | null>(
res,
(response) => typeof response == "string" || response == null
);
});
}

/** Rewrite the configuration file with the current configuration.
* See https://redis.io/commands/config-rewrite/ for details.
*
Expand Down
11 changes: 11 additions & 0 deletions node/src/Transaction.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
InfoOptions,
SetOptions,
createClientGetName,
createConfigGet,
createConfigResetStat,
createConfigRewrite,
Expand Down Expand Up @@ -85,6 +86,16 @@ export class BaseTransaction {
this.commands.push(createDel(keys));
}

/** Get the name of the current connection.
* See https://redis.io/commands/client-getname/ for more details.
*
* Command Response - the name of the client connection as a string if a name is set, or null if no name is assigned.
* When specifying a route other than a single node, the response will be a dictionary of Address: nodeResponse.
*/
public clientGetName() {
this.commands.push(createClientGetName());
}

/** Rewrite the configuration file with the current configuration.
* See https://redis.io/commands/select/ for details.
*
Expand Down
11 changes: 11 additions & 0 deletions node/tests/SharedTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type BaseClient = {
ping: (str?: string) => Promise<string>;
get: (key: string) => Promise<string | null>;
del: (keys: string[]) => Promise<number>;
clientGetName: () => Promise<ClusterResponse<string | null>>;
configRewrite: () => Promise<"OK">;
info(options?: InfoOptions[]): Promise<ClusterResponse<string>>;
configResetStat: () => Promise<"OK">;
Expand Down Expand Up @@ -218,6 +219,16 @@ export function runBaseTests<Context>(config: {
config.timeout
);

it(
"testing clientGetName",
async () => {
await runTest(async (client: BaseClient) => {
expect(await client.clientGetName()).toBeNull();
});
},
config.timeout
);

it(
"test config rewrite",
async () => {
Expand Down

0 comments on commit eb3d9bc

Please sign in to comment.