Skip to content

Commit e996382

Browse files
authoredApr 25, 2024··
Merge pull request #1802 from alumni/fix-url-parse
fix: replace deprecated `node:url` methods
2 parents b368f62 + 7e20ced commit e996382

File tree

1 file changed

+20
-41
lines changed

1 file changed

+20
-41
lines changed
 

‎src/node/index.js

+20-41
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22
* Module dependencies.
33
*/
44

5-
// eslint-disable-next-line node/no-deprecated-api
6-
const { parse, format, resolve } = require('url');
5+
const { format } = require('url');
76
const Stream = require('stream');
87
const https = require('https');
98
const http = require('http');
@@ -503,15 +502,15 @@ Request.prototype._redirect = function (res) {
503502
debug('redirect %s -> %s', this.url, url);
504503

505504
// location
506-
url = resolve(this.url, url);
505+
url = new URL(url, this.url).href;
507506

508507
// ensure the response is being consumed
509508
// this is required for Node v0.10+
510509
res.resume();
511510

512511
let headers = this.req.getHeaders ? this.req.getHeaders() : this.req._headers;
513512

514-
const changesOrigin = parse(url).host !== parse(this.url).host;
513+
const changesOrigin = new URL(url).host !== new URL(this.url).host;
515514

516515
// implementation of 302 following defacto standard
517516
if (res.statusCode === 301 || res.statusCode === 302) {
@@ -695,43 +694,24 @@ Request.prototype.request = function () {
695694
return this.emit('error', err);
696695
}
697696

698-
let { url } = this;
697+
let { url: urlString } = this;
699698
const retries = this._retries;
700699

701-
// Capture backticks as-is from the final query string built above.
702-
// Note: this'll only find backticks entered in req.query(String)
703-
// calls, because qs.stringify unconditionally encodes backticks.
704-
let queryStringBackticks;
705-
if (url.includes('`')) {
706-
const queryStartIndex = url.indexOf('?');
707-
708-
if (queryStartIndex !== -1) {
709-
const queryString = url.slice(queryStartIndex + 1);
710-
queryStringBackticks = queryString.match(/`|%60/g);
711-
}
712-
}
713-
714700
// default to http://
715-
if (url.indexOf('http') !== 0) url = `http://${url}`;
716-
url = parse(url);
717-
718-
// See https://github.com/ladjs/superagent/issues/1367
719-
if (queryStringBackticks) {
720-
let i = 0;
721-
url.query = url.query.replace(/%60/g, () => queryStringBackticks[i++]);
722-
url.search = `?${url.query}`;
723-
url.path = url.pathname + url.search;
724-
}
701+
if (urlString.indexOf('http') !== 0) urlString = `http://${urlString}`;
702+
const url = new URL(urlString);
703+
let { protocol } = url;
704+
let path = `${url.pathname}${url.search}`;
725705

726706
// support unix sockets
727-
if (/^https?\+unix:/.test(url.protocol) === true) {
707+
if (/^https?\+unix:/.test(protocol) === true) {
728708
// get the protocol
729-
url.protocol = `${url.protocol.split('+')[0]}:`;
709+
protocol = `${protocol.split('+')[0]}:`;
730710

731-
// get the socket, path
732-
const unixParts = url.path.match(/^([^/]+)(.+)$/);
733-
options.socketPath = unixParts[1].replace(/%2F/g, '/');
734-
url.path = unixParts[2];
711+
// get the socket path
712+
options.socketPath = url.hostname.replace(/%2F/g, '/');
713+
url.host = '';
714+
url.hostname = '';
735715
}
736716

737717
// Override IP address of a hostname
@@ -772,7 +752,7 @@ Request.prototype.request = function () {
772752
// options
773753
options.method = this.method;
774754
options.port = url.port;
775-
options.path = url.path;
755+
options.path = path;
776756
options.host = url.hostname;
777757
options.ca = this._ca;
778758
options.key = this._key;
@@ -800,8 +780,8 @@ Request.prototype.request = function () {
800780

801781
// initiate request
802782
const module_ = this._enableHttp2
803-
? exports.protocols['http2:'].setProtocol(url.protocol)
804-
: exports.protocols[url.protocol];
783+
? exports.protocols['http2:'].setProtocol(protocol)
784+
: exports.protocols[protocol];
805785

806786
// request
807787
this.req = module_.request(options);
@@ -814,7 +794,7 @@ Request.prototype.request = function () {
814794
req.setHeader('Accept-Encoding', 'gzip, deflate');
815795
}
816796

817-
this.protocol = url.protocol;
797+
this.protocol = protocol;
818798
this.host = url.host;
819799

820800
// expose events
@@ -837,9 +817,8 @@ Request.prototype.request = function () {
837817
});
838818

839819
// auth
840-
if (url.auth) {
841-
const auth = url.auth.split(':');
842-
this.auth(auth[0], auth[1]);
820+
if (url.username || url.password) {
821+
this.auth(url.username, url.password);
843822
}
844823

845824
if (this.username && this.password) {

0 commit comments

Comments
 (0)
Please sign in to comment.