Skip to content

Commit

Permalink
fix: catch update check github error
Browse files Browse the repository at this point in the history
closes #798
  • Loading branch information
fent committed Jan 6, 2021
1 parent 90d2942 commit 3609aa2
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
6 changes: 5 additions & 1 deletion lib/utils.js
Expand Up @@ -141,17 +141,21 @@ 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' },
}).text().then(response => {
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;
Expand Down
20 changes: 19 additions & 1 deletion test/utils-test.js
Expand Up @@ -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());
Expand Down Expand Up @@ -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]));
});
});
});

0 comments on commit 3609aa2

Please sign in to comment.