forked from github/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
archived-enterprise-versions-assets.js
39 lines (33 loc) · 1.36 KB
/
archived-enterprise-versions-assets.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import path from 'path'
import patterns from '../lib/patterns.js'
import isArchivedVersion from '../lib/is-archived-version.js'
import got from '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
export default 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(404)
}
}