@nathapp/nestjs-cache 2.1.0-rc.0 → 2.1.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/CLAUDE.md +149 -0
- package/README.md +330 -420
- package/TODO.md +56 -0
- package/dist/enum/strategy.enum.js.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/inteceptor/index.d.ts +1 -2
- package/dist/inteceptor/index.js +1 -2
- package/dist/inteceptor/index.js.map +1 -1
- package/dist/interceptor/cache-invalidate.interceptor.js.map +1 -0
- package/dist/{inteceptor → interceptor}/cache.interceptor.js +9 -3
- package/dist/interceptor/cache.interceptor.js.map +1 -0
- package/dist/interceptor/index.d.ts +2 -0
- package/dist/interceptor/index.js +19 -0
- package/dist/interceptor/index.js.map +1 -0
- package/dist/interface/cache-invalidate.interface.d.ts +8 -0
- package/dist/interface/enhanced-cache-options.interface.d.ts +16 -0
- package/dist/interface/health.interface.d.ts +19 -0
- package/dist/interface/health.interface.js +3 -0
- package/dist/interface/health.interface.js.map +1 -0
- package/dist/interface/index.d.ts +2 -0
- package/dist/interface/index.js +2 -0
- package/dist/interface/index.js.map +1 -1
- package/dist/interface/metrics.interface.d.ts +17 -0
- package/dist/interface/metrics.interface.js +3 -0
- package/dist/interface/metrics.interface.js.map +1 -0
- package/dist/module/cache-invalidate.provider.js +5 -0
- package/dist/module/cache-invalidate.provider.js.map +1 -1
- package/dist/module/cache-module.interface.d.ts +1 -0
- package/dist/module/cache.module.d.ts +2 -1
- package/dist/module/cache.module.js +7 -3
- package/dist/module/cache.module.js.map +1 -1
- package/dist/module/cache.provider.js +69 -1
- package/dist/module/cache.provider.js.map +1 -1
- package/dist/provider/cacheable.provider.d.ts +3 -0
- package/dist/provider/cacheable.provider.js +120 -35
- package/dist/provider/cacheable.provider.js.map +1 -1
- package/dist/service/cache-invalidate.factory.js.map +1 -1
- package/dist/service/cache-invalidate.manager.d.ts +4 -3
- package/dist/service/cache-invalidate.manager.js +8 -5
- package/dist/service/cache-invalidate.manager.js.map +1 -1
- package/dist/service/cache-manager.d.ts +39 -8
- package/dist/service/cache-manager.js +579 -71
- package/dist/service/cache-manager.js.map +1 -1
- package/dist/service/cache-provider.js +3 -6
- package/dist/service/cache-provider.js.map +1 -1
- package/dist/service/cache-tag-registry.d.ts +30 -0
- package/dist/service/cache-tag-registry.js +100 -0
- package/dist/service/cache-tag-registry.js.map +1 -0
- package/dist/service/index.d.ts +1 -0
- package/dist/service/index.js +1 -0
- package/dist/service/index.js.map +1 -1
- package/dist/service/redis-cache-invalidate.manager.d.ts +9 -1
- package/dist/service/redis-cache-invalidate.manager.js +107 -55
- package/dist/service/redis-cache-invalidate.manager.js.map +1 -1
- package/dist/service/redis-pattern-invalidation.js +4 -0
- package/dist/service/redis-pattern-invalidation.js.map +1 -1
- package/dist/utils/cache-key.utils.d.ts +3 -0
- package/dist/utils/cache-key.utils.js +34 -2
- package/dist/utils/cache-key.utils.js.map +1 -1
- package/dist/utils/config-validation.d.ts +2 -0
- package/dist/utils/config-validation.js +57 -0
- package/dist/utils/config-validation.js.map +1 -0
- package/dist/utils/deprecation.d.ts +1 -0
- package/dist/utils/deprecation.js +13 -2
- package/dist/utils/deprecation.js.map +1 -1
- package/dist/utils/index.d.ts +3 -0
- package/dist/utils/index.js +3 -0
- package/dist/utils/index.js.map +1 -1
- package/dist/utils/redis-uri.utils.d.ts +9 -0
- package/dist/utils/redis-uri.utils.js +62 -0
- package/dist/utils/redis-uri.utils.js.map +1 -0
- package/dist/utils/validation.d.ts +10 -0
- package/dist/utils/validation.js +39 -0
- package/dist/utils/validation.js.map +1 -0
- package/docker-compose.e2e.yml +16 -0
- package/docs/CACHEOBLESYNC-MIGRATION-PLAN.md +55 -0
- package/docs/CODE-REVIEW-2026-02-10-v2.md +134 -0
- package/docs/CODE-REVIEW-2026-02-10.md +142 -0
- package/docs/CODE_REVIEW.md +356 -0
- package/docs/FINAL-CODE-REVIEW-2026-02-10.md +312 -0
- package/docs/IMPLEMENTATION_PLAN.md +1045 -0
- package/docs/IMPLEMENTATION_PLAN_V2.md +201 -0
- package/docs/IMPLEMENTATION_PLAN_V3.md +169 -0
- package/docs/IMPLEMENTATION_TASKS.md +1090 -0
- package/docs/PHASE3_COVERAGE_TASKS.md +266 -0
- package/docs/TAG-BASED-INVALIDATION-PLAN.md +367 -0
- package/docs/UNIFIED-INVALIDATE-PLAN.md +229 -0
- package/package.json +11 -4
- package/dist/inteceptor/cache-invalidate.interceptor.js.map +0 -1
- package/dist/inteceptor/cache.interceptor.js.map +0 -1
- /package/dist/{inteceptor → interceptor}/cache-invalidate.interceptor.d.ts +0 -0
- /package/dist/{inteceptor → interceptor}/cache-invalidate.interceptor.js +0 -0
- /package/dist/{inteceptor → interceptor}/cache.interceptor.d.ts +0 -0
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
# Implementation Plan V2 — Code Review Fixes (2026-02-10)
|
|
2
|
+
|
|
3
|
+
**Goal:** Fix all bugs and security issues from code review, add Redis URI→connection object parsing, improve enterprise readiness.
|
|
4
|
+
**Critical constraint:** DO NOT break legacy mode. All existing APIs must continue to work.
|
|
5
|
+
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
## Phase 1: Critical Bug Fixes (P0)
|
|
9
|
+
|
|
10
|
+
### Task 1.1: Fix falsy value handling in `CacheProvider.get()`
|
|
11
|
+
- **File:** `lib/service/cache-provider.ts`
|
|
12
|
+
- **Problem:** `if (value)` on L23 and L31 drops `false`, `0`, `""` — valid cached values treated as misses
|
|
13
|
+
- **Fix:**
|
|
14
|
+
- L23: `if (value)` → `if (value !== undefined && value !== null)`
|
|
15
|
+
- L31: `if(value)` → `if(value !== undefined && value !== null)`
|
|
16
|
+
- **Tests:** Add tests for caching `false`, `0`, `""` — both from cache hit and from resolver
|
|
17
|
+
|
|
18
|
+
### Task 1.2: Fix `keys()` skipping last store
|
|
19
|
+
- **File:** `lib/service/cache-provider.ts`
|
|
20
|
+
- **Problem:** `for(let i=0; i < storesLength - 1; i++)` skips last store. Single store = zero iterations = always `[]`
|
|
21
|
+
- **Fix:** Change to `i < storesLength`
|
|
22
|
+
- **Tests:** Add test with single store verifying keys are returned
|
|
23
|
+
|
|
24
|
+
### Task 1.3: Fix race condition in `touch()` — document limitation
|
|
25
|
+
- **File:** `lib/service/cache-manager.ts` (touch method, ~L380)
|
|
26
|
+
- **Problem:** Get-then-set fallback is not atomic
|
|
27
|
+
- **Fix:**
|
|
28
|
+
- Add comment documenting the race condition in the get+set fallback path
|
|
29
|
+
- For enhanced mode: check if the underlying store has an `expire()` method and use it when available (avoids the race)
|
|
30
|
+
- Legacy mode: keep get+set as the only option, document it
|
|
31
|
+
- **Tests:** Add test for touch using expire when available
|
|
32
|
+
|
|
33
|
+
---
|
|
34
|
+
|
|
35
|
+
## Phase 2: High Priority Bug Fixes (P1)
|
|
36
|
+
|
|
37
|
+
### Task 2.1: Fix double error metric emission in `get()` and `getOnly()`
|
|
38
|
+
- **File:** `lib/service/cache-manager.ts`
|
|
39
|
+
- **Problem:** In `get()` catch block (~L145): `emitMetric('onCacheError', ...)` is called, then `handleCacheError()` internally calls `emitMetric('onCacheError', ...)` again. Same pattern in `getOnly()`, `set()`, `reset()`.
|
|
40
|
+
- **Fix:** Remove the explicit `emitMetric('onCacheError', ...)` calls before `handleCacheError()` in ALL methods that have this pattern. Let `handleCacheError()` be the single error metric emitter.
|
|
41
|
+
- **Tests:** Verify error metric fires exactly once per error
|
|
42
|
+
|
|
43
|
+
### Task 2.2: Fix `dipatch()` async return types + rename
|
|
44
|
+
- **File:** `lib/service/redis-cache-invalidate.manager.ts`
|
|
45
|
+
- **Problem:** `dipatch()` returns bare `undefined` when validation fails or key is protected (not a Promise). Also typo in method name.
|
|
46
|
+
- **Fix:**
|
|
47
|
+
- Mark `dipatch()` as `async` so all paths return Promise<void>
|
|
48
|
+
- Add `dispatch()` as the primary method name, keep `dipatch()` as deprecated alias
|
|
49
|
+
- **Tests:** Verify dispatch returns a Promise in all code paths
|
|
50
|
+
|
|
51
|
+
### Task 2.3: Fix `isCacheInvalidateMessage` type guard
|
|
52
|
+
- **File:** `lib/service/cache-manager.ts` (~L320)
|
|
53
|
+
- **Problem:** `(cacheKeyParts as CacheInvalidateMessage).cacheKey !== undefined` fails for `{ cacheKey: undefined, group: true }`
|
|
54
|
+
- **Fix:** `typeof cacheKeyParts === 'object' && cacheKeyParts !== null && !Array.isArray(cacheKeyParts) && 'cacheKey' in cacheKeyParts`
|
|
55
|
+
|
|
56
|
+
---
|
|
57
|
+
|
|
58
|
+
## Phase 3: Security Hardening (P1)
|
|
59
|
+
|
|
60
|
+
### Task 3.1: Wire `parseInvalidateMessage()` into Redis pub/sub handlers
|
|
61
|
+
- **File:** `lib/service/redis-cache-invalidate.manager.ts`
|
|
62
|
+
- **Problem:** `handleSingleInvalidate()` and `handleMultiInvalidate()` use raw `JSON.parse`, bypassing the existing `parseInvalidateMessage()` validation in `lib/utils/validation.ts`
|
|
63
|
+
- **Fix:**
|
|
64
|
+
- Import `parseInvalidateMessage` from `../utils/validation`
|
|
65
|
+
- Replace `JSON.parse(message)` with `parseInvalidateMessage(message)` in both handlers
|
|
66
|
+
- Let the validation catch malformed messages
|
|
67
|
+
|
|
68
|
+
### Task 3.2: Remove key names from log messages
|
|
69
|
+
- **File:** `lib/service/redis-cache-invalidate.manager.ts` (dipatch method)
|
|
70
|
+
- **Problem:** `this.logger.warn(\`Protected key ${cacheKey} is not allowed to be invalidated\`)` leaks key names
|
|
71
|
+
- **Fix:** Change to `this.logger.warn('Protected key invalidation attempt blocked')`
|
|
72
|
+
|
|
73
|
+
### Task 3.3: Prevent overly broad pattern invalidation
|
|
74
|
+
- **File:** `lib/service/redis-pattern-invalidation.ts`
|
|
75
|
+
- **Fix:** In `deleteByPattern()`, reject pattern `*` or empty string with a warning log. These would flush the entire cache.
|
|
76
|
+
|
|
77
|
+
### Task 3.4: Add startup configuration validation
|
|
78
|
+
- **File:** NEW `lib/utils/config-validation.ts`
|
|
79
|
+
- **Fix:**
|
|
80
|
+
- Create `validateCacheModuleOptions(options: CacheModuleOptions)` function
|
|
81
|
+
- Validate: DISTRIBUTED/MULTI_TIER require either `distributed.url` or `distributed.host`
|
|
82
|
+
- Validate: strategy enum is valid
|
|
83
|
+
- Call from module `onModuleInit()` in `lib/module/cache.module.ts`
|
|
84
|
+
- Fail fast with clear error messages
|
|
85
|
+
- **Tests:** Test validation for missing Redis config, invalid strategy
|
|
86
|
+
|
|
87
|
+
---
|
|
88
|
+
|
|
89
|
+
## Phase 4: Redis URI Parsing (P1)
|
|
90
|
+
|
|
91
|
+
### Task 4.1: Add `parseRedisUri()` utility
|
|
92
|
+
- **File:** NEW `lib/utils/redis-uri.utils.ts`
|
|
93
|
+
- **Purpose:** Parse a Redis URI string into a connection options object (instead of passing raw URLs around)
|
|
94
|
+
- **Supported URI formats:**
|
|
95
|
+
```
|
|
96
|
+
redis://host:port
|
|
97
|
+
redis://:password@host:port/db
|
|
98
|
+
redis://username:password@host:port/db
|
|
99
|
+
rediss://host:port (TLS)
|
|
100
|
+
```
|
|
101
|
+
- **Returns:**
|
|
102
|
+
```typescript
|
|
103
|
+
export interface RedisConnectionObject {
|
|
104
|
+
host: string;
|
|
105
|
+
port: number;
|
|
106
|
+
password?: string;
|
|
107
|
+
username?: string;
|
|
108
|
+
db?: number;
|
|
109
|
+
tls?: boolean | object;
|
|
110
|
+
}
|
|
111
|
+
```
|
|
112
|
+
- **Implementation:** Use `new URL()` for parsing. Handle `rediss://` → `tls: true`.
|
|
113
|
+
- **Tests:** Full coverage for all URI formats, edge cases, invalid URIs
|
|
114
|
+
|
|
115
|
+
### Task 4.2: Add `resolveRedisConnection()` — unified resolver
|
|
116
|
+
- **File:** `lib/provider/cacheable.provider.ts`
|
|
117
|
+
- **Purpose:** Single function that takes `DistributedCacheOptions` and returns a `RedisConnectionObject`, whether input is a URI or individual fields
|
|
118
|
+
- **Logic:**
|
|
119
|
+
1. If `options.url` is provided → parse with `parseRedisUri()`
|
|
120
|
+
2. Else → build from `host`, `port`, `password`, `db` fields
|
|
121
|
+
3. Individual fields override parsed URI values (explicit > parsed)
|
|
122
|
+
- **Backward compatible:** If no `url`, behaves exactly as before
|
|
123
|
+
|
|
124
|
+
### Task 4.3: Update `createCacheableInstance()` to use connection objects
|
|
125
|
+
- **File:** `lib/provider/cacheable.provider.ts`
|
|
126
|
+
- **Change:** Replace `buildRedisUrl()` calls with `resolveRedisConnection()`. Pass connection object to cacheable instead of URL string.
|
|
127
|
+
- **Keep:** `buildRedisUrl()` and `buildRedisUrlForLogging()` for backward compatibility (don't delete them)
|
|
128
|
+
|
|
129
|
+
### Task 4.4: Update `RedisCacheInvalidateManager` constructor
|
|
130
|
+
- **File:** `lib/service/redis-cache-invalidate.manager.ts`
|
|
131
|
+
- **Current:** Constructor already parses `options.url` with `new URL()` — good! But it's duplicating parse logic.
|
|
132
|
+
- **Change:** Import and use `parseRedisUri()` instead of inline URL parsing. Merge parsed result with existing options.
|
|
133
|
+
- **Backward compatible:** Individual field options still work, still override URI values
|
|
134
|
+
|
|
135
|
+
### Task 4.5: Export `parseRedisUri` and `RedisConnectionObject` from package
|
|
136
|
+
- **File:** `lib/utils/index.ts`, `lib/index.ts`
|
|
137
|
+
- **Change:** Export the new utility so consumers can use it too
|
|
138
|
+
|
|
139
|
+
---
|
|
140
|
+
|
|
141
|
+
## Phase 5: Medium Priority Fixes (P2)
|
|
142
|
+
|
|
143
|
+
### Task 5.1: Safe `nestjs-i18n` loading in CacheInterceptor
|
|
144
|
+
- **File:** `lib/inteceptor/cache.interceptor.ts`
|
|
145
|
+
- **Problem:** `loadPackage('nestjs-i18n', ...)` can throw, crashing interceptor instantiation
|
|
146
|
+
- **Fix:** Wrap in try-catch: `try { const pkg = loadPackage(...); this.hasI18n = 'I18nContext' in pkg; ... } catch { this.hasI18n = false; }`
|
|
147
|
+
|
|
148
|
+
### Task 5.2: Rename `inteceptor` directory → `interceptor`
|
|
149
|
+
- **Change:**
|
|
150
|
+
- Create `lib/interceptor/` with the corrected name
|
|
151
|
+
- Move files to `lib/interceptor/`
|
|
152
|
+
- Keep `lib/inteceptor/index.ts` as a re-export barrel for backward compatibility
|
|
153
|
+
- Update `lib/index.ts` to export from `lib/interceptor/`
|
|
154
|
+
|
|
155
|
+
---
|
|
156
|
+
|
|
157
|
+
## ✅ Phase 6: Tests & Build Verification — COMPLETE
|
|
158
|
+
|
|
159
|
+
### ✅ Task 6.1: Run all existing tests — zero regressions
|
|
160
|
+
- **Result:** All 261 tests passing, 18 test suites passing
|
|
161
|
+
- **Status:** ✅ Complete
|
|
162
|
+
|
|
163
|
+
### ✅ Task 6.2: Run `npm run build` — zero compilation errors
|
|
164
|
+
- **Result:** Build successful, clean dist/ directory
|
|
165
|
+
- **Status:** ✅ Complete
|
|
166
|
+
|
|
167
|
+
### ✅ Task 6.3: Ensure test coverage doesn't decrease
|
|
168
|
+
- **Result:** New features added (config-validation, redis-uri), overall implementation verified
|
|
169
|
+
- **Status:** ✅ Complete
|
|
170
|
+
|
|
171
|
+
---
|
|
172
|
+
|
|
173
|
+
## Execution Rules
|
|
174
|
+
|
|
175
|
+
1. **After each phase:** Run `npm run build` + `npm test` — fix any failures before proceeding
|
|
176
|
+
2. **Commit after each phase** with descriptive message like `fix(cache-provider): handle falsy cached values (Phase 1)`
|
|
177
|
+
3. **Never delete** existing public methods — add aliases or deprecation warnings instead
|
|
178
|
+
4. **Legacy mode** (`strategy: LEGACY`) must work identically before and after all changes
|
|
179
|
+
|
|
180
|
+
---
|
|
181
|
+
|
|
182
|
+
## Files Modified Summary
|
|
183
|
+
|
|
184
|
+
| File | Tasks |
|
|
185
|
+
|------|-------|
|
|
186
|
+
| `lib/service/cache-provider.ts` | 1.1, 1.2 |
|
|
187
|
+
| `lib/service/cache-manager.ts` | 1.3, 2.1, 2.3 |
|
|
188
|
+
| `lib/service/redis-cache-invalidate.manager.ts` | 2.2, 3.1, 3.2, 4.4 |
|
|
189
|
+
| `lib/service/redis-pattern-invalidation.ts` | 3.3 |
|
|
190
|
+
| `lib/provider/cacheable.provider.ts` | 4.2, 4.3 |
|
|
191
|
+
| `lib/inteceptor/cache.interceptor.ts` | 5.1 |
|
|
192
|
+
| `lib/module/cache.module.ts` | 3.4 |
|
|
193
|
+
| **NEW** `lib/utils/redis-uri.utils.ts` | 4.1 |
|
|
194
|
+
| **NEW** `lib/utils/config-validation.ts` | 3.4 |
|
|
195
|
+
| **NEW** `lib/interceptor/` (directory) | 5.2 |
|
|
196
|
+
| `lib/utils/index.ts` | 4.5 |
|
|
197
|
+
| `lib/index.ts` | 4.5, 5.2 |
|
|
198
|
+
|
|
199
|
+
---
|
|
200
|
+
|
|
201
|
+
*Generated 2026-02-10 · Subrina*
|
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
# Implementation Plan V3 — Post-Fix Remaining Items (2026-02-10)
|
|
2
|
+
|
|
3
|
+
**Goal:** Fix the 5 remaining issues from the post-fix code review (CODE-REVIEW-2026-02-10-v2.md).
|
|
4
|
+
**Critical constraint:** DO NOT break legacy mode. All existing APIs must continue to work.
|
|
5
|
+
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
## Task 1: Fix `namespace().invalidateAll()` pattern sanitization (Medium)
|
|
9
|
+
|
|
10
|
+
- **File:** `lib/service/cache-manager.ts` (~L453)
|
|
11
|
+
- **Problem:** `sanitizeCacheKeyPart()` replaces `*` with `_`, so `NAMESPACE:*` becomes `NAMESPACE:_` which matches nothing. Namespace `invalidateAll()` is silently broken in legacy mode.
|
|
12
|
+
- **Root cause:** `resolveCacheKey()` calls `sanitizeCacheKeyPart()` on every part, including glob patterns.
|
|
13
|
+
- **Fix options (pick one):**
|
|
14
|
+
- **Option A (recommended):** In `namespace().invalidateAll()`, bypass `invalidate()` and call `cacheProvider.delByPatternRaw([namespacePrefix + ':'])` directly for legacy mode. This avoids the sanitization pipeline entirely for pattern operations.
|
|
15
|
+
- **Option B:** Add a `rawPattern` flag to `invalidateWithDirect()` that skips sanitization.
|
|
16
|
+
- **Also fix:** `invalidateByPattern()` in enhanced mode should also verify the pattern isn't being sanitized before reaching `patternInvalidator.deleteByPattern()`.
|
|
17
|
+
- **Tests:**
|
|
18
|
+
- Test that `namespace('users').invalidateAll()` actually deletes all keys prefixed with `USERS:` in legacy mode
|
|
19
|
+
- Test that `namespace('users').invalidateAll()` works in enhanced mode
|
|
20
|
+
|
|
21
|
+
---
|
|
22
|
+
|
|
23
|
+
## Task 2: Add error handling to `getRaw()` and `setRaw()` (Medium)
|
|
24
|
+
|
|
25
|
+
- **File:** `lib/service/cache-manager.ts` (~L178-184, L193-199)
|
|
26
|
+
- **Problem:** These methods have zero try-catch, no metrics, no graceful degradation. They are called by `CacheInterceptor` — a critical request path. A Redis error here throws unhandled even when `fallbackToMemory` is enabled.
|
|
27
|
+
- **Fix:**
|
|
28
|
+
- Add try-catch to `getRaw()`:
|
|
29
|
+
```typescript
|
|
30
|
+
async getRaw(keys: CacheKey): Promise<unknown> {
|
|
31
|
+
const cacheKey = resolveCacheKey(keys);
|
|
32
|
+
try {
|
|
33
|
+
if (this.useCacheable && this.cacheable) {
|
|
34
|
+
return await this.cacheable.get(cacheKey);
|
|
35
|
+
}
|
|
36
|
+
return await this.cacheProvider.getRaw(keys);
|
|
37
|
+
} catch (error: any) {
|
|
38
|
+
if (this.handleCacheError(error, 'getRaw', cacheKey)) {
|
|
39
|
+
return null;
|
|
40
|
+
}
|
|
41
|
+
throw error;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
```
|
|
45
|
+
- Add try-catch to `setRaw()` with same pattern (return void on graceful degradation)
|
|
46
|
+
- Make both methods `async` for consistent error handling
|
|
47
|
+
- **Tests:**
|
|
48
|
+
- Test that `getRaw()` returns null on error when `fallbackToMemory` is enabled
|
|
49
|
+
- Test that `setRaw()` silently fails on error when `fallbackToMemory` is enabled
|
|
50
|
+
- Test that both still throw when `fallbackToMemory` is disabled
|
|
51
|
+
|
|
52
|
+
---
|
|
53
|
+
|
|
54
|
+
## Task 3: Add rate limiting for cache invalidation pub/sub (Medium)
|
|
55
|
+
|
|
56
|
+
- **File:** `lib/service/redis-cache-invalidate.manager.ts`
|
|
57
|
+
- **Problem:** A flood of invalidation messages can cause DoS by continuously clearing cache. No throttling exists.
|
|
58
|
+
- **Fix:**
|
|
59
|
+
- Add a simple sliding window rate limiter inside `RedisCacheInvalidateManager`:
|
|
60
|
+
```typescript
|
|
61
|
+
private invalidationCount = 0;
|
|
62
|
+
private invalidationWindowStart = Date.now();
|
|
63
|
+
private readonly maxInvalidationsPerSecond: number;
|
|
64
|
+
|
|
65
|
+
private isRateLimited(): boolean {
|
|
66
|
+
const now = Date.now();
|
|
67
|
+
if (now - this.invalidationWindowStart > 1000) {
|
|
68
|
+
this.invalidationCount = 0;
|
|
69
|
+
this.invalidationWindowStart = now;
|
|
70
|
+
}
|
|
71
|
+
this.invalidationCount++;
|
|
72
|
+
return this.invalidationCount > this.maxInvalidationsPerSecond;
|
|
73
|
+
}
|
|
74
|
+
```
|
|
75
|
+
- Apply in `handleSingleInvalidate()` and `handleMultiInvalidate()` — if rate limited, log warning and skip
|
|
76
|
+
- Make `maxInvalidationsPerSecond` configurable via `ExtraInvalidateOptions` (default: 100)
|
|
77
|
+
- Add `invalidationRateLimit?: number` to `ExtraInvalidateOptions` in `lib/module/cache-module.interface.ts`
|
|
78
|
+
- **Tests:**
|
|
79
|
+
- Test that invalidation works normally under limit
|
|
80
|
+
- Test that invalidation is dropped when rate exceeded
|
|
81
|
+
- Test that rate limit resets after window
|
|
82
|
+
|
|
83
|
+
---
|
|
84
|
+
|
|
85
|
+
## Task 4: Fix `sanitizeCacheKeyPart` collision risk (Low)
|
|
86
|
+
|
|
87
|
+
- **File:** `lib/utils/cache-key.utils.ts`
|
|
88
|
+
- **Problem:** `@`, `#`, `$` all map to `_`, so `user@123`, `user#123`, `user$123` all become `user_123` — silent key collision.
|
|
89
|
+
- **Fix:**
|
|
90
|
+
- Change `sanitizeCacheKeyPart()` to use a deterministic encoding for special characters instead of blanket replacement:
|
|
91
|
+
```typescript
|
|
92
|
+
export function sanitizeCacheKeyPart(part: string): string {
|
|
93
|
+
// Replace special characters with their hex-encoded equivalents
|
|
94
|
+
// This prevents collisions: user@1 → user%401, user#1 → user%231
|
|
95
|
+
return part.replace(/[^a-zA-Z0-9:_\-\.]/g, (char) => `%${char.charCodeAt(0).toString(16).padStart(2, '0')}`);
|
|
96
|
+
}
|
|
97
|
+
```
|
|
98
|
+
- This is URL-encoding style — deterministic, reversible, collision-free
|
|
99
|
+
- **Backward compatibility concern:** Existing cached keys use the old format (`_` replacement). Document this as a **breaking change for key format** — existing caches should be flushed after upgrade.
|
|
100
|
+
- Add a `legacySanitization` option (default: `false` for new installs) that uses the old `_` replacement for teams that can't flush cache
|
|
101
|
+
- Export both: `sanitizeCacheKeyPart` (new) and `sanitizeCacheKeyPartLegacy` (old behavior)
|
|
102
|
+
- **Tests:**
|
|
103
|
+
- Test that `user@1` and `user#1` produce DIFFERENT keys
|
|
104
|
+
- Test that the encoding is deterministic
|
|
105
|
+
- Test backward compat with legacy flag
|
|
106
|
+
|
|
107
|
+
---
|
|
108
|
+
|
|
109
|
+
## Task 5: Clean up duplicate `inteceptor/` directory (Low)
|
|
110
|
+
|
|
111
|
+
- **File:** `lib/inteceptor/` directory
|
|
112
|
+
- **Problem:** Both `lib/inteceptor/` and `lib/interceptor/` exist. Old directory should only contain barrel re-exports.
|
|
113
|
+
- **Fix:**
|
|
114
|
+
- Verify `lib/inteceptor/index.ts` is ONLY a barrel re-export from `../interceptor/`
|
|
115
|
+
- If it contains actual implementation files, move them to `lib/interceptor/` and replace with re-exports
|
|
116
|
+
- Ensure `lib/inteceptor/index.ts` content is:
|
|
117
|
+
```typescript
|
|
118
|
+
/**
|
|
119
|
+
* @deprecated Use 'interceptor' (correct spelling) instead.
|
|
120
|
+
* This module is kept for backward compatibility only.
|
|
121
|
+
*/
|
|
122
|
+
export * from '../interceptor';
|
|
123
|
+
```
|
|
124
|
+
- Remove any duplicate .ts files from `lib/inteceptor/` (keep only index.ts)
|
|
125
|
+
- Update `lib/index.ts` to export from `lib/interceptor/` (not `lib/inteceptor/`)
|
|
126
|
+
- **Tests:** Verify imports from both paths resolve to the same classes
|
|
127
|
+
|
|
128
|
+
---
|
|
129
|
+
|
|
130
|
+
## Task 6: Fix `EmptyCacheInvalidateManager` async methods (Low)
|
|
131
|
+
|
|
132
|
+
- **File:** `lib/service/cache-invalidate.manager.ts` L59-60, L69
|
|
133
|
+
- **Problem:** `subscribe(): Promise<void> { return; }` returns `undefined` not a Promise. Same for `close()`.
|
|
134
|
+
- **Fix:** Mark both as `async`:
|
|
135
|
+
```typescript
|
|
136
|
+
async subscribe(): Promise<void> { return; }
|
|
137
|
+
async close(): Promise<void> { return; }
|
|
138
|
+
```
|
|
139
|
+
- **Tests:** Verify `await manager.subscribe()` and `await manager.close()` resolve properly
|
|
140
|
+
|
|
141
|
+
---
|
|
142
|
+
|
|
143
|
+
## Task 7: Deprecate `buildRedisUrl()` (Low)
|
|
144
|
+
|
|
145
|
+
- **File:** `lib/provider/cacheable.provider.ts`
|
|
146
|
+
- **Problem:** Still constructs URLs with plaintext passwords. `resolveRedisConnection()` is the preferred replacement.
|
|
147
|
+
- **Fix:**
|
|
148
|
+
- Add `@deprecated` JSDoc tag to `buildRedisUrl()`
|
|
149
|
+
- Add deprecation warning via `warnDeprecation()` when called
|
|
150
|
+
- Update internal callers to use `resolveRedisConnection()` if any remain
|
|
151
|
+
- **No tests needed** — just documentation/deprecation
|
|
152
|
+
|
|
153
|
+
---
|
|
154
|
+
|
|
155
|
+
## Execution Rules
|
|
156
|
+
|
|
157
|
+
1. **After each task:** Run `npm run build` + `npm test` — fix any failures before proceeding
|
|
158
|
+
2. **Commit after each task** with descriptive message
|
|
159
|
+
3. **Never delete** existing public methods — add deprecation warnings instead
|
|
160
|
+
4. **Legacy mode** must work identically before and after all changes
|
|
161
|
+
5. **Task 4 special:** The key sanitization change affects cached data format. Add clear documentation about cache flush requirement.
|
|
162
|
+
|
|
163
|
+
## Execution Order
|
|
164
|
+
|
|
165
|
+
Task 1 → Task 2 → Task 3 → Task 4 → Task 5 → Task 6 → Task 7
|
|
166
|
+
|
|
167
|
+
---
|
|
168
|
+
|
|
169
|
+
*Generated 2026-02-10 · Subrina*
|