mit-license/middleware/load-options.js
Richie Bendall f189d33f29
fix: Properly parse gravatar boolean and switch to standard linter.
Signed-off-by: Richie Bendall <richiebendall@gmail.com>
2020-01-14 21:05:39 +13:00

63 lines
1.3 KiB
JavaScript

const currentYear = new Date().getFullYear()
module.exports = (req, res, next) => {
const parts = req.url.split('/')
res.locals.options = parts.reduce(
(acc, curr) => {
if (!curr) return acc
let match = curr.match(/^@?(\d{4})$/) || []
if (match.length) {
// Pinned year
if (curr.startsWith('@')) {
acc.pinnedYear = parseInt(curr.substr(1), 10)
} else {
acc.startYear = parseInt(curr, 10)
}
return acc
}
match = curr.match(/^(\d{4})-(\d{4})$/) || []
if (match.length) {
acc.startYear = parseInt(match[1], 10)
acc.endYear = parseInt(match[2], 10)
return acc
}
if (curr.startsWith('license')) {
acc.format = curr
.split('.')
.pop()
.trim()
return acc
}
if (curr.startsWith('+')) {
acc.license = curr.substr(1).toUpperCase()
return acc
}
acc.sha = curr // not actually supported now - 2019-06-19
return acc
},
{
format: 'html',
startYear: null,
endYear: currentYear,
sha: null
}
)
if (res.locals.options.sha) {
res.setHeader(
'X-note',
'SHA and commit pinning is no longer supported, showing you latest release'
)
}
next()
}