Skip to content

Commit dd60b12

Browse files
yuxizhewatson
authored andcommitted
chore: use url.URL instead of url.parse (#1340)
The url.parse function is deprecated
1 parent 17e7022 commit dd60b12

File tree

4 files changed

+32
-12
lines changed

4 files changed

+32
-12
lines changed

lib/instrumentation/express-utils.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
'use strict'
22

3+
var symbols = require('../symbols')
4+
var parsers = require('../parsers')
5+
36
var parseUrl
47
try {
58
parseUrl = require('parseurl')
69
} catch (e) {
7-
const url = require('url')
8-
parseUrl = req => url.parse(req.url)
10+
parseUrl = req => parsers.parseUrl(req.url)
911
}
10-
var symbols = require('../symbols')
1112

1213
function normalizeSlash (value) {
1314
return value[0] === '/' ? value : '/' + value

lib/instrumentation/http-shared.js

+20-6
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ var url = require('url')
44

55
var endOfStream = require('end-of-stream')
66

7+
var parsers = require('../parsers')
8+
79
const transactionForResponse = new WeakMap()
810
exports.transactionForResponse = transactionForResponse
911

@@ -80,15 +82,27 @@ function isRequestBlacklisted (agent, req) {
8082
return false
8183
}
8284

85+
function formatURL (item) {
86+
return {
87+
href: item.href,
88+
pathname: item.pathname,
89+
path: item.pathname + (item.search || ''),
90+
protocol: item.protocol,
91+
host: item.host,
92+
port: item.port,
93+
hostname: item.hostname,
94+
hash: item.hash,
95+
search: item.search
96+
}
97+
}
98+
8399
// NOTE: This will also stringify and parse URL instances
84100
// to a format which can be mixed into the options object.
85101
function ensureUrl (v) {
86102
if (typeof v === 'string') {
87-
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())
103+
return formatURL(parsers.parseUrl(v))
104+
} else if (url.URL && v instanceof url.URL) {
105+
return formatURL(v)
92106
} else {
93107
return v
94108
}
@@ -142,7 +156,7 @@ exports.traceOutgoingRequest = function (agent, moduleName, method) {
142156

143157
ins.bindEmitter(req)
144158

145-
span.name = req.method + ' ' + req._headers.host + url.parse(req.path).pathname
159+
span.name = req.method + ' ' + req._headers.host + parsers.parseUrl(req.path).pathname
146160

147161
// TODO: Research if it's possible to add this to the prototype instead.
148162
// Or if it's somehow preferable to listen for when a `response` listener

lib/instrumentation/modules/http2.js

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

3-
var url = require('url')
4-
53
var eos = require('end-of-stream')
64

75
var shimmer = require('../shimmer')
86
var symbols = require('../../symbols')
7+
var parsers = require('../../parsers')
98

109
module.exports = function (http2, agent, { enabled }) {
1110
if (!enabled) return http2
@@ -155,7 +154,8 @@ module.exports = function (http2, agent, { enabled }) {
155154

156155
ins.bindEmitter(req)
157156

158-
var path = url.parse(headers[':path']).pathname
157+
var urlObj = parsers.parseUrl(headers[':path'])
158+
var path = urlObj.pathname
159159
span.name = headers[':method'] + ' ' + host + path
160160

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

lib/parsers.js

+5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict'
22

33
var util = require('util')
4+
var url = require('url')
45

56
var afterAll = require('after-all-results')
67
var basicAuth = require('basic-auth')
@@ -215,6 +216,10 @@ exports.parseCallsite = function (callsite, isError, agent, cb) {
215216
})
216217
}
217218

219+
exports.parseUrl = function (urlStr) {
220+
return url.URL ? new url.URL(urlStr, 'relative:///') : url.parse(urlStr)
221+
}
222+
218223
// Default `culprit` to the top of the stack or the highest non `library_frame`
219224
// frame if such exists
220225
function getCulprit (frames) {

0 commit comments

Comments
 (0)