-
Notifications
You must be signed in to change notification settings - Fork 46
fix(controller): resolve MCP global middleware lazily on first request #460
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
Closed
Closed
Changes from all commits
Commits
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
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,96 @@ | ||
| import assert from 'assert'; | ||
| import compose from 'koa-compose'; | ||
| import { MCPControllerRegister } from '../../lib/impl/mcp/MCPControllerRegister'; | ||
|
|
||
| // Unit tests for the lazy global-middleware resolution. | ||
| // | ||
| // MCPControllerRegister.register() runs during the tegg load-unit init | ||
| // (postCreate), which happens before egg's `loadMiddleware` populates | ||
| // `app.middlewares`. The global middleware named in `config.mcp.middleware` | ||
| // must therefore be resolved lazily — on the first request — rather than at | ||
| // registration time, otherwise booting an app that configures `mcp.middleware` | ||
| // throws `Middleware <name> not found`. | ||
| function createRegister(mcp: any, middlewares: any) { | ||
| const app: any = { | ||
| eggContainerFactory: {}, | ||
| router: {}, | ||
| config: { mcp }, | ||
| middlewares, | ||
| }; | ||
| const register = new (MCPControllerRegister as any)({}, {}, app); | ||
| return { register, app }; | ||
| } | ||
|
|
||
| describe('plugin/controller/test/mcp/mcpGlobalMiddleware.test.ts', () => { | ||
| it('does not read app.middlewares at registration time', () => { | ||
| // app.middlewares is still empty here, mirroring registration time. | ||
| const { register } = createRegister({ middleware: [ 'trace' ] }, {}); | ||
| const base: any = async (_ctx: any, next: any) => next(); | ||
|
|
||
| // Wrapping the base middleware must not throw even though `trace` is not | ||
| // yet available; resolution is deferred to the first request. | ||
| assert.doesNotThrow(() => register.composeGlobalMiddleware(base)); | ||
| assert.strictEqual(register.globalMiddlewares, undefined); | ||
| }); | ||
|
|
||
| it('resolves and runs the configured global middleware on first request', async () => { | ||
| const { register, app } = createRegister({ middleware: [ 'trace' ] }, {}); | ||
| const order: string[] = []; | ||
| const base: any = async (_ctx: any, next: any) => { | ||
| order.push('base'); | ||
| return next(); | ||
| }; | ||
| const wrapped = register.composeGlobalMiddleware(base); | ||
|
|
||
| // app.middlewares is populated later by loadMiddleware. | ||
| app.middlewares.trace = () => async (_ctx: any, next: any) => { | ||
| order.push('trace'); | ||
| return next(); | ||
| }; | ||
|
|
||
| await wrapped({} as any, async () => { | ||
| order.push('handler'); | ||
| }); | ||
|
|
||
| assert.deepStrictEqual(order, [ 'base', 'trace', 'handler' ]); | ||
| assert.ok(register.globalMiddlewares, 'built and cached after first request'); | ||
| }); | ||
|
|
||
| it('getGlobalMiddleware is idempotent (builds once)', () => { | ||
| const { register } = createRegister({ middleware: [] }, {}); | ||
| register.getGlobalMiddleware(); | ||
| const first = register.globalMiddlewares; | ||
| register.getGlobalMiddleware(); | ||
| assert.strictEqual(register.globalMiddlewares, first); | ||
| }); | ||
|
|
||
| it('still surfaces a genuinely missing middleware at resolution time', () => { | ||
| // The fix defers *when* middlewares are resolved; it must not hide a real | ||
| // misconfiguration — an unknown name still throws once resolved. | ||
| const { register } = createRegister({ middleware: [ 'nope' ] }, {}); | ||
| assert.throws(() => register.getGlobalMiddleware(), /Middleware nope not found/); | ||
| }); | ||
|
|
||
| it('keeps koa-compose ordering for multiple global middlewares', async () => { | ||
| const { register, app } = createRegister({ middleware: [ 'a', 'b' ] }, {}); | ||
| const order: string[] = []; | ||
| const make = (name: string) => () => async (_ctx: any, next: any) => { | ||
| order.push(`${name}:before`); | ||
| await next(); | ||
| order.push(`${name}:after`); | ||
| }; | ||
| app.middlewares.a = make('a'); | ||
| app.middlewares.b = make('b'); | ||
| const base: any = async (_ctx: any, next: any) => { | ||
| order.push('base'); | ||
| return next(); | ||
| }; | ||
| const wrapped = register.composeGlobalMiddleware(base); | ||
| await wrapped({} as any, async () => order.push('handler')); | ||
| assert.deepStrictEqual(order, [ | ||
| 'base', 'a:before', 'b:before', 'handler', 'b:after', 'a:after', | ||
| ]); | ||
| // compose is imported to assert the helper relies on koa-compose semantics. | ||
| assert.strictEqual(typeof compose, 'function'); | ||
| }); | ||
| }); |
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.
Calling
compose([ mw, self.globalMiddlewares ])on every request allocates a new composed middleware function and array on every invocation, which is inefficient. Sinceself.globalMiddlewaresis resolved once andmwis constant for each endpoint, we can cache the composed middleware locally insidecomposeGlobalMiddlewareto avoid redundant allocations.