Skip to content

Commit

Permalink
fix(services/version): prevent checkForUpdate() 5xx response codes fr…
Browse files Browse the repository at this point in the history
…om stopping connection
  • Loading branch information
crookse committed Oct 30, 2024
1 parent f0b3beb commit af6f9b2
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/services/version.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,19 @@ export const checkForUpdate = () => new Promise(async (resolve, reject) => {
const res = await fetch(UPDATE_URL)
const data = []
const extract = tar.extract()
if (res.status === 404) { return resolve({ available: false }) }

if (res.status === 404) {
return resolve({ available: false })
}

if (res.status >= 500) {
if (process.env.DEBUG) {
console.log(chalk.yellow(`Warning: The update server (${UPDATE_URL}) responded with a ${res.status} status code\n`))
}

return resolve({ available: false })
}

Readable.fromWeb(res.body).pipe(createGunzip()).pipe(extract)

extract.on('entry', (header, stream, next) => {
Expand Down
40 changes: 40 additions & 0 deletions test/services/version.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { mock, test } from 'node:test'
import * as assert from 'node:assert'
import { checkForUpdate } from '../../src/services/version.js'
import { STATUS_CODES } from 'node:http'

test('checkForUpdate() handles 404 response', async (context) => {
fetch = context.mock.fn(fetch, () => {
return {
status: 404
}
})

const actual = await checkForUpdate()
assert.deepEqual(actual, { available: false })
})

test('checkForUpdate() handles 5xx responses', async (context) => {
const statusCodes = Object
.keys(STATUS_CODES)
.filter((statusCode) => +statusCode >= 500 )
.map((statusCode) => +statusCode)

const statusContexts = statusCodes.map((statusCode) => {
return {
status: statusCode,
expected: { available: false },
}
});

for (const statusContext of statusContexts) {
fetch = context.mock.fn(fetch, () => {
return {
status: statusContext.status,
}
})

const actual = await checkForUpdate()
assert.deepEqual(actual, statusContext.expected)
}
})

0 comments on commit af6f9b2

Please sign in to comment.