Skip to content

Commit b50a84a

Browse files
mscdexevanlucas
authored andcommitted
url: avoid instanceof for WHATWG URL
PR-URL: #12507 Reviewed-By: James M Snell <[email protected]>
1 parent b9b93f2 commit b50a84a

File tree

3 files changed

+23
-17
lines changed

3 files changed

+23
-17
lines changed

benchmark/url/url-searchparams-read.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const { URLSearchParams } = require('url');
55
const bench = common.createBenchmark(main, {
66
method: ['get', 'getAll', 'has'],
77
param: ['one', 'two', 'three', 'nonexistent'],
8-
n: [1e6]
8+
n: [2e7]
99
});
1010

1111
const str = 'one=single&two=first&three=first&two=2nd&three=2nd&three=3rd';

benchmark/url/whatwg-url-properties.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const bench = common.createBenchmark(main, {
88
prop: ['href', 'origin', 'protocol',
99
'username', 'password', 'host', 'hostname', 'port',
1010
'pathname', 'search', 'searchParams', 'hash'],
11-
n: [1e4]
11+
n: [3e5]
1212
});
1313

1414
function setAndGet(n, url, prop, alternative) {

lib/internal/url.js

+21-15
Original file line numberDiff line numberDiff line change
@@ -239,8 +239,10 @@ class URL {
239239
constructor(input, base) {
240240
// toUSVString is not needed.
241241
input = `${input}`;
242-
if (base !== undefined && !(base instanceof URL))
242+
if (base !== undefined &&
243+
(!base[searchParams] || !base[searchParams][searchParams])) {
243244
base = new URL(base);
245+
}
244246
parse(this, input, base);
245247
}
246248

@@ -885,7 +887,7 @@ class URLSearchParams {
885887
}
886888

887889
[util.inspect.custom](recurseTimes, ctx) {
888-
if (!this || !(this instanceof URLSearchParams)) {
890+
if (!this || !this[searchParams] || this[searchParams][searchParams]) {
889891
throw new TypeError('Value of `this` is not a URLSearchParams');
890892
}
891893

@@ -947,7 +949,7 @@ function merge(out, start, mid, end, lBuffer, rBuffer) {
947949

948950
defineIDLClass(URLSearchParams.prototype, 'URLSearchParams', {
949951
append(name, value) {
950-
if (!this || !(this instanceof URLSearchParams)) {
952+
if (!this || !this[searchParams] || this[searchParams][searchParams]) {
951953
throw new TypeError('Value of `this` is not a URLSearchParams');
952954
}
953955
if (arguments.length < 2) {
@@ -961,7 +963,7 @@ defineIDLClass(URLSearchParams.prototype, 'URLSearchParams', {
961963
},
962964

963965
delete(name) {
964-
if (!this || !(this instanceof URLSearchParams)) {
966+
if (!this || !this[searchParams] || this[searchParams][searchParams]) {
965967
throw new TypeError('Value of `this` is not a URLSearchParams');
966968
}
967969
if (arguments.length < 1) {
@@ -982,7 +984,7 @@ defineIDLClass(URLSearchParams.prototype, 'URLSearchParams', {
982984
},
983985

984986
get(name) {
985-
if (!this || !(this instanceof URLSearchParams)) {
987+
if (!this || !this[searchParams] || this[searchParams][searchParams]) {
986988
throw new TypeError('Value of `this` is not a URLSearchParams');
987989
}
988990
if (arguments.length < 1) {
@@ -1000,7 +1002,7 @@ defineIDLClass(URLSearchParams.prototype, 'URLSearchParams', {
10001002
},
10011003

10021004
getAll(name) {
1003-
if (!this || !(this instanceof URLSearchParams)) {
1005+
if (!this || !this[searchParams] || this[searchParams][searchParams]) {
10041006
throw new TypeError('Value of `this` is not a URLSearchParams');
10051007
}
10061008
if (arguments.length < 1) {
@@ -1019,7 +1021,7 @@ defineIDLClass(URLSearchParams.prototype, 'URLSearchParams', {
10191021
},
10201022

10211023
has(name) {
1022-
if (!this || !(this instanceof URLSearchParams)) {
1024+
if (!this || !this[searchParams] || this[searchParams][searchParams]) {
10231025
throw new TypeError('Value of `this` is not a URLSearchParams');
10241026
}
10251027
if (arguments.length < 1) {
@@ -1037,7 +1039,7 @@ defineIDLClass(URLSearchParams.prototype, 'URLSearchParams', {
10371039
},
10381040

10391041
set(name, value) {
1040-
if (!this || !(this instanceof URLSearchParams)) {
1042+
if (!this || !this[searchParams] || this[searchParams][searchParams]) {
10411043
throw new TypeError('Value of `this` is not a URLSearchParams');
10421044
}
10431045
if (arguments.length < 2) {
@@ -1125,15 +1127,15 @@ defineIDLClass(URLSearchParams.prototype, 'URLSearchParams', {
11251127
// Define entries here rather than [Symbol.iterator] as the function name
11261128
// must be set to `entries`.
11271129
entries() {
1128-
if (!this || !(this instanceof URLSearchParams)) {
1130+
if (!this || !this[searchParams] || this[searchParams][searchParams]) {
11291131
throw new TypeError('Value of `this` is not a URLSearchParams');
11301132
}
11311133

11321134
return createSearchParamsIterator(this, 'key+value');
11331135
},
11341136

11351137
forEach(callback, thisArg = undefined) {
1136-
if (!this || !(this instanceof URLSearchParams)) {
1138+
if (!this || !this[searchParams] || this[searchParams][searchParams]) {
11371139
throw new TypeError('Value of `this` is not a URLSearchParams');
11381140
}
11391141
if (typeof callback !== 'function') {
@@ -1155,15 +1157,15 @@ defineIDLClass(URLSearchParams.prototype, 'URLSearchParams', {
11551157

11561158
// https://heycam.github.io/webidl/#es-iterable
11571159
keys() {
1158-
if (!this || !(this instanceof URLSearchParams)) {
1160+
if (!this || !this[searchParams] || this[searchParams][searchParams]) {
11591161
throw new TypeError('Value of `this` is not a URLSearchParams');
11601162
}
11611163

11621164
return createSearchParamsIterator(this, 'key');
11631165
},
11641166

11651167
values() {
1166-
if (!this || !(this instanceof URLSearchParams)) {
1168+
if (!this || !this[searchParams] || this[searchParams][searchParams]) {
11671169
throw new TypeError('Value of `this` is not a URLSearchParams');
11681170
}
11691171

@@ -1173,7 +1175,7 @@ defineIDLClass(URLSearchParams.prototype, 'URLSearchParams', {
11731175
// https://heycam.github.io/webidl/#es-stringifier
11741176
// https://url.spec.whatwg.org/#urlsearchparams-stringification-behavior
11751177
toString() {
1176-
if (!this || !(this instanceof URLSearchParams)) {
1178+
if (!this || !this[searchParams] || this[searchParams][searchParams]) {
11771179
throw new TypeError('Value of `this` is not a URLSearchParams');
11781180
}
11791181

@@ -1275,8 +1277,10 @@ defineIDLClass(URLSearchParamsIteratorPrototype, 'URLSearchParamsIterator', {
12751277
});
12761278

12771279
function originFor(url, base) {
1278-
if (!(url instanceof URL))
1280+
if (url != undefined &&
1281+
(!url[searchParams] || !url[searchParams][searchParams])) {
12791282
url = new URL(url, base);
1283+
}
12801284
var origin;
12811285
const protocol = url.protocol;
12821286
switch (protocol) {
@@ -1399,8 +1403,10 @@ function getPathFromURLPosix(url) {
13991403
}
14001404

14011405
function getPathFromURL(path) {
1402-
if (!(path instanceof URL))
1406+
if (path == undefined || !path[searchParams] ||
1407+
!path[searchParams][searchParams]) {
14031408
return path;
1409+
}
14041410
if (path.protocol !== 'file:')
14051411
return new TypeError('Only `file:` URLs are supported');
14061412
return isWindows ? getPathFromURLWin32(path) : getPathFromURLPosix(path);

0 commit comments

Comments
 (0)