@valkey/valkey-glide 1.2.0-rc9 → 1.2.1-rc0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -3,8 +3,8 @@
3
3
  */
4
4
  import { ClusterScanCursor, Script } from "glide-rs";
5
5
  import * as net from "net";
6
- import { BaseClient, BaseClientConfiguration, DecoderOption, GlideReturnType, GlideString, PubSubMsg } from "./BaseClient";
7
- import { FlushMode, FunctionListOptions, FunctionListResponse, FunctionRestorePolicy, FunctionStatsSingleResponse, InfoOptions, LolwutOptions, ScanOptions } from "./Commands";
6
+ import { AdvancedBaseClientConfiguration, BaseClient, BaseClientConfiguration, DecoderOption, GlideReturnType, GlideString, PubSubMsg } from "./BaseClient";
7
+ import { FlushMode, FunctionListOptions, FunctionListResponse, FunctionRestorePolicy, FunctionStatsSingleResponse, InfoOptions, LolwutOptions, ClusterScanOptions } from "./Commands";
8
8
  import { command_request, connection_request } from "./ProtobufMessage";
9
9
  import { ClusterTransaction } from "./Transaction";
10
10
  /** An extension to command option types with {@link Routes}. */
@@ -59,6 +59,9 @@ export declare namespace GlideClusterClientConfiguration {
59
59
  */
60
60
  Sharded = 2
61
61
  }
