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‎

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

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

‎benchmark/url/whatwg-url-properties.js‎

Lines changed: 1 addition & 1 deletion
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
functionsetAndGet(n,url,prop,alternative){

‎lib/internal/url.js‎

Lines changed: 21 additions & 15 deletions
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&&!(baseinstanceofURL))
242+
if(base!==undefined&&
243+
(!base[searchParams]||!base[searchParams][searchParams])){
243244
base=newURL(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||!(thisinstanceofURLSearchParams)){
890+
if(!this||!this[searchParams]||this[searchParams][searchParams]){
889891
thrownewTypeError('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||!(thisinstanceofURLSearchParams)){
952+
if(!this||!this[searchParams]||this[searchParams][searchParams]){
951953
thrownewTypeError('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||!(thisinstanceofURLSearchParams)){
966+
if(!this||!this[searchParams]||this[searchParams][searchParams]){
965967
thrownewTypeError('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||!(thisinstanceofURLSearchParams)){
987+
if(!this||!this[searchParams]||this[searchParams][searchParams]){
986988
thrownewTypeError('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||!(thisinstanceofURLSearchParams)){
1005+
if(!this||!this[searchParams]||this[searchParams][searchParams]){
10041006
thrownewTypeError('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||!(thisinstanceofURLSearchParams)){
1024+
if(!this||!this[searchParams]||this[searchParams][searchParams]){
10231025
thrownewTypeError('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||!(thisinstanceofURLSearchParams)){
1042+
if(!this||!this[searchParams]||this[searchParams][searchParams]){
10411043
thrownewTypeError('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||!(thisinstanceofURLSearchParams)){
1130+
if(!this||!this[searchParams]||this[searchParams][searchParams]){
11291131
thrownewTypeError('Value of `this` is not a URLSearchParams');
11301132
}
11311133

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

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

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

11621164
returncreateSearchParamsIterator(this,'key');
11631165
},
11641166

11651167
values(){
1166-
if(!this||!(thisinstanceofURLSearchParams)){
1168+
if(!this||!this[searchParams]||this[searchParams][searchParams]){
11671169
thrownewTypeError('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||!(thisinstanceofURLSearchParams)){
1178+
if(!this||!this[searchParams]||this[searchParams][searchParams]){
11771179
thrownewTypeError('Value of `this` is not a URLSearchParams');
11781180
}
11791181

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

12771279
functionoriginFor(url,base){
1278-
if(!(urlinstanceofURL))
1280+
if(url!=undefined&&
1281+
(!url[searchParams]||!url[searchParams][searchParams])){
12791282
url=newURL(url,base);
1283+
}
12801284
varorigin;
12811285
constprotocol=url.protocol;
12821286
switch(protocol){
@@ -1399,8 +1403,10 @@ function getPathFromURLPosix(url){
13991403
}
14001404

14011405
functiongetPathFromURL(path){
1402-
if(!(pathinstanceofURL))
1406+
if(path==undefined||!path[searchParams]||
1407+
!path[searchParams][searchParams]){
14031408
returnpath;
1409+
}
14041410
if(path.protocol!=='file:')
14051411
returnnewTypeError('Only `file:` URLs are supported');
14061412
returnisWindows ? getPathFromURLWin32(path) : getPathFromURLPosix(path);

0 commit comments

Comments
(0)