Redirect ?json => ?meta

Fixes #60
This commit is contained in:
MICHAEL JACKSON 2017-08-29 14:38:23 -07:00
parent dd8df34574
commit 1da995666b
1 changed files with 14 additions and 8 deletions

View File

@ -46,13 +46,25 @@ function createSearch(query) {
* Parse and validate the URL.
*/
function packageURL(req, res, next) {
// Redirect /_meta/pkg to /pkg?meta.
// Redirect /_meta/path to /path?meta.
if (req.path.match(/^\/_meta\//)) {
delete req.query.json
req.query.meta = ''
return res.redirect(req.path.substr(6) + createSearch(req.query))
}
// Redirect /path?json => /path?meta
if (req.query.json != null) {
delete req.query.json
req.query.meta = ''
return res.redirect(req.path + createSearch(req.query))
}
// Redirect requests with unknown query params to their equivalents
// with only known params so they can be served from the cache. This
// prevents people using random query params designed to bust the cache.
if (!queryIsKnown(req.query))
return res.redirect(req.path + createSearch(sanitizeQuery(req.query)))
const url = parsePackageURL(req.url)
// Do not allow invalid URLs.
@ -65,12 +77,6 @@ function packageURL(req, res, next) {
if (nameErrors)
return res.status(403).type('text').send(`Invalid package name: ${url.packageName} (${nameErrors.join(', ')})`)
// Redirect requests with unknown query params to their equivalents
// with only known params so they can be served from the cache. This
// prevents people using random query params designed to bust the cache.
if (!queryIsKnown(url.query))
return res.redirect(url.pathname + createSearch(sanitizeQuery(url.query)))
req.packageName = url.packageName
req.packageVersion = url.packageVersion
req.packageSpec = `${req.packageName}@${req.packageVersion}`