Skip to content

Commit 31ae154

Browse files
committed
perf: use $fetch timeout instead separate controller + timeout
1 parent 2ebd12d commit 31ae154

2 files changed

Lines changed: 6 additions & 15 deletions

File tree

server/api/contributors.get.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export default defineCachedEventHandler(
1515
{
1616
headers: {
1717
'Accept': 'application/vnd.github.v3+json',
18-
'User-Agent': 'npmx.dev',
18+
'User-Agent': 'npmx',
1919
},
2020
},
2121
)

server/utils/docs/client.ts

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -88,15 +88,12 @@ function createLoader(): (
8888
return undefined
8989
}
9090

91-
const controller = new AbortController()
92-
const timeoutId = setTimeout(() => controller.abort(), FETCH_TIMEOUT_MS)
93-
9491
try {
95-
const response = await fetch(url.toString(), {
92+
const response = await $fetch.raw(url.toString(), {
93+
method: 'GET',
94+
timeout: FETCH_TIMEOUT_MS,
9695
redirect: 'follow',
97-
signal: controller.signal,
9896
})
99-
clearTimeout(timeoutId)
10097

10198
if (response.status !== 200) {
10299
return undefined
@@ -115,7 +112,6 @@ function createLoader(): (
115112
content,
116113
}
117114
} catch {
118-
clearTimeout(timeoutId)
119115
return undefined
120116
}
121117
}
@@ -156,18 +152,13 @@ function createResolver(): (specifier: string, referrer: string) => string {
156152
async function getTypesUrl(packageName: string, version: string): Promise<string | null> {
157153
const url = `https://esm.sh/${packageName}@${version}`
158154

159-
const controller = new AbortController()
160-
const timeoutId = setTimeout(() => controller.abort(), FETCH_TIMEOUT_MS)
161-
162155
try {
163-
const response = await fetch(url, {
156+
const response = await $fetch.raw(url, {
164157
method: 'HEAD',
165-
signal: controller.signal,
158+
timeout: FETCH_TIMEOUT_MS,
166159
})
167-
clearTimeout(timeoutId)
168160
return response.headers.get('x-typescript-types')
169161
} catch {
170-
clearTimeout(timeoutId)
171162
return null
172163
}
173164
}

0 commit comments

Comments
 (0)