62
+ /**
63
+ * Configuration for Pub/Sub subscriptions that the client will establish upon connection.
64
+ */
62
65
  interface PubSubSubscriptions {
63
66
  /**
64
67
  * Channels and patterns by modes.
@@ -74,6 +77,39 @@ export declare namespace GlideClusterClientConfiguration {
74
77
  context?: any;
75
78
  }
76
79
  }
80
+ /**
81
+ * Configuration options for creating a {@link GlideClusterClient | GlideClusterClient}.
82
+ *
83
+ * Extends {@link BaseClientConfiguration | BaseClientConfiguration} with properties specific to `GlideClusterClient`, such as periodic topology checks
84
+ * and Pub/Sub subscription settings.
85
+ *
86
+ * @remarks
87
+ * This configuration allows you to tailor the client's behavior when connecting to a Valkey GLIDE Cluster.
88
+ *
89
+ * - **Periodic Topology Checks**: Use `periodicChecks` to configure how the client performs periodic checks to detect changes in the cluster's topology.
90
+ * - `"enabledDefaultConfigs"`: Enables periodic checks with default configurations.
91
+ * - `"disabled"`: Disables periodic topology checks.
92
+ * - `{ duration_in_sec: number }`: Manually configure the interval for periodic checks.
93
+ * - **Pub/Sub Subscriptions**: Predefine Pub/Sub channels and patterns to subscribe to upon connection establishment.
94
+ * - Supports exact channels, patterns, and sharded channels (available since Valkey version 7.0).
95
+ *
96
+ * @example
97
+ * ```typescript
98
+ * const config: GlideClusterClientConfiguration = {
99
+ * periodicChecks: {
100
+ * duration_in_sec: 30, // Perform periodic checks every 30 seconds
101
+ * },
102
+ * pubsubSubscriptions: {
103
+ * channelsAndPatterns: {
104
+ * [GlideClusterClientConfiguration.PubSubChannelModes.Pattern]: new Set(['cluster.*']),
105
+ * },
106
+ * callback: (msg) => {
107
+ * console.log(`Received message on ${msg.channel}:`, msg.payload);
108
+ * },
109
+ * },
110
+ * };
111
+ * ```
112
+ */
77
113
  export type GlideClusterClientConfiguration = BaseClientConfiguration & {
78
114
  /**
79
115
  * Configure the periodic topology checks.
@@ -87,12 +123,59 @@ export type GlideClusterClientConfiguration = BaseClientConfiguration & {
87
123
  * Will be applied via SUBSCRIBE/PSUBSCRIBE/SSUBSCRIBE commands during connection establishment.
88
124
  */
89
125
  pubsubSubscriptions?: GlideClusterClientConfiguration.PubSubSubscriptions;
126
+ /**
127
+ * Advanced configuration settings for the client.
128
+ */
129
+ advancedConfiguration?: AdvancedGlideClusterClientConfiguration;
90
130
  };
131
+ /**
132
+ * Represents advanced configuration settings for creating a {@link GlideClusterClient | GlideClusterClient} used in {@link GlideClusterClientConfiguration | GlideClusterClientConfiguration}.
133
+ *
134
+ *
135
+ * @example
136
+ * ```typescript
137
+ * const config: AdvancedGlideClusterClientConfiguration = {
138
+ * connectionTimeout: 500, // Set the connection timeout to 500ms
139
+ * };
140
+ * ```
141
+ */
142
+ export type AdvancedGlideClusterClientConfiguration = AdvancedBaseClientConfiguration & {};
91
143
  /**
92
144
  * If the command's routing is to one node we will get T as a response type,
93
145
  * otherwise, we will get a dictionary of address: nodeResponse, address is of type string and nodeResponse is of type T.
94
146
  */
95
147
  export type ClusterResponse<T> = T | Record<string, T>;
148
+ /**
149
+ * Routing configuration for commands based on a specific slot ID in a Valkey cluster.
150
+ *
151
+ * @remarks
152
+ * This interface allows you to specify routing of a command to a node responsible for a particular slot ID in the cluster.
153
+ * Valkey clusters use hash slots to distribute data across multiple shards. There are 16,384 slots in total, and each shard
154
+ * manages a range of slots.
155
+ *
156
+ * - **Slot ID**: A number between 0 and 16383 representing a hash slot.
157
+ * - **Routing Type**:
158
+ * - `"primarySlotId"`: Routes the command to the primary node responsible for the specified slot ID.
159
+ * - `"replicaSlotId"`: Routes the command to a replica node responsible for the specified slot ID, overriding the `readFrom` configuration.
160
+ *
161
+ * @example
162
+ * ```typescript
163
+ * // Route command to the primary node responsible for slot ID 12345
164
+ * const routeBySlotId: SlotIdTypes = {
165
+ * type: "primarySlotId",
166
+ * id: 12345,
167
+ * };
168
+ *
169
+ * // Route command to a replica node responsible for slot ID 12345
170
+ * const routeToReplicaBySlotId: SlotIdTypes = {
171
+ * type: "replicaSlotId",
172
+ * id: 12345,
173
+ * };
174
+ *
175
+ * // Use the routing configuration when executing a command
176
+ * const result = await client.get("mykey", { route: routeBySlotId });
177
+ * ```
178
+ */
96
179
  export interface SlotIdTypes {
97
180
  /**
98
181
  * `replicaSlotId` overrides the `readFrom` configuration. If it's used the request
@@ -105,6 +188,36 @@ export interface SlotIdTypes {
105
188
  */
106
189
  id: number;
107
190
  }
191
+ /**
192
+ * Routing configuration for commands based on a key in a Valkey cluster.
193
+ *
194
+ * @remarks
195
+ * This interface allows you to specify routing of a command to a node responsible for the slot that a specific key hashes to.
196
+ * Valkey clusters use consistent hashing to map keys to hash slots, which are then managed by different shards in the cluster.
197
+ *
198
+ * - **Key**: The key whose hash slot will determine the routing of the command.
199
+ * - **Routing Type**:
200
+ * - `"primarySlotKey"`: Routes the command to the primary node responsible for the key's slot.
201
+ * - `"replicaSlotKey"`: Routes the command to a replica node responsible for the key's slot, overriding the `readFrom` configuration.
202
+ *
203
+ * @example
204
+ * ```typescript
205
+ * // Route command to the primary node responsible for the key's slot
206
+ * const routeByKey: SlotKeyTypes = {
207
+ * type: "primarySlotKey",
208
+ * key: "user:1001",
209
+ * };
210
+ *
211
+ * // Route command to a replica node responsible for the key's slot
212
+ * const routeToReplicaByKey: SlotKeyTypes = {
213
+ * type: "replicaSlotKey",
214
+ * key: "user:1001",
215
+ * };
216
+ *
217
+ * // Use the routing configuration when executing a command
218
+ * const result = await client.get("user:1001", { route: routeByKey });
219
+ * ```
220
+ */
108
221
  export interface SlotKeyTypes {
109
222
  /**
110
223
  * `replicaSlotKey` overrides the `readFrom` configuration. If it's used the request
@@ -116,6 +229,39 @@ export interface SlotKeyTypes {
116
229
  */
117
230
  key: string;
118
231
  }
232
+ /**
233
+ * Routing configuration to send a command to a specific node by its address and port.
234
+ *
235
+ * @remarks
236
+ * This interface allows you to specify routing of a command to a node in the Valkey cluster by providing its network address and port.
237
+ * It's useful when you need to direct a command to a particular node.
238
+ *
239
+ * - **Type**: Must be set to `"routeByAddress"` to indicate that the routing should be based on the provided address.
240
+ * - **Host**: The endpoint of the node.
241
+ * - If `port` is not provided, `host` should be in the format `${address}:${port}`, where `address` is the preferred endpoint as shown in the output of the `CLUSTER SLOTS` command.
242
+ * - If `port` is provided, `host` should be the address or hostname of the node without the port.
243
+ * - **Port**: (Optional) The port to access on the node.
244
+ * - If `port` is not provided, `host` is assumed to include the port number.
245
+ *
246
+ * @example
247
+ * ```typescript
248
+ * // Route command to a node at '192.168.1.10:6379'
249
+ * const routeByAddress: RouteByAddress = {
250
+ * type: "routeByAddress",
251
+ * host: "192.168.1.10",
252
+ * port: 6379,
253
+ * };
254
+ *
255
+ * // Alternatively, include the port in the host string
256
+ * const routeByAddressWithPortInHost: RouteByAddress = {
257
+ * type: "routeByAddress",
258
+ * host: "192.168.1.10:6379",
259
+ * };
260
+ *
261
+ * // Use the routing configuration when executing a command
262
+ * const result = await client.ping({ route: routeByAddress });
263
+ * ```
264
+ */
119
265
  export interface RouteByAddress {
120
266
  type: "routeByAddress";
121
267
  /**
@@ -127,6 +273,52 @@ export interface RouteByAddress {
127
273
  */
128
274
  port?: number;
129
275
  }
276
+ /**
277
+ * Defines the routing configuration for a command in a Valkey cluster.
278
+ *
279
+ * @remarks
280
+ * The `Routes` type allows you to specify how a command should be routed in a Valkey cluster.
281
+ * Commands can be routed to a single node or broadcast to multiple nodes depending on the routing strategy.
282
+ *
283
+ * **Routing Options**:
284
+ *
285
+ * - **Single Node Routing** (`SingleNodeRoute`):
286
+ * - **"randomNode"**: Route the command to a random node in the cluster.
287
+ * - **`SlotIdTypes`**: Route based on a specific slot ID.
288
+ * - **`SlotKeyTypes`**: Route based on the slot of a specific key.
289
+ * - **`RouteByAddress`**: Route to a specific node by its address and port.
290
+ * - **Broadcast Routing**:
291
+ * - **"allPrimaries"**: Route the command to all primary nodes in the cluster.
292
+ * - **"allNodes"**: Route the command to all nodes (both primaries and replicas) in the cluster.
293
+ *
294
+ * @example
295
+ * ```typescript
296
+ * // Route command to a random node
297
+ * const routeRandom: Routes = "randomNode";
298
+ *
299
+ * // Route command to all primary nodes
300
+ * const routeAllPrimaries: Routes = "allPrimaries";
301
+ *
302
+ * // Route command to all nodes
303
+ * const routeAllNodes: Routes = "allNodes";
304
+ *
305
+ * // Route command to a node by slot key
306
+ * const routeByKey: Routes = {
307
+ * type: "primarySlotKey",
308
+ * key: "myKey",
309
+ * };
310
+ *
311
+ * // Route command to a specific node by address
312
+ * const routeByAddress: Routes = {
313
+ * type: "routeByAddress",
314
+ * host: "192.168.1.10",
315
+ * port: 6379,
316
+ * };
317
+ *
318
+ * // Use the routing configuration when executing a command
319
+ * const result = await client.ping({ route: routeByAddress });
320
+ * ```
321
+ */
130
322
  export type Routes = SingleNodeRoute
131
323
  /**
132
324
  * Route request to all primary nodes.
@@ -136,6 +328,48 @@ export type Routes = SingleNodeRoute
136
328
  * Route request to all nodes.
137
329
  */
138
330
  | "allNodes";
331
+ /**
332
+ * Defines the routing configuration to a single node in the Valkey cluster.
333
+ *
334
+ * @remarks
335
+ * The `SingleNodeRoute` type allows you to specify routing of a command to a single node in the cluster.
336
+ * This can be based on various criteria such as a random node, a node responsible for a specific slot, or a node identified by its address.
337
+ *
338
+ * **Options**:
339
+ *
340
+ * - **"randomNode"**: Route the command to a random node in the cluster.
341
+ * - **`SlotIdTypes`**: Route to the node responsible for a specific slot ID.
342
+ * - **`SlotKeyTypes`**: Route to the node responsible for the slot of a specific key.
343
+ * - **`RouteByAddress`**: Route to a specific node by its address and port.
344
+ *
345
+ * @example
346
+ * ```typescript
347
+ * // Route to a random node
348
+ * const routeRandomNode: SingleNodeRoute = "randomNode";
349
+ *
350
+ * // Route based on slot ID
351
+ * const routeBySlotId: SingleNodeRoute = {
352
+ * type: "primarySlotId",
353
+ * id: 12345,
354
+ * };
355
+ *
356
+ * // Route based on key
357
+ * const routeByKey: SingleNodeRoute = {
358
+ * type: "primarySlotKey",
359
+ * key: "myKey",
360
+ * };
361
+ *
362
+ * // Route to a specific node by address
363
+ * const routeByAddress: SingleNodeRoute = {
364
+ * type: "routeByAddress",
365
+ * host: "192.168.1.10",
366
+ * port: 6379,
367
+ * };
368
+ *
369
+ * // Use the routing configuration when executing a command
370
+ * const result = await client.get("myKey", { route: routeByKey });
371
+ * ```
372
+ */
139
373
  export type SingleNodeRoute =
140
374
  /**
141
375
  * Route request to a random node.
@@ -159,16 +393,64 @@ export declare class GlideClusterClient extends BaseClient {
159
393
  * @internal
160
394
  */
161
395
  protected createClientRequest(options: GlideClusterClientConfiguration): connection_request.IConnectionRequest;
396
+ /**
397
+ * Creates a new `GlideClusterClient` instance and establishes connections to a Valkey GLIDE Cluster.
398
+ *
399
+ * @param options - The configuration options for the client, including cluster addresses, authentication credentials, TLS settings, periodic checks, and Pub/Sub subscriptions.
400
+ * @returns A promise that resolves to a connected `GlideClusterClient` instance.
401
+ *
402
+ * @remarks
403
+ * Use this static method to create and connect a `GlideClusterClient` to a Valkey GLIDE Cluster. The client will automatically handle connection establishment, including cluster topology discovery and handling of authentication and TLS configurations.
404
+ *
405
+ * ### Example - Connecting to a Cluster
406
+ * ```typescript
407
+ * import { GlideClusterClient, GlideClusterClientConfiguration } from '@valkey/valkey-glide';
408
+ *
409
+ * const client = await GlideClusterClient.createClient({
410
+ * addresses: [
411
+ * { host: 'address1.example.com', port: 6379 },
412
+ * { host: 'address2.example.com', port: 6379 },
413
+ * ],
414
+ * credentials: {
415
+ * username: 'user1',
416
+ * password: 'passwordA',
417
+ * },
418
+ * useTLS: true,
419
+ * periodicChecks: {
420
+ * duration_in_sec: 30, // Perform periodic checks every 30 seconds
421
+ * },
422
+ * pubsubSubscriptions: {
423
+ * channelsAndPatterns: {
424
+ * [GlideClusterClientConfiguration.PubSubChannelModes.Exact]: new Set(['updates']),
425
+ * [GlideClusterClientConfiguration.PubSubChannelModes.Sharded]: new Set(['sharded_channel']),
426
+ * },
427
+ * callback: (msg) => {
428
+ * console.log(`Received message: ${msg.payload}`);
429
+ * },
430
+ * },
431
+ * });
432
+ * ```
433
+ *
434
+ * @remarks
435
+ * - **Cluster Topology Discovery**: The client will automatically discover the cluster topology based on the seed addresses provided.
436
+ * - **Authentication**: If `credentials` are provided, the client will attempt to authenticate using the specified username and password.
437
+ * - **TLS**: If `useTLS` is set to `true`, the client will establish secure connections using TLS.
438
+ * - **Periodic Checks**: The `periodicChecks` setting allows you to configure how often the client checks for cluster topology changes.
439
+ * - **Pub/Sub Subscriptions**: Any channels or patterns specified in `pubsubSubscriptions` will be subscribed to upon connection.
440
+ */
162
441
  static createClient(options: GlideClusterClientConfiguration): Promise<GlideClusterClient>;
442
+ /**
443
+ * @internal
444
+ */
163
445
  static __createClient(options: BaseClientConfiguration, connectedSocket: net.Socket): Promise<GlideClusterClient>;
164
446
  /**
165
447
  * @internal
166
448
  */
167
- protected scanOptionsToProto(cursor: string, options?: ScanOptions): command_request.ClusterScan;
449
+ protected scanOptionsToProto(cursor: string, options?: ClusterScanOptions): command_request.ClusterScan;
168
450
  /**
169
451
  * @internal
170
452
  */
171
- protected createClusterScanPromise(cursor: ClusterScanCursor, options?: ScanOptions & DecoderOption): Promise<[ClusterScanCursor, GlideString[]]>;
453
+ protected createClusterScanPromise(cursor: ClusterScanCursor, options?: ClusterScanOptions & DecoderOption): Promise<[ClusterScanCursor, GlideString[]]>;
172
454
  /**
173
455
  * Incrementally iterates over the keys in the Cluster.
174
456
  *
@@ -189,7 +471,7 @@ export declare class GlideClusterClient extends BaseClient {
189
471
  *
190
472
  * @param cursor - The cursor object that wraps the scan state.
191
473
  * To start a new scan, create a new empty `ClusterScanCursor` using {@link ClusterScanCursor}.
192
- * @param options - (Optional) The scan options, see {@link ScanOptions} and {@link DecoderOption}.
474
+ * @param options - (Optional) The scan options, see {@link ClusterScanOptions} and {@link DecoderOption}.
193
475
  * @returns A Promise resolving to an array containing the next cursor and an array of keys,
194
476
  * formatted as [`ClusterScanCursor`, `string[]`].
195
477
  *
@@ -207,14 +489,14 @@ export declare class GlideClusterClient extends BaseClient {
207
489
  * console.log(allKeys); // ["key1", "key2", "key3"]
208
490
  *
209
491
  * // Iterate over keys matching a pattern
210
- * await client.mset([{key: "key1", value: "value1"}, {key: "key2", value: "value2"}, {key: "notMykey", value: "value3"}, {key: "somethingElse", value: "value4"}]);
492
+ * await client.mset([{key: "key1", value: "value1"}, {key: "key2", value: "value2"}, {key: "notMyKey", value: "value3"}, {key: "somethingElse", value: "value4"}]);
211
493
  * let cursor = new ClusterScanCursor();
212
494
  * const matchedKeys: GlideString[] = [];
213
495
  * while (!cursor.isFinished()) {
214
496
  * const [cursor, keys] = await client.scan(cursor, { match: "*key*", count: 10 });
215
497
  * matchedKeys.push(...keys);
216
498
  * }
217
- * console.log(matchedKeys); // ["key1", "key2", "notMykey"]
499
+ * console.log(matchedKeys); // ["key1", "key2", "notMyKey"]
218
500
  *
219
501
  * // Iterate over keys of a specific type
220
502
  * await client.mset([{key: "key1", value: "value1"}, {key: "key2", value: "value2"}, {key: "key3", value: "value3"}]);
@@ -228,7 +510,7 @@ export declare class GlideClusterClient extends BaseClient {
228
510
  * console.log(stringKeys); // ["key1", "key2", "key3"]
229
511
  * ```
230
512
  */
231
- scan(cursor: ClusterScanCursor, options?: ScanOptions & DecoderOption): Promise<[ClusterScanCursor, GlideString[]]>;
513
+ scan(cursor: ClusterScanCursor, options?: ClusterScanOptions & DecoderOption): Promise<[ClusterScanCursor, GlideString[]]>;
232
514
  /** Executes a single command, without checking inputs. Every part of the command, including subcommands,
233
515
  * should be added as a separate value in args.
234
516
  * The command will be routed automatically based on the passed command's default request policy, unless `route` is provided,
@@ -444,7 +726,7 @@ export declare class GlideClusterClient extends BaseClient {
444
726
  * @example
445
727
  * ```typescript
446
728
  * // Example usage of configSet method to set multiple configuration parameters
447
- * const result = await client.configSet({ timeout: "1000", maxmemory, "1GB" });
729
+ * const result = await client.configSet({ timeout: "1000", maxmemory: "1GB" });
448
730
  * console.log(result); // Output: 'OK'
449
731
  * ```
450
732
  */
@@ -995,7 +1277,7 @@ export declare class GlideClusterClient extends BaseClient {
995
1277
  * @example
996
1278
  * ```typescript
997
1279
  * const luaScript = new Script("return { ARGV[1] }");
998
- * const result = await invokeScript(luaScript, { args: ["bar"] });
1280
+ * const result = await client.invokeScript(luaScript, { args: ["bar"] });
999
1281
  * console.log(result); // Output: ['bar']
1000
1282
  * ```
1001
1283
  */