@@ -250,7 +250,7 @@ class URLSearchParams {
250
250
} else {
251
251
// https://url.spec.whatwg.org/#dom-urlsearchparams-urlsearchparams
252
252
init = toUSVString ( init ) ;
253
- initSearchParams ( this , init ) ;
253
+ this [ searchParams ] = init ? parseParams ( init ) : [ ] ;
254
254
}
255
255
}
256
256
@@ -557,7 +557,7 @@ ObjectDefineProperties(URLSearchParams.prototype, {
557
557
} ,
558
558
} ) ;
559
559
560
- function isURLThis ( self ) {
560
+ function isURL ( self ) {
561
561
return self != null && ObjectPrototypeHasOwnProperty ( self , context ) ;
562
562
}
563
563
@@ -625,160 +625,161 @@ class URL {
625
625
ctx . password = password ;
626
626
ctx . port = port ;
627
627
ctx . hash = hash ;
628
- if ( ! this [ searchParams ] ) { // Invoked from URL constructor
629
- this [ searchParams ] = new URLSearchParams ( ) ;
630
- this [ searchParams ] [ context ] = this ;
628
+ if ( this [ searchParams ] ) {
629
+ this [ searchParams ] [ searchParams ] = parseParams ( search ) ;
631
630
}
632
- initSearchParams ( this [ searchParams ] , ctx . search ) ;
633
631
} ;
634
632
635
633
toString ( ) {
636
- if ( ! isURLThis ( this ) )
634
+ if ( ! isURL ( this ) )
637
635
throw new ERR_INVALID_THIS ( 'URL' ) ;
638
636
return this [ context ] . href ;
639
637
}
640
638
641
639
get href ( ) {
642
- if ( ! isURLThis ( this ) )
640
+ if ( ! isURL ( this ) )
643
641
throw new ERR_INVALID_THIS ( 'URL' ) ;
644
642
return this [ context ] . href ;
645
643
}
646
644
647
645
set href ( value ) {
648
- if ( ! isURLThis ( this ) )
646
+ if ( ! isURL ( this ) )
649
647
throw new ERR_INVALID_THIS ( 'URL' ) ;
650
648
const valid = updateUrl ( this [ context ] . href , updateActions . kHref , `${ value } ` , this . #onParseComplete) ;
651
649
if ( ! valid ) { throw ERR_INVALID_URL ( `${ value } ` ) ; }
652
650
}
653
651
654
652
// readonly
655
653
get origin ( ) {
656
- if ( ! isURLThis ( this ) )
654
+ if ( ! isURL ( this ) )
657
655
throw new ERR_INVALID_THIS ( 'URL' ) ;
658
656
return this [ context ] . origin ;
659
657
}
660
658
661
659
get protocol ( ) {
662
- if ( ! isURLThis ( this ) )
660
+ if ( ! isURL ( this ) )
663
661
throw new ERR_INVALID_THIS ( 'URL' ) ;
664
662
return this [ context ] . protocol ;
665
663
}
666
664
667
665
set protocol ( value ) {
668
- if ( ! isURLThis ( this ) )
666
+ if ( ! isURL ( this ) )
669
667
throw new ERR_INVALID_THIS ( 'URL' ) ;
670
668
updateUrl ( this [ context ] . href , updateActions . kProtocol , `${ value } ` , this . #onParseComplete) ;
671
669
}
672
670
673
671
get username ( ) {
674
- if ( ! isURLThis ( this ) )
672
+ if ( ! isURL ( this ) )
675
673
throw new ERR_INVALID_THIS ( 'URL' ) ;
676
674
return this [ context ] . username ;
677
675
}
678
676
679
677
set username ( value ) {
680
- if ( ! isURLThis ( this ) )
678
+ if ( ! isURL ( this ) )
681
679
throw new ERR_INVALID_THIS ( 'URL' ) ;
682
680
updateUrl ( this [ context ] . href , updateActions . kUsername , `${ value } ` , this . #onParseComplete) ;
683
681
}
684
682
685
683
get password ( ) {
686
- if ( ! isURLThis ( this ) )
684
+ if ( ! isURL ( this ) )
687
685
throw new ERR_INVALID_THIS ( 'URL' ) ;
688
686
return this [ context ] . password ;
689
687
}
690
688
691
689
set password ( value ) {
692
- if ( ! isURLThis ( this ) )
690
+ if ( ! isURL ( this ) )
693
691
throw new ERR_INVALID_THIS ( 'URL' ) ;
694
692
updateUrl ( this [ context ] . href , updateActions . kPassword , `${ value } ` , this . #onParseComplete) ;
695
693
}
696
694
697
695
get host ( ) {
698
- if ( ! isURLThis ( this ) )
696
+ if ( ! isURL ( this ) )
699
697
throw new ERR_INVALID_THIS ( 'URL' ) ;
700
698
const port = this [ context ] . port ;
701
699
const suffix = port . length > 0 ? `:${ port } ` : '' ;
702
700
return this [ context ] . hostname + suffix ;
703
701
}
704
702
705
703
set host ( value ) {
706
- if ( ! isURLThis ( this ) )
704
+ if ( ! isURL ( this ) )
707
705
throw new ERR_INVALID_THIS ( 'URL' ) ;
708
706
updateUrl ( this [ context ] . href , updateActions . kHost , `${ value } ` , this . #onParseComplete) ;
709
707
}
710
708
711
709
get hostname ( ) {
712
- if ( ! isURLThis ( this ) )
710
+ if ( ! isURL ( this ) )
713
711
throw new ERR_INVALID_THIS ( 'URL' ) ;
714
712
return this [ context ] . hostname ;
715
713
}
716
714
717
715
set hostname ( value ) {
718
- if ( ! isURLThis ( this ) )
716
+ if ( ! isURL ( this ) )
719
717
throw new ERR_INVALID_THIS ( 'URL' ) ;
720
718
updateUrl ( this [ context ] . href , updateActions . kHostname , `${ value } ` , this . #onParseComplete) ;
721
719
}
722
720
723
721
get port ( ) {
724
- if ( ! isURLThis ( this ) )
722
+ if ( ! isURL ( this ) )
725
723
throw new ERR_INVALID_THIS ( 'URL' ) ;
726
724
return this [ context ] . port ;
727
725
}
728
726
729
727
set port ( value ) {
730
- if ( ! isURLThis ( this ) )
728
+ if ( ! isURL ( this ) )
731
729
throw new ERR_INVALID_THIS ( 'URL' ) ;
732
730
updateUrl ( this [ context ] . href , updateActions . kPort , `${ value } ` , this . #onParseComplete) ;
733
731
}
734
732
735
733
get pathname ( ) {
736
- if ( ! isURLThis ( this ) )
734
+ if ( ! isURL ( this ) )
737
735
throw new ERR_INVALID_THIS ( 'URL' ) ;
738
736
return this [ context ] . pathname ;
739
737
}
740
738
741
739
set pathname ( value ) {
742
- if ( ! isURLThis ( this ) )
740
+ if ( ! isURL ( this ) )
743
741
throw new ERR_INVALID_THIS ( 'URL' ) ;
744
742
updateUrl ( this [ context ] . href , updateActions . kPathname , `${ value } ` , this . #onParseComplete) ;
745
743
}
746
744
747
745
get search ( ) {
748
- if ( ! isURLThis ( this ) )
746
+ if ( ! isURL ( this ) )
749
747
throw new ERR_INVALID_THIS ( 'URL' ) ;
750
748
return this [ context ] . search ;
751
749
}
752
750
753
- set search ( search ) {
754
- if ( ! isURLThis ( this ) )
751
+ set search ( value ) {
752
+ if ( ! isURL ( this ) )
755
753
throw new ERR_INVALID_THIS ( 'URL' ) ;
756
- search = toUSVString ( search ) ;
757
- updateUrl ( this [ context ] . href , updateActions . kSearch , search , this . #onParseComplete) ;
758
- initSearchParams ( this [ searchParams ] , this [ context ] . search ) ;
754
+ updateUrl ( this [ context ] . href , updateActions . kSearch , toUSVString ( value ) , this . #onParseComplete) ;
759
755
}
760
756
761
757
// readonly
762
758
get searchParams ( ) {
763
- if ( ! isURLThis ( this ) )
759
+ if ( ! isURL ( this ) )
764
760
throw new ERR_INVALID_THIS ( 'URL' ) ;
761
+ // Create URLSearchParams on demand to greatly improve the URL performance.
762
+ if ( this [ searchParams ] == null ) {
763
+ this [ searchParams ] = new URLSearchParams ( this [ context ] . search ) ;
764
+ this [ searchParams ] [ context ] = this ;
765
+ }
765
766
return this [ searchParams ] ;
766
767
}
767
768
768
769
get hash ( ) {
769
- if ( ! isURLThis ( this ) )
770
+ if ( ! isURL ( this ) )
770
771
throw new ERR_INVALID_THIS ( 'URL' ) ;
771
772
return this [ context ] . hash ;
772
773
}
773
774
774
775
set hash ( value ) {
775
- if ( ! isURLThis ( this ) )
776
+ if ( ! isURL ( this ) )
776
777
throw new ERR_INVALID_THIS ( 'URL' ) ;
777
778
updateUrl ( this [ context ] . href , updateActions . kHash , `${ value } ` , this . #onParseComplete) ;
778
779
}
779
780
780
781
toJSON ( ) {
781
- if ( ! isURLThis ( this ) )
782
+ if ( ! isURL ( this ) )
782
783
throw new ERR_INVALID_THIS ( 'URL' ) ;
783
784
return this [ context ] . href ;
784
785
}
@@ -836,14 +837,6 @@ ObjectDefineProperties(URL, {
836
837
revokeObjectURL : kEnumerableProperty ,
837
838
} ) ;
838
839
839
- function initSearchParams ( url , init ) {
840
- if ( ! init ) {
841
- url [ searchParams ] = [ ] ;
842
- return ;
843
- }
844
- url [ searchParams ] = parseParams ( init ) ;
845
- }
846
-
847
840
// application/x-www-form-urlencoded parser
848
841
// Ref: https://url.spec.whatwg.org/#concept-urlencoded-parser
849
842
function parseParams ( qs ) {
@@ -1168,9 +1161,9 @@ function urlToHttpOptions(url) {
1168
1161
__proto__ : null ,
1169
1162
...url , // In case the url object was extended by the user.
1170
1163
protocol : url . protocol ,
1171
- hostname : typeof url . hostname === 'string' && StringPrototypeStartsWith ( hostname , '[' ) ?
1172
- StringPrototypeSlice ( hostname , 1 , - 1 ) :
1173
- hostname ,
1164
+ hostname : url . hostname && StringPrototypeStartsWith ( url . hostname , '[' ) ?
1165
+ StringPrototypeSlice ( url . hostname , 1 , - 1 ) :
1166
+ url . hostname ,
1174
1167
hash : url . hash ,
1175
1168
search : search ,
1176
1169
pathname : pathname ,
@@ -1339,6 +1332,6 @@ module.exports = {
1339
1332
domainToASCII,
1340
1333
domainToUnicode,
1341
1334
urlToHttpOptions,
1342
- searchParamsSymbol : searchParams ,
1343
1335
encodeStr,
1336
+ isURL,
1344
1337
} ;
0 commit comments