Skip to content
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

egg-router-plus v2 存在 bug #5377

Open
wild-learner opened this issue Dec 27, 2024 · 1 comment
Open

egg-router-plus v2 存在 bug #5377

wild-learner opened this issue Dec 27, 2024 · 1 comment

Comments

@wild-learner
Copy link

egg-router-plus v2 sortRouters 方法使用了 trie-prefix-tree 处理 namespace prefix,但是 trie-prefix-tree 会对输入进行小写处理。如果 namespace prefix 存在大写字母,sortedPrefixes 的 prefix 就跟 namespaceRouters 不对应了,最终导致路由 404

https://github.com/eggjs/egg-router-plus/blob/cdfec8012f8e051a6b55d848cd4112191e566085/lib/router.js#L106-L110

function sortRouters(namespaceRouters) {
  const sortedPrefixes = trie(Array.from(namespaceRouters.keys())).getWords().reverse();
  if (namespaceRouters.has('')) sortedPrefixes.push('');
  return sortedPrefixes.map(prefix => [ prefix, namespaceRouters.get(prefix) ]);
}

https://github.com/lyndseybrowning/trie-prefix-tree/blob/dcb48612606e4af37f9c1d240420bf9b13228f65/src/create.js#L8-L15

  const trie = input.reduce((accumulator, item) => {
    item
      .toLowerCase()
      .split('')
      .reduce(append, accumulator);

    return accumulator;
  }, {});

例子

import { Application } from 'egg';

export default (app: Application) => {
  const { controller, router } = app;

  const subRouter = router.namespace('/A');
  subRouter.get('/b', controller.home.test);
};

访问 /A/a 会 404,访问 /a/a 会抛错
nodejs.TypeError: Cannot read properties of undefined (reading 'middleware')
at node_modules/egg-router-plus/lib/router.js:20:25

@wild-learner
Copy link
Author

wild-learner commented Dec 27, 2024

#4941 的根本原因

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant