Move package name validation into parsePackageURL
This commit is contained in:
parent
67a17a961b
commit
4b3f606d7d
|
@ -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,
|
||||
|
|
|
@ -80,5 +80,6 @@ describe('parsePackageURL', () => {
|
|||
|
||||
it('returns null for invalid pathnames', () => {
|
||||
expect(parsePackageURL('history')).toBe(null)
|
||||
expect(parsePackageURL('/.invalid')).toBe(null)
|
||||
})
|
||||
})
|
||||
|
|
|
@ -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])
|
||||
|
||||
|
|
|
@ -0,0 +1,7 @@
|
|||
const validateNpmPackageName = require('validate-npm-package-name')
|
||||
|
||||
function validatePackageName(packageName) {
|
||||
return validateNpmPackageName(packageName).errors == null
|
||||
}
|
||||
|
||||
module.exports = validatePackageName
|
Loading…
Reference in New Issue