Skip to content
This repository was archived by the owner on Mar 13, 2025. It is now read-only.

Commit 138859c

Browse files
committed
Allow */* route
1 parent 738a7f2 commit 138859c

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

packages/core/src/router.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ export class Router {
3636
const protocol = hasProtocol ? url.protocol : undefined;
3737

3838
const allowHostnamePrefix = url.hostname.startsWith("*");
39-
if (allowHostnamePrefix) {
39+
const anyHostname = url.hostname === "*";
40+
if (allowHostnamePrefix && !anyHostname) {
4041
url.hostname = url.hostname.substring(1);
4142
}
4243

@@ -51,7 +52,7 @@ export class Router {
5152
`Route "${route}" for "${target}" contains a query string. This is not allowed.`
5253
);
5354
}
54-
if (url.toString().includes("*")) {
55+
if (url.toString().includes("*") && !anyHostname) {
5556
throw new RouterError(
5657
"ERR_INFIX_WILDCARD",
5758
`Route "${route}" for "${target}" contains an infix wildcard. This is not allowed.`
@@ -64,7 +65,7 @@ export class Router {
6465

6566
protocol,
6667
allowHostnamePrefix,
67-
hostname: url.hostname,
68+
hostname: anyHostname ? "" : url.hostname,
6869
path: url.pathname,
6970
allowPathSuffix,
7071
});

packages/core/test/router.spec.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,3 +142,10 @@ test("Router: returns null if no routes match", (t) => {
142142
router.update(new Map([["a", ["example.com/*"]]]));
143143
t.is(router.match(new URL("https://miniflare.dev/")), null);
144144
});
145+
test("Router: matches everything route", (t) => {
146+
const router = new Router();
147+
router.update(new Map([["a", ["*/*"]]]));
148+
t.is(router.match(new URL("http://example.com/")), "a");
149+
t.is(router.match(new URL("https://example.com/")), "a");
150+
t.is(router.match(new URL("https://miniflare.dev/")), "a");
151+
});

0 commit comments

Comments
 (0)