Skip to content

Commit

Permalink
Node: add binary variant to function commands (valkey-io#2172)
Browse files Browse the repository at this point in the history
* Node: add binary variant to function commands

---------

Signed-off-by: Yi-Pin Chen <[email protected]>
  • Loading branch information
yipin-chen authored Aug 26, 2024
1 parent e1811c4 commit 610ff42
Show file tree
Hide file tree
Showing 9 changed files with 295 additions and 215 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* Node: Added binary variant to geo commands ([#2149](https://github.com/valkey-io/valkey-glide/pull/2149))
* Node: Added binary variant to HYPERLOGLOG commands ([#2176](https://github.com/valkey-io/valkey-glide/pull/2176))
* Node: Added FUNCTION DUMP and FUNCTION RESTORE commands ([#2129](https://github.com/valkey-io/valkey-glide/pull/2129), [#2173](https://github.com/valkey-io/valkey-glide/pull/2173))
* Node: Added binary variant to FUNCTION commands ([#2172](https://github.com/valkey-io/valkey-glide/pull/2172))
* Node: Added ZUNIONSTORE command ([#2145](https://github.com/valkey-io/valkey-glide/pull/2145))
* Node: Added XREADGROUP command ([#2124](https://github.com/valkey-io/valkey-glide/pull/2124))
* Node: Added XINFO GROUPS command ([#2122](https://github.com/valkey-io/valkey-glide/pull/2122))
Expand Down
30 changes: 20 additions & 10 deletions node/src/BaseClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5682,6 +5682,8 @@ export class BaseClient {
* @param keys - A list of `keys` accessed by the function. To ensure the correct execution of functions,
* all names of keys that a function accesses must be explicitly provided as `keys`.
* @param args - A list of `function` arguments and it should not represent names of keys.
* @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 The invoked function's return value.
*
* @example
Expand All @@ -5691,11 +5693,14 @@ export class BaseClient {
* ```
*/
public async fcall(
func: string,
keys: string[],
args: string[],
): Promise<string> {
return this.createWritePromise(createFCall(func, keys, args));
func: GlideString,
keys: GlideString[],
args: GlideString[],
decoder?: Decoder,
): Promise<ReturnType> {
return this.createWritePromise(createFCall(func, keys, args), {
decoder,
});
}

/**
Expand All @@ -5709,6 +5714,8 @@ export class BaseClient {
* @param keys - A list of `keys` accessed by the function. To ensure the correct execution of functions,
* all names of keys that a function accesses must be explicitly provided as `keys`.
* @param args - A list of `function` arguments and it should not represent names of keys.
* @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 The invoked function's return value.
*
* @example
Expand All @@ -5719,11 +5726,14 @@ export class BaseClient {
* ```
*/
public async fcallReadonly(
func: string,
keys: string[],
args: string[],
): Promise<string> {
return this.createWritePromise(createFCallReadOnly(func, keys, args));
func: GlideString,
keys: GlideString[],
args: GlideString[],
decoder?: Decoder,
): Promise<ReturnType> {
return this.createWritePromise(createFCallReadOnly(func, keys, args), {
decoder,
});
}

/**
Expand Down
40 changes: 24 additions & 16 deletions node/src/Commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2239,33 +2239,41 @@ export function createBLPop(
* @internal
*/
export function createFCall(
func: string,
keys: string[],
args: string[],
func: GlideString,
keys: GlideString[],
args: GlideString[],
): command_request.Command {
let params: string[] = [];
params = params.concat(func, keys.length.toString(), keys, args);
const params: GlideString[] = [
func,
keys.length.toString(),
...keys,
...args,
];
return createCommand(RequestType.FCall, params);
}

/**
* @internal
*/
export function createFCallReadOnly(
func: string,
keys: string[],
args: string[],
func: GlideString,
keys: GlideString[],
args: GlideString[],
): command_request.Command {
let params: string[] = [];
params = params.concat(func, keys.length.toString(), keys, args);
const params: GlideString[] = [
func,
keys.length.toString(),
...keys,
...args,
];
return createCommand(RequestType.FCallReadOnly, params);
}

/**
* @internal
*/
export function createFunctionDelete(
libraryCode: string,
libraryCode: GlideString,
): command_request.Command {
return createCommand(RequestType.FunctionDelete, [libraryCode]);
}
Expand All @@ -2285,7 +2293,7 @@ export function createFunctionFlush(mode?: FlushMode): command_request.Command {
* @internal
*/
export function createFunctionLoad(
libraryCode: string,
libraryCode: GlideString,
replace?: boolean,
): command_request.Command {
const args = replace ? ["REPLACE", libraryCode] : [libraryCode];
Expand All @@ -2295,15 +2303,15 @@ export function createFunctionLoad(
/** Optional arguments for `FUNCTION LIST` command. */
export type FunctionListOptions = {
/** A wildcard pattern for matching library names. */
libNamePattern?: string;
libNamePattern?: GlideString;
/** Specifies whether to request the library code from the server or not. */
withCode?: boolean;
};

/** Type of the response of `FUNCTION LIST` command. */
export type FunctionListResponse = Record<
string,
string | Record<string, string | string[]>[]
GlideString | Record<string, GlideString | GlideString[]>[]
>[];

/**
Expand All @@ -2312,7 +2320,7 @@ export type FunctionListResponse = Record<
export function createFunctionList(
options?: FunctionListOptions,
): command_request.Command {
const args: string[] = [];
const args: GlideString[] = [];

if (options) {
if (options.libNamePattern) {
Expand All @@ -2335,7 +2343,7 @@ export function createFunctionList(
export type FunctionStatsSingleResponse = Record<
string,
| null
| Record<string, string | string[] | number> // Running function/script information
| Record<string, GlideString | GlideString[] | number> // Running function/script information
| Record<string, Record<string, number>> // Execution engines information
>;

Expand Down
55 changes: 36 additions & 19 deletions node/src/GlideClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -558,16 +558,18 @@ export class GlideClient extends BaseClient {
* @remarks Since Valkey version 7.0.0.
*
* @param libraryCode - The library name to delete.
* @returns A simple OK response.
* @returns A simple `"OK"` response.
*
* @example
* ```typescript
* const result = await client.functionDelete("libName");
* console.log(result); // Output: 'OK'
* ```
*/
public async functionDelete(libraryCode: string): Promise<string> {
return this.createWritePromise(createFunctionDelete(libraryCode));
public async functionDelete(libraryCode: GlideString): Promise<"OK"> {
return this.createWritePromise(createFunctionDelete(libraryCode), {
decoder: Decoder.String,
});
}

/**
Expand All @@ -577,8 +579,10 @@ export class GlideClient extends BaseClient {
* @remarks Since Valkey version 7.0.0.
*
* @param libraryCode - The source code that implements the library.
* @param replace - Whether the given library should overwrite a library with the same name if it
* @param options - (Optional) Additional parameters:
* - (Optional) `replace`: Whether the given library should overwrite a library with the same name if it
* already exists.
* - (Optional) `decoder`: see {@link DecoderOption}.
* @returns The library name that was loaded.
*
* @example
Expand All @@ -589,11 +593,12 @@ export class GlideClient extends BaseClient {
* ```
*/
public async functionLoad(
libraryCode: string,
replace?: boolean,
): Promise<string> {
libraryCode: GlideString,
options?: { replace?: boolean } & DecoderOption,
): Promise<GlideString> {
return this.createWritePromise(
createFunctionLoad(libraryCode, replace),
createFunctionLoad(libraryCode, options?.replace),
{ decoder: options?.decoder },
);
}

Expand All @@ -603,17 +608,19 @@ export class GlideClient extends BaseClient {
* @see {@link https://valkey.io/commands/function-flush/|valkey.io} for details.
* @remarks Since Valkey version 7.0.0.
*
* @param mode - The flushing mode, could be either {@link FlushMode.SYNC} or {@link FlushMode.ASYNC}.
* @returns A simple OK response.
* @param mode - (Optional) The flushing mode, could be either {@link FlushMode.SYNC} or {@link FlushMode.ASYNC}.
* @returns A simple `"OK"` response.
*
* @example
* ```typescript
* const result = await client.functionFlush(FlushMode.SYNC);
* console.log(result); // Output: 'OK'
* ```
*/
public async functionFlush(mode?: FlushMode): Promise<string> {
return this.createWritePromise(createFunctionFlush(mode));
public async functionFlush(mode?: FlushMode): Promise<"OK"> {
return this.createWritePromise(createFunctionFlush(mode), {
decoder: Decoder.String,
});
}

/**
Expand All @@ -622,7 +629,7 @@ export class GlideClient extends BaseClient {
* @see {@link https://valkey.io/commands/function-list/|valkey.io} for details.
* @remarks Since Valkey version 7.0.0.
*
* @param options - Parameters to filter and request additional info.
* @param options - (Optional) See {@link FunctionListOptions} and {@link DecoderOption}.
* @returns Info about all or selected libraries and their functions in {@link FunctionListResponse} format.
*
* @example
Expand All @@ -645,9 +652,11 @@ export class GlideClient extends BaseClient {
* ```
*/
public async functionList(
options?: FunctionListOptions,
options?: FunctionListOptions & DecoderOption,
): Promise<FunctionListResponse> {
return this.createWritePromise(createFunctionList(options));
return this.createWritePromise(createFunctionList(options), {
decoder: options?.decoder,
});
}

/**
Expand All @@ -660,6 +669,8 @@ export class GlideClient extends BaseClient {
* @see {@link https://valkey.io/commands/function-stats/|valkey.io} for details.
* @remarks Since Valkey version 7.0.0.
*
* @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 Record where the key is the node address and the value is a Record with two keys:
* - `"running_script"`: Information about the running script, or `null` if no script is running.
* - `"engines"`: Information about available engines and their stats.
Expand Down Expand Up @@ -694,8 +705,12 @@ export class GlideClient extends BaseClient {
* // }
* ```
*/
public async functionStats(): Promise<FunctionStatsFullResponse> {
return this.createWritePromise(createFunctionStats());
public async functionStats(
decoder?: Decoder,
): Promise<FunctionStatsFullResponse> {
return this.createWritePromise(createFunctionStats(), {
decoder,
});
}

/**
Expand All @@ -706,14 +721,16 @@ export class GlideClient extends BaseClient {
* @see {@link https://valkey.io/commands/function-kill/|valkey.io} for details.
* @remarks Since Valkey version 7.0.0.
*
* @returns `OK` if function is terminated. Otherwise, throws an error.
* @returns `"OK"` if function is terminated. Otherwise, throws an error.
* @example
* ```typescript
* await client.functionKill();
* ```
*/
public async functionKill(): Promise<"OK"> {
return this.createWritePromise(createFunctionKill());
return this.createWritePromise(createFunctionKill(), {
decoder: Decoder.String,
});
}

/**
Expand Down
Loading

0 comments on commit 610ff42

Please sign in to comment.