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

fix(registry): Prioritize route registration based on specificity #18002

Merged
merged 3 commits into from
Feb 19, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 29 additions & 5 deletions lib/registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,16 +84,40 @@
export { namespaces };

const app = new Hono();
const sortRoutes = (routes: Record<string, Route & { location: string }>) =>
Object.entries(routes).sort(([pathA], [pathB]) => {

Check failure on line 88 in lib/registry.ts

View workflow job for this annotation

GitHub Actions / Vitest on Node lts/-1

lib/app.test.ts

TypeError: Cannot convert undefined or null to object ❯ sortRoutes lib/registry.ts:88:12 ❯ lib/registry.ts:111:26 ❯ lib/app.tsx:19:32 ❯ lib/app.test.ts:2:31

Check failure on line 88 in lib/registry.ts

View workflow job for this annotation

GitHub Actions / Vitest on Node lts/-1

lib/pkg.test.ts > pkg > config

TypeError: Cannot convert undefined or null to object ❯ sortRoutes lib/registry.ts:88:12 ❯ lib/registry.ts:111:26 ❯ lib/app.tsx:19:32 ❯ Module.init lib/pkg.ts:15:12 ❯ lib/pkg.test.ts:6:9

Check failure on line 88 in lib/registry.ts

View workflow job for this annotation

GitHub Actions / Vitest on Node lts/-1

lib/registry.test.ts

TypeError: Cannot convert undefined or null to object ❯ sortRoutes lib/registry.ts:88:12 ❯ lib/registry.ts:111:26 ❯ lib/app.tsx:19:32 ❯ lib/registry.test.ts:2:31

Check failure on line 88 in lib/registry.ts

View workflow job for this annotation

GitHub Actions / Vitest on Node lts/-1

lib/routes.test.ts

TypeError: Cannot convert undefined or null to object ❯ sortRoutes lib/registry.ts:88:12 ❯ lib/registry.ts:111:26 ❯ lib/app.tsx:19:32 ❯ lib/routes.test.ts:2:31

Check failure on line 88 in lib/registry.ts

View workflow job for this annotation

GitHub Actions / Vitest on Node lts/-1

lib/errors/index.test.ts

TypeError: Cannot convert undefined or null to object ❯ sortRoutes lib/registry.ts:88:12 ❯ lib/registry.ts:111:26 ❯ lib/app.tsx:19:32 ❯ lib/index.ts:5:31

Check failure on line 88 in lib/registry.ts

View workflow job for this annotation

GitHub Actions / Vitest on Node lts/-1

lib/middleware/access-control.test.ts > access-control > access key

TypeError: Cannot convert undefined or null to object ❯ sortRoutes lib/registry.ts:88:12 ❯ lib/registry.ts:111:26 ❯ lib/app.tsx:19:32 ❯ lib/middleware/access-control.test.ts:21:22

Check failure on line 88 in lib/registry.ts

View workflow job for this annotation

GitHub Actions / Vitest on Node lts/-1

lib/middleware/anti-hotlink.test.ts > anti-hotlink > template-legacy

TypeError: Cannot convert undefined or null to object ❯ sortRoutes lib/registry.ts:88:12 ❯ lib/registry.ts:111:26 ❯ lib/app.tsx:19:32 ❯ testAntiHotlink lib/middleware/anti-hotlink.test.ts:264:18 ❯ expectImgProcessed lib/middleware/anti-hotlink.test.ts:313:5 ❯ lib/middleware/anti-hotlink.test.ts:340:9

Check failure on line 88 in lib/registry.ts

View workflow job for this annotation

GitHub Actions / Vitest on Node lts/-1

lib/middleware/anti-hotlink.test.ts > anti-hotlink > template-experimental

TypeError: Cannot convert undefined or null to object ❯ sortRoutes lib/registry.ts:88:12 ❯ lib/registry.ts:111:26 ❯ lib/app.tsx:19:32 ❯ testAntiHotlink lib/middleware/anti-hotlink.test.ts:264:18 ❯ expectImgProcessed lib/middleware/anti-hotlink.test.ts:313:5 ❯ lib/middleware/anti-hotlink.test.ts:346:9

Check failure on line 88 in lib/registry.ts

View workflow job for this annotation

GitHub Actions / Vitest on Node lts/*

lib/app.test.ts

TypeError: Cannot convert undefined or null to object ❯ sortRoutes lib/registry.ts:88:12 ❯ lib/registry.ts:111:26 ❯ lib/app.tsx:19:32 ❯ lib/app.test.ts:2:31

Check failure on line 88 in lib/registry.ts

View workflow job for this annotation

GitHub Actions / Vitest on Node lts/*

lib/pkg.test.ts > pkg > config

TypeError: Cannot convert undefined or null to object ❯ sortRoutes lib/registry.ts:88:12 ❯ lib/registry.ts:111:26 ❯ lib/app.tsx:19:32 ❯ Module.init lib/pkg.ts:15:12 ❯ lib/pkg.test.ts:6:9

Check failure on line 88 in lib/registry.ts

View workflow job for this annotation

GitHub Actions / Vitest on Node lts/*

lib/registry.test.ts

TypeError: Cannot convert undefined or null to object ❯ sortRoutes lib/registry.ts:88:12 ❯ lib/registry.ts:111:26 ❯ lib/app.tsx:19:32 ❯ lib/registry.test.ts:2:31

Check failure on line 88 in lib/registry.ts

View workflow job for this annotation

GitHub Actions / Vitest on Node lts/*

lib/routes.test.ts

TypeError: Cannot convert undefined or null to object ❯ sortRoutes lib/registry.ts:88:12 ❯ lib/registry.ts:111:26 ❯ lib/app.tsx:19:32 ❯ lib/routes.test.ts:2:31

Check failure on line 88 in lib/registry.ts

View workflow job for this annotation

GitHub Actions / Vitest on Node lts/*

lib/errors/index.test.ts

TypeError: Cannot convert undefined or null to object ❯ sortRoutes lib/registry.ts:88:12 ❯ lib/registry.ts:111:26 ❯ lib/app.tsx:19:32 ❯ lib/index.ts:5:31

Check failure on line 88 in lib/registry.ts

View workflow job for this annotation

GitHub Actions / Vitest on Node lts/*

lib/middleware/access-control.test.ts > access-control > access key

TypeError: Cannot convert undefined or null to object ❯ sortRoutes lib/registry.ts:88:12 ❯ lib/registry.ts:111:26 ❯ lib/app.tsx:19:32 ❯ lib/middleware/access-control.test.ts:21:22

Check failure on line 88 in lib/registry.ts

View workflow job for this annotation

GitHub Actions / Vitest on Node lts/*

lib/middleware/anti-hotlink.test.ts > anti-hotlink > template-legacy

TypeError: Cannot convert undefined or null to object ❯ sortRoutes lib/registry.ts:88:12 ❯ lib/registry.ts:111:26 ❯ lib/app.tsx:19:32 ❯ testAntiHotlink lib/middleware/anti-hotlink.test.ts:264:18 ❯ expectImgProcessed lib/middleware/anti-hotlink.test.ts:313:5 ❯ lib/middleware/anti-hotlink.test.ts:340:9

Check failure on line 88 in lib/registry.ts

View workflow job for this annotation

GitHub Actions / Vitest on Node lts/*

lib/middleware/anti-hotlink.test.ts > anti-hotlink > template-experimental

TypeError: Cannot convert undefined or null to object ❯ sortRoutes lib/registry.ts:88:12 ❯ lib/registry.ts:111:26 ❯ lib/app.tsx:19:32 ❯ testAntiHotlink lib/middleware/anti-hotlink.test.ts:264:18 ❯ expectImgProcessed lib/middleware/anti-hotlink.test.ts:313:5 ❯ lib/middleware/anti-hotlink.test.ts:346:9

Check failure on line 88 in lib/registry.ts

View workflow job for this annotation

GitHub Actions / Vitest on Node latest

lib/app.test.ts

TypeError: Cannot convert undefined or null to object ❯ sortRoutes lib/registry.ts:88:12 ❯ lib/registry.ts:111:26 ❯ lib/app.tsx:19:32 ❯ lib/app.test.ts:2:31

Check failure on line 88 in lib/registry.ts

View workflow job for this annotation

GitHub Actions / Vitest on Node latest

lib/pkg.test.ts > pkg > config

TypeError: Cannot convert undefined or null to object ❯ sortRoutes lib/registry.ts:88:12 ❯ lib/registry.ts:111:26 ❯ lib/app.tsx:19:32 ❯ Module.init lib/pkg.ts:15:12 ❯ lib/pkg.test.ts:6:9

Check failure on line 88 in lib/registry.ts

View workflow job for this annotation

GitHub Actions / Vitest on Node latest

lib/registry.test.ts

TypeError: Cannot convert undefined or null to object ❯ sortRoutes lib/registry.ts:88:12 ❯ lib/registry.ts:111:26 ❯ lib/app.tsx:19:32 ❯ lib/registry.test.ts:2:31

Check failure on line 88 in lib/registry.ts

View workflow job for this annotation

GitHub Actions / Vitest on Node latest

lib/routes.test.ts

TypeError: Cannot convert undefined or null to object ❯ sortRoutes lib/registry.ts:88:12 ❯ lib/registry.ts:111:26 ❯ lib/app.tsx:19:32 ❯ lib/routes.test.ts:2:31

Check failure on line 88 in lib/registry.ts

View workflow job for this annotation

GitHub Actions / Vitest on Node latest

lib/errors/index.test.ts

TypeError: Cannot convert undefined or null to object ❯ sortRoutes lib/registry.ts:88:12 ❯ lib/registry.ts:111:26 ❯ lib/app.tsx:19:32 ❯ lib/index.ts:5:31

Check failure on line 88 in lib/registry.ts

View workflow job for this annotation

GitHub Actions / Vitest on Node latest

lib/middleware/access-control.test.ts > access-control > access key

TypeError: Cannot convert undefined or null to object ❯ sortRoutes lib/registry.ts:88:12 ❯ lib/registry.ts:111:26 ❯ lib/app.tsx:19:32 ❯ lib/middleware/access-control.test.ts:21:22

Check failure on line 88 in lib/registry.ts

View workflow job for this annotation

GitHub Actions / Vitest on Node latest

lib/middleware/anti-hotlink.test.ts > anti-hotlink > template-legacy

TypeError: Cannot convert undefined or null to object ❯ sortRoutes lib/registry.ts:88:12 ❯ lib/registry.ts:111:26 ❯ lib/app.tsx:19:32 ❯ testAntiHotlink lib/middleware/anti-hotlink.test.ts:264:18 ❯ expectImgProcessed lib/middleware/anti-hotlink.test.ts:313:5 ❯ lib/middleware/anti-hotlink.test.ts:340:9

Check failure on line 88 in lib/registry.ts

View workflow job for this annotation

GitHub Actions / Vitest on Node latest

lib/middleware/anti-hotlink.test.ts > anti-hotlink > template-experimental

TypeError: Cannot convert undefined or null to object ❯ sortRoutes lib/registry.ts:88:12 ❯ lib/registry.ts:111:26 ❯ lib/app.tsx:19:32 ❯ testAntiHotlink lib/middleware/anti-hotlink.test.ts:264:18 ❯ expectImgProcessed lib/middleware/anti-hotlink.test.ts:313:5 ❯ lib/middleware/anti-hotlink.test.ts:346:9
const segmentsA = pathA.split('/');
const segmentsB = pathB.split('/');
const lenA = segmentsA.length;
const lenB = segmentsB.length;
const minLen = Math.min(lenA, lenB);

for (let i = 0; i < minLen; i++) {
const segmentA = segmentsA[i];
const segmentB = segmentsB[i];

// Literal segments have priority over parameter segments
if (segmentA.startsWith(':') !== segmentB.startsWith(':')) {
return segmentA.startsWith(':') ? 1 : -1;
}
}

return 0;
});

for (const namespace in namespaces) {
const subApp = app.basePath(`/${namespace}`);
for (const path in namespaces[namespace].routes) {

const sortedRoutes = sortRoutes(namespaces[namespace].routes);

for (const [path, routeData] of sortedRoutes) {
const wrappedHandler: Handler = async (ctx) => {
if (!ctx.get('data')) {
if (typeof namespaces[namespace].routes[path].handler !== 'function') {
const { route } = await import(`./routes/${namespace}/${namespaces[namespace].routes[path].location}`);
namespaces[namespace].routes[path].handler = route.handler;
if (typeof routeData.handler !== 'function') {
const { route } = await import(`./routes/${namespace}/${routeData.location}`);
routeData.handler = route.handler;
}
ctx.set('data', await namespaces[namespace].routes[path].handler(ctx));
ctx.set('data', await routeData.handler(ctx));
}
};
subApp.get(path, wrappedHandler);
Expand Down
Loading