Skip to content

Commit f636f05

Browse files
committed
refactor: url.parse
1 parent d5c01ff commit f636f05

File tree

1 file changed

+10
-30
lines changed

1 file changed

+10
-30
lines changed

modules/runtime/server/cache.ts

Lines changed: 10 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -102,12 +102,8 @@ function parseScopedPackageWithVersion(input: string): { name: string; version?:
102102
}
103103

104104
function getMockForUrl(url: string): MockResult | null {
105-
let urlObj: URL
106-
try {
107-
urlObj = new URL(url)
108-
} catch {
109-
return null
110-
}
105+
const urlObj = URL.parse(url)
106+
if (!urlObj) return null
111107

112108
const { host, pathname, searchParams } = urlObj
113109

@@ -359,12 +355,8 @@ async function handleFastNpmMeta(
359355
url: string,
360356
storage: ReturnType<typeof useStorage>,
361357
): Promise<MockResult | null> {
362-
let urlObj: URL
363-
try {
364-
urlObj = new URL(url)
365-
} catch {
366-
return null
367-
}
358+
const urlObj = URL.parse(url)
359+
if (!urlObj) return null
368360

369361
const { host, pathname, searchParams } = urlObj
370362

@@ -404,12 +396,8 @@ async function handleGitHubApi(
404396
url: string,
405397
storage: ReturnType<typeof useStorage>,
406398
): Promise<MockResult | null> {
407-
let urlObj: URL
408-
try {
409-
urlObj = new URL(url)
410-
} catch {
411-
return null
412-
}
399+
const urlObj = URL.parse(url)
400+
if (!urlObj) return null
413401

414402
const { host, pathname } = urlObj
415403

@@ -460,12 +448,8 @@ interface FixtureMatchWithVersion extends FixtureMatch {
460448
}
461449

462450
function matchUrlToFixture(url: string): FixtureMatchWithVersion | null {
463-
let urlObj: URL
464-
try {
465-
urlObj = new URL(url)
466-
} catch {
467-
return null
468-
}
451+
const urlObj = URL.parse(url)
452+
if (!urlObj) return null
469453

470454
const { host, pathname, searchParams } = urlObj
471455

@@ -549,12 +533,8 @@ async function handleJsdelivrDataApi(
549533
url: string,
550534
storage: ReturnType<typeof useStorage>,
551535
): Promise<MockResult | null> {
552-
let urlObj: URL
553-
try {
554-
urlObj = new URL(url)
555-
} catch {
556-
return null
557-
}
536+
const urlObj = URL.parse(url)
537+
if (!urlObj) return null
558538

559539
if (urlObj.host !== 'data.jsdelivr.com') return null
560540

0 commit comments

Comments
 (0)