-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move [StaticBadge] to own service & add test; also affects [gitter] (#…
…2284) 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.
- Loading branch information
1 parent
921adc9
commit 065dd57
Showing
8 changed files
with
203 additions
and
124 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
'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) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,39 @@ | ||
'use strict' | ||
|
||
const LegacyService = require('../legacy-service') | ||
const { makeBadgeData: getBadgeData } = require('../../lib/badge-data') | ||
const BaseStaticService = require('../base-static') | ||
|
||
module.exports = class Gitter extends LegacyService { | ||
module.exports = class Gitter extends BaseStaticService { | ||
static get category() { | ||
return 'chat' | ||
} | ||
|
||
static get route() { | ||
return { base: 'gitter/room' } | ||
return { | ||
base: 'gitter/room', | ||
pattern: ':user/:repo', | ||
} | ||
} | ||
|
||
static get examples() { | ||
return [ | ||
{ | ||
title: 'Gitter', | ||
previewUrl: 'nwjs/nw.js', | ||
urlPattern: ':user/:repo', | ||
staticExample: this.render(), | ||
exampleUrl: 'nwjs/nw.js', | ||
}, | ||
] | ||
} | ||
|
||
static registerLegacyRouteHandler({ camp, cache }) { | ||
camp.route( | ||
/^\/gitter\/room\/([^/]+\/[^/]+)\.(svg|png|gif|jpg|json)$/, | ||
cache((data, match, sendBadge, request) => { | ||
// match[1] is the repo, which is not used. | ||
const format = match[2] | ||
static get defaultBadgeData() { | ||
return { label: 'chat' } | ||
} | ||
|
||
static render() { | ||
return { message: 'on gitter', color: 'brightgreen' } | ||
} | ||
|
||
const badgeData = getBadgeData('chat', data) | ||
badgeData.text[1] = 'on gitter' | ||
badgeData.colorscheme = 'brightgreen' | ||
sendBadge(format, badgeData) | ||
}) | ||
) | ||
handle() { | ||
return this.constructor.render() | ||
} | ||
} |
Oops, something went wrong.