kopia lustrzana https://github.com/badgen/badgen.net
Add lru cache for live badges
rodzic
d1ec796212
commit
d23d89a432
|
|
@ -1,6 +1,6 @@
|
|||
const r2 = require('r2')
|
||||
|
||||
module.exports = async function (args) {
|
||||
module.exports = async function (...args) {
|
||||
const version = await fetchVersion(args.join('%2F'), args[0][0] === '@')
|
||||
|
||||
return {
|
||||
|
|
@ -18,6 +18,7 @@ async function fetchVersion (pkg, scoped) {
|
|||
const fullMeta = (await r2(endpoint).json)
|
||||
return fullMeta.versions[fullMeta['dist-tags']['latest']].version
|
||||
} else {
|
||||
// a smaller response for just latest version
|
||||
const endpointLatest = `https://registry.npmjs.org/${pkg}/latest`
|
||||
return (await r2(endpointLatest).json).version
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,14 +1,20 @@
|
|||
const LRU = require('lru-cache')
|
||||
const liveFns = require('./live-fns/index.js')
|
||||
|
||||
const cache = LRU({
|
||||
max: 5000,
|
||||
maxAge: 3e4,
|
||||
stale: true
|
||||
})
|
||||
|
||||
module.exports = function (router) {
|
||||
Object.entries(liveFns).forEach(([key, fn]) => {
|
||||
router.get(`/${key}/*`, async (req, res, params) => {
|
||||
// todo: cache
|
||||
const {
|
||||
subject = key,
|
||||
status = 'unknown',
|
||||
color = 'grey'
|
||||
} = await timeout(fn(params['*'].split('/')), 30000).catch(e => ({}))
|
||||
} = await fetchLiveParams(key, params['*'], fn)
|
||||
|
||||
res.writeHead(302, {
|
||||
Location: `https://badgen.now.sh/badge/${subject}/${status}/${color}`
|
||||
|
|
@ -16,6 +22,42 @@ module.exports = function (router) {
|
|||
res.end()
|
||||
})
|
||||
})
|
||||
|
||||
router.get('/list-cache-live', (req, res) => {
|
||||
res.writeHead(200)
|
||||
res.end(`Total ${cache.length}\n${cache.keys().join('\n')}`)
|
||||
})
|
||||
|
||||
router.get('/clear-cache-live', (req, res) => {
|
||||
const count = cache.length
|
||||
const keys = cache.keys().join('\n')
|
||||
cache.reset()
|
||||
|
||||
res.writeHead(200)
|
||||
res.end(`Cleaned ${count}\n${keys}`)
|
||||
})
|
||||
}
|
||||
|
||||
async function fetchLiveParams (key, paramsPath, fn) {
|
||||
const cached = cache.get(paramsPath)
|
||||
if (cached) {
|
||||
return cached
|
||||
} else {
|
||||
const logStamp = `$${key} ${paramsPath}`
|
||||
console.time(logStamp)
|
||||
return timeout(fn(...paramsPath.split('/')), 30000)
|
||||
.then(fetched => {
|
||||
// Update cache if deleted (after got stale)
|
||||
cache.has(paramsPath) || cache.set(paramsPath, fetched)
|
||||
return fetched
|
||||
}, e => {
|
||||
console.error(e)
|
||||
return {}
|
||||
}).then(result => {
|
||||
console.timeEnd(logStamp)
|
||||
return result
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
function timeout (promise, period) {
|
||||
|
|
|
|||
Ładowanie…
Reference in New Issue