unjs / rou3

🌳 Lightweight and fast rou(ter) for JavaScript
MIT License
442 stars 16 forks source link

Wildcard matcher overlaps prefix #70

Closed pi0 closed 7 months ago

pi0 commented 11 months ago

Context: https://github.com/nuxt/nuxt/issues/24107

Radix 3 version: 1.1.0 and main

import { createRouter, toRouteMatcher } from "./src";
const router = createRouter();
router.insert("/c/**", { name: "c/**" });
router.insert("/cart", { name: "a" });
const matcher = toRouteMatcher(router);
console.log(matcher.matchAll("/cart"));
michalzaq12 commented 8 months ago

It seems that / (slash) is incorrectly ignored in the wildcard route. /c/** matches everything starting with "c"

Reproduction:

const { createRouter, toRouteMatcher } = require("radix3");
const router = createRouter();
router.insert("/c/**", { name: "c/**" });
router.insert("/cart", { name: "a" });

const matcher = toRouteMatcher(router);
// BUG: Should return empty array
console.log(matcher.matchAll("/cAnything"));

// Example from README.md
const router2 = createRouter({
  routes: {
    "/foo": { m: "foo" }, // Matches /foo only
    "/foo/**": { m: "foo/**" }, // Matches /foo/<any>
    "/foo/bar": { m: "foo/bar" }, // Matches /foo/bar only
    "/foo/bar/baz": { m: "foo/bar/baz" }, // Matches /foo/bar/baz only
    "/foo/*/baz": { m: "foo/*/baz" }, // Matches /foo/<any>/baz
  },
});

const matcher2 = toRouteMatcher(router2);
// BUG: Should return empty array
console.log(matcher2.matchAll("/foobarbaz"))