transport.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. "use strict";
  2. var __importDefault = (this && this.__importDefault) || function (mod) {
  3. return (mod && mod.__esModule) ? mod : { "default": mod };
  4. };
  5. Object.defineProperty(exports, "__esModule", { value: true });
  6. exports.Transport = void 0;
  7. const engine_io_parser_1 = require("engine.io-parser");
  8. const component_emitter_1 = require("@socket.io/component-emitter");
  9. const util_js_1 = require("./util.js");
  10. const debug_1 = __importDefault(require("debug")); // debug()
  11. const debug = (0, debug_1.default)("engine.io-client:transport"); // debug()
  12. class Transport extends component_emitter_1.Emitter {
  13. /**
  14. * Transport abstract constructor.
  15. *
  16. * @param {Object} options.
  17. * @api private
  18. */
  19. constructor(opts) {
  20. super();
  21. this.writable = false;
  22. (0, util_js_1.installTimerFunctions)(this, opts);
  23. this.opts = opts;
  24. this.query = opts.query;
  25. this.readyState = "";
  26. this.socket = opts.socket;
  27. }
  28. /**
  29. * Emits an error.
  30. *
  31. * @param {String} str
  32. * @return {Transport} for chaining
  33. * @api protected
  34. */
  35. onError(msg, desc) {
  36. const err = new Error(msg);
  37. // @ts-ignore
  38. err.type = "TransportError";
  39. // @ts-ignore
  40. err.description = desc;
  41. super.emit("error", err);
  42. return this;
  43. }
  44. /**
  45. * Opens the transport.
  46. *
  47. * @api public
  48. */
  49. open() {
  50. if ("closed" === this.readyState || "" === this.readyState) {
  51. this.readyState = "opening";
  52. this.doOpen();
  53. }
  54. return this;
  55. }
  56. /**
  57. * Closes the transport.
  58. *
  59. * @api public
  60. */
  61. close() {
  62. if ("opening" === this.readyState || "open" === this.readyState) {
  63. this.doClose();
  64. this.onClose();
  65. }
  66. return this;
  67. }
  68. /**
  69. * Sends multiple packets.
  70. *
  71. * @param {Array} packets
  72. * @api public
  73. */
  74. send(packets) {
  75. if ("open" === this.readyState) {
  76. this.write(packets);
  77. }
  78. else {
  79. // this might happen if the transport was silently closed in the beforeunload event handler
  80. debug("transport is not open, discarding packets");
  81. }
  82. }
  83. /**
  84. * Called upon open
  85. *
  86. * @api protected
  87. */
  88. onOpen() {
  89. this.readyState = "open";
  90. this.writable = true;
  91. super.emit("open");
  92. }
  93. /**
  94. * Called with data.
  95. *
  96. * @param {String} data
  97. * @api protected
  98. */
  99. onData(data) {
  100. const packet = (0, engine_io_parser_1.decodePacket)(data, this.socket.binaryType);
  101. this.onPacket(packet);
  102. }
  103. /**
  104. * Called with a decoded packet.
  105. *
  106. * @api protected
  107. */
  108. onPacket(packet) {
  109. super.emit("packet", packet);
  110. }
  111. /**
  112. * Called upon close.
  113. *
  114. * @api protected
  115. */
  116. onClose() {
  117. this.readyState = "closed";
  118. super.emit("close");
  119. }
  120. }
  121. exports.Transport = Transport;