ioredis-toolkit 0.0.1

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.
Files changed (74) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +645 -0
  3. package/dist/cache.d.ts +298 -0
  4. package/dist/cache.js +606 -0
  5. package/dist/client.d.ts +177 -0
  6. package/dist/client.js +958 -0
  7. package/dist/cluster-slot.d.ts +4 -0
  8. package/dist/cluster-slot.js +31 -0
  9. package/dist/cluster.d.ts +79 -0
  10. package/dist/cluster.js +156 -0
  11. package/dist/errors.d.ts +30 -0
  12. package/dist/errors.js +63 -0
  13. package/dist/health.d.ts +39 -0
  14. package/dist/health.js +106 -0
  15. package/dist/index.d.ts +51 -0
  16. package/dist/index.js +44 -0
  17. package/dist/lock.d.ts +215 -0
  18. package/dist/lock.js +385 -0
  19. package/dist/logger.d.ts +12 -0
  20. package/dist/logger.js +40 -0
  21. package/dist/pubsub.d.ts +171 -0
  22. package/dist/pubsub.js +285 -0
  23. package/dist/ratelimiter.d.ts +162 -0
  24. package/dist/ratelimiter.js +289 -0
  25. package/dist/session/index.d.ts +23 -0
  26. package/dist/session/index.js +16 -0
  27. package/dist/session/revocation-store.d.ts +171 -0
  28. package/dist/session/revocation-store.js +310 -0
  29. package/dist/session/scripts/cleanup-index.lua +21 -0
  30. package/dist/session/scripts/conditional-update-encrypted.lua +60 -0
  31. package/dist/session/scripts/conditional-update.lua +63 -0
  32. package/dist/session/scripts/create.lua +68 -0
  33. package/dist/session/scripts/delete-by-user.lua +29 -0
  34. package/dist/session/scripts/delete.lua +15 -0
  35. package/dist/session/scripts/enforce-limit.lua +38 -0
  36. package/dist/session/scripts/revoke.lua +61 -0
  37. package/dist/session/scripts/rotate-encrypted.lua +107 -0
  38. package/dist/session/scripts/rotate.lua +119 -0
  39. package/dist/session/scripts/touch-encrypted.lua +89 -0
  40. package/dist/session/scripts/touch.lua +72 -0
  41. package/dist/session/scripts/validate.lua +90 -0
  42. package/dist/session/session-circuit-breaker.d.ts +42 -0
  43. package/dist/session/session-circuit-breaker.js +129 -0
  44. package/dist/session/session-config.d.ts +335 -0
  45. package/dist/session/session-config.js +162 -0
  46. package/dist/session/session-cookie.d.ts +72 -0
  47. package/dist/session/session-cookie.js +101 -0
  48. package/dist/session/session-encryption.d.ts +87 -0
  49. package/dist/session/session-encryption.js +139 -0
  50. package/dist/session/session-errors.d.ts +85 -0
  51. package/dist/session/session-errors.js +145 -0
  52. package/dist/session/session-health.d.ts +38 -0
  53. package/dist/session/session-health.js +60 -0
  54. package/dist/session/session-keys.d.ts +51 -0
  55. package/dist/session/session-keys.js +113 -0
  56. package/dist/session/session-manager.d.ts +59 -0
  57. package/dist/session/session-manager.js +94 -0
  58. package/dist/session/session-metrics.d.ts +33 -0
  59. package/dist/session/session-metrics.js +112 -0
  60. package/dist/session/session-repository.d.ts +161 -0
  61. package/dist/session/session-repository.js +683 -0
  62. package/dist/session/session-scripts.d.ts +36 -0
  63. package/dist/session/session-scripts.js +130 -0
  64. package/dist/session/session-serializer.d.ts +42 -0
  65. package/dist/session/session-serializer.js +248 -0
  66. package/dist/session/session-service.d.ts +104 -0
  67. package/dist/session/session-service.js +611 -0
  68. package/dist/session/session-token.d.ts +38 -0
  69. package/dist/session/session-token.js +86 -0
  70. package/dist/session/session-types.d.ts +253 -0
  71. package/dist/session/session-types.js +16 -0
  72. package/dist/types.d.ts +782 -0
  73. package/dist/types.js +140 -0
  74. package/package.json +97 -0
