Chore: add timeout on speedtest API fetching

This commit is contained in:
SukkaW
2023-12-24 16:44:26 +08:00
parent 5be5a4e0f6
commit d83f8ccd04
2 changed files with 26 additions and 12 deletions

View File

@@ -13,7 +13,7 @@ import { getGorhillPublicSuffixPromise } from './lib/get-gorhill-publicsuffix';
const s = new Sema(3);
const latestTopUserAgentsPromise = fetchWithRetry('https://unpkg.com/top-user-agents@latest/index.json')
.then(res => res.json() as Promise<string[]>);
.then(res => res.json<string[]>());
const querySpeedtestApi = async (keyword: string): Promise<Array<string | null>> => {
const topUserAgents = (await Promise.all([
@@ -39,13 +39,17 @@ const querySpeedtestApi = async (keyword: string): Promise<Array<string | null>>
'Sec-Fetch-Mode': 'cors',
'Sec-Fetch-Site': 'same-origin',
'Sec-Gpc': '1'
}
},
retry: {
retryOnAborted: true
},
signal: AbortSignal.timeout(4000)
});
if (!res.ok) {
throw new Error(`${res.statusText}\n${await res.text()}`);
}
const json = await res.json() as Array<{ url: string }>;
const json = await res.json<Array<{ url: string }>>();
s.release();