From a2bb7254d5712b83a35104e36315b1124c6f4349 Mon Sep 17 00:00:00 2001 From: Thomas Watson Date: Mon, 30 Sep 2024 10:24:37 +0200 Subject: [PATCH] Don't use deprecated url.parse function --- .../dd-trace/src/exporters/common/request.js | 45 +------------------ 1 file changed, 2 insertions(+), 43 deletions(-) diff --git a/packages/dd-trace/src/exporters/common/request.js b/packages/dd-trace/src/exporters/common/request.js index 6823119c0d8..7bc957f3ec0 100644 --- a/packages/dd-trace/src/exporters/common/request.js +++ b/packages/dd-trace/src/exporters/common/request.js @@ -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') @@ -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 {