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