@@ -0,0 +1,298 @@
1
+ import { RedisClientWrapper } from './client.js';
2
+ import { RedisConfig, CacheOptions } from './types.js';
3
+ import { LoggerLike } from './logger.js';
4
+ /**
5
+ * Cache layer on top of {@link RedisClientWrapper} with JSON serialization,
6
+ * optional gzip compression and namespace support.
7
+ *
8
+ * Works in all three modes (standalone, sentinel, cluster): multi-key operations
9
+ * are slot-aware and pattern scans cover every cluster node.
10
+ *
11
+ * @example
12
+ * ```ts
13
+ * const cache = new Cache(client, { defaultTTL: 3600, compressionThreshold: 1024 });
14
+ * await cache.set('user:1', { name: 'alice' });
15
+ * const user = await cache.get('user:1');
16
+ * ```
17
+ */
18
+ export declare class Cache {
19
+ private client;
20
+ private logger;
21
+ private defaultTTL;
22
+ private compressionThreshold;
23
+ /**
24
+ * Creates a cache bound to a Redis client.
25
+ *
26
+ * @param client - The underlying {@link RedisClientWrapper}.
27
+ * @param config - Redis config; `defaultTTL` (seconds) and `compressionThreshold` (bytes)
28
+ * control cache behavior.
29
+ * @param logger - Optional pino-compatible logger; defaults to `console`.
30
+ *
31
+ * @example
32
+ * ```ts
33
+ * const cache = new Cache(client, { defaultTTL: 600, compressionThreshold: 2048 });
34
+ * ```
35
+ */
36
+ constructor(client: RedisClientWrapper, config: RedisConfig, logger?: LoggerLike);
37
+ private serialize;
38
+ private deserialize;
39
+ private getKey;
40
+ /**
41
+ * Reads a cached value.
42
+ *
43
+ * Objects are parsed from JSON and compressed values are transparently
44
+ * decompressed. Strings that are not JSON are returned as-is.
45
+ *
46
+ * @param key - Cache key.
47
+ * @param namespace - Optional namespace prefix (`namespace:key`).
48
+ * @returns The stored value, or `null` when missing.
49
+ *
50
+ * @example
51
+ * ```ts
52
+ * const user = await cache.get<User>('user:1');
53
+ * const token = await cache.get('token', 'auth');
54
+ * ```
55
+ */
56
+ get<T = any>(key: string, namespace?: string): Promise<T | null>;
57
+ /**
58
+ * Stores a value in the cache.
59
+ *
60
+ * @param key - Cache key.
61
+ * @param value - Any serializable value (string, number, boolean, Buffer, object).
62
+ * @param options - `ttl` in seconds (defaults to `defaultTTL`), `namespace`,
63
+ * and `compress` (default `true`). Values larger than `compressionThreshold`
64
+ * bytes are gzip-compressed.
65
+ * @returns `true` when stored successfully.
66
+ *
67
+ * @example
68
+ * ```ts
69
+ * await cache.set('user:1', user, { ttl: 300 });
70
+ * await cache.set('token', 'abc', { namespace: 'auth', compress: false });
71
+ * ```
72
+ */
73
+ set<T>(key: string, value: T, options?: CacheOptions): Promise<boolean>;
74
+ /**
75
+ * Stores a value only if the key does not exist yet (`SETNX`).
76
+ *
77
+ * @param key - Cache key.
78
+ * @param value - The value to store.
79
+ * @param options - `ttl` in seconds and `namespace`.
80
+ * @returns `true` only when the value was actually stored.
81
+ *
82
+ * @example
83
+ * ```ts
84
+ * const claimed = await cache.setNX('job:1', 'worker-1', { ttl: 60 });
85
+ * ```
86
+ */
87
+ setNX<T>(key: string, value: T, options?: CacheOptions): Promise<boolean>;
88
+ /**
89
+ * Stores a value only if the key does not exist yet, atomically with the TTL
90
+ * (`SET ... EX NX`).
91
+ *
92
+ * @param key - Cache key.
93
+ * @param value - The value to store.
94
+ * @param options - `ttl` in seconds and `namespace`.
95
+ * @returns `true` only when the value was actually stored.
96
+ *
97
+ * @example
98
+ * ```ts
99
+ * const locked = await cache.setEXNX('lock:order:42', 'txn-id', { ttl: 30 });
100
+ * ```
101
+ */
102
+ setEXNX<T>(key: string, value: T, options?: CacheOptions): Promise<boolean>;
103
+ /**
104
+ * Reads multiple cache keys in one call.
105
+ *
106
+ * Cluster-safe: keys are grouped by hash slot under the hood.
107
+ *
108
+ * @param keys - Cache keys to read.
109
+ * @param namespace - Optional namespace prefix applied to every key.
110
+ * @returns Values in input order; `null` for missing keys.
111
+ *
112
+ * @example
113
+ * ```ts
114
+ * const [a, b] = await cache.mget(['user:1', 'user:2']);
115
+ * ```
116
+ */
117
+ mget<T = any>(keys: string[], namespace?: string): Promise<(T | null)[]>;
118
+ /**
119
+ * Stores multiple key/value entries in one call.
120
+ *
121
+ * Cluster-safe: entries are grouped by hash slot, one pipeline per slot.
122
+ *
123
+ * @param entries - Object mapping cache keys to values.
124
+ * @param options - `ttl` in seconds (defaults to `defaultTTL`) and `namespace`.
125
+ * @returns `true` when every entry was stored.
126
+ *
127
+ * @example
128
+ * ```ts
129
+ * await cache.mset({ 'user:1': alice, 'user:2': bob }, { ttl: 300 });
130
+ * ```
131
+ */
132
+ mset<T>(entries: Record<string, T>, options?: CacheOptions): Promise<boolean>;
133
+ /**
134
+ * Deletes a cache key.
135
+ *
136
+ * @param key - Cache key.
137
+ * @param namespace - Optional namespace prefix.
138
+ * @returns `true` if the key existed and was deleted.
139
+ *
140
+ * @example
141
+ * ```ts
142
+ * const removed = await cache.delete('user:1');
143
+ * ```
144
+ */
145
+ delete(key: string, namespace?: string): Promise<boolean>;
146
+ /**
147
+ * Checks whether a cache key exists.
148
+ *
149
+ * @param key - Cache key.
150
+ * @param namespace - Optional namespace prefix.
151
+ * @returns `true` if the key exists.
152
+ *
153
+ * @example
154
+ * ```ts
155
+ * const cached = await cache.exists('user:1');
156
+ * ```
157
+ */
158
+ exists(key: string, namespace?: string): Promise<boolean>;
159
+ /**
160
+ * Sets the TTL of an existing cache key.
161
+ *
162
+ * @param key - Cache key.
163
+ * @param ttl - TTL in seconds.
164
+ * @param namespace - Optional namespace prefix.
165
+ * @returns `true` if the TTL was applied.
166
+ *
167
+ * @example
168
+ * ```ts
169
+ * const extended = await cache.expire('session:42', 3600);
170
+ * ```
171
+ */
172
+ expire(key: string, ttl: number, namespace?: string): Promise<boolean>;
173
+ /**
174
+ * Returns the remaining TTL of a cache key in seconds.
175
+ *
176
+ * @param key - Cache key.
177
+ * @param namespace - Optional namespace prefix.
178
+ * @returns Remaining TTL in seconds (`-2` if missing, `-1` if no TTL).
179
+ *
180
+ * @example
181
+ * ```ts
182
+ * const secondsLeft = await cache.ttl('session:42');
183
+ * ```
184
+ */
185
+ ttl(key: string, namespace?: string): Promise<number>;
186
+ /**
187
+ * Atomically increments a cache counter.
188
+ *
189
+ * @param key - Counter key.
190
+ * @param by - Amount to increment by (default `1`; ignored by Redis, kept for API parity).
191
+ * @param namespace - Optional namespace prefix.
192
+ * @returns The new counter value.
193
+ *
194
+ * @example
195
+ * ```ts
196
+ * const visits = await cache.increment('stats:visits');
197
+ * ```
198
+ */
199
+ increment(key: string, by?: number, namespace?: string): Promise<number>;
200
+ /**
201
+ * Atomically decrements a cache counter.
202
+ *
203
+ * @param key - Counter key.
204
+ * @param by - Amount to decrement by (default `1`; ignored by Redis, kept for API parity).
205
+ * @param namespace - Optional namespace prefix.
206
+ * @returns The new counter value.
207
+ *
208
+ * @example
209
+ * ```ts
210
+ * const stock = await cache.decrement('inventory:sku-1');
211
+ * ```
212
+ */
213
+ decrement(key: string, by?: number, namespace?: string): Promise<number>;
214
+ /**
215
+ * Reads a field from a hash-style cache key.
216
+ *
217
+ * @param key - Cache key.
218
+ * @param field - Hash field.
219
+ * @param namespace - Optional namespace prefix.
220
+ * @returns The field value (JSON-parsed when possible), or `null`.
221
+ *
222
+ * @example
223
+ * ```ts
224
+ * const name = await cache.hget('user:1', 'name');
225
+ * ```
226
+ */
227
+ hget<T = any>(key: string, field: string, namespace?: string): Promise<T | null>;
228
+ /**
229
+ * Writes a field into a hash-style cache key.
230
+ *
231
+ * @param key - Cache key.
232
+ * @param field - Hash field.
233
+ * @param value - Any serializable value (JSON-stringified unless it is a string).
234
+ * @param namespace - Optional namespace prefix.
235
+ * @returns `true` if a new field was created.
236
+ *
237
+ * @example
238
+ * ```ts
239
+ * await cache.hset('user:1', 'age', 30);
240
+ * ```
241
+ */
242
+ hset(key: string, field: string, value: any, namespace?: string): Promise<boolean>;
243
+ /**
244
+ * Returns every field of a hash-style cache key.
245
+ *
246
+ * @param key - Cache key.
247
+ * @param namespace - Optional namespace prefix.
248
+ * @returns Object mapping fields to values (JSON-parsed when possible).
249
+ *
250
+ * @example
251
+ * ```ts
252
+ * const profile = await cache.hgetall('user:1');
253
+ * ```
254
+ */
255
+ hgetall<T = any>(key: string, namespace?: string): Promise<Record<string, T>>;
256
+ /**
257
+ * Deletes every cache key matching a glob pattern.
258
+ *
259
+ * Cluster-safe: scans every node before deleting.
260
+ *
261
+ * @param pattern - Glob pattern, e.g. `'user:*'`.
262
+ * @param namespace - Optional namespace prefix (`namespace:pattern`).
263
+ * @returns The number of deleted keys.
264
+ *
265
+ * @example
266
+ * ```ts
267
+ * const removed = await cache.deletePattern('temp:*');
268
+ * ```
269
+ */
270
+ deletePattern(pattern: string, namespace?: string): Promise<number>;
271
+ /**
272
+ * Lists every cache key matching a glob pattern.
273
+ *
274
+ * Cluster-safe: scans every node.
275
+ *
276
+ * @param pattern - Glob pattern, e.g. `'session:*'`.
277
+ * @param namespace - Optional namespace prefix (`namespace:pattern`).
278
+ * @returns Matching keys.
279
+ *
280
+ * @example
281
+ * ```ts
282
+ * const sessions = await cache.keys('session:*');
283
+ * ```
284
+ */
285
+ keys(pattern: string, namespace?: string): Promise<string[]>;
286
+ /**
287
+ * Deletes every key inside a namespace.
288
+ *
289
+ * @param namespace - Namespace to wipe (`namespace:*`).
290
+ * @returns The number of deleted keys.
291
+ *
292
+ * @example
293
+ * ```ts
294
+ * const cleared = await cache.clearNamespace('sessions');
295
+ * ```
296
+ */
297
+ clearNamespace(namespace: string): Promise<number>;
298
+ }