Skip to content

Commit

Permalink
Signed-off-by: Yury-Fridlyand <[email protected]>
Browse files Browse the repository at this point in the history
  • Loading branch information
Yury-Fridlyand committed Sep 5, 2024
1 parent 5497328 commit efa54d9
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 29 deletions.
15 changes: 3 additions & 12 deletions node/src/BaseClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -376,33 +376,24 @@ export function glideRecordToRecord<T>(

/**
* @internal
* Check whether an object is of type `GlideRecord`.
* Check whether an object is a `GlideRecord` (see {@link GlideRecord}).
*/
function isGlideRecord(obj?: unknown): boolean {
return (
obj !== undefined &&
obj !== null &&
Array.isArray(obj) &&
obj.length > 0 &&
typeof obj[0] === "object" &&
!Array.isArray(obj[0]) &&
"key" in obj[0] &&
"value" in obj[0]
);
}

/**
* @internal
* Check whether an object is of type `GlideRecord[]`.
* Check whether an object is a `GlideRecord[]` (see {@link GlideRecord}).
*/
function isGlideRecordArray(obj?: unknown): boolean {
return (
obj !== undefined &&
obj !== null &&
Array.isArray(obj) &&
obj.length > 0 &&
isGlideRecord(obj[0])
);
return Array.isArray(obj) && obj.length > 0 && isGlideRecord(obj[0]);
}

/** Represents the return type of {@link xinfoStream} command. */
Expand Down
32 changes: 15 additions & 17 deletions node/src/GlideClusterClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,10 @@ function convertClusterGlideRecord<T>(
route?: Routes,
): ClusterResponse<T> {
const isSingleNodeResponse =
(route === undefined && isRoutedToSingleNodeByDefault) ||
(route !== undefined && isSingleNodeRoute(route));
// route not given and command is routed by default to a random node
(!route && isRoutedToSingleNodeByDefault) ||
// or route is given and it is a single node route
(Boolean(route) && route !== "allPrimaries" && route !== "allNodes");

return isSingleNodeResponse
? (res as T)
Expand Down Expand Up @@ -257,14 +259,6 @@ export type SingleNodeRoute =
| SlotKeyTypes
| RouteByAddress;

/**
* @internal
* Detect whether the route is a single node route.
*/
function isSingleNodeRoute(route: Routes): boolean {
return route !== "allPrimaries" && route !== "allNodes";
}

function toProtobufRoute(
route: Routes | undefined,
): command_request.Routes | undefined {
Expand Down Expand Up @@ -844,6 +838,8 @@ export class GlideClusterClient extends BaseClient {
/**
* Invokes a previously loaded function.
*
* The command will be routed to a random node, unless `route` is provided.
*
* @see {@link https://valkey.io/commands/fcall/|valkey.io} for details.
* @remarks Since Valkey version 7.0.0.
*
Expand All @@ -861,20 +857,22 @@ export class GlideClusterClient extends BaseClient {
public async fcallWithRoute(
func: GlideString,
args: GlideString[],
options: RouteOption & DecoderOption,
options?: RouteOption & DecoderOption,
): Promise<ClusterResponse<ReturnType>> {
return this.createWritePromise<ClusterGlideRecord<ReturnType>>(
createFCall(func, [], args),
{
route: toProtobufRoute(options.route),
route: toProtobufRoute(options?.route),
decoder: options?.decoder,
},
).then((res) => convertClusterGlideRecord(res, true, options.route));
).then((res) => convertClusterGlideRecord(res, true, options?.route));
}

/**
* Invokes a previously loaded read-only function.
*
* The command will be routed to a random node, unless `route` is provided.
*
* @see {@link https://valkey.io/commands/fcall/|valkey.io} for details.
* @remarks Since Valkey version 7.0.0.
*
Expand All @@ -893,15 +891,15 @@ export class GlideClusterClient extends BaseClient {
public async fcallReadonlyWithRoute(
func: GlideString,
args: GlideString[],
options: RouteOption & DecoderOption,
options?: RouteOption & DecoderOption,
): Promise<ClusterResponse<ReturnType>> {
return this.createWritePromise<ClusterGlideRecord<ReturnType>>(
createFCallReadOnly(func, [], args),
{
route: toProtobufRoute(options.route),
route: toProtobufRoute(options?.route),
decoder: options?.decoder,
},
).then((res) => convertClusterGlideRecord(res, true, options.route));
).then((res) => convertClusterGlideRecord(res, true, options?.route));
}

/**
Expand Down Expand Up @@ -1042,7 +1040,7 @@ export class GlideClusterClient extends BaseClient {
((res as GlideRecord<unknown>[]).map(
glideRecordToRecord,
) as FunctionListResponse)
: // multi node respose
: // multi node response
glideRecordToRecord(
res as GlideRecord<unknown>,
)) as ClusterResponse<FunctionListResponse>),
Expand Down

0 comments on commit efa54d9

Please sign in to comment.