Move legacy services from server.js into services/ (#1958)

This builds on the work of #1931 by moving the legacy services into `services/`.
This commit is contained in:
Paul Melnikow
2018-08-27 13:29:54 -04:00
committed by GitHub
parent f3037c5e15
commit bedba47d77
151 changed files with 9074 additions and 6813 deletions

View File

@@ -0,0 +1,42 @@
'use strict'
const LegacyService = require('../legacy-service')
const { makeBadgeData: getBadgeData } = require('../../lib/badge-data')
const { addv: versionText } = require('../../lib/text-formatters')
const { version: versionColor } = require('../../lib/color-formatters')
const { latest: latestVersion } = require('../../lib/version')
// For Dart's pub.
module.exports = class Pub extends LegacyService {
static registerLegacyRouteHandler({ camp, cache }) {
camp.route(
/^\/pub\/v(pre)?\/(.*)\.(svg|png|gif|jpg|json)$/,
cache((data, match, sendBadge, request) => {
const includePre = Boolean(match[1])
const userRepo = match[2] // eg, "box2d"
const format = match[3]
const apiUrl = 'https://pub.dartlang.org/packages/' + userRepo + '.json'
const badgeData = getBadgeData('pub', data)
request(apiUrl, (err, res, buffer) => {
if (err != null) {
badgeData.text[1] = 'inaccessible'
sendBadge(format, badgeData)
return
}
try {
const data = JSON.parse(buffer)
// Grab the latest stable version, or an unstable
const versions = data.versions
const version = latestVersion(versions, { pre: includePre })
badgeData.text[1] = versionText(version)
badgeData.colorscheme = versionColor(version)
sendBadge(format, badgeData)
} catch (e) {
badgeData.text[1] = 'invalid'
sendBadge(format, badgeData)
}
})
})
)
}
}