@@ -535,14 +535,16 @@ ObjectDefineProperties(URLSearchParams.prototype, {
535535} ) ;
536536
537537function isURL ( self ) {
538- return self != null && ObjectPrototypeHasOwnProperty ( self , context ) ;
538+ return self != null && self . href && self . origin ;
539539}
540540
541541class URL {
542+ #context = new URLContext ( ) ;
543+ #searchParams;
544+
542545 constructor ( input , base = undefined ) {
543546 // toUSVString is not needed.
544547 input = `${ input } ` ;
545- this [ context ] = new URLContext ( ) ;
546548
547549 if ( base !== undefined ) {
548550 base = `${ base } ` ;
@@ -558,11 +560,6 @@ class URL {
558560 }
559561
560562 [ inspect . custom ] ( depth , opts ) {
561- if ( this == null ||
562- ObjectGetPrototypeOf ( this [ context ] ) !== URLContext . prototype ) {
563- throw new ERR_INVALID_THIS ( 'URL' ) ;
564- }
565-
566563 if ( typeof depth === 'number' && depth < 0 )
567564 return this ;
568565
@@ -583,182 +580,133 @@ class URL {
583580 obj . hash = this . hash ;
584581
585582 if ( opts . showHidden ) {
586- obj [ context ] = this [ context ] ;
583+ obj [ context ] = this . # context;
587584 }
588585
589586 return `${ constructor . name } ${ inspect ( obj , opts ) } ` ;
590587 }
591588
592589 #onParseComplete = ( href , origin , protocol , hostname , pathname ,
593590 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 ) ;
607603 }
608604 } ;
609605
610606 toString ( ) {
611- if ( ! isURL ( this ) )
612- throw new ERR_INVALID_THIS ( 'URL' ) ;
613- return this [ context ] . href ;
607+ return this . #context. href ;
614608 }
615609
616610 get href ( ) {
617- if ( ! isURL ( this ) )
618- throw new ERR_INVALID_THIS ( 'URL' ) ;
619- return this [ context ] . href ;
611+ return this . #context. href ;
620612 }
621613
622614 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) ;
626616 if ( ! valid ) { throw ERR_INVALID_URL ( `${ value } ` ) ; }
627617 }
628618
629619 // readonly
630620 get origin ( ) {
631- if ( ! isURL ( this ) )
632- throw new ERR_INVALID_THIS ( 'URL' ) ;
633- return this [ context ] . origin ;
621+ return this . #context. origin ;
634622 }
635623
636624 get protocol ( ) {
637- if ( ! isURL ( this ) )
638- throw new ERR_INVALID_THIS ( 'URL' ) ;
639- return this [ context ] . protocol ;
625+ return this . #context. protocol ;
640626 }
641627
642628 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) ;
646630 }
647631
648632 get username ( ) {
649- if ( ! isURL ( this ) )
650- throw new ERR_INVALID_THIS ( 'URL' ) ;
651- return this [ context ] . username ;
633+ return this . #context. username ;
652634 }
653635
654636 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) ;
658638 }
659639
660640 get password ( ) {
661- if ( ! isURL ( this ) )
662- throw new ERR_INVALID_THIS ( 'URL' ) ;
663- return this [ context ] . password ;
641+ return this . #context. password ;
664642 }
665643
666644 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) ;
670646 }
671647
672648 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 ;
676650 const suffix = port . length > 0 ? `:${ port } ` : '' ;
677- return this [ context ] . hostname + suffix ;
651+ return this . # context. hostname + suffix ;
678652 }
679653
680654 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) ;
684656 }
685657
686658 get hostname ( ) {
687- if ( ! isURL ( this ) )
688- throw new ERR_INVALID_THIS ( 'URL' ) ;
689- return this [ context ] . hostname ;
659+ return this . #context. hostname ;
690660 }
691661
692662 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) ;
696664 }
697665
698666 get port ( ) {
699- if ( ! isURL ( this ) )
700- throw new ERR_INVALID_THIS ( 'URL' ) ;
701- return this [ context ] . port ;
667+ return this . #context. port ;
702668 }
703669
704670 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) ;
708672 }
709673
710674 get pathname ( ) {
711- if ( ! isURL ( this ) )
712- throw new ERR_INVALID_THIS ( 'URL' ) ;
713- return this [ context ] . pathname ;
675+ return this . #context. pathname ;
714676 }
715677
716678 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) ;
720680 }
721681
722682 get search ( ) {
723- if ( ! isURL ( this ) )
724- throw new ERR_INVALID_THIS ( 'URL' ) ;
725- return this [ context ] . search ;
683+ return this . #context. search ;
726684 }
727685
728686 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) ;
732688 }
733689
734690 // readonly
735691 get searchParams ( ) {
736- if ( ! isURL ( this ) )
737- throw new ERR_INVALID_THIS ( 'URL' ) ;
738692 // 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 ;
742696 }
743- return this [ searchParams ] ;
697+ return this . # searchParams;
744698 }
745699
746700 get hash ( ) {
747- if ( ! isURL ( this ) )
748- throw new ERR_INVALID_THIS ( 'URL' ) ;
749- return this [ context ] . hash ;
701+ return this . #context. hash ;
750702 }
751703
752704 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) ;
756706 }
757707
758708 toJSON ( ) {
759- if ( ! isURL ( this ) )
760- throw new ERR_INVALID_THIS ( 'URL' ) ;
761- return this [ context ] . href ;
709+ return this . #context. href ;
762710 }
763711
764712 static createObjectURL ( obj ) {
@@ -1206,7 +1154,7 @@ function getPathFromURLPosix(url) {
12061154function fileURLToPath ( path ) {
12071155 if ( typeof path === 'string' )
12081156 path = new URL ( path ) ;
1209- else if ( ! isURLInstance ( path ) )
1157+ else if ( ! isURL ( path ) )
12101158 throw new ERR_INVALID_ARG_TYPE ( 'path' , [ 'string' , 'URL' ] , path ) ;
12111159 if ( path . protocol !== 'file:' )
12121160 throw new ERR_INVALID_URL_SCHEME ( 'file' ) ;
@@ -1282,12 +1230,8 @@ function pathToFileURL(filepath) {
12821230 return outURL ;
12831231}
12841232
1285- function isURLInstance ( fileURLOrPath ) {
1286- return fileURLOrPath != null && fileURLOrPath . href && fileURLOrPath . origin ;
1287- }
1288-
12891233function toPathIfFileURL ( fileURLOrPath ) {
1290- if ( ! isURLInstance ( fileURLOrPath ) )
1234+ if ( ! isURL ( fileURLOrPath ) )
12911235 return fileURLOrPath ;
12921236 return fileURLToPath ( fileURLOrPath ) ;
12931237}
@@ -1297,7 +1241,6 @@ module.exports = {
12971241 fileURLToPath,
12981242 pathToFileURL,
12991243 toPathIfFileURL,
1300- isURLInstance,
13011244 URL ,
13021245 URLSearchParams,
13031246 domainToASCII,
0 commit comments