diff --git a/README.md b/README.md index 8e02944..1c172a0 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Badgen Service -The repo for https://badgen.now.sh, a fast badge generating service. +Fast badge generating service. ## Usage @@ -34,7 +34,7 @@ Available color names: |![](https://badgen.now.sh/badge/chat/gitter/cyan) | [https://badgen.now.sh/badge/chat/gitter/purple](https://badgen.now.sh/badge/chat/gitter/purple) |![](https://badgen.now.sh/badge/style/standard/f2a) | [https://badgen.now.sh/badge/style/standard/f2a](https://badgen.now.sh/badge/style/standard/f2a) |![](https://badgen.now.sh/badge/license/Apache-2.0/blue) | [https://badgen.now.sh/badge/license/Apache-2.0/blue](https://badgen.now.sh/badge/license/Apache-2.0/blue) -|![](https://badgen.now.sh/list/platform/ios,macos,tvos/36E) | [https://badgen.now.sh/list/platform/ios,macos,tvos/36E](https://badgen.now.sh/list/platform/ios,macos,tvos/36E) +|![](https://badgen.now.sh/badge/Language/Swift%203.0.1/orange) | [https://badgen.now.sh/badge/Language/Swift%203.0.1/orange](https://badgen.now.sh/badge/Language/Swift%203.0.1/orange) #### Live Badge @@ -42,9 +42,9 @@ For full list of live badges, see https://badgen.now.sh ## Developing -- `npm run start` or better if you have nodemon: `nodemon service.js` +- `npm start` or better if you have nodemon: `nodemon service.js` ## About Made with ❤️ by [Amio](https://github.com/amio), -built with ⚡️ from [badgen](https://github.com/amio/badgen) +built with ⚡️ from [badgen](https://github.com/amio/badgen). diff --git a/libs/index.md b/libs/index.md index fd07e91..03eaaf1 100644 --- a/libs/index.md +++ b/libs/index.md @@ -1,6 +1,6 @@ # Badgen -Fast badge generating service. Built with [badgen](https://github.com/amio/badgen). +Fast badge generating service. ## Usage @@ -48,6 +48,9 @@ Available color names: | npm downloads/day | ![](/npm/dd/express) | [/npm/dd/express](/npm/dd/express) | npm downloads/week | ![](/npm/dw/express) | [/npm/dw/express](/npm/dw/express) | npm downloads/month | ![](/npm/dm/express) | [/npm/dm/express](/npm/dm/express) +| npm downloads/year | ![](/npm/dy/express) | [/npm/dy/express](/npm/dy/express) +| npm license | ![](/npm/license/lodash) | [/npm/license/lodash](/npm/license/lodash) +| npm node | ![](/npm/node/express) | [/npm/node/express](/npm/node/express) | crates.io version | ![](/crates/v/regex) | [/crates/v/regex](/crates/v/regex) | crates.io downloads | ![](/crates/d/regex) | [/crates/d/regex](/crates/d/regex) | crates.io downloads/latest | ![](/crates/dl/regex) | [/crates/dl/regex](/crates/dl/regex) diff --git a/libs/live-fns/chrome-web-store.js b/libs/live-fns/chrome-web-store.js index 34586f8..0651509 100644 --- a/libs/live-fns/chrome-web-store.js +++ b/libs/live-fns/chrome-web-store.js @@ -22,8 +22,8 @@ module.exports = async function (method, ...args) { case 'v': return { subject: 'chrome web store', - status: meta.version, - color: 'blue' + status: 'v' + meta.version, + color: meta.version[0] === '0' ? 'orange' : 'blue' } case 'users': return { @@ -57,7 +57,7 @@ module.exports = async function (method, ...args) { } default: return { - subject: 'chrome', + subject: 'chrome web store', status: 'unknown', color: 'grey' } diff --git a/libs/live-fns/npm.js b/libs/live-fns/npm.js index a7ba942..92b238c 100644 --- a/libs/live-fns/npm.js +++ b/libs/live-fns/npm.js @@ -1,16 +1,25 @@ const axios = require('../axios.js') const millify = require('millify') +// https://github.com/npm/registry/blob/master/docs/REGISTRY-API.md +// https://unpkg.com/ + module.exports = async function (method, ...args) { switch (method) { case 'v': - return v(args) + return version(args) case 'dd': - return d('last-day', args) + return download('last-day', args) case 'dw': - return d('last-week', args) + return download('last-week', args) case 'dm': - return d('last-month', args) + return download('last-month', args) + case 'dy': + return download('last-year', args) + case 'license': + return pkg('license', args) + case 'node': + return pkg('node', args) default: return { subject: 'npm', @@ -20,8 +29,27 @@ module.exports = async function (method, ...args) { } } -// npm download -async function d (period, args) { +async function pkg (topic, args) { + const endpoint = `https://unpkg.com/${args.join('/')}/package.json` + const meta = await axios.get(endpoint).then(res => res.data) + + switch (topic) { + case 'license': + return { + subject: 'license', + status: meta.license || 'unknown', + color: 'blue' + } + case 'node': + return { + subject: 'node', + status: (meta.engines && meta.engines.node) || '*', + color: 'green' + } + } +} + +async function download (period, args) { const endpoint = `https://api.npmjs.org/downloads/point/${period}/${args.join('/')}` const stats = await axios.get(endpoint).then(res => res.data) return { @@ -31,9 +59,16 @@ async function d (period, args) { } } -// npm version -async function v (args) { - const version = await fetchLatestVersion(args) +async function version (args) { + // Due to an bug of npm registry api, scoped package need to be handled + // separately: https://github.com/npm/registry/issues/34 + // A workaround is using version range("*" for "latest") by Andrew Goode: + // https://github.com/npm/registry/issues/34#issuecomment-228349870 + const scoped = args.length === 2 && args[0][0] === '@' + const endpoint = scoped + ? `https://registry.npmjs.org/${args.join('%2F')}/*` + : `https://registry.npmjs.org/${args}/latest` + const { version } = await axios.get(endpoint).then(res => res.data) return { subject: 'npm', @@ -41,18 +76,3 @@ async function v (args) { color: version.split('.')[0] === '0' ? 'orange' : 'blue' } } - -async function fetchLatestVersion (args) { - const scoped = args.length === 2 && args[0][0] === '@' - let endpoint - // Due to an bug of npm registry api, scoped package need to be handled - // separately: https://github.com/npm/registry/issues/34 - // A workaround is using version range("*" for "latest") by Andrew Goode: - // https://github.com/npm/registry/issues/34#issuecomment-228349870 - if (scoped) { - endpoint = `https://registry.npmjs.org/${args.join('%2F')}/*` - } else { - endpoint = `https://registry.npmjs.org/${args}/latest` - } - return (await axios.get(endpoint).then(res => res.data)).version -} diff --git a/libs/setup-live-badge.js b/libs/setup-live-badge.js index 64f4b58..caf03c5 100644 --- a/libs/setup-live-badge.js +++ b/libs/setup-live-badge.js @@ -8,13 +8,14 @@ module.exports = function (router) { const { subject = name, status = 'unknown', - color = 'grey' + color = 'grey', + fail = false } = await fetchLiveParams(name, fn, params['*']) - const sharedMaxAge = (Math.random() * 60 + 60).toFixed() + const sharedMaxAge = fail ? '0' : (Math.random() * 60 + 60).toFixed() res.writeHead(200, { 'Content-Type': 'image/svg+xml;charset=utf-8', - 'Cache-Control': 'public, max-age=60, s-maxage=' + sharedMaxAge + 'Cache-Control': 'public, max-age=20, s-maxage=' + sharedMaxAge }) res.end(badgen({subject, status, color})) }) @@ -26,11 +27,9 @@ async function fetchLiveParams (scope, fn, paramsPath) { if (waitings[fetchKey]) return waitings[fetchKey] console.time(fetchKey) - const task = fn(...paramsPath.split('/')) - const timer = new Promise((resolve, reject) => setTimeout(reject, 30000)) - waitings[fetchKey] = Promise.race([task, timer]).catch(e => { - console.error(e) - return {} + waitings[fetchKey] = fn(...paramsPath.split('/')).catch(e => { + console.error(fetchKey, e) + return { fail: true } }).then(result => { console.timeEnd(fetchKey) waitings[fetchKey] = undefined