Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't use deprecated url.parse function #4743

Merged
merged 2 commits into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions packages/datadog-plugin-cypress/test/app/cypress/plugins/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,32 @@
// 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.
const url = require('url')
if (url.urlToHttpOptions === undefined) {
url.urlToHttpOptions = (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
}
}

module.exports = (on, config) => {
// We can't use the tracer available in the testing process, because this code is
// run in a different process. We need to init a different tracer reporting to the
Expand Down
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,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,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
Loading