Simplify update request

This commit is contained in:
Asher 2020-11-03 14:31:32 -06:00
parent 3a074fd844
commit f4e5855318
No known key found for this signature in database
GPG Key ID: D63C1EF81242354A
1 changed files with 9 additions and 10 deletions

View File

@ -105,24 +105,23 @@ export class UpdateProvider {
logger.debug("Making request", field("uri", uri)) logger.debug("Making request", field("uri", uri))
const httpx = uri.startsWith("https") ? https : http const httpx = uri.startsWith("https") ? https : http
const client = httpx.get(uri, { headers: { "User-Agent": "code-server" } }, (response) => { const client = httpx.get(uri, { headers: { "User-Agent": "code-server" } }, (response) => {
if ( if (!response.statusCode || response.statusCode < 200 || response.statusCode >= 400) {
response.statusCode && return reject(new Error(`${uri}: ${response.statusCode || "500"}`))
response.statusCode >= 300 && }
response.statusCode < 400 &&
response.headers.location if (response.statusCode >= 300) {
) {
++redirects ++redirects
if (redirects > maxRedirects) { if (redirects > maxRedirects) {
response.destroy()
return reject(new Error("reached max redirects")) return reject(new Error("reached max redirects"))
} }
if (!response.headers.location) {
return reject(new Error("received redirect with no location header"))
}
response.destroy() response.destroy()
return request(url.resolve(uri, response.headers.location)) 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) resolve(response)
}) })
client.on("error", reject) client.on("error", reject)