messageSocketServer.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. function _url() {
  7. const data = _interopRequireDefault(require("url"));
  8. _url = function () {
  9. return data;
  10. };
  11. return data;
  12. }
  13. function _ws() {
  14. const data = require("ws");
  15. _ws = function () {
  16. return data;
  17. };
  18. return data;
  19. }
  20. function _cliTools() {
  21. const data = require("@react-native-community/cli-tools");
  22. _cliTools = function () {
  23. return data;
  24. };
  25. return data;
  26. }
  27. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  28. /**
  29. * Copyright (c) Facebook, Inc. and its affiliates.
  30. *
  31. * This source code is licensed under the MIT license found in the
  32. * LICENSE file in the root directory of this source tree.
  33. */
  34. const PROTOCOL_VERSION = 2;
  35. function parseMessage(data, binary) {
  36. if (binary) {
  37. _cliTools().logger.error('Expected text message, got binary!');
  38. return undefined;
  39. }
  40. try {
  41. const message = JSON.parse(data);
  42. if (message.version === PROTOCOL_VERSION) {
  43. return message;
  44. }
  45. _cliTools().logger.error(`Received message had wrong protocol version: ${message.version}`);
  46. } catch (e) {
  47. _cliTools().logger.error(`Failed to parse the message as JSON:\n${data}`);
  48. }
  49. return undefined;
  50. }
  51. function isBroadcast(message) {
  52. return typeof message.method === 'string' && message.id === undefined && message.target === undefined;
  53. }
  54. function isRequest(message) {
  55. return typeof message.method === 'string' && typeof message.target === 'string';
  56. }
  57. function isResponse(message) {
  58. return typeof message.id === 'object' && typeof message.id.requestId !== 'undefined' && typeof message.id.clientId === 'string' && (message.result !== undefined || message.error !== undefined);
  59. }
  60. function attachToServer(server, path) {
  61. const wss = new (_ws().Server)({
  62. server,
  63. path
  64. });
  65. const clients = new Map();
  66. let nextClientId = 0;
  67. function getClientWs(clientId) {
  68. const clientWs = clients.get(clientId);
  69. if (clientWs === undefined) {
  70. throw new Error(`could not find id "${clientId}" while forwarding request`);
  71. }
  72. return clientWs;
  73. }
  74. function handleSendBroadcast(broadcasterId, message) {
  75. const forwarded = {
  76. version: PROTOCOL_VERSION,
  77. method: message.method,
  78. params: message.params
  79. };
  80. if (clients.size === 0) {
  81. _cliTools().logger.warn(`No apps connected. Sending "${message.method}" to all React Native apps failed. Make sure your app is running in the simulator or on a phone connected via USB.`);
  82. }
  83. for (const [otherId, otherWs] of clients) {
  84. if (otherId !== broadcasterId) {
  85. try {
  86. otherWs.send(JSON.stringify(forwarded));
  87. } catch (e) {
  88. _cliTools().logger.error(`Failed to send broadcast to client: '${otherId}' ` + `due to:\n ${e.toString()}`);
  89. }
  90. }
  91. }
  92. }
  93. wss.on('connection', clientWs => {
  94. const clientId = `client#${nextClientId++}`;
  95. function handleCaughtError(message, error) {
  96. const errorMessage = {
  97. id: message.id,
  98. method: message.method,
  99. target: message.target,
  100. error: message.error === undefined ? 'undefined' : 'defined',
  101. params: message.params === undefined ? 'undefined' : 'defined',
  102. result: message.result === undefined ? 'undefined' : 'defined'
  103. };
  104. if (message.id === undefined) {
  105. _cliTools().logger.error(`Handling message from ${clientId} failed with:\n${error}\n` + `message:\n${JSON.stringify(errorMessage)}`);
  106. } else {
  107. try {
  108. clientWs.send(JSON.stringify({
  109. version: PROTOCOL_VERSION,
  110. error,
  111. id: message.id
  112. }));
  113. } catch (e) {
  114. _cliTools().logger.error(`Failed to reply to ${clientId} with error:\n${error}` + `\nmessage:\n${JSON.stringify(errorMessage)}` + `\ndue to error: ${e.toString()}`);
  115. }
  116. }
  117. }
  118. function handleServerRequest(message) {
  119. let result = null;
  120. switch (message.method) {
  121. case 'getid':
  122. result = clientId;
  123. break;
  124. case 'getpeers':
  125. result = {};
  126. clients.forEach((otherWs, otherId) => {
  127. if (clientId !== otherId) {
  128. result[otherId] = _url().default.parse(otherWs.upgradeReq.url, true).query;
  129. }
  130. });
  131. break;
  132. default:
  133. throw new Error(`unknown method: ${message.method}`);
  134. }
  135. clientWs.send(JSON.stringify({
  136. version: PROTOCOL_VERSION,
  137. result,
  138. id: message.id
  139. }));
  140. }
  141. function forwardRequest(message) {
  142. getClientWs(message.target).send(JSON.stringify({
  143. version: PROTOCOL_VERSION,
  144. method: message.method,
  145. params: message.params,
  146. id: message.id === undefined ? undefined : {
  147. requestId: message.id,
  148. clientId
  149. }
  150. }));
  151. }
  152. function forwardResponse(message) {
  153. if (!message.id) {
  154. return;
  155. }
  156. getClientWs(message.id.clientId).send(JSON.stringify({
  157. version: PROTOCOL_VERSION,
  158. result: message.result,
  159. error: message.error,
  160. id: message.id.requestId
  161. }));
  162. }
  163. clients.set(clientId, clientWs);
  164. const onCloseHandler = () => {
  165. // @ts-ignore
  166. clientWs.onmessage = null;
  167. clients.delete(clientId);
  168. };
  169. clientWs.onclose = onCloseHandler;
  170. clientWs.onerror = onCloseHandler;
  171. clientWs.onmessage = event => {
  172. const message = parseMessage(event.data, event.binary);
  173. if (message === undefined) {
  174. _cliTools().logger.error('Received message not matching protocol');
  175. return;
  176. }
  177. try {
  178. if (isBroadcast(message)) {
  179. handleSendBroadcast(clientId, message);
  180. } else if (isRequest(message)) {
  181. if (message.target === 'server') {
  182. handleServerRequest(message);
  183. } else {
  184. forwardRequest(message);
  185. }
  186. } else if (isResponse(message)) {
  187. forwardResponse(message);
  188. } else {
  189. throw new Error('Invalid message, did not match the protocol');
  190. }
  191. } catch (e) {
  192. handleCaughtError(message, e.toString());
  193. }
  194. };
  195. });
  196. return {
  197. broadcast: (method, params) => {
  198. handleSendBroadcast(null, {
  199. method,
  200. params
  201. });
  202. }
  203. };
  204. }
  205. var _default = {
  206. attachToServer,
  207. parseMessage
  208. };
  209. exports.default = _default;
  210. //# sourceMappingURL=messageSocketServer.js.map