From d3f3aa610cccffd8aacba70e928a75c013f9cc42 Mon Sep 17 00:00:00 2001 From: MICHAEL JACKSON Date: Fri, 1 Sep 2017 22:43:56 -0700 Subject: [PATCH] Remove ?main from fully-resolved URLs --- server/middleware/fetchFile.js | 8 +++++--- server/middleware/packageURL.js | 17 +---------------- server/middleware/utils/createSearch.js | 17 +++++++++++++++++ 3 files changed, 23 insertions(+), 19 deletions(-) create mode 100644 server/middleware/utils/createSearch.js diff --git a/server/middleware/fetchFile.js b/server/middleware/fetchFile.js index dbedeb1..14e30f9 100644 --- a/server/middleware/fetchFile.js +++ b/server/middleware/fetchFile.js @@ -2,8 +2,9 @@ const fs = require('fs') const path = require('path') const semver = require('semver') const createPackageURL = require('../utils/createPackageURL') -const getPackage = require('./utils/getPackage') +const createSearch = require('./utils/createSearch') const getPackageInfo = require('./utils/getPackageInfo') +const getPackage = require('./utils/getPackage') function getBasename(file) { return path.basename(file, path.extname(file)) @@ -112,13 +113,14 @@ function fetchFile(req, res, next) { filename = file.replace(req.packageDir, '') - if (getBasename(req.filename) !== getBasename(filename)) { + if (req.query.main != null || getBasename(req.filename) !== getBasename(filename)) { // Need to redirect to the module file so relative imports resolve // correctly. Cache module redirects for 1 minute. + delete req.query.main res.set({ 'Cache-Control': 'public, max-age=60', 'Cache-Tag': 'redirect,module-redirect' - }).redirect(302, createPackageURL(req.packageName, req.packageVersion, filename, req.search)) + }).redirect(302, createPackageURL(req.packageName, req.packageVersion, filename, createSearch(req.query))) } else { req.filename = filename req.stats = stats diff --git a/server/middleware/packageURL.js b/server/middleware/packageURL.js index ff527e6..ae8f191 100644 --- a/server/middleware/packageURL.js +++ b/server/middleware/packageURL.js @@ -1,5 +1,6 @@ const validateNPMPackageName = require('validate-npm-package-name') const parsePackageURL = require('../utils/parsePackageURL') +const createSearch = require('./utils/createSearch') const KnownQueryParams = { main: true, @@ -26,22 +27,6 @@ function sanitizeQuery(query) { return saneQuery } -function createSearch(query) { - const params = [] - - Object.keys(query).forEach(function (param) { - if (query[param] === '') { - params.push(param) // Omit the trailing "=" from param= - } else { - params.push(`${param}=${encodeURIComponent(query[param])}`) - } - }) - - const search = params.join('&') - - return search ? `?${search}` : '' -} - /** * Parse and validate the URL. */ diff --git a/server/middleware/utils/createSearch.js b/server/middleware/utils/createSearch.js new file mode 100644 index 0000000..a4fd805 --- /dev/null +++ b/server/middleware/utils/createSearch.js @@ -0,0 +1,17 @@ +function createSearch(query) { + const params = [] + + Object.keys(query).forEach(function (param) { + if (query[param] === '') { + params.push(param) // Omit the trailing "=" from param= + } else { + params.push(`${param}=${encodeURIComponent(query[param])}`) + } + }) + + const search = params.join('&') + + return search ? `?${search}` : '' +} + +module.exports = createSearch