Skip to content

Commit

Permalink
Don't use deprecated url.parse function
Browse files Browse the repository at this point in the history
  • Loading branch information
watson committed Sep 30, 2024
1 parent 7f49451 commit a2bb725
Showing 1 changed file with 2 additions and 43 deletions.
45 changes: 2 additions & 43 deletions packages/dd-trace/src/exporters/common/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
const { Readable } = require('stream')
const http = require('http')
const https = require('https')
// eslint-disable-next-line n/no-deprecated-api
const { parse: urlParse } = require('url')
const { urlToHttpOptions } = require('url')
const zlib = require('zlib')

const docker = require('./docker')
Expand All @@ -20,53 +19,13 @@ const containerId = docker.id()

let activeRequests = 0

// TODO: Replace with `url.urlToHttpOptions` when supported by all versions
function urlToOptions (url) {
const agent = url.agent || http.globalAgent
const options = {
protocol: url.protocol || agent.protocol,
hostname: typeof url.hostname === 'string' && url.hostname.startsWith('[')
? url.hostname.slice(1, -1)
: url.hostname ||
url.host ||
'localhost',
hash: url.hash,
search: url.search,
pathname: url.pathname,
path: `${url.pathname || ''}${url.search || ''}`,
href: url.href
}
if (url.port !== '') {
options.port = Number(url.port)
}
if (url.username || url.password) {
options.auth = `${url.username}:${url.password}`
}
return options
}

function fromUrlString (urlString) {
const url = typeof urlToHttpOptions === 'function'
? urlToOptions(new URL(urlString))
: urlParse(urlString)

// Add the 'hostname' back if we're using named pipes
if (url.protocol === 'unix:' && url.host === '.') {
const udsPath = urlString.replace(/^unix:/, '')
url.path = udsPath
url.pathname = udsPath
}

return url
}

function request (data, options, callback) {
if (!options.headers) {
options.headers = {}
}

if (options.url) {
const url = typeof options.url === 'object' ? urlToOptions(options.url) : fromUrlString(options.url)
const url = urlToHttpOptions(typeof options.url === 'object' ? options.url : new URL(options.url))
if (url.protocol === 'unix:') {
options.socketPath = url.pathname
} else {
Expand Down

0 comments on commit a2bb725

Please sign in to comment.