Skip to content

Commit ad2c3c0

Browse files
authoredMar 19, 2023
url: allow extension of user provided URL objects
PR-URL: #46989 Fixes: #46981 Reviewed-By: Yagiz Nizipli <[email protected]>
1 parent f7e9a12 commit ad2c3c0

File tree

3 files changed

+51
-13
lines changed

3 files changed

+51
-13
lines changed
 

‎doc/api/url.md

+5
Original file line numberDiff line numberDiff line change
@@ -1226,6 +1226,11 @@ pathToFileURL('/some/path%.c'); // Correct: file:///some/path%25.c (POSI
12261226
added:
12271227
- v15.7.0
12281228
- v14.18.0
1229+
changes:
1230+
- version: REPLACEME
1231+
pr-url: https://github.com/nodejs/node/pull/46989
1232+
description: The returned object will also contain all the own enumerable
1233+
properties of the `url` argument.
12291234
-->
12301235
12311236
* `url` {URL} The [WHATWG URL][] object to convert to an options object.

‎lib/internal/url.js

+19-13
Original file line numberDiff line numberDiff line change
@@ -1098,26 +1098,32 @@ function domainToUnicode(domain) {
10981098
return _domainToUnicode(`${domain}`);
10991099
}
11001100

1101-
// Utility function that converts a URL object into an ordinary
1102-
// options object as expected by the http.request and https.request
1103-
// APIs.
1101+
/**
1102+
* Utility function that converts a URL object into an ordinary options object
1103+
* as expected by the `http.request` and `https.request` APIs.
1104+
* @param {URL} url
1105+
* @returns {Record<string, unknown>}
1106+
*/
11041107
function urlToHttpOptions(url) {
1108+
const { hostname, pathname, port, username, password, search } = url;
11051109
const options = {
1110+
__proto__: null,
1111+
...url, // In case the url object was extended by the user.
11061112
protocol: url.protocol,
1107-
hostname: url.hostname && StringPrototypeStartsWith(url.hostname, '[') ?
1108-
StringPrototypeSlice(url.hostname, 1, -1) :
1109-
url.hostname,
1113+
hostname: hostname && StringPrototypeStartsWith(hostname, '[') ?
1114+
StringPrototypeSlice(hostname, 1, -1) :
1115+
hostname,
11101116
hash: url.hash,
1111-
search: url.search,
1112-
pathname: url.pathname,
1113-
path: `${url.pathname || ''}${url.search || ''}`,
1117+
search: search,
1118+
pathname: pathname,
1119+
path: `${pathname || ''}${search || ''}`,
11141120
href: url.href,
11151121
};
1116-
if (url.port !== '') {
1117-
options.port = Number(url.port);
1122+
if (port !== '') {
1123+
options.port = Number(port);
11181124
}
1119-
if (url.username || url.password) {
1120-
options.auth = `${decodeURIComponent(url.username)}:${decodeURIComponent(url.password)}`;
1125+
if (username || password) {
1126+
options.auth = `${decodeURIComponent(username)}:${decodeURIComponent(password)}`;
11211127
}
11221128
return options;
11231129
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const assert = require('node:assert');
5+
const http = require('node:http');
6+
7+
const headers = { foo: 'Bar' };
8+
const server = http.createServer(common.mustCall((req, res) => {
9+
assert.strictEqual(req.url, '/ping?q=term');
10+
assert.strictEqual(req.headers?.foo, headers.foo);
11+
req.resume();
12+
req.on('end', () => {
13+
res.writeHead(200);
14+
res.end('pong');
15+
});
16+
}));
17+
18+
server.listen(0, common.localhostIPv4, () => {
19+
const { address, port } = server.address();
20+
const url = new URL(`http://${address}:${port}/ping?q=term`);
21+
url.headers = headers;
22+
const clientReq = http.request(url);
23+
clientReq.on('close', common.mustCall(() => {
24+
server.close();
25+
}));
26+
clientReq.end();
27+
});

0 commit comments

Comments
 (0)