Description
TTLCache currently allows null or undefined to be used as cache keys in methods like get, set, has, delete, and update.
This can lead to unexpected behavior in Map storage and inconsistent cache state.
Location
lib/cache.ts (TTLCache class)
Steps to Reproduce
cache.set(null as any, "value", 1000);
cache.get(null as any);
Expected Behavior
The cache should throw a TypeError when key is null or undefined.
Actual Behavior
The cache accepts invalid keys and stores them in Map.
Suggested Fix
Add validation in all public methods:
- get
- set
- has
- delete
- update
Throw:
TypeError("Cache key cannot be null or undefined")
Steps to Reproduce
- Open lib/cache.ts and import TTLCache
- Create a new cache instance:
const cache = new TTLCache();
- Try using null or undefined as a key:
cache.set(null as any, "value", 1000);
cache.get(null as any);
- Observe that no error is thrown and the cache accepts the invalid key.
Expected Behavior
The cache should reject invalid keys.
Specifically, calling get, set, has, delete, or update with null or undefined should throw a TypeError.
Example:
cache.set(null, "value", 1000);
Expected:
A TypeError is thrown indicating that cache keys cannot be null or undefined.
This ensures the cache only accepts valid string keys and prevents unexpected behavior in the internal Map storage.
Screenshots / Logs
Not applicable. This is a backend logic issue in TTLCache and does not produce UI logs or screenshots.
GitHub Username (If applicable)
not applicable
Environment
Other
Description
TTLCache currently allows null or undefined to be used as cache keys in methods like get, set, has, delete, and update.
This can lead to unexpected behavior in Map storage and inconsistent cache state.
Location
lib/cache.ts (TTLCache class)
Steps to Reproduce
cache.set(null as any, "value", 1000);
cache.get(null as any);
Expected Behavior
The cache should throw a TypeError when key is null or undefined.
Actual Behavior
The cache accepts invalid keys and stores them in Map.
Suggested Fix
Add validation in all public methods:
Throw:
TypeError("Cache key cannot be null or undefined")
Steps to Reproduce
const cache = new TTLCache();
cache.set(null as any, "value", 1000);
cache.get(null as any);
Expected Behavior
The cache should reject invalid keys.
Specifically, calling get, set, has, delete, or update with null or undefined should throw a TypeError.
Example:
cache.set(null, "value", 1000);
Expected:
A TypeError is thrown indicating that cache keys cannot be null or undefined.
This ensures the cache only accepts valid string keys and prevents unexpected behavior in the internal Map storage.
Screenshots / Logs
Not applicable. This is a backend logic issue in TTLCache and does not produce UI logs or screenshots.
GitHub Username (If applicable)
not applicable
Environment
Other