Repository navigation
memory - fix: keep falsy values in has(), hasMany(), take(), and takeMany() - #1767
Conversation
…Many() has() and hasMany() used Boolean() on the value. take() used a falsy check. Thus a stored 0, false, "", or null showed as missing, and take() did not remove the entry. Now each method compares the value with undefined.
|
|
||
| ## CacheableMemory - API | ||
|
|
||
| A returned `undefined` means no value. The cache treats `0`, `false`, `""`, `null`, `NaN`, and `0n` as valid values. |
There was a problem hiding this comment.
🔵 Low - BigInt values are omitted from documented value-size statistics
The new API documentation declares 0n a valid cached value, but an enabled cache records zero value bytes for every BigInt because the size estimator has no BigInt case. As a result, stats.vsize undercounts caches that store BigInt values and remains inconsistent with the documented current-content size counter.
Show fix
Handle bigint in Stats.roughSizeOfObject with an explicit size estimate, and add a statistics-enabled test that verifies vsize after setting, overwriting, and deleting a BigInt value.
More info - Reply on this comment to give feedback or ignore the issue.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #1767 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 30 30
Lines 3702 3697 -5
Branches 832 832
=========================================
- Hits 3702 3697 -5 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Please check if the PR fulfills these requirements
What kind of change does this PR introduce? (Bug fix, feature, docs update, ...)
Bug fix. Fixes #1766.
Problem
CacheableMemorytreats a stored falsy value as a missing key in four methods:The result is the same for
false,"",null,NaN, and0n.has()andhasMany()useBoolean(item)on the value.take()usesif (!item), so it also does not delete the entry.takeMany()callstake().The
KeyvCacheableMemoryadapter has the same problem inget(). It usesif (result), so it returnsundefinedfor a stored falsy value. After thehas()fix, the adapterhas()returnstruefor the same key.Fix
has()andhasMany()returnthis.get(key) !== undefined.take()returns early only when the value isundefined.KeyvCacheableMemory.get()returns the result of the storeget()directly.undefinedmeans no value. Falsy values remain valid cache values.The methods still read through
get(). Thus stats, hooks, LRU order, and expiry stay the same.Verification
pnpm buildpasses.node scripts/test-build.mjspasses.pnpm testpasses all 1,409 tests across 64 files on Node.js 24.a51d2b8for the expected defects.6a3b56d:expected undefined to be +0.NaNand0n.git diff --checkpasses.Test audit
CacheableMemory Has > should return true for falsy valueshas()andhasMany()returntruefor0,false,"",null,NaN, and0n.NaNand0nto protect the same contract for numeric and BigInt values.CacheableMemory Take > should take and delete falsy valuestakeMany(), throughtake(), returns each falsy value and removes its entry.undefinedand leaves the entry in the cache. This test fails against the original source.NaNand0nto check their return values and entry removal.Keyv Cacheable Memory > should keep falsy values in has(), get(), and getMany()has(),get(), andgetMany()for all six values.get()returnsundefinedfor a stored value. This test fails before the adapter fix.NaNand0nto check the adapter with numeric and BigInt values.This PR keeps all three tests. It changes no test from the base branch and deletes no test.