transport.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. /**
  10. * Noop function.
  11. *
  12. * @api private
  13. */
  14. function noop() { }
  15. class Transport extends events_1.EventEmitter {
  16. /**
  17. * Transport constructor.
  18. *
  19. * @param {http.IncomingMessage} request
  20. * @api public
  21. */
  22. constructor(req) {
  23. super();
  24. this.readyState = "open";
  25. this.discarded = false;
  26. this.protocol = req._query.EIO === "4" ? 4 : 3; // 3rd revision by default
  27. this.parser = this.protocol === 4 ? parser_v4 : parser_v3;
  28. }
  29. get readyState() {
  30. return this._readyState;
  31. }
  32. set readyState(state) {
  33. debug("readyState updated from %s to %s (%s)", this._readyState, state, this.name);
  34. this._readyState = state;
  35. }
  36. /**
  37. * Flags the transport as discarded.
  38. *
  39. * @api private
  40. */
  41. discard() {
  42. this.discarded = true;
  43. }
  44. /**
  45. * Called with an incoming HTTP request.
  46. *
  47. * @param {http.IncomingMessage} request
  48. * @api protected
  49. */
  50. onRequest(req) {
  51. debug("setting request");
  52. this.req = req;
  53. }
  54. /**
  55. * Closes the transport.
  56. *
  57. * @api private
  58. */
  59. close(fn) {
  60. if ("closed" === this.readyState || "closing" === this.readyState)
  61. return;
  62. this.readyState = "closing";
  63. this.doClose(fn || noop);
  64. }
  65. /**
  66. * Called with a transport error.
  67. *
  68. * @param {String} message error
  69. * @param {Object} error description
  70. * @api protected
  71. */
  72. onError(msg, desc) {
  73. if (this.listeners("error").length) {
  74. const err = new Error(msg);
  75. // @ts-ignore
  76. err.type = "TransportError";
  77. // @ts-ignore
  78. err.description = desc;
  79. this.emit("error", err);
  80. }
  81. else {
  82. debug("ignored transport error %s (%s)", msg, desc);
  83. }
  84. }
  85. /**
  86. * Called with parsed out a packets from the data stream.
  87. *
  88. * @param {Object} packet
  89. * @api protected
  90. */
  91. onPacket(packet) {
  92. this.emit("packet", packet);
  93. }
  94. /**
  95. * Called with the encoded packet data.
  96. *
  97. * @param {String} data
  98. * @api protected
  99. */
  100. onData(data) {
  101. this.onPacket(this.parser.decodePacket(data));
  102. }
  103. /**
  104. * Called upon transport close.
  105. *
  106. * @api protected
  107. */
  108. onClose() {
  109. this.readyState = "closed";
  110. this.emit("close");
  111. }
  112. }
  113. exports.Transport = Transport;