Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

archived-enterprise-versions.js 4.2 KB

You have to be logged in to leave a comment. Sign In
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
  1. const path = require('path')
  2. const slash = require('slash')
  3. const { firstVersionDeprecatedOnNewSite, lastVersionWithoutArchivedRedirectsFile } = require('../lib/enterprise-server-releases')
  4. const patterns = require('../lib/patterns')
  5. const versionSatisfiesRange = require('../lib/version-satisfies-range')
  6. const isArchivedVersion = require('../lib/is-archived-version')
  7. const got = require('got')
  8. const archvivedRedirects = require('../lib/redirects/static/archived-redirects-from-213-to-217')
  9. const archivedFrontmatterFallbacks = require('../lib/redirects/static/archived-frontmatter-fallbacks')
  10. // This module handles requests for deprecated GitHub Enterprise versions
  11. // by routing them to static content in help-docs-archived-enterprise-versions
  12. module.exports = async function archivedEnterpriseVersions (req, res, next) {
  13. const { isArchived, requestedVersion } = isArchivedVersion(req)
  14. if (!isArchived) return next()
  15. // Skip asset paths
  16. if (patterns.assetPaths.test(req.path)) return next()
  17. // redirect language-prefixed URLs like /en/enterprise/2.10 -> /enterprise/2.10
  18. // (this only applies to versions <2.13)
  19. if (req.path.startsWith('/en/') && versionSatisfiesRange(requestedVersion, `<${firstVersionDeprecatedOnNewSite}`)) {
  20. return res.redirect(301, req.baseUrl + req.path.replace(/^\/en/, ''))
  21. }
  22. // find redirects for versions between 2.13 and 2.17
  23. // starting with 2.18, we updated the archival script to create a redirects.json file
  24. if (versionSatisfiesRange(requestedVersion, `>=${firstVersionDeprecatedOnNewSite}`) &&
  25. versionSatisfiesRange(requestedVersion, `<=${lastVersionWithoutArchivedRedirectsFile}`)) {
  26. const redirect = archvivedRedirects[req.path]
  27. if (redirect && redirect !== req.path) {
  28. return res.redirect(301, redirect)
  29. }
  30. }
  31. let reqPath = req.path
  32. let isRedirect = false
  33. if (versionSatisfiesRange(requestedVersion, `>${lastVersionWithoutArchivedRedirectsFile}`)) {
  34. try {
  35. const res = await got(getProxyPath('redirects.json', requestedVersion))
  36. const redirectJson = JSON.parse(res.body)
  37. if (redirectJson[req.path]) {
  38. isRedirect = true
  39. }
  40. reqPath = redirectJson[req.path] || req.path
  41. } catch (err) {
  42. // nooop
  43. }
  44. }
  45. try {
  46. const r = await got(getProxyPath(reqPath, requestedVersion))
  47. res.set('content-type', r.headers['content-type'])
  48. res.set('x-robots-tag', 'noindex')
  49. // make redirects found via redirects.json return 301 instead of 200
  50. if (isRedirect) {
  51. res.status(301)
  52. res.set('location', reqPath)
  53. }
  54. // make stubbed redirect files (which exist in versions <2.13) return 301 instead of 200
  55. const staticRedirect = r.body.match(patterns.staticRedirect)
  56. if (staticRedirect) {
  57. res.status(301)
  58. res.set('location', staticRedirect[1])
  59. }
  60. return res.send(r.body)
  61. } catch (err) {
  62. for (const fallbackRedirect of getFallbackRedirects(req, requestedVersion) || []) {
  63. try {
  64. await got(getProxyPath(fallbackRedirect, requestedVersion))
  65. return res.redirect(301, fallbackRedirect)
  66. } catch (err) { } // noop
  67. }
  68. return next()
  69. }
  70. }
  71. // paths are slightly different depending on the version
  72. // for >=2.13: /2.13/en/enterprise/2.13/user/articles/viewing-contributions-on-your-profile
  73. // for <2.13: /2.12/user/articles/viewing-contributions-on-your-profile
  74. function getProxyPath (reqPath, requestedVersion) {
  75. const proxyPath = versionSatisfiesRange(requestedVersion, `>=${firstVersionDeprecatedOnNewSite}`)
  76. ? slash(path.join('/', requestedVersion, reqPath))
  77. : reqPath.replace(/^\/enterprise/, '')
  78. return `https://github.github.com/help-docs-archived-enterprise-versions${proxyPath}`
  79. }
  80. // from 2.13 to 2.17, we lost access to frontmatter redirects during the archival process
  81. // this workaround finds potentially relevant frontmatter redirects in currently supported pages
  82. function getFallbackRedirects (req, requestedVersion) {
  83. if (versionSatisfiesRange(requestedVersion, `<${firstVersionDeprecatedOnNewSite}`)) return
  84. if (versionSatisfiesRange(requestedVersion, `>${lastVersionWithoutArchivedRedirectsFile}`)) return
  85. return archivedFrontmatterFallbacks.find(arrayOfFallbacks => arrayOfFallbacks.includes(req.path))
  86. }
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...