2017-08-15 18:56:08 +00:00
|
|
|
const validateNPMPackageName = require('validate-npm-package-name')
|
2017-08-12 03:27:27 +00:00
|
|
|
const PackageURL = require('../PackageURL')
|
2017-08-10 17:12:50 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Parse and validate the URL.
|
|
|
|
*/
|
2017-08-16 21:55:40 +00:00
|
|
|
function parsePackageURL(req, res, next) {
|
2017-08-12 03:27:27 +00:00
|
|
|
const url = PackageURL.parse(req.url)
|
2017-08-10 17:12:50 +00:00
|
|
|
|
2017-08-11 05:11:55 +00:00
|
|
|
if (url == null)
|
2017-08-13 00:23:40 +00:00
|
|
|
return res.status(403).type('text').send(`Invalid URL: ${req.url}`)
|
2017-08-10 17:12:50 +00:00
|
|
|
|
2017-08-15 18:56:08 +00:00
|
|
|
const nameErrors = validateNPMPackageName(url.packageName).errors
|
|
|
|
|
|
|
|
// Do not allow invalid package names.
|
|
|
|
if (nameErrors)
|
|
|
|
return res.status(403).type('text').send(`Invalid package name: ${url.packageName} (${nameErrors.join(', ')})`)
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-08-16 21:55:40 +00:00
|
|
|
module.exports = parsePackageURL
|