move is-archived-version into a function instead of middleware

This commit is contained in:
Sarah Schneider 2020-11-18 15:18:50 -05:00
Родитель dbcb3a5cee
Коммит 67f490752a
4 изменённых файлов: 14 добавлений и 13 удалений

Просмотреть файл

@ -1,7 +1,7 @@
const patterns = require('../lib/patterns')
const { deprecated } = require('../lib/enterprise-server-releases')
module.exports = async (req, res, next) => {
module.exports = function isArchivedVersion (req) {
// if this is an assets path, use the referrer
// if this is a docs path, use the req.path
const pathToCheck = patterns.assetPaths.test(req.path)
@ -9,7 +9,9 @@ module.exports = async (req, res, next) => {
: req.path
// ignore paths that don't have an enterprise version number
if (!(patterns.getEnterpriseVersionNumber.test(pathToCheck) || patterns.getEnterpriseServerNumber.test(pathToCheck))) return next()
if (!(patterns.getEnterpriseVersionNumber.test(pathToCheck) || patterns.getEnterpriseServerNumber.test(pathToCheck))) {
return {}
}
// extract enterprise version from path, e.g. 2.16
const requestedVersion = pathToCheck.includes('enterprise-server@')
@ -17,11 +19,9 @@ module.exports = async (req, res, next) => {
: pathToCheck.match(patterns.getEnterpriseVersionNumber)[1]
// bail if the request version is not deprecated
if (!deprecated.includes(requestedVersion)) return next()
if (!deprecated.includes(requestedVersion)) {
return {}
}
// attach convenience props
req.isArchivedVersion = true
req.requestedVersion = requestedVersion
return next()
return { isArchived: true, requestedVersion }
}

Просмотреть файл

@ -1,5 +1,6 @@
const path = require('path')
const patterns = require('../lib/patterns')
const isArchivedVersion = require('../lib/is-archived-version')
const got = require('got')
// This module handles requests for the CSS and JS assets for
@ -9,8 +10,8 @@ const got = require('got')
// See also ./archived-enterprise-versions.js for non-CSS/JS paths
module.exports = async (req, res, next) => {
if (!req.isArchivedVersion) return next()
const requestedVersion = req.requestedVersion
const { isArchived, requestedVersion } = isArchivedVersion(req)
if (!isArchived) return next()
// Only match asset paths
if (!patterns.assetPaths.test(req.path)) return next()

Просмотреть файл

@ -3,6 +3,7 @@ const slash = require('slash')
const { latest, firstVersionDeprecatedOnNewSite, lastVersionWithoutStubbedRedirectFiles } = require('../lib/enterprise-server-releases')
const patterns = require('../lib/patterns')
const versionSatisfiesRange = require('../lib/version-satisfies-range')
const isArchivedVersion = require('../lib/is-archived-version')
const got = require('got')
const findPage = require('../lib/find-page')
@ -11,8 +12,8 @@ const findPage = require('../lib/find-page')
// https://github.com/github/help-docs-archived-enterprise-versions
module.exports = async (req, res, next) => {
if (!req.isArchivedVersion) return next()
const requestedVersion = req.requestedVersion
const { isArchived, requestedVersion } = isArchivedVersion(req)
if (!isArchived) return next()
// Skip asset paths
if (patterns.assetPaths.test(req.path)) return next()

Просмотреть файл

@ -54,7 +54,6 @@ module.exports = function (app) {
// *** Rendering, 2xx responses ***
// I largely ordered these by use frequency
app.use(require('./is-archived-version')) // Must come before archived-enterprise-versions* middleware
app.use(require('./archived-enterprise-versions-assets')) // Must come before static/assets
app.use('/dist', express.static('dist'))
app.use('/assets', express.static('assets'))