From 99c44b9c265da6ab3b0183050be31c6af8b3b5c0 Mon Sep 17 00:00:00 2001 From: SukkaW Date: Tue, 26 Dec 2023 14:25:58 +0800 Subject: [PATCH] Chore: fix fetch timeout --- Build/build-speedtest-domainset.ts | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/Build/build-speedtest-domainset.ts b/Build/build-speedtest-domainset.ts index a19fbc70..af40c13e 100644 --- a/Build/build-speedtest-domainset.ts +++ b/Build/build-speedtest-domainset.ts @@ -27,6 +27,10 @@ const querySpeedtestApi = async (keyword: string): Promise> console.log(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`, { headers: { dnt: '1', @@ -44,8 +48,11 @@ const querySpeedtestApi = async (keyword: string): Promise> retry: { retryOnAborted: true }, - signal: AbortSignal.timeout(4000) + signal: controller.signal }); + + clearTimeout(timer); + if (!res.ok) { throw new Error(`${res.statusText}\n${await res.text()}`); }