Browse Source

Chore: fix fetch timeout

SukkaW 2 years ago
parent
commit
99c44b9c26
1 changed files with 8 additions and 1 deletions
  1. 8 1
      Build/build-speedtest-domainset.ts

+ 8 - 1
Build/build-speedtest-domainset.ts

@@ -27,6 +27,10 @@ const querySpeedtestApi = async (keyword: string): Promise<Array<string | null>>
     console.log(key);
     console.log(key);
     console.time(key);
     console.time(key);
 
 
+    // AbortSignal.timeout() is not supported by bun.
+    const controller = new AbortController();
+    const timer = setTimeout(() => controller.abort(), 4000);
+
     const res = await fetchWithRetry(`https://www.speedtest.net/api/js/servers?engine=js&search=${keyword}&limit=100`, {
     const res = await fetchWithRetry(`https://www.speedtest.net/api/js/servers?engine=js&search=${keyword}&limit=100`, {
       headers: {
       headers: {
         dnt: '1',
         dnt: '1',
@@ -44,8 +48,11 @@ const querySpeedtestApi = async (keyword: string): Promise<Array<string | null>>
       retry: {
       retry: {
         retryOnAborted: true
         retryOnAborted: true
       },
       },
-      signal: AbortSignal.timeout(4000)
+      signal: controller.signal
     });
     });
+
+    clearTimeout(timer);
+
     if (!res.ok) {
     if (!res.ok) {
       throw new Error(`${res.statusText}\n${await res.text()}`);
       throw new Error(`${res.statusText}\n${await res.text()}`);
     }
     }