@@ -15,7 +15,6 @@ const {
15
15
ObjectGetOwnPropertySymbols,
16
16
ObjectGetPrototypeOf,
17
17
ObjectKeys,
18
- ObjectPrototypeHasOwnProperty,
19
18
ReflectGetOwnPropertyDescriptor,
20
19
ReflectOwnKeys,
21
20
RegExpPrototypeSymbolReplace,
@@ -535,14 +534,16 @@ ObjectDefineProperties(URLSearchParams.prototype, {
535
534
} ) ;
536
535
537
536
function isURL ( self ) {
538
- return self != null && ObjectPrototypeHasOwnProperty ( self , context ) ;
537
+ return self != null && self . href && self . origin ;
539
538
}
540
539
541
540
class URL {
541
+ #context = new URLContext ( ) ;
542
+ #searchParams;
543
+
542
544
constructor ( input , base = undefined ) {
543
545
// toUSVString is not needed.
544
546
input = `${ input } ` ;
545
- this [ context ] = new URLContext ( ) ;
546
547
547
548
if ( base !== undefined ) {
548
549
base = `${ base } ` ;
@@ -558,11 +559,6 @@ class URL {
558
559
}
559
560
560
561
[ inspect . custom ] ( depth , opts ) {
561
- if ( this == null ||
562
- ObjectGetPrototypeOf ( this [ context ] ) !== URLContext . prototype ) {
563
- throw new ERR_INVALID_THIS ( 'URL' ) ;
564
- }
565
-
566
562
if ( typeof depth === 'number' && depth < 0 )
567
563
return this ;
568
564
@@ -583,182 +579,133 @@ class URL {
583
579
obj . hash = this . hash ;
584
580
585
581
if ( opts . showHidden ) {
586
- obj [ context ] = this [ context ] ;
582
+ obj [ context ] = this . # context;
587
583
}
588
584
589
585
return `${ constructor . name } ${ inspect ( obj , opts ) } ` ;
590
586
}
591
587
592
588
#onParseComplete = ( href , origin , protocol , hostname , pathname ,
593
589
search , username , password , port , hash ) => {
594
- const ctx = this [ context ] ;
595
- ctx . href = href ;
596
- ctx . origin = origin ;
597
- ctx . protocol = protocol ;
598
- ctx . hostname = hostname ;
599
- ctx . pathname = pathname ;
600
- ctx . search = search ;
601
- ctx . username = username ;
602
- ctx . password = password ;
603
- ctx . port = port ;
604
- ctx . hash = hash ;
605
- if ( this [ searchParams ] ) {
606
- this [ searchParams ] [ searchParams ] = parseParams ( search ) ;
590
+ this . #context. href = href ;
591
+ this . #context. origin = origin ;
592
+ this . #context. protocol = protocol ;
593
+ this . #context. hostname = hostname ;
594
+ this . #context. pathname = pathname ;
595
+ this . #context. search = search ;
596
+ this . #context. username = username ;
597
+ this . #context. password = password ;
598
+ this . #context. port = port ;
599
+ this . #context. hash = hash ;
600
+ if ( this . #searchParams) {
601
+ this . #searchParams[ searchParams ] = parseParams ( search ) ;
607
602
}
608
603
} ;
609
604
610
605
toString ( ) {
611
- if ( ! isURL ( this ) )
612
- throw new ERR_INVALID_THIS ( 'URL' ) ;
613
- return this [ context ] . href ;
606
+ return this . #context. href ;
614
607
}
615
608
616
609
get href ( ) {
617
- if ( ! isURL ( this ) )
618
- throw new ERR_INVALID_THIS ( 'URL' ) ;
619
- return this [ context ] . href ;
610
+ return this . #context. href ;
620
611
}
621
612
622
613
set href ( value ) {
623
- if ( ! isURL ( this ) )
624
- throw new ERR_INVALID_THIS ( 'URL' ) ;
625
- const valid = updateUrl ( this [ context ] . href , updateActions . kHref , `${ value } ` , this . #onParseComplete) ;
614
+ const valid = updateUrl ( this . #context. href , updateActions . kHref , `${ value } ` , this . #onParseComplete) ;
626
615
if ( ! valid ) { throw ERR_INVALID_URL ( `${ value } ` ) ; }
627
616
}
628
617
629
618
// readonly
630
619
get origin ( ) {
631
- if ( ! isURL ( this ) )
632
- throw new ERR_INVALID_THIS ( 'URL' ) ;
633
- return this [ context ] . origin ;
620
+ return this . #context. origin ;
634
621
}
635
622
636
623
get protocol ( ) {
637
- if ( ! isURL ( this ) )
638
- throw new ERR_INVALID_THIS ( 'URL' ) ;
639
- return this [ context ] . protocol ;
624
+ return this . #context. protocol ;
640
625
}
641
626
642
627
set protocol ( value ) {
643
- if ( ! isURL ( this ) )
644
- throw new ERR_INVALID_THIS ( 'URL' ) ;
645
- updateUrl ( this [ context ] . href , updateActions . kProtocol , `${ value } ` , this . #onParseComplete) ;
628
+ updateUrl ( this . #context. href , updateActions . kProtocol , `${ value } ` , this . #onParseComplete) ;
646
629
}
647
630
648
631
get username ( ) {
649
- if ( ! isURL ( this ) )
650
- throw new ERR_INVALID_THIS ( 'URL' ) ;
651
- return this [ context ] . username ;
632
+ return this . #context. username ;
652
633
}
653
634
654
635
set username ( value ) {
655
- if ( ! isURL ( this ) )
656
- throw new ERR_INVALID_THIS ( 'URL' ) ;
657
- updateUrl ( this [ context ] . href , updateActions . kUsername , `${ value } ` , this . #onParseComplete) ;
636
+ updateUrl ( this . #context. href , updateActions . kUsername , `${ value } ` , this . #onParseComplete) ;
658
637
}
659
638
660
639
get password ( ) {
661
- if ( ! isURL ( this ) )
662
- throw new ERR_INVALID_THIS ( 'URL' ) ;
663
- return this [ context ] . password ;
640
+ return this . #context. password ;
664
641
}
665
642
666
643
set password ( value ) {
667
- if ( ! isURL ( this ) )
668
- throw new ERR_INVALID_THIS ( 'URL' ) ;
669
- updateUrl ( this [ context ] . href , updateActions . kPassword , `${ value } ` , this . #onParseComplete) ;
644
+ updateUrl ( this . #context. href , updateActions . kPassword , `${ value } ` , this . #onParseComplete) ;
670
645
}
671
646
672
647
get host ( ) {
673
- if ( ! isURL ( this ) )
674
- throw new ERR_INVALID_THIS ( 'URL' ) ;
675
- const port = this [ context ] . port ;
648
+ const port = this . #context. port ;
676
649
const suffix = port . length > 0 ? `:${ port } ` : '' ;
677
- return this [ context ] . hostname + suffix ;
650
+ return this . # context. hostname + suffix ;
678
651
}
679
652
680
653
set host ( value ) {
681
- if ( ! isURL ( this ) )
682
- throw new ERR_INVALID_THIS ( 'URL' ) ;
683
- updateUrl ( this [ context ] . href , updateActions . kHost , `${ value } ` , this . #onParseComplete) ;
654
+ updateUrl ( this . #context. href , updateActions . kHost , `${ value } ` , this . #onParseComplete) ;
684
655
}
685
656
686
657
get hostname ( ) {
687
- if ( ! isURL ( this ) )
688
- throw new ERR_INVALID_THIS ( 'URL' ) ;
689
- return this [ context ] . hostname ;
658
+ return this . #context. hostname ;
690
659
}
691
660
692
661
set hostname ( value ) {
693
- if ( ! isURL ( this ) )
694
- throw new ERR_INVALID_THIS ( 'URL' ) ;
695
- updateUrl ( this [ context ] . href , updateActions . kHostname , `${ value } ` , this . #onParseComplete) ;
662
+ updateUrl ( this . #context. href , updateActions . kHostname , `${ value } ` , this . #onParseComplete) ;
696
663
}
697
664
698
665
get port ( ) {
699
- if ( ! isURL ( this ) )
700
- throw new ERR_INVALID_THIS ( 'URL' ) ;
701
- return this [ context ] . port ;
666
+ return this . #context. port ;
702
667
}
703
668
704
669
set port ( value ) {
705
- if ( ! isURL ( this ) )
706
- throw new ERR_INVALID_THIS ( 'URL' ) ;
707
- updateUrl ( this [ context ] . href , updateActions . kPort , `${ value } ` , this . #onParseComplete) ;
670
+ updateUrl ( this . #context. href , updateActions . kPort , `${ value } ` , this . #onParseComplete) ;
708
671
}
709
672
710
673
get pathname ( ) {
711
- if ( ! isURL ( this ) )
712
- throw new ERR_INVALID_THIS ( 'URL' ) ;
713
- return this [ context ] . pathname ;
674
+ return this . #context. pathname ;
714
675
}
715
676
716
677
set pathname ( value ) {
717
- if ( ! isURL ( this ) )
718
- throw new ERR_INVALID_THIS ( 'URL' ) ;
719
- updateUrl ( this [ context ] . href , updateActions . kPathname , `${ value } ` , this . #onParseComplete) ;
678
+ updateUrl ( this . #context. href , updateActions . kPathname , `${ value } ` , this . #onParseComplete) ;
720
679
}
721
680
722
681
get search ( ) {
723
- if ( ! isURL ( this ) )
724
- throw new ERR_INVALID_THIS ( 'URL' ) ;
725
- return this [ context ] . search ;
682
+ return this . #context. search ;
726
683
}
727
684
728
685
set search ( value ) {
729
- if ( ! isURL ( this ) )
730
- throw new ERR_INVALID_THIS ( 'URL' ) ;
731
- updateUrl ( this [ context ] . href , updateActions . kSearch , toUSVString ( value ) , this . #onParseComplete) ;
686
+ updateUrl ( this . #context. href , updateActions . kSearch , toUSVString ( value ) , this . #onParseComplete) ;
732
687
}
733
688
734
689
// readonly
735
690
get searchParams ( ) {
736
- if ( ! isURL ( this ) )
737
- throw new ERR_INVALID_THIS ( 'URL' ) ;
738
691
// 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 ;
692
+ if ( this . # searchParams == null ) {
693
+ this . # searchParams = new URLSearchParams ( this . # context. search ) ;
694
+ this . # searchParams[ context ] = this ;
742
695
}
743
- return this [ searchParams ] ;
696
+ return this . # searchParams;
744
697
}
745
698
746
699
get hash ( ) {
747
- if ( ! isURL ( this ) )
748
- throw new ERR_INVALID_THIS ( 'URL' ) ;
749
- return this [ context ] . hash ;
700
+ return this . #context. hash ;
750
701
}
751
702
752
703
set hash ( value ) {
753
- if ( ! isURL ( this ) )
754
- throw new ERR_INVALID_THIS ( 'URL' ) ;
755
- updateUrl ( this [ context ] . href , updateActions . kHash , `${ value } ` , this . #onParseComplete) ;
704
+ updateUrl ( this . #context. href , updateActions . kHash , `${ value } ` , this . #onParseComplete) ;
756
705
}
757
706
758
707
toJSON ( ) {
759
- if ( ! isURL ( this ) )
760
- throw new ERR_INVALID_THIS ( 'URL' ) ;
761
- return this [ context ] . href ;
708
+ return this . #context. href ;
762
709
}
763
710
764
711
static createObjectURL ( obj ) {
@@ -1206,7 +1153,7 @@ function getPathFromURLPosix(url) {
1206
1153
function fileURLToPath ( path ) {
1207
1154
if ( typeof path === 'string' )
1208
1155
path = new URL ( path ) ;
1209
- else if ( ! isURLInstance ( path ) )
1156
+ else if ( ! isURL ( path ) )
1210
1157
throw new ERR_INVALID_ARG_TYPE ( 'path' , [ 'string' , 'URL' ] , path ) ;
1211
1158
if ( path . protocol !== 'file:' )
1212
1159
throw new ERR_INVALID_URL_SCHEME ( 'file' ) ;
@@ -1282,12 +1229,8 @@ function pathToFileURL(filepath) {
1282
1229
return outURL ;
1283
1230
}
1284
1231
1285
- function isURLInstance ( fileURLOrPath ) {
1286
- return fileURLOrPath != null && fileURLOrPath . href && fileURLOrPath . origin ;
1287
- }
1288
-
1289
1232
function toPathIfFileURL ( fileURLOrPath ) {
1290
- if ( ! isURLInstance ( fileURLOrPath ) )
1233
+ if ( ! isURL ( fileURLOrPath ) )
1291
1234
return fileURLOrPath ;
1292
1235
return fileURLToPath ( fileURLOrPath ) ;
1293
1236
}
@@ -1297,7 +1240,6 @@ module.exports = {
1297
1240
fileURLToPath,
1298
1241
pathToFileURL,
1299
1242
toPathIfFileURL,
1300
- isURLInstance,
1301
1243
URL ,
1302
1244
URLSearchParams,
1303
1245
domainToASCII,
0 commit comments