2017-08-10 17:12:50 +00:00
|
|
|
const { parsePackageURL } = require('./PackageUtils')
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parse and validate the URL.
|
|
|
|
*/
|
2017-08-11 05:11:55 +00:00
|
|
|
function parseURL(req, res, next) {
|
|
|
|
let url
|
|
|
|
try {
|
|
|
|
url = parsePackageURL(req.url)
|
|
|
|
} catch (error) {
|
|
|
|
return res.status(403).send(`Invalid URL: ${req.url}`)
|
|
|
|
}
|
2017-08-10 17:12:50 +00:00
|
|
|
|
2017-08-11 05:11:55 +00:00
|
|
|
if (url == null)
|
|
|
|
return res.status(403).send(`Invalid URL: ${req.url}`)
|
2017-08-10 17:12:50 +00:00
|
|
|
|
2017-08-11 05:11:55 +00:00
|
|
|
req.packageName = url.packageName
|
|
|
|
req.packageVersion = url.packageVersion
|
|
|
|
req.packageSpec = `${req.packageName}@${req.packageVersion}`
|
|
|
|
req.pathname = url.pathname
|
|
|
|
req.filename = url.filename
|
|
|
|
req.search = url.search
|
|
|
|
req.query = url.query
|
2017-08-10 17:12:50 +00:00
|
|
|
|
2017-08-11 05:11:55 +00:00
|
|
|
next()
|
2017-08-10 17:12:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = parseURL
|