Fix NPM license badge for scoped packages (#796)

Closes #757
This commit is contained in:
Maël Nison 2017-04-19 03:42:14 +01:00 committed by Paul Melnikow
parent 1ffd5d949c
commit 5a8b565ebb

View File

@ -1673,12 +1673,24 @@ cache(function(data, match, sendBadge, request) {
})); }));
// npm license integration. // npm license integration.
camp.route(/^\/npm\/l\/(.*)\.(svg|png|gif|jpg|json)$/, camp.route(/^\/npm\/l\/(?:@([^\/]+)\/)?([^\/]+)\.(svg|png|gif|jpg|json)$/,
cache(function(data, match, sendBadge, request) { cache(function(data, match, sendBadge, request) {
var repo = encodeURIComponent(match[1]); // eg, "express" or "@user/express" const scope = match[1]; // "user" (when a scope "@user" is supplied)
var format = match[2]; const packageName = match[2]; // "express"
var apiUrl = 'http://registry.npmjs.org/' + repo + '/latest'; const format = match[3]; // "svg"
var badgeData = getBadgeData('license', data); let apiUrl;
if (scope === undefined) {
// e.g. https://registry.npmjs.org/express/latest
// Use this endpoint as an optimization. It covers the vast majority of
// these badges, and the response is smaller.
apiUrl = `https://registry.npmjs.org/${packageName}/latest`;
} else {
// e.g. https://registry.npmjs.org/@cedx%2Fgulp-david
// because https://registry.npmjs.org/@cedx%2Fgulp-david/latest does not work
const path = encodeURIComponent(`${scope}/${packageName}`);
apiUrl = `https://registry.npmjs.org/@${path}`;
}
const badgeData = getBadgeData('license', data);
request(apiUrl, { headers: { 'Accept': '*/*' } }, function(err, res, buffer) { request(apiUrl, { headers: { 'Accept': '*/*' } }, function(err, res, buffer) {
if (err != null) { if (err != null) {
badgeData.text[1] = 'inaccessible'; badgeData.text[1] = 'inaccessible';
@ -1686,8 +1698,14 @@ cache(function(data, match, sendBadge, request) {
return; return;
} }
try { try {
var data = JSON.parse(buffer); const data = JSON.parse(buffer);
var license = data.license; let license;
if (scope === undefined) {
license = data.license;
} else {
const latestVersion = data['dist-tags'].latest;
license = data.versions[latestVersion].license;
}
if (Array.isArray(license)) { if (Array.isArray(license)) {
license = license.join(', '); license = license.join(', ');
} else if (typeof license == 'object') { } else if (typeof license == 'object') {