This picks up @RedSparr0w's work in #1802. 1. The handler for the static badge is moved into its own service with a synchronous handler. Avoiding an async call may make the static badges slightly faster, though it may be worth profiling this if it turns out we want asynchronous static badges in the future. If it doesn't make a performance difference we could make this handler `async` like the others. 2. Most of the custom static-badge logic is in a BaseStaticBadge base class. 3. Rewrite the static Gitter badge to use BaseStaticBadge. 4. A bit of minor cleanup in related functions.
59 lines
1.8 KiB
JavaScript
59 lines
1.8 KiB
JavaScript
'use strict'
|
|
|
|
const makeBadge = require('../gh-badges/lib/make-badge')
|
|
const { makeSend } = require('../lib/result-sender')
|
|
const analytics = require('../lib/analytics')
|
|
const BaseService = require('./base')
|
|
|
|
const serverStartTime = new Date(new Date().toGMTString())
|
|
|
|
module.exports = class BaseStaticService extends BaseService {
|
|
// Note: Since this is a static service, it is not `async`.
|
|
handle(namedParams, queryParams) {
|
|
throw new Error(`Handler not implemented for ${this.constructor.name}`)
|
|
}
|
|
|
|
static register({ camp }, serviceConfig) {
|
|
camp.route(this._regex, (queryParams, match, end, ask) => {
|
|
analytics.noteRequest(queryParams, match)
|
|
|
|
if (+new Date(ask.req.headers['if-modified-since']) >= +serverStartTime) {
|
|
// Send Not Modified.
|
|
ask.res.statusCode = 304
|
|
ask.res.end()
|
|
return
|
|
}
|
|
|
|
const serviceInstance = new this({}, serviceConfig)
|
|
const namedParams = this._namedParamsForMatch(match)
|
|
let serviceData
|
|
try {
|
|
// Note: no `await`.
|
|
serviceData = serviceInstance.handle(namedParams, queryParams)
|
|
} catch (error) {
|
|
serviceData = serviceInstance._handleError(error)
|
|
}
|
|
|
|
const badgeData = this._makeBadgeData(queryParams, serviceData)
|
|
|
|
// The final capture group is the extension.
|
|
const format = match.slice(-1)[0]
|
|
badgeData.format = format
|
|
|
|
if (serviceConfig.profiling.makeBadge) {
|
|
console.time('makeBadge total')
|
|
}
|
|
const svg = makeBadge(badgeData)
|
|
if (serviceConfig.profiling.makeBadge) {
|
|
console.timeEnd('makeBadge total')
|
|
}
|
|
|
|
const cacheDuration = 3600 * 24 * 1 // 1 day.
|
|
ask.res.setHeader('Cache-Control', `max-age=${cacheDuration}`)
|
|
ask.res.setHeader('Last-Modified', serverStartTime.toGMTString())
|
|
|
|
makeSend(format, ask.res, end)(svg)
|
|
})
|
|
}
|
|
}
|