-
Notifications
You must be signed in to change notification settings - Fork 13.1k
chore: Introduce middleware support for function patching #37560
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Conversation
|
Looks like this PR is not ready to merge, because of the following issues:
Please fix the issues and try again If you have any trouble, please check the PR guidelines |
|
WalkthroughThe changes replace an internal patch-tracking mechanism with a middleware-based approach for the patch injection system. A new Changes
Sequence Diagram(s)sequenceDiagram
participant Client
participant PatchedFn as Patched Function
participant Middleware as Middleware Chain
participant OriginalFn as Original Function
Client->>PatchedFn: Call patched function(args)
PatchedFn->>Middleware: Execute middleware runner
loop For each middleware
Middleware->>Middleware: Transform context
end
Middleware->>OriginalFn: Invoke with final context
OriginalFn-->>Middleware: Return result
Middleware-->>PatchedFn: Return result
PatchedFn-->>Client: Return result
Client->>PatchedFn: patch(fn, condition?)
PatchedFn->>Middleware: Register middleware via use()
Middleware-->>PatchedFn: Return disposer
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~22 minutes Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## develop #37560 +/- ##
===========================================
- Coverage 70.79% 70.76% -0.04%
===========================================
Files 3158 3156 -2
Lines 109390 109370 -20
Branches 19682 20069 +387
===========================================
- Hits 77445 77398 -47
- Misses 29919 29944 +25
- Partials 2026 2028 +2
Flags with carried forward coverage won't be shown. Click here to find out more. 🚀 New features to boost your workflow:
|
baa0b03 to
a74e100
Compare
a74e100 to
3f5c94d
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No issues found across 3 files
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 1
🤖 Fix all issues with AI agents
In `@packages/patch-injection/src/midleware.ts`:
- Around line 18-25: The disposer currently uses middlewares.indexOf(middleware)
which will remove the first matching function when the same middleware is
registered multiple times; change use so each registration gets a unique entry
(e.g., wrap the provided middleware in a per-registration object/wrapper or
generate a token) and push that unique entry into middlewares (use the unique
wrapper/token when calling buildRunner); have the returned disposer remove that
exact entry (indexOf(wrapper/token)) so removals target the correct registration
and then rebuild runner with buildRunner().
🧹 Nitpick comments (2)
packages/patch-injection/src/index.ts (1)
1-4: Remove commented-out exports and confirm the API change.Line 1 and Line 4 leave commented exports in production code. If
definitiontypes are still public, re-export them explicitly; otherwise delete the commented lines and document the breaking change for consumers.♻️ Suggested cleanup
-// export * from './definition'; export * from './makeFunction'; export * from './midleware'; -// export * from './addPatch';As per coding guidelines Avoid code comments in the implementation.
packages/patch-injection/src/midleware.ts (1)
7-13: Confirm middleware execution order (currently last-registered runs first).Line 8 composes with
reduce, which makes the most recently added middleware execute first. If you want registration order to run first (common middleware behavior), switch toreduceRightor reverse the array.♻️ Option to preserve registration order
- return middlewares.reduce( + return middlewares.reduceRight(
| const use = (middleware: Middleware<F>) => { | ||
| middlewares.push(middleware); | ||
|
|
||
| runner = buildRunner(); | ||
| return () => { | ||
| const index = middlewares.indexOf(middleware); | ||
| if (index > -1) { | ||
| middlewares.splice(index, 1); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Disposer can remove the wrong middleware when the same function is registered twice.
Line 23 uses indexOf(middleware). If the same middleware is registered multiple times, the disposer for the later registration removes the first entry. Wrap the middleware per registration (or store an entry token) so disposal removes the correct instance.
🐛 Ensure disposer targets the exact registration
const use = (middleware: Middleware<F>) => {
- middlewares.push(middleware);
+ const entry: Middleware<F> = (ctx, next) => middleware(ctx, next);
+ middlewares.push(entry);
runner = buildRunner();
return () => {
- const index = middlewares.indexOf(middleware);
+ const index = middlewares.indexOf(entry);
if (index > -1) {
middlewares.splice(index, 1);
}📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| const use = (middleware: Middleware<F>) => { | |
| middlewares.push(middleware); | |
| runner = buildRunner(); | |
| return () => { | |
| const index = middlewares.indexOf(middleware); | |
| if (index > -1) { | |
| middlewares.splice(index, 1); | |
| const use = (middleware: Middleware<F>) => { | |
| const entry: Middleware<F> = (ctx, next) => middleware(ctx, next); | |
| middlewares.push(entry); | |
| runner = buildRunner(); | |
| return () => { | |
| const index = middlewares.indexOf(entry); | |
| if (index > -1) { | |
| middlewares.splice(index, 1); |
🤖 Prompt for AI Agents
In `@packages/patch-injection/src/midleware.ts` around lines 18 - 25, The disposer
currently uses middlewares.indexOf(middleware) which will remove the first
matching function when the same middleware is registered multiple times; change
use so each registration gets a unique entry (e.g., wrap the provided middleware
in a per-registration object/wrapper or generate a token) and push that unique
entry into middlewares (use the unique wrapper/token when calling buildRunner);
have the returned disposer remove that exact entry (indexOf(wrapper/token)) so
removals target the correct registration and then rebuild runner with
buildRunner().
Proposed changes (including videos or screenshots)
Issue(s)
Steps to test or reproduce
Further comments
Summary by CodeRabbit
Release Notes
✏️ Tip: You can customize this high-level summary in your review settings.