Skip to content

Commit de0d699

Browse files
committed
fix(url): deopt absolute URIs in FastURL
1 parent 4e6ace6 commit de0d699

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

src/_url.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,11 @@ export const FastURL: { new (url: string | URLInit): URL & { _url: URL } } =
4343

4444
constructor(url: string | URLInit) {
4545
if (typeof url === "string") {
46-
this.#href = url;
46+
if (url[0] === "/") {
47+
this.#href = url;
48+
} else {
49+
this.#url = new NativeURL(url);
50+
}
4751
} else if (_needsNormRE.test(url.pathname)) {
4852
this.#url = new NativeURL(
4953
`${url.protocol || "http:"}//${url.host || "localhost"}${url.pathname}${url.search || ""}`,

test/url.test.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,35 @@ describe("FastURL", () => {
125125
}
126126
});
127127

128+
describe("absolute URI in request line", () => {
129+
const cases = [
130+
["http://example.com/path", "/path"],
131+
["http://example.com/path?q=1", "/path"],
132+
["file://hehe?/internal/run", "/"],
133+
["file://hehe/abc", "/abc"],
134+
["http://evil.com?/secret", "/"],
135+
["https://host/a/b/c?x=1", "/a/b/c"],
136+
] as const;
137+
138+
for (const [input, expected] of cases) {
139+
test(`"${input}" => pathname "${expected}"`, () => {
140+
const url = new NodeRequestURL({
141+
req: { url: input, headers: { host: "localhost" } } as any,
142+
});
143+
expect(url.pathname).toBe(expected);
144+
});
145+
146+
test(`"${input}" => pathname "${expected}" (after deopt)`, () => {
147+
const url = new NodeRequestURL({
148+
req: { url: input, headers: { host: "localhost" } } as any,
149+
});
150+
// Access hostname to trigger _url deopt
151+
void url.hostname;
152+
expect(url.pathname).toBe(expected);
153+
});
154+
}
155+
});
156+
128157
describe("pathname normalization", () => {
129158
const cases = [
130159
// Literal dot segments

0 commit comments

Comments
 (0)