Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion lib/core/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,13 +102,24 @@ function isBlobLike (object) {
}
}

/**
* @param {string} url The path to check for query strings or fragments.
* @returns {boolean} Returns true if the path contains a query string or fragment.
*/
function pathHasQueryOrFragment (url) {
return (
url.includes('?') ||
url.includes('#')
)
}

/**
* @param {string} url The URL to add the query params to
* @param {import('node:querystring').ParsedUrlQueryInput} queryParams The object to serialize into a URL query string
* @returns {string} The URL with the query params added
*/
function serializePathWithQuery (url, queryParams) {
if (url.includes('?') || url.includes('#')) {
if (pathHasQueryOrFragment(url)) {
throw new Error('Query params cannot be passed when url already contains "?" or "#".')
}

Expand Down Expand Up @@ -924,6 +935,7 @@ module.exports = {
assertRequestHandler,
getSocketInfo,
isFormDataLike,
pathHasQueryOrFragment,
serializePathWithQuery,
addAbortListener,
isValidHTTPToken,
Expand Down
13 changes: 6 additions & 7 deletions lib/util/cache.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
'use strict'

const {
safeHTTPMethods
safeHTTPMethods,
pathHasQueryOrFragment
} = require('../core/util')

const { serializePathWithQuery } = require('../core/util')
Expand All @@ -14,12 +15,10 @@ function makeCacheKey (opts) {
throw new Error('opts.origin is undefined')
}

let fullPath
try {
fullPath = serializePathWithQuery(opts.path || '/', opts.query)
} catch (error) {
// If fails (path already has query params), use as-is
fullPath = opts.path || '/'
let fullPath = opts.path || '/'

if (opts.query && !pathHasQueryOrFragment(opts.path)) {
fullPath = serializePathWithQuery(fullPath, opts.query)
}

return {
Expand Down
Loading