NetworkOverlay.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601
  1. /**
  2. * Copyright (c) Facebook, Inc. and its affiliates.
  3. *
  4. * This source code is licensed under the MIT license found in the
  5. * LICENSE file in the root directory of this source tree.
  6. *
  7. * @format
  8. * @flow
  9. */
  10. 'use strict';
  11. const FlatList = require('../Lists/FlatList');
  12. const React = require('react');
  13. const ScrollView = require('../Components/ScrollView/ScrollView');
  14. const StyleSheet = require('../StyleSheet/StyleSheet');
  15. const Text = require('../Text/Text');
  16. const TouchableHighlight = require('../Components/Touchable/TouchableHighlight');
  17. const View = require('../Components/View/View');
  18. const WebSocketInterceptor = require('../WebSocket/WebSocketInterceptor');
  19. const XHRInterceptor = require('../Network/XHRInterceptor');
  20. const LISTVIEW_CELL_HEIGHT = 15;
  21. // Global id for the intercepted XMLHttpRequest objects.
  22. let nextXHRId = 0;
  23. type NetworkRequestInfo = {
  24. id: number,
  25. type?: string,
  26. url?: string,
  27. method?: string,
  28. status?: number,
  29. dataSent?: any,
  30. responseContentType?: string,
  31. responseSize?: number,
  32. requestHeaders?: Object,
  33. responseHeaders?: string,
  34. response?: Object | string,
  35. responseURL?: string,
  36. responseType?: string,
  37. timeout?: number,
  38. closeReason?: string,
  39. messages?: string,
  40. serverClose?: Object,
  41. serverError?: Object,
  42. ...
  43. };
  44. type Props = $ReadOnly<{||}>;
  45. type State = {|
  46. detailRowId: ?number,
  47. requests: Array<NetworkRequestInfo>,
  48. |};
  49. function getStringByValue(value: any): string {
  50. if (value === undefined) {
  51. return 'undefined';
  52. }
  53. if (typeof value === 'object') {
  54. return JSON.stringify(value);
  55. }
  56. if (typeof value === 'string' && value.length > 500) {
  57. return String(value)
  58. .substr(0, 500)
  59. .concat('\n***TRUNCATED TO 500 CHARACTERS***');
  60. }
  61. return value;
  62. }
  63. function getTypeShortName(type: any): string {
  64. if (type === 'XMLHttpRequest') {
  65. return 'XHR';
  66. } else if (type === 'WebSocket') {
  67. return 'WS';
  68. }
  69. return '';
  70. }
  71. function keyExtractor(request: NetworkRequestInfo): string {
  72. return String(request.id);
  73. }
  74. /**
  75. * Show all the intercepted network requests over the InspectorPanel.
  76. */
  77. class NetworkOverlay extends React.Component<Props, State> {
  78. _requestsListView: ?React.ElementRef<typeof FlatList>;
  79. _detailScrollView: ?React.ElementRef<typeof ScrollView>;
  80. // Metrics are used to decide when if the request list should be sticky, and
  81. // scroll to the bottom as new network requests come in, or if the user has
  82. // intentionally scrolled away from the bottom - to instead flash the scroll bar
  83. // and keep the current position
  84. _requestsListViewScrollMetrics = {
  85. offset: 0,
  86. visibleLength: 0,
  87. contentLength: 0,
  88. };
  89. // Map of `socketId` -> `index in `this.state.requests`.
  90. _socketIdMap = {};
  91. // Map of `xhr._index` -> `index in `this.state.requests`.
  92. _xhrIdMap: {[key: number]: number, ...} = {};
  93. state: State = {
  94. detailRowId: null,
  95. requests: [],
  96. };
  97. _enableXHRInterception(): void {
  98. if (XHRInterceptor.isInterceptorEnabled()) {
  99. return;
  100. }
  101. // Show the XHR request item in listView as soon as it was opened.
  102. XHRInterceptor.setOpenCallback((method, url, xhr) => {
  103. // Generate a global id for each intercepted xhr object, add this id
  104. // to the xhr object as a private `_index` property to identify it,
  105. // so that we can distinguish different xhr objects in callbacks.
  106. xhr._index = nextXHRId++;
  107. const xhrIndex = this.state.requests.length;
  108. this._xhrIdMap[xhr._index] = xhrIndex;
  109. const _xhr: NetworkRequestInfo = {
  110. id: xhrIndex,
  111. type: 'XMLHttpRequest',
  112. method: method,
  113. url: url,
  114. };
  115. this.setState(
  116. {
  117. requests: this.state.requests.concat(_xhr),
  118. },
  119. this._indicateAdditionalRequests,
  120. );
  121. });
  122. XHRInterceptor.setRequestHeaderCallback((header, value, xhr) => {
  123. const xhrIndex = this._getRequestIndexByXHRID(xhr._index);
  124. if (xhrIndex === -1) {
  125. return;
  126. }
  127. this.setState(({requests}) => {
  128. const networkRequestInfo = requests[xhrIndex];
  129. if (!networkRequestInfo.requestHeaders) {
  130. networkRequestInfo.requestHeaders = {};
  131. }
  132. networkRequestInfo.requestHeaders[header] = value;
  133. return {requests};
  134. });
  135. });
  136. XHRInterceptor.setSendCallback((data, xhr) => {
  137. const xhrIndex = this._getRequestIndexByXHRID(xhr._index);
  138. if (xhrIndex === -1) {
  139. return;
  140. }
  141. this.setState(({requests}) => {
  142. const networkRequestInfo = requests[xhrIndex];
  143. networkRequestInfo.dataSent = data;
  144. return {requests};
  145. });
  146. });
  147. XHRInterceptor.setHeaderReceivedCallback(
  148. (type, size, responseHeaders, xhr) => {
  149. const xhrIndex = this._getRequestIndexByXHRID(xhr._index);
  150. if (xhrIndex === -1) {
  151. return;
  152. }
  153. this.setState(({requests}) => {
  154. const networkRequestInfo = requests[xhrIndex];
  155. networkRequestInfo.responseContentType = type;
  156. networkRequestInfo.responseSize = size;
  157. networkRequestInfo.responseHeaders = responseHeaders;
  158. return {requests};
  159. });
  160. },
  161. );
  162. XHRInterceptor.setResponseCallback(
  163. (status, timeout, response, responseURL, responseType, xhr) => {
  164. const xhrIndex = this._getRequestIndexByXHRID(xhr._index);
  165. if (xhrIndex === -1) {
  166. return;
  167. }
  168. this.setState(({requests}) => {
  169. const networkRequestInfo = requests[xhrIndex];
  170. networkRequestInfo.status = status;
  171. networkRequestInfo.timeout = timeout;
  172. networkRequestInfo.response = response;
  173. networkRequestInfo.responseURL = responseURL;
  174. networkRequestInfo.responseType = responseType;
  175. return {requests};
  176. });
  177. },
  178. );
  179. // Fire above callbacks.
  180. XHRInterceptor.enableInterception();
  181. }
  182. _enableWebSocketInterception(): void {
  183. if (WebSocketInterceptor.isInterceptorEnabled()) {
  184. return;
  185. }
  186. // Show the WebSocket request item in listView when 'connect' is called.
  187. WebSocketInterceptor.setConnectCallback(
  188. (url, protocols, options, socketId) => {
  189. const socketIndex = this.state.requests.length;
  190. this._socketIdMap[socketId] = socketIndex;
  191. const _webSocket: NetworkRequestInfo = {
  192. id: socketIndex,
  193. type: 'WebSocket',
  194. url: url,
  195. protocols: protocols,
  196. };
  197. this.setState(
  198. {
  199. requests: this.state.requests.concat(_webSocket),
  200. },
  201. this._indicateAdditionalRequests,
  202. );
  203. },
  204. );
  205. WebSocketInterceptor.setCloseCallback(
  206. (statusCode, closeReason, socketId) => {
  207. const socketIndex = this._socketIdMap[socketId];
  208. if (socketIndex === undefined) {
  209. return;
  210. }
  211. if (statusCode !== null && closeReason !== null) {
  212. this.setState(({requests}) => {
  213. const networkRequestInfo = requests[socketIndex];
  214. networkRequestInfo.status = statusCode;
  215. networkRequestInfo.closeReason = closeReason;
  216. return {requests};
  217. });
  218. }
  219. },
  220. );
  221. WebSocketInterceptor.setSendCallback((data, socketId) => {
  222. const socketIndex = this._socketIdMap[socketId];
  223. if (socketIndex === undefined) {
  224. return;
  225. }
  226. this.setState(({requests}) => {
  227. const networkRequestInfo = requests[socketIndex];
  228. if (!networkRequestInfo.messages) {
  229. networkRequestInfo.messages = '';
  230. }
  231. networkRequestInfo.messages += 'Sent: ' + JSON.stringify(data) + '\n';
  232. return {requests};
  233. });
  234. });
  235. WebSocketInterceptor.setOnMessageCallback((socketId, message) => {
  236. const socketIndex = this._socketIdMap[socketId];
  237. if (socketIndex === undefined) {
  238. return;
  239. }
  240. this.setState(({requests}) => {
  241. const networkRequestInfo = requests[socketIndex];
  242. if (!networkRequestInfo.messages) {
  243. networkRequestInfo.messages = '';
  244. }
  245. networkRequestInfo.messages +=
  246. 'Received: ' + JSON.stringify(message) + '\n';
  247. return {requests};
  248. });
  249. });
  250. WebSocketInterceptor.setOnCloseCallback((socketId, message) => {
  251. const socketIndex = this._socketIdMap[socketId];
  252. if (socketIndex === undefined) {
  253. return;
  254. }
  255. this.setState(({requests}) => {
  256. const networkRequestInfo = requests[socketIndex];
  257. networkRequestInfo.serverClose = message;
  258. return {requests};
  259. });
  260. });
  261. WebSocketInterceptor.setOnErrorCallback((socketId, message) => {
  262. const socketIndex = this._socketIdMap[socketId];
  263. if (socketIndex === undefined) {
  264. return;
  265. }
  266. this.setState(({requests}) => {
  267. const networkRequestInfo = requests[socketIndex];
  268. networkRequestInfo.serverError = message;
  269. return {requests};
  270. });
  271. });
  272. // Fire above callbacks.
  273. WebSocketInterceptor.enableInterception();
  274. }
  275. componentDidMount() {
  276. this._enableXHRInterception();
  277. this._enableWebSocketInterception();
  278. }
  279. componentWillUnmount() {
  280. XHRInterceptor.disableInterception();
  281. WebSocketInterceptor.disableInterception();
  282. }
  283. _renderItem = ({item, index}): React.Element<any> => {
  284. const tableRowViewStyle = [
  285. styles.tableRow,
  286. index % 2 === 1 ? styles.tableRowOdd : styles.tableRowEven,
  287. index === this.state.detailRowId && styles.tableRowPressed,
  288. ];
  289. const urlCellViewStyle = styles.urlCellView;
  290. const methodCellViewStyle = styles.methodCellView;
  291. return (
  292. <TouchableHighlight
  293. onPress={() => {
  294. this._pressRow(index);
  295. }}>
  296. <View>
  297. <View style={tableRowViewStyle}>
  298. <View style={urlCellViewStyle}>
  299. <Text style={styles.cellText} numberOfLines={1}>
  300. {item.url}
  301. </Text>
  302. </View>
  303. <View style={methodCellViewStyle}>
  304. <Text style={styles.cellText} numberOfLines={1}>
  305. {getTypeShortName(item.type)}
  306. </Text>
  307. </View>
  308. </View>
  309. </View>
  310. </TouchableHighlight>
  311. );
  312. };
  313. _renderItemDetail(id) {
  314. const requestItem = this.state.requests[id];
  315. const details = Object.keys(requestItem).map(key => {
  316. if (key === 'id') {
  317. return;
  318. }
  319. return (
  320. <View style={styles.detailViewRow} key={key}>
  321. <Text style={[styles.detailViewText, styles.detailKeyCellView]}>
  322. {key}
  323. </Text>
  324. <Text style={[styles.detailViewText, styles.detailValueCellView]}>
  325. {getStringByValue(requestItem[key])}
  326. </Text>
  327. </View>
  328. );
  329. });
  330. return (
  331. <View>
  332. <TouchableHighlight
  333. style={styles.closeButton}
  334. onPress={this._closeButtonClicked}>
  335. <View>
  336. <Text style={styles.closeButtonText}>v</Text>
  337. </View>
  338. </TouchableHighlight>
  339. <ScrollView
  340. style={styles.detailScrollView}
  341. ref={scrollRef => (this._detailScrollView = scrollRef)}>
  342. {details}
  343. </ScrollView>
  344. </View>
  345. );
  346. }
  347. _indicateAdditionalRequests = (): void => {
  348. if (this._requestsListView) {
  349. const distanceFromEndThreshold = LISTVIEW_CELL_HEIGHT * 2;
  350. const {
  351. offset,
  352. visibleLength,
  353. contentLength,
  354. } = this._requestsListViewScrollMetrics;
  355. const distanceFromEnd = contentLength - visibleLength - offset;
  356. const isCloseToEnd = distanceFromEnd <= distanceFromEndThreshold;
  357. if (isCloseToEnd) {
  358. this._requestsListView.scrollToEnd();
  359. } else {
  360. this._requestsListView.flashScrollIndicators();
  361. }
  362. }
  363. };
  364. _captureRequestsListView = (listRef: ?FlatList<NetworkRequestInfo>): void => {
  365. this._requestsListView = listRef;
  366. };
  367. _requestsListViewOnScroll = (e: Object): void => {
  368. this._requestsListViewScrollMetrics.offset = e.nativeEvent.contentOffset.y;
  369. this._requestsListViewScrollMetrics.visibleLength =
  370. e.nativeEvent.layoutMeasurement.height;
  371. this._requestsListViewScrollMetrics.contentLength =
  372. e.nativeEvent.contentSize.height;
  373. };
  374. /**
  375. * Popup a scrollView to dynamically show detailed information of
  376. * the request, when pressing a row in the network flow listView.
  377. */
  378. _pressRow(rowId: number): void {
  379. this.setState({detailRowId: rowId}, this._scrollDetailToTop);
  380. }
  381. _scrollDetailToTop = (): void => {
  382. if (this._detailScrollView) {
  383. this._detailScrollView.scrollTo({
  384. y: 0,
  385. animated: false,
  386. });
  387. }
  388. };
  389. _closeButtonClicked = () => {
  390. this.setState({detailRowId: null});
  391. };
  392. _getRequestIndexByXHRID(index: number): number {
  393. if (index === undefined) {
  394. return -1;
  395. }
  396. const xhrIndex = this._xhrIdMap[index];
  397. if (xhrIndex === undefined) {
  398. return -1;
  399. } else {
  400. return xhrIndex;
  401. }
  402. }
  403. render(): React.Node {
  404. const {requests, detailRowId} = this.state;
  405. return (
  406. <View style={styles.container}>
  407. {detailRowId != null && this._renderItemDetail(detailRowId)}
  408. <View style={styles.listViewTitle}>
  409. {requests.length > 0 && (
  410. <View style={styles.tableRow}>
  411. <View style={styles.urlTitleCellView}>
  412. <Text style={styles.cellText} numberOfLines={1}>
  413. URL
  414. </Text>
  415. </View>
  416. <View style={styles.methodTitleCellView}>
  417. <Text style={styles.cellText} numberOfLines={1}>
  418. Type
  419. </Text>
  420. </View>
  421. </View>
  422. )}
  423. </View>
  424. <FlatList
  425. ref={this._captureRequestsListView}
  426. onScroll={this._requestsListViewOnScroll}
  427. style={styles.listView}
  428. data={requests}
  429. renderItem={this._renderItem}
  430. keyExtractor={keyExtractor}
  431. extraData={this.state}
  432. />
  433. </View>
  434. );
  435. }
  436. }
  437. const styles = StyleSheet.create({
  438. container: {
  439. paddingTop: 10,
  440. paddingBottom: 10,
  441. paddingLeft: 5,
  442. paddingRight: 5,
  443. },
  444. listViewTitle: {
  445. height: 20,
  446. },
  447. listView: {
  448. flex: 1,
  449. height: 60,
  450. },
  451. tableRow: {
  452. flexDirection: 'row',
  453. flex: 1,
  454. height: LISTVIEW_CELL_HEIGHT,
  455. },
  456. tableRowEven: {
  457. backgroundColor: '#555',
  458. },
  459. tableRowOdd: {
  460. backgroundColor: '#000',
  461. },
  462. tableRowPressed: {
  463. backgroundColor: '#3B5998',
  464. },
  465. cellText: {
  466. color: 'white',
  467. fontSize: 12,
  468. },
  469. methodTitleCellView: {
  470. height: 18,
  471. borderColor: '#DCD7CD',
  472. borderTopWidth: 1,
  473. borderBottomWidth: 1,
  474. borderRightWidth: 1,
  475. alignItems: 'center',
  476. justifyContent: 'center',
  477. backgroundColor: '#444',
  478. flex: 1,
  479. },
  480. urlTitleCellView: {
  481. height: 18,
  482. borderColor: '#DCD7CD',
  483. borderTopWidth: 1,
  484. borderBottomWidth: 1,
  485. borderLeftWidth: 1,
  486. borderRightWidth: 1,
  487. justifyContent: 'center',
  488. backgroundColor: '#444',
  489. flex: 5,
  490. paddingLeft: 3,
  491. },
  492. methodCellView: {
  493. height: 15,
  494. borderColor: '#DCD7CD',
  495. borderRightWidth: 1,
  496. alignItems: 'center',
  497. justifyContent: 'center',
  498. flex: 1,
  499. },
  500. urlCellView: {
  501. height: 15,
  502. borderColor: '#DCD7CD',
  503. borderLeftWidth: 1,
  504. borderRightWidth: 1,
  505. justifyContent: 'center',
  506. flex: 5,
  507. paddingLeft: 3,
  508. },
  509. detailScrollView: {
  510. flex: 1,
  511. height: 180,
  512. marginTop: 5,
  513. marginBottom: 5,
  514. },
  515. detailKeyCellView: {
  516. flex: 1.3,
  517. },
  518. detailValueCellView: {
  519. flex: 2,
  520. },
  521. detailViewRow: {
  522. flexDirection: 'row',
  523. paddingHorizontal: 3,
  524. },
  525. detailViewText: {
  526. color: 'white',
  527. fontSize: 11,
  528. },
  529. closeButtonText: {
  530. color: 'white',
  531. fontSize: 10,
  532. },
  533. closeButton: {
  534. marginTop: 5,
  535. backgroundColor: '#888',
  536. justifyContent: 'center',
  537. alignItems: 'center',
  538. },
  539. });
  540. module.exports = NetworkOverlay;