transport.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.Transport = void 0;
  4. const events_1 = require("events");
  5. const parser_v4 = require("engine.io-parser");
  6. const parser_v3 = require("./parser-v3/index");
  7. const debug_1 = require("debug");
  8. const debug = (0, debug_1.default)("engine:transport");
  9. function noop() { }
  10. class Transport extends events_1.EventEmitter {
  11. get readyState() {
  12. return this._readyState;
  13. }
  14. set readyState(state) {
  15. debug("readyState updated from %s to %s (%s)", this._readyState, state, this.name);
  16. this._readyState = state;
  17. }
  18. /**
  19. * Transport constructor.
  20. *
  21. * @param {EngineRequest} req
  22. */
  23. constructor(req) {
  24. super();
  25. /**
  26. * Whether the transport is currently ready to send packets.
  27. */
  28. this.writable = false;
  29. /**
  30. * The current state of the transport.
  31. * @protected
  32. */
  33. this._readyState = "open";
  34. /**
  35. * Whether the transport is discarded and can be safely closed (used during upgrade).
  36. * @protected
  37. */
  38. this.discarded = false;
  39. this.protocol = req._query.EIO === "4" ? 4 : 3; // 3rd revision by default
  40. this.parser = this.protocol === 4 ? parser_v4 : parser_v3;
  41. this.supportsBinary = !(req._query && req._query.b64);
  42. }
  43. /**
  44. * Flags the transport as discarded.
  45. *
  46. * @package
  47. */
  48. discard() {
  49. this.discarded = true;
  50. }
  51. /**
  52. * Called with an incoming HTTP request.
  53. *
  54. * @param req
  55. * @package
  56. */
  57. onRequest(req) { }
  58. /**
  59. * Closes the transport.
  60. *
  61. * @package
  62. */
  63. close(fn) {
  64. if ("closed" === this.readyState || "closing" === this.readyState)
  65. return;
  66. this.readyState = "closing";
  67. this.doClose(fn || noop);
  68. }
  69. /**
  70. * Called with a transport error.
  71. *
  72. * @param {String} msg - message error
  73. * @param {Object} desc - error description
  74. * @protected
  75. */
  76. onError(msg, desc) {
  77. if (this.listeners("error").length) {
  78. const err = new Error(msg);
  79. // @ts-ignore
  80. err.type = "TransportError";
  81. // @ts-ignore
  82. err.description = desc;
  83. this.emit("error", err);
  84. }
  85. else {
  86. debug("ignored transport error %s (%s)", msg, desc);
  87. }
  88. }
  89. /**
  90. * Called with parsed out a packets from the data stream.
  91. *
  92. * @param {Object} packet
  93. * @protected
  94. */
  95. onPacket(packet) {
  96. this.emit("packet", packet);
  97. }
  98. /**
  99. * Called with the encoded packet data.
  100. *
  101. * @param {String} data
  102. * @protected
  103. */
  104. onData(data) {
  105. this.onPacket(this.parser.decodePacket(data));
  106. }
  107. /**
  108. * Called upon transport close.
  109. *
  110. * @protected
  111. */
  112. onClose() {
  113. this.readyState = "closed";
  114. this.emit("close");
  115. }
  116. }
  117. exports.Transport = Transport;