Skip to content

Commit

Permalink
Don't use deprecated url.parse function (#4743)
Browse files Browse the repository at this point in the history
  • Loading branch information
watson authored Oct 2, 2024
1 parent 92515a6 commit 4d2f5b8
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 34 deletions.
42 changes: 8 additions & 34 deletions packages/dd-trace/src/exporters/common/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@
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 zlib = require('zlib')

const { urlToHttpOptions } = require('./url-to-http-options-polyfill')
const docker = require('./docker')
const { httpAgent, httpsAgent } = require('./agents')
const { storage } = require('../../../../datadog-core')
Expand All @@ -20,39 +19,14 @@ 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 parseUrl (urlObjOrString) {
if (typeof urlObjOrString === 'object') return urlToHttpOptions(urlObjOrString)

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

// Add the 'hostname' back if we're using named pipes
if (url.protocol === 'unix:' && url.host === '.') {
const udsPath = urlString.replace(/^unix:/, '')
// Special handling if we're using named pipes on Windows
if (url.protocol === 'unix:' && url.hostname === '.') {
const udsPath = urlObjOrString.slice(5)
url.path = udsPath
url.pathname = udsPath
}
Expand All @@ -66,7 +40,7 @@ function request (data, options, callback) {
}

if (options.url) {
const url = typeof options.url === 'object' ? urlToOptions(options.url) : fromUrlString(options.url)
const url = parseUrl(options.url)
if (url.protocol === 'unix:') {
options.socketPath = url.pathname
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
'use strict'

const { urlToHttpOptions } = require('url')

// TODO: Remove `urlToHttpOptions` polyfill once we drop support for the older Cypress versions that uses a built-in
// version of Node.js doesn't include that function.
module.exports = {
urlToHttpOptions: urlToHttpOptions ?? function (url) {
const { hostname, pathname, port, username, password, search } = url
const options = {
__proto__: null,
...url, // In case the url object was extended by the user.
protocol: url.protocol,
hostname: typeof hostname === 'string' && hostname.startsWith('[')
? hostname.slice(1, -1)
: hostname,
hash: url.hash,
search,
pathname,
path: `${pathname || ''}${search || ''}`,
href: url.href
}
if (port !== '') {
options.port = Number(port)
}
if (username || password) {
options.auth = `${decodeURIComponent(username)}:${decodeURIComponent(password)}`
}
return options
}
}

0 comments on commit 4d2f5b8

Please sign in to comment.