url: improve resolveObject with ObjectAssign

PR-URL: https://github.com/nodejs/node/pull/54092
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
Reviewed-By: Daeyeon Jeong <daeyeon.dev@gmail.com>
This commit is contained in:
Early Riser 2024-07-31 15:17:59 +09:00 committed by GitHub
parent e7edcf38cd
commit b4fd1fd63f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -24,6 +24,7 @@
const {
Boolean,
Int8Array,
ObjectAssign,
ObjectKeys,
StringPrototypeCharCodeAt,
decodeURIComponent,
@ -735,11 +736,7 @@ Url.prototype.resolveObject = function resolveObject(relative) {
}
const result = new Url();
const tkeys = ObjectKeys(this);
for (let tk = 0; tk < tkeys.length; tk++) {
const tkey = tkeys[tk];
result[tkey] = this[tkey];
}
ObjectAssign(result, this);
// Hash is always overridden, no matter what.
// even href="" will remove it.
@ -754,12 +751,13 @@ Url.prototype.resolveObject = function resolveObject(relative) {
// Hrefs like //foo/bar always cut to the protocol.
if (relative.slashes && !relative.protocol) {
// Take everything except the protocol from relative
const rkeys = ObjectKeys(relative);
for (let rk = 0; rk < rkeys.length; rk++) {
const rkey = rkeys[rk];
if (rkey !== 'protocol')
result[rkey] = relative[rkey];
}
const relativeWithoutProtocol = ObjectKeys(relative).reduce((acc, key) => {
if (key !== 'protocol') {
acc[key] = relative[key];
}
return acc;
}, {});
ObjectAssign(result, relativeWithoutProtocol);
// urlParse appends trailing / to urls like http://www.example.com
if (slashedProtocol.has(result.protocol) &&
@ -781,11 +779,7 @@ Url.prototype.resolveObject = function resolveObject(relative) {
// because that's known to be hostless.
// anything else is assumed to be absolute.
if (!slashedProtocol.has(relative.protocol)) {
const keys = ObjectKeys(relative);
for (let v = 0; v < keys.length; v++) {
const k = keys[v];
result[k] = relative[k];
}
ObjectAssign(result, relative);
result.href = result.format();
return result;
}