@@ -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,
@@ -534,15 +533,27 @@ ObjectDefineProperties(URLSearchParams.prototype, {
534
533
} ,
535
534
} ) ;
536
535
536
+ /**
537
+ * Checks if a value has the shape of a WHATWG URL object.
538
+ *
539
+ * Using a symbol or instanceof would not be able to recognize URL objects
540
+ * coming from other implementations (e.g. in Electron), so instead we are
541
+ * checking some well known properties for a lack of a better test.
542
+ *
543
+ * @param {* } self
544
+ * @returns {self is URL }
545
+ */
537
546
function isURL ( self ) {
538
- return self != null && ObjectPrototypeHasOwnProperty ( self , context ) ;
547
+ return self != null && self . href && self . origin ;
539
548
}
540
549
541
550
class URL {
551
+ #context = new URLContext ( ) ;
552
+ #searchParams;
553
+
542
554
constructor ( input , base = undefined ) {
543
555
// toUSVString is not needed.
544
556
input = `${ input } ` ;
545
- this [ context ] = new URLContext ( ) ;
546
557
547
558
if ( base !== undefined ) {
548
559
base = `${ base } ` ;
@@ -558,11 +569,6 @@ class URL {
558
569
}
559
570
560
571
[ inspect . custom ] ( depth , opts ) {
561
- if ( this == null ||
562
- ObjectGetPrototypeOf ( this [ context ] ) !== URLContext . prototype ) {
563
- throw new ERR_INVALID_THIS ( 'URL' ) ;
564
- }
565
-
566
572
if ( typeof depth === 'number' && depth < 0 )
567
573
return this ;
568
574
@@ -583,182 +589,133 @@ class URL {
583
589
obj . hash = this . hash ;
584
590
585
591
if ( opts . showHidden ) {
586
- obj [ context ] = this [ context ] ;
592
+ obj [ context ] = this . # context;
587
593
}
588
594
589
595
return `${ constructor . name } ${ inspect ( obj , opts ) } ` ;
590
596
}
591
597
592
598
#onParseComplete = ( href , origin , protocol , hostname , pathname ,
593
599
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 ) ;
600
+ this . #context. href = href ;
601
+ this . #context. origin = origin ;
602
+ this . #context. protocol = protocol ;
603
+ this . #context. hostname = hostname ;
604
+ this . #context. pathname = pathname ;
605
+ this . #context. search = search ;
606
+ this . #context. username = username ;
607
+ this . #context. password = password ;
608
+ this . #context. port = port ;
609
+ this . #context. hash = hash ;
610
+ if ( this . #searchParams) {
611
+ this . #searchParams[ searchParams ] = parseParams ( search ) ;
607
612
}
608
613
} ;
609
614
610
615
toString ( ) {
611
- if ( ! isURL ( this ) )
612
- throw new ERR_INVALID_THIS ( 'URL' ) ;
613
- return this [ context ] . href ;
616
+ return this . #context. href ;
614
617
}
615
618
616
619
get href ( ) {
617
- if ( ! isURL ( this ) )
618
- throw new ERR_INVALID_THIS ( 'URL' ) ;
619
- return this [ context ] . href ;
620
+ return this . #context. href ;
620
621
}
621
622
622
623
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) ;
624
+ const valid = updateUrl ( this . #context. href , updateActions . kHref , `${ value } ` , this . #onParseComplete) ;
626
625
if ( ! valid ) { throw ERR_INVALID_URL ( `${ value } ` ) ; }
627
626
}
628
627
629
628
// readonly
630
629
get origin ( ) {
631
- if ( ! isURL ( this ) )
632
- throw new ERR_INVALID_THIS ( 'URL' ) ;
633
- return this [ context ] . origin ;
630
+ return this . #context. origin ;
634
631
}
635
632
636
633
get protocol ( ) {
637
- if ( ! isURL ( this ) )
638
- throw new ERR_INVALID_THIS ( 'URL' ) ;
639
- return this [ context ] . protocol ;
634
+ return this . #context. protocol ;
640
635
}
641
636
642
637
set protocol ( value ) {
643
- if ( ! isURL ( this ) )
644
- throw new ERR_INVALID_THIS ( 'URL' ) ;
645
- updateUrl ( this [ context ] . href , updateActions . kProtocol , `${ value } ` , this . #onParseComplete) ;
638
+ updateUrl ( this . #context. href , updateActions . kProtocol , `${ value } ` , this . #onParseComplete) ;
646
639
}
647
640
648
641
get username ( ) {
649
- if ( ! isURL ( this ) )
650
- throw new ERR_INVALID_THIS ( 'URL' ) ;
651
- return this [ context ] . username ;
642
+ return this . #context. username ;
652
643
}
653
644
654
645
set username ( value ) {
655
- if ( ! isURL ( this ) )
656
- throw new ERR_INVALID_THIS ( 'URL' ) ;
657
- updateUrl ( this [ context ] . href , updateActions . kUsername , `${ value } ` , this . #onParseComplete) ;
646
+ updateUrl ( this . #context. href , updateActions . kUsername , `${ value } ` , this . #onParseComplete) ;
658
647
}
659
648
660
649
get password ( ) {
661
- if ( ! isURL ( this ) )
662
- throw new ERR_INVALID_THIS ( 'URL' ) ;
663
- return this [ context ] . password ;
650
+ return this . #context. password ;
664
651
}
665
652
666
653
set password ( value ) {
667
- if ( ! isURL ( this ) )
668
- throw new ERR_INVALID_THIS ( 'URL' ) ;
669
- updateUrl ( this [ context ] . href , updateActions . kPassword , `${ value } ` , this . #onParseComplete) ;
654
+ updateUrl ( this . #context. href , updateActions . kPassword , `${ value } ` , this . #onParseComplete) ;
670
655
}
671
656
672
657
get host ( ) {
673
- if ( ! isURL ( this ) )
674
- throw new ERR_INVALID_THIS ( 'URL' ) ;
675
- const port = this [ context ] . port ;
658
+ const port = this . #context. port ;
676
659
const suffix = port . length > 0 ? `:${ port } ` : '' ;
677
- return this [ context ] . hostname + suffix ;
660
+ return this . # context. hostname + suffix ;
678
661
}
679
662
680
663
set host ( value ) {
681
- if ( ! isURL ( this ) )
682
- throw new ERR_INVALID_THIS ( 'URL' ) ;
683
- updateUrl ( this [ context ] . href , updateActions . kHost , `${ value } ` , this . #onParseComplete) ;
664
+ updateUrl ( this . #context. href , updateActions . kHost , `${ value } ` , this . #onParseComplete) ;
684
665
}
685
666
686
667
get hostname ( ) {
687
- if ( ! isURL ( this ) )
688
- throw new ERR_INVALID_THIS ( 'URL' ) ;
689
- return this [ context ] . hostname ;
668
+ return this . #context. hostname ;
690
669
}
691
670
692
671
set hostname ( value ) {
693
- if ( ! isURL ( this ) )
694
- throw new ERR_INVALID_THIS ( 'URL' ) ;
695
- updateUrl ( this [ context ] . href , updateActions . kHostname , `${ value } ` , this . #onParseComplete) ;
672
+ updateUrl ( this . #context. href , updateActions . kHostname , `${ value } ` , this . #onParseComplete) ;
696
673
}
697
674
698
675
get port ( ) {
699
- if ( ! isURL ( this ) )
700
- throw new ERR_INVALID_THIS ( 'URL' ) ;
701
- return this [ context ] . port ;
676
+ return this . #context. port ;
702
677
}
703
678
704
679
set port ( value ) {
705
- if ( ! isURL ( this ) )
706
- throw new ERR_INVALID_THIS ( 'URL' ) ;
707
- updateUrl ( this [ context ] . href , updateActions . kPort , `${ value } ` , this . #onParseComplete) ;
680
+ updateUrl ( this . #context. href , updateActions . kPort , `${ value } ` , this . #onParseComplete) ;
708
681
}
709
682
710
683
get pathname ( ) {
711
- if ( ! isURL ( this ) )
712
- throw new ERR_INVALID_THIS ( 'URL' ) ;
713
- return this [ context ] . pathname ;
684
+ return this . #context. pathname ;
714
685
}
715
686
716
687
set pathname ( value ) {
717
- if ( ! isURL ( this ) )
718
- throw new ERR_INVALID_THIS ( 'URL' ) ;
719
- updateUrl ( this [ context ] . href , updateActions . kPathname , `${ value } ` , this . #onParseComplete) ;
688
+ updateUrl ( this . #context. href , updateActions . kPathname , `${ value } ` , this . #onParseComplete) ;
720
689
}
721
690
722
691
get search ( ) {
723
- if ( ! isURL ( this ) )
724
- throw new ERR_INVALID_THIS ( 'URL' ) ;
725
- return this [ context ] . search ;
692
+ return this . #context. search ;
726
693
}
727
694
728
695
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) ;
696
+ updateUrl ( this . #context. href , updateActions . kSearch , toUSVString ( value ) , this . #onParseComplete) ;
732
697
}
733
698
734
699
// readonly
735
700
get searchParams ( ) {
736
- if ( ! isURL ( this ) )
737
- throw new ERR_INVALID_THIS ( 'URL' ) ;
738
701
// 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 ;
702
+ if ( this . # searchParams == null ) {
703
+ this . # searchParams = new URLSearchParams ( this . # context. search ) ;
704
+ this . # searchParams[ context ] = this ;
742
705
}
743
- return this [ searchParams ] ;
706
+ return this . # searchParams;
744
707
}
745
708
746
709
get hash ( ) {
747
- if ( ! isURL ( this ) )
748
- throw new ERR_INVALID_THIS ( 'URL' ) ;
749
- return this [ context ] . hash ;
710
+ return this . #context. hash ;
750
711
}
751
712
752
713
set hash ( value ) {
753
- if ( ! isURL ( this ) )
754
- throw new ERR_INVALID_THIS ( 'URL' ) ;
755
- updateUrl ( this [ context ] . href , updateActions . kHash , `${ value } ` , this . #onParseComplete) ;
714
+ updateUrl ( this . #context. href , updateActions . kHash , `${ value } ` , this . #onParseComplete) ;
756
715
}
757
716
758
717
toJSON ( ) {
759
- if ( ! isURL ( this ) )
760
- throw new ERR_INVALID_THIS ( 'URL' ) ;
761
- return this [ context ] . href ;
718
+ return this . #context. href ;
762
719
}
763
720
764
721
static createObjectURL ( obj ) {
@@ -1206,7 +1163,7 @@ function getPathFromURLPosix(url) {
1206
1163
function fileURLToPath ( path ) {
1207
1164
if ( typeof path === 'string' )
1208
1165
path = new URL ( path ) ;
1209
- else if ( ! isURLInstance ( path ) )
1166
+ else if ( ! isURL ( path ) )
1210
1167
throw new ERR_INVALID_ARG_TYPE ( 'path' , [ 'string' , 'URL' ] , path ) ;
1211
1168
if ( path . protocol !== 'file:' )
1212
1169
throw new ERR_INVALID_URL_SCHEME ( 'file' ) ;
@@ -1282,12 +1239,8 @@ function pathToFileURL(filepath) {
1282
1239
return outURL ;
1283
1240
}
1284
1241
1285
- function isURLInstance ( fileURLOrPath ) {
1286
- return fileURLOrPath != null && fileURLOrPath . href && fileURLOrPath . origin ;
1287
- }
1288
-
1289
1242
function toPathIfFileURL ( fileURLOrPath ) {
1290
- if ( ! isURLInstance ( fileURLOrPath ) )
1243
+ if ( ! isURL ( fileURLOrPath ) )
1291
1244
return fileURLOrPath ;
1292
1245
return fileURLToPath ( fileURLOrPath ) ;
1293
1246
}
@@ -1297,7 +1250,6 @@ module.exports = {
1297
1250
fileURLToPath,
1298
1251
pathToFileURL,
1299
1252
toPathIfFileURL,
1300
- isURLInstance,
1301
1253
URL ,
1302
1254
URLSearchParams,
1303
1255
domainToASCII,
0 commit comments