From f4e58553187e085fbee1fb3bfc3ed86ecf9633c6 Mon Sep 17 00:00:00 2001 From: Asher Date: Tue, 3 Nov 2020 14:31:32 -0600 Subject: [PATCH] Simplify update request --- src/node/update.ts | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/src/node/update.ts b/src/node/update.ts index 42baa1848..2959b874c 100644 --- a/src/node/update.ts +++ b/src/node/update.ts @@ -105,24 +105,23 @@ export class UpdateProvider { logger.debug("Making request", field("uri", uri)) const httpx = uri.startsWith("https") ? https : http const client = httpx.get(uri, { headers: { "User-Agent": "code-server" } }, (response) => { - if ( - response.statusCode && - response.statusCode >= 300 && - response.statusCode < 400 && - response.headers.location - ) { + if (!response.statusCode || response.statusCode < 200 || response.statusCode >= 400) { + return reject(new Error(`${uri}: ${response.statusCode || "500"}`)) + } + + if (response.statusCode >= 300) { ++redirects if (redirects > maxRedirects) { + response.destroy() return reject(new Error("reached max redirects")) } + if (!response.headers.location) { + return reject(new Error("received redirect with no location header")) + } response.destroy() return request(url.resolve(uri, response.headers.location)) } - if (!response.statusCode || response.statusCode < 200 || response.statusCode >= 400) { - return reject(new Error(`${uri}: ${response.statusCode || "500"}`)) - } - resolve(response) }) client.on("error", reject)