transport.js 3.6 KB

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