reconnecting-websocket.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589
  1. import "./chunk-2LSFTFF7.js";
  2. // node_modules/.pnpm/reconnecting-websocket@4.4.0/node_modules/reconnecting-websocket/dist/reconnecting-websocket-mjs.js
  3. var extendStatics = function(d, b) {
  4. extendStatics = Object.setPrototypeOf || { __proto__: [] } instanceof Array && function(d2, b2) {
  5. d2.__proto__ = b2;
  6. } || function(d2, b2) {
  7. for (var p in b2)
  8. if (b2.hasOwnProperty(p))
  9. d2[p] = b2[p];
  10. };
  11. return extendStatics(d, b);
  12. };
  13. function __extends(d, b) {
  14. extendStatics(d, b);
  15. function __() {
  16. this.constructor = d;
  17. }
  18. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  19. }
  20. function __values(o) {
  21. var m = typeof Symbol === "function" && o[Symbol.iterator], i = 0;
  22. if (m)
  23. return m.call(o);
  24. return {
  25. next: function() {
  26. if (o && i >= o.length)
  27. o = void 0;
  28. return { value: o && o[i++], done: !o };
  29. }
  30. };
  31. }
  32. function __read(o, n) {
  33. var m = typeof Symbol === "function" && o[Symbol.iterator];
  34. if (!m)
  35. return o;
  36. var i = m.call(o), r, ar = [], e;
  37. try {
  38. while ((n === void 0 || n-- > 0) && !(r = i.next()).done)
  39. ar.push(r.value);
  40. } catch (error) {
  41. e = { error };
  42. } finally {
  43. try {
  44. if (r && !r.done && (m = i["return"]))
  45. m.call(i);
  46. } finally {
  47. if (e)
  48. throw e.error;
  49. }
  50. }
  51. return ar;
  52. }
  53. function __spread() {
  54. for (var ar = [], i = 0; i < arguments.length; i++)
  55. ar = ar.concat(__read(arguments[i]));
  56. return ar;
  57. }
  58. var Event = (
  59. /** @class */
  60. function() {
  61. function Event2(type, target) {
  62. this.target = target;
  63. this.type = type;
  64. }
  65. return Event2;
  66. }()
  67. );
  68. var ErrorEvent = (
  69. /** @class */
  70. function(_super) {
  71. __extends(ErrorEvent2, _super);
  72. function ErrorEvent2(error, target) {
  73. var _this = _super.call(this, "error", target) || this;
  74. _this.message = error.message;
  75. _this.error = error;
  76. return _this;
  77. }
  78. return ErrorEvent2;
  79. }(Event)
  80. );
  81. var CloseEvent = (
  82. /** @class */
  83. function(_super) {
  84. __extends(CloseEvent2, _super);
  85. function CloseEvent2(code, reason, target) {
  86. if (code === void 0) {
  87. code = 1e3;
  88. }
  89. if (reason === void 0) {
  90. reason = "";
  91. }
  92. var _this = _super.call(this, "close", target) || this;
  93. _this.wasClean = true;
  94. _this.code = code;
  95. _this.reason = reason;
  96. return _this;
  97. }
  98. return CloseEvent2;
  99. }(Event)
  100. );
  101. var getGlobalWebSocket = function() {
  102. if (typeof WebSocket !== "undefined") {
  103. return WebSocket;
  104. }
  105. };
  106. var isWebSocket = function(w) {
  107. return typeof w !== "undefined" && !!w && w.CLOSING === 2;
  108. };
  109. var DEFAULT = {
  110. maxReconnectionDelay: 1e4,
  111. minReconnectionDelay: 1e3 + Math.random() * 4e3,
  112. minUptime: 5e3,
  113. reconnectionDelayGrowFactor: 1.3,
  114. connectionTimeout: 4e3,
  115. maxRetries: Infinity,
  116. maxEnqueuedMessages: Infinity,
  117. startClosed: false,
  118. debug: false
  119. };
  120. var ReconnectingWebSocket = (
  121. /** @class */
  122. function() {
  123. function ReconnectingWebSocket2(url, protocols, options) {
  124. var _this = this;
  125. if (options === void 0) {
  126. options = {};
  127. }
  128. this._listeners = {
  129. error: [],
  130. message: [],
  131. open: [],
  132. close: []
  133. };
  134. this._retryCount = -1;
  135. this._shouldReconnect = true;
  136. this._connectLock = false;
  137. this._binaryType = "blob";
  138. this._closeCalled = false;
  139. this._messageQueue = [];
  140. this.onclose = null;
  141. this.onerror = null;
  142. this.onmessage = null;
  143. this.onopen = null;
  144. this._handleOpen = function(event) {
  145. _this._debug("open event");
  146. var _a = _this._options.minUptime, minUptime = _a === void 0 ? DEFAULT.minUptime : _a;
  147. clearTimeout(_this._connectTimeout);
  148. _this._uptimeTimeout = setTimeout(function() {
  149. return _this._acceptOpen();
  150. }, minUptime);
  151. _this._ws.binaryType = _this._binaryType;
  152. _this._messageQueue.forEach(function(message) {
  153. return _this._ws.send(message);
  154. });
  155. _this._messageQueue = [];
  156. if (_this.onopen) {
  157. _this.onopen(event);
  158. }
  159. _this._listeners.open.forEach(function(listener) {
  160. return _this._callEventListener(event, listener);
  161. });
  162. };
  163. this._handleMessage = function(event) {
  164. _this._debug("message event");
  165. if (_this.onmessage) {
  166. _this.onmessage(event);
  167. }
  168. _this._listeners.message.forEach(function(listener) {
  169. return _this._callEventListener(event, listener);
  170. });
  171. };
  172. this._handleError = function(event) {
  173. _this._debug("error event", event.message);
  174. _this._disconnect(void 0, event.message === "TIMEOUT" ? "timeout" : void 0);
  175. if (_this.onerror) {
  176. _this.onerror(event);
  177. }
  178. _this._debug("exec error listeners");
  179. _this._listeners.error.forEach(function(listener) {
  180. return _this._callEventListener(event, listener);
  181. });
  182. _this._connect();
  183. };
  184. this._handleClose = function(event) {
  185. _this._debug("close event");
  186. _this._clearTimeouts();
  187. if (_this._shouldReconnect) {
  188. _this._connect();
  189. }
  190. if (_this.onclose) {
  191. _this.onclose(event);
  192. }
  193. _this._listeners.close.forEach(function(listener) {
  194. return _this._callEventListener(event, listener);
  195. });
  196. };
  197. this._url = url;
  198. this._protocols = protocols;
  199. this._options = options;
  200. if (this._options.startClosed) {
  201. this._shouldReconnect = false;
  202. }
  203. this._connect();
  204. }
  205. Object.defineProperty(ReconnectingWebSocket2, "CONNECTING", {
  206. get: function() {
  207. return 0;
  208. },
  209. enumerable: true,
  210. configurable: true
  211. });
  212. Object.defineProperty(ReconnectingWebSocket2, "OPEN", {
  213. get: function() {
  214. return 1;
  215. },
  216. enumerable: true,
  217. configurable: true
  218. });
  219. Object.defineProperty(ReconnectingWebSocket2, "CLOSING", {
  220. get: function() {
  221. return 2;
  222. },
  223. enumerable: true,
  224. configurable: true
  225. });
  226. Object.defineProperty(ReconnectingWebSocket2, "CLOSED", {
  227. get: function() {
  228. return 3;
  229. },
  230. enumerable: true,
  231. configurable: true
  232. });
  233. Object.defineProperty(ReconnectingWebSocket2.prototype, "CONNECTING", {
  234. get: function() {
  235. return ReconnectingWebSocket2.CONNECTING;
  236. },
  237. enumerable: true,
  238. configurable: true
  239. });
  240. Object.defineProperty(ReconnectingWebSocket2.prototype, "OPEN", {
  241. get: function() {
  242. return ReconnectingWebSocket2.OPEN;
  243. },
  244. enumerable: true,
  245. configurable: true
  246. });
  247. Object.defineProperty(ReconnectingWebSocket2.prototype, "CLOSING", {
  248. get: function() {
  249. return ReconnectingWebSocket2.CLOSING;
  250. },
  251. enumerable: true,
  252. configurable: true
  253. });
  254. Object.defineProperty(ReconnectingWebSocket2.prototype, "CLOSED", {
  255. get: function() {
  256. return ReconnectingWebSocket2.CLOSED;
  257. },
  258. enumerable: true,
  259. configurable: true
  260. });
  261. Object.defineProperty(ReconnectingWebSocket2.prototype, "binaryType", {
  262. get: function() {
  263. return this._ws ? this._ws.binaryType : this._binaryType;
  264. },
  265. set: function(value) {
  266. this._binaryType = value;
  267. if (this._ws) {
  268. this._ws.binaryType = value;
  269. }
  270. },
  271. enumerable: true,
  272. configurable: true
  273. });
  274. Object.defineProperty(ReconnectingWebSocket2.prototype, "retryCount", {
  275. /**
  276. * Returns the number or connection retries
  277. */
  278. get: function() {
  279. return Math.max(this._retryCount, 0);
  280. },
  281. enumerable: true,
  282. configurable: true
  283. });
  284. Object.defineProperty(ReconnectingWebSocket2.prototype, "bufferedAmount", {
  285. /**
  286. * The number of bytes of data that have been queued using calls to send() but not yet
  287. * transmitted to the network. This value resets to zero once all queued data has been sent.
  288. * This value does not reset to zero when the connection is closed; if you keep calling send(),
  289. * this will continue to climb. Read only
  290. */
  291. get: function() {
  292. var bytes = this._messageQueue.reduce(function(acc, message) {
  293. if (typeof message === "string") {
  294. acc += message.length;
  295. } else if (message instanceof Blob) {
  296. acc += message.size;
  297. } else {
  298. acc += message.byteLength;
  299. }
  300. return acc;
  301. }, 0);
  302. return bytes + (this._ws ? this._ws.bufferedAmount : 0);
  303. },
  304. enumerable: true,
  305. configurable: true
  306. });
  307. Object.defineProperty(ReconnectingWebSocket2.prototype, "extensions", {
  308. /**
  309. * The extensions selected by the server. This is currently only the empty string or a list of
  310. * extensions as negotiated by the connection
  311. */
  312. get: function() {
  313. return this._ws ? this._ws.extensions : "";
  314. },
  315. enumerable: true,
  316. configurable: true
  317. });
  318. Object.defineProperty(ReconnectingWebSocket2.prototype, "protocol", {
  319. /**
  320. * A string indicating the name of the sub-protocol the server selected;
  321. * this will be one of the strings specified in the protocols parameter when creating the
  322. * WebSocket object
  323. */
  324. get: function() {
  325. return this._ws ? this._ws.protocol : "";
  326. },
  327. enumerable: true,
  328. configurable: true
  329. });
  330. Object.defineProperty(ReconnectingWebSocket2.prototype, "readyState", {
  331. /**
  332. * The current state of the connection; this is one of the Ready state constants
  333. */
  334. get: function() {
  335. if (this._ws) {
  336. return this._ws.readyState;
  337. }
  338. return this._options.startClosed ? ReconnectingWebSocket2.CLOSED : ReconnectingWebSocket2.CONNECTING;
  339. },
  340. enumerable: true,
  341. configurable: true
  342. });
  343. Object.defineProperty(ReconnectingWebSocket2.prototype, "url", {
  344. /**
  345. * The URL as resolved by the constructor
  346. */
  347. get: function() {
  348. return this._ws ? this._ws.url : "";
  349. },
  350. enumerable: true,
  351. configurable: true
  352. });
  353. ReconnectingWebSocket2.prototype.close = function(code, reason) {
  354. if (code === void 0) {
  355. code = 1e3;
  356. }
  357. this._closeCalled = true;
  358. this._shouldReconnect = false;
  359. this._clearTimeouts();
  360. if (!this._ws) {
  361. this._debug("close enqueued: no ws instance");
  362. return;
  363. }
  364. if (this._ws.readyState === this.CLOSED) {
  365. this._debug("close: already closed");
  366. return;
  367. }
  368. this._ws.close(code, reason);
  369. };
  370. ReconnectingWebSocket2.prototype.reconnect = function(code, reason) {
  371. this._shouldReconnect = true;
  372. this._closeCalled = false;
  373. this._retryCount = -1;
  374. if (!this._ws || this._ws.readyState === this.CLOSED) {
  375. this._connect();
  376. } else {
  377. this._disconnect(code, reason);
  378. this._connect();
  379. }
  380. };
  381. ReconnectingWebSocket2.prototype.send = function(data) {
  382. if (this._ws && this._ws.readyState === this.OPEN) {
  383. this._debug("send", data);
  384. this._ws.send(data);
  385. } else {
  386. var _a = this._options.maxEnqueuedMessages, maxEnqueuedMessages = _a === void 0 ? DEFAULT.maxEnqueuedMessages : _a;
  387. if (this._messageQueue.length < maxEnqueuedMessages) {
  388. this._debug("enqueue", data);
  389. this._messageQueue.push(data);
  390. }
  391. }
  392. };
  393. ReconnectingWebSocket2.prototype.addEventListener = function(type, listener) {
  394. if (this._listeners[type]) {
  395. this._listeners[type].push(listener);
  396. }
  397. };
  398. ReconnectingWebSocket2.prototype.dispatchEvent = function(event) {
  399. var e_1, _a;
  400. var listeners = this._listeners[event.type];
  401. if (listeners) {
  402. try {
  403. for (var listeners_1 = __values(listeners), listeners_1_1 = listeners_1.next(); !listeners_1_1.done; listeners_1_1 = listeners_1.next()) {
  404. var listener = listeners_1_1.value;
  405. this._callEventListener(event, listener);
  406. }
  407. } catch (e_1_1) {
  408. e_1 = { error: e_1_1 };
  409. } finally {
  410. try {
  411. if (listeners_1_1 && !listeners_1_1.done && (_a = listeners_1.return))
  412. _a.call(listeners_1);
  413. } finally {
  414. if (e_1)
  415. throw e_1.error;
  416. }
  417. }
  418. }
  419. return true;
  420. };
  421. ReconnectingWebSocket2.prototype.removeEventListener = function(type, listener) {
  422. if (this._listeners[type]) {
  423. this._listeners[type] = this._listeners[type].filter(function(l) {
  424. return l !== listener;
  425. });
  426. }
  427. };
  428. ReconnectingWebSocket2.prototype._debug = function() {
  429. var args = [];
  430. for (var _i = 0; _i < arguments.length; _i++) {
  431. args[_i] = arguments[_i];
  432. }
  433. if (this._options.debug) {
  434. console.log.apply(console, __spread(["RWS>"], args));
  435. }
  436. };
  437. ReconnectingWebSocket2.prototype._getNextDelay = function() {
  438. var _a = this._options, _b = _a.reconnectionDelayGrowFactor, reconnectionDelayGrowFactor = _b === void 0 ? DEFAULT.reconnectionDelayGrowFactor : _b, _c = _a.minReconnectionDelay, minReconnectionDelay = _c === void 0 ? DEFAULT.minReconnectionDelay : _c, _d = _a.maxReconnectionDelay, maxReconnectionDelay = _d === void 0 ? DEFAULT.maxReconnectionDelay : _d;
  439. var delay = 0;
  440. if (this._retryCount > 0) {
  441. delay = minReconnectionDelay * Math.pow(reconnectionDelayGrowFactor, this._retryCount - 1);
  442. if (delay > maxReconnectionDelay) {
  443. delay = maxReconnectionDelay;
  444. }
  445. }
  446. this._debug("next delay", delay);
  447. return delay;
  448. };
  449. ReconnectingWebSocket2.prototype._wait = function() {
  450. var _this = this;
  451. return new Promise(function(resolve) {
  452. setTimeout(resolve, _this._getNextDelay());
  453. });
  454. };
  455. ReconnectingWebSocket2.prototype._getNextUrl = function(urlProvider) {
  456. if (typeof urlProvider === "string") {
  457. return Promise.resolve(urlProvider);
  458. }
  459. if (typeof urlProvider === "function") {
  460. var url = urlProvider();
  461. if (typeof url === "string") {
  462. return Promise.resolve(url);
  463. }
  464. if (!!url.then) {
  465. return url;
  466. }
  467. }
  468. throw Error("Invalid URL");
  469. };
  470. ReconnectingWebSocket2.prototype._connect = function() {
  471. var _this = this;
  472. if (this._connectLock || !this._shouldReconnect) {
  473. return;
  474. }
  475. this._connectLock = true;
  476. var _a = this._options, _b = _a.maxRetries, maxRetries = _b === void 0 ? DEFAULT.maxRetries : _b, _c = _a.connectionTimeout, connectionTimeout = _c === void 0 ? DEFAULT.connectionTimeout : _c, _d = _a.WebSocket, WebSocket2 = _d === void 0 ? getGlobalWebSocket() : _d;
  477. if (this._retryCount >= maxRetries) {
  478. this._debug("max retries reached", this._retryCount, ">=", maxRetries);
  479. return;
  480. }
  481. this._retryCount++;
  482. this._debug("connect", this._retryCount);
  483. this._removeListeners();
  484. if (!isWebSocket(WebSocket2)) {
  485. throw Error("No valid WebSocket class provided");
  486. }
  487. this._wait().then(function() {
  488. return _this._getNextUrl(_this._url);
  489. }).then(function(url) {
  490. if (_this._closeCalled) {
  491. return;
  492. }
  493. _this._debug("connect", { url, protocols: _this._protocols });
  494. _this._ws = _this._protocols ? new WebSocket2(url, _this._protocols) : new WebSocket2(url);
  495. _this._ws.binaryType = _this._binaryType;
  496. _this._connectLock = false;
  497. _this._addListeners();
  498. _this._connectTimeout = setTimeout(function() {
  499. return _this._handleTimeout();
  500. }, connectionTimeout);
  501. });
  502. };
  503. ReconnectingWebSocket2.prototype._handleTimeout = function() {
  504. this._debug("timeout event");
  505. this._handleError(new ErrorEvent(Error("TIMEOUT"), this));
  506. };
  507. ReconnectingWebSocket2.prototype._disconnect = function(code, reason) {
  508. if (code === void 0) {
  509. code = 1e3;
  510. }
  511. this._clearTimeouts();
  512. if (!this._ws) {
  513. return;
  514. }
  515. this._removeListeners();
  516. try {
  517. this._ws.close(code, reason);
  518. this._handleClose(new CloseEvent(code, reason, this));
  519. } catch (error) {
  520. }
  521. };
  522. ReconnectingWebSocket2.prototype._acceptOpen = function() {
  523. this._debug("accept open");
  524. this._retryCount = 0;
  525. };
  526. ReconnectingWebSocket2.prototype._callEventListener = function(event, listener) {
  527. if ("handleEvent" in listener) {
  528. listener.handleEvent(event);
  529. } else {
  530. listener(event);
  531. }
  532. };
  533. ReconnectingWebSocket2.prototype._removeListeners = function() {
  534. if (!this._ws) {
  535. return;
  536. }
  537. this._debug("removeListeners");
  538. this._ws.removeEventListener("open", this._handleOpen);
  539. this._ws.removeEventListener("close", this._handleClose);
  540. this._ws.removeEventListener("message", this._handleMessage);
  541. this._ws.removeEventListener("error", this._handleError);
  542. };
  543. ReconnectingWebSocket2.prototype._addListeners = function() {
  544. if (!this._ws) {
  545. return;
  546. }
  547. this._debug("addListeners");
  548. this._ws.addEventListener("open", this._handleOpen);
  549. this._ws.addEventListener("close", this._handleClose);
  550. this._ws.addEventListener("message", this._handleMessage);
  551. this._ws.addEventListener("error", this._handleError);
  552. };
  553. ReconnectingWebSocket2.prototype._clearTimeouts = function() {
  554. clearTimeout(this._connectTimeout);
  555. clearTimeout(this._uptimeTimeout);
  556. };
  557. return ReconnectingWebSocket2;
  558. }()
  559. );
  560. var reconnecting_websocket_mjs_default = ReconnectingWebSocket;
  561. export {
  562. reconnecting_websocket_mjs_default as default
  563. };
  564. /*! Bundled license information:
  565. reconnecting-websocket/dist/reconnecting-websocket-mjs.js:
  566. (*! *****************************************************************************
  567. Copyright (c) Microsoft Corporation. All rights reserved.
  568. Licensed under the Apache License, Version 2.0 (the "License"); you may not use
  569. this file except in compliance with the License. You may obtain a copy of the
  570. License at http://www.apache.org/licenses/LICENSE-2.0
  571. THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  572. KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
  573. WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
  574. MERCHANTABLITY OR NON-INFRINGEMENT.
  575. See the Apache Version 2.0 License for specific language governing permissions
  576. and limitations under the License.
  577. ***************************************************************************** *)
  578. (*!
  579. * Reconnecting WebSocket
  580. * by Pedro Ladaria <pedro.ladaria@gmail.com>
  581. * https://github.com/pladaria/reconnecting-websocket
  582. * License MIT
  583. *)
  584. */
  585. //# sourceMappingURL=reconnecting-websocket.js.map