Skip to content

Commit 00d83ca

Browse files
committed
🐛 处理特殊的match #440
1 parent 6b7878b commit 00d83ca

2 files changed

Lines changed: 43 additions & 4 deletions

File tree

src/pkg/utils/match.test.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,14 @@ describe("UrlMatch-search", () => {
7979
expect(url.match("https://example.com/foo/bar/example.path")).toEqual(["ok1"]);
8080
expect(url.match("https://example.com/foo/bar/example.path2")).toEqual(["ok1"]);
8181
});
82+
it("*.example.com/path/*", () => {
83+
const url = new UrlMatch<string>();
84+
url.add("*.example.com/path/*", "ok1");
85+
expect(url.match("https://www.example.com/path/foo")).toEqual(["ok1"]);
86+
expect(url.match("https://www.example.com/path/foo/bar")).toEqual(["ok1"]);
87+
expect(url.match("https://www.example.com/path/foo/bar/baz")).toEqual(["ok1"]);
88+
expect(url.match("https://www.example.com/path2/foo")).toEqual([]);
89+
});
8290
});
8391

8492
describe("UrlMatch-port1", () => {
@@ -137,18 +145,21 @@ describe("dealPatternMatches", () => {
137145
"*://api.*.example.com/*",
138146
"*://api.*.*.example.com/*",
139147
"*://*example.com/*",
148+
"*.example.com/path/*",
140149
]);
141150
expect(matches.patternResult).toEqual([
142151
"*://*/*",
143152
"*://*.example.com/*",
144153
"*://*.example.com/*",
145154
"*://example.com/*",
155+
"*://*.example.com/path/*",
146156
]);
147157
expect(matches.result).toEqual([
148158
"*://www.example.com*",
149159
"*://api.*.example.com/*",
150160
"*://api.*.*.example.com/*",
151161
"*://*example.com/*",
162+
"*.example.com/path/*",
152163
]);
153164
});
154165
it("特殊情况-exclude", () => {
@@ -239,5 +250,12 @@ describe("parsePatternMatchesURL", () => {
239250
host: "*.example.com",
240251
path: "*",
241252
});
253+
matches = parsePatternMatchesURL("*.example.com/path/*");
254+
console.log(matches);
255+
expect(matches).toEqual({
256+
scheme: "*",
257+
host: "*.example.com",
258+
path: "path/*",
259+
});
242260
});
243261
});

src/pkg/utils/match.ts

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,19 @@ export default class Match<T> {
4343
search: "*",
4444
};
4545
default:
46+
// 无*://的情况
47+
if (!url.includes("://")) {
48+
// 直接转为通配符
49+
const match = /^(.*?)((\/.*?)(\?.*?|)|)$/.exec(url);
50+
if (match) {
51+
return {
52+
scheme: "*",
53+
host: match[1],
54+
path: match[3] || (url[url.length - 1] === "*" ? "*" : "/"),
55+
search: match[4],
56+
};
57+
}
58+
}
4659
}
4760
return undefined;
4861
}
@@ -61,10 +74,6 @@ export default class Match<T> {
6174
break;
6275
default:
6376
}
64-
let pos = u.host.indexOf("*");
65-
if (u.host === "*" || u.host === "**") {
66-
pos = -1;
67-
}
6877
u.host = u.host.replace(/\*/g, "[^/]*?");
6978
// 处理 *.开头
7079
if (u.host.startsWith("[^/]*?.")) {
@@ -240,6 +249,18 @@ export function parsePatternMatchesURL(
240249
};
241250
break;
242251
default:
252+
// 无*://的情况
253+
if (!url.includes("*://")) {
254+
// 直接转为通配符
255+
const match = /^(.+?)(\/(.*?)(\?.*?|)|)$/.exec(url);
256+
if (match) {
257+
result = {
258+
scheme: "*",
259+
host: match[1],
260+
path: match[3] || (url[url.length - 1] === "*" ? "*" : ""),
261+
};
262+
}
263+
}
243264
}
244265
}
245266
if (result) {

0 commit comments

Comments
 (0)