diff --git a/lib/utils.js b/lib/utils.js index 3d2ae843..b1cc349a 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -141,10 +141,11 @@ exports.deprecate = (obj, prop, value, oldPath, newPath) => { // Check for updates. const pkg = require('../package.json'); +const UPDATE_INTERVAL = 1000 * 60 * 60 * 12; exports.lastUpdateCheck = 0; exports.checkForUpdates = () => { if (!process.env.YTDL_NO_UPDATE && !pkg.version.startsWith('0.0.0-') && - Date.now() - exports.lastUpdateCheck >= 1000 * 60 * 60 * 12) { + Date.now() - exports.lastUpdateCheck >= UPDATE_INTERVAL) { exports.lastUpdateCheck = Date.now(); return miniget('https://api.github.com/repos/fent/node-ytdl-core/releases/latest', { headers: { 'User-Agent': 'ytdl-core' }, @@ -152,6 +153,9 @@ exports.checkForUpdates = () => { if (JSON.parse(response).tag_name !== `v${pkg.version}`) { console.warn('\x1b[33mWARNING:\x1B[0m ytdl-core is out of date! Update with "npm install ytdl-core@latest".'); } + }, err => { + console.warn('Error checking for updates:', err.message); + console.warn('You can disable this check by setting the `YTDL_NO_UPDATE` env variable.'); }); } return null; diff --git a/test/utils-test.js b/test/utils-test.js index 70cc5537..14b7e6d7 100644 --- a/test/utils-test.js +++ b/test/utils-test.js @@ -99,7 +99,7 @@ describe('utils.parseAbbreviatedNumber', () => { describe('utils.checkForUpdates', () => { - before(() => delete process.env.YTDL_NO_UPDATE); + beforeEach(() => delete process.env.YTDL_NO_UPDATE); after(() => process.env.YTDL_NO_UPDATE = 'true'); beforeEach(() => utils.lastUpdateCheck = Date.now()); afterEach(() => sinon.restore()); @@ -155,4 +155,22 @@ describe('utils.checkForUpdates', () => { assert.ok(warnSpy.notCalled); }); }); + + describe('When there is an error checking for updates', () => { + it('Warns the console', async() => { + const pkg = require('../package.json'); + sinon.replace(pkg, 'version', 'v1.0.0'); + const scope = nock('https://api.github.com') + .get('/repos/fent/node-ytdl-core/releases/latest') + .reply(403, 'slow down there'); + const warnSpy = sinon.spy(); + sinon.replace(console, 'warn', warnSpy); + sinon.replace(Date, 'now', sinon.stub().returns(Infinity)); + await utils.checkForUpdates(); + scope.done(); + assert.ok(warnSpy.called); + assert.ok(/Error checking for updates/.test(warnSpy.firstCall.args[0])); + assert.ok(/Status code: 403/.test(warnSpy.firstCall.args[1])); + }); + }); });