Skip to content

Commit b20d483

Browse files
committed
fix: handle NaN and function types in query cache normalization
NaN and functions are not properly serialized by JSON.stringify: - NaN becomes null - Functions are omitted entirely This caused cache collisions where { age: NaN } and { age: null } would share the same cache key, bypassing validation for NaN. Fix by adding special markers for these types before serialization.
1 parent 338426a commit b20d483

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

src/utils/parse-query.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,18 @@ const CACHE_MAX_AGE = 5 * 60 * 1000 // 5 minutes
1515

1616
/**
1717
* Recursively sort object keys for deterministic JSON serialization
18+
* Handles special cases like NaN and functions to ensure proper cache keys
1819
* @param obj - Object to normalize
1920
* @returns Normalized object with sorted keys
2021
*/
2122
function normalizeObject(obj: unknown): unknown {
23+
// Handle special primitive cases that JSON.stringify doesn't handle well
24+
if (typeof obj === 'number' && isNaN(obj)) {
25+
return '__NaN__' // Special marker for NaN
26+
}
27+
if (typeof obj === 'function') {
28+
return '__function__' // Special marker for functions
29+
}
2230
if (obj === null || typeof obj !== 'object') {
2331
return obj
2432
}

0 commit comments

Comments
 (0)