transport.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 = exports.TransportError = 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 parseqs_js_1 = require("./contrib/parseqs.js");
  12. const debug = (0, debug_1.default)("engine.io-client:transport"); // debug()
  13. class TransportError extends Error {
  14. constructor(reason, description, context) {
  15. super(reason);
  16. this.description = description;
  17. this.context = context;
  18. this.type = "TransportError";
  19. }
  20. }
  21. exports.TransportError = TransportError;
  22. class Transport extends component_emitter_1.Emitter {
  23. /**
  24. * Transport abstract constructor.
  25. *
  26. * @param {Object} opts - options
  27. * @protected
  28. */
  29. constructor(opts) {
  30. super();
  31. this.writable = false;
  32. (0, util_js_1.installTimerFunctions)(this, opts);
  33. this.opts = opts;
  34. this.query = opts.query;
  35. this.socket = opts.socket;
  36. }
  37. /**
  38. * Emits an error.
  39. *
  40. * @param {String} reason
  41. * @param description
  42. * @param context - the error context
  43. * @return {Transport} for chaining
  44. * @protected
  45. */
  46. onError(reason, description, context) {
  47. super.emitReserved("error", new TransportError(reason, description, context));
  48. return this;
  49. }
  50. /**
  51. * Opens the transport.
  52. */
  53. open() {
  54. this.readyState = "opening";
  55. this.doOpen();
  56. return this;
  57. }
  58. /**
  59. * Closes the transport.
  60. */
  61. close() {
  62. if (this.readyState === "opening" || this.readyState === "open") {
  63. this.doClose();
  64. this.onClose();
  65. }
  66. return this;
  67. }
  68. /**
  69. * Sends multiple packets.
  70. *
  71. * @param {Array} packets
  72. */
  73. send(packets) {
  74. if (this.readyState === "open") {
  75. this.write(packets);
  76. }
  77. else {
  78. // this might happen if the transport was silently closed in the beforeunload event handler
  79. debug("transport is not open, discarding packets");
  80. }
  81. }
  82. /**
  83. * Called upon open
  84. *
  85. * @protected
  86. */
  87. onOpen() {
  88. this.readyState = "open";
  89. this.writable = true;
  90. super.emitReserved("open");
  91. }
  92. /**
  93. * Called with data.
  94. *
  95. * @param {String} data
  96. * @protected
  97. */
  98. onData(data) {
  99. const packet = (0, engine_io_parser_1.decodePacket)(data, this.socket.binaryType);
  100. this.onPacket(packet);
  101. }
  102. /**
  103. * Called with a decoded packet.
  104. *
  105. * @protected
  106. */
  107. onPacket(packet) {
  108. super.emitReserved("packet", packet);
  109. }
  110. /**
  111. * Called upon close.
  112. *
  113. * @protected
  114. */
  115. onClose(details) {
  116. this.readyState = "closed";
  117. super.emitReserved("close", details);
  118. }
  119. /**
  120. * Pauses the transport, in order not to lose packets during an upgrade.
  121. *
  122. * @param onPause
  123. */
  124. pause(onPause) { }
  125. createUri(schema, query = {}) {
  126. return (schema +
  127. "://" +
  128. this._hostname() +
  129. this._port() +
  130. this.opts.path +
  131. this._query(query));
  132. }
  133. _hostname() {
  134. const hostname = this.opts.hostname;
  135. return hostname.indexOf(":") === -1 ? hostname : "[" + hostname + "]";
  136. }
  137. _port() {
  138. if (this.opts.port &&
  139. ((this.opts.secure && Number(this.opts.port !== 443)) ||
  140. (!this.opts.secure && Number(this.opts.port) !== 80))) {
  141. return ":" + this.opts.port;
  142. }
  143. else {
  144. return "";
  145. }
  146. }
  147. _query(query) {
  148. const encodedQuery = (0, parseqs_js_1.encode)(query);
  149. return encodedQuery.length ? "?" + encodedQuery : "";
  150. }
  151. }
  152. exports.Transport = Transport;