From 9d019aef086e7fce355c2be0dd1140bc6ed71f62 Mon Sep 17 00:00:00 2001 From: chris48s Date: Sun, 29 Apr 2018 20:05:33 +0100 Subject: [PATCH 1/2] pass error object to InvaildResponse() this prevents us from throwing TypeError: Cannot read property 'stack' of undefined when we attempt to parse invalid json --- lib/error-helper.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/error-helper.js b/lib/error-helper.js index a12ea3130981a..8778e6fefc47b 100644 --- a/lib/error-helper.js +++ b/lib/error-helper.js @@ -39,7 +39,7 @@ async function asJson({ buffer, res }) { try { return JSON.parse(buffer); } catch (err) { - throw new InvalidResponse(); + throw new InvalidResponse(undefined, err); } }; From fa9e4ed3b799fd8bf39f626a73aebcb48ec7a43c Mon Sep 17 00:00:00 2001 From: chris48s Date: Sun, 29 Apr 2018 20:10:01 +0100 Subject: [PATCH 2/2] refactor [cdnjs] integration --- lib/all-badge-examples.js | 8 ----- server.js | 33 --------------------- services/cdnjs/cdnjs.js | 61 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 61 insertions(+), 41 deletions(-) create mode 100644 services/cdnjs/cdnjs.js diff --git a/lib/all-badge-examples.js b/lib/all-badge-examples.js index 20ef83b2b7c40..4c25835336512 100644 --- a/lib/all-badge-examples.js +++ b/lib/all-badge-examples.js @@ -739,14 +739,6 @@ const allBadgeExamples = [ name: 'Version' }, examples: [ - { - title: 'CDNJS', - previewUri: '/cdnjs/v/jquery.svg', - keywords: [ - 'cdn', - 'cdnjs' - ] - }, { title: 'npm', previewUri: '/npm/v/npm.svg', diff --git a/server.js b/server.js index 1f827c0be13b7..c9e6de111c83b 100644 --- a/server.js +++ b/server.js @@ -1609,39 +1609,6 @@ cache(function(data, match, sendBadge, request) { }); })); -// CDNJS version integration -camp.route(/^\/cdnjs\/v\/(.*)\.(svg|png|gif|jpg|json)$/, -cache(function(data, match, sendBadge, request) { - const library = encodeURIComponent(match[1]); // eg, "express" or "@user/express" - const format = match[2]; - const apiUrl = 'https://api.cdnjs.com/libraries/' + library + '?fields=version'; - const badgeData = getBadgeData('cdnjs', data); - request(apiUrl, function(err, res, buffer) { - if (err != null) { - badgeData.text[1] = 'inaccessible'; - sendBadge(format, badgeData); - return; - } - try { - const json = JSON.parse(buffer); - if (Object.keys(json).length === 0) { - /* Note the 'not found' response from cdnjs is: - status code = 200, body = {} */ - badgeData.text[1] = 'not found'; - sendBadge(format, badgeData); - return; - } - const version = json.version || 0; - badgeData.text[1] = versionText(version); - badgeData.colorscheme = versionColor(version); - sendBadge(format, badgeData); - } catch(e) { - badgeData.text[1] = 'invalid'; - sendBadge(format, badgeData); - } - }); -})); - // npm weekly download integration. mapNpmDownloads({ camp, cache }, 'dw', 'last-week'); diff --git a/services/cdnjs/cdnjs.js b/services/cdnjs/cdnjs.js new file mode 100644 index 0000000000000..e49afc4cc6ba9 --- /dev/null +++ b/services/cdnjs/cdnjs.js @@ -0,0 +1,61 @@ +'use strict'; + +const BaseService = require('../base'); +const { + checkErrorResponse, + asJson, +} = require('../../lib/error-helper'); +const { NotFound } = require('../errors'); +const { addv: versionText } = require('../../lib/text-formatters'); +const { version: versionColor} = require('../../lib/color-formatters'); + +module.exports = class Cdnjs extends BaseService { + async handle({library}) { + const apiUrl = 'https://api.cdnjs.com/libraries/' + library + '?fields=version'; + const json = await this._sendAndCacheRequest(apiUrl, { + headers: { 'Accept': 'application/json' } + }).then(checkErrorResponse.asPromise()) + .then(asJson); + + if (Object.keys(json).length === 0) { + /* Note the 'not found' response from cdnjs is: + status code = 200, body = {} */ + throw new NotFound(); + } + const version = json.version || 0; + + return { + message: versionText(version), + color: versionColor(version) + }; + } + + // Metadata + static get defaultBadgeData() { + return { label: 'cdnjs' }; + } + + static get category() { + return 'version'; + } + + static get url() { + return { + base: 'cdnjs/v', + format: '(.*)', + capture: ['library'] + }; + } + + static get examples() { + return [ + { + previewUrl: 'jquery', + keywords: [ + 'cdn', + 'cdnjs' + ] + } + ]; + } +};