Move package name validation into parsePackageURL

This commit is contained in:
MICHAEL JACKSON 2017-11-14 16:44:53 -08:00
parent 67a17a961b
commit 4b3f606d7d
4 changed files with 15 additions and 5 deletions

View File

@ -1,7 +1,6 @@
const parseURL = require('url').parse
const startOfDay = require('date-fns/start_of_day')
const addDays = require('date-fns/add_days')
const validateNPMPackageName = require('validate-npm-package-name')
const parsePackageURL = require('./utils/parsePackageURL')
const CloudflareAPI = require('./CloudflareAPI')
const StatsAPI = require('./StatsAPI')
@ -67,10 +66,7 @@ function computeCounters(stream) {
const url = parsePackageURL(parseURL(clientRequest.uri).pathname)
const packageName = url && url.packageName
if (
packageName &&
validateNPMPackageName(packageName).errors == null
) {
if (packageName) {
incr(
`stats-packageRequests-${dayKey}`,
packageName,

View File

@ -80,5 +80,6 @@ describe('parsePackageURL', () => {
it('returns null for invalid pathnames', () => {
expect(parsePackageURL('history')).toBe(null)
expect(parsePackageURL('/.invalid')).toBe(null)
})
})

View File

@ -1,4 +1,5 @@
const url = require('url')
const validatePackageName = require('./validatePackageName')
const URLFormat = /^\/((?:@[^\/@]+\/)?[^\/@]+)(?:@([^\/]+))?(\/.*)?$/
@ -19,9 +20,14 @@ function parsePackageURL(packageURL) {
const match = URLFormat.exec(pathname)
// Disallow invalid URL formats.
if (match == null) return null
const packageName = match[1]
// Disallow invalid npm package names.
if (!validatePackageName(packageName)) return null
const packageVersion = decodeParam(match[2]) || 'latest'
const filename = decodeParam(match[3])

View File

@ -0,0 +1,7 @@
const validateNpmPackageName = require('validate-npm-package-name')
function validatePackageName(packageName) {
return validateNpmPackageName(packageName).errors == null
}
module.exports = validatePackageName