transport.js 2.8 KB

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