unpkg/server/middleware/parsePackageURL.js

31 lines
870 B
JavaScript
Raw Normal View History

2017-08-15 18:56:08 +00:00
const validateNPMPackageName = require('validate-npm-package-name')
const PackageURL = require('../PackageURL')
/**
* Parse and validate the URL.
*/
2017-08-16 21:55:40 +00:00
function parsePackageURL(req, res, next) {
const url = PackageURL.parse(req.url)
if (url == null)
2017-08-13 00:23:40 +00:00
return res.status(403).type('text').send(`Invalid URL: ${req.url}`)
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(', ')})`)
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()
}
2017-08-16 21:55:40 +00:00
module.exports = parsePackageURL