[READ-ONLY] a fast, modern browser for the npm registry
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

perf: use `$fetch.raw` instead fetch with separate controller + timeout (#485)

authored by

Robin and committed by
GitHub
f8f43732 92c7a853

+16 -19
+1 -1
server/api/contributors.get.ts
··· 15 15 { 16 16 headers: { 17 17 'Accept': 'application/vnd.github.v3+json', 18 - 'User-Agent': 'npmx.dev', 18 + 'User-Agent': 'npmx', 19 19 }, 20 20 }, 21 21 )
+15 -18
server/utils/docs/client.ts
··· 84 84 let url: URL 85 85 try { 86 86 url = new URL(specifier) 87 - } catch { 87 + } catch (e) { 88 + // eslint-disable-next-line no-console 89 + console.error(e) 88 90 return undefined 89 91 } 90 92 ··· 93 95 return undefined 94 96 } 95 97 96 - const controller = new AbortController() 97 - const timeoutId = setTimeout(() => controller.abort(), FETCH_TIMEOUT_MS) 98 - 99 98 try { 100 - const response = await fetch(url.toString(), { 99 + const response = await $fetch.raw<Blob>(url.toString(), { 100 + method: 'GET', 101 + timeout: FETCH_TIMEOUT_MS, 101 102 redirect: 'follow', 102 - signal: controller.signal, 103 103 }) 104 - clearTimeout(timeoutId) 105 104 106 105 if (response.status !== 200) { 107 106 return undefined 108 107 } 109 108 110 - const content = await response.text() 109 + const content = (await response._data?.text()) ?? '' 111 110 const headers: Record<string, string> = {} 112 111 for (const [key, value] of response.headers) { 113 112 headers[key.toLowerCase()] = value ··· 119 118 headers, 120 119 content, 121 120 } 122 - } catch { 123 - clearTimeout(timeoutId) 121 + } catch (e) { 122 + // eslint-disable-next-line no-console 123 + console.error(e) 124 124 return undefined 125 125 } 126 126 } ··· 161 161 async function getTypesUrl(packageName: string, version: string): Promise<string | null> { 162 162 const url = `https://esm.sh/${packageName}@${version}` 163 163 164 - const controller = new AbortController() 165 - const timeoutId = setTimeout(() => controller.abort(), FETCH_TIMEOUT_MS) 166 - 167 164 try { 168 - const response = await fetch(url, { 165 + const response = await $fetch.raw(url, { 169 166 method: 'HEAD', 170 - signal: controller.signal, 167 + timeout: FETCH_TIMEOUT_MS, 171 168 }) 172 - clearTimeout(timeoutId) 173 169 return response.headers.get('x-typescript-types') 174 - } catch { 175 - clearTimeout(timeoutId) 170 + } catch (e) { 171 + // eslint-disable-next-line no-console 172 + console.error(e) 176 173 return null 177 174 } 178 175 }