@@ -44,6 +44,31 @@ function handleError(e, isFatal) {
4444 }
4545}
4646
47+ /**
48+ * Assigns a new global property, replacing the existing one if there is one.
49+ *
50+ * Existing properties are preserved as `originalPropertyName`. Both properties
51+ * will maintain the same enumerability & configurability.
52+ *
53+ * This allows you to undo the more aggressive polyfills, should you need to.
54+ * For example, if you want to route network requests through DevTools (to trace
55+ * them):
56+ *
57+ * GLOBAL.XMLHTTPRequest = GLOBAL.originalXMLHTTPRequest;
58+ *
59+ * For more info on that particular case, see:
60+ * https://github.com/facebook/react-native/issues/934
61+ */
62+ function polyfillGlobal ( name , newValue , scope = GLOBAL ) {
63+ var descriptor = Object . getOwnPropertyDescriptor ( scope , name ) ;
64+
65+ if ( scope [ name ] !== undefined ) {
66+ var backupName = `original${ name [ 0 ] . toUpperCase ( ) } ${ name . substr ( 1 ) } ` ;
67+ Object . defineProperty ( scope , backupName , { ...descriptor , value : scope [ name ] } ) ;
68+ }
69+ Object . defineProperty ( scope , name , { ...descriptor , value : newValue } ) ;
70+ }
71+
4772function setUpRedBoxErrorHandler ( ) {
4873 var ErrorUtils = require ( 'ErrorUtils' ) ;
4974 ErrorUtils . setGlobalHandler ( handleError ) ;
@@ -111,23 +136,23 @@ function setUpPromise() {
111136function setUpXHR ( ) {
112137 // The native XMLHttpRequest in Chrome dev tools is CORS aware and won't
113138 // let you fetch anything from the internet
114- GLOBAL . XMLHttpRequest = require ( 'XMLHttpRequest' ) ;
115- GLOBAL . FormData = require ( 'FormData' ) ;
139+ polyfillGlobal ( ' XMLHttpRequest' , require ( 'XMLHttpRequest' ) ) ;
140+ polyfillGlobal ( ' FormData' , require ( 'FormData' ) ) ;
116141
117142 var fetchPolyfill = require ( 'fetch' ) ;
118- GLOBAL . fetch = fetchPolyfill . fetch ;
119- GLOBAL . Headers = fetchPolyfill . Headers ;
120- GLOBAL . Request = fetchPolyfill . Request ;
121- GLOBAL . Response = fetchPolyfill . Response ;
143+ polyfillGlobal ( ' fetch' , fetchPolyfill . fetch ) ;
144+ polyfillGlobal ( ' Headers' , fetchPolyfill . Headers ) ;
145+ polyfillGlobal ( ' Request' , fetchPolyfill . Request ) ;
146+ polyfillGlobal ( ' Response' , fetchPolyfill . Response ) ;
122147}
123148
124149function setUpGeolocation ( ) {
125150 GLOBAL . navigator = GLOBAL . navigator || { } ;
126- GLOBAL . navigator . geolocation = require ( 'Geolocation' ) ;
151+ polyfillGlobal ( ' geolocation' , require ( 'Geolocation' ) , GLOBAL . navigator ) ;
127152}
128153
129154function setUpWebSockets ( ) {
130- GLOBAL . WebSocket = require ( 'WebSocket' ) ;
155+ polyfillGlobal ( ' WebSocket' , require ( 'WebSocket' ) ) ;
131156}
132157
133158function setUpProfile ( ) {
0 commit comments