Skip to content

Commit

Permalink
feat: support GH_TOKEN auth; support rate limit retries
Browse files Browse the repository at this point in the history
  • Loading branch information
dominykas committed Aug 10, 2020
1 parent ff1e6f0 commit 865fe86
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 8 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ List the Node.js versions supported by the package/repository

This repository is managed by the [Package Maintenance Working Group](https://github.com/nodejs/package-maintenance), see [Governance](https://github.com/nodejs/package-maintenance/blob/master/Governance.md).

## Setup

No setup is required, however if you do not have a `GH_TOKEN` environment limit, you will likely hit a request rate limit on Github API, which may result in very long wait times for retries.

## Usage (command line)

```
Expand Down
28 changes: 23 additions & 5 deletions lib/loader/octokit-wrapper.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,35 @@
'use strict';

const { Octokit } = require('@octokit/rest');
const { throttling } = require('@octokit/plugin-throttling');

const Constants = require('../constants');
const Logger = require('../logger');


const internals = {
Octokit: Octokit.plugin(throttling)
};


exports.create = () => {

const octokit = new Octokit({
userAgent: Constants.userAgent
});
const octokit = new internals.Octokit({
auth: process.env.GH_TOKEN,
userAgent: Constants.userAgent,
throttle: {
onRateLimit: (retryAfter, options) => {

Logger.warn(['loader'], 'Request quota exceeded for request %s %s. Will retry in %d seconds. Have you set a GH_TOKEN in env?', options.method, options.url, retryAfter);

// @todo: onRateLimit
// @todo: auth
return true;
},
onAbuseLimit: (retryAfter, options) => {

return false;
}
}
});

return octokit;
};
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
},
"dependencies": {
"@npmcli/arborist": "0.0.15",
"@octokit/plugin-throttling": "^3.2.2",
"@octokit/rest": "^18.0.0",
"@pkgjs/nv": "0.0.3",
"debug": "^4.1.1",
Expand Down
61 changes: 58 additions & 3 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -533,10 +533,9 @@ describe('detect-node-support', () => {
content: Fs.readFileSync(Path.join(__dirname, '..', 'package.json')).toString('base64')
})
.get('/repos/pkgjs/detect-node-support/contents/.travis.yml')
.reply(500);
.reply(500, 'Simulated server error');

const err = await expect(NodeSupport.detect({ repository: 'git+https://github.com/pkgjs/detect-node-support.git' })).to.reject();
expect(err.name).to.equal('HttpError');
await expect(NodeSupport.detect({ repository: 'git+https://github.com/pkgjs/detect-node-support.git' })).to.reject('Simulated server error');
});

it('throws when repository does not have a package.json', async () => {
Expand Down Expand Up @@ -585,6 +584,62 @@ describe('detect-node-support', () => {
await expect(NodeSupport.detect({ repository: 'git+https://github.example.com/pkgjs/detect-node-support.git' }))
.to.reject('Only github.com paths supported, feel free to PR at https://github.com/pkgjs/detect-node-support');
});

it('retries when rate limited', async () => {

fixture.stubs.listRemote
.returns('9cef39d21ad229dea4b10295f55b0d9a83800b23\tHEAD\n');

Nock('https://api.github.com')
.get('/repos/pkgjs/detect-node-support/contents/package.json')
.reply(200, {
content: Fs.readFileSync(Path.join(__dirname, '..', 'package.json')).toString('base64')
})
.get('/repos/pkgjs/detect-node-support/contents/.travis.yml')
.reply(403, null, {
'x-ratelimit-limit': '60',
'x-ratelimit-remaining': '0',
'x-ratelimit-reset': `${Math.round(Date.now() / 1000) + 1}`
})
.get('/repos/pkgjs/detect-node-support/contents/.travis.yml')
.reply(200, {
content: Fs.readFileSync(Path.join(__dirname, '..', '.travis.yml')).toString('base64')
});

const result = await NodeSupport.detect({ repository: 'git+https://github.com/pkgjs/detect-node-support.git' });

expect(result).to.equal({
name: 'detect-node-support',
version: '0.0.0-development',
commit: '9cef39d21ad229dea4b10295f55b0d9a83800b23',
timestamp: 1580673602000,
travis: {
raw: ['14', '12', '10'],
resolved: {
'10': '10.20.1',
'12': '12.17.0',
'14': '14.3.0'
}
},
engines: '>=10'
});
});

it('aborts on abuse limit', async () => {

fixture.stubs.listRemote
.returns('9cef39d21ad229dea4b10295f55b0d9a83800b23\tHEAD\n');

Nock('https://api.github.com')
.get('/repos/pkgjs/detect-node-support/contents/package.json')
.reply(200, {
content: Fs.readFileSync(Path.join(__dirname, '..', 'package.json')).toString('base64')
})
.get('/repos/pkgjs/detect-node-support/contents/.travis.yml')
.reply(403, 'Abuse detected');

await expect(NodeSupport.detect({ repository: 'git+https://github.com/pkgjs/detect-node-support.git' })).to.reject(/Abuse detected/);
});
});

describe('packageName', () => {
Expand Down

0 comments on commit 865fe86

Please sign in to comment.