When JSON responses come back, they are sometimes not in the format expected by the API. As a result we have a lot of defensive coding (expressions like `(data || {}).someProperty`) to avoid exceptions being thrown in badge processing. Often we rely on the `try` blocks that wrap so much of the badge-processing code, which catch all JavaScript exceptions and return some error result, usually **invalid**. The problem with this is that these `try` blocks catch all sorts of programmer errors too, so when we see **invalid** we don't know whether the API returned something unexpected, or we've made a mistake. We also spend a lot of time writing defensive tests around malformed responses, and creating and maintaining the defensive coding.
A better solution is to validate the API responses using declarative contracts. Here the programmer says exactly what they expect from the API. That way, if the response isn't what we expect we can just say it's an **invalid json response**. And if our code then throws an exception, well that's our mistake; when we catch that we can call it a **shields internal error**. It's also less code and less error-prone. Over time we may be confident enough in the contracts that we won't need so many tests of malformed responses. The contract doesn't need to describe the entire response, only the part that's needed. Unknown keys can simply be dropped, preventing unvalidated parts of the response from creeping into the code. Checking what's in our response before calling values on it also makes our code more secure.
I used Joi here, since we're already using it for testing. There may be another contracts library that's a better fit, though I think we could look at that later.
Those changes are in base.js.
The rest is a rewrite of the remaining NPM badges, including the extraction of an NpmBase class. Inspired by @chris48s's work in #1740, this class splits the service concerns into fetching, validation, transformation, and rendering. This is treated as a design pattern. See the PR discussion for more. There are two URL patterns, one which allows specifying a tag (used by e.g. the version badge `https://img.shields.io/npm/v/npm/next.svg`), and the other which does not accept a tag (e.g. the license badge `https://img.shields.io/npm/l/express.svg`). Subclasses like NpmLicense and NpmTypeDefinitions can specify the URL fragment, examples, the validation schema for the chunk of the package data they use, and a render function. The NpmVersion subclass uses a different endpoint, so it overrides the `handle` implementation from NpmBase.
The remaining services using BaseJsonService are shimmed, so they will keep working after the changes.
194 lines
6.6 KiB
JSON
194 lines
6.6 KiB
JSON
{
|
|
"name": "gh-badges",
|
|
"version": "1.3.0",
|
|
"description": "Official Shields.io badge library.",
|
|
"keywords": [
|
|
"GitHub",
|
|
"badge",
|
|
"SVG",
|
|
"image",
|
|
"shields.io"
|
|
],
|
|
"homepage": "http://shields.io",
|
|
"bugs": {
|
|
"url": "https://github.com/badges/shields/issues",
|
|
"email": "thaddee.tyl@gmail.com"
|
|
},
|
|
"license": "CC0-1.0",
|
|
"author": "Thaddée Tyl <thaddee.tyl@gmail.com>",
|
|
"main": "lib/make-badge.js",
|
|
"repository": {
|
|
"type": "git",
|
|
"url": "https://github.com/badges/shields"
|
|
},
|
|
"dependencies": {
|
|
"camp": "~17.2.1",
|
|
"chrome-web-store-item-property": "~1.1.2",
|
|
"dot": "~1.1.2",
|
|
"escape-string-regexp": "^1.0.5",
|
|
"glob": "^7.1.1",
|
|
"gm": "^1.23.0",
|
|
"is-css-color": "^1.0.0",
|
|
"joi": "^13.3.0",
|
|
"js-yaml": "^3.11.0",
|
|
"json-autosave": "~1.1.2",
|
|
"jsonpath": "~1.0.0",
|
|
"lodash.countby": "^4.6.0",
|
|
"lodash.mapkeys": "^4.6.0",
|
|
"lodash.throttle": "^4.1.1",
|
|
"lodash.times": "^4.3.2",
|
|
"lodash.uniq": "~4.5.0",
|
|
"moment": "^2.19.3",
|
|
"node-env-flag": "^0.1.0",
|
|
"pdfkit": "~0.8.0",
|
|
"pretty-bytes": "^5.0.0",
|
|
"priorityqueuejs": "^1.0.0",
|
|
"query-string": "^6.0.0",
|
|
"raven": "^2.4.2",
|
|
"redis": "~2.6.2",
|
|
"request": "~2.87.0",
|
|
"semver": "~5.5.0",
|
|
"simple-icons": "^1.7.1",
|
|
"svgo": "~1.0.5",
|
|
"xml2js": "~0.4.16",
|
|
"xmldom": "~0.1.27",
|
|
"xpath": "~0.0.27"
|
|
},
|
|
"scripts": {
|
|
"coverage:test:js": "nyc node_modules/mocha/bin/_mocha \"*.spec.js\" \"lib/**/*.spec.js\" \"services/**/*.spec.js\"",
|
|
"coverage:test:services": "nyc node_modules/mocha/bin/_mocha --delay lib/service-test-runner/cli.js",
|
|
"coverage:test": "rimraf .nyc_output coverage; npm run coverage:test:js; npm run coverage:test:services",
|
|
"coverage:report": "nyc report",
|
|
"coverage:report:reopen": "opn coverage/lcov-report/index.html",
|
|
"coverage:report:open": "npm run coverage:report && npm run coverage:report:reopen",
|
|
"lint": "eslint \"**/*.js\"",
|
|
"danger": "danger",
|
|
"test:js:frontend": "NODE_ENV=mocha mocha --require babel-polyfill --require babel-register \"frontend/**/*.spec.js\"",
|
|
"test:js:server": "HANDLE_INTERNAL_ERRORS=false mocha \"*.spec.js\" \"lib/**/*.spec.js\" \"services/**/*.spec.js\"",
|
|
"test:integration": "mocha \"services/**/*.integration.js\"",
|
|
"test:services": "HANDLE_INTERNAL_ERRORS=false mocha --delay lib/service-test-runner/cli.js",
|
|
"test:services:pr:prepare": "node lib/service-test-runner/pull-request-services-cli.js > pull-request-services.log",
|
|
"test:services:pr:run": "HANDLE_INTERNAL_ERRORS=false mocha --delay lib/service-test-runner/cli.js --stdin < pull-request-services.log",
|
|
"test:services:pr": "npm run test:services:pr:prepare && npm run test:services:pr:run",
|
|
"test": "npm run lint && npm run test:js:frontend && npm run test:js:server",
|
|
"circle-images:build": "docker build -t shieldsio/shields-ci-node-8:${IMAGE_TAG} -f .circleci/images/node-8/Dockerfile . && docker build -t shieldsio/shields-ci-node-latest:${IMAGE_TAG} -f .circleci/images/node-latest/Dockerfile .",
|
|
"circle-images:push": "docker push shieldsio/shields-ci-node-8:${IMAGE_TAG} && docker push shieldsio/shields-ci-node-latest:${IMAGE_TAG}",
|
|
"depcheck": "check-node-version --node \">= 8.0\"",
|
|
"postinstall": "npm run depcheck",
|
|
"prebuild": "npm run depcheck",
|
|
"features": "node lib/export-supported-features-cli.js > supported-features.json",
|
|
"examples": "node lib/export-badge-examples-cli.js > badge-examples.json",
|
|
"build": "npm run examples && npm run features && next build && next export -o build/",
|
|
"heroku-postbuild": "npm run build",
|
|
"analyze": "ANALYZE=true LONG_CACHE=false BASE_URL=https://img.shields.io npm run build",
|
|
"start:server": "HANDLE_INTERNAL_ERRORS=false RATE_LIMIT=false node server 8080 ::",
|
|
"now-start": "node server",
|
|
"prestart": "npm run depcheck && npm run examples && npm run features",
|
|
"start": "concurrently --names server,frontend \"ALLOWED_ORIGIN=http://localhost:3000 npm run start:server\" \"BASE_URL=http://[::]:8080 next dev\""
|
|
},
|
|
"bin": {
|
|
"badge": "lib/badge-cli.js"
|
|
},
|
|
"files": [
|
|
"README.md",
|
|
"lib/badge-cli.js",
|
|
"lib/make-badge.js",
|
|
"lib/colorscheme.json",
|
|
"lib/lru-cache.js",
|
|
"lib/text-measurer.js",
|
|
"lib/svg-to-img.js",
|
|
"templates",
|
|
"logo"
|
|
],
|
|
"devDependencies": {
|
|
"@mapbox/react-click-to-select": "^2.2.0",
|
|
"almost-equal": "^1.1.0",
|
|
"babel-eslint": "^8.0.2",
|
|
"babel-polyfill": "^6.26.0",
|
|
"babel-preset-env": "^1.7.0",
|
|
"chai": "^4.1.2",
|
|
"chai-as-promised": "^7.1.1",
|
|
"chai-string": "^1.4.0",
|
|
"chainsmoker": "^0.1.0",
|
|
"check-node-version": "^3.1.0",
|
|
"child-process-promise": "^2.2.1",
|
|
"classnames": "^2.2.5",
|
|
"concurrently": "^3.5.1",
|
|
"danger": "^3.7.13",
|
|
"dejavu-fonts-ttf": "^2.37.3",
|
|
"eslint": "^5.0.1",
|
|
"eslint-config-prettier": "^2.6.0",
|
|
"eslint-config-standard": "^12.0.0-alpha.0",
|
|
"eslint-config-standard-jsx": "^5.0.0",
|
|
"eslint-config-standard-react": "^6.0.0",
|
|
"eslint-plugin-chai-friendly": "^0.4.1",
|
|
"eslint-plugin-import": "^2.8.0",
|
|
"eslint-plugin-node": "^7.0.0",
|
|
"eslint-plugin-prettier": "^2.3.1",
|
|
"eslint-plugin-promise": "^3.6.0",
|
|
"eslint-plugin-react": "^7.6.1",
|
|
"eslint-plugin-standard": "^3.0.1",
|
|
"fetch-ponyfill": "^6.0.0",
|
|
"icedfrisby": "2.0.0-alpha.2",
|
|
"icedfrisby-nock": "^1.0.0",
|
|
"is-png": "^1.1.0",
|
|
"is-svg": "^3.0.0",
|
|
"lodash.debounce": "^4.0.8",
|
|
"lodash.difference": "^4.5.0",
|
|
"lodash.mapvalues": "^4.6.0",
|
|
"minimist": "^1.2.0",
|
|
"mkdirp": "^0.5.1",
|
|
"mocha": "^5.0.0",
|
|
"next": "^5.0.0",
|
|
"nock": "^9.3.2",
|
|
"node-fetch": "^2.0.0",
|
|
"nyc": "^12.0.1",
|
|
"opn-cli": "^3.1.0",
|
|
"prettier": "1.14.0",
|
|
"pretty": "^2.0.0",
|
|
"prop-types": "^15.6.0",
|
|
"react": "^16.4.2",
|
|
"react-dom": "^16.4.2",
|
|
"react-modal": "^3.5.1",
|
|
"react-router-dom": "^4.3.1",
|
|
"read-all-stdin-sync": "^1.0.5",
|
|
"rimraf": "^2.6.2",
|
|
"sazerac": "^0.4.2",
|
|
"semver-regex": "^2.0.0",
|
|
"sinon": "^6.0.0",
|
|
"sinon-chai": "^3.0.0",
|
|
"snap-shot-it": "^5.0.1",
|
|
"url": "^0.11.0"
|
|
},
|
|
"greenkeeper": {
|
|
"ignore": [
|
|
"joi",
|
|
"redis"
|
|
]
|
|
},
|
|
"engines": {
|
|
"node": ">= 8.x",
|
|
"npm": "5.x"
|
|
},
|
|
"babel": {
|
|
"presets": [
|
|
"next/babel"
|
|
],
|
|
"plugins": [
|
|
"transform-class-properties"
|
|
],
|
|
"env": {
|
|
"mocha": {
|
|
"presets": [
|
|
"env"
|
|
]
|
|
}
|
|
}
|
|
},
|
|
"collective": {
|
|
"type": "opencollective",
|
|
"url": "https://opencollective.com/shields",
|
|
"logo": "https://opencollective.com/opencollective/logo.txt"
|
|
}
|
|
}
|