@@ -35,6 +35,7 @@ goog.require('goog.html.TrustedResourceUrl');
35
35
goog . require ( 'goog.json' ) ;
36
36
goog . require ( 'goog.net.CorsXmlHttpFactory' ) ;
37
37
goog . require ( 'goog.net.EventType' ) ;
38
+ goog . require ( 'goog.net.FetchXmlHttpFactory' ) ;
38
39
goog . require ( 'goog.net.XhrIo' ) ;
39
40
goog . require ( 'goog.net.XmlHttpFactory' ) ;
40
41
goog . require ( 'goog.net.jsloader' ) ;
@@ -93,13 +94,6 @@ fireauth.XmlHttpFactory.prototype.internalGetOptions = function() {
93
94
* @constructor
94
95
*/
95
96
fireauth . RpcHandler = function ( apiKey , opt_config , opt_firebaseClientVersion ) {
96
- // Get XMLHttpRequest reference.
97
- var XMLHttpRequest = fireauth . util . getXMLHttpRequest ( ) ;
98
- if ( ! XMLHttpRequest ) {
99
- // In a Node.js environment, xmlhttprequest module needs to be required.
100
- throw new fireauth . AuthError ( fireauth . authenum . Error . INTERNAL_ERROR ,
101
- 'The XMLHttpRequest compatibility library was not found.' ) ;
102
- }
103
97
this . apiKey_ = apiKey ;
104
98
var config = opt_config || { } ;
105
99
this . secureTokenEndpoint_ = config [ 'secureTokenEndpoint' ] ||
@@ -131,10 +125,46 @@ fireauth.RpcHandler = function(apiKey, opt_config, opt_firebaseClientVersion) {
131
125
// Log client version for securetoken server.
132
126
this . secureTokenHeaders_ [ 'X-Client-Version' ] = opt_firebaseClientVersion ;
133
127
}
134
- /** @const @private {!goog.net.CorsXmlHttpFactory} The CORS XHR factory. */
135
- this . corsXhrFactory_ = new goog . net . CorsXmlHttpFactory ( ) ;
136
- /** @const @private {!goog.net.XmlHttpFactory} The XHR factory. */
137
- this . xhrFactory_ = new fireauth . XmlHttpFactory ( XMLHttpRequest ) ;
128
+
129
+ // Get XMLHttpRequest reference.
130
+ var XMLHttpRequest = fireauth . RpcHandler . getXMLHttpRequest ( ) ;
131
+ if ( ! XMLHttpRequest && ! fireauth . util . isWorker ( ) ) {
132
+ // In a Node.js environment, xmlhttprequest module needs to be required.
133
+ throw new fireauth . AuthError ( fireauth . authenum . Error . INTERNAL_ERROR ,
134
+ 'The XMLHttpRequest compatibility library was not found.' ) ;
135
+ }
136
+ /** @private {!goog.net.XmlHttpFactory|undefined} The XHR factory. */
137
+ this . rpcHandlerXhrFactory_ = undefined ;
138
+ // Initialize XHR factory. CORS does not apply in native environments or
139
+ // workers so don't use CorsXmlHttpFactory in those cases.
140
+ if ( fireauth . util . isWorker ( ) ) {
141
+ // For worker environment use FetchXmlHttpFactory.
142
+ this . rpcHandlerXhrFactory_ = new goog . net . FetchXmlHttpFactory (
143
+ /** @type {!WorkerGlobalScope } */ ( self ) ) ;
144
+ } else if ( fireauth . util . isNativeEnvironment ( ) ) {
145
+ // For Node.js, this is the polyfill library. For other environments,
146
+ // this is the native global XMLHttpRequest.
147
+ this . rpcHandlerXhrFactory_ = new fireauth . XmlHttpFactory (
148
+ /** @type {function(new:XMLHttpRequest) } */ ( XMLHttpRequest ) ) ;
149
+ } else {
150
+ // CORS Browser environment.
151
+ this . rpcHandlerXhrFactory_ = new goog . net . CorsXmlHttpFactory ( ) ;
152
+ }
153
+ } ;
154
+
155
+
156
+ /**
157
+ * @return {?function(new:XMLHttpRequest)|undefined } The current environment
158
+ * XMLHttpRequest. This is undefined for worker environment.
159
+ */
160
+ fireauth . RpcHandler . getXMLHttpRequest = function ( ) {
161
+ // In Node.js XMLHttpRequest is polyfilled.
162
+ var isNode = fireauth . util . getEnvironment ( ) == fireauth . util . Env . NODE ;
163
+ var XMLHttpRequest = goog . global [ 'XMLHttpRequest' ] ||
164
+ ( isNode &&
165
+ firebase . INTERNAL [ 'node' ] &&
166
+ firebase . INTERNAL [ 'node' ] [ 'XMLHttpRequest' ] ) ;
167
+ return XMLHttpRequest ;
138
168
} ;
139
169
140
170
@@ -394,7 +424,7 @@ fireauth.RpcHandler.prototype.sendXhr_ = function(
394
424
return ;
395
425
}
396
426
var sendXhr ;
397
- if ( fireauth . util . supportsCors ( ) ) {
427
+ if ( fireauth . util . supportsCors ( ) || fireauth . util . isWorker ( ) ) {
398
428
// If supports CORS use goog.net.XhrIo.
399
429
sendXhr = goog . bind ( this . sendXhrUsingXhrIo_ , this ) ;
400
430
} else {
@@ -432,13 +462,7 @@ fireauth.RpcHandler.prototype.sendXhrUsingXhrIo_ = function(
432
462
opt_data ,
433
463
opt_headers ,
434
464
opt_timeout ) {
435
- // Send XHR request. CORS does not apply in native environments so don't use
436
- // CorsXmlHttpFactory in those cases.
437
- // For a Node.js environment use the fireauth.XmlHttpFactory instance.
438
- var isNode = fireauth . util . getEnvironment ( ) == fireauth . util . Env . NODE ;
439
- var xhrIo = fireauth . util . isNativeEnvironment ( ) ?
440
- ( isNode ? new goog . net . XhrIo ( this . xhrFactory_ ) : new goog . net . XhrIo ( ) ) :
441
- new goog . net . XhrIo ( this . corsXhrFactory_ ) ;
465
+ var xhrIo = new goog . net . XhrIo ( this . rpcHandlerXhrFactory_ ) ;
442
466
443
467
// xhrIo.setTimeoutInterval not working in IE10 and IE11, handle manually.
444
468
var requestTimeout ;
0 commit comments