uniapp,h5

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