Skip to content

Commit a6dacec

Browse files
committed
remove URLSearchParams
1 parent 90a545f commit a6dacec

3 files changed

Lines changed: 40 additions & 49 deletions

File tree

packages/plugin-network-instrumentation/lib/extract-domain.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,19 @@
55
*/
66
module.exports = function (url) {
77
try {
8-
const urlObj = new URL(url)
9-
return urlObj.host
8+
const isAbsolute = /^https?:\/\//i.test(url)
9+
if (!isAbsolute) {
10+
return 'unknown'
11+
}
12+
13+
const urlWithoutProtocol = url.replace(/^https?:\/\//i, '')
14+
15+
const firstSlashIndex = urlWithoutProtocol.indexOf('/')
16+
if (firstSlashIndex !== -1) {
17+
return urlWithoutProtocol.substring(0, firstSlashIndex)
18+
}
19+
20+
return urlWithoutProtocol
1021
} catch (e) {
1122
return 'unknown'
1223
}

packages/plugin-network-instrumentation/lib/parse-query-params.js

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,24 @@
55
*/
66
module.exports = function (url) {
77
try {
8-
const urlObj = new URL(url)
9-
const params = {}
10-
urlObj.searchParams.forEach((value, key) => {
11-
params[key] = value
8+
const queryStart = url.indexOf('?')
9+
const hashStart = url.indexOf('#')
10+
11+
let queryString = ''
12+
if (queryStart !== -1) {
13+
const queryEnd = hashStart !== -1 && hashStart > queryStart ? hashStart : url.length
14+
queryString = url.substring(queryStart + 1, queryEnd)
15+
}
16+
17+
// convert query string to object without using UrlSearchParams
18+
const queryStringObject = {}
19+
const pairs = queryString.split('&').filter(pair => pair.length > 0)
20+
pairs.forEach(pair => {
21+
const [key, value] = pair.split('=')
22+
queryStringObject[decodeURIComponent(key)] = decodeURIComponent(value || '')
1223
})
13-
return params
24+
25+
return queryStringObject
1426
} catch (e) {
1527
return {}
1628
}
Lines changed: 10 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,23 @@
1+
const parseQueryParams = require('./parse-query-params')
12
const redactValues = require('./redact-values')
23

3-
function isAbsoluteURL (url) {
4-
try {
5-
// eslint-disable-next-line no-new
6-
new URL(url)
7-
return true
8-
} catch (e) {
9-
return false
10-
}
11-
}
12-
134
module.exports = function (url, redactedKeys) {
14-
const isAbsolute = isAbsoluteURL(url)
15-
const base = isAbsolute ? undefined : 'http://localhost'
16-
17-
// Parse the URL - use a base only for relative URLs
18-
const urlObj = new URL(url, base)
5+
const paramsObject = parseQueryParams(url)
6+
const redactedParams = redactValues(paramsObject, redactedKeys)
7+
const redactedQueryString = Object.entries(redactedParams).map(([key, value]) => `${key}=${value}`).join('&')
198

20-
// Extract query string manually from the original URL
219
const queryStart = url.indexOf('?')
2210
const hashStart = url.indexOf('#')
23-
24-
let queryString = ''
25-
if (queryStart !== -1) {
26-
const queryEnd = hashStart !== -1 && hashStart > queryStart ? hashStart : url.length
27-
queryString = url.substring(queryStart + 1, queryEnd)
28-
}
29-
30-
// Convert URLSearchParams to object without using Object.fromEntries()
31-
const params = new URLSearchParams(queryString)
32-
const paramsObject = {}
33-
params.forEach((value, key) => {
34-
paramsObject[key] = value
35-
})
36-
37-
const redactedParams = redactValues(paramsObject, redactedKeys)
38-
const redactedQueryString = new URLSearchParams(redactedParams).toString()
11+
const hash = hashStart !== -1 ? url.substring(hashStart) : ''
12+
let result = queryStart !== -1 ? url.substring(0, queryStart) : url
3913

4014
// Build the result URL manually
41-
let result = urlObj.pathname
42-
if (redactedQueryString) {
15+
if (redactedQueryString && redactedQueryString.length > 0) {
4316
result += '?' + redactedQueryString
4417
}
45-
if (urlObj.hash) {
46-
result += urlObj.hash
47-
}
48-
49-
// Return appropriate format based on original URL type
50-
if (isAbsolute) {
51-
return decodeURI(urlObj.origin + result)
18+
if (hash) {
19+
result += hash
5220
}
5321

54-
return decodeURI(result)
22+
return result
5523
}

0 commit comments

Comments
 (0)