debuggerProxyServer.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. function _ws() {
  7. const data = _interopRequireDefault(require("ws"));
  8. _ws = function () {
  9. return data;
  10. };
  11. return data;
  12. }
  13. function _cliTools() {
  14. const data = require("@react-native-community/cli-tools");
  15. _cliTools = function () {
  16. return data;
  17. };
  18. return data;
  19. }
  20. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  21. /**
  22. * Copyright (c) Facebook, Inc. and its affiliates.
  23. *
  24. * This source code is licensed under the MIT license found in the
  25. * LICENSE file in the root directory of this source tree.
  26. *
  27. * @format
  28. */
  29. function attachToServer(server, path) {
  30. const WebSocketServer = _ws().default.Server;
  31. const wss = new WebSocketServer({
  32. server,
  33. path
  34. });
  35. let debuggerSocket;
  36. let clientSocket;
  37. function send(dest, message) {
  38. if (!dest) {
  39. return;
  40. }
  41. try {
  42. dest.send(message);
  43. } catch (e) {
  44. _cliTools().logger.warn(e); // Sometimes this call throws 'not opened'
  45. }
  46. }
  47. const debuggerSocketCloseHandler = () => {
  48. debuggerSocket = null;
  49. if (clientSocket) {
  50. clientSocket.close(1011, 'Debugger was disconnected');
  51. }
  52. };
  53. const clientSocketCloseHandler = () => {
  54. clientSocket = null;
  55. send(debuggerSocket, JSON.stringify({
  56. method: '$disconnected'
  57. }));
  58. };
  59. wss.on('connection', connection => {
  60. // @ts-ignore current definition of ws does not have upgradeReq type
  61. const {
  62. url
  63. } = connection.upgradeReq;
  64. if (url.indexOf('role=debugger') > -1) {
  65. if (debuggerSocket) {
  66. connection.close(1011, 'Another debugger is already connected');
  67. return;
  68. }
  69. debuggerSocket = connection;
  70. if (debuggerSocket) {
  71. debuggerSocket.onerror = debuggerSocketCloseHandler;
  72. debuggerSocket.onclose = debuggerSocketCloseHandler;
  73. debuggerSocket.onmessage = ({
  74. data
  75. }) => send(clientSocket, data);
  76. }
  77. } else if (url.indexOf('role=client') > -1) {
  78. if (clientSocket) {
  79. // @ts-ignore not nullable with current type definition of ws
  80. clientSocket.onerror = null; // @ts-ignore not nullable with current type definition of ws
  81. clientSocket.onclose = null; // @ts-ignore not nullable with current type definition of ws
  82. clientSocket.onmessage = null;
  83. clientSocket.close(1011, 'Another client connected');
  84. }
  85. clientSocket = connection;
  86. clientSocket.onerror = clientSocketCloseHandler;
  87. clientSocket.onclose = clientSocketCloseHandler;
  88. clientSocket.onmessage = ({
  89. data
  90. }) => send(debuggerSocket, data);
  91. } else {
  92. connection.close(1011, 'Missing role param');
  93. }
  94. });
  95. return {
  96. server: wss,
  97. isDebuggerConnected() {
  98. return !!debuggerSocket;
  99. }
  100. };
  101. }
  102. var _default = {
  103. attachToServer
  104. };
  105. exports.default = _default;
  106. //# sourceMappingURL=debuggerProxyServer.js.map