Style tweaks

This commit is contained in:
Michael Jackson 2018-03-20 10:34:31 -07:00
parent 27e49d9298
commit 6e37129ec5
3 changed files with 13 additions and 9 deletions

View File

@ -2,14 +2,14 @@ const validateNpmPackageName = require("validate-npm-package-name");
const parsePackageURL = require("../utils/parsePackageURL");
const createSearch = require("./utils/createSearch");
const KnownQueryParams = {
const knownQueryParams = {
main: true,
meta: true,
module: true
};
function isKnownQueryParam(param) {
return !!KnownQueryParams[param];
return !!knownQueryParams[param];
}
function queryIsKnown(query) {

View File

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

View File

@ -1,5 +1,5 @@
const url = require("url");
const validatePackageName = require("./validatePackageName");
const isValidPackageName = require("./isValidPackageName");
const URLFormat = /^\/((?:@[^/@]+\/)?[^/@]+)(?:@([^/]+))?(\/.*)?$/;
@ -15,18 +15,22 @@ function decodeParam(param) {
return "";
}
function parsePackageURL(packageURL) {
const { pathname, search, query } = url.parse(packageURL, true);
function parsePackageURL(originalURL) {
const { pathname, search, query } = url.parse(originalURL, true);
const match = URLFormat.exec(pathname);
// Disallow invalid URL formats.
if (match == null) return null;
if (match == null) {
return null;
}
const packageName = match[1];
// Disallow invalid npm package names.
if (!validatePackageName(packageName)) return null;
if (!isValidPackageName(packageName)) {
return null;
}
const packageVersion = decodeParam(match[2]) || "latest";
const filename = decodeParam(match[3]);