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