Skip to content

Commit 71c386d

Browse files
committed
feat: change code to use url.URL instead of url.parse
1 parent 248c945 commit 71c386d

File tree

3 files changed

+21
-14
lines changed

3 files changed

+21
-14
lines changed

lib/instrumentation/express-utils.js

+3-8
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
11
'use strict'
22

3-
var parseUrl
4-
try {
5-
parseUrl = require('parseurl')
6-
} catch (e) {
7-
const url = require('url')
8-
parseUrl = req => url.parse(req.url)
9-
}
3+
var url = require('url')
4+
105
var symbols = require('../symbols')
116

127
function normalizeSlash (value) {
@@ -50,7 +45,7 @@ function getPathFromRequest (req, useBase, usePathAsTransactionName) {
5045
}
5146

5247
if (usePathAsTransactionName) {
53-
const parsed = parseUrl(req)
48+
const parsed = url.URL ? new url.URL(req.url, 'relative:///') : url.parse(req.url)
5449
return parsed && parsed.pathname
5550
}
5651
}

lib/instrumentation/http-shared.js

+16-5
Original file line numberDiff line numberDiff line change
@@ -80,15 +80,26 @@ function isRequestBlacklisted (agent, req) {
8080
return false
8181
}
8282

83+
function formatURL (item) {
84+
return {
85+
href: item.href,
86+
pathname: item.pathname,
87+
protocol: item.protocol,
88+
host: item.host,
89+
port: item.port,
90+
hostname: item.hostname,
91+
hash: item.hash,
92+
search: item.search
93+
}
94+
}
8395
// NOTE: This will also stringify and parse URL instances
8496
// to a format which can be mixed into the options object.
8597
function ensureUrl (v) {
86-
if (typeof v === 'string') {
98+
if (url.URL && (v instanceof url.URL || typeof v === 'string')) {
99+
const urlObj = new url.URL(v.toString(), 'relative:///')
100+
return formatURL(urlObj)
101+
} else if (typeof v === 'string') {
87102
return url.parse(v)
88-
} else if (v instanceof url.Url) {
89-
return v
90-
} else if (url.URL && v instanceof url.URL) { // check for url.URL because it wasn't added until Node.js 6.13.0
91-
return url.parse(v.toString())
92103
} else {
93104
return v
94105
}

lib/instrumentation/modules/http2.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,8 @@ module.exports = function (http2, agent, { enabled }) {
155155

156156
ins.bindEmitter(req)
157157

158-
var path = url.parse(headers[':path']).pathname
158+
var urlObj = url.URL ? new url.URL(headers[':path'], 'relative:///') : url.parse(headers[':path'])
159+
var path = urlObj.pathname
159160
span.name = headers[':method'] + ' ' + host + path
160161

161162
req.on('end', () => {

0 commit comments

Comments
 (0)