Repository navigation
fix: instantiate subclass in duplicate and preserve constructor in applyMixin (#2053) - #2283
EnesKartal wants to merge 1 commit into
Conversation
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 7b480cddc2
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
| override?: Override | ||
| ): Redis<ReplyMappingFromOptions<ReplyMapping, Override>> { | ||
| return new Redis({ | ||
| const Ctor = (this.constructor as typeof Redis) || Redis; |
There was a problem hiding this comment.
Avoid selecting a command as the duplicate constructor
When a client is created with scripts: { constructor: ... } (accepted by the Record<string, ...> option and installed by defineCommand as an own this.constructor method), this lookup selects the generated Lua-command function rather than the Redis class. new Ctor(...) then executes that function against an uninitialized object and throws before creating a duplicate, whereas the previous hard-coded constructor worked; the identical lookup in Cluster.duplicate has the same regression. Resolve the class from the prototype rather than the potentially shadowed instance property.
AGENTS.md reference: AGENTS.md:L88-L88
Useful? React with 👍 / 👎.
What does this PR do?
Fixes #2053 by ensuring
duplicate()instantiates the subclass constructor when Redis or Cluster is extended (e.g.class CustomRedis extends Redis {}), and preserving constructor identity when applying mixins.Problem
lib/Redis.tsandlib/cluster/index.ts,duplicate()explicitly invokednew Redis(...)ornew Cluster(...), discarding any subclass instance and methods.lib/utils/applyMixin.ts, copying properties fromsource.prototypeindiscriminately included'constructor'(EventEmitter.prototype.constructor), overwritingderivedConstructor.prototype.constructor. This corruptedthis.constructoron instances ofRedis/ClustertoEventEmitter.Solution
applyMixin.ts, explicitly skip copying'constructor'when applying prototype mixins.Redis.prototype.duplicate, resolve constructor dynamically viaconst Ctor = (this.constructor as typeof Redis) || Redis; return new Ctor(...).Cluster.prototype.duplicate, resolve constructor dynamically viaconst Ctor = (this.constructor as typeof Cluster) || Cluster; return new Ctor(...).duplicate()retains subclass type and custom properties for bothRedisandCluster.Checks
npm run build) passing without errorsnpm run test:tsd) passingnpm run lint) passing without errorsNote
Low Risk
Small inheritance and mixin fixes with targeted tests; behavior change only affects users who extend Redis/Cluster or rely on correct
constructorafter mixins.Overview
Fixes subclass extension behavior:
duplicate()onRedisandClusternow instantiates viathis.constructorinstead of always creating baseRedis/Clusterinstances, so custom subclasses keep their type and methods when duplicated.applyMixinno longer copies the mixin’sconstructoronto the derived prototype, which previously overwrote instanceconstructorwithEventEmitterand broke the dynamic constructor lookup induplicate().Functional and unit tests assert duplicated clients remain instances of
CustomRedis/CustomCluster.Reviewed by Cursor Bugbot for commit 7b480cd. Bugbot is set up for automated code reviews on this repo. Configure here.