-
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
Open
ggazzo
wants to merge
3
commits into
develop
Choose a base branch
from
chore/middleware
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+56
−32
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,4 @@ | ||
| export * from './definition'; | ||
| // export * from './definition'; | ||
| export * from './makeFunction'; | ||
| export * from './midleware'; | ||
| // export * from './addPatch'; |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,40 +1,26 @@ | ||
| import { addPatch } from './addPatch'; | ||
| import { calledFunctions, functions } from './data'; | ||
| import type { BaseFunction, PatchData, PatchFunction, PatchedFunction } from './definition'; | ||
| import type { BaseFunction, PatchFunction, PatchedFunction } from './definition'; | ||
| import { withMiddleware } from './midleware'; | ||
|
|
||
| export const makeFunction = <T extends BaseFunction>(fn: T): PatchedFunction<T> => { | ||
| const patches = new Set<PatchData<T>>(); | ||
|
|
||
| patches.add({ | ||
| patchFunction: (_next, ...args) => fn(...args), | ||
| }); | ||
|
|
||
| const result = ((...args: Parameters<T>): ReturnType<T> => { | ||
| let newFn: T = fn; | ||
|
|
||
| for (const patch of patches) { | ||
| if (patch.condition && !patch.condition()) { | ||
| continue; | ||
| const wrapped = withMiddleware(fn); | ||
| const patch = (fn: PatchFunction<T>, condition?: () => boolean) => { | ||
| return wrapped.use((ctx, next) => { | ||
| if (!condition || condition()) { | ||
| return fn(next as unknown as T, ...ctx); | ||
| } | ||
|
|
||
| const nextFn = newFn; | ||
| newFn = ((...args: Parameters<T>) => patch.patchFunction(nextFn, ...args)) as T; | ||
| } | ||
|
|
||
| calledFunctions.add(result); | ||
| return newFn(...args); | ||
| }) as PatchedFunction<T>; | ||
|
|
||
| functions.set(result, patches as Set<PatchData<BaseFunction>>); | ||
|
|
||
| result.patch = (patch: PatchFunction<T>, condition?: () => boolean) => addPatch(result, patch, condition); | ||
|
|
||
| result.originalSignature = (() => { | ||
| return next(...ctx); | ||
| }); | ||
| }; | ||
| const originalSignature = (() => { | ||
| throw new Error('OriginalSignature of patched functions is not meant to be executed directly.'); | ||
| }) as unknown as T; | ||
| result.patchSignature = (() => { | ||
| const patchSignature = (() => { | ||
| throw new Error('PatchSignature of patched functions is not meant to be executed directly.'); | ||
| }) as unknown as PatchFunction<T>; | ||
|
|
||
| return result; | ||
| return Object.assign(wrapped, { | ||
| patch, | ||
| originalSignature, | ||
| patchSignature, | ||
| }) as PatchedFunction<T>; | ||
| }; |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| type Middleware<F extends (...args: any[]) => any> = (ctx: Parameters<F>, next: NextFunction<F>) => ReturnType<F>; | ||
| type NextFunction<F extends (...args: any[]) => any> = (...args: Parameters<F> | []) => ReturnType<F>; | ||
|
|
||
| export function withMiddleware<F extends (...args: any[]) => any>(fn: F) { | ||
| const middlewares: Middleware<F>[] = []; | ||
|
|
||
| const buildRunner = (): ((ctx: Parameters<F>) => ReturnType<F>) => { | ||
| return middlewares.reduce( | ||
| (next, middleware) => { | ||
| return (ctx) => middleware(ctx, (...args) => next(args.length ? (args as Parameters<F>) : ctx)); | ||
| }, | ||
| (ctx: Parameters<F>) => fn(...ctx), | ||
| ); | ||
| }; | ||
|
|
||
| let runner = buildRunner(); | ||
|
|
||
| const use = (middleware: Middleware<F>) => { | ||
| middlewares.push(middleware); | ||
|
|
||
| runner = buildRunner(); | ||
| return () => { | ||
| const index = middlewares.indexOf(middleware); | ||
| if (index > -1) { | ||
| middlewares.splice(index, 1); | ||
| } | ||
| runner = buildRunner(); | ||
| }; | ||
| }; | ||
|
|
||
| const run = (...args: Parameters<F>): ReturnType<F> => { | ||
| return runner(args); | ||
| }; | ||
|
|
||
| return Object.assign(run as F, { use, fn }); | ||
| } | ||
Oops, something went wrong.
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.
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
🤖 Prompt for AI Agents