diff --git a/.eslintrc b/.eslintrc index eb72040..304a9d6 100644 --- a/.eslintrc +++ b/.eslintrc @@ -1,5 +1,5 @@ { - "extends": "@hapi/hapi", + "extends": "plugin:@hapi/recommended", "parserOptions": { "ecmaVersion": 2019 diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml new file mode 100644 index 0000000..76adba4 --- /dev/null +++ b/.github/workflows/ci.yaml @@ -0,0 +1,19 @@ +name: Node.js CI + +on: + push: + branches: + - main + pull_request: + +jobs: + + test: + uses: pkgjs/action/.github/workflows/node-test.yaml@v0 + with: + post-checkout-steps: | + - name: Set git user to avoid warnings + shell: bash + run: | + git config --global user.name github-actions + git config --global user.email github-actions@github.com diff --git a/.travis.yml b/.travis.yml index 932aa54..f8d8247 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,14 +1,12 @@ language: node_js -sudo: false -node_js: - - "10" - - "12" - - "14" +import: + - nodejs/ci-config-travis:lts/gte-14.yml cache: npm: false install: + - "npm install -g npm" - "npm install --ignore-scripts" - "npx allow-scripts" diff --git a/CODE_OF_CONDUCT.md b/CODE_OF_CONDUCT.md new file mode 100644 index 0000000..eb07a97 --- /dev/null +++ b/CODE_OF_CONDUCT.md @@ -0,0 +1,4 @@ +# Code of Conduct + +The Node.js Code of Conduct, which applies to this project, can be found at +https://github.com/nodejs/admin/blob/master/CODE_OF_CONDUCT.md. diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100644 index 0000000..97035e0 --- /dev/null +++ b/CONTRIBUTING.md @@ -0,0 +1,36 @@ +# Contributing + +## Code of Conduct + +The Node.js project has a +[Code of Conduct](https://github.com/nodejs/admin/blob/master/CODE_OF_CONDUCT.md) +to which all contributors must adhere. + +See [details on our policy on Code of Conduct](https://github.com/nodejs/node/blob/master/doc/guides/contributing/code-of-conduct.md). + + +## Developer's Certificate of Origin 1.1 + +By making a contribution to this project, I certify that: + +* (a) The contribution was created in whole or in part by me and I + have the right to submit it under the open source license + indicated in the file; or + +* (b) The contribution is based upon previous work that, to the best + of my knowledge, is covered under an appropriate open source + license and I have the right under that license to submit that + work with modifications, whether created in whole or in part + by me, under the same open source license (unless I am + permitted to submit under a different license), as indicated + in the file; or + +* (c) The contribution was provided directly to me by some other + person who certified (a), (b) or (c) and I have not modified + it. + +* (d) I understand and agree that this project and the contribution + are public and that a record of the contribution (including all + personal information I submit with it, including my sign-off) is + maintained indefinitely and may be redistributed consistent with + this project or the open source license(s) involved. diff --git a/LICENSE b/LICENSE index e621b78..8c456b7 100644 --- a/LICENSE +++ b/LICENSE @@ -1,21 +1,10 @@ MIT License -Copyright Node.js contributors. All rights reserved. +Copyright (c) 2020 Contributors https://github.com/pkgjs/detect-node-support/graphs/contributors -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. diff --git a/README.md b/README.md index aefb45f..b9203af 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,12 @@ 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) ``` diff --git a/bin/detect-node-support b/bin/detect-node-support index c8908ec..a1f01af 100755 --- a/bin/detect-node-support +++ b/bin/detect-node-support @@ -29,7 +29,7 @@ Options: `; }; -exports.main = async ({ _: [what], deps, deep, dev, json }) => { +exports.main = async ({ _: [what], help, deps, deep, dev, json }) => { const enabledLogs = ['detect-node-support:warn:*', 'detect-node-support:error:*']; @@ -39,11 +39,15 @@ exports.main = async ({ _: [what], deps, deep, dev, json }) => { Debug.enable(enabledLogs.join(',')); - if (!what) { + if (help) { console.log(internals.help()); return; } + if (!what) { + what = process.cwd(); + } + if (!deps && (deep || dev)) { console.log('--deep and --dev can only be used together with --deps\n'); console.log(internals.help()); diff --git a/lib/deps.js b/lib/deps.js index 41245f9..3c6e6b3 100644 --- a/lib/deps.js +++ b/lib/deps.js @@ -31,12 +31,17 @@ internals.resolve = async ({ packageJson, lockfile }, options) => { for (const dep of arborist.idealTree.inventory.values()) { + if (dep.isProjectRoot) { + // root node is not a dep, really + continue; + } + if (!options.dev && dep.dev) { // only include dev deps when `options.dev` flag is set continue; } - if (!options.deep && ![...dep.edgesIn].some(({ from }) => from === arborist.idealTree)) { + if (!options.deep && ![...dep.edgesIn].some(({ from }) => from.isProjectRoot)) { continue; } diff --git a/lib/loader/octokit-wrapper.js b/lib/loader/octokit-wrapper.js new file mode 100644 index 0000000..f52be68 --- /dev/null +++ b/lib/loader/octokit-wrapper.js @@ -0,0 +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 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); + + return true; + }, + onAbuseLimit: (retryAfter, options) => { + + return false; + } + } + }); + + return octokit; +}; diff --git a/lib/loader/repository.js b/lib/loader/repository.js index a1aca16..02113d4 100644 --- a/lib/loader/repository.js +++ b/lib/loader/repository.js @@ -1,9 +1,9 @@ 'use strict'; const GitUrlParse = require('git-url-parse'); -const Wreck = require('@hapi/wreck'); const Logger = require('../logger'); +const OctokitWrapper = require('./octokit-wrapper'); const Utils = require('../utils'); @@ -30,40 +30,54 @@ exports.create = (repository) => { return head; }, - loadFile: async (filename, options) => { + loadFile: async (filename, options = {}) => { if (parsedRepository.source !== 'github.com') { throw new Error('Only github.com paths supported, feel free to PR at https://github.com/pkgjs/detect-node-support'); } - const url = `https://raw.githubusercontent.com/${parsedRepository.full_name}/HEAD/${filename}`; - Logger.log(['loader'], 'Loading: %s', url); + const resource = `${parsedRepository.full_name}:${filename}@HEAD`; + Logger.log(['loader'], 'Loading: %s', resource); - if (options === undefined && internals.cache.has(url)) { - Logger.log(['loader'], 'From cache: %s', url); - return internals.cache.get(url); - } + const octokit = OctokitWrapper.create(); try { - const { payload } = await Wreck.get(url, options); - if (options === undefined) { - internals.cache.set(url, payload); + let result; + if (internals.cache.has(resource)) { + Logger.log(['loader'], 'From cache: %s', resource); + result = internals.cache.get(resource); + } + else { + result = await octokit.repos.getContent({ + owner: parsedRepository.owner, + repo: parsedRepository.name, + path: filename + }); + } + + internals.cache.set(resource, result); + + Logger.log(['loader'], 'Loaded: %s', resource); + + const content = Buffer.from(result.data.content, 'base64'); + + if (options.json) { + return JSON.parse(content.toString()); } - Logger.log(['loader'], 'Loaded: %s', url); - return payload; + return content; } catch (err) { - if (err.data && err.data.res.statusCode === 404) { - Logger.log(['loader'], 'Not found: %s', url); + if (err.status === 404) { + Logger.log(['loader'], 'Not found: %s', resource); const error = new Error(`${repository} does not contain a ${filename}`); error.code = 'ENOENT'; throw error; } - Logger.error(['loader'], 'Failed to load: %s', url); + Logger.error(['loader'], 'Failed to load: %s', resource); throw err; } } diff --git a/lib/package.js b/lib/package.js index 9c88080..8077699 100644 --- a/lib/package.js +++ b/lib/package.js @@ -45,7 +45,7 @@ exports.detect = async (what) => { const { loadFile, getCommit } = await Loader.create({ path, repository, packageName }); - const packageJson = await loadFile('package.json', { json: 'force' }); + const packageJson = await loadFile('package.json', { json: true }); const meta = { packageJson, diff --git a/lib/travis/imports.js b/lib/travis/imports.js index 3ba3e7f..5fd35a2 100644 --- a/lib/travis/imports.js +++ b/lib/travis/imports.js @@ -88,7 +88,7 @@ exports.apply = async (yaml, { loadFile, relativeTo, breadcrumb = ['.travis.yml' const buffer = await internals.loadSource(entry.source, { loadFile }); - const imported = Yaml.safeLoad(buffer, { + const imported = Yaml.load(buffer, { schema: Yaml.FAILSAFE_SCHEMA, json: true }); diff --git a/lib/travis/index.js b/lib/travis/index.js index 9d0e3d9..e3f7b9f 100644 --- a/lib/travis/index.js +++ b/lib/travis/index.js @@ -86,7 +86,7 @@ exports.detect = async ({ loadFile }) => { throw err; } - const travisYaml = Yaml.safeLoad(buffer, { + const travisYaml = Yaml.load(buffer, { schema: Yaml.FAILSAFE_SCHEMA, json: true }); diff --git a/lib/utils.js b/lib/utils.js index 63fcb68..3f68c61 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -1,6 +1,6 @@ 'use strict'; -const SimpleGit = require('simple-git/promise'); +const SimpleGit = require('simple-git'); /* $lab:coverage:off$ */ // this is wrapped primarily to be able to stub it diff --git a/package.json b/package.json index 0b38781..3beeb9c 100644 --- a/package.json +++ b/package.json @@ -24,26 +24,27 @@ "url": "https://github.com/pkgjs/detect-node-support/issues" }, "engines": { - "node": ">=10" + "node": ">=14" }, "homepage": "https://github.com/pkgjs/detect-node-support#readme", "devDependencies": { "@hapi/code": "^8.0.0", - "@hapi/lab": "^22.0.0", + "@hapi/lab": "^24.0.0", "allow-scripts": "^1.5.2", - "nock": "^12.0.1", - "sinon": "^9.0.0" + "nock": "^13.0.0", + "sinon": "^13.0.0" }, "dependencies": { - "@hapi/wreck": "^17.0.0", - "@npmcli/arborist": "0.0.0-pre.21", - "@pkgjs/nv": "0.0.3", + "@npmcli/arborist": "^4.0.0", + "@octokit/plugin-throttling": "^3.2.2", + "@octokit/rest": "^18.0.0", + "@pkgjs/nv": "0.2.1", "debug": "^4.1.1", "git-url-parse": "^11.1.2", - "js-yaml": "^3.13.1", + "js-yaml": "^4.0.0", "minimist": "^1.2.5", - "pacote": "^11.1.0", - "simple-git": "2.5.0", + "pacote": "^12.0.0", + "simple-git": "^3.0.0", "tmp": "^0.2.0" } } diff --git a/release.config.js b/release.config.js new file mode 100644 index 0000000..f521e36 --- /dev/null +++ b/release.config.js @@ -0,0 +1,8 @@ +'use strict'; + +module.exports = { + branches: [ + 'main', + { name: 'alpha', prerelease: true } + ] +}; diff --git a/renovate.json b/renovate.json new file mode 100644 index 0000000..6d81213 --- /dev/null +++ b/renovate.json @@ -0,0 +1,7 @@ +{ + "extends": [ + "config:base", + ":preserveSemverRanges", + ":disableDependencyDashboard" + ] +} diff --git a/test/fixtures/index.js b/test/fixtures/index.js index b64d2ce..f1e65f3 100644 --- a/test/fixtures/index.js +++ b/test/fixtures/index.js @@ -3,7 +3,7 @@ const Fs = require('fs'); const Nock = require('nock'); const Path = require('path'); -const SimpleGit = require('simple-git/promise'); +const SimpleGit = require('simple-git'); const Sinon = require('sinon'); const Tmp = require('tmp'); @@ -70,6 +70,14 @@ module.exports = class TestContext { .get('/dist/index.json') .reply(200, Fs.readFileSync(Path.join(__dirname, 'node-release-dist.json'))); + Nock('https://api.github.com') + .persist() + .get('/repos/nodejs/ci-config-travis/contents/lts%2Fgte-14.yml') + .reply(200, { + // note: this is mocked out to an earlier version of node, to avoid having to update too many assertions/fixtures + content: Fs.readFileSync(Path.join(__dirname, 'travis-ymls', 'nodejs-ci-config-travis-gte-10.yml')).toString('base64') + }); + this._cleanup.push(() => { Nock.restore(); diff --git a/test/fixtures/travis-ymls/nodejs-ci-config-travis-gte-10.yml b/test/fixtures/travis-ymls/nodejs-ci-config-travis-gte-10.yml new file mode 100644 index 0000000..b5fd230 --- /dev/null +++ b/test/fixtures/travis-ymls/nodejs-ci-config-travis-gte-10.yml @@ -0,0 +1,4 @@ +node_js: + - 14 + - 12 + - 10 diff --git a/test/index.js b/test/index.js index ced0d70..8f49ac5 100644 --- a/test/index.js +++ b/test/index.js @@ -4,10 +4,10 @@ const Fs = require('fs'); const Nock = require('nock'); const Path = require('path'); const Sinon = require('sinon'); -const Wreck = require('@hapi/wreck'); const NodeSupport = require('..'); +const OctokitWrapper = require('../lib/loader/octokit-wrapper'); const TestContext = require('./fixtures'); @@ -55,14 +55,14 @@ describe('detect-node-support', () => { version: '0.0.0-development', timestamp: 1580673602000, travis: { - raw: ['10', '12', '14'], + raw: ['14', '12', '10'], resolved: { '10': '10.20.1', '12': '12.17.0', '14': '14.3.0' } }, - engines: '>=10' + engines: '>=14' }); }); @@ -426,11 +426,15 @@ describe('detect-node-support', () => { fixture.stubs.listRemote .returns('9cef39d21ad229dea4b10295f55b0d9a83800b23\tHEAD\n'); - Nock('https://raw.githubusercontent.com') - .get('/pkgjs/detect-node-support/HEAD/package.json') - .reply(200, Fs.readFileSync(Path.join(__dirname, '..', 'package.json'))) - .get('/pkgjs/detect-node-support/HEAD/.travis.yml') - .reply(200, Fs.readFileSync(Path.join(__dirname, '..', '.travis.yml'))); + 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(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' }); @@ -443,14 +447,14 @@ describe('detect-node-support', () => { commit: '9cef39d21ad229dea4b10295f55b0d9a83800b23', timestamp: 1580673602000, travis: { - raw: ['10', '12', '14'], + raw: ['14', '12', '10'], resolved: { '10': '10.20.1', '12': '12.17.0', '14': '14.3.0' } }, - engines: '>=10' + engines: '>=14' }); }); @@ -459,11 +463,15 @@ describe('detect-node-support', () => { fixture.stubs.listRemote .returns('9cef39d21ad229dea4b10295f55b0d9a83800b23\tHEAD\n'); - Nock('https://raw.githubusercontent.com') - .get('/pkgjs/detect-node-support/HEAD/package.json') - .reply(200, Fs.readFileSync(Path.join(__dirname, '..', 'package.json'))) - .get('/pkgjs/detect-node-support/HEAD/.travis.yml') - .reply(200, Fs.readFileSync(Path.join(__dirname, '..', '.travis.yml'))); + 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(200, { + content: Fs.readFileSync(Path.join(__dirname, '..', '.travis.yml')).toString('base64') + }); const result = await NodeSupport.detect({ repository: 'pkgjs/detect-node-support' }); @@ -476,14 +484,14 @@ describe('detect-node-support', () => { commit: '9cef39d21ad229dea4b10295f55b0d9a83800b23', timestamp: 1580673602000, travis: { - raw: ['10', '12', '14'], + raw: ['14', '12', '10'], resolved: { '10': '10.20.1', '12': '12.17.0', '14': '14.3.0' } }, - engines: '>=10' + engines: '>=14' }); }); @@ -492,10 +500,12 @@ describe('detect-node-support', () => { fixture.stubs.listRemote .returns('9cef39d21ad229dea4b10295f55b0d9a83800b23\tHEAD\n'); - Nock('https://raw.githubusercontent.com') - .get('/pkgjs/detect-node-support/HEAD/package.json') - .reply(200, Fs.readFileSync(Path.join(__dirname, '..', 'package.json'))) - .get('/pkgjs/detect-node-support/HEAD/.travis.yml') + 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(404); const result = await NodeSupport.detect({ repository: 'git+https://github.com/pkgjs/detect-node-support.git' }); @@ -508,7 +518,7 @@ describe('detect-node-support', () => { version: '0.0.0-development', commit: '9cef39d21ad229dea4b10295f55b0d9a83800b23', timestamp: 1580673602000, - engines: '>=10' + engines: '>=14' }); }); @@ -517,14 +527,15 @@ describe('detect-node-support', () => { fixture.stubs.listRemote .returns('9cef39d21ad229dea4b10295f55b0d9a83800b23\tHEAD\n'); - Nock('https://raw.githubusercontent.com') - .get('/pkgjs/detect-node-support/HEAD/package.json') - .reply(200, Fs.readFileSync(Path.join(__dirname, '..', 'package.json'))) - .get('/pkgjs/detect-node-support/HEAD/.travis.yml') - .reply(500); + 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(500, 'Simulated server error'); - await expect(NodeSupport.detect({ repository: 'git+https://github.com/pkgjs/detect-node-support.git' })) - .to.reject('Response Error: 500 null'); // the null is a Nock/Wreck implementation detail + 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 () => { @@ -532,11 +543,13 @@ describe('detect-node-support', () => { fixture.stubs.listRemote .returns('9cef39d21ad229dea4b10295f55b0d9a83800b23\tHEAD\n'); - Nock('https://raw.githubusercontent.com') - .get('/pkgjs/detect-node-support/HEAD/package.json') + Nock('https://api.github.com') + .get('/repos/pkgjs/detect-node-support/contents/package.json') .reply(404) - .get('/pkgjs/detect-node-support/HEAD/.travis.yml') - .reply(200, Fs.readFileSync(Path.join(__dirname, '..', '.travis.yml'))); + .get('/repos/pkgjs/detect-node-support/contents/.travis.yml') + .reply(200, { + content: Fs.readFileSync(Path.join(__dirname, '..', '.travis.yml')).toString('base64') + }); await expect(NodeSupport.detect({ repository: 'git+https://github.com/pkgjs/detect-node-support.git' })) .to.reject(`git+https://github.com/pkgjs/detect-node-support.git does not contain a package.json`); @@ -544,21 +557,23 @@ describe('detect-node-support', () => { it('rethrows server errors', async () => { - Nock('https://raw.githubusercontent.com') - .get('/pkgjs/detect-node-support/HEAD/package.json') + Nock('https://api.github.com') + .get('/repos/pkgjs/detect-node-support/contents/package.json') .reply(500) - .get('/pkgjs/detect-node-support/HEAD/.travis.yml') - .reply(200, Fs.readFileSync(Path.join(__dirname, '..', '.travis.yml'))); + .get('/repos/pkgjs/detect-node-support/contents/.travis.yml') + .reply(200, { + content: Fs.readFileSync(Path.join(__dirname, '..', '.travis.yml')).toString('base64') + }); - await expect(NodeSupport.detect({ repository: 'git+https://github.com/pkgjs/detect-node-support.git' })) - .to.reject(/Response 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'); }); it('rethrows generic errors', async () => { const err = new Error('Something went wrong'); - Sinon.stub(Wreck, 'get').throws(err); + Sinon.stub(OctokitWrapper, 'create').throws(err); await expect(NodeSupport.detect({ repository: 'git+https://github.com/pkgjs/detect-node-support.git' })) .to.reject('Something went wrong'); @@ -569,6 +584,63 @@ 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, '', { + '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: '>=14' + }); + }); + + 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') + }) + // https://docs.github.com/en/rest/overview/resources-in-the-rest-api#secondary-rate-limits + .get('/repos/pkgjs/detect-node-support/contents/.travis.yml') + .reply(403, 'You have exceeded a secondary rate limit'); + + await expect(NodeSupport.detect({ repository: 'git+https://github.com/pkgjs/detect-node-support.git' })).to.reject(/You have exceeded a secondary rate limit/); + }); }); describe('packageName', () => { @@ -578,11 +650,19 @@ describe('detect-node-support', () => { fixture.stubs.listRemote .returns('9cef39d21ad229dea4b10295f55b0d9a83800b23\tHEAD\n'); - Nock('https://raw.githubusercontent.com') - .get('/pkgjs/detect-node-support/HEAD/package.json') - .reply(200, Fs.readFileSync(Path.join(__dirname, '..', 'package.json'))) - .get('/pkgjs/detect-node-support/HEAD/.travis.yml') - .reply(200, Fs.readFileSync(Path.join(__dirname, '..', '.travis.yml'))); + Nock('https://api.github.com', { + reqheaders: { + 'user-agent': /detect-node-support\/.* \(https:\/\/github.com\/pkgjs\/detect-node-support#readme\)/ + } + }) + .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(200, { + content: Fs.readFileSync(Path.join(__dirname, '..', '.travis.yml')).toString('base64') + }); Nock('https://registry.npmjs.org') .get('/detect-node-support') @@ -599,14 +679,14 @@ describe('detect-node-support', () => { commit: '9cef39d21ad229dea4b10295f55b0d9a83800b23', timestamp: 1580673602000, travis: { - raw: ['10', '12', '14'], + raw: ['14', '12', '10'], resolved: { '10': '10.20.1', '12': '12.17.0', '14': '14.3.0' } }, - engines: '>=10' + engines: '>=14' }); }); @@ -615,10 +695,12 @@ describe('detect-node-support', () => { fixture.stubs.listRemote .returns('9cef39d21ad229dea4b10295f55b0d9a83800b23\tHEAD\n'); - Nock('https://raw.githubusercontent.com') - .get('/pkgjs/detect-node-support/HEAD/package.json') - .reply(200, Fs.readFileSync(Path.join(__dirname, '..', 'package.json'))) - .get('/pkgjs/detect-node-support/HEAD/.travis.yml') + 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(404); Nock('https://registry.npmjs.org') @@ -635,7 +717,7 @@ describe('detect-node-support', () => { version: '0.0.0-development', commit: '9cef39d21ad229dea4b10295f55b0d9a83800b23', timestamp: 1580673602000, - engines: '>=10' + engines: '>=14' }); }); @@ -667,7 +749,7 @@ describe('detect-node-support', () => { const err = new Error('Something went wrong'); - Sinon.stub(Wreck, 'get').throws(err); + Sinon.stub(OctokitWrapper, 'create').throws(err); await expect(NodeSupport.detect({ packageName: 'detect-node-support' })) .to.reject('Something went wrong'); @@ -698,11 +780,15 @@ describe('detect-node-support', () => { fixture.stubs.listRemote .returns('9cef39d21ad229dea4b10295f55b0d9a83800b23\tHEAD\n'); - Nock('https://raw.githubusercontent.com') - .get('/pkgjs/detect-node-support/HEAD/package.json') - .reply(200, Fs.readFileSync(Path.join(__dirname, '..', 'package.json'))) - .get('/pkgjs/detect-node-support/HEAD/.travis.yml') - .reply(200, Fs.readFileSync(Path.join(__dirname, '..', '.travis.yml'))); + 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(200, { + content: Fs.readFileSync(Path.join(__dirname, '..', '.travis.yml')).toString('base64') + }); Nock('https://registry.npmjs.org') .get('/detect-node-support') @@ -722,14 +808,14 @@ describe('detect-node-support', () => { commit: '9cef39d21ad229dea4b10295f55b0d9a83800b23', timestamp: 1580673602000, travis: { - raw: ['10', '12', '14'], + raw: ['14', '12', '10'], resolved: { '10': '10.20.1', '12': '12.17.0', '14': '14.3.0' } }, - engines: '>=10' + engines: '>=14' }); }); @@ -738,11 +824,15 @@ describe('detect-node-support', () => { fixture.stubs.listRemote .returns('9cef39d21ad229dea4b10295f55b0d9a83800b23\tHEAD\n'); - Nock('https://raw.githubusercontent.com') - .get('/pkgjs/detect-node-support/HEAD/package.json') - .reply(200, JSON.stringify({ name: 'something-else' })) - .get('/pkgjs/detect-node-support/HEAD/.travis.yml') - .reply(200, Fs.readFileSync(Path.join(__dirname, '..', '.travis.yml'))); + Nock('https://api.github.com') + .get('/repos/pkgjs/detect-node-support/contents/package.json') + .reply(200, { + content: Buffer.from(JSON.stringify({ name: 'something-else' })).toString('base64') + }) + .get('/repos/pkgjs/detect-node-support/contents/.travis.yml') + .reply(200, { + content: Fs.readFileSync(Path.join(__dirname, '..', '.travis.yml')).toString('base64') + }); Nock('https://registry.npmjs.org') .get('/detect-node-support') @@ -768,14 +858,14 @@ describe('detect-node-support', () => { version: '0.0.0-development', timestamp: 1580673602000, travis: { - raw: ['10', '12', '14'], + raw: ['14', '12', '10'], resolved: { '10': '10.20.1', '12': '12.17.0', '14': '14.3.0' } }, - engines: '>=10' + engines: '>=14' }); }); @@ -784,11 +874,15 @@ describe('detect-node-support', () => { fixture.stubs.listRemote .returns('9cef39d21ad229dea4b10295f55b0d9a83800b23\tHEAD\n'); - Nock('https://raw.githubusercontent.com') - .get('/pkgjs/detect-node-support/HEAD/package.json') - .reply(200, Fs.readFileSync(Path.join(__dirname, '..', 'package.json'))) - .get('/pkgjs/detect-node-support/HEAD/.travis.yml') - .reply(200, Fs.readFileSync(Path.join(__dirname, '..', '.travis.yml'))); + 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(200, { + content: Fs.readFileSync(Path.join(__dirname, '..', '.travis.yml')).toString('base64') + }); const result = await NodeSupport.detect('git+https://github.com/pkgjs/detect-node-support.git'); @@ -801,14 +895,14 @@ describe('detect-node-support', () => { commit: '9cef39d21ad229dea4b10295f55b0d9a83800b23', timestamp: 1580673602000, travis: { - raw: ['10', '12', '14'], + raw: ['14', '12', '10'], resolved: { '10': '10.20.1', '12': '12.17.0', '14': '14.3.0' } }, - engines: '>=10' + engines: '>=14' }); }); @@ -817,11 +911,15 @@ describe('detect-node-support', () => { fixture.stubs.listRemote .returns('9cef39d21ad229dea4b10295f55b0d9a83800b23\tHEAD\n'); - Nock('https://raw.githubusercontent.com') - .get('/pkgjs/detect-node-support/HEAD/package.json') - .reply(200, Fs.readFileSync(Path.join(__dirname, '..', 'package.json'))) - .get('/pkgjs/detect-node-support/HEAD/.travis.yml') - .reply(200, Fs.readFileSync(Path.join(__dirname, '..', '.travis.yml'))); + 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(200, { + content: Fs.readFileSync(Path.join(__dirname, '..', '.travis.yml')).toString('base64') + }); const result = await NodeSupport.detect('pkgjs/detect-node-support'); @@ -834,14 +932,14 @@ describe('detect-node-support', () => { commit: '9cef39d21ad229dea4b10295f55b0d9a83800b23', timestamp: 1580673602000, travis: { - raw: ['10', '12', '14'], + raw: ['14', '12', '10'], resolved: { '10': '10.20.1', '12': '12.17.0', '14': '14.3.0' } }, - engines: '>=10' + engines: '>=14' }); }); @@ -850,11 +948,15 @@ describe('detect-node-support', () => { fixture.stubs.listRemote .returns('9cef39d21ad229dea4b10295f55b0d9a83800b23\tHEAD\n'); - Nock('https://raw.githubusercontent.com') - .get('/pkgjs/detect-node-support/HEAD/package.json') - .reply(200, Fs.readFileSync(Path.join(__dirname, '..', 'package.json'))) - .get('/pkgjs/detect-node-support/HEAD/.travis.yml') - .reply(200, Fs.readFileSync(Path.join(__dirname, '..', '.travis.yml'))); + 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(200, { + content: Fs.readFileSync(Path.join(__dirname, '..', '.travis.yml')).toString('base64') + }); Nock('https://registry.npmjs.org') .get('/detect-node-support') @@ -871,14 +973,14 @@ describe('detect-node-support', () => { commit: '9cef39d21ad229dea4b10295f55b0d9a83800b23', timestamp: 1580673602000, travis: { - raw: ['10', '12', '14'], + raw: ['14', '12', '10'], resolved: { '10': '10.20.1', '12': '12.17.0', '14': '14.3.0' } }, - engines: '>=10' + engines: '>=14' }); }); @@ -887,11 +989,15 @@ describe('detect-node-support', () => { fixture.stubs.listRemote .returns('9cef39d21ad229dea4b10295f55b0d9a83800b23\tHEAD\n'); - Nock('https://raw.githubusercontent.com') - .get('/hapijs/hapi/HEAD/package.json') - .reply(200, Fs.readFileSync(Path.join(__dirname, 'fixtures', 'hapi-package.json'))) - .get('/hapijs/hapi/HEAD/.travis.yml') - .reply(200, Fs.readFileSync(Path.join(__dirname, '..', '.travis.yml'))); + Nock('https://api.github.com') + .get('/repos/hapijs/hapi/contents/package.json') + .reply(200, { + content: Fs.readFileSync(Path.join(__dirname, 'fixtures', 'hapi-package.json')).toString('base64') + }) + .get('/repos/hapijs/hapi/contents/.travis.yml') + .reply(200, { + content: Fs.readFileSync(Path.join(__dirname, '..', '.travis.yml')).toString('base64') + }); Nock('https://registry.npmjs.org') .get('/@hapi%2fhapi') @@ -908,7 +1014,7 @@ describe('detect-node-support', () => { commit: '9cef39d21ad229dea4b10295f55b0d9a83800b23', timestamp: 1580673602000, travis: { - raw: ['10', '12', '14'], + raw: ['14', '12', '10'], resolved: { '10': '10.20.1', '12': '12.17.0', @@ -939,24 +1045,36 @@ describe('detect-node-support', () => { .get('/rimraf') .reply(200, Fs.readFileSync(Path.join(__dirname, 'fixtures', 'packuments', 'rimraf.json'))); - Nock('https://raw.githubusercontent.com') - .get('/watson/is-ci/HEAD/package.json') - .reply(200, JSON.stringify({ name: 'is-ci', version: '2.0.0' })) - .get('/watson/is-ci/HEAD/.travis.yml') - .reply(200, Fs.readFileSync(Path.join(__dirname, 'fixtures', 'travis-ymls', 'testing-single-version.yml'))) - .get('/watson/ci-info/HEAD/package.json') - .reply(200, JSON.stringify({ name: 'ci-info', version: '2.0.0' })) - .get('/watson/ci-info/HEAD/.travis.yml') - .reply(200, Fs.readFileSync(Path.join(__dirname, 'fixtures', 'travis-ymls', 'testing-single-version.yml'))) - .get('/visionmedia/debug/HEAD/package.json') - .reply(200, JSON.stringify({ name: 'debug', version: '4.1.1' })) - .get('/visionmedia/debug/HEAD/.travis.yml') + Nock('https://api.github.com') + .get('/repos/watson/is-ci/contents/package.json') + .reply(200, { + content: Buffer.from(JSON.stringify({ name: 'is-ci', version: '2.0.0' })).toString('base64') + }) + .get('/repos/watson/is-ci/contents/.travis.yml') + .reply(200, { + content: Fs.readFileSync(Path.join(__dirname, 'fixtures', 'travis-ymls', 'testing-single-version.yml')).toString('base64') + }) + .get('/repos/watson/ci-info/contents/package.json') + .reply(200, { + content: Buffer.from(JSON.stringify({ name: 'ci-info', version: '2.0.0' })).toString('base64') + }) + .get('/repos/watson/ci-info/contents/.travis.yml') + .reply(200, { + content: Fs.readFileSync(Path.join(__dirname, 'fixtures', 'travis-ymls', 'testing-single-version.yml')).toString('base64') + }) + .get('/repos/visionmedia/debug/contents/package.json') + .reply(200, { + content: Buffer.from(JSON.stringify({ name: 'debug', version: '4.1.1' })).toString('base64') + }) + .get('/repos/visionmedia/debug/contents/.travis.yml') .reply(404) - .get('/zeit/ms/HEAD/package.json') - .reply(200, JSON.stringify({ name: 'ms', version: '2.1.2' })) - .get('/zeit/ms/HEAD/.travis.yml') + .get('/repos/zeit/ms/contents/package.json') + .reply(200, { + content: Buffer.from(JSON.stringify({ name: 'ms', version: '2.1.2' })).toString('base64') + }) + .get('/repos/zeit/ms/contents/.travis.yml') .reply(404) - .get('/isaacs/rimraf/HEAD/package.json') + .get('/repos/isaacs/rimraf/contents/package.json') .reply(404); }); diff --git a/test/travis.js b/test/travis.js index 36f01dc..1f4b2ab 100644 --- a/test/travis.js +++ b/test/travis.js @@ -132,11 +132,15 @@ describe('.travis.yml parsing', () => { it('resolves from another repo', async () => { - Nock('https://raw.githubusercontent.com') - .get('/pkgjs/detect-node-support/HEAD/test/fixtures/travis-ymls/testing-imports/partials/indirect-node-14.yml') - .reply(200, Fs.readFileSync(Path.join(__dirname, 'fixtures', 'travis-ymls', 'testing-imports', 'partials', 'indirect-node-14.yml'))) - .get('/pkgjs/detect-node-support/HEAD/test/fixtures/travis-ymls/testing-imports/partials/node-14.yml') - .reply(200, Fs.readFileSync(Path.join(__dirname, 'fixtures', 'travis-ymls', 'testing-imports', 'partials', 'node-14.yml'))); + Nock('https://api.github.com') + .get(`/repos/pkgjs/detect-node-support/contents/${encodeURIComponent('test/fixtures/travis-ymls/testing-imports/partials/indirect-node-14.yml')}`) + .reply(200, { + content: Fs.readFileSync(Path.join(__dirname, 'fixtures', 'travis-ymls', 'testing-imports', 'partials', 'indirect-node-14.yml')).toString('base64') + }) + .get(`/repos/pkgjs/detect-node-support/contents/${encodeURIComponent('test/fixtures/travis-ymls/testing-imports/partials/node-14.yml')}`) + .reply(200, { + content: Fs.readFileSync(Path.join(__dirname, 'fixtures', 'travis-ymls', 'testing-imports', 'partials', 'node-14.yml')).toString('base64') + }); await fixture.setupRepoFolder({ partials: true,