Consolidate url parse/create in PackageURL module
This commit is contained in:
parent
0d004b581b
commit
182e98bb3a
|
@ -6,9 +6,7 @@ const ndjson = require('ndjson')
|
|||
const redis = require('redis')
|
||||
const startOfDay = require('date-fns/start_of_day')
|
||||
const addDays = require('date-fns/add_days')
|
||||
const {
|
||||
parsePackageURL
|
||||
} = require('./middleware/PackageUtils')
|
||||
const PackageURL = require('./PackageURL')
|
||||
const {
|
||||
createDayKey,
|
||||
createHourKey
|
||||
|
@ -75,7 +73,7 @@ const stringifySeconds = (seconds) =>
|
|||
new Date(seconds * 1000).toISOString()
|
||||
|
||||
const getPackageName = (pathname) => {
|
||||
const parsed = parsePackageURL(pathname)
|
||||
const parsed = PackageURL.parse(pathname)
|
||||
return parsed && parsed.packageName
|
||||
}
|
||||
|
||||
|
|
|
@ -1,9 +1,18 @@
|
|||
const { parse: parseURL } = require('url')
|
||||
const url = require('url')
|
||||
|
||||
const URLFormat = /^\/((?:@[^\/@]+\/)?[^\/@]+)(?:@([^\/]+))?(\/.*)?$/
|
||||
|
||||
const decodeParam = (param) =>
|
||||
param ? decodeURIComponent(param) : ''
|
||||
function decodeParam(param) {
|
||||
if (param) {
|
||||
try {
|
||||
return decodeURIComponent(param) : ''
|
||||
} catch (error) {
|
||||
// Ignore param parsing errors.
|
||||
}
|
||||
}
|
||||
|
||||
return null
|
||||
}
|
||||
|
||||
const ValidQueryKeys = {
|
||||
main: true,
|
||||
|
@ -11,12 +20,17 @@ const ValidQueryKeys = {
|
|||
json: true
|
||||
}
|
||||
|
||||
const queryIsValid = (query) =>
|
||||
Object.keys(query).every(key => ValidQueryKeys[key])
|
||||
function queryIsValid(query) {
|
||||
return Object.keys(query).every(function (key) {
|
||||
return ValidQueryKeys[key]
|
||||
})
|
||||
}
|
||||
|
||||
const parsePackageURL = (url) => {
|
||||
const { pathname, search, query } = parseURL(url, true)
|
||||
function parsePackageURL(packageURL) {
|
||||
const { pathname, search, query } = url.parse(packageURL, true)
|
||||
|
||||
// Do not allow unrecognized query parameters because
|
||||
// some people use them to bust the cache.
|
||||
if (!queryIsValid(query))
|
||||
return null
|
||||
|
||||
|
@ -39,7 +53,7 @@ const parsePackageURL = (url) => {
|
|||
}
|
||||
}
|
||||
|
||||
const createPackageURL = (packageName, version, filename, search) => {
|
||||
function createPackageURL(packageName, version, filename, search) {
|
||||
let pathname = `/${packageName}`
|
||||
|
||||
if (version != null)
|
||||
|
@ -55,6 +69,6 @@ const createPackageURL = (packageName, version, filename, search) => {
|
|||
}
|
||||
|
||||
module.exports = {
|
||||
parsePackageURL,
|
||||
createPackageURL
|
||||
parse: parsePackageURL,
|
||||
create: createPackageURL
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
const { parsePackageURL } = require('./PackageUtils')
|
||||
const { parsePackageURL } = require('./PackageURL').parse
|
||||
|
||||
describe('parsePackageURL', () => {
|
||||
it('parses plain packages', () => {
|
|
@ -1,7 +1,7 @@
|
|||
const { maxSatisfying: maxSatisfyingVersion } = require('semver')
|
||||
const PackageCache = require('../PackageCache')
|
||||
const PackageInfo = require('../PackageInfo')
|
||||
const { createPackageURL } = require('./PackageUtils')
|
||||
const PackageURL = require('../PackageURL')
|
||||
|
||||
/**
|
||||
* Fetch the package from the registry and store a local copy on disk.
|
||||
|
@ -35,12 +35,12 @@ function fetchPackage(req, res, next) {
|
|||
}
|
||||
})
|
||||
} else if (req.packageVersion in tags) {
|
||||
res.redirect(createPackageURL(req.packageName, tags[req.packageVersion], req.filename, req.search))
|
||||
res.redirect(PackageURL.create(req.packageName, tags[req.packageVersion], req.filename, req.search))
|
||||
} else {
|
||||
const maxVersion = maxSatisfyingVersion(Object.keys(versions), req.packageVersion)
|
||||
|
||||
if (maxVersion) {
|
||||
res.redirect(createPackageURL(req.packageName, maxVersion, req.filename, req.search))
|
||||
res.redirect(PackageURL.create(req.packageName, maxVersion, req.filename, req.search))
|
||||
} else {
|
||||
res.status(404).send(`Cannot find package ${req.packageSpec}`)
|
||||
}
|
||||
|
|
|
@ -1,15 +1,10 @@
|
|||
const { parsePackageURL } = require('./PackageUtils')
|
||||
const PackageURL = require('../PackageURL')
|
||||
|
||||
/**
|
||||
* Parse and validate the URL.
|
||||
*/
|
||||
function parseURL(req, res, next) {
|
||||
let url
|
||||
try {
|
||||
url = parsePackageURL(req.url)
|
||||
} catch (error) {
|
||||
return res.status(403).send(`Invalid URL: ${req.url}`)
|
||||
}
|
||||
const url = PackageURL.parse(req.url)
|
||||
|
||||
if (url == null)
|
||||
return res.status(403).send(`Invalid URL: ${req.url}`)
|
||||
|
|
Loading…
Reference in New Issue