Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ findRoute(router, "GET", "/");
> [!IMPORTANT]
> Method should **always be UPPERCASE**.

> [!TIP]
> If you need to register a pattern containing literal `:` or `*`, you can escape them with `\\`. For example, `/static\\:path/\\*\\*` matches only the static `/static:path/**` route.

## Compiler

<!-- automd:jsdocs src="./src/compiler.ts" -->
Expand Down
11 changes: 9 additions & 2 deletions src/operations/add.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ export function addRoute<T>(
path = `/${path}`;
}

path = path.replace(/\\:/g, "%3A");

const segments = splitPath(path);

let node = ctx.root;
Expand All @@ -26,7 +28,7 @@ export function addRoute<T>(
const paramsRegexp: RegExp[] = [];

for (let i = 0; i < segments.length; i++) {
const segment = segments[i];
let segment = segments[i];

// Wildcard
if (segment.startsWith("**")) {
Expand Down Expand Up @@ -64,6 +66,11 @@ export function addRoute<T>(
}

// Static
if (segment === "\\*") {
segment = segments[i] = "*";
} else if (segment === "\\*\\*") {
segment = segments[i] = "**";
}
const child = node.static?.[segment];
if (child) {
node = child;
Expand Down Expand Up @@ -91,7 +98,7 @@ export function addRoute<T>(

// Static
if (!hasParams) {
ctx.static[path] = node;
ctx.static["/" + segments.join("/")] = node;
}
}

Expand Down
32 changes: 18 additions & 14 deletions test/.snapshot/compiled-aot.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@ const findRoute = /* @__PURE__ */ (() => {
$3 = { path: "/test/foo/baz" },
$4 = { path: "/test/fooo" },
$5 = { path: "/another/path" },
$6 = { path: "/test/foo/*" },
$7 = { path: "/test/foo/**" },
$8 = { path: "/test/:id" },
$9 = { path: "/test/:idY/y" },
$10 = { path: "/test/:idYZ/y/z" },
$11 = { path: "/wildcard/**" },
$12 = { path: "/**" };
$6 = { path: "/static\\:path/\\*/\\*\\*" },
$7 = { path: "/test/foo/*" },
$8 = { path: "/test/foo/**" },
$9 = { path: "/test/:id" },
$10 = { path: "/test/:idY/y" },
$11 = { path: "/test/:idYZ/y/z" },
$12 = { path: "/wildcard/**" },
$13 = { path: "/**" };
return (m, p) => {
if (p.charCodeAt(p.length - 1) === 47) p = p.slice(0, -1) || "/";
if (p === "/test") {
Expand All @@ -32,34 +33,37 @@ const findRoute = /* @__PURE__ */ (() => {
if (p === "/another/path") {
if (m === "GET") return { data: $5 };
}
if (p === "/static%3Apath/*/**") {
if (m === "GET") return { data: $6 };
}
let s = p.split("/"),
l = s.length - 1;
if (s[1] === "test") {
if (s[2] === "foo") {
if (l === 3 || l === 2) {
if (m === "GET") return { data: $6, params: { _0: s[3] } };
if (m === "GET") return { data: $7, params: { _0: s[3] } };
}
if (m === "GET")
return { data: $7, params: { _: s.slice(3).join("/") } };
return { data: $8, params: { _: s.slice(3).join("/") } };
}
if (l === 2 || l === 1) {
if (m === "GET") if (l >= 2) return { data: $8, params: { id: s[2] } };
if (m === "GET") if (l >= 2) return { data: $9, params: { id: s[2] } };
}
if (s[3] === "y") {
if (l === 3) {
if (m === "GET") return { data: $9, params: { idY: s[2] } };
if (m === "GET") return { data: $10, params: { idY: s[2] } };
}
if (s[4] === "z") {
if (l === 4) {
if (m === "GET") return { data: $10, params: { idYZ: s[2] } };
if (m === "GET") return { data: $11, params: { idYZ: s[2] } };
}
}
}
}
if (s[1] === "wildcard") {
if (m === "GET")
return { data: $11, params: { _: s.slice(2).join("/") } };
return { data: $12, params: { _: s.slice(2).join("/") } };
}
if (m === "GET") return { data: $12, params: { _: s.slice(1).join("/") } };
if (m === "GET") return { data: $13, params: { _: s.slice(1).join("/") } };
};
})();
17 changes: 10 additions & 7 deletions test/.snapshot/compiled-jit.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -18,31 +18,34 @@
if (p === "/another/path") {
if (m === "GET") return { data: $5 };
}
if (p === "/static%3Apath/*/**") {
if (m === "GET") return { data: $6 };
}
let s = p.split("/"),
l = s.length - 1;
if (s[1] === "test") {
if (s[2] === "foo") {
if (l === 3 || l === 2) {
if (m === "GET") return { data: $6, params: { _0: s[3] } };
if (m === "GET") return { data: $7, params: { _0: s[3] } };
}
if (m === "GET") return { data: $7, params: { _: s.slice(3).join("/") } };
if (m === "GET") return { data: $8, params: { _: s.slice(3).join("/") } };
}
if (l === 2 || l === 1) {
if (m === "GET") if (l >= 2) return { data: $8, params: { id: s[2] } };
if (m === "GET") if (l >= 2) return { data: $9, params: { id: s[2] } };
}
if (s[3] === "y") {
if (l === 3) {
if (m === "GET") return { data: $9, params: { idY: s[2] } };
if (m === "GET") return { data: $10, params: { idY: s[2] } };
}
if (s[4] === "z") {
if (l === 4) {
if (m === "GET") return { data: $10, params: { idYZ: s[2] } };
if (m === "GET") return { data: $11, params: { idYZ: s[2] } };
}
}
}
}
if (s[1] === "wildcard") {
if (m === "GET") return { data: $11, params: { _: s.slice(2).join("/") } };
if (m === "GET") return { data: $12, params: { _: s.slice(2).join("/") } };
}
if (m === "GET") return { data: $12, params: { _: s.slice(1).join("/") } };
if (m === "GET") return { data: $13, params: { _: s.slice(1).join("/") } };
};
16 changes: 15 additions & 1 deletion test/find.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ describe("route matching", () => {
"/test/fooo",
"/another/path",
"/wildcard/**",
"/static\\:path/\\*/\\*\\*",
"/**",
]);

Expand All @@ -41,6 +42,9 @@ describe("route matching", () => {
│ ├── /path ┈> [GET] /another/path
├── /wildcard
│ ├── /** ┈> [GET] /wildcard/**
├── /static%3Apath
│ ├── /*
│ │ ├── /** ┈> [GET] /static\\:path/\\*/\\*\\*
├── /** ┈> [GET] /**"
`);
});
Expand Down Expand Up @@ -134,6 +138,13 @@ describe("route matching", () => {
data: { path: "/**" },
params: { _: "any/deep/path" },
});
// Escaped characters
expect(match("GET", "/static%3Apath/*/**")).toMatchObject({
data: { path: "/static\\:path/\\*/\\*\\*" },
});
expect(match("GET", "/static:path/some/deep/path")).toMatchObject({
data: { path: "/**" }, // should not match static route
});
});
}

Expand All @@ -157,7 +168,10 @@ describe("route matching", () => {
├── /another
│ ├── /path ┈> [GET] /another/path
├── /wildcard
│ ├── /** ┈> [GET] /wildcard/**"
│ ├── /** ┈> [GET] /wildcard/**
├── /static%3Apath
│ ├── /*
│ │ ├── /** ┈> [GET] /static\\:path/\\*/\\*\\*"
`);
expect(findRoute(router, "GET", "/test")).toBeUndefined();
});
Expand Down
4 changes: 4 additions & 0 deletions test/regexp.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ describe("routeToRegExp", () => {
regex: /^\/base\/?(?<path>.+)\/?$/,
match: [["/base/anything/more", { path: "anything/more" }]],
},
"/static%3Apath/\\*/\\*\\*": {
regex: /^\/static%3Apath\/\*\/\*\*\/?$/,
match: [["/static%3Apath/*/**"]],
},
"/**": {
regex: /^\/?(?<_>.*)\/?$/,
match: [
Expand Down
5 changes: 4 additions & 1 deletion test/router.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,7 @@ describe("Router insert", () => {
"/api/v1",
"/api/v2",
"/api/v3",
"/static\\:path/\\*\\*",
]);

addRoute(router, "", "/api/v3", {
Expand All @@ -510,7 +511,9 @@ describe("Router insert", () => {
├── /api
│ ├── /v1 ┈> [GET] /api/v1
│ ├── /v2 ┈> [GET] /api/v2
│ ├── /v3 ┈> [GET] /api/v3, [*] /api/v3(overridden)"
│ ├── /v3 ┈> [GET] /api/v3, [*] /api/v3(overridden)
├── /static%3Apath
│ ├── /** ┈> [GET] /static\\:path/\\*\\*"
`);
});
});
Expand Down