forked from github/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharchived-enterprise-versions-assets.js
54 lines (45 loc) · 2.17 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
const path = require('path')
const versionSatisfiesRange = require('../lib/version-satisfies-range')
const enterpriseServerReleases = require('../lib/enterprise-server-releases')
const patterns = require('../lib/patterns')
const firstVersionDeprecatedOnNewSite = '2.13'
const got = require('got')
// This module handles requests for the CSS and JS assets for
// deprecated GitHub Enterprise versions by routing them to static content in
// https://github.com/github/help-docs-archived-enterprise-versions
//
// See also ./archived-enterprise-versions.js for non-CSS/JS paths
module.exports = async (req, res, next) => {
// Only match asset paths
if (!patterns.assetPaths.test(req.path)) return next()
// Get the referrer, which may contain an enterprise version
const referrer = req.get('referrer')
// ignore paths that don't have an enterprise version number
if (!patterns.getEnterpriseVersionNumber.test(referrer)) return next()
// extract enterprise version from path, e.g. 2.16
const requestedVersion = referrer.match(patterns.getEnterpriseVersionNumber)[1]
// bail if the request version is not deprecated
if (!enterpriseServerReleases.deprecated.includes(requestedVersion)) return next()
// get /dist/index.js and /dist/index.css paths from enterprisified paths
const assetPath = req.path.replace(`/enterprise/${requestedVersion}`, '')
// paths are slightly different depending on the enterprise version
let proxyPath
if (versionSatisfiesRange(requestedVersion, `>=${firstVersionDeprecatedOnNewSite}`)) {
// routing for >=2.13
proxyPath = path.join('/', requestedVersion, assetPath)
} else if (versionSatisfiesRange(requestedVersion, `<${firstVersionDeprecatedOnNewSite}`)) {
// routing for <2.13
proxyPath = path.join('/', requestedVersion, 'assets', 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', 'none')
res.send(r.body)
} catch (err) {
next()
}
}