77 ArrayPrototypePush,
88 ArrayPrototypeReduce,
99 ArrayPrototypeSlice,
10+ Boolean,
1011 Int8Array,
1112 IteratorPrototype,
1213 Number,
@@ -15,7 +16,6 @@ const {
1516 ObjectGetOwnPropertySymbols,
1617 ObjectGetPrototypeOf,
1718 ObjectKeys,
18- ObjectPrototypeHasOwnProperty,
1919 ReflectGetOwnPropertyDescriptor,
2020 ReflectOwnKeys,
2121 RegExpPrototypeSymbolReplace,
@@ -534,15 +534,27 @@ ObjectDefineProperties(URLSearchParams.prototype, {
534534 } ,
535535} ) ;
536536
537+ /**
538+ * Checks if a value has the shape of a WHATWG URL object.
539+ *
540+ * Using a symbol or instanceof would not be able to recognize URL objects
541+ * coming from other implementations (e.g. in Electron), so instead we are
542+ * checking some well known properties for a lack of a better test.
543+ *
544+ * @param {* } self
545+ * @returns {self is URL }
546+ */
537547function isURL ( self ) {
538- return self != null && ObjectPrototypeHasOwnProperty ( self , context ) ;
548+ return Boolean ( self ?. href && self . origin ) ;
539549}
540550
541551class URL {
552+ #context = new URLContext ( ) ;
553+ #searchParams;
554+
542555 constructor ( input , base = undefined ) {
543556 // toUSVString is not needed.
544557 input = `${ input } ` ;
545- this [ context ] = new URLContext ( ) ;
546558
547559 if ( base !== undefined ) {
548560 base = `${ base } ` ;
@@ -558,11 +570,6 @@ class URL {
558570 }
559571
560572 [ inspect . custom ] ( depth , opts ) {
561- if ( this == null ||
562- ObjectGetPrototypeOf ( this [ context ] ) !== URLContext . prototype ) {
563- throw new ERR_INVALID_THIS ( 'URL' ) ;
564- }
565-
566573 if ( typeof depth === 'number' && depth < 0 )
567574 return this ;
568575
@@ -583,182 +590,133 @@ class URL {
583590 obj . hash = this . hash ;
584591
585592 if ( opts . showHidden ) {
586- obj [ context ] = this [ context ] ;
593+ obj [ context ] = this . # context;
587594 }
588595
589596 return `${ constructor . name } ${ inspect ( obj , opts ) } ` ;
590597 }
591598
592599 #onParseComplete = ( href , origin , protocol , hostname , pathname ,
593600 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 ) ;
601+ this . #context. href = href ;
602+ this . #context. origin = origin ;
603+ this . #context. protocol = protocol ;
604+ this . #context. hostname = hostname ;
605+ this . #context. pathname = pathname ;
606+ this . #context. search = search ;
607+ this . #context. username = username ;
608+ this . #context. password = password ;
609+ this . #context. port = port ;
610+ this . #context. hash = hash ;
611+ if ( this . #searchParams) {
612+ this . #searchParams[ searchParams ] = parseParams ( search ) ;
607613 }
608614 } ;
609615
610616 toString ( ) {
611- if ( ! isURL ( this ) )
612- throw new ERR_INVALID_THIS ( 'URL' ) ;
613- return this [ context ] . href ;
617+ return this . #context. href ;
614618 }
615619
616620 get href ( ) {
617- if ( ! isURL ( this ) )
618- throw new ERR_INVALID_THIS ( 'URL' ) ;
619- return this [ context ] . href ;
621+ return this . #context. href ;
620622 }
621623
622624 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) ;
625+ const valid = updateUrl ( this . #context. href , updateActions . kHref , `${ value } ` , this . #onParseComplete) ;
626626 if ( ! valid ) { throw ERR_INVALID_URL ( `${ value } ` ) ; }
627627 }
628628
629629 // readonly
630630 get origin ( ) {
631- if ( ! isURL ( this ) )
632- throw new ERR_INVALID_THIS ( 'URL' ) ;
633- return this [ context ] . origin ;
631+ return this . #context. origin ;
634632 }
635633
636634 get protocol ( ) {
637- if ( ! isURL ( this ) )
638- throw new ERR_INVALID_THIS ( 'URL' ) ;
639- return this [ context ] . protocol ;
635+ return this . #context. protocol ;
640636 }
641637
642638 set protocol ( value ) {
643- if ( ! isURL ( this ) )
644- throw new ERR_INVALID_THIS ( 'URL' ) ;
645- updateUrl ( this [ context ] . href , updateActions . kProtocol , `${ value } ` , this . #onParseComplete) ;
639+ updateUrl ( this . #context. href , updateActions . kProtocol , `${ value } ` , this . #onParseComplete) ;
646640 }
647641
648642 get username ( ) {
649- if ( ! isURL ( this ) )
650- throw new ERR_INVALID_THIS ( 'URL' ) ;
651- return this [ context ] . username ;
643+ return this . #context. username ;
652644 }
653645
654646 set username ( value ) {
655- if ( ! isURL ( this ) )
656- throw new ERR_INVALID_THIS ( 'URL' ) ;
657- updateUrl ( this [ context ] . href , updateActions . kUsername , `${ value } ` , this . #onParseComplete) ;
647+ updateUrl ( this . #context. href , updateActions . kUsername , `${ value } ` , this . #onParseComplete) ;
658648 }
659649
660650 get password ( ) {
661- if ( ! isURL ( this ) )
662- throw new ERR_INVALID_THIS ( 'URL' ) ;
663- return this [ context ] . password ;
651+ return this . #context. password ;
664652 }
665653
666654 set password ( value ) {
667- if ( ! isURL ( this ) )
668- throw new ERR_INVALID_THIS ( 'URL' ) ;
669- updateUrl ( this [ context ] . href , updateActions . kPassword , `${ value } ` , this . #onParseComplete) ;
655+ updateUrl ( this . #context. href , updateActions . kPassword , `${ value } ` , this . #onParseComplete) ;
670656 }
671657
672658 get host ( ) {
673- if ( ! isURL ( this ) )
674- throw new ERR_INVALID_THIS ( 'URL' ) ;
675- const port = this [ context ] . port ;
659+ const port = this . #context. port ;
676660 const suffix = port . length > 0 ? `:${ port } ` : '' ;
677- return this [ context ] . hostname + suffix ;
661+ return this . # context. hostname + suffix ;
678662 }
679663
680664 set host ( value ) {
681- if ( ! isURL ( this ) )
682- throw new ERR_INVALID_THIS ( 'URL' ) ;
683- updateUrl ( this [ context ] . href , updateActions . kHost , `${ value } ` , this . #onParseComplete) ;
665+ updateUrl ( this . #context. href , updateActions . kHost , `${ value } ` , this . #onParseComplete) ;
684666 }
685667
686668 get hostname ( ) {
687- if ( ! isURL ( this ) )
688- throw new ERR_INVALID_THIS ( 'URL' ) ;
689- return this [ context ] . hostname ;
669+ return this . #context. hostname ;
690670 }
691671
692672 set hostname ( value ) {
693- if ( ! isURL ( this ) )
694- throw new ERR_INVALID_THIS ( 'URL' ) ;
695- updateUrl ( this [ context ] . href , updateActions . kHostname , `${ value } ` , this . #onParseComplete) ;
673+ updateUrl ( this . #context. href , updateActions . kHostname , `${ value } ` , this . #onParseComplete) ;
696674 }
697675
698676 get port ( ) {
699- if ( ! isURL ( this ) )
700- throw new ERR_INVALID_THIS ( 'URL' ) ;
701- return this [ context ] . port ;
677+ return this . #context. port ;
702678 }
703679
704680 set port ( value ) {
705- if ( ! isURL ( this ) )
706- throw new ERR_INVALID_THIS ( 'URL' ) ;
707- updateUrl ( this [ context ] . href , updateActions . kPort , `${ value } ` , this . #onParseComplete) ;
681+ updateUrl ( this . #context. href , updateActions . kPort , `${ value } ` , this . #onParseComplete) ;
708682 }
709683
710684 get pathname ( ) {
711- if ( ! isURL ( this ) )
712- throw new ERR_INVALID_THIS ( 'URL' ) ;
713- return this [ context ] . pathname ;
685+ return this . #context. pathname ;
714686 }
715687
716688 set pathname ( value ) {
717- if ( ! isURL ( this ) )
718- throw new ERR_INVALID_THIS ( 'URL' ) ;
719- updateUrl ( this [ context ] . href , updateActions . kPathname , `${ value } ` , this . #onParseComplete) ;
689+ updateUrl ( this . #context. href , updateActions . kPathname , `${ value } ` , this . #onParseComplete) ;
720690 }
721691
722692 get search ( ) {
723- if ( ! isURL ( this ) )
724- throw new ERR_INVALID_THIS ( 'URL' ) ;
725- return this [ context ] . search ;
693+ return this . #context. search ;
726694 }
727695
728696 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) ;
697+ updateUrl ( this . #context. href , updateActions . kSearch , toUSVString ( value ) , this . #onParseComplete) ;
732698 }
733699
734700 // readonly
735701 get searchParams ( ) {
736- if ( ! isURL ( this ) )
737- throw new ERR_INVALID_THIS ( 'URL' ) ;
738702 // 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 ;
703+ if ( this . # searchParams == null ) {
704+ this . # searchParams = new URLSearchParams ( this . # context. search ) ;
705+ this . # searchParams[ context ] = this ;
742706 }
743- return this [ searchParams ] ;
707+ return this . # searchParams;
744708 }
745709
746710 get hash ( ) {
747- if ( ! isURL ( this ) )
748- throw new ERR_INVALID_THIS ( 'URL' ) ;
749- return this [ context ] . hash ;
711+ return this . #context. hash ;
750712 }
751713
752714 set hash ( value ) {
753- if ( ! isURL ( this ) )
754- throw new ERR_INVALID_THIS ( 'URL' ) ;
755- updateUrl ( this [ context ] . href , updateActions . kHash , `${ value } ` , this . #onParseComplete) ;
715+ updateUrl ( this . #context. href , updateActions . kHash , `${ value } ` , this . #onParseComplete) ;
756716 }
757717
758718 toJSON ( ) {
759- if ( ! isURL ( this ) )
760- throw new ERR_INVALID_THIS ( 'URL' ) ;
761- return this [ context ] . href ;
719+ return this . #context. href ;
762720 }
763721
764722 static createObjectURL ( obj ) {
@@ -1206,7 +1164,7 @@ function getPathFromURLPosix(url) {
12061164function fileURLToPath ( path ) {
12071165 if ( typeof path === 'string' )
12081166 path = new URL ( path ) ;
1209- else if ( ! isURLInstance ( path ) )
1167+ else if ( ! isURL ( path ) )
12101168 throw new ERR_INVALID_ARG_TYPE ( 'path' , [ 'string' , 'URL' ] , path ) ;
12111169 if ( path . protocol !== 'file:' )
12121170 throw new ERR_INVALID_URL_SCHEME ( 'file' ) ;
@@ -1282,12 +1240,8 @@ function pathToFileURL(filepath) {
12821240 return outURL ;
12831241}
12841242
1285- function isURLInstance ( fileURLOrPath ) {
1286- return fileURLOrPath != null && fileURLOrPath . href && fileURLOrPath . origin ;
1287- }
1288-
12891243function toPathIfFileURL ( fileURLOrPath ) {
1290- if ( ! isURLInstance ( fileURLOrPath ) )
1244+ if ( ! isURL ( fileURLOrPath ) )
12911245 return fileURLOrPath ;
12921246 return fileURLToPath ( fileURLOrPath ) ;
12931247}
@@ -1297,7 +1251,6 @@ module.exports = {
12971251 fileURLToPath,
12981252 pathToFileURL,
12991253 toPathIfFileURL,
1300- isURLInstance,
13011254 URL ,
13021255 URLSearchParams,
13031256 domainToASCII,
0 commit comments