@@ -224,7 +224,7 @@ class URLSearchParams {
224
224
} else {
225
225
// USVString
226
226
init = toUSVString ( init ) ;
227
- initSearchParams ( this , init ) ;
227
+ this [ searchParams ] = init ? parseParams ( init ) : [ ] ;
228
228
}
229
229
230
230
// "associated url object"
@@ -534,7 +534,7 @@ ObjectDefineProperties(URLSearchParams.prototype, {
534
534
} ,
535
535
} ) ;
536
536
537
- function isURLThis ( self ) {
537
+ function isURL ( self ) {
538
538
return self != null && ObjectPrototypeHasOwnProperty ( self , context ) ;
539
539
}
540
540
@@ -602,160 +602,161 @@ class URL {
602
602
ctx . password = password ;
603
603
ctx . port = port ;
604
604
ctx . hash = hash ;
605
- if ( ! this [ searchParams ] ) { // Invoked from URL constructor
606
- this [ searchParams ] = new URLSearchParams ( ) ;
607
- this [ searchParams ] [ context ] = this ;
605
+ if ( this [ searchParams ] ) {
606
+ this [ searchParams ] [ searchParams ] = parseParams ( search ) ;
608
607
}
609
- initSearchParams ( this [ searchParams ] , ctx . search ) ;
610
608
} ;
611
609
612
610
toString ( ) {
613
- if ( ! isURLThis ( this ) )
611
+ if ( ! isURL ( this ) )
614
612
throw new ERR_INVALID_THIS ( 'URL' ) ;
615
613
return this [ context ] . href ;
616
614
}
617
615
618
616
get href ( ) {
619
- if ( ! isURLThis ( this ) )
617
+ if ( ! isURL ( this ) )
620
618
throw new ERR_INVALID_THIS ( 'URL' ) ;
621
619
return this [ context ] . href ;
622
620
}
623
621
624
622
set href ( value ) {
625
- if ( ! isURLThis ( this ) )
623
+ if ( ! isURL ( this ) )
626
624
throw new ERR_INVALID_THIS ( 'URL' ) ;
627
625
const valid = updateUrl ( this [ context ] . href , updateActions . kHref , `${ value } ` , this . #onParseComplete) ;
628
626
if ( ! valid ) { throw ERR_INVALID_URL ( `${ value } ` ) ; }
629
627
}
630
628
631
629
// readonly
632
630
get origin ( ) {
633
- if ( ! isURLThis ( this ) )
631
+ if ( ! isURL ( this ) )
634
632
throw new ERR_INVALID_THIS ( 'URL' ) ;
635
633
return this [ context ] . origin ;
636
634
}
637
635
638
636
get protocol ( ) {
639
- if ( ! isURLThis ( this ) )
637
+ if ( ! isURL ( this ) )
640
638
throw new ERR_INVALID_THIS ( 'URL' ) ;
641
639
return this [ context ] . protocol ;
642
640
}
643
641
644
642
set protocol ( value ) {
645
- if ( ! isURLThis ( this ) )
643
+ if ( ! isURL ( this ) )
646
644
throw new ERR_INVALID_THIS ( 'URL' ) ;
647
645
updateUrl ( this [ context ] . href , updateActions . kProtocol , `${ value } ` , this . #onParseComplete) ;
648
646
}
649
647
650
648
get username ( ) {
651
- if ( ! isURLThis ( this ) )
649
+ if ( ! isURL ( this ) )
652
650
throw new ERR_INVALID_THIS ( 'URL' ) ;
653
651
return this [ context ] . username ;
654
652
}
655
653
656
654
set username ( value ) {
657
- if ( ! isURLThis ( this ) )
655
+ if ( ! isURL ( this ) )
658
656
throw new ERR_INVALID_THIS ( 'URL' ) ;
659
657
updateUrl ( this [ context ] . href , updateActions . kUsername , `${ value } ` , this . #onParseComplete) ;
660
658
}
661
659
662
660
get password ( ) {
663
- if ( ! isURLThis ( this ) )
661
+ if ( ! isURL ( this ) )
664
662
throw new ERR_INVALID_THIS ( 'URL' ) ;
665
663
return this [ context ] . password ;
666
664
}
667
665
668
666
set password ( value ) {
669
- if ( ! isURLThis ( this ) )
667
+ if ( ! isURL ( this ) )
670
668
throw new ERR_INVALID_THIS ( 'URL' ) ;
671
669
updateUrl ( this [ context ] . href , updateActions . kPassword , `${ value } ` , this . #onParseComplete) ;
672
670
}
673
671
674
672
get host ( ) {
675
- if ( ! isURLThis ( this ) )
673
+ if ( ! isURL ( this ) )
676
674
throw new ERR_INVALID_THIS ( 'URL' ) ;
677
675
const port = this [ context ] . port ;
678
676
const suffix = port . length > 0 ? `:${ port } ` : '' ;
679
677
return this [ context ] . hostname + suffix ;
680
678
}
681
679
682
680
set host ( value ) {
683
- if ( ! isURLThis ( this ) )
681
+ if ( ! isURL ( this ) )
684
682
throw new ERR_INVALID_THIS ( 'URL' ) ;
685
683
updateUrl ( this [ context ] . href , updateActions . kHost , `${ value } ` , this . #onParseComplete) ;
686
684
}
687
685
688
686
get hostname ( ) {
689
- if ( ! isURLThis ( this ) )
687
+ if ( ! isURL ( this ) )
690
688
throw new ERR_INVALID_THIS ( 'URL' ) ;
691
689
return this [ context ] . hostname ;
692
690
}
693
691
694
692
set hostname ( value ) {
695
- if ( ! isURLThis ( this ) )
693
+ if ( ! isURL ( this ) )
696
694
throw new ERR_INVALID_THIS ( 'URL' ) ;
697
695
updateUrl ( this [ context ] . href , updateActions . kHostname , `${ value } ` , this . #onParseComplete) ;
698
696
}
699
697
700
698
get port ( ) {
701
- if ( ! isURLThis ( this ) )
699
+ if ( ! isURL ( this ) )
702
700
throw new ERR_INVALID_THIS ( 'URL' ) ;
703
701
return this [ context ] . port ;
704
702
}
705
703
706
704
set port ( value ) {
707
- if ( ! isURLThis ( this ) )
705
+ if ( ! isURL ( this ) )
708
706
throw new ERR_INVALID_THIS ( 'URL' ) ;
709
707
updateUrl ( this [ context ] . href , updateActions . kPort , `${ value } ` , this . #onParseComplete) ;
710
708
}
711
709
712
710
get pathname ( ) {
713
- if ( ! isURLThis ( this ) )
711
+ if ( ! isURL ( this ) )
714
712
throw new ERR_INVALID_THIS ( 'URL' ) ;
715
713
return this [ context ] . pathname ;
716
714
}
717
715
718
716
set pathname ( value ) {
719
- if ( ! isURLThis ( this ) )
717
+ if ( ! isURL ( this ) )
720
718
throw new ERR_INVALID_THIS ( 'URL' ) ;
721
719
updateUrl ( this [ context ] . href , updateActions . kPathname , `${ value } ` , this . #onParseComplete) ;
722
720
}
723
721
724
722
get search ( ) {
725
- if ( ! isURLThis ( this ) )
723
+ if ( ! isURL ( this ) )
726
724
throw new ERR_INVALID_THIS ( 'URL' ) ;
727
725
return this [ context ] . search ;
728
726
}
729
727
730
- set search ( search ) {
731
- if ( ! isURLThis ( this ) )
728
+ set search ( value ) {
729
+ if ( ! isURL ( this ) )
732
730
throw new ERR_INVALID_THIS ( 'URL' ) ;
733
- search = toUSVString ( search ) ;
734
- updateUrl ( this [ context ] . href , updateActions . kSearch , search , this . #onParseComplete) ;
735
- initSearchParams ( this [ searchParams ] , this [ context ] . search ) ;
731
+ updateUrl ( this [ context ] . href , updateActions . kSearch , toUSVString ( value ) , this . #onParseComplete) ;
736
732
}
737
733
738
734
// readonly
739
735
get searchParams ( ) {
740
- if ( ! isURLThis ( this ) )
736
+ if ( ! isURL ( this ) )
741
737
throw new ERR_INVALID_THIS ( 'URL' ) ;
738
+ // Create URLSearchParams on demand to greatly improve the URL performance.
739
+ if ( this [ searchParams ] == null ) {
740
+ this [ searchParams ] = new URLSearchParams ( this [ context ] . search ) ;
741
+ this [ searchParams ] [ context ] = this ;
742
+ }
742
743
return this [ searchParams ] ;
743
744
}
744
745
745
746
get hash ( ) {
746
- if ( ! isURLThis ( this ) )
747
+ if ( ! isURL ( this ) )
747
748
throw new ERR_INVALID_THIS ( 'URL' ) ;
748
749
return this [ context ] . hash ;
749
750
}
750
751
751
752
set hash ( value ) {
752
- if ( ! isURLThis ( this ) )
753
+ if ( ! isURL ( this ) )
753
754
throw new ERR_INVALID_THIS ( 'URL' ) ;
754
755
updateUrl ( this [ context ] . href , updateActions . kHash , `${ value } ` , this . #onParseComplete) ;
755
756
}
756
757
757
758
toJSON ( ) {
758
- if ( ! isURLThis ( this ) )
759
+ if ( ! isURL ( this ) )
759
760
throw new ERR_INVALID_THIS ( 'URL' ) ;
760
761
return this [ context ] . href ;
761
762
}
@@ -813,14 +814,6 @@ ObjectDefineProperties(URL, {
813
814
revokeObjectURL : kEnumerableProperty ,
814
815
} ) ;
815
816
816
- function initSearchParams ( url , init ) {
817
- if ( ! init ) {
818
- url [ searchParams ] = [ ] ;
819
- return ;
820
- }
821
- url [ searchParams ] = parseParams ( init ) ;
822
- }
823
-
824
817
// application/x-www-form-urlencoded parser
825
818
// Ref: https://url.spec.whatwg.org/#concept-urlencoded-parser
826
819
function parseParams ( qs ) {
@@ -1139,8 +1132,7 @@ function domainToUnicode(domain) {
1139
1132
function urlToHttpOptions ( url ) {
1140
1133
const options = {
1141
1134
protocol : url . protocol ,
1142
- hostname : typeof url . hostname === 'string' &&
1143
- StringPrototypeStartsWith ( url . hostname , '[' ) ?
1135
+ hostname : url . hostname && StringPrototypeStartsWith ( url . hostname , '[' ) ?
1144
1136
StringPrototypeSlice ( url . hostname , 1 , - 1 ) :
1145
1137
url . hostname ,
1146
1138
hash : url . hash ,
@@ -1311,6 +1303,6 @@ module.exports = {
1311
1303
domainToASCII,
1312
1304
domainToUnicode,
1313
1305
urlToHttpOptions,
1314
- searchParamsSymbol : searchParams ,
1315
1306
encodeStr,
1307
+ isURL,
1316
1308
} ;
0 commit comments