diff --git a/libs/live-fns/npm.js b/libs/live-fns/npm.js index e740a9f..26aac18 100644 --- a/libs/live-fns/npm.js +++ b/libs/live-fns/npm.js @@ -26,7 +26,7 @@ async function d (period, args) { const stats = await r2(endpoint).json return { subject: 'downloads', - status: millify(stats.downloads) + period.replace('last-', '%2F'), + status: millify(stats.downloads) + period.replace('last-', '/'), color: 'green' } } diff --git a/libs/lru-cache-live.js b/libs/lru-cache-live.js deleted file mode 100644 index c25b4d1..0000000 --- a/libs/lru-cache-live.js +++ /dev/null @@ -1,27 +0,0 @@ -const LRU = require('lru-cache') - -const cache = LRU({ - max: 5000, - maxAge: 12e4, // 2 minutes - stale: true -}) - -function listCache (req, res) { - res.writeHead(200) - res.end(`Total ${cache.length}\n${cache.keys().join('\n')}`) -} - -function clearCache (req, res) { - const count = cache.length - const keys = cache.keys().join('\n') - cache.reset() - - res.writeHead(200) - res.end(`Cleared ${count}\n${keys}`) -} - -module.exports = { - cache, - listCache, - clearCache -} diff --git a/libs/setup-live-badge.js b/libs/setup-live-badge.js index 9c61059..87a44c4 100644 --- a/libs/setup-live-badge.js +++ b/libs/setup-live-badge.js @@ -1,5 +1,5 @@ +const badgen = require('badgen') const liveFns = require('./live-fns/_index.js') -const { cache, listCache, clearCache } = require('./lru-cache-live.js') module.exports = function (router) { Object.entries(liveFns).forEach(([name, fn]) => { @@ -10,42 +10,29 @@ module.exports = function (router) { color = 'grey' } = await fetchLiveParams(name, fn, params['*']) - res.writeHead(302, { - Location: `/badge/${subject}/${status}/${color}` + res.writeHead(200, { + 'Content-Type': 'image/svg+xml;charset=utf-8', + 'Cache-Control': 'public, max-age=60, s-maxage=60' }) - res.end() + res.end(badgen({subject, status, color})) }) }) - - router.get('/list-cache-live', listCache) - router.get('/clear-cache-live', clearCache) } async function fetchLiveParams (scope, fn, paramsPath) { - const cached = cache.get(paramsPath) - if (cached) { - return cached - } else { - const cacheKey = `#${scope} ${paramsPath}` - console.time(cacheKey) - return timeout(fn(...paramsPath.split('/')), 30000) - .then(fetched => { - // Update cache if deleted (after got stale) - cache.has(paramsPath) || cache.set(cacheKey, fetched) - return fetched - }, e => { - console.error(e) - return {} - }).then(result => { - console.timeEnd(cacheKey) - return result - }) - } + const params = paramsPath.split('/') + const logKey = `#${scope} ${paramsPath}` + return timeout(fn(...params), 30000, logKey) } -function timeout (promise, period) { - return Promise.race([ - new Promise((resolve, reject) => setTimeout(reject, period)), - promise - ]) +function timeout (task, period, logKey) { + console.time(logKey) + const timer = new Promise((resolve, reject) => setTimeout(reject, period)) + return Promise.race([task, timer]).catch(e => { + console.error(e) + return {} + }).then(result => { + console.timeEnd(logKey) + return result + }) }