@forklaunch/infrastructure-redis 1.5.0 → 1.6.0
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.
- package/lib/eject/infrastructure/redis.ts +123 -23
- package/lib/index.d.ts +30 -1
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +87 -20
- package/lib/index.js.map +1 -1
- package/lib/index.mjs +85 -19
- package/lib/index.mjs.map +1 -1
- package/package.json +7 -7
|
@@ -36,6 +36,48 @@ export interface RedisCacheEncryptionOptions {
|
|
|
36
36
|
encryptor: FieldEncryptor;
|
|
37
37
|
}
|
|
38
38
|
|
|
39
|
+
/** Env vars the platform sets for an app on a shared (partitioned) Redis. */
|
|
40
|
+
const KEY_PREFIX_VARS = [
|
|
41
|
+
'REDIS_KEY_PREFIX',
|
|
42
|
+
'FORKLAUNCH_REDIS_KEY_PREFIX'
|
|
43
|
+
] as const;
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* The app's key prefix on a shared Redis, without a trailing separator, or
|
|
47
|
+
* undefined on a dedicated cache (local Redis, own ElastiCache).
|
|
48
|
+
*
|
|
49
|
+
* On a shared pool each app is an ACL user whose key pattern is
|
|
50
|
+
* `~<prefix>:*`; a key outside it is answered NOPERM. The platform passes the
|
|
51
|
+
* prefix as `REDIS_KEY_PREFIX` (`FORKLAUNCH_REDIS_KEY_PREFIX` is the same
|
|
52
|
+
* value under the platform's own name).
|
|
53
|
+
*/
|
|
54
|
+
/** `value` trimmed, with any trailing `:` separators removed. */
|
|
55
|
+
function bareKeyPrefix(value: string): string {
|
|
56
|
+
let end = value.length;
|
|
57
|
+
while (end > 0 && value[end - 1] === ':') end--;
|
|
58
|
+
return value.slice(0, end);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export function redisKeyPrefix(
|
|
62
|
+
env: Record<string, string | undefined> = process.env
|
|
63
|
+
): string | undefined {
|
|
64
|
+
for (const name of KEY_PREFIX_VARS) {
|
|
65
|
+
const value = bareKeyPrefix(env[name]?.trim() ?? '');
|
|
66
|
+
if (value) return value;
|
|
67
|
+
}
|
|
68
|
+
return undefined;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** Options beyond the Redis client's own. */
|
|
72
|
+
export interface RedisTtlCacheOptions {
|
|
73
|
+
/**
|
|
74
|
+
* Prefix for every key and queue name, joined with `:`. Defaults to the
|
|
75
|
+
* platform's `REDIS_KEY_PREFIX`, so an app on a shared Redis stays inside
|
|
76
|
+
* its namespace without configuring anything. Pass `''` to turn it off.
|
|
77
|
+
*/
|
|
78
|
+
keyPrefix?: string;
|
|
79
|
+
}
|
|
80
|
+
|
|
39
81
|
/**
|
|
40
82
|
* Class representing a Redis-based TTL (Time-To-Live) cache.
|
|
41
83
|
* Implements the TtlCache interface to provide caching functionality with automatic expiration.
|
|
@@ -47,6 +89,8 @@ export class RedisTtlCache implements TtlCache {
|
|
|
47
89
|
private client;
|
|
48
90
|
private telemetryOptions;
|
|
49
91
|
private encryptor?: FieldEncryptor;
|
|
92
|
+
/** `<prefix>:` or '' — every key and queue name the client sees starts with it. */
|
|
93
|
+
private readonly keyPrefix: string;
|
|
50
94
|
|
|
51
95
|
/**
|
|
52
96
|
* Creates an instance of RedisTtlCache.
|
|
@@ -56,14 +100,20 @@ export class RedisTtlCache implements TtlCache {
|
|
|
56
100
|
* @param {RedisClientOptions} options - Configuration options for the Redis client
|
|
57
101
|
* @param {TelemetryOptions} telemetryOptions - Configuration options for telemetry
|
|
58
102
|
* @param {RedisCacheEncryptionOptions} encryption - Encryption configuration
|
|
103
|
+
* @param {RedisTtlCacheOptions} [cacheOptions] - Key prefix (defaults to the platform's `REDIS_KEY_PREFIX`)
|
|
59
104
|
*/
|
|
60
105
|
constructor(
|
|
61
106
|
private ttlMilliseconds: number,
|
|
62
107
|
private openTelemetryCollector: OpenTelemetryCollector<MetricsDefinition>,
|
|
63
108
|
options: RedisClientOptions,
|
|
64
109
|
telemetryOptions: TelemetryOptions,
|
|
65
|
-
encryption: RedisCacheEncryptionOptions
|
|
110
|
+
encryption: RedisCacheEncryptionOptions,
|
|
111
|
+
cacheOptions: RedisTtlCacheOptions = {}
|
|
66
112
|
) {
|
|
113
|
+
const prefix = bareKeyPrefix(
|
|
114
|
+
(cacheOptions.keyPrefix ?? redisKeyPrefix() ?? '').trim()
|
|
115
|
+
);
|
|
116
|
+
this.keyPrefix = prefix ? `${prefix}:` : '';
|
|
67
117
|
this.telemetryOptions = evaluateTelemetryOptions(telemetryOptions);
|
|
68
118
|
this.client = createClient(options);
|
|
69
119
|
this.encryptor = encryption.encryptor;
|
|
@@ -73,6 +123,46 @@ export class RedisTtlCache implements TtlCache {
|
|
|
73
123
|
}
|
|
74
124
|
}
|
|
75
125
|
|
|
126
|
+
// ---------------------------------------------------------------------------
|
|
127
|
+
// Key prefix — callers use bare keys; Redis only ever sees prefixed ones
|
|
128
|
+
// ---------------------------------------------------------------------------
|
|
129
|
+
|
|
130
|
+
private withPrefix(key: string): string {
|
|
131
|
+
return this.keyPrefix + key;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
private withoutPrefix(key: string): string {
|
|
135
|
+
return this.keyPrefix && key.startsWith(this.keyPrefix)
|
|
136
|
+
? key.slice(this.keyPrefix.length)
|
|
137
|
+
: key;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/** Prefixed keys for a key list, or every key under a (bare) key prefix. */
|
|
141
|
+
private async resolveKeys(
|
|
142
|
+
keysOrPrefix: string[] | string
|
|
143
|
+
): Promise<string[]> {
|
|
144
|
+
return Array.isArray(keysOrPrefix)
|
|
145
|
+
? keysOrPrefix.map((key) => this.withPrefix(key))
|
|
146
|
+
: this.scanKeys(this.withPrefix(keysOrPrefix));
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* Every key starting with `prefix` (already prefixed), via SCAN. KEYS is in
|
|
151
|
+
* Redis's `@dangerous` category, which shared-pool ACLs deny, and it blocks
|
|
152
|
+
* the server while it walks the whole keyspace.
|
|
153
|
+
*/
|
|
154
|
+
private async scanKeys(prefix: string): Promise<string[]> {
|
|
155
|
+
const keys = new Set<string>();
|
|
156
|
+
for await (const batch of this.client.scanIterator({
|
|
157
|
+
// same glob semantics KEYS had: the caller's prefix may itself be a pattern
|
|
158
|
+
MATCH: `${prefix}*`,
|
|
159
|
+
COUNT: 500
|
|
160
|
+
})) {
|
|
161
|
+
for (const key of batch) keys.add(String(key));
|
|
162
|
+
}
|
|
163
|
+
return [...keys];
|
|
164
|
+
}
|
|
165
|
+
|
|
76
166
|
// ---------------------------------------------------------------------------
|
|
77
167
|
// Encryption helpers — only active when compliance context is provided
|
|
78
168
|
// ---------------------------------------------------------------------------
|
|
@@ -150,7 +240,7 @@ export class RedisTtlCache implements TtlCache {
|
|
|
150
240
|
this.openTelemetryCollector.info(`Putting record into cache: ${key}`);
|
|
151
241
|
}
|
|
152
242
|
await this.client.set(
|
|
153
|
-
key,
|
|
243
|
+
this.withPrefix(key),
|
|
154
244
|
this.encryptValue(safeStringify(value), compliance),
|
|
155
245
|
{ PX: ttlMilliseconds }
|
|
156
246
|
);
|
|
@@ -170,7 +260,7 @@ export class RedisTtlCache implements TtlCache {
|
|
|
170
260
|
);
|
|
171
261
|
}
|
|
172
262
|
const result = await this.client.set(
|
|
173
|
-
key,
|
|
263
|
+
this.withPrefix(key),
|
|
174
264
|
this.encryptValue(safeStringify(value), compliance),
|
|
175
265
|
{ PX: ttlMilliseconds, NX: true }
|
|
176
266
|
);
|
|
@@ -184,7 +274,7 @@ export class RedisTtlCache implements TtlCache {
|
|
|
184
274
|
const multiCommand = this.client.multi();
|
|
185
275
|
for (const { key, value, ttlMilliseconds } of cacheRecords) {
|
|
186
276
|
multiCommand.set(
|
|
187
|
-
key,
|
|
277
|
+
this.withPrefix(key),
|
|
188
278
|
this.encryptValue(safeStringify(value), compliance),
|
|
189
279
|
{ PX: ttlMilliseconds || this.ttlMilliseconds }
|
|
190
280
|
);
|
|
@@ -198,7 +288,7 @@ export class RedisTtlCache implements TtlCache {
|
|
|
198
288
|
compliance?: ComplianceContext
|
|
199
289
|
): Promise<void> {
|
|
200
290
|
await this.client.lPush(
|
|
201
|
-
queueName,
|
|
291
|
+
this.withPrefix(queueName),
|
|
202
292
|
this.encryptValue(safeStringify(value), compliance)
|
|
203
293
|
);
|
|
204
294
|
}
|
|
@@ -211,7 +301,7 @@ export class RedisTtlCache implements TtlCache {
|
|
|
211
301
|
const multiCommand = this.client.multi();
|
|
212
302
|
for (const value of values) {
|
|
213
303
|
multiCommand.lPush(
|
|
214
|
-
queueName,
|
|
304
|
+
this.withPrefix(queueName),
|
|
215
305
|
this.encryptValue(safeStringify(value), compliance)
|
|
216
306
|
);
|
|
217
307
|
}
|
|
@@ -219,13 +309,13 @@ export class RedisTtlCache implements TtlCache {
|
|
|
219
309
|
}
|
|
220
310
|
|
|
221
311
|
async deleteRecord(cacheRecordKey: string): Promise<void> {
|
|
222
|
-
await this.client.del(cacheRecordKey);
|
|
312
|
+
await this.client.del(this.withPrefix(cacheRecordKey));
|
|
223
313
|
}
|
|
224
314
|
|
|
225
315
|
async deleteBatchRecords(cacheRecordKeys: string[]): Promise<void> {
|
|
226
316
|
const multiCommand = this.client.multi();
|
|
227
317
|
for (const key of cacheRecordKeys) {
|
|
228
|
-
multiCommand.del(key);
|
|
318
|
+
multiCommand.del(this.withPrefix(key));
|
|
229
319
|
}
|
|
230
320
|
await multiCommand.exec();
|
|
231
321
|
}
|
|
@@ -234,7 +324,7 @@ export class RedisTtlCache implements TtlCache {
|
|
|
234
324
|
queueName: string,
|
|
235
325
|
compliance?: ComplianceContext
|
|
236
326
|
): Promise<T> {
|
|
237
|
-
const value = await this.client.rPop(queueName);
|
|
327
|
+
const value = await this.client.rPop(this.withPrefix(queueName));
|
|
238
328
|
if (value === null) {
|
|
239
329
|
throw new Error(`Queue is empty: ${queueName}`);
|
|
240
330
|
}
|
|
@@ -248,7 +338,7 @@ export class RedisTtlCache implements TtlCache {
|
|
|
248
338
|
): Promise<T[]> {
|
|
249
339
|
const multiCommand = this.client.multi();
|
|
250
340
|
for (let i = 0; i < pageSize; i++) {
|
|
251
|
-
multiCommand.rPop(queueName);
|
|
341
|
+
multiCommand.rPop(this.withPrefix(queueName));
|
|
252
342
|
}
|
|
253
343
|
const values = await multiCommand.exec();
|
|
254
344
|
return values
|
|
@@ -267,8 +357,8 @@ export class RedisTtlCache implements TtlCache {
|
|
|
267
357
|
): Promise<TtlCacheRecord<T>> {
|
|
268
358
|
const [value, ttl] = await this.client
|
|
269
359
|
.multi()
|
|
270
|
-
.get(cacheRecordKey)
|
|
271
|
-
.ttl(cacheRecordKey)
|
|
360
|
+
.get(this.withPrefix(cacheRecordKey))
|
|
361
|
+
.ttl(this.withPrefix(cacheRecordKey))
|
|
272
362
|
.exec();
|
|
273
363
|
if (value === null) {
|
|
274
364
|
throw new Error(`Record not found for key: ${cacheRecordKey}`);
|
|
@@ -295,9 +385,7 @@ export class RedisTtlCache implements TtlCache {
|
|
|
295
385
|
cacheRecordKeysOrPrefix: string[] | string,
|
|
296
386
|
compliance?: ComplianceContext
|
|
297
387
|
): Promise<TtlCacheRecord<T>[]> {
|
|
298
|
-
const keys =
|
|
299
|
-
? cacheRecordKeysOrPrefix
|
|
300
|
-
: await this.client.keys(cacheRecordKeysOrPrefix + '*');
|
|
388
|
+
const keys = await this.resolveKeys(cacheRecordKeysOrPrefix);
|
|
301
389
|
const multiCommand = this.client.multi();
|
|
302
390
|
for (const key of keys) {
|
|
303
391
|
multiCommand.get(key);
|
|
@@ -318,7 +406,7 @@ export class RedisTtlCache implements TtlCache {
|
|
|
318
406
|
);
|
|
319
407
|
if (maybeValue && ttl) {
|
|
320
408
|
acc.push({
|
|
321
|
-
key: keys[index / 2],
|
|
409
|
+
key: this.withoutPrefix(keys[index / 2]),
|
|
322
410
|
value: maybeValue,
|
|
323
411
|
ttlMilliseconds: ttl * 1000
|
|
324
412
|
});
|
|
@@ -329,20 +417,19 @@ export class RedisTtlCache implements TtlCache {
|
|
|
329
417
|
}
|
|
330
418
|
|
|
331
419
|
async listKeys(pattern_prefix: string): Promise<string[]> {
|
|
332
|
-
|
|
420
|
+
const keys = await this.scanKeys(this.withPrefix(pattern_prefix));
|
|
421
|
+
return keys.map((key) => this.withoutPrefix(key));
|
|
333
422
|
}
|
|
334
423
|
|
|
335
424
|
async peekRecord(cacheRecordKey: string): Promise<boolean> {
|
|
336
|
-
const result = await this.client.exists(cacheRecordKey);
|
|
425
|
+
const result = await this.client.exists(this.withPrefix(cacheRecordKey));
|
|
337
426
|
return result === 1;
|
|
338
427
|
}
|
|
339
428
|
|
|
340
429
|
async peekBatchRecords(
|
|
341
430
|
cacheRecordKeysOrPrefix: string[] | string
|
|
342
431
|
): Promise<boolean[]> {
|
|
343
|
-
const keys =
|
|
344
|
-
? cacheRecordKeysOrPrefix
|
|
345
|
-
: await this.client.keys(cacheRecordKeysOrPrefix + '*');
|
|
432
|
+
const keys = await this.resolveKeys(cacheRecordKeysOrPrefix);
|
|
346
433
|
const multiCommand = this.client.multi();
|
|
347
434
|
for (const key of keys) {
|
|
348
435
|
multiCommand.exists(key);
|
|
@@ -358,7 +445,7 @@ export class RedisTtlCache implements TtlCache {
|
|
|
358
445
|
// Queues use lPush + rPop, so the next item to dequeue lives at the
|
|
359
446
|
// tail of the list, not the head. Reading lRange(0, 0) would return the
|
|
360
447
|
// most-recently-pushed item — the opposite of dequeue order.
|
|
361
|
-
const value = await this.client.lRange(queueName, -1, -1);
|
|
448
|
+
const value = await this.client.lRange(this.withPrefix(queueName), -1, -1);
|
|
362
449
|
return this.parseValue<T>(value[0], compliance);
|
|
363
450
|
}
|
|
364
451
|
|
|
@@ -370,7 +457,11 @@ export class RedisTtlCache implements TtlCache {
|
|
|
370
457
|
// Tail-relative range: the last `pageSize` items, where the very last
|
|
371
458
|
// item is the next to be dequeued. Redis returns them in list order
|
|
372
459
|
// (oldest-tail-end first), so reverse to put next-to-dequeue first.
|
|
373
|
-
const values = await this.client.lRange(
|
|
460
|
+
const values = await this.client.lRange(
|
|
461
|
+
this.withPrefix(queueName),
|
|
462
|
+
-pageSize,
|
|
463
|
+
-1
|
|
464
|
+
);
|
|
374
465
|
if (values.length === 0) return [];
|
|
375
466
|
return values
|
|
376
467
|
.reverse()
|
|
@@ -382,10 +473,19 @@ export class RedisTtlCache implements TtlCache {
|
|
|
382
473
|
await this.client.quit();
|
|
383
474
|
}
|
|
384
475
|
|
|
476
|
+
/** The prefix (with its trailing `:`) applied to every key, or ''. */
|
|
477
|
+
getKeyPrefix(): string {
|
|
478
|
+
return this.keyPrefix;
|
|
479
|
+
}
|
|
480
|
+
|
|
385
481
|
getTtlMilliseconds(): number {
|
|
386
482
|
return this.ttlMilliseconds;
|
|
387
483
|
}
|
|
388
484
|
|
|
485
|
+
/**
|
|
486
|
+
* The raw node-redis client. Commands sent through it bypass the key
|
|
487
|
+
* prefix: on a shared Redis, prefix keys with `getKeyPrefix()` yourself.
|
|
488
|
+
*/
|
|
389
489
|
getClient(): typeof this.client {
|
|
390
490
|
return this.client;
|
|
391
491
|
}
|
package/lib/index.d.ts
CHANGED
|
@@ -10,6 +10,16 @@ export interface RedisCacheEncryptionOptions {
|
|
|
10
10
|
/** The FieldEncryptor instance to use for encrypting cache values. */
|
|
11
11
|
encryptor: FieldEncryptor;
|
|
12
12
|
}
|
|
13
|
+
export declare function redisKeyPrefix(env?: Record<string, string | undefined>): string | undefined;
|
|
14
|
+
/** Options beyond the Redis client's own. */
|
|
15
|
+
export interface RedisTtlCacheOptions {
|
|
16
|
+
/**
|
|
17
|
+
* Prefix for every key and queue name, joined with `:`. Defaults to the
|
|
18
|
+
* platform's `REDIS_KEY_PREFIX`, so an app on a shared Redis stays inside
|
|
19
|
+
* its namespace without configuring anything. Pass `''` to turn it off.
|
|
20
|
+
*/
|
|
21
|
+
keyPrefix?: string;
|
|
22
|
+
}
|
|
13
23
|
/**
|
|
14
24
|
* Class representing a Redis-based TTL (Time-To-Live) cache.
|
|
15
25
|
* Implements the TtlCache interface to provide caching functionality with automatic expiration.
|
|
@@ -23,6 +33,8 @@ export declare class RedisTtlCache implements TtlCache {
|
|
|
23
33
|
private client;
|
|
24
34
|
private telemetryOptions;
|
|
25
35
|
private encryptor?;
|
|
36
|
+
/** `<prefix>:` or '' — every key and queue name the client sees starts with it. */
|
|
37
|
+
private readonly keyPrefix;
|
|
26
38
|
/**
|
|
27
39
|
* Creates an instance of RedisTtlCache.
|
|
28
40
|
*
|
|
@@ -31,8 +43,19 @@ export declare class RedisTtlCache implements TtlCache {
|
|
|
31
43
|
* @param {RedisClientOptions} options - Configuration options for the Redis client
|
|
32
44
|
* @param {TelemetryOptions} telemetryOptions - Configuration options for telemetry
|
|
33
45
|
* @param {RedisCacheEncryptionOptions} encryption - Encryption configuration
|
|
46
|
+
* @param {RedisTtlCacheOptions} [cacheOptions] - Key prefix (defaults to the platform's `REDIS_KEY_PREFIX`)
|
|
34
47
|
*/
|
|
35
|
-
constructor(ttlMilliseconds: number, openTelemetryCollector: OpenTelemetryCollector<MetricsDefinition>, options: RedisClientOptions, telemetryOptions: TelemetryOptions, encryption: RedisCacheEncryptionOptions);
|
|
48
|
+
constructor(ttlMilliseconds: number, openTelemetryCollector: OpenTelemetryCollector<MetricsDefinition>, options: RedisClientOptions, telemetryOptions: TelemetryOptions, encryption: RedisCacheEncryptionOptions, cacheOptions?: RedisTtlCacheOptions);
|
|
49
|
+
private withPrefix;
|
|
50
|
+
private withoutPrefix;
|
|
51
|
+
/** Prefixed keys for a key list, or every key under a (bare) key prefix. */
|
|
52
|
+
private resolveKeys;
|
|
53
|
+
/**
|
|
54
|
+
* Every key starting with `prefix` (already prefixed), via SCAN. KEYS is in
|
|
55
|
+
* Redis's `@dangerous` category, which shared-pool ACLs deny, and it blocks
|
|
56
|
+
* the server while it walks the whole keyspace.
|
|
57
|
+
*/
|
|
58
|
+
private scanKeys;
|
|
36
59
|
private encryptValue;
|
|
37
60
|
private decryptValue;
|
|
38
61
|
private parseValue;
|
|
@@ -57,7 +80,13 @@ export declare class RedisTtlCache implements TtlCache {
|
|
|
57
80
|
peekQueueRecord<T>(queueName: string, compliance?: ComplianceContext): Promise<T>;
|
|
58
81
|
peekQueueRecords<T>(queueName: string, pageSize: number, compliance?: ComplianceContext): Promise<T[]>;
|
|
59
82
|
disconnect(): Promise<void>;
|
|
83
|
+
/** The prefix (with its trailing `:`) applied to every key, or ''. */
|
|
84
|
+
getKeyPrefix(): string;
|
|
60
85
|
getTtlMilliseconds(): number;
|
|
86
|
+
/**
|
|
87
|
+
* The raw node-redis client. Commands sent through it bypass the key
|
|
88
|
+
* prefix: on a shared Redis, prefix keys with `getKeyPrefix()` yourself.
|
|
89
|
+
*/
|
|
61
90
|
getClient(): typeof this.client;
|
|
62
91
|
}
|
|
63
92
|
//# sourceMappingURL=index.d.ts.map
|
package/lib/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":"AACA,OAAO,EACL,KAAK,iBAAiB,EACtB,QAAQ,EACR,cAAc,EACf,MAAM,wBAAwB,CAAC;AAChC,OAAO,EAEL,iBAAiB,EACjB,sBAAsB,EACtB,gBAAgB,EACjB,MAAM,uBAAuB,CAAC;AAC/B,OAAO,EAAE,KAAK,cAAc,EAAE,MAAM,8BAA8B,CAAC;AACnE,OAAO,EAAgB,kBAAkB,EAAE,MAAM,OAAO,CAAC;AAgBzD;;;GAGG;AACH,MAAM,WAAW,2BAA2B;IAC1C,sEAAsE;IACtE,SAAS,EAAE,cAAc,CAAC;CAC3B;AAED;;;;;;GAMG;AACH,qBAAa,aAAc,YAAW,QAAQ;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":"AACA,OAAO,EACL,KAAK,iBAAiB,EACtB,QAAQ,EACR,cAAc,EACf,MAAM,wBAAwB,CAAC;AAChC,OAAO,EAEL,iBAAiB,EACjB,sBA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CAAC,CAAC,CAyB5B;IAEK,gBAAgB,CAAC,CAAC,EACtB,uBAAuB,EAAE,MAAM,EAAE,GAAG,MAAM,EAC1C,UAAU,CAAC,EAAE,iBAAiB,GAC7B,OAAO,CAAC,cAAc,CAAC,CAAC,CAAC,EAAE,CAAC,CA8B9B;IAEK,QAAQ,CAAC,cAAc,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC,CAGxD;IAEK,UAAU,CAAC,cAAc,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC,CAGzD;IAEK,gBAAgB,CACpB,uBAAuB,EAAE,MAAM,EAAE,GAAG,MAAM,GACzC,OAAO,CAAC,OAAO,EAAE,CAAC,CAQpB;IAEK,eAAe,CAAC,CAAC,EACrB,SAAS,EAAE,MAAM,EACjB,UAAU,CAAC,EAAE,iBAAiB,GAC7B,OAAO,CAAC,CAAC,CAAC,CAMZ;IAEK,gBAAgB,CAAC,CAAC,EACtB,SAAS,EAAE,MAAM,EACjB,QAAQ,EAAE,MAAM,EAChB,UAAU,CAAC,EAAE,iBAAiB,GAC7B,OAAO,CAAC,CAAC,EAAE,CAAC,CAcd;IAEK,UAAU,IAAI,OAAO,CAAC,IAAI,CAAC,CAEhC;IAED,sEAAsE;IACtE,YAAY,IAAI,MAAM,CAErB;IAED,kBAAkB,IAAI,MAAM,CAE3B;IAED;;;OAGG;IACH,SAAS,IAAI,OAAO,IAAI,CAAC,MAAM,CAE9B;CACF"}
|
package/lib/index.js
CHANGED
|
@@ -20,7 +20,8 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
20
20
|
// index.ts
|
|
21
21
|
var index_exports = {};
|
|
22
22
|
__export(index_exports, {
|
|
23
|
-
RedisTtlCache: () => RedisTtlCache
|
|
23
|
+
RedisTtlCache: () => RedisTtlCache,
|
|
24
|
+
redisKeyPrefix: () => redisKeyPrefix
|
|
24
25
|
});
|
|
25
26
|
module.exports = __toCommonJS(index_exports);
|
|
26
27
|
var import_common = require("@forklaunch/common");
|
|
@@ -30,6 +31,22 @@ var ENCRYPTED_PREFIXES = ["v1:", "v2:", "v3:", "v4:"];
|
|
|
30
31
|
function isEncrypted(value) {
|
|
31
32
|
return ENCRYPTED_PREFIXES.some((p) => value.startsWith(p));
|
|
32
33
|
}
|
|
34
|
+
var KEY_PREFIX_VARS = [
|
|
35
|
+
"REDIS_KEY_PREFIX",
|
|
36
|
+
"FORKLAUNCH_REDIS_KEY_PREFIX"
|
|
37
|
+
];
|
|
38
|
+
function bareKeyPrefix(value) {
|
|
39
|
+
let end = value.length;
|
|
40
|
+
while (end > 0 && value[end - 1] === ":") end--;
|
|
41
|
+
return value.slice(0, end);
|
|
42
|
+
}
|
|
43
|
+
function redisKeyPrefix(env = process.env) {
|
|
44
|
+
for (const name of KEY_PREFIX_VARS) {
|
|
45
|
+
const value = bareKeyPrefix(env[name]?.trim() ?? "");
|
|
46
|
+
if (value) return value;
|
|
47
|
+
}
|
|
48
|
+
return void 0;
|
|
49
|
+
}
|
|
33
50
|
var RedisTtlCache = class {
|
|
34
51
|
/**
|
|
35
52
|
* Creates an instance of RedisTtlCache.
|
|
@@ -39,10 +56,15 @@ var RedisTtlCache = class {
|
|
|
39
56
|
* @param {RedisClientOptions} options - Configuration options for the Redis client
|
|
40
57
|
* @param {TelemetryOptions} telemetryOptions - Configuration options for telemetry
|
|
41
58
|
* @param {RedisCacheEncryptionOptions} encryption - Encryption configuration
|
|
59
|
+
* @param {RedisTtlCacheOptions} [cacheOptions] - Key prefix (defaults to the platform's `REDIS_KEY_PREFIX`)
|
|
42
60
|
*/
|
|
43
|
-
constructor(ttlMilliseconds, openTelemetryCollector, options, telemetryOptions, encryption) {
|
|
61
|
+
constructor(ttlMilliseconds, openTelemetryCollector, options, telemetryOptions, encryption, cacheOptions = {}) {
|
|
44
62
|
this.ttlMilliseconds = ttlMilliseconds;
|
|
45
63
|
this.openTelemetryCollector = openTelemetryCollector;
|
|
64
|
+
const prefix = bareKeyPrefix(
|
|
65
|
+
(cacheOptions.keyPrefix ?? redisKeyPrefix() ?? "").trim()
|
|
66
|
+
);
|
|
67
|
+
this.keyPrefix = prefix ? `${prefix}:` : "";
|
|
46
68
|
this.telemetryOptions = (0, import_http.evaluateTelemetryOptions)(telemetryOptions);
|
|
47
69
|
this.client = (0, import_redis.createClient)(options);
|
|
48
70
|
this.encryptor = encryption.encryptor;
|
|
@@ -56,6 +78,37 @@ var RedisTtlCache = class {
|
|
|
56
78
|
client;
|
|
57
79
|
telemetryOptions;
|
|
58
80
|
encryptor;
|
|
81
|
+
/** `<prefix>:` or '' — every key and queue name the client sees starts with it. */
|
|
82
|
+
keyPrefix;
|
|
83
|
+
// ---------------------------------------------------------------------------
|
|
84
|
+
// Key prefix — callers use bare keys; Redis only ever sees prefixed ones
|
|
85
|
+
// ---------------------------------------------------------------------------
|
|
86
|
+
withPrefix(key) {
|
|
87
|
+
return this.keyPrefix + key;
|
|
88
|
+
}
|
|
89
|
+
withoutPrefix(key) {
|
|
90
|
+
return this.keyPrefix && key.startsWith(this.keyPrefix) ? key.slice(this.keyPrefix.length) : key;
|
|
91
|
+
}
|
|
92
|
+
/** Prefixed keys for a key list, or every key under a (bare) key prefix. */
|
|
93
|
+
async resolveKeys(keysOrPrefix) {
|
|
94
|
+
return Array.isArray(keysOrPrefix) ? keysOrPrefix.map((key) => this.withPrefix(key)) : this.scanKeys(this.withPrefix(keysOrPrefix));
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Every key starting with `prefix` (already prefixed), via SCAN. KEYS is in
|
|
98
|
+
* Redis's `@dangerous` category, which shared-pool ACLs deny, and it blocks
|
|
99
|
+
* the server while it walks the whole keyspace.
|
|
100
|
+
*/
|
|
101
|
+
async scanKeys(prefix) {
|
|
102
|
+
const keys = /* @__PURE__ */ new Set();
|
|
103
|
+
for await (const batch of this.client.scanIterator({
|
|
104
|
+
// same glob semantics KEYS had: the caller's prefix may itself be a pattern
|
|
105
|
+
MATCH: `${prefix}*`,
|
|
106
|
+
COUNT: 500
|
|
107
|
+
})) {
|
|
108
|
+
for (const key of batch) keys.add(String(key));
|
|
109
|
+
}
|
|
110
|
+
return [...keys];
|
|
111
|
+
}
|
|
59
112
|
// ---------------------------------------------------------------------------
|
|
60
113
|
// Encryption helpers — only active when compliance context is provided
|
|
61
114
|
// ---------------------------------------------------------------------------
|
|
@@ -108,7 +161,7 @@ var RedisTtlCache = class {
|
|
|
108
161
|
this.openTelemetryCollector.info(`Putting record into cache: ${key}`);
|
|
109
162
|
}
|
|
110
163
|
await this.client.set(
|
|
111
|
-
key,
|
|
164
|
+
this.withPrefix(key),
|
|
112
165
|
this.encryptValue((0, import_common.safeStringify)(value), compliance),
|
|
113
166
|
{ PX: ttlMilliseconds }
|
|
114
167
|
);
|
|
@@ -124,7 +177,7 @@ var RedisTtlCache = class {
|
|
|
124
177
|
);
|
|
125
178
|
}
|
|
126
179
|
const result = await this.client.set(
|
|
127
|
-
key,
|
|
180
|
+
this.withPrefix(key),
|
|
128
181
|
this.encryptValue((0, import_common.safeStringify)(value), compliance),
|
|
129
182
|
{ PX: ttlMilliseconds, NX: true }
|
|
130
183
|
);
|
|
@@ -134,7 +187,7 @@ var RedisTtlCache = class {
|
|
|
134
187
|
const multiCommand = this.client.multi();
|
|
135
188
|
for (const { key, value, ttlMilliseconds } of cacheRecords) {
|
|
136
189
|
multiCommand.set(
|
|
137
|
-
key,
|
|
190
|
+
this.withPrefix(key),
|
|
138
191
|
this.encryptValue((0, import_common.safeStringify)(value), compliance),
|
|
139
192
|
{ PX: ttlMilliseconds || this.ttlMilliseconds }
|
|
140
193
|
);
|
|
@@ -143,7 +196,7 @@ var RedisTtlCache = class {
|
|
|
143
196
|
}
|
|
144
197
|
async enqueueRecord(queueName, value, compliance) {
|
|
145
198
|
await this.client.lPush(
|
|
146
|
-
queueName,
|
|
199
|
+
this.withPrefix(queueName),
|
|
147
200
|
this.encryptValue((0, import_common.safeStringify)(value), compliance)
|
|
148
201
|
);
|
|
149
202
|
}
|
|
@@ -151,24 +204,24 @@ var RedisTtlCache = class {
|
|
|
151
204
|
const multiCommand = this.client.multi();
|
|
152
205
|
for (const value of values) {
|
|
153
206
|
multiCommand.lPush(
|
|
154
|
-
queueName,
|
|
207
|
+
this.withPrefix(queueName),
|
|
155
208
|
this.encryptValue((0, import_common.safeStringify)(value), compliance)
|
|
156
209
|
);
|
|
157
210
|
}
|
|
158
211
|
await multiCommand.exec();
|
|
159
212
|
}
|
|
160
213
|
async deleteRecord(cacheRecordKey) {
|
|
161
|
-
await this.client.del(cacheRecordKey);
|
|
214
|
+
await this.client.del(this.withPrefix(cacheRecordKey));
|
|
162
215
|
}
|
|
163
216
|
async deleteBatchRecords(cacheRecordKeys) {
|
|
164
217
|
const multiCommand = this.client.multi();
|
|
165
218
|
for (const key of cacheRecordKeys) {
|
|
166
|
-
multiCommand.del(key);
|
|
219
|
+
multiCommand.del(this.withPrefix(key));
|
|
167
220
|
}
|
|
168
221
|
await multiCommand.exec();
|
|
169
222
|
}
|
|
170
223
|
async dequeueRecord(queueName, compliance) {
|
|
171
|
-
const value = await this.client.rPop(queueName);
|
|
224
|
+
const value = await this.client.rPop(this.withPrefix(queueName));
|
|
172
225
|
if (value === null) {
|
|
173
226
|
throw new Error(`Queue is empty: ${queueName}`);
|
|
174
227
|
}
|
|
@@ -177,7 +230,7 @@ var RedisTtlCache = class {
|
|
|
177
230
|
async dequeueBatchRecords(queueName, pageSize, compliance) {
|
|
178
231
|
const multiCommand = this.client.multi();
|
|
179
232
|
for (let i = 0; i < pageSize; i++) {
|
|
180
|
-
multiCommand.rPop(queueName);
|
|
233
|
+
multiCommand.rPop(this.withPrefix(queueName));
|
|
181
234
|
}
|
|
182
235
|
const values = await multiCommand.exec();
|
|
183
236
|
return values.map(
|
|
@@ -190,7 +243,7 @@ var RedisTtlCache = class {
|
|
|
190
243
|
).filter(Boolean);
|
|
191
244
|
}
|
|
192
245
|
async readRecord(cacheRecordKey, compliance) {
|
|
193
|
-
const [value, ttl] = await this.client.multi().get(cacheRecordKey).ttl(cacheRecordKey).exec();
|
|
246
|
+
const [value, ttl] = await this.client.multi().get(this.withPrefix(cacheRecordKey)).ttl(this.withPrefix(cacheRecordKey)).exec();
|
|
194
247
|
if (value === null) {
|
|
195
248
|
throw new Error(`Record not found for key: ${cacheRecordKey}`);
|
|
196
249
|
}
|
|
@@ -210,7 +263,7 @@ var RedisTtlCache = class {
|
|
|
210
263
|
};
|
|
211
264
|
}
|
|
212
265
|
async readBatchRecords(cacheRecordKeysOrPrefix, compliance) {
|
|
213
|
-
const keys =
|
|
266
|
+
const keys = await this.resolveKeys(cacheRecordKeysOrPrefix);
|
|
214
267
|
const multiCommand = this.client.multi();
|
|
215
268
|
for (const key of keys) {
|
|
216
269
|
multiCommand.get(key);
|
|
@@ -231,7 +284,7 @@ var RedisTtlCache = class {
|
|
|
231
284
|
);
|
|
232
285
|
if (maybeValue && ttl) {
|
|
233
286
|
acc.push({
|
|
234
|
-
key: keys[index / 2],
|
|
287
|
+
key: this.withoutPrefix(keys[index / 2]),
|
|
235
288
|
value: maybeValue,
|
|
236
289
|
ttlMilliseconds: ttl * 1e3
|
|
237
290
|
});
|
|
@@ -241,14 +294,15 @@ var RedisTtlCache = class {
|
|
|
241
294
|
}, []);
|
|
242
295
|
}
|
|
243
296
|
async listKeys(pattern_prefix) {
|
|
244
|
-
|
|
297
|
+
const keys = await this.scanKeys(this.withPrefix(pattern_prefix));
|
|
298
|
+
return keys.map((key) => this.withoutPrefix(key));
|
|
245
299
|
}
|
|
246
300
|
async peekRecord(cacheRecordKey) {
|
|
247
|
-
const result = await this.client.exists(cacheRecordKey);
|
|
301
|
+
const result = await this.client.exists(this.withPrefix(cacheRecordKey));
|
|
248
302
|
return result === 1;
|
|
249
303
|
}
|
|
250
304
|
async peekBatchRecords(cacheRecordKeysOrPrefix) {
|
|
251
|
-
const keys =
|
|
305
|
+
const keys = await this.resolveKeys(cacheRecordKeysOrPrefix);
|
|
252
306
|
const multiCommand = this.client.multi();
|
|
253
307
|
for (const key of keys) {
|
|
254
308
|
multiCommand.exists(key);
|
|
@@ -257,26 +311,39 @@ var RedisTtlCache = class {
|
|
|
257
311
|
return results.map((result) => result === 1);
|
|
258
312
|
}
|
|
259
313
|
async peekQueueRecord(queueName, compliance) {
|
|
260
|
-
const value = await this.client.lRange(queueName, -1, -1);
|
|
314
|
+
const value = await this.client.lRange(this.withPrefix(queueName), -1, -1);
|
|
261
315
|
return this.parseValue(value[0], compliance);
|
|
262
316
|
}
|
|
263
317
|
async peekQueueRecords(queueName, pageSize, compliance) {
|
|
264
|
-
const values = await this.client.lRange(
|
|
318
|
+
const values = await this.client.lRange(
|
|
319
|
+
this.withPrefix(queueName),
|
|
320
|
+
-pageSize,
|
|
321
|
+
-1
|
|
322
|
+
);
|
|
265
323
|
if (values.length === 0) return [];
|
|
266
324
|
return values.reverse().map((value) => this.parseValue(value, compliance)).filter(Boolean);
|
|
267
325
|
}
|
|
268
326
|
async disconnect() {
|
|
269
327
|
await this.client.quit();
|
|
270
328
|
}
|
|
329
|
+
/** The prefix (with its trailing `:`) applied to every key, or ''. */
|
|
330
|
+
getKeyPrefix() {
|
|
331
|
+
return this.keyPrefix;
|
|
332
|
+
}
|
|
271
333
|
getTtlMilliseconds() {
|
|
272
334
|
return this.ttlMilliseconds;
|
|
273
335
|
}
|
|
336
|
+
/**
|
|
337
|
+
* The raw node-redis client. Commands sent through it bypass the key
|
|
338
|
+
* prefix: on a shared Redis, prefix keys with `getKeyPrefix()` yourself.
|
|
339
|
+
*/
|
|
274
340
|
getClient() {
|
|
275
341
|
return this.client;
|
|
276
342
|
}
|
|
277
343
|
};
|
|
278
344
|
// Annotate the CommonJS export names for ESM import in node:
|
|
279
345
|
0 && (module.exports = {
|
|
280
|
-
RedisTtlCache
|
|
346
|
+
RedisTtlCache,
|
|
347
|
+
redisKeyPrefix
|
|
281
348
|
});
|
|
282
349
|
//# sourceMappingURL=index.js.map
|
package/lib/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../index.ts"],"sourcesContent":["import { safeParse, safeStringify } from '@forklaunch/common';\nimport {\n type ComplianceContext,\n TtlCache,\n TtlCacheRecord\n} from '@forklaunch/core/cache';\nimport {\n evaluateTelemetryOptions,\n MetricsDefinition,\n OpenTelemetryCollector,\n TelemetryOptions\n} from '@forklaunch/core/http';\nimport { type FieldEncryptor } from '@forklaunch/core/persistence';\nimport { createClient, RedisClientOptions } from 'redis';\n\n/**\n * Type representing a raw reply from Redis commands.\n * Can be a string, number, Buffer, null, undefined, or array of raw replies.\n */\ntype RedisCommandRawReply =\n string | number | Buffer | null | undefined | Array<RedisCommandRawReply>;\n\n// v4 is the randomized, stamped envelope native (Rust) writers produce.\nconst ENCRYPTED_PREFIXES = ['v1:', 'v2:', 'v3:', 'v4:'] as const;\n\nfunction isEncrypted(value: string): boolean {\n return ENCRYPTED_PREFIXES.some((p) => value.startsWith(p));\n}\n\n/**\n * Options for configuring encryption on the Redis cache.\n * Required — every consumer must explicitly configure encryption.\n */\nexport interface RedisCacheEncryptionOptions {\n /** The FieldEncryptor instance to use for encrypting cache values. */\n encryptor: FieldEncryptor;\n}\n\n/**\n * Class representing a Redis-based TTL (Time-To-Live) cache.\n * Implements the TtlCache interface to provide caching functionality with automatic expiration.\n *\n * Encryption is activated per-operation when a `compliance` context is provided.\n * Without it, values are stored and read as plaintext.\n */\nexport class RedisTtlCache implements TtlCache {\n private client;\n private telemetryOptions;\n private encryptor?: FieldEncryptor;\n\n /**\n * Creates an instance of RedisTtlCache.\n *\n * @param {number} ttlMilliseconds - The default Time-To-Live in milliseconds for cache entries\n * @param {OpenTelemetryCollector<MetricsDefinition>} openTelemetryCollector - Collector for OpenTelemetry metrics\n * @param {RedisClientOptions} options - Configuration options for the Redis client\n * @param {TelemetryOptions} telemetryOptions - Configuration options for telemetry\n * @param {RedisCacheEncryptionOptions} encryption - Encryption configuration\n */\n constructor(\n private ttlMilliseconds: number,\n private openTelemetryCollector: OpenTelemetryCollector<MetricsDefinition>,\n options: RedisClientOptions,\n telemetryOptions: TelemetryOptions,\n encryption: RedisCacheEncryptionOptions\n ) {\n this.telemetryOptions = evaluateTelemetryOptions(telemetryOptions);\n this.client = createClient(options);\n this.encryptor = encryption.encryptor;\n if (this.telemetryOptions.enabled.logging) {\n this.client.on('error', (err) => this.openTelemetryCollector.error(err));\n this.client.connect().catch(this.openTelemetryCollector.error);\n }\n }\n\n // ---------------------------------------------------------------------------\n // Encryption helpers — only active when compliance context is provided\n // ---------------------------------------------------------------------------\n\n private encryptValue(\n serialized: string,\n compliance?: ComplianceContext\n ): string {\n if (!compliance || !this.encryptor) return serialized;\n return (\n this.encryptor.encrypt(serialized, compliance.tenantId) ?? serialized\n );\n }\n\n private decryptValue(value: string, compliance?: ComplianceContext): string {\n if (!compliance || !this.encryptor) return value;\n if (!isEncrypted(value)) return value;\n // If a value is encrypted but we cannot decrypt it, treat the entry as\n // unreadable rather than returning the ciphertext. Returning the raw\n // bytes lets callers (e.g. cache services that JSON.parse the result)\n // surface garbage as if it were a successful read, masking key/tenant\n // mismatches and corrupting downstream consumers. Throwing forces the\n // caller's catch path (cache miss) to run.\n let decrypted: string | null;\n try {\n decrypted = this.encryptor.decrypt(value, compliance.tenantId);\n } catch (err) {\n throw new Error(\n `Redis: failed to decrypt value for tenant ${compliance.tenantId}: ${err instanceof Error ? err.message : String(err)}`,\n { cause: err }\n );\n }\n if (decrypted === null) {\n throw new Error(\n `Redis: encrypted value for tenant ${compliance.tenantId} could not be decrypted (null result)`\n );\n }\n return decrypted;\n }\n\n private parseValue<T>(\n value: RedisCommandRawReply,\n compliance?: ComplianceContext\n ): T {\n if (value == null) {\n return null as T;\n }\n\n if (Array.isArray(value)) {\n return value.map((v) => this.parseValue<T>(v, compliance)) as T;\n }\n\n if (Buffer.isBuffer(value)) {\n return value.toJSON() as T;\n }\n\n switch (typeof value) {\n case 'object':\n case 'string':\n return safeParse(this.decryptValue(String(value), compliance)) as T;\n case 'number':\n return value as T;\n }\n }\n\n // ---------------------------------------------------------------------------\n // TtlCache implementation\n // ---------------------------------------------------------------------------\n\n async putRecord<T>(\n { key, value, ttlMilliseconds = this.ttlMilliseconds }: TtlCacheRecord<T>,\n compliance?: ComplianceContext\n ): Promise<void> {\n if (this.telemetryOptions.enabled.logging) {\n this.openTelemetryCollector.info(`Putting record into cache: ${key}`);\n }\n await this.client.set(\n key,\n this.encryptValue(safeStringify(value), compliance),\n { PX: ttlMilliseconds }\n );\n }\n\n /**\n * Atomic set-if-absent with expiry (`SET key value NX PX ttl`). Answers\n * `true` only for the first writer of the key.\n */\n async putRecordIfAbsent<T>(\n { key, value, ttlMilliseconds = this.ttlMilliseconds }: TtlCacheRecord<T>,\n compliance?: ComplianceContext\n ): Promise<boolean> {\n if (this.telemetryOptions.enabled.logging) {\n this.openTelemetryCollector.info(\n `Putting record into cache if absent: ${key}`\n );\n }\n const result = await this.client.set(\n key,\n this.encryptValue(safeStringify(value), compliance),\n { PX: ttlMilliseconds, NX: true }\n );\n return result === 'OK';\n }\n\n async putBatchRecords<T>(\n cacheRecords: TtlCacheRecord<T>[],\n compliance?: ComplianceContext\n ): Promise<void> {\n const multiCommand = this.client.multi();\n for (const { key, value, ttlMilliseconds } of cacheRecords) {\n multiCommand.set(\n key,\n this.encryptValue(safeStringify(value), compliance),\n { PX: ttlMilliseconds || this.ttlMilliseconds }\n );\n }\n await multiCommand.exec();\n }\n\n async enqueueRecord<T>(\n queueName: string,\n value: T,\n compliance?: ComplianceContext\n ): Promise<void> {\n await this.client.lPush(\n queueName,\n this.encryptValue(safeStringify(value), compliance)\n );\n }\n\n async enqueueBatchRecords<T>(\n queueName: string,\n values: T[],\n compliance?: ComplianceContext\n ): Promise<void> {\n const multiCommand = this.client.multi();\n for (const value of values) {\n multiCommand.lPush(\n queueName,\n this.encryptValue(safeStringify(value), compliance)\n );\n }\n await multiCommand.exec();\n }\n\n async deleteRecord(cacheRecordKey: string): Promise<void> {\n await this.client.del(cacheRecordKey);\n }\n\n async deleteBatchRecords(cacheRecordKeys: string[]): Promise<void> {\n const multiCommand = this.client.multi();\n for (const key of cacheRecordKeys) {\n multiCommand.del(key);\n }\n await multiCommand.exec();\n }\n\n async dequeueRecord<T>(\n queueName: string,\n compliance?: ComplianceContext\n ): Promise<T> {\n const value = await this.client.rPop(queueName);\n if (value === null) {\n throw new Error(`Queue is empty: ${queueName}`);\n }\n return safeParse(this.decryptValue(value, compliance)) as T;\n }\n\n async dequeueBatchRecords<T>(\n queueName: string,\n pageSize: number,\n compliance?: ComplianceContext\n ): Promise<T[]> {\n const multiCommand = this.client.multi();\n for (let i = 0; i < pageSize; i++) {\n multiCommand.rPop(queueName);\n }\n const values = await multiCommand.exec();\n return values\n .map((value) =>\n // node-redis hands back `ReplyUnion`, while the parse/serialize helpers\n // are typed against `RedisCommandRawReply`. The two unions describe the\n // same wire values but share no member, so TypeScript sees no overlap.\n this.parseValue<T>(value as unknown as RedisCommandRawReply, compliance)\n )\n .filter(Boolean);\n }\n\n async readRecord<T>(\n cacheRecordKey: string,\n compliance?: ComplianceContext\n ): Promise<TtlCacheRecord<T>> {\n const [value, ttl] = await this.client\n .multi()\n .get(cacheRecordKey)\n .ttl(cacheRecordKey)\n .exec();\n if (value === null) {\n throw new Error(`Record not found for key: ${cacheRecordKey}`);\n }\n\n return {\n key: cacheRecordKey,\n value: this.parseValue<T>(\n // node-redis hands back `ReplyUnion`, while the parse/serialize helpers\n // are typed against `RedisCommandRawReply`. The two unions describe the\n // same wire values but share no member, so TypeScript sees no overlap.\n value as unknown as RedisCommandRawReply,\n compliance\n ),\n ttlMilliseconds:\n this.parseValue<number>(\n ttl as unknown as RedisCommandRawReply,\n compliance\n ) * 1000\n };\n }\n\n async readBatchRecords<T>(\n cacheRecordKeysOrPrefix: string[] | string,\n compliance?: ComplianceContext\n ): Promise<TtlCacheRecord<T>[]> {\n const keys = Array.isArray(cacheRecordKeysOrPrefix)\n ? cacheRecordKeysOrPrefix\n : await this.client.keys(cacheRecordKeysOrPrefix + '*');\n const multiCommand = this.client.multi();\n for (const key of keys) {\n multiCommand.get(key);\n multiCommand.ttl(key);\n }\n const values = await multiCommand.exec();\n return values.reduce<TtlCacheRecord<T>[]>((acc, value, index) => {\n if (index % 2 === 0) {\n const maybeValue = this.parseValue<T>(\n // `ReplyUnion` and `RedisCommandRawReply` name the same wire values\n // through disjoint unions, so neither is assignable to the other.\n value as unknown as RedisCommandRawReply,\n compliance\n );\n const ttl = this.parseValue<number>(\n values[index + 1] as unknown as RedisCommandRawReply,\n compliance\n );\n if (maybeValue && ttl) {\n acc.push({\n key: keys[index / 2],\n value: maybeValue,\n ttlMilliseconds: ttl * 1000\n });\n }\n }\n return acc;\n }, []);\n }\n\n async listKeys(pattern_prefix: string): Promise<string[]> {\n return this.client.keys(pattern_prefix + '*');\n }\n\n async peekRecord(cacheRecordKey: string): Promise<boolean> {\n const result = await this.client.exists(cacheRecordKey);\n return result === 1;\n }\n\n async peekBatchRecords(\n cacheRecordKeysOrPrefix: string[] | string\n ): Promise<boolean[]> {\n const keys = Array.isArray(cacheRecordKeysOrPrefix)\n ? cacheRecordKeysOrPrefix\n : await this.client.keys(cacheRecordKeysOrPrefix + '*');\n const multiCommand = this.client.multi();\n for (const key of keys) {\n multiCommand.exists(key);\n }\n const results = await multiCommand.exec();\n return results.map((result) => (result as unknown as number) === 1);\n }\n\n async peekQueueRecord<T>(\n queueName: string,\n compliance?: ComplianceContext\n ): Promise<T> {\n // Queues use lPush + rPop, so the next item to dequeue lives at the\n // tail of the list, not the head. Reading lRange(0, 0) would return the\n // most-recently-pushed item — the opposite of dequeue order.\n const value = await this.client.lRange(queueName, -1, -1);\n return this.parseValue<T>(value[0], compliance);\n }\n\n async peekQueueRecords<T>(\n queueName: string,\n pageSize: number,\n compliance?: ComplianceContext\n ): Promise<T[]> {\n // Tail-relative range: the last `pageSize` items, where the very last\n // item is the next to be dequeued. Redis returns them in list order\n // (oldest-tail-end first), so reverse to put next-to-dequeue first.\n const values = await this.client.lRange(queueName, -pageSize, -1);\n if (values.length === 0) return [];\n return values\n .reverse()\n .map((value) => this.parseValue<T>(value, compliance))\n .filter(Boolean);\n }\n\n async disconnect(): Promise<void> {\n await this.client.quit();\n }\n\n getTtlMilliseconds(): number {\n return this.ttlMilliseconds;\n }\n\n getClient(): typeof this.client {\n return this.client;\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,oBAAyC;AAMzC,kBAKO;AAEP,mBAAiD;AAUjD,IAAM,qBAAqB,CAAC,OAAO,OAAO,OAAO,KAAK;AAEtD,SAAS,YAAY,OAAwB;AAC3C,SAAO,mBAAmB,KAAK,CAAC,MAAM,MAAM,WAAW,CAAC,CAAC;AAC3D;AAkBO,IAAM,gBAAN,MAAwC;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAc7C,YACU,iBACA,wBACR,SACA,kBACA,YACA;AALQ;AACA;AAKR,SAAK,uBAAmB,sCAAyB,gBAAgB;AACjE,SAAK,aAAS,2BAAa,OAAO;AAClC,SAAK,YAAY,WAAW;AAC5B,QAAI,KAAK,iBAAiB,QAAQ,SAAS;AACzC,WAAK,OAAO,GAAG,SAAS,CAAC,QAAQ,KAAK,uBAAuB,MAAM,GAAG,CAAC;AACvE,WAAK,OAAO,QAAQ,EAAE,MAAM,KAAK,uBAAuB,KAAK;AAAA,IAC/D;AAAA,EACF;AAAA,EAbU;AAAA,EACA;AAAA,EAfF;AAAA,EACA;AAAA,EACA;AAAA;AAAA;AAAA;AAAA,EA+BA,aACN,YACA,YACQ;AACR,QAAI,CAAC,cAAc,CAAC,KAAK,UAAW,QAAO;AAC3C,WACE,KAAK,UAAU,QAAQ,YAAY,WAAW,QAAQ,KAAK;AAAA,EAE/D;AAAA,EAEQ,aAAa,OAAe,YAAwC;AAC1E,QAAI,CAAC,cAAc,CAAC,KAAK,UAAW,QAAO;AA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AAM,SAAS,MAAM,KAAK,OAAO,OAAO,WAAW,CAAC,UAAU,EAAE;AAChE,QAAI,OAAO,WAAW,EAAG,QAAO,CAAC;AACjC,WAAO,OACJ,QAAQ,EACR,IAAI,CAAC,UAAU,KAAK,WAAc,OAAO,UAAU,CAAC,EACpD,OAAO,OAAO;AAAA,EACnB;AAAA,EAEA,MAAM,aAA4B;AAChC,UAAM,KAAK,OAAO,KAAK;AAAA,EACzB;AAAA,EAEA,qBAA6B;AAC3B,WAAO,KAAK;AAAA,EACd;AAAA,EAEA,YAAgC;AAC9B,WAAO,KAAK;AAAA,EACd;AACF;","names":[]}
|
|
1
|
+
{"version":3,"sources":["../index.ts"],"sourcesContent":["import { safeParse, safeStringify } from '@forklaunch/common';\nimport {\n type ComplianceContext,\n TtlCache,\n TtlCacheRecord\n} from '@forklaunch/core/cache';\nimport {\n evaluateTelemetryOptions,\n MetricsDefinition,\n OpenTelemetryCollector,\n TelemetryOptions\n} from '@forklaunch/core/http';\nimport { type FieldEncryptor } from '@forklaunch/core/persistence';\nimport { createClient, RedisClientOptions } from 'redis';\n\n/**\n * Type representing a raw reply from Redis commands.\n * Can be a string, number, Buffer, null, undefined, or array of raw replies.\n */\ntype RedisCommandRawReply =\n string | number | Buffer | null | undefined | Array<RedisCommandRawReply>;\n\n// v4 is the randomized, stamped envelope native (Rust) writers produce.\nconst ENCRYPTED_PREFIXES = ['v1:', 'v2:', 'v3:', 'v4:'] as const;\n\nfunction isEncrypted(value: string): boolean {\n return ENCRYPTED_PREFIXES.some((p) => value.startsWith(p));\n}\n\n/**\n * Options for configuring encryption on the Redis cache.\n * Required — every consumer must explicitly configure encryption.\n */\nexport interface RedisCacheEncryptionOptions {\n /** The FieldEncryptor instance to use for encrypting cache values. */\n encryptor: FieldEncryptor;\n}\n\n/** Env vars the platform sets for an app on a shared (partitioned) Redis. */\nconst KEY_PREFIX_VARS = [\n 'REDIS_KEY_PREFIX',\n 'FORKLAUNCH_REDIS_KEY_PREFIX'\n] as const;\n\n/**\n * The app's key prefix on a shared Redis, without a trailing separator, or\n * undefined on a dedicated cache (local Redis, own ElastiCache).\n *\n * On a shared pool each app is an ACL user whose key pattern is\n * `~<prefix>:*`; a key outside it is answered NOPERM. The platform passes the\n * prefix as `REDIS_KEY_PREFIX` (`FORKLAUNCH_REDIS_KEY_PREFIX` is the same\n * value under the platform's own name).\n */\n/** `value` trimmed, with any trailing `:` separators removed. */\nfunction bareKeyPrefix(value: string): string {\n let end = value.length;\n while (end > 0 && value[end - 1] === ':') end--;\n return value.slice(0, end);\n}\n\nexport function redisKeyPrefix(\n env: Record<string, string | undefined> = process.env\n): string | undefined {\n for (const name of KEY_PREFIX_VARS) {\n const value = bareKeyPrefix(env[name]?.trim() ?? '');\n if (value) return value;\n }\n return undefined;\n}\n\n/** Options beyond the Redis client's own. */\nexport interface RedisTtlCacheOptions {\n /**\n * Prefix for every key and queue name, joined with `:`. Defaults to the\n * platform's `REDIS_KEY_PREFIX`, so an app on a shared Redis stays inside\n * its namespace without configuring anything. Pass `''` to turn it off.\n */\n keyPrefix?: string;\n}\n\n/**\n * Class representing a Redis-based TTL (Time-To-Live) cache.\n * Implements the TtlCache interface to provide caching functionality with automatic expiration.\n *\n * Encryption is activated per-operation when a `compliance` context is provided.\n * Without it, values are stored and read as plaintext.\n */\nexport class RedisTtlCache implements TtlCache {\n private client;\n private telemetryOptions;\n private encryptor?: FieldEncryptor;\n /** `<prefix>:` or '' — every key and queue name the client sees starts with it. */\n private readonly keyPrefix: string;\n\n /**\n * Creates an instance of RedisTtlCache.\n *\n * @param {number} ttlMilliseconds - The default Time-To-Live in milliseconds for cache entries\n * @param {OpenTelemetryCollector<MetricsDefinition>} openTelemetryCollector - Collector for OpenTelemetry metrics\n * @param {RedisClientOptions} options - Configuration options for the Redis client\n * @param {TelemetryOptions} telemetryOptions - Configuration options for telemetry\n * @param {RedisCacheEncryptionOptions} encryption - Encryption configuration\n * @param {RedisTtlCacheOptions} [cacheOptions] - Key prefix (defaults to the platform's `REDIS_KEY_PREFIX`)\n */\n constructor(\n private ttlMilliseconds: number,\n private openTelemetryCollector: OpenTelemetryCollector<MetricsDefinition>,\n options: RedisClientOptions,\n telemetryOptions: TelemetryOptions,\n encryption: RedisCacheEncryptionOptions,\n cacheOptions: RedisTtlCacheOptions = {}\n ) {\n const prefix = bareKeyPrefix(\n (cacheOptions.keyPrefix ?? redisKeyPrefix() ?? '').trim()\n );\n this.keyPrefix = prefix ? `${prefix}:` : '';\n this.telemetryOptions = evaluateTelemetryOptions(telemetryOptions);\n this.client = createClient(options);\n this.encryptor = encryption.encryptor;\n if (this.telemetryOptions.enabled.logging) {\n this.client.on('error', (err) => this.openTelemetryCollector.error(err));\n this.client.connect().catch(this.openTelemetryCollector.error);\n }\n }\n\n // ---------------------------------------------------------------------------\n // Key prefix — callers use bare keys; Redis only ever sees prefixed ones\n // ---------------------------------------------------------------------------\n\n private withPrefix(key: string): string {\n return this.keyPrefix + key;\n }\n\n private withoutPrefix(key: string): string {\n return this.keyPrefix && key.startsWith(this.keyPrefix)\n ? key.slice(this.keyPrefix.length)\n : key;\n }\n\n /** Prefixed keys for a key list, or every key under a (bare) key prefix. */\n private async resolveKeys(\n keysOrPrefix: string[] | string\n ): Promise<string[]> {\n return Array.isArray(keysOrPrefix)\n ? keysOrPrefix.map((key) => this.withPrefix(key))\n : this.scanKeys(this.withPrefix(keysOrPrefix));\n }\n\n /**\n * Every key starting with `prefix` (already prefixed), via SCAN. KEYS is in\n * Redis's `@dangerous` category, which shared-pool ACLs deny, and it blocks\n * the server while it walks the whole keyspace.\n */\n private async scanKeys(prefix: string): Promise<string[]> {\n const keys = new Set<string>();\n for await (const batch of this.client.scanIterator({\n // same glob semantics KEYS had: the caller's prefix may itself be a pattern\n MATCH: `${prefix}*`,\n COUNT: 500\n })) {\n for (const key of batch) keys.add(String(key));\n }\n return [...keys];\n }\n\n // ---------------------------------------------------------------------------\n // Encryption helpers — only active when compliance context is provided\n // ---------------------------------------------------------------------------\n\n private encryptValue(\n serialized: string,\n compliance?: ComplianceContext\n ): string {\n if (!compliance || !this.encryptor) return serialized;\n return (\n this.encryptor.encrypt(serialized, compliance.tenantId) ?? serialized\n );\n }\n\n private decryptValue(value: string, compliance?: ComplianceContext): string {\n if (!compliance || !this.encryptor) return value;\n if (!isEncrypted(value)) return value;\n // If a value is encrypted but we cannot decrypt it, treat the entry as\n // unreadable rather than returning the ciphertext. Returning the raw\n // bytes lets callers (e.g. cache services that JSON.parse the result)\n // surface garbage as if it were a successful read, masking key/tenant\n // mismatches and corrupting downstream consumers. Throwing forces the\n // caller's catch path (cache miss) to run.\n let decrypted: string | null;\n try {\n decrypted = this.encryptor.decrypt(value, compliance.tenantId);\n } catch (err) {\n throw new Error(\n `Redis: failed to decrypt value for tenant ${compliance.tenantId}: ${err instanceof Error ? err.message : String(err)}`,\n { cause: err }\n );\n }\n if (decrypted === null) {\n throw new Error(\n `Redis: encrypted value for tenant ${compliance.tenantId} could not be decrypted (null result)`\n );\n }\n return decrypted;\n }\n\n private parseValue<T>(\n value: RedisCommandRawReply,\n compliance?: ComplianceContext\n ): T {\n if (value == null) {\n return null as T;\n }\n\n if (Array.isArray(value)) {\n return value.map((v) => this.parseValue<T>(v, compliance)) as T;\n }\n\n if (Buffer.isBuffer(value)) {\n return value.toJSON() as T;\n }\n\n switch (typeof value) {\n case 'object':\n case 'string':\n return safeParse(this.decryptValue(String(value), compliance)) as T;\n case 'number':\n return value as T;\n }\n }\n\n // ---------------------------------------------------------------------------\n // TtlCache implementation\n // ---------------------------------------------------------------------------\n\n async putRecord<T>(\n { key, value, ttlMilliseconds = this.ttlMilliseconds }: TtlCacheRecord<T>,\n compliance?: ComplianceContext\n ): Promise<void> {\n if (this.telemetryOptions.enabled.logging) {\n this.openTelemetryCollector.info(`Putting record into cache: ${key}`);\n }\n await this.client.set(\n this.withPrefix(key),\n this.encryptValue(safeStringify(value), compliance),\n { PX: ttlMilliseconds }\n );\n }\n\n /**\n * Atomic set-if-absent with expiry (`SET key value NX PX ttl`). Answers\n * `true` only for the first writer of the key.\n */\n async putRecordIfAbsent<T>(\n { key, value, ttlMilliseconds = this.ttlMilliseconds }: TtlCacheRecord<T>,\n compliance?: ComplianceContext\n ): Promise<boolean> {\n if (this.telemetryOptions.enabled.logging) {\n this.openTelemetryCollector.info(\n `Putting record into cache if absent: ${key}`\n );\n }\n const result = await this.client.set(\n this.withPrefix(key),\n this.encryptValue(safeStringify(value), compliance),\n { PX: ttlMilliseconds, NX: true }\n );\n return result === 'OK';\n }\n\n async putBatchRecords<T>(\n cacheRecords: TtlCacheRecord<T>[],\n compliance?: ComplianceContext\n ): Promise<void> {\n const multiCommand = this.client.multi();\n for (const { key, value, ttlMilliseconds } of cacheRecords) {\n multiCommand.set(\n this.withPrefix(key),\n this.encryptValue(safeStringify(value), compliance),\n { PX: ttlMilliseconds || this.ttlMilliseconds }\n );\n }\n await multiCommand.exec();\n }\n\n async enqueueRecord<T>(\n queueName: string,\n value: T,\n compliance?: ComplianceContext\n ): Promise<void> {\n await this.client.lPush(\n this.withPrefix(queueName),\n this.encryptValue(safeStringify(value), compliance)\n );\n }\n\n async enqueueBatchRecords<T>(\n queueName: string,\n values: T[],\n compliance?: ComplianceContext\n ): Promise<void> {\n const multiCommand = this.client.multi();\n for (const value of values) {\n multiCommand.lPush(\n this.withPrefix(queueName),\n this.encryptValue(safeStringify(value), compliance)\n );\n }\n await multiCommand.exec();\n }\n\n async deleteRecord(cacheRecordKey: string): Promise<void> {\n await this.client.del(this.withPrefix(cacheRecordKey));\n }\n\n async deleteBatchRecords(cacheRecordKeys: string[]): Promise<void> {\n const multiCommand = this.client.multi();\n for (const key of cacheRecordKeys) {\n multiCommand.del(this.withPrefix(key));\n }\n await multiCommand.exec();\n }\n\n async dequeueRecord<T>(\n queueName: string,\n compliance?: ComplianceContext\n ): Promise<T> {\n const value = await this.client.rPop(this.withPrefix(queueName));\n if (value === null) {\n throw new Error(`Queue is empty: ${queueName}`);\n }\n return safeParse(this.decryptValue(value, compliance)) as T;\n }\n\n async dequeueBatchRecords<T>(\n queueName: string,\n pageSize: number,\n compliance?: ComplianceContext\n ): Promise<T[]> {\n const multiCommand = this.client.multi();\n for (let i = 0; i < pageSize; i++) {\n multiCommand.rPop(this.withPrefix(queueName));\n }\n const values = await multiCommand.exec();\n return values\n .map((value) =>\n // node-redis hands back `ReplyUnion`, while the parse/serialize helpers\n // are typed against `RedisCommandRawReply`. The two unions describe the\n // same wire values but share no member, so TypeScript sees no overlap.\n this.parseValue<T>(value as unknown as RedisCommandRawReply, compliance)\n )\n .filter(Boolean);\n }\n\n async readRecord<T>(\n cacheRecordKey: string,\n compliance?: ComplianceContext\n ): Promise<TtlCacheRecord<T>> {\n const [value, ttl] = await this.client\n .multi()\n .get(this.withPrefix(cacheRecordKey))\n .ttl(this.withPrefix(cacheRecordKey))\n .exec();\n if (value === null) {\n throw new Error(`Record not found for key: ${cacheRecordKey}`);\n }\n\n return {\n key: cacheRecordKey,\n value: this.parseValue<T>(\n // node-redis hands back `ReplyUnion`, while the parse/serialize helpers\n // are typed against `RedisCommandRawReply`. The two unions describe the\n // same wire values but share no member, so TypeScript sees no overlap.\n value as unknown as RedisCommandRawReply,\n compliance\n ),\n ttlMilliseconds:\n this.parseValue<number>(\n ttl as unknown as RedisCommandRawReply,\n compliance\n ) * 1000\n };\n }\n\n async readBatchRecords<T>(\n cacheRecordKeysOrPrefix: string[] | string,\n compliance?: ComplianceContext\n ): Promise<TtlCacheRecord<T>[]> {\n const keys = await this.resolveKeys(cacheRecordKeysOrPrefix);\n const multiCommand = this.client.multi();\n for (const key of keys) {\n multiCommand.get(key);\n multiCommand.ttl(key);\n }\n const values = await multiCommand.exec();\n return values.reduce<TtlCacheRecord<T>[]>((acc, value, index) => {\n if (index % 2 === 0) {\n const maybeValue = this.parseValue<T>(\n // `ReplyUnion` and `RedisCommandRawReply` name the same wire values\n // through disjoint unions, so neither is assignable to the other.\n value as unknown as RedisCommandRawReply,\n compliance\n );\n const ttl = this.parseValue<number>(\n values[index + 1] as unknown as RedisCommandRawReply,\n compliance\n );\n if (maybeValue && ttl) {\n acc.push({\n key: this.withoutPrefix(keys[index / 2]),\n value: maybeValue,\n ttlMilliseconds: ttl * 1000\n });\n }\n }\n return acc;\n }, []);\n }\n\n async listKeys(pattern_prefix: string): Promise<string[]> {\n const keys = await this.scanKeys(this.withPrefix(pattern_prefix));\n return keys.map((key) => this.withoutPrefix(key));\n }\n\n async peekRecord(cacheRecordKey: string): Promise<boolean> {\n const result = await this.client.exists(this.withPrefix(cacheRecordKey));\n return result === 1;\n }\n\n async peekBatchRecords(\n cacheRecordKeysOrPrefix: string[] | string\n ): Promise<boolean[]> {\n const keys = await this.resolveKeys(cacheRecordKeysOrPrefix);\n const multiCommand = this.client.multi();\n for (const key of keys) {\n multiCommand.exists(key);\n }\n const results = await multiCommand.exec();\n return results.map((result) => (result as unknown as number) === 1);\n }\n\n async peekQueueRecord<T>(\n queueName: string,\n compliance?: ComplianceContext\n ): Promise<T> {\n // Queues use lPush + rPop, so the next item to dequeue lives at the\n // tail of the list, not the head. Reading lRange(0, 0) would return the\n // most-recently-pushed item — the opposite of dequeue order.\n const value = await this.client.lRange(this.withPrefix(queueName), -1, -1);\n return this.parseValue<T>(value[0], compliance);\n }\n\n async peekQueueRecords<T>(\n queueName: string,\n pageSize: number,\n compliance?: ComplianceContext\n ): Promise<T[]> {\n // Tail-relative range: the last `pageSize` items, where the very last\n // item is the next to be dequeued. Redis returns them in list order\n // (oldest-tail-end first), so reverse to put next-to-dequeue first.\n const values = await this.client.lRange(\n this.withPrefix(queueName),\n -pageSize,\n -1\n );\n if (values.length === 0) return [];\n return values\n .reverse()\n .map((value) => this.parseValue<T>(value, compliance))\n .filter(Boolean);\n }\n\n async disconnect(): Promise<void> {\n await this.client.quit();\n }\n\n /** The prefix (with its trailing `:`) applied to every key, or ''. */\n getKeyPrefix(): string {\n return this.keyPrefix;\n }\n\n getTtlMilliseconds(): number {\n return this.ttlMilliseconds;\n }\n\n /**\n * The raw node-redis client. Commands sent through it bypass the key\n * prefix: on a shared Redis, prefix keys with `getKeyPrefix()` yourself.\n */\n getClient(): typeof this.client {\n return this.client;\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,oBAAyC;AAMzC,kBAKO;AAEP,mBAAiD;AAUjD,IAAM,qBAAqB,CAAC,OAAO,OAAO,OAAO,KAAK;AAEtD,SAAS,YAAY,OAAwB;AAC3C,SAAO,mBAAmB,KAAK,CAAC,MAAM,MAAM,WAAW,CAAC,CAAC;AAC3D;AAYA,IAAM,kBAAkB;AAAA,EACtB;AAAA,EACA;AACF;AAYA,SAAS,cAAc,OAAuB;AAC5C,MAAI,MAAM,MAAM;AAChB,SAAO,MAAM,KAAK,MAAM,MAAM,CAAC,MAAM,IAAK;AAC1C,SAAO,MAAM,MAAM,GAAG,GAAG;AAC3B;AAEO,SAAS,eACd,MAA0C,QAAQ,KAC9B;AACpB,aAAW,QAAQ,iBAAiB;AAClC,UAAM,QAAQ,cAAc,IAAI,IAAI,GAAG,KAAK,KAAK,EAAE;AACnD,QAAI,MAAO,QAAO;AAAA,EACpB;AACA,SAAO;AACT;AAmBO,IAAM,gBAAN,MAAwC;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAiB7C,YACU,iBACA,wBACR,SACA,kBACA,YACA,eAAqC,CAAC,GACtC;AANQ;AACA;AAMR,UAAM,SAAS;AAAA,OACZ,aAAa,aAAa,eAAe,KAAK,IAAI,KAAK;AAAA,IAC1D;AACA,SAAK,YAAY,SAAS,GAAG,MAAM,MAAM;AACzC,SAAK,uBAAmB,sCAAyB,gBAAgB;AACjE,SAAK,aAAS,2BAAa,OAAO;AAClC,SAAK,YAAY,WAAW;AAC5B,QAAI,KAAK,iBAAiB,QAAQ,SAAS;AACzC,WAAK,OAAO,GAAG,SAAS,CAAC,QAAQ,KAAK,uBAAu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AO,KAAK,WAAW,SAAS,GAAG,IAAI,EAAE;AACzE,WAAO,KAAK,WAAc,MAAM,CAAC,GAAG,UAAU;AAAA,EAChD;AAAA,EAEA,MAAM,iBACJ,WACA,UACA,YACc;AAId,UAAM,SAAS,MAAM,KAAK,OAAO;AAAA,MAC/B,KAAK,WAAW,SAAS;AAAA,MACzB,CAAC;AAAA,MACD;AAAA,IACF;AACA,QAAI,OAAO,WAAW,EAAG,QAAO,CAAC;AACjC,WAAO,OACJ,QAAQ,EACR,IAAI,CAAC,UAAU,KAAK,WAAc,OAAO,UAAU,CAAC,EACpD,OAAO,OAAO;AAAA,EACnB;AAAA,EAEA,MAAM,aAA4B;AAChC,UAAM,KAAK,OAAO,KAAK;AAAA,EACzB;AAAA;AAAA,EAGA,eAAuB;AACrB,WAAO,KAAK;AAAA,EACd;AAAA,EAEA,qBAA6B;AAC3B,WAAO,KAAK;AAAA,EACd;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,YAAgC;AAC9B,WAAO,KAAK;AAAA,EACd;AACF;","names":[]}
|
package/lib/index.mjs
CHANGED
|
@@ -8,6 +8,22 @@ var ENCRYPTED_PREFIXES = ["v1:", "v2:", "v3:", "v4:"];
|
|
|
8
8
|
function isEncrypted(value) {
|
|
9
9
|
return ENCRYPTED_PREFIXES.some((p) => value.startsWith(p));
|
|
10
10
|
}
|
|
11
|
+
var KEY_PREFIX_VARS = [
|
|
12
|
+
"REDIS_KEY_PREFIX",
|
|
13
|
+
"FORKLAUNCH_REDIS_KEY_PREFIX"
|
|
14
|
+
];
|
|
15
|
+
function bareKeyPrefix(value) {
|
|
16
|
+
let end = value.length;
|
|
17
|
+
while (end > 0 && value[end - 1] === ":") end--;
|
|
18
|
+
return value.slice(0, end);
|
|
19
|
+
}
|
|
20
|
+
function redisKeyPrefix(env = process.env) {
|
|
21
|
+
for (const name of KEY_PREFIX_VARS) {
|
|
22
|
+
const value = bareKeyPrefix(env[name]?.trim() ?? "");
|
|
23
|
+
if (value) return value;
|
|
24
|
+
}
|
|
25
|
+
return void 0;
|
|
26
|
+
}
|
|
11
27
|
var RedisTtlCache = class {
|
|
12
28
|
/**
|
|
13
29
|
* Creates an instance of RedisTtlCache.
|
|
@@ -17,10 +33,15 @@ var RedisTtlCache = class {
|
|
|
17
33
|
* @param {RedisClientOptions} options - Configuration options for the Redis client
|
|
18
34
|
* @param {TelemetryOptions} telemetryOptions - Configuration options for telemetry
|
|
19
35
|
* @param {RedisCacheEncryptionOptions} encryption - Encryption configuration
|
|
36
|
+
* @param {RedisTtlCacheOptions} [cacheOptions] - Key prefix (defaults to the platform's `REDIS_KEY_PREFIX`)
|
|
20
37
|
*/
|
|
21
|
-
constructor(ttlMilliseconds, openTelemetryCollector, options, telemetryOptions, encryption) {
|
|
38
|
+
constructor(ttlMilliseconds, openTelemetryCollector, options, telemetryOptions, encryption, cacheOptions = {}) {
|
|
22
39
|
this.ttlMilliseconds = ttlMilliseconds;
|
|
23
40
|
this.openTelemetryCollector = openTelemetryCollector;
|
|
41
|
+
const prefix = bareKeyPrefix(
|
|
42
|
+
(cacheOptions.keyPrefix ?? redisKeyPrefix() ?? "").trim()
|
|
43
|
+
);
|
|
44
|
+
this.keyPrefix = prefix ? `${prefix}:` : "";
|
|
24
45
|
this.telemetryOptions = evaluateTelemetryOptions(telemetryOptions);
|
|
25
46
|
this.client = createClient(options);
|
|
26
47
|
this.encryptor = encryption.encryptor;
|
|
@@ -34,6 +55,37 @@ var RedisTtlCache = class {
|
|
|
34
55
|
client;
|
|
35
56
|
telemetryOptions;
|
|
36
57
|
encryptor;
|
|
58
|
+
/** `<prefix>:` or '' — every key and queue name the client sees starts with it. */
|
|
59
|
+
keyPrefix;
|
|
60
|
+
// ---------------------------------------------------------------------------
|
|
61
|
+
// Key prefix — callers use bare keys; Redis only ever sees prefixed ones
|
|
62
|
+
// ---------------------------------------------------------------------------
|
|
63
|
+
withPrefix(key) {
|
|
64
|
+
return this.keyPrefix + key;
|
|
65
|
+
}
|
|
66
|
+
withoutPrefix(key) {
|
|
67
|
+
return this.keyPrefix && key.startsWith(this.keyPrefix) ? key.slice(this.keyPrefix.length) : key;
|
|
68
|
+
}
|
|
69
|
+
/** Prefixed keys for a key list, or every key under a (bare) key prefix. */
|
|
70
|
+
async resolveKeys(keysOrPrefix) {
|
|
71
|
+
return Array.isArray(keysOrPrefix) ? keysOrPrefix.map((key) => this.withPrefix(key)) : this.scanKeys(this.withPrefix(keysOrPrefix));
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Every key starting with `prefix` (already prefixed), via SCAN. KEYS is in
|
|
75
|
+
* Redis's `@dangerous` category, which shared-pool ACLs deny, and it blocks
|
|
76
|
+
* the server while it walks the whole keyspace.
|
|
77
|
+
*/
|
|
78
|
+
async scanKeys(prefix) {
|
|
79
|
+
const keys = /* @__PURE__ */ new Set();
|
|
80
|
+
for await (const batch of this.client.scanIterator({
|
|
81
|
+
// same glob semantics KEYS had: the caller's prefix may itself be a pattern
|
|
82
|
+
MATCH: `${prefix}*`,
|
|
83
|
+
COUNT: 500
|
|
84
|
+
})) {
|
|
85
|
+
for (const key of batch) keys.add(String(key));
|
|
86
|
+
}
|
|
87
|
+
return [...keys];
|
|
88
|
+
}
|
|
37
89
|
// ---------------------------------------------------------------------------
|
|
38
90
|
// Encryption helpers — only active when compliance context is provided
|
|
39
91
|
// ---------------------------------------------------------------------------
|
|
@@ -86,7 +138,7 @@ var RedisTtlCache = class {
|
|
|
86
138
|
this.openTelemetryCollector.info(`Putting record into cache: ${key}`);
|
|
87
139
|
}
|
|
88
140
|
await this.client.set(
|
|
89
|
-
key,
|
|
141
|
+
this.withPrefix(key),
|
|
90
142
|
this.encryptValue(safeStringify(value), compliance),
|
|
91
143
|
{ PX: ttlMilliseconds }
|
|
92
144
|
);
|
|
@@ -102,7 +154,7 @@ var RedisTtlCache = class {
|
|
|
102
154
|
);
|
|
103
155
|
}
|
|
104
156
|
const result = await this.client.set(
|
|
105
|
-
key,
|
|
157
|
+
this.withPrefix(key),
|
|
106
158
|
this.encryptValue(safeStringify(value), compliance),
|
|
107
159
|
{ PX: ttlMilliseconds, NX: true }
|
|
108
160
|
);
|
|
@@ -112,7 +164,7 @@ var RedisTtlCache = class {
|
|
|
112
164
|
const multiCommand = this.client.multi();
|
|
113
165
|
for (const { key, value, ttlMilliseconds } of cacheRecords) {
|
|
114
166
|
multiCommand.set(
|
|
115
|
-
key,
|
|
167
|
+
this.withPrefix(key),
|
|
116
168
|
this.encryptValue(safeStringify(value), compliance),
|
|
117
169
|
{ PX: ttlMilliseconds || this.ttlMilliseconds }
|
|
118
170
|
);
|
|
@@ -121,7 +173,7 @@ var RedisTtlCache = class {
|
|
|
121
173
|
}
|
|
122
174
|
async enqueueRecord(queueName, value, compliance) {
|
|
123
175
|
await this.client.lPush(
|
|
124
|
-
queueName,
|
|
176
|
+
this.withPrefix(queueName),
|
|
125
177
|
this.encryptValue(safeStringify(value), compliance)
|
|
126
178
|
);
|
|
127
179
|
}
|
|
@@ -129,24 +181,24 @@ var RedisTtlCache = class {
|
|
|
129
181
|
const multiCommand = this.client.multi();
|
|
130
182
|
for (const value of values) {
|
|
131
183
|
multiCommand.lPush(
|
|
132
|
-
queueName,
|
|
184
|
+
this.withPrefix(queueName),
|
|
133
185
|
this.encryptValue(safeStringify(value), compliance)
|
|
134
186
|
);
|
|
135
187
|
}
|
|
136
188
|
await multiCommand.exec();
|
|
137
189
|
}
|
|
138
190
|
async deleteRecord(cacheRecordKey) {
|
|
139
|
-
await this.client.del(cacheRecordKey);
|
|
191
|
+
await this.client.del(this.withPrefix(cacheRecordKey));
|
|
140
192
|
}
|
|
141
193
|
async deleteBatchRecords(cacheRecordKeys) {
|
|
142
194
|
const multiCommand = this.client.multi();
|
|
143
195
|
for (const key of cacheRecordKeys) {
|
|
144
|
-
multiCommand.del(key);
|
|
196
|
+
multiCommand.del(this.withPrefix(key));
|
|
145
197
|
}
|
|
146
198
|
await multiCommand.exec();
|
|
147
199
|
}
|
|
148
200
|
async dequeueRecord(queueName, compliance) {
|
|
149
|
-
const value = await this.client.rPop(queueName);
|
|
201
|
+
const value = await this.client.rPop(this.withPrefix(queueName));
|
|
150
202
|
if (value === null) {
|
|
151
203
|
throw new Error(`Queue is empty: ${queueName}`);
|
|
152
204
|
}
|
|
@@ -155,7 +207,7 @@ var RedisTtlCache = class {
|
|
|
155
207
|
async dequeueBatchRecords(queueName, pageSize, compliance) {
|
|
156
208
|
const multiCommand = this.client.multi();
|
|
157
209
|
for (let i = 0; i < pageSize; i++) {
|
|
158
|
-
multiCommand.rPop(queueName);
|
|
210
|
+
multiCommand.rPop(this.withPrefix(queueName));
|
|
159
211
|
}
|
|
160
212
|
const values = await multiCommand.exec();
|
|
161
213
|
return values.map(
|
|
@@ -168,7 +220,7 @@ var RedisTtlCache = class {
|
|
|
168
220
|
).filter(Boolean);
|
|
169
221
|
}
|
|
170
222
|
async readRecord(cacheRecordKey, compliance) {
|
|
171
|
-
const [value, ttl] = await this.client.multi().get(cacheRecordKey).ttl(cacheRecordKey).exec();
|
|
223
|
+
const [value, ttl] = await this.client.multi().get(this.withPrefix(cacheRecordKey)).ttl(this.withPrefix(cacheRecordKey)).exec();
|
|
172
224
|
if (value === null) {
|
|
173
225
|
throw new Error(`Record not found for key: ${cacheRecordKey}`);
|
|
174
226
|
}
|
|
@@ -188,7 +240,7 @@ var RedisTtlCache = class {
|
|
|
188
240
|
};
|
|
189
241
|
}
|
|
190
242
|
async readBatchRecords(cacheRecordKeysOrPrefix, compliance) {
|
|
191
|
-
const keys =
|
|
243
|
+
const keys = await this.resolveKeys(cacheRecordKeysOrPrefix);
|
|
192
244
|
const multiCommand = this.client.multi();
|
|
193
245
|
for (const key of keys) {
|
|
194
246
|
multiCommand.get(key);
|
|
@@ -209,7 +261,7 @@ var RedisTtlCache = class {
|
|
|
209
261
|
);
|
|
210
262
|
if (maybeValue && ttl) {
|
|
211
263
|
acc.push({
|
|
212
|
-
key: keys[index / 2],
|
|
264
|
+
key: this.withoutPrefix(keys[index / 2]),
|
|
213
265
|
value: maybeValue,
|
|
214
266
|
ttlMilliseconds: ttl * 1e3
|
|
215
267
|
});
|
|
@@ -219,14 +271,15 @@ var RedisTtlCache = class {
|
|
|
219
271
|
}, []);
|
|
220
272
|
}
|
|
221
273
|
async listKeys(pattern_prefix) {
|
|
222
|
-
|
|
274
|
+
const keys = await this.scanKeys(this.withPrefix(pattern_prefix));
|
|
275
|
+
return keys.map((key) => this.withoutPrefix(key));
|
|
223
276
|
}
|
|
224
277
|
async peekRecord(cacheRecordKey) {
|
|
225
|
-
const result = await this.client.exists(cacheRecordKey);
|
|
278
|
+
const result = await this.client.exists(this.withPrefix(cacheRecordKey));
|
|
226
279
|
return result === 1;
|
|
227
280
|
}
|
|
228
281
|
async peekBatchRecords(cacheRecordKeysOrPrefix) {
|
|
229
|
-
const keys =
|
|
282
|
+
const keys = await this.resolveKeys(cacheRecordKeysOrPrefix);
|
|
230
283
|
const multiCommand = this.client.multi();
|
|
231
284
|
for (const key of keys) {
|
|
232
285
|
multiCommand.exists(key);
|
|
@@ -235,25 +288,38 @@ var RedisTtlCache = class {
|
|
|
235
288
|
return results.map((result) => result === 1);
|
|
236
289
|
}
|
|
237
290
|
async peekQueueRecord(queueName, compliance) {
|
|
238
|
-
const value = await this.client.lRange(queueName, -1, -1);
|
|
291
|
+
const value = await this.client.lRange(this.withPrefix(queueName), -1, -1);
|
|
239
292
|
return this.parseValue(value[0], compliance);
|
|
240
293
|
}
|
|
241
294
|
async peekQueueRecords(queueName, pageSize, compliance) {
|
|
242
|
-
const values = await this.client.lRange(
|
|
295
|
+
const values = await this.client.lRange(
|
|
296
|
+
this.withPrefix(queueName),
|
|
297
|
+
-pageSize,
|
|
298
|
+
-1
|
|
299
|
+
);
|
|
243
300
|
if (values.length === 0) return [];
|
|
244
301
|
return values.reverse().map((value) => this.parseValue(value, compliance)).filter(Boolean);
|
|
245
302
|
}
|
|
246
303
|
async disconnect() {
|
|
247
304
|
await this.client.quit();
|
|
248
305
|
}
|
|
306
|
+
/** The prefix (with its trailing `:`) applied to every key, or ''. */
|
|
307
|
+
getKeyPrefix() {
|
|
308
|
+
return this.keyPrefix;
|
|
309
|
+
}
|
|
249
310
|
getTtlMilliseconds() {
|
|
250
311
|
return this.ttlMilliseconds;
|
|
251
312
|
}
|
|
313
|
+
/**
|
|
314
|
+
* The raw node-redis client. Commands sent through it bypass the key
|
|
315
|
+
* prefix: on a shared Redis, prefix keys with `getKeyPrefix()` yourself.
|
|
316
|
+
*/
|
|
252
317
|
getClient() {
|
|
253
318
|
return this.client;
|
|
254
319
|
}
|
|
255
320
|
};
|
|
256
321
|
export {
|
|
257
|
-
RedisTtlCache
|
|
322
|
+
RedisTtlCache,
|
|
323
|
+
redisKeyPrefix
|
|
258
324
|
};
|
|
259
325
|
//# sourceMappingURL=index.mjs.map
|
package/lib/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../index.ts"],"sourcesContent":["import { safeParse, safeStringify } from '@forklaunch/common';\nimport {\n type ComplianceContext,\n TtlCache,\n TtlCacheRecord\n} from '@forklaunch/core/cache';\nimport {\n evaluateTelemetryOptions,\n MetricsDefinition,\n OpenTelemetryCollector,\n TelemetryOptions\n} from '@forklaunch/core/http';\nimport { type FieldEncryptor } from '@forklaunch/core/persistence';\nimport { createClient, RedisClientOptions } from 'redis';\n\n/**\n * Type representing a raw reply from Redis commands.\n * Can be a string, number, Buffer, null, undefined, or array of raw replies.\n */\ntype RedisCommandRawReply =\n string | number | Buffer | null | undefined | Array<RedisCommandRawReply>;\n\n// v4 is the randomized, stamped envelope native (Rust) writers produce.\nconst ENCRYPTED_PREFIXES = ['v1:', 'v2:', 'v3:', 'v4:'] as const;\n\nfunction isEncrypted(value: string): boolean {\n return ENCRYPTED_PREFIXES.some((p) => value.startsWith(p));\n}\n\n/**\n * Options for configuring encryption on the Redis cache.\n * Required — every consumer must explicitly configure encryption.\n */\nexport interface RedisCacheEncryptionOptions {\n /** The FieldEncryptor instance to use for encrypting cache values. */\n encryptor: FieldEncryptor;\n}\n\n/**\n * Class representing a Redis-based TTL (Time-To-Live) cache.\n * Implements the TtlCache interface to provide caching functionality with automatic expiration.\n *\n * Encryption is activated per-operation when a `compliance` context is provided.\n * Without it, values are stored and read as plaintext.\n */\nexport class RedisTtlCache implements TtlCache {\n private client;\n private telemetryOptions;\n private encryptor?: FieldEncryptor;\n\n /**\n * Creates an instance of RedisTtlCache.\n *\n * @param {number} ttlMilliseconds - The default Time-To-Live in milliseconds for cache entries\n * @param {OpenTelemetryCollector<MetricsDefinition>} openTelemetryCollector - Collector for OpenTelemetry metrics\n * @param {RedisClientOptions} options - Configuration options for the Redis client\n * @param {TelemetryOptions} telemetryOptions - Configuration options for telemetry\n * @param {RedisCacheEncryptionOptions} encryption - Encryption configuration\n */\n constructor(\n private ttlMilliseconds: number,\n private openTelemetryCollector: OpenTelemetryCollector<MetricsDefinition>,\n options: RedisClientOptions,\n telemetryOptions: TelemetryOptions,\n encryption: RedisCacheEncryptionOptions\n ) {\n this.telemetryOptions = evaluateTelemetryOptions(telemetryOptions);\n this.client = createClient(options);\n this.encryptor = encryption.encryptor;\n if (this.telemetryOptions.enabled.logging) {\n this.client.on('error', (err) => this.openTelemetryCollector.error(err));\n this.client.connect().catch(this.openTelemetryCollector.error);\n }\n }\n\n // ---------------------------------------------------------------------------\n // Encryption helpers — only active when compliance context is provided\n // ---------------------------------------------------------------------------\n\n private encryptValue(\n serialized: string,\n compliance?: ComplianceContext\n ): string {\n if (!compliance || !this.encryptor) return serialized;\n return (\n this.encryptor.encrypt(serialized, compliance.tenantId) ?? serialized\n );\n }\n\n private decryptValue(value: string, compliance?: ComplianceContext): string {\n if (!compliance || !this.encryptor) return value;\n if (!isEncrypted(value)) return value;\n // If a value is encrypted but we cannot decrypt it, treat the entry as\n // unreadable rather than returning the ciphertext. Returning the raw\n // bytes lets callers (e.g. cache services that JSON.parse the result)\n // surface garbage as if it were a successful read, masking key/tenant\n // mismatches and corrupting downstream consumers. Throwing forces the\n // caller's catch path (cache miss) to run.\n let decrypted: string | null;\n try {\n decrypted = this.encryptor.decrypt(value, compliance.tenantId);\n } catch (err) {\n throw new Error(\n `Redis: failed to decrypt value for tenant ${compliance.tenantId}: ${err instanceof Error ? err.message : String(err)}`,\n { cause: err }\n );\n }\n if (decrypted === null) {\n throw new Error(\n `Redis: encrypted value for tenant ${compliance.tenantId} could not be decrypted (null result)`\n );\n }\n return decrypted;\n }\n\n private parseValue<T>(\n value: RedisCommandRawReply,\n compliance?: ComplianceContext\n ): T {\n if (value == null) {\n return null as T;\n }\n\n if (Array.isArray(value)) {\n return value.map((v) => this.parseValue<T>(v, compliance)) as T;\n }\n\n if (Buffer.isBuffer(value)) {\n return value.toJSON() as T;\n }\n\n switch (typeof value) {\n case 'object':\n case 'string':\n return safeParse(this.decryptValue(String(value), compliance)) as T;\n case 'number':\n return value as T;\n }\n }\n\n // ---------------------------------------------------------------------------\n // TtlCache implementation\n // ---------------------------------------------------------------------------\n\n async putRecord<T>(\n { key, value, ttlMilliseconds = this.ttlMilliseconds }: TtlCacheRecord<T>,\n compliance?: ComplianceContext\n ): Promise<void> {\n if (this.telemetryOptions.enabled.logging) {\n this.openTelemetryCollector.info(`Putting record into cache: ${key}`);\n }\n await this.client.set(\n key,\n this.encryptValue(safeStringify(value), compliance),\n { PX: ttlMilliseconds }\n );\n }\n\n /**\n * Atomic set-if-absent with expiry (`SET key value NX PX ttl`). Answers\n * `true` only for the first writer of the key.\n */\n async putRecordIfAbsent<T>(\n { key, value, ttlMilliseconds = this.ttlMilliseconds }: TtlCacheRecord<T>,\n compliance?: ComplianceContext\n ): Promise<boolean> {\n if (this.telemetryOptions.enabled.logging) {\n this.openTelemetryCollector.info(\n `Putting record into cache if absent: ${key}`\n );\n }\n const result = await this.client.set(\n key,\n this.encryptValue(safeStringify(value), compliance),\n { PX: ttlMilliseconds, NX: true }\n );\n return result === 'OK';\n }\n\n async putBatchRecords<T>(\n cacheRecords: TtlCacheRecord<T>[],\n compliance?: ComplianceContext\n ): Promise<void> {\n const multiCommand = this.client.multi();\n for (const { key, value, ttlMilliseconds } of cacheRecords) {\n multiCommand.set(\n key,\n this.encryptValue(safeStringify(value), compliance),\n { PX: ttlMilliseconds || this.ttlMilliseconds }\n );\n }\n await multiCommand.exec();\n }\n\n async enqueueRecord<T>(\n queueName: string,\n value: T,\n compliance?: ComplianceContext\n ): Promise<void> {\n await this.client.lPush(\n queueName,\n this.encryptValue(safeStringify(value), compliance)\n );\n }\n\n async enqueueBatchRecords<T>(\n queueName: string,\n values: T[],\n compliance?: ComplianceContext\n ): Promise<void> {\n const multiCommand = this.client.multi();\n for (const value of values) {\n multiCommand.lPush(\n queueName,\n this.encryptValue(safeStringify(value), compliance)\n );\n }\n await multiCommand.exec();\n }\n\n async deleteRecord(cacheRecordKey: string): Promise<void> {\n await this.client.del(cacheRecordKey);\n }\n\n async deleteBatchRecords(cacheRecordKeys: string[]): Promise<void> {\n const multiCommand = this.client.multi();\n for (const key of cacheRecordKeys) {\n multiCommand.del(key);\n }\n await multiCommand.exec();\n }\n\n async dequeueRecord<T>(\n queueName: string,\n compliance?: ComplianceContext\n ): Promise<T> {\n const value = await this.client.rPop(queueName);\n if (value === null) {\n throw new Error(`Queue is empty: ${queueName}`);\n }\n return safeParse(this.decryptValue(value, compliance)) as T;\n }\n\n async dequeueBatchRecords<T>(\n queueName: string,\n pageSize: number,\n compliance?: ComplianceContext\n ): Promise<T[]> {\n const multiCommand = this.client.multi();\n for (let i = 0; i < pageSize; i++) {\n multiCommand.rPop(queueName);\n }\n const values = await multiCommand.exec();\n return values\n .map((value) =>\n // node-redis hands back `ReplyUnion`, while the parse/serialize helpers\n // are typed against `RedisCommandRawReply`. The two unions describe the\n // same wire values but share no member, so TypeScript sees no overlap.\n this.parseValue<T>(value as unknown as RedisCommandRawReply, compliance)\n )\n .filter(Boolean);\n }\n\n async readRecord<T>(\n cacheRecordKey: string,\n compliance?: ComplianceContext\n ): Promise<TtlCacheRecord<T>> {\n const [value, ttl] = await this.client\n .multi()\n .get(cacheRecordKey)\n .ttl(cacheRecordKey)\n .exec();\n if (value === null) {\n throw new Error(`Record not found for key: ${cacheRecordKey}`);\n }\n\n return {\n key: cacheRecordKey,\n value: this.parseValue<T>(\n // node-redis hands back `ReplyUnion`, while the parse/serialize helpers\n // are typed against `RedisCommandRawReply`. The two unions describe the\n // same wire values but share no member, so TypeScript sees no overlap.\n value as unknown as RedisCommandRawReply,\n compliance\n ),\n ttlMilliseconds:\n this.parseValue<number>(\n ttl as unknown as RedisCommandRawReply,\n compliance\n ) * 1000\n };\n }\n\n async readBatchRecords<T>(\n cacheRecordKeysOrPrefix: string[] | string,\n compliance?: ComplianceContext\n ): Promise<TtlCacheRecord<T>[]> {\n const keys = Array.isArray(cacheRecordKeysOrPrefix)\n ? cacheRecordKeysOrPrefix\n : await this.client.keys(cacheRecordKeysOrPrefix + '*');\n const multiCommand = this.client.multi();\n for (const key of keys) {\n multiCommand.get(key);\n multiCommand.ttl(key);\n }\n const values = await multiCommand.exec();\n return values.reduce<TtlCacheRecord<T>[]>((acc, value, index) => {\n if (index % 2 === 0) {\n const maybeValue = this.parseValue<T>(\n // `ReplyUnion` and `RedisCommandRawReply` name the same wire values\n // through disjoint unions, so neither is assignable to the other.\n value as unknown as RedisCommandRawReply,\n compliance\n );\n const ttl = this.parseValue<number>(\n values[index + 1] as unknown as RedisCommandRawReply,\n compliance\n );\n if (maybeValue && ttl) {\n acc.push({\n key: keys[index / 2],\n value: maybeValue,\n ttlMilliseconds: ttl * 1000\n });\n }\n }\n return acc;\n }, []);\n }\n\n async listKeys(pattern_prefix: string): Promise<string[]> {\n return this.client.keys(pattern_prefix + '*');\n }\n\n async peekRecord(cacheRecordKey: string): Promise<boolean> {\n const result = await this.client.exists(cacheRecordKey);\n return result === 1;\n }\n\n async peekBatchRecords(\n cacheRecordKeysOrPrefix: string[] | string\n ): Promise<boolean[]> {\n const keys = Array.isArray(cacheRecordKeysOrPrefix)\n ? cacheRecordKeysOrPrefix\n : await this.client.keys(cacheRecordKeysOrPrefix + '*');\n const multiCommand = this.client.multi();\n for (const key of keys) {\n multiCommand.exists(key);\n }\n const results = await multiCommand.exec();\n return results.map((result) => (result as unknown as number) === 1);\n }\n\n async peekQueueRecord<T>(\n queueName: string,\n compliance?: ComplianceContext\n ): Promise<T> {\n // Queues use lPush + rPop, so the next item to dequeue lives at the\n // tail of the list, not the head. Reading lRange(0, 0) would return the\n // most-recently-pushed item — the opposite of dequeue order.\n const value = await this.client.lRange(queueName, -1, -1);\n return this.parseValue<T>(value[0], compliance);\n }\n\n async peekQueueRecords<T>(\n queueName: string,\n pageSize: number,\n compliance?: ComplianceContext\n ): Promise<T[]> {\n // Tail-relative range: the last `pageSize` items, where the very last\n // item is the next to be dequeued. Redis returns them in list order\n // (oldest-tail-end first), so reverse to put next-to-dequeue first.\n const values = await this.client.lRange(queueName, -pageSize, -1);\n if (values.length === 0) return [];\n return values\n .reverse()\n .map((value) => this.parseValue<T>(value, compliance))\n .filter(Boolean);\n }\n\n async disconnect(): Promise<void> {\n await this.client.quit();\n }\n\n getTtlMilliseconds(): number {\n return this.ttlMilliseconds;\n }\n\n getClient(): typeof this.client {\n return this.client;\n }\n}\n"],"mappings":";AAAA,SAAS,WAAW,qBAAqB;AAMzC;AAAA,EACE;AAAA,OAIK;AAEP,SAAS,oBAAwC;AAUjD,IAAM,qBAAqB,CAAC,OAAO,OAAO,OAAO,KAAK;AAEtD,SAAS,YAAY,OAAwB;AAC3C,SAAO,mBAAmB,KAAK,CAAC,MAAM,MAAM,WAAW,CAAC,CAAC;AAC3D;AAkBO,IAAM,gBAAN,MAAwC;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAc7C,YACU,iBACA,wBACR,SACA,kBACA,YACA;AALQ;AACA;AAKR,SAAK,mBAAmB,yBAAyB,gBAAgB;AACjE,SAAK,SAAS,aAAa,OAAO;AAClC,SA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BACJ,yBACoB;AACpB,UAAM,OAAO,MAAM,QAAQ,uBAAuB,IAC9C,0BACA,MAAM,KAAK,OAAO,KAAK,0BAA0B,GAAG;AACxD,UAAM,eAAe,KAAK,OAAO,MAAM;AACvC,eAAW,OAAO,MAAM;AACtB,mBAAa,OAAO,GAAG;AAAA,IACzB;AACA,UAAM,UAAU,MAAM,aAAa,KAAK;AACxC,WAAO,QAAQ,IAAI,CAAC,WAAY,WAAiC,CAAC;AAAA,EACpE;AAAA,EAEA,MAAM,gBACJ,WACA,YACY;AAIZ,UAAM,QAAQ,MAAM,KAAK,OAAO,OAAO,WAAW,IAAI,EAAE;AACxD,WAAO,KAAK,WAAc,MAAM,CAAC,GAAG,UAAU;AAAA,EAChD;AAAA,EAEA,MAAM,iBACJ,WACA,UACA,YACc;AAId,UAAM,SAAS,MAAM,KAAK,OAAO,OAAO,WAAW,CAAC,UAAU,EAAE;AAChE,QAAI,OAAO,WAAW,EAAG,QAAO,CAAC;AACjC,WAAO,OACJ,QAAQ,EACR,IAAI,CAAC,UAAU,KAAK,WAAc,OAAO,UAAU,CAAC,EACpD,OAAO,OAAO;AAAA,EACnB;AAAA,EAEA,MAAM,aAA4B;AAChC,UAAM,KAAK,OAAO,KAAK;AAAA,EACzB;AAAA,EAEA,qBAA6B;AAC3B,WAAO,KAAK;AAAA,EACd;AAAA,EAEA,YAAgC;AAC9B,WAAO,KAAK;AAAA,EACd;AACF;","names":[]}
|
|
1
|
+
{"version":3,"sources":["../index.ts"],"sourcesContent":["import { safeParse, safeStringify } from '@forklaunch/common';\nimport {\n type ComplianceContext,\n TtlCache,\n TtlCacheRecord\n} from '@forklaunch/core/cache';\nimport {\n evaluateTelemetryOptions,\n MetricsDefinition,\n OpenTelemetryCollector,\n TelemetryOptions\n} from '@forklaunch/core/http';\nimport { type FieldEncryptor } from '@forklaunch/core/persistence';\nimport { createClient, RedisClientOptions } from 'redis';\n\n/**\n * Type representing a raw reply from Redis commands.\n * Can be a string, number, Buffer, null, undefined, or array of raw replies.\n */\ntype RedisCommandRawReply =\n string | number | Buffer | null | undefined | Array<RedisCommandRawReply>;\n\n// v4 is the randomized, stamped envelope native (Rust) writers produce.\nconst ENCRYPTED_PREFIXES = ['v1:', 'v2:', 'v3:', 'v4:'] as const;\n\nfunction isEncrypted(value: string): boolean {\n return ENCRYPTED_PREFIXES.some((p) => value.startsWith(p));\n}\n\n/**\n * Options for configuring encryption on the Redis cache.\n * Required — every consumer must explicitly configure encryption.\n */\nexport interface RedisCacheEncryptionOptions {\n /** The FieldEncryptor instance to use for encrypting cache values. */\n encryptor: FieldEncryptor;\n}\n\n/** Env vars the platform sets for an app on a shared (partitioned) Redis. */\nconst KEY_PREFIX_VARS = [\n 'REDIS_KEY_PREFIX',\n 'FORKLAUNCH_REDIS_KEY_PREFIX'\n] as const;\n\n/**\n * The app's key prefix on a shared Redis, without a trailing separator, or\n * undefined on a dedicated cache (local Redis, own ElastiCache).\n *\n * On a shared pool each app is an ACL user whose key pattern is\n * `~<prefix>:*`; a key outside it is answered NOPERM. The platform passes the\n * prefix as `REDIS_KEY_PREFIX` (`FORKLAUNCH_REDIS_KEY_PREFIX` is the same\n * value under the platform's own name).\n */\n/** `value` trimmed, with any trailing `:` separators removed. */\nfunction bareKeyPrefix(value: string): string {\n let end = value.length;\n while (end > 0 && value[end - 1] === ':') end--;\n return value.slice(0, end);\n}\n\nexport function redisKeyPrefix(\n env: Record<string, string | undefined> = process.env\n): string | undefined {\n for (const name of KEY_PREFIX_VARS) {\n const value = bareKeyPrefix(env[name]?.trim() ?? '');\n if (value) return value;\n }\n return undefined;\n}\n\n/** Options beyond the Redis client's own. */\nexport interface RedisTtlCacheOptions {\n /**\n * Prefix for every key and queue name, joined with `:`. Defaults to the\n * platform's `REDIS_KEY_PREFIX`, so an app on a shared Redis stays inside\n * its namespace without configuring anything. Pass `''` to turn it off.\n */\n keyPrefix?: string;\n}\n\n/**\n * Class representing a Redis-based TTL (Time-To-Live) cache.\n * Implements the TtlCache interface to provide caching functionality with automatic expiration.\n *\n * Encryption is activated per-operation when a `compliance` context is provided.\n * Without it, values are stored and read as plaintext.\n */\nexport class RedisTtlCache implements TtlCache {\n private client;\n private telemetryOptions;\n private encryptor?: FieldEncryptor;\n /** `<prefix>:` or '' — every key and queue name the client sees starts with it. */\n private readonly keyPrefix: string;\n\n /**\n * Creates an instance of RedisTtlCache.\n *\n * @param {number} ttlMilliseconds - The default Time-To-Live in milliseconds for cache entries\n * @param {OpenTelemetryCollector<MetricsDefinition>} openTelemetryCollector - Collector for OpenTelemetry metrics\n * @param {RedisClientOptions} options - Configuration options for the Redis client\n * @param {TelemetryOptions} telemetryOptions - Configuration options for telemetry\n * @param {RedisCacheEncryptionOptions} encryption - Encryption configuration\n * @param {RedisTtlCacheOptions} [cacheOptions] - Key prefix (defaults to the platform's `REDIS_KEY_PREFIX`)\n */\n constructor(\n private ttlMilliseconds: number,\n private openTelemetryCollector: OpenTelemetryCollector<MetricsDefinition>,\n options: RedisClientOptions,\n telemetryOptions: TelemetryOptions,\n encryption: RedisCacheEncryptionOptions,\n cacheOptions: RedisTtlCacheOptions = {}\n ) {\n const prefix = bareKeyPrefix(\n (cacheOptions.keyPrefix ?? redisKeyPrefix() ?? '').trim()\n );\n this.keyPrefix = prefix ? `${prefix}:` : '';\n this.telemetryOptions = evaluateTelemetryOptions(telemetryOptions);\n this.client = createClient(options);\n this.encryptor = encryption.encryptor;\n if (this.telemetryOptions.enabled.logging) {\n this.client.on('error', (err) => this.openTelemetryCollector.error(err));\n this.client.connect().catch(this.openTelemetryCollector.error);\n }\n }\n\n // ---------------------------------------------------------------------------\n // Key prefix — callers use bare keys; Redis only ever sees prefixed ones\n // ---------------------------------------------------------------------------\n\n private withPrefix(key: string): string {\n return this.keyPrefix + key;\n }\n\n private withoutPrefix(key: string): string {\n return this.keyPrefix && key.startsWith(this.keyPrefix)\n ? key.slice(this.keyPrefix.length)\n : key;\n }\n\n /** Prefixed keys for a key list, or every key under a (bare) key prefix. */\n private async resolveKeys(\n keysOrPrefix: string[] | string\n ): Promise<string[]> {\n return Array.isArray(keysOrPrefix)\n ? keysOrPrefix.map((key) => this.withPrefix(key))\n : this.scanKeys(this.withPrefix(keysOrPrefix));\n }\n\n /**\n * Every key starting with `prefix` (already prefixed), via SCAN. KEYS is in\n * Redis's `@dangerous` category, which shared-pool ACLs deny, and it blocks\n * the server while it walks the whole keyspace.\n */\n private async scanKeys(prefix: string): Promise<string[]> {\n const keys = new Set<string>();\n for await (const batch of this.client.scanIterator({\n // same glob semantics KEYS had: the caller's prefix may itself be a pattern\n MATCH: `${prefix}*`,\n COUNT: 500\n })) {\n for (const key of batch) keys.add(String(key));\n }\n return [...keys];\n }\n\n // ---------------------------------------------------------------------------\n // Encryption helpers — only active when compliance context is provided\n // ---------------------------------------------------------------------------\n\n private encryptValue(\n serialized: string,\n compliance?: ComplianceContext\n ): string {\n if (!compliance || !this.encryptor) return serialized;\n return (\n this.encryptor.encrypt(serialized, compliance.tenantId) ?? serialized\n );\n }\n\n private decryptValue(value: string, compliance?: ComplianceContext): string {\n if (!compliance || !this.encryptor) return value;\n if (!isEncrypted(value)) return value;\n // If a value is encrypted but we cannot decrypt it, treat the entry as\n // unreadable rather than returning the ciphertext. Returning the raw\n // bytes lets callers (e.g. cache services that JSON.parse the result)\n // surface garbage as if it were a successful read, masking key/tenant\n // mismatches and corrupting downstream consumers. Throwing forces the\n // caller's catch path (cache miss) to run.\n let decrypted: string | null;\n try {\n decrypted = this.encryptor.decrypt(value, compliance.tenantId);\n } catch (err) {\n throw new Error(\n `Redis: failed to decrypt value for tenant ${compliance.tenantId}: ${err instanceof Error ? err.message : String(err)}`,\n { cause: err }\n );\n }\n if (decrypted === null) {\n throw new Error(\n `Redis: encrypted value for tenant ${compliance.tenantId} could not be decrypted (null result)`\n );\n }\n return decrypted;\n }\n\n private parseValue<T>(\n value: RedisCommandRawReply,\n compliance?: ComplianceContext\n ): T {\n if (value == null) {\n return null as T;\n }\n\n if (Array.isArray(value)) {\n return value.map((v) => this.parseValue<T>(v, compliance)) as T;\n }\n\n if (Buffer.isBuffer(value)) {\n return value.toJSON() as T;\n }\n\n switch (typeof value) {\n case 'object':\n case 'string':\n return safeParse(this.decryptValue(String(value), compliance)) as T;\n case 'number':\n return value as T;\n }\n }\n\n // ---------------------------------------------------------------------------\n // TtlCache implementation\n // ---------------------------------------------------------------------------\n\n async putRecord<T>(\n { key, value, ttlMilliseconds = this.ttlMilliseconds }: TtlCacheRecord<T>,\n compliance?: ComplianceContext\n ): Promise<void> {\n if (this.telemetryOptions.enabled.logging) {\n this.openTelemetryCollector.info(`Putting record into cache: ${key}`);\n }\n await this.client.set(\n this.withPrefix(key),\n this.encryptValue(safeStringify(value), compliance),\n { PX: ttlMilliseconds }\n );\n }\n\n /**\n * Atomic set-if-absent with expiry (`SET key value NX PX ttl`). Answers\n * `true` only for the first writer of the key.\n */\n async putRecordIfAbsent<T>(\n { key, value, ttlMilliseconds = this.ttlMilliseconds }: TtlCacheRecord<T>,\n compliance?: ComplianceContext\n ): Promise<boolean> {\n if (this.telemetryOptions.enabled.logging) {\n this.openTelemetryCollector.info(\n `Putting record into cache if absent: ${key}`\n );\n }\n const result = await this.client.set(\n this.withPrefix(key),\n this.encryptValue(safeStringify(value), compliance),\n { PX: ttlMilliseconds, NX: true }\n );\n return result === 'OK';\n }\n\n async putBatchRecords<T>(\n cacheRecords: TtlCacheRecord<T>[],\n compliance?: ComplianceContext\n ): Promise<void> {\n const multiCommand = this.client.multi();\n for (const { key, value, ttlMilliseconds } of cacheRecords) {\n multiCommand.set(\n this.withPrefix(key),\n this.encryptValue(safeStringify(value), compliance),\n { PX: ttlMilliseconds || this.ttlMilliseconds }\n );\n }\n await multiCommand.exec();\n }\n\n async enqueueRecord<T>(\n queueName: string,\n value: T,\n compliance?: ComplianceContext\n ): Promise<void> {\n await this.client.lPush(\n this.withPrefix(queueName),\n this.encryptValue(safeStringify(value), compliance)\n );\n }\n\n async enqueueBatchRecords<T>(\n queueName: string,\n values: T[],\n compliance?: ComplianceContext\n ): Promise<void> {\n const multiCommand = this.client.multi();\n for (const value of values) {\n multiCommand.lPush(\n this.withPrefix(queueName),\n this.encryptValue(safeStringify(value), compliance)\n );\n }\n await multiCommand.exec();\n }\n\n async deleteRecord(cacheRecordKey: string): Promise<void> {\n await this.client.del(this.withPrefix(cacheRecordKey));\n }\n\n async deleteBatchRecords(cacheRecordKeys: string[]): Promise<void> {\n const multiCommand = this.client.multi();\n for (const key of cacheRecordKeys) {\n multiCommand.del(this.withPrefix(key));\n }\n await multiCommand.exec();\n }\n\n async dequeueRecord<T>(\n queueName: string,\n compliance?: ComplianceContext\n ): Promise<T> {\n const value = await this.client.rPop(this.withPrefix(queueName));\n if (value === null) {\n throw new Error(`Queue is empty: ${queueName}`);\n }\n return safeParse(this.decryptValue(value, compliance)) as T;\n }\n\n async dequeueBatchRecords<T>(\n queueName: string,\n pageSize: number,\n compliance?: ComplianceContext\n ): Promise<T[]> {\n const multiCommand = this.client.multi();\n for (let i = 0; i < pageSize; i++) {\n multiCommand.rPop(this.withPrefix(queueName));\n }\n const values = await multiCommand.exec();\n return values\n .map((value) =>\n // node-redis hands back `ReplyUnion`, while the parse/serialize helpers\n // are typed against `RedisCommandRawReply`. The two unions describe the\n // same wire values but share no member, so TypeScript sees no overlap.\n this.parseValue<T>(value as unknown as RedisCommandRawReply, compliance)\n )\n .filter(Boolean);\n }\n\n async readRecord<T>(\n cacheRecordKey: string,\n compliance?: ComplianceContext\n ): Promise<TtlCacheRecord<T>> {\n const [value, ttl] = await this.client\n .multi()\n .get(this.withPrefix(cacheRecordKey))\n .ttl(this.withPrefix(cacheRecordKey))\n .exec();\n if (value === null) {\n throw new Error(`Record not found for key: ${cacheRecordKey}`);\n }\n\n return {\n key: cacheRecordKey,\n value: this.parseValue<T>(\n // node-redis hands back `ReplyUnion`, while the parse/serialize helpers\n // are typed against `RedisCommandRawReply`. The two unions describe the\n // same wire values but share no member, so TypeScript sees no overlap.\n value as unknown as RedisCommandRawReply,\n compliance\n ),\n ttlMilliseconds:\n this.parseValue<number>(\n ttl as unknown as RedisCommandRawReply,\n compliance\n ) * 1000\n };\n }\n\n async readBatchRecords<T>(\n cacheRecordKeysOrPrefix: string[] | string,\n compliance?: ComplianceContext\n ): Promise<TtlCacheRecord<T>[]> {\n const keys = await this.resolveKeys(cacheRecordKeysOrPrefix);\n const multiCommand = this.client.multi();\n for (const key of keys) {\n multiCommand.get(key);\n multiCommand.ttl(key);\n }\n const values = await multiCommand.exec();\n return values.reduce<TtlCacheRecord<T>[]>((acc, value, index) => {\n if (index % 2 === 0) {\n const maybeValue = this.parseValue<T>(\n // `ReplyUnion` and `RedisCommandRawReply` name the same wire values\n // through disjoint unions, so neither is assignable to the other.\n value as unknown as RedisCommandRawReply,\n compliance\n );\n const ttl = this.parseValue<number>(\n values[index + 1] as unknown as RedisCommandRawReply,\n compliance\n );\n if (maybeValue && ttl) {\n acc.push({\n key: this.withoutPrefix(keys[index / 2]),\n value: maybeValue,\n ttlMilliseconds: ttl * 1000\n });\n }\n }\n return acc;\n }, []);\n }\n\n async listKeys(pattern_prefix: string): Promise<string[]> {\n const keys = await this.scanKeys(this.withPrefix(pattern_prefix));\n return keys.map((key) => this.withoutPrefix(key));\n }\n\n async peekRecord(cacheRecordKey: string): Promise<boolean> {\n const result = await this.client.exists(this.withPrefix(cacheRecordKey));\n return result === 1;\n }\n\n async peekBatchRecords(\n cacheRecordKeysOrPrefix: string[] | string\n ): Promise<boolean[]> {\n const keys = await this.resolveKeys(cacheRecordKeysOrPrefix);\n const multiCommand = this.client.multi();\n for (const key of keys) {\n multiCommand.exists(key);\n }\n const results = await multiCommand.exec();\n return results.map((result) => (result as unknown as number) === 1);\n }\n\n async peekQueueRecord<T>(\n queueName: string,\n compliance?: ComplianceContext\n ): Promise<T> {\n // Queues use lPush + rPop, so the next item to dequeue lives at the\n // tail of the list, not the head. Reading lRange(0, 0) would return the\n // most-recently-pushed item — the opposite of dequeue order.\n const value = await this.client.lRange(this.withPrefix(queueName), -1, -1);\n return this.parseValue<T>(value[0], compliance);\n }\n\n async peekQueueRecords<T>(\n queueName: string,\n pageSize: number,\n compliance?: ComplianceContext\n ): Promise<T[]> {\n // Tail-relative range: the last `pageSize` items, where the very last\n // item is the next to be dequeued. Redis returns them in list order\n // (oldest-tail-end first), so reverse to put next-to-dequeue first.\n const values = await this.client.lRange(\n this.withPrefix(queueName),\n -pageSize,\n -1\n );\n if (values.length === 0) return [];\n return values\n .reverse()\n .map((value) => this.parseValue<T>(value, compliance))\n .filter(Boolean);\n }\n\n async disconnect(): Promise<void> {\n await this.client.quit();\n }\n\n /** The prefix (with its trailing `:`) applied to every key, or ''. */\n getKeyPrefix(): string {\n return this.keyPrefix;\n }\n\n getTtlMilliseconds(): number {\n return this.ttlMilliseconds;\n }\n\n /**\n * The raw node-redis client. Commands sent through it bypass the key\n * prefix: on a shared Redis, prefix keys with `getKeyPrefix()` yourself.\n */\n getClient(): typeof this.client {\n return this.client;\n }\n}\n"],"mappings":";AAAA,SAAS,WAAW,qBAAqB;AAMzC;AAAA,EACE;AAAA,OAIK;AAEP,SAAS,oBAAwC;AAUjD,IAAM,qBAAqB,CAAC,OAAO,OAAO,OAAO,KAAK;AAEtD,SAAS,YAAY,OAAwB;AAC3C,SAAO,mBAAmB,KAAK,CAAC,MAAM,MAAM,WAAW,CAAC,CAAC;AAC3D;AAYA,IAAM,kBAAkB;AAAA,EACtB;AAAA,EACA;AACF;AAYA,SAAS,cAAc,OAAuB;AAC5C,MAAI,MAAM,MAAM;AAChB,SAAO,MAAM,KAAK,MAAM,MAAM,CAAC,MAAM,IAAK;AAC1C,SAAO,MAAM,MAAM,GAAG,GAAG;AAC3B;AAEO,SAAS,eACd,MAA0C,QAAQ,KAC9B;AACpB,aAAW,QAAQ,iBAAiB;AAClC,UAAM,QAAQ,cAAc,IAAI,IAAI,GAAG,KAAK,KAAK,EAAE;AACnD,QAAI,MAAO,QAAO;AAAA,EACpB;AACA,SAAO;A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3
|
+
"version": "1.6.0",
|
|
4
4
|
"description": "Redis infrastructure for ForkLaunch components.",
|
|
5
5
|
"homepage": "https://github.com/forklaunch/forklaunch-js#readme",
|
|
6
6
|
"bugs": {
|
|
@@ -31,19 +31,19 @@
|
|
|
31
31
|
"lib/**"
|
|
32
32
|
],
|
|
33
33
|
"dependencies": {
|
|
34
|
-
"@forklaunch/common": "^
|
|
35
|
-
"@forklaunch/core": "^4.
|
|
36
|
-
"redis": "^6.
|
|
34
|
+
"@forklaunch/common": "^2.0.0",
|
|
35
|
+
"@forklaunch/core": "^4.4.0",
|
|
36
|
+
"redis": "^6.4.0"
|
|
37
37
|
},
|
|
38
38
|
"devDependencies": {
|
|
39
39
|
"@eslint/js": "^10.0.1",
|
|
40
|
-
"globals": "^17.
|
|
40
|
+
"globals": "^17.15.0",
|
|
41
41
|
"prettier": "^3.10.1",
|
|
42
|
-
"testcontainers": "^12.2.
|
|
42
|
+
"testcontainers": "^12.2.2",
|
|
43
43
|
"ts-node": "^10.9.2",
|
|
44
44
|
"tsup": "^9.0.0",
|
|
45
45
|
"typescript": "^7.0.2",
|
|
46
|
-
"typescript-eslint": "^8.
|
|
46
|
+
"typescript-eslint": "^8.74.0"
|
|
47
47
|
},
|
|
48
48
|
"scripts": {
|
|
49
49
|
"build": "tsc --noEmit && tsup ./index.ts --format cjs,esm --no-splitting --tsconfig tsconfig.json --out-dir lib --clean --sourcemap && tsc -p tsconfig.build.json --emitDeclarationOnly && if [ -f eject-infrastructure-package.bash ]; then pnpm package:eject; fi",
|