From a6c921695db8f76e7e6593a4500c1a1cfee9aec5 Mon Sep 17 00:00:00 2001 From: Nathan Ridge Date: Sun, 26 Feb 2023 22:17:08 -0500 Subject: [PATCH] Add timeout to github API request This avoids the auto-install process hanging if the github website is unresponsive for some reason. Fixes the remaining issue in https://github.com/clangd/vscode-clangd/issues/406 --- src/index.ts | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/src/index.ts b/src/index.ts index b615792..b19fb33 100644 --- a/src/index.ts +++ b/src/index.ts @@ -165,12 +165,19 @@ export interface Asset { // Fetch the metadata for the latest stable clangd release. export async function latestRelease(): Promise { - const response = await fetch(githubReleaseURL); - if (!response.ok) { - console.log(response.url, response.status, response.statusText); - throw new Error(`Can't fetch release: ${response.statusText}`); + const timeoutController = new AbortController(); + const timeout = setTimeout(() => { timeoutController.abort(); }, 5000); + try { + const response = + await fetch(githubReleaseURL, {signal: timeoutController.signal}); + if (!response.ok) { + console.log(response.url, response.status, response.statusText); + throw new Error(`Can't fetch release: ${response.statusText}`); + } + return await response.json() as Release; + } finally { + clearTimeout(timeout); } - return await response.json() as Release; } // Determine which release asset should be installed for this machine.