fix: snapshot cached definition options - #120
Closed
lan17 wants to merge 1 commit into
Closed
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fix #101 by capturing each long-lived
cached()definition before it is registered.useCase,keyType, the cache-key selector reference, serializer choice, and invalidation-tracking modegetOrLoad()behavior unchangedCloses #101
Root cause
CachedOptionsfields are TypeScript-readonly, butcached()retained the caller-owned object and reread it on every invocation. JavaScript callers, structurally typed mutable objects, accessors, and proxies can therefore change a definition after DialCache validates and registers its use case.That time-of-check/time-of-use gap could change:
useCase,keyType, selectortrackForInvalidationTwo wrappers could consequently converge on the same runtime identity even though DialCache registered them as distinct definitions, allowing one wrapper to receive the other's cached value.
Design
Registration now builds one private
CachedDefinitioncontaining:useCaseandkeyTypenulltrackForInvalidation, normalized tofalseAll reads and normalization complete before the use-case registry is mutated. The registry checks
definition.useCase, and the returned wrapper closes over only that internal snapshot.Existing registration snapshots remain unchanged:
defaultConfigis still validated, cloned, and frozenfallbackTimeoutMsis still validated and capturedshadowComparatoris still captured by referenceThe cache-key selector is still invoked only after the enabled-context check, preserving disabled calls as true pass-through operations.
Behavior and compatibility
getOrLoad()remains a one-shot operation and is unchanged.Regression coverage
Focused tests prove that:
useCasevalue checked for reserved/duplicate names is the value used for invocationValidation
Node
v22.22.0:corepack pnpm checkcorepack pnpm test:integrationgit diff --check