mirror of
https://github.com/status-im/react-native.git
synced 2025-01-15 20:15:11 +00:00
d5e9e55fa3
Summary: This PR removes the need for having the `providesModule` tags in all the modules in the repository. It configures Flow, Jest and Metro to get the module names from the filenames (`Libraries/Animated/src/nodes/AnimatedInterpolation.js` => `AnimatedInterpolation`) * Checked the Flow configuration by running flow on the project root (no errors): ``` yarn flow ``` * Checked the Jest configuration by running the tests with a clean cache: ``` yarn jest --clearCache && yarn test ``` * Checked the Metro configuration by starting the server with a clean cache and requesting some bundles: ``` yarn run start --reset-cache curl 'localhost:8081/IntegrationTests/AccessibilityManagerTest.bundle?platform=android' curl 'localhost:8081/Libraries/Alert/Alert.bundle?platform=ios' ``` [INTERNAL] [FEATURE] [All] - Removed providesModule from all modules and configured tools. Closes https://github.com/facebook/react-native/pull/18995 Reviewed By: mjesun Differential Revision: D7729509 Pulled By: rubennorte fbshipit-source-id: 892f760a05ce1fddb088ff0cd2e97e521fb8e825
160 lines
4.7 KiB
JavaScript
160 lines
4.7 KiB
JavaScript
/**
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
*
|
|
* This source code is licensed under the MIT license found in the
|
|
* LICENSE file in the root directory of this source tree.
|
|
*
|
|
*/
|
|
'use strict';
|
|
|
|
const XMLHttpRequest = require('XMLHttpRequest');
|
|
const originalXHROpen = XMLHttpRequest.prototype.open;
|
|
const originalXHRSend = XMLHttpRequest.prototype.send;
|
|
const originalXHRSetRequestHeader = XMLHttpRequest.prototype.setRequestHeader;
|
|
|
|
var openCallback;
|
|
var sendCallback;
|
|
var requestHeaderCallback;
|
|
var headerReceivedCallback;
|
|
var responseCallback;
|
|
|
|
var isInterceptorEnabled = false;
|
|
|
|
/**
|
|
* A network interceptor which monkey-patches XMLHttpRequest methods
|
|
* to gather all network requests/responses, in order to show their
|
|
* information in the React Native inspector development tool.
|
|
* This supports interception with XMLHttpRequest API, including Fetch API
|
|
* and any other third party libraries that depend on XMLHttpRequest.
|
|
*/
|
|
const XHRInterceptor = {
|
|
/**
|
|
* Invoked before XMLHttpRequest.open(...) is called.
|
|
*/
|
|
setOpenCallback(callback) {
|
|
openCallback = callback;
|
|
},
|
|
|
|
/**
|
|
* Invoked before XMLHttpRequest.send(...) is called.
|
|
*/
|
|
setSendCallback(callback) {
|
|
sendCallback = callback;
|
|
},
|
|
|
|
/**
|
|
* Invoked after xhr's readyState becomes xhr.HEADERS_RECEIVED.
|
|
*/
|
|
setHeaderReceivedCallback(callback) {
|
|
headerReceivedCallback = callback;
|
|
},
|
|
|
|
/**
|
|
* Invoked after xhr's readyState becomes xhr.DONE.
|
|
*/
|
|
setResponseCallback(callback) {
|
|
responseCallback = callback;
|
|
},
|
|
|
|
/**
|
|
* Invoked before XMLHttpRequest.setRequestHeader(...) is called.
|
|
*/
|
|
setRequestHeaderCallback(callback) {
|
|
requestHeaderCallback = callback;
|
|
},
|
|
|
|
isInterceptorEnabled() {
|
|
return isInterceptorEnabled;
|
|
},
|
|
|
|
enableInterception() {
|
|
if (isInterceptorEnabled) {
|
|
return;
|
|
}
|
|
// Override `open` method for all XHR requests to intercept the request
|
|
// method and url, then pass them through the `openCallback`.
|
|
XMLHttpRequest.prototype.open = function(method, url) {
|
|
if (openCallback) {
|
|
openCallback(method, url, this);
|
|
}
|
|
originalXHROpen.apply(this, arguments);
|
|
};
|
|
|
|
// Override `setRequestHeader` method for all XHR requests to intercept
|
|
// the request headers, then pass them through the `requestHeaderCallback`.
|
|
XMLHttpRequest.prototype.setRequestHeader = function(header, value) {
|
|
if (requestHeaderCallback) {
|
|
requestHeaderCallback(header, value, this);
|
|
}
|
|
originalXHRSetRequestHeader.apply(this, arguments);
|
|
};
|
|
|
|
// Override `send` method of all XHR requests to intercept the data sent,
|
|
// register listeners to intercept the response, and invoke the callbacks.
|
|
XMLHttpRequest.prototype.send = function(data) {
|
|
if (sendCallback) {
|
|
sendCallback(data, this);
|
|
}
|
|
if (this.addEventListener) {
|
|
this.addEventListener('readystatechange', () => {
|
|
if (!isInterceptorEnabled) {
|
|
return;
|
|
}
|
|
if (this.readyState === this.HEADERS_RECEIVED) {
|
|
const contentTypeString = this.getResponseHeader('Content-Type');
|
|
const contentLengthString =
|
|
this.getResponseHeader('Content-Length');
|
|
let responseContentType, responseSize;
|
|
if (contentTypeString) {
|
|
responseContentType = contentTypeString.split(';')[0];
|
|
}
|
|
if (contentLengthString) {
|
|
responseSize = parseInt(contentLengthString, 10);
|
|
}
|
|
if (headerReceivedCallback) {
|
|
headerReceivedCallback(
|
|
responseContentType,
|
|
responseSize,
|
|
this.getAllResponseHeaders(),
|
|
this,
|
|
);
|
|
}
|
|
}
|
|
if (this.readyState === this.DONE) {
|
|
if (responseCallback) {
|
|
responseCallback(
|
|
this.status,
|
|
this.timeout,
|
|
this.response,
|
|
this.responseURL,
|
|
this.responseType,
|
|
this,
|
|
);
|
|
}
|
|
}
|
|
}, false);
|
|
}
|
|
originalXHRSend.apply(this, arguments);
|
|
};
|
|
isInterceptorEnabled = true;
|
|
},
|
|
|
|
// Unpatch XMLHttpRequest methods and remove the callbacks.
|
|
disableInterception() {
|
|
if (!isInterceptorEnabled) {
|
|
return;
|
|
}
|
|
isInterceptorEnabled = false;
|
|
XMLHttpRequest.prototype.send = originalXHRSend;
|
|
XMLHttpRequest.prototype.open = originalXHROpen;
|
|
XMLHttpRequest.prototype.setRequestHeader = originalXHRSetRequestHeader;
|
|
responseCallback = null;
|
|
openCallback = null;
|
|
sendCallback = null;
|
|
headerReceivedCallback = null;
|
|
requestHeaderCallback = null;
|
|
},
|
|
};
|
|
|
|
module.exports = XHRInterceptor;
|