31 lines
870 B
JavaScript
31 lines
870 B
JavaScript
const validateNPMPackageName = require('validate-npm-package-name')
|
|
const PackageURL = require('../PackageURL')
|
|
|
|
/**
|
|
* Parse and validate the URL.
|
|
*/
|
|
function parsePackageURL(req, res, next) {
|
|
const url = PackageURL.parse(req.url)
|
|
|
|
if (url == null)
|
|
return res.status(403).type('text').send(`Invalid URL: ${req.url}`)
|
|
|
|
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(', ')})`)
|
|
|
|
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
|
|
|
|
next()
|
|
}
|
|
|
|
module.exports = parsePackageURL
|