1
0
mirror of synced 2025-12-22 03:16:52 -05:00
Files
docs/middleware/archived-enterprise-versions-assets.js
James M. Greene c940dcd98b Middleware overhaul! (#18218)
* Middleware overhaul!

- Remove unnecessary 'async' keywords from middleware functions
- Ensure all middleware functions we create have names
- Wrap the method contents of all async middleware functions in a try-catch+next(error) pattern

* Use asyncMiddleware wrapper instead of try-catch+next(error) pattern

* Remove unnecessary try-catch+next(error) pattern from context middleware
2021-03-11 01:12:41 +00:00

38 lines
1.4 KiB
JavaScript

const path = require('path')
const patterns = require('../lib/patterns')
const isArchivedVersion = require('../lib/is-archived-version')
const got = require('got')
const ONE_DAY = 24 * 60 * 60 // 1 day in seconds
// This module handles requests for the CSS and JS assets for
// deprecated GitHub Enterprise versions by routing them to static content in
// help-docs-archived-enterprise-versions
//
// See also ./archived-enterprise-versions.js for non-CSS/JS paths
module.exports = async function archivedEnterpriseVersionsAssets (req, res, next) {
const { isArchived, requestedVersion } = isArchivedVersion(req)
if (!isArchived) return next()
// Only match asset paths
if (!patterns.assetPaths.test(req.path)) return next()
const assetPath = req.path.replace(`/enterprise/${requestedVersion}`, '')
const proxyPath = path.join('/', requestedVersion, assetPath)
try {
const r = await got(`https://github.github.com/help-docs-archived-enterprise-versions${proxyPath}`)
res.set('accept-ranges', 'bytes')
res.set('content-type', r.headers['content-type'])
res.set('content-length', r.headers['content-length'])
res.set('x-is-archived', 'true')
res.set('x-robots-tag', 'noindex')
// Allow the browser and Fastly to cache these
res.set('cache-control', `public, max-age=${ONE_DAY}`)
return res.send(r.body)
} catch (err) {
return next()
}
}