Skip to content

Commit

Permalink
Node: Add binary variant to server management commands. (valkey-io#2179)
Browse files Browse the repository at this point in the history
* Add binary variant to server management commands.

Signed-off-by: Yury-Fridlyand <yury.fridlyand@improving.com>
Co-authored-by: Andrew Carbonetto <andrew.carbonetto@improving.com>
  • Loading branch information
Yury-Fridlyand and acarbonetto authored Aug 23, 2024
1 parent 68deaab commit 82cbd33
Show file tree
Hide file tree
Showing 9 changed files with 294 additions and 195 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#### Changes
* Node: Added binary variant to server management commands ([#2179](https://github.com/valkey-io/valkey-glide/pull/2179))
* Node: Added/updated binary variant to connection management commands and WATCH/UNWATCH ([#2160](https://github.com/valkey-io/valkey-glide/pull/2160))
* Java: Fix docs for stream commands ([#2086](https://github.com/valkey-io/valkey-glide/pull/2086))
* Node: Added binary variant to bitmap commands ([#2178](https://github.com/valkey-io/valkey-glide/pull/2178))
Expand Down
2 changes: 2 additions & 0 deletions node/npm/glide/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ function initialize() {
SlotKeyTypes,
TimeUnit,
RouteByAddress,
RouteOption,
Routes,
RestoreOptions,
SingleNodeRoute,
Expand Down Expand Up @@ -227,6 +228,7 @@ function initialize() {
TimeUnit,
ReturnTypeXinfoStream,
RouteByAddress,
RouteOption,
Routes,
RestoreOptions,
SingleNodeRoute,
Expand Down
10 changes: 9 additions & 1 deletion node/src/BaseClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import {
BitwiseOperation,
Boundary,
CoordOrigin, // eslint-disable-line @typescript-eslint/no-unused-vars
DecoderOption,
ExpireOptions,
GeoAddOptions,
GeoBoxShape, // eslint-disable-line @typescript-eslint/no-unused-vars
Expand Down Expand Up @@ -289,6 +288,15 @@ export enum Decoder {
String,
}

/** An extension to command option types with {@link Decoder}. */
export type DecoderOption = {
/**
* {@link Decoder} type which defines how to handle the response.
* If not set, the {@link BaseClientConfiguration.defaultDecoder|default decoder} will be used.
*/
decoder?: Decoder;
};

/**
* Our purpose in creating PointerResponse type is to mark when response is of number/long pointer response type.
* Consequently, when the response is returned, we can check whether it is instanceof the PointerResponse type and pass it to the Rust core function with the proper parameters.
Expand Down
35 changes: 6 additions & 29 deletions node/src/Commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import { createLeakedStringVec, MAX_REQUEST_ARGS_LEN } from "glide-rs";
import Long from "long";

/* eslint-disable-next-line @typescript-eslint/no-unused-vars */
import { BaseClient, Decoder } from "src/BaseClient";
import { BaseClient } from "src/BaseClient";
/* eslint-disable-next-line @typescript-eslint/no-unused-vars */
import { GlideClient } from "src/GlideClient";
/* eslint-disable-next-line @typescript-eslint/no-unused-vars */
import { GlideClusterClient, Routes } from "src/GlideClusterClient";
import { GlideClusterClient } from "src/GlideClusterClient";
import { GlideString } from "./BaseClient";
import { command_request } from "./ProtobufMessage";

Expand Down Expand Up @@ -89,24 +89,6 @@ function createCommand(
return singleCommand;
}

/** An extension to command option types with {@link Decoder}. */
export type DecoderOption = {
/**
* {@link Decoder} type which defines how to handle the response.
* If not set, the {@link BaseClientConfiguration.defaultDecoder|default decoder} will be used.
*/
decoder?: Decoder;
};

/** An extension to command option types with {@link Routes}. */
export type RouteOption = {
/**
* Specifies the routing configuration for the command.
* The client will route the command to the nodes defined by `route`.
*/
route?: Routes;
};

/**
* @internal
*/
Expand Down Expand Up @@ -408,7 +390,7 @@ export function createConfigGet(parameters: string[]): command_request.Command {
* @internal
*/
export function createConfigSet(
parameters: Record<string, string>,
parameters: Record<string, GlideString>,
): command_request.Command {
return createCommand(
RequestType.ConfigSet,
Expand Down Expand Up @@ -2902,23 +2884,18 @@ export function createObjectRefcount(
return createCommand(RequestType.ObjectRefCount, [key]);
}

/** Additional parameters for `LOLWUT` command. */
export type LolwutOptions = {
/**
* An optional argument that can be used to specify the version of computer art to generate.
*/
version?: number;
/**
* An optional argument that can be used to specify the output:
* For version `5`, those are length of the line, number of squares per row, and number of squares per column.
* For version `6`, those are number of columns and number of lines.
* - For version `5`, those are length of the line, number of squares per row, and number of squares per column.
* - For version `6`, those are number of columns and number of lines.
*/
parameters?: number[];
/**
* An optional argument specifies the type of decoding.
* Use Decoder.String to get the response as a String.
* Use Decoder.Bytes to get the response in a buffer.
*/
decoder?: Decoder;
};

/**
Expand Down
103 changes: 66 additions & 37 deletions node/src/GlideClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ import {
BaseClient,
BaseClientConfiguration,
Decoder,
DecoderOption,
GlideString,
PubSubMsg,
ReadFrom, // eslint-disable-line @typescript-eslint/no-unused-vars
ReturnType,
} from "./BaseClient";
import {
DecoderOption,
FlushMode,
FunctionListOptions,
FunctionListResponse,
Expand Down Expand Up @@ -260,15 +260,19 @@ export class GlideClient extends BaseClient {
});
}

/** Get information and statistics about the Redis server.
/**
* Gets information and statistics about the server.
*
* @see {@link https://valkey.io/commands/info/|valkey.io} for details.
*
* @param options - A list of InfoSection values specifying which sections of information to retrieve.
* When no parameter is provided, the default option is assumed.
* @returns a string containing the information for the sections requested.
* @param sections - (Optional) A list of {@link InfoOptions} values specifying which sections of information to retrieve.
* When no parameter is provided, {@link InfoOptions.Default|Default} is assumed.
* @returns A string containing the information for the sections requested.
*/
public async info(options?: InfoOptions[]): Promise<string> {
return this.createWritePromise(createInfo(options));
public async info(sections?: InfoOptions[]): Promise<string> {
return this.createWritePromise(createInfo(sections), {
decoder: Decoder.String,
});
}

/**
Expand Down Expand Up @@ -314,7 +318,9 @@ export class GlideClient extends BaseClient {
});
}

/** Rewrite the configuration file with the current configuration.
/**
* Rewrites the configuration file with the current configuration.
*
* @see {@link https://valkey.io/commands/config-rewrite/|valkey.io} for details.
*
* @returns "OK" when the configuration was rewritten properly. Otherwise, an error is thrown.
Expand All @@ -327,10 +333,13 @@ export class GlideClient extends BaseClient {
* ```
*/
public async configRewrite(): Promise<"OK"> {
return this.createWritePromise(createConfigRewrite());
return this.createWritePromise(createConfigRewrite(), {
decoder: Decoder.String,
});
}

/** Resets the statistics reported by Redis using the INFO and LATENCY HISTOGRAM commands.
/**
* Resets the statistics reported by the server using the `INFO` and `LATENCY HISTOGRAM` commands.
*
* @see {@link https://valkey.io/commands/config-resetstat/|valkey.io} for details.
*
Expand All @@ -344,7 +353,9 @@ export class GlideClient extends BaseClient {
* ```
*/
public async configResetStat(): Promise<"OK"> {
return this.createWritePromise(createConfigResetStat());
return this.createWritePromise(createConfigResetStat(), {
decoder: Decoder.String,
});
}

/**
Expand All @@ -364,11 +375,14 @@ export class GlideClient extends BaseClient {
return this.createWritePromise(createClientId());
}

/** Reads the configuration parameters of a running Redis server.
/**
* Reads the configuration parameters of the running server.
*
* @see {@link https://valkey.io/commands/config-get/|valkey.io} for details.
*
* @param parameters - A list of configuration parameter names to retrieve values for.
* @param decoder - (Optional) {@link Decoder} type which defines how to handle the response.
* If not set, the {@link BaseClientConfiguration.defaultDecoder|default decoder} will be used.
*
* @returns A map of values corresponding to the configuration parameters.
*
Expand All @@ -381,16 +395,19 @@ export class GlideClient extends BaseClient {
*/
public async configGet(
parameters: string[],
): Promise<Record<string, string>> {
return this.createWritePromise(createConfigGet(parameters));
decoder?: Decoder,
): Promise<Record<string, GlideString>> {
return this.createWritePromise(createConfigGet(parameters), {
decoder: decoder,
});
}

/**
* Set configuration parameters to the specified values.
* Sets configuration parameters to the specified values.
*
* @see {@link https://valkey.io/commands/config-set/|valkey.io} for details.
* @param parameters - A List of keyValuePairs consisting of configuration parameters and their respective values to set.
* @returns "OK" when the configuration was set properly. Otherwise an error is thrown.
* @param parameters - A map consisting of configuration parameters and their respective values to set.
* @returns `"OK"` when the configuration was set properly. Otherwise an error is thrown.
*
* @example
* ```typescript
Expand All @@ -399,8 +416,12 @@ export class GlideClient extends BaseClient {
* console.log(result); // Output: 'OK'
* ```
*/
public async configSet(parameters: Record<string, string>): Promise<"OK"> {
return this.createWritePromise(createConfigSet(parameters));
public async configSet(
parameters: Record<string, GlideString>,
): Promise<"OK"> {
return this.createWritePromise(createConfigSet(parameters), {
decoder: Decoder.String,
});
}

/**
Expand Down Expand Up @@ -429,22 +450,24 @@ export class GlideClient extends BaseClient {
});
}

/** Returns the server time
/**
* Returns the server time.
*
* @see {@link https://valkey.io/commands/time/|valkey.io} for details.
*
* @returns - The current server time as a two items `array`:
* A Unix timestamp and the amount of microseconds already elapsed in the current second.
* The returned `array` is in a [Unix timestamp, Microseconds already elapsed] format.
* @returns The current server time as an `array` with two items:
* - A Unix timestamp,
* - The amount of microseconds already elapsed in the current second.
*
* @example
* ```typescript
* // Example usage of time command
* const result = await client.time();
* console.log(result); // Output: ['1710925775', '913580']
* console.log(await client.time()); // Output: ['1710925775', '913580']
* ```
*/
public async time(): Promise<[string, string]> {
return this.createWritePromise(createTime());
return this.createWritePromise(createTime(), {
decoder: Decoder.String,
});
}

/**
Expand Down Expand Up @@ -513,7 +536,7 @@ export class GlideClient extends BaseClient {
*
* @see {@link https://valkey.io/commands/lolwut/|valkey.io} for more details.
*
* @param options - The LOLWUT options
* @param options - (Optional) The LOLWUT options - see {@link LolwutOptions}.
* @returns A piece of generative computer art along with the current server version.
*
* @example
Expand All @@ -523,7 +546,9 @@ export class GlideClient extends BaseClient {
* ```
*/
public async lolwut(options?: LolwutOptions): Promise<string> {
return this.createWritePromise(createLolwut(options));
return this.createWritePromise(createLolwut(options), {
decoder: Decoder.String,
});
}

/**
Expand Down Expand Up @@ -741,35 +766,39 @@ export class GlideClient extends BaseClient {
*
* @see {@link https://valkey.io/commands/flushall/|valkey.io} for more details.
*
* @param mode - The flushing mode, could be either {@link FlushMode.SYNC} or {@link FlushMode.ASYNC}.
* @returns `OK`.
* @param mode - (Optional) The flushing mode, could be either {@link FlushMode.SYNC} or {@link FlushMode.ASYNC}.
* @returns `"OK"`.
*
* @example
* ```typescript
* const result = await client.flushall(FlushMode.SYNC);
* console.log(result); // Output: 'OK'
* ```
*/
public async flushall(mode?: FlushMode): Promise<string> {
return this.createWritePromise(createFlushAll(mode));
public async flushall(mode?: FlushMode): Promise<"OK"> {
return this.createWritePromise(createFlushAll(mode), {
decoder: Decoder.String,
});
}

/**
* Deletes all the keys of the currently selected database. This command never fails.
*
* @see {@link https://valkey.io/commands/flushdb/|valkey.io} for more details.
*
* @param mode - The flushing mode, could be either {@link FlushMode.SYNC} or {@link FlushMode.ASYNC}.
* @returns `OK`.
* @param mode - (Optional) The flushing mode, could be either {@link FlushMode.SYNC} or {@link FlushMode.ASYNC}.
* @returns `"OK"`.
*
* @example
* ```typescript
* const result = await client.flushdb(FlushMode.SYNC);
* console.log(result); // Output: 'OK'
* ```
*/
public async flushdb(mode?: FlushMode): Promise<string> {
return this.createWritePromise(createFlushDB(mode));
public async flushdb(mode?: FlushMode): Promise<"OK"> {
return this.createWritePromise(createFlushDB(mode), {
decoder: Decoder.String,
});
}

/**
Expand Down
Loading

0 comments on commit 82cbd33

Please sign in to comment.