transport.js 4.0 KB

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