{"size": 1504, "ext": "js", "max_stars_repo_path": "services/base-static.js", "max_stars_repo_name": "Antoine38660/shields", "max_stars_repo_stars_event_min_datetime": "2018-12-16T22:12:07.000Z", "max_stars_repo_stars_event_max_datetime": "2018-12-16T22:12:37.000Z", "max_issues_repo_path": "services/base-static.js", "max_issues_repo_name": "Antoine38660/shields", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "services/base-static.js", "max_forks_repo_name": "Antoine38660/shields", "max_forks_repo_forks_event_min_datetime": "2019-11-01T20:27:21.000Z", "max_forks_repo_forks_event_max_datetime": "2019-11-01T20:27:21.000Z"}
{"max_stars_count": 2, "max_issues_count": null, "max_forks_count": 1, "avg_line_length": 27.3454545455, "max_line_length": 69, "alphanum_fraction": 0.6542553191}
'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 {
  serverHasBeenUpSinceResourceCached,
  setCacheHeadersForStaticResource,
} = require('./cache-headers')

module.exports = class BaseStaticService extends BaseService {
  static register({ camp }, serviceConfig) {
    const {
      profiling: { makeBadge: shouldProfileMakeBadge },
    } = serviceConfig

    camp.route(this._regex, async (queryParams, match, end, ask) => {
      analytics.noteRequest(queryParams, match)

      if (serverHasBeenUpSinceResourceCached(ask.req)) {
        // Send Not Modified.
        ask.res.statusCode = 304
        ask.res.end()
        return
      }

      const namedParams = this._namedParamsForMatch(match)
      const serviceData = await this.invoke(
        {},
        serviceConfig,
        namedParams,
        queryParams
      )

      const badgeData = this._makeBadgeData(queryParams, serviceData)
      // The final capture group is the extension.
      const format = match.slice(-1)[0]
      badgeData.format = format

      if (shouldProfileMakeBadge) {
        console.time('makeBadge total')
      }
      const svg = makeBadge(badgeData)
      if (shouldProfileMakeBadge) {
        console.timeEnd('makeBadge total')
      }

      setCacheHeadersForStaticResource(ask.res)

      makeSend(format, ask.res, end)(svg)
    })
  }
}
