url: avoid instanceof for WHATWG URL
PR-URL: https://github.com/nodejs/node/pull/11690 Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com> Reviewed-By: Timothy Gu <timothygu99@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Daijiro Wachi <daijiro.wachi@gmail.com>
This commit is contained in:
parent
82ef00cc0a
commit
b76a350a19
@ -5,7 +5,7 @@ const { URLSearchParams } = require('url');
|
|||||||
const bench = common.createBenchmark(main, {
|
const bench = common.createBenchmark(main, {
|
||||||
method: ['get', 'getAll', 'has'],
|
method: ['get', 'getAll', 'has'],
|
||||||
param: ['one', 'two', 'three', 'nonexistent'],
|
param: ['one', 'two', 'three', 'nonexistent'],
|
||||||
n: [1e6]
|
n: [2e7]
|
||||||
});
|
});
|
||||||
|
|
||||||
const str = 'one=single&two=first&three=first&two=2nd&three=2nd&three=3rd';
|
const str = 'one=single&two=first&three=first&two=2nd&three=2nd&three=3rd';
|
||||||
|
@ -8,7 +8,7 @@ const bench = common.createBenchmark(main, {
|
|||||||
prop: ['href', 'origin', 'protocol',
|
prop: ['href', 'origin', 'protocol',
|
||||||
'username', 'password', 'host', 'hostname', 'port',
|
'username', 'password', 'host', 'hostname', 'port',
|
||||||
'pathname', 'search', 'searchParams', 'hash'],
|
'pathname', 'search', 'searchParams', 'hash'],
|
||||||
n: [1e4]
|
n: [3e5]
|
||||||
});
|
});
|
||||||
|
|
||||||
function setAndGet(n, url, prop, alternative) {
|
function setAndGet(n, url, prop, alternative) {
|
||||||
|
@ -239,8 +239,10 @@ class URL {
|
|||||||
constructor(input, base) {
|
constructor(input, base) {
|
||||||
// toUSVString is not needed.
|
// toUSVString is not needed.
|
||||||
input = `${input}`;
|
input = `${input}`;
|
||||||
if (base !== undefined && !(base instanceof URL))
|
if (base !== undefined &&
|
||||||
|
(!base[searchParams] || !base[searchParams][searchParams])) {
|
||||||
base = new URL(base);
|
base = new URL(base);
|
||||||
|
}
|
||||||
parse(this, input, base);
|
parse(this, input, base);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -885,7 +887,7 @@ class URLSearchParams {
|
|||||||
}
|
}
|
||||||
|
|
||||||
[util.inspect.custom](recurseTimes, ctx) {
|
[util.inspect.custom](recurseTimes, ctx) {
|
||||||
if (!this || !(this instanceof URLSearchParams)) {
|
if (!this || !this[searchParams] || this[searchParams][searchParams]) {
|
||||||
throw new TypeError('Value of `this` is not a URLSearchParams');
|
throw new TypeError('Value of `this` is not a URLSearchParams');
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -947,7 +949,7 @@ function merge(out, start, mid, end, lBuffer, rBuffer) {
|
|||||||
|
|
||||||
defineIDLClass(URLSearchParams.prototype, 'URLSearchParams', {
|
defineIDLClass(URLSearchParams.prototype, 'URLSearchParams', {
|
||||||
append(name, value) {
|
append(name, value) {
|
||||||
if (!this || !(this instanceof URLSearchParams)) {
|
if (!this || !this[searchParams] || this[searchParams][searchParams]) {
|
||||||
throw new TypeError('Value of `this` is not a URLSearchParams');
|
throw new TypeError('Value of `this` is not a URLSearchParams');
|
||||||
}
|
}
|
||||||
if (arguments.length < 2) {
|
if (arguments.length < 2) {
|
||||||
@ -961,7 +963,7 @@ defineIDLClass(URLSearchParams.prototype, 'URLSearchParams', {
|
|||||||
},
|
},
|
||||||
|
|
||||||
delete(name) {
|
delete(name) {
|
||||||
if (!this || !(this instanceof URLSearchParams)) {
|
if (!this || !this[searchParams] || this[searchParams][searchParams]) {
|
||||||
throw new TypeError('Value of `this` is not a URLSearchParams');
|
throw new TypeError('Value of `this` is not a URLSearchParams');
|
||||||
}
|
}
|
||||||
if (arguments.length < 1) {
|
if (arguments.length < 1) {
|
||||||
@ -982,7 +984,7 @@ defineIDLClass(URLSearchParams.prototype, 'URLSearchParams', {
|
|||||||
},
|
},
|
||||||
|
|
||||||
get(name) {
|
get(name) {
|
||||||
if (!this || !(this instanceof URLSearchParams)) {
|
if (!this || !this[searchParams] || this[searchParams][searchParams]) {
|
||||||
throw new TypeError('Value of `this` is not a URLSearchParams');
|
throw new TypeError('Value of `this` is not a URLSearchParams');
|
||||||
}
|
}
|
||||||
if (arguments.length < 1) {
|
if (arguments.length < 1) {
|
||||||
@ -1000,7 +1002,7 @@ defineIDLClass(URLSearchParams.prototype, 'URLSearchParams', {
|
|||||||
},
|
},
|
||||||
|
|
||||||
getAll(name) {
|
getAll(name) {
|
||||||
if (!this || !(this instanceof URLSearchParams)) {
|
if (!this || !this[searchParams] || this[searchParams][searchParams]) {
|
||||||
throw new TypeError('Value of `this` is not a URLSearchParams');
|
throw new TypeError('Value of `this` is not a URLSearchParams');
|
||||||
}
|
}
|
||||||
if (arguments.length < 1) {
|
if (arguments.length < 1) {
|
||||||
@ -1019,7 +1021,7 @@ defineIDLClass(URLSearchParams.prototype, 'URLSearchParams', {
|
|||||||
},
|
},
|
||||||
|
|
||||||
has(name) {
|
has(name) {
|
||||||
if (!this || !(this instanceof URLSearchParams)) {
|
if (!this || !this[searchParams] || this[searchParams][searchParams]) {
|
||||||
throw new TypeError('Value of `this` is not a URLSearchParams');
|
throw new TypeError('Value of `this` is not a URLSearchParams');
|
||||||
}
|
}
|
||||||
if (arguments.length < 1) {
|
if (arguments.length < 1) {
|
||||||
@ -1037,7 +1039,7 @@ defineIDLClass(URLSearchParams.prototype, 'URLSearchParams', {
|
|||||||
},
|
},
|
||||||
|
|
||||||
set(name, value) {
|
set(name, value) {
|
||||||
if (!this || !(this instanceof URLSearchParams)) {
|
if (!this || !this[searchParams] || this[searchParams][searchParams]) {
|
||||||
throw new TypeError('Value of `this` is not a URLSearchParams');
|
throw new TypeError('Value of `this` is not a URLSearchParams');
|
||||||
}
|
}
|
||||||
if (arguments.length < 2) {
|
if (arguments.length < 2) {
|
||||||
@ -1125,7 +1127,7 @@ defineIDLClass(URLSearchParams.prototype, 'URLSearchParams', {
|
|||||||
// Define entries here rather than [Symbol.iterator] as the function name
|
// Define entries here rather than [Symbol.iterator] as the function name
|
||||||
// must be set to `entries`.
|
// must be set to `entries`.
|
||||||
entries() {
|
entries() {
|
||||||
if (!this || !(this instanceof URLSearchParams)) {
|
if (!this || !this[searchParams] || this[searchParams][searchParams]) {
|
||||||
throw new TypeError('Value of `this` is not a URLSearchParams');
|
throw new TypeError('Value of `this` is not a URLSearchParams');
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1133,7 +1135,7 @@ defineIDLClass(URLSearchParams.prototype, 'URLSearchParams', {
|
|||||||
},
|
},
|
||||||
|
|
||||||
forEach(callback, thisArg = undefined) {
|
forEach(callback, thisArg = undefined) {
|
||||||
if (!this || !(this instanceof URLSearchParams)) {
|
if (!this || !this[searchParams] || this[searchParams][searchParams]) {
|
||||||
throw new TypeError('Value of `this` is not a URLSearchParams');
|
throw new TypeError('Value of `this` is not a URLSearchParams');
|
||||||
}
|
}
|
||||||
if (typeof callback !== 'function') {
|
if (typeof callback !== 'function') {
|
||||||
@ -1155,7 +1157,7 @@ defineIDLClass(URLSearchParams.prototype, 'URLSearchParams', {
|
|||||||
|
|
||||||
// https://heycam.github.io/webidl/#es-iterable
|
// https://heycam.github.io/webidl/#es-iterable
|
||||||
keys() {
|
keys() {
|
||||||
if (!this || !(this instanceof URLSearchParams)) {
|
if (!this || !this[searchParams] || this[searchParams][searchParams]) {
|
||||||
throw new TypeError('Value of `this` is not a URLSearchParams');
|
throw new TypeError('Value of `this` is not a URLSearchParams');
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1163,7 +1165,7 @@ defineIDLClass(URLSearchParams.prototype, 'URLSearchParams', {
|
|||||||
},
|
},
|
||||||
|
|
||||||
values() {
|
values() {
|
||||||
if (!this || !(this instanceof URLSearchParams)) {
|
if (!this || !this[searchParams] || this[searchParams][searchParams]) {
|
||||||
throw new TypeError('Value of `this` is not a URLSearchParams');
|
throw new TypeError('Value of `this` is not a URLSearchParams');
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1173,7 +1175,7 @@ defineIDLClass(URLSearchParams.prototype, 'URLSearchParams', {
|
|||||||
// https://heycam.github.io/webidl/#es-stringifier
|
// https://heycam.github.io/webidl/#es-stringifier
|
||||||
// https://url.spec.whatwg.org/#urlsearchparams-stringification-behavior
|
// https://url.spec.whatwg.org/#urlsearchparams-stringification-behavior
|
||||||
toString() {
|
toString() {
|
||||||
if (!this || !(this instanceof URLSearchParams)) {
|
if (!this || !this[searchParams] || this[searchParams][searchParams]) {
|
||||||
throw new TypeError('Value of `this` is not a URLSearchParams');
|
throw new TypeError('Value of `this` is not a URLSearchParams');
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1275,8 +1277,10 @@ defineIDLClass(URLSearchParamsIteratorPrototype, 'URLSearchParamsIterator', {
|
|||||||
});
|
});
|
||||||
|
|
||||||
function originFor(url, base) {
|
function originFor(url, base) {
|
||||||
if (!(url instanceof URL))
|
if (url != undefined &&
|
||||||
|
(!url[searchParams] || !url[searchParams][searchParams])) {
|
||||||
url = new URL(url, base);
|
url = new URL(url, base);
|
||||||
|
}
|
||||||
var origin;
|
var origin;
|
||||||
const protocol = url.protocol;
|
const protocol = url.protocol;
|
||||||
switch (protocol) {
|
switch (protocol) {
|
||||||
@ -1399,8 +1403,10 @@ function getPathFromURLPosix(url) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function getPathFromURL(path) {
|
function getPathFromURL(path) {
|
||||||
if (!(path instanceof URL))
|
if (path == undefined || !path[searchParams] ||
|
||||||
|
!path[searchParams][searchParams]) {
|
||||||
return path;
|
return path;
|
||||||
|
}
|
||||||
if (path.protocol !== 'file:')
|
if (path.protocol !== 'file:')
|
||||||
return new TypeError('Only `file:` URLs are supported');
|
return new TypeError('Only `file:` URLs are supported');
|
||||||
return isWindows ? getPathFromURLWin32(path) : getPathFromURLPosix(path);
|
return isWindows ? getPathFromURLWin32(path) : getPathFromURLPosix(path);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user