@@ -58,22 +58,60 @@ export function useAuthEmulator(
58
58
AuthErrorCode . INVALID_EMULATOR_SCHEME
59
59
) ;
60
60
61
- const parsedUrl = new URL ( url ) ;
62
61
const disableWarnings = ! ! options ?. disableWarnings ;
63
62
64
- // Store the normalized URL whose path is always nonempty (i.e. containing at least a single '/').
65
- authInternal . config . emulator = { url : parsedUrl . toString ( ) } ;
63
+ const protocol = extractProtocol ( url ) ;
64
+ const { host, port } = extractHostAndPort ( url ) ;
65
+ const portStr = port === null ? '' : `:${ port } ` ;
66
+
67
+ // Always replace path with "/" (even if input url had no path at all, or had a different one).
68
+ authInternal . config . emulator = { url : `${ protocol } //${ host } ${ portStr } /` } ;
66
69
authInternal . settings . appVerificationDisabledForTesting = true ;
67
70
authInternal . emulatorConfig = Object . freeze ( {
68
- host : parsedUrl . hostname ,
69
- port : parsedUrl . port ? Number ( parsedUrl . port ) : null ,
70
- protocol : parsedUrl . protocol . replace ( ':' , '' ) ,
71
+ host,
72
+ port,
73
+ protocol : protocol . replace ( ':' , '' ) ,
71
74
options : Object . freeze ( { disableWarnings } )
72
75
} ) ;
73
76
74
77
emitEmulatorWarning ( disableWarnings ) ;
75
78
}
76
79
80
+ function extractProtocol ( url : string ) : string {
81
+ const protocolEnd = url . indexOf ( ':' ) ;
82
+ return protocolEnd < 0 ? '' : url . substr ( 0 , protocolEnd + 1 ) ;
83
+ }
84
+
85
+ function extractHostAndPort (
86
+ url : string
87
+ ) : { host : string ; port : number | null } {
88
+ const protocol = extractProtocol ( url ) ;
89
+ const authority = / ( \/ \/ ) ? ( [ ^ ? # / ] + ) / . exec ( url . substr ( protocol . length ) ) ; // Between // and /, ? or #.
90
+ if ( ! authority ) {
91
+ return { host : '' , port : null } ;
92
+ }
93
+ const hostAndPort = authority [ 2 ] . split ( '@' ) . pop ( ) || '' ; // Strip out "username:password@".
94
+ const bracketedIPv6 = / ^ ( \[ [ ^ \] ] + \] ) ( : | $ ) / . exec ( hostAndPort ) ;
95
+ if ( bracketedIPv6 ) {
96
+ const host = bracketedIPv6 [ 1 ] ;
97
+ return { host, port : parsePort ( hostAndPort . substr ( host . length + 1 ) ) } ;
98
+ } else {
99
+ const [ host , port ] = hostAndPort . split ( ':' ) ;
100
+ return { host, port : parsePort ( port ) } ;
101
+ }
102
+ }
103
+
104
+ function parsePort ( portStr : string ) : number | null {
105
+ if ( ! portStr ) {
106
+ return null ;
107
+ }
108
+ const port = Number ( portStr ) ;
109
+ if ( isNaN ( port ) ) {
110
+ return null ;
111
+ }
112
+ return port ;
113
+ }
114
+
77
115
function emitEmulatorWarning ( disableBanner : boolean ) : void {
78
116
function attachBanner ( ) : void {
79
117
const el = document . createElement ( 'p' ) ;
0 commit comments