Skip to content

Commit c612d83

Browse files
nevirfacebook-github-bot-4
authored andcommitted
Preserve original global properties when polyfilling them
Summary: Fixes facebook#934. Closes facebook#3293 Reviewed By: @​svcscm Differential Revision: D2525598 Pulled By: @vjeux fb-gh-sync-id: 90672550f723a183897456dc9512851bfa34807a
1 parent aa8ead7 commit c612d83

File tree

1 file changed

+33
-8
lines changed

1 file changed

+33
-8
lines changed

Libraries/JavaScriptAppEngine/Initialization/InitializeJavaScriptAppEngine.js

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
4772
function setUpRedBoxErrorHandler() {
4873
var ErrorUtils = require('ErrorUtils');
4974
ErrorUtils.setGlobalHandler(handleError);
@@ -111,23 +136,23 @@ function setUpPromise() {
111136
function 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

124149
function setUpGeolocation() {
125150
GLOBAL.navigator = GLOBAL.navigator || {};
126-
GLOBAL.navigator.geolocation = require('Geolocation');
151+
polyfillGlobal('geolocation', require('Geolocation'), GLOBAL.navigator);
127152
}
128153

129154
function setUpWebSockets() {
130-
GLOBAL.WebSocket = require('WebSocket');
155+
polyfillGlobal('WebSocket', require('WebSocket'));
131156
}
132157

133158
function setUpProfile() {

0 commit comments

Comments
 (0)