zignal-stencil-library 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (294) hide show
  1. package/LICENSE +21 -0
  2. package/dist/cjs/app-globals-V2Kpy_OQ.js +8 -0
  3. package/dist/cjs/app-globals-V2Kpy_OQ.js.map +1 -0
  4. package/dist/cjs/autocomplete-select_18.cjs.entry.js +41433 -0
  5. package/dist/cjs/autocomplete-select_18.cjs.entry.js.map +1 -0
  6. package/dist/cjs/index-C4CtrJXU.js +2199 -0
  7. package/dist/cjs/index-C4CtrJXU.js.map +1 -0
  8. package/dist/cjs/index-tRPYdc8n.js +1927 -0
  9. package/dist/cjs/index-tRPYdc8n.js.map +1 -0
  10. package/dist/cjs/index.cjs.js +7 -0
  11. package/dist/cjs/index.cjs.js.map +1 -0
  12. package/dist/cjs/loader.cjs.js +16 -0
  13. package/dist/cjs/loader.cjs.js.map +1 -0
  14. package/dist/cjs/zignal-stencil-library.cjs.js +28 -0
  15. package/dist/cjs/zignal-stencil-library.cjs.js.map +1 -0
  16. package/dist/collection/api/index.js +132 -0
  17. package/dist/collection/api/index.js.map +1 -0
  18. package/dist/collection/collection-manifest.json +29 -0
  19. package/dist/collection/components/autocomplete-select/autocomplete-select.css +1 -0
  20. package/dist/collection/components/autocomplete-select/autocomplete-select.js +230 -0
  21. package/dist/collection/components/autocomplete-select/autocomplete-select.js.map +1 -0
  22. package/dist/collection/components/channel-box/channel-box.css +1 -0
  23. package/dist/collection/components/channel-box/channel-box.js +93 -0
  24. package/dist/collection/components/channel-box/channel-box.js.map +1 -0
  25. package/dist/collection/components/chat-app/chat-app.css +1 -0
  26. package/dist/collection/components/chat-app/chat-app.js +227 -0
  27. package/dist/collection/components/chat-app/chat-app.js.map +1 -0
  28. package/dist/collection/components/chat-header/chat-header.css +1 -0
  29. package/dist/collection/components/chat-header/chat-header.js +45 -0
  30. package/dist/collection/components/chat-header/chat-header.js.map +1 -0
  31. package/dist/collection/components/chat-input/chat-input.css +1 -0
  32. package/dist/collection/components/chat-input/chat-input.js +118 -0
  33. package/dist/collection/components/chat-input/chat-input.js.map +1 -0
  34. package/dist/collection/components/dialog-editor/dialog-editor.css +1 -0
  35. package/dist/collection/components/dialog-editor/dialog-editor.js +176 -0
  36. package/dist/collection/components/dialog-editor/dialog-editor.js.map +1 -0
  37. package/dist/collection/components/dialog-preview-media/dialog-preview-media.css +1 -0
  38. package/dist/collection/components/dialog-preview-media/dialog-preview-media.js +96 -0
  39. package/dist/collection/components/dialog-preview-media/dialog-preview-media.js.map +1 -0
  40. package/dist/collection/components/dialog-search/dialog-search.css +1 -0
  41. package/dist/collection/components/dialog-search/dialog-search.js +105 -0
  42. package/dist/collection/components/dialog-search/dialog-search.js.map +1 -0
  43. package/dist/collection/components/internal-component/chat-item.js +28 -0
  44. package/dist/collection/components/internal-component/chat-item.js.map +1 -0
  45. package/dist/collection/components/internal-component/icon.js +9 -0
  46. package/dist/collection/components/internal-component/icon.js.map +1 -0
  47. package/dist/collection/components/internal-component/tag-or-status-item.js +9 -0
  48. package/dist/collection/components/internal-component/tag-or-status-item.js.map +1 -0
  49. package/dist/collection/components/internal-component/z-button.js +24 -0
  50. package/dist/collection/components/internal-component/z-button.js.map +1 -0
  51. package/dist/collection/components/internal-component/z-icon-button.js +23 -0
  52. package/dist/collection/components/internal-component/z-icon-button.js.map +1 -0
  53. package/dist/collection/components/internal-component/z-input-area.js +8 -0
  54. package/dist/collection/components/internal-component/z-input-area.js.map +1 -0
  55. package/dist/collection/components/internal-component/z-input.js +8 -0
  56. package/dist/collection/components/internal-component/z-input.js.map +1 -0
  57. package/dist/collection/components/internal-component/z-select-input.js +9 -0
  58. package/dist/collection/components/internal-component/z-select-input.js.map +1 -0
  59. package/dist/collection/components/menu-list/menu-list.css +1 -0
  60. package/dist/collection/components/menu-list/menu-list.js +168 -0
  61. package/dist/collection/components/menu-list/menu-list.js.map +1 -0
  62. package/dist/collection/components/message-box/message-box.css +1 -0
  63. package/dist/collection/components/message-box/message-box.js +179 -0
  64. package/dist/collection/components/message-box/message-box.js.map +1 -0
  65. package/dist/collection/components/message-content/message-content.css +1 -0
  66. package/dist/collection/components/message-content/message-content.js +98 -0
  67. package/dist/collection/components/message-content/message-content.js.map +1 -0
  68. package/dist/collection/components/message-item/message-item.css +1 -0
  69. package/dist/collection/components/message-item/message-item.js +115 -0
  70. package/dist/collection/components/message-item/message-item.js.map +1 -0
  71. package/dist/collection/components/profile-history/profile-history.css +1 -0
  72. package/dist/collection/components/profile-history/profile-history.js +18 -0
  73. package/dist/collection/components/profile-history/profile-history.js.map +1 -0
  74. package/dist/collection/components/profile-info/profile-info.css +1 -0
  75. package/dist/collection/components/profile-info/profile-info.js +255 -0
  76. package/dist/collection/components/profile-info/profile-info.js.map +1 -0
  77. package/dist/collection/components/profile-media/profile-media.css +1 -0
  78. package/dist/collection/components/profile-media/profile-media.js +18 -0
  79. package/dist/collection/components/profile-media/profile-media.js.map +1 -0
  80. package/dist/collection/components/profile-tabs/profile-tabs.css +1 -0
  81. package/dist/collection/components/profile-tabs/profile-tabs.js +43 -0
  82. package/dist/collection/components/profile-tabs/profile-tabs.js.map +1 -0
  83. package/dist/collection/components/search-box/search-box.css +1 -0
  84. package/dist/collection/components/search-box/search-box.js +71 -0
  85. package/dist/collection/components/search-box/search-box.js.map +1 -0
  86. package/dist/collection/components/tag-popper/tag-popper.css +1 -0
  87. package/dist/collection/components/tag-popper/tag-popper.js +124 -0
  88. package/dist/collection/components/tag-popper/tag-popper.js.map +1 -0
  89. package/dist/collection/global/i18n.js +13 -0
  90. package/dist/collection/global/i18n.js.map +1 -0
  91. package/dist/collection/index.js +11 -0
  92. package/dist/collection/index.js.map +1 -0
  93. package/dist/collection/postcss/property-to-variable.js +54 -0
  94. package/dist/collection/store/store.js +25 -0
  95. package/dist/collection/store/store.js.map +1 -0
  96. package/dist/collection/utils/axios.js +11 -0
  97. package/dist/collection/utils/axios.js.map +1 -0
  98. package/dist/collection/utils/constant.js +5 -0
  99. package/dist/collection/utils/constant.js.map +1 -0
  100. package/dist/collection/utils/dayjs.js +175 -0
  101. package/dist/collection/utils/dayjs.js.map +1 -0
  102. package/dist/collection/utils/socket.js +59 -0
  103. package/dist/collection/utils/socket.js.map +1 -0
  104. package/dist/collection/utils/utils.js +8 -0
  105. package/dist/collection/utils/utils.js.map +1 -0
  106. package/dist/components/autocomplete-select.d.ts +11 -0
  107. package/dist/components/autocomplete-select.js +9 -0
  108. package/dist/components/autocomplete-select.js.map +1 -0
  109. package/dist/components/channel-box.d.ts +11 -0
  110. package/dist/components/channel-box.js +9 -0
  111. package/dist/components/channel-box.js.map +1 -0
  112. package/dist/components/chat-app.d.ts +11 -0
  113. package/dist/components/chat-app.js +226 -0
  114. package/dist/components/chat-app.js.map +1 -0
  115. package/dist/components/chat-header.d.ts +11 -0
  116. package/dist/components/chat-header.js +9 -0
  117. package/dist/components/chat-header.js.map +1 -0
  118. package/dist/components/chat-input.d.ts +11 -0
  119. package/dist/components/chat-input.js +9 -0
  120. package/dist/components/chat-input.js.map +1 -0
  121. package/dist/components/dialog-editor.d.ts +11 -0
  122. package/dist/components/dialog-editor.js +9 -0
  123. package/dist/components/dialog-editor.js.map +1 -0
  124. package/dist/components/dialog-preview-media.d.ts +11 -0
  125. package/dist/components/dialog-preview-media.js +9 -0
  126. package/dist/components/dialog-preview-media.js.map +1 -0
  127. package/dist/components/dialog-search.d.ts +11 -0
  128. package/dist/components/dialog-search.js +9 -0
  129. package/dist/components/dialog-search.js.map +1 -0
  130. package/dist/components/index.d.ts +33 -0
  131. package/dist/components/index.js +2199 -0
  132. package/dist/components/index.js.map +1 -0
  133. package/dist/components/menu-list.d.ts +11 -0
  134. package/dist/components/menu-list.js +9 -0
  135. package/dist/components/menu-list.js.map +1 -0
  136. package/dist/components/message-box.d.ts +11 -0
  137. package/dist/components/message-box.js +9 -0
  138. package/dist/components/message-box.js.map +1 -0
  139. package/dist/components/message-content.d.ts +11 -0
  140. package/dist/components/message-content.js +9 -0
  141. package/dist/components/message-content.js.map +1 -0
  142. package/dist/components/message-item.d.ts +11 -0
  143. package/dist/components/message-item.js +9 -0
  144. package/dist/components/message-item.js.map +1 -0
  145. package/dist/components/p-6xqe-pLQ.js +1509 -0
  146. package/dist/components/p-6xqe-pLQ.js.map +1 -0
  147. package/dist/components/p-B8jcXcXq.js +10 -0
  148. package/dist/components/p-B8jcXcXq.js.map +1 -0
  149. package/dist/components/p-BAxNTWPI.js +32 -0
  150. package/dist/components/p-BAxNTWPI.js.map +1 -0
  151. package/dist/components/p-BZ4TvILZ.js +24 -0
  152. package/dist/components/p-BZ4TvILZ.js.map +1 -0
  153. package/dist/components/p-BZ_KFdA7.js +99 -0
  154. package/dist/components/p-BZ_KFdA7.js.map +1 -0
  155. package/dist/components/p-BaEi6GXz.js +27448 -0
  156. package/dist/components/p-BaEi6GXz.js.map +1 -0
  157. package/dist/components/p-BcC6bTqM.js +104 -0
  158. package/dist/components/p-BcC6bTqM.js.map +1 -0
  159. package/dist/components/p-Beycm8NN.js +3027 -0
  160. package/dist/components/p-Beycm8NN.js.map +1 -0
  161. package/dist/components/p-Bf89k6bd.js +3932 -0
  162. package/dist/components/p-Bf89k6bd.js.map +1 -0
  163. package/dist/components/p-BtkLsQfZ.js +17 -0
  164. package/dist/components/p-BtkLsQfZ.js.map +1 -0
  165. package/dist/components/p-C9EokLnL.js +102 -0
  166. package/dist/components/p-C9EokLnL.js.map +1 -0
  167. package/dist/components/p-CHaS9akB.js +33 -0
  168. package/dist/components/p-CHaS9akB.js.map +1 -0
  169. package/dist/components/p-CQZRPVaY.js +1715 -0
  170. package/dist/components/p-CQZRPVaY.js.map +1 -0
  171. package/dist/components/p-CS8eN-II.js +60 -0
  172. package/dist/components/p-CS8eN-II.js.map +1 -0
  173. package/dist/components/p-CUmphmPD.js +48 -0
  174. package/dist/components/p-CUmphmPD.js.map +1 -0
  175. package/dist/components/p-ChdOg2lI.js +90 -0
  176. package/dist/components/p-ChdOg2lI.js.map +1 -0
  177. package/dist/components/p-CkqA8kPB.js +210 -0
  178. package/dist/components/p-CkqA8kPB.js.map +1 -0
  179. package/dist/components/p-CmWIRJ5t.js +91 -0
  180. package/dist/components/p-CmWIRJ5t.js.map +1 -0
  181. package/dist/components/p-CzESq-LE.js +88 -0
  182. package/dist/components/p-CzESq-LE.js.map +1 -0
  183. package/dist/components/p-DD3M-jE2.js +87 -0
  184. package/dist/components/p-DD3M-jE2.js.map +1 -0
  185. package/dist/components/p-DFjLV5Vy.js +71 -0
  186. package/dist/components/p-DFjLV5Vy.js.map +1 -0
  187. package/dist/components/p-DYI8iHdE.js +279 -0
  188. package/dist/components/p-DYI8iHdE.js.map +1 -0
  189. package/dist/components/p-DfRY75vS.js +16 -0
  190. package/dist/components/p-DfRY75vS.js.map +1 -0
  191. package/dist/components/p-DspEZ2pG.js +85 -0
  192. package/dist/components/p-DspEZ2pG.js.map +1 -0
  193. package/dist/components/p-LCZ_Vyoy.js +4084 -0
  194. package/dist/components/p-LCZ_Vyoy.js.map +1 -0
  195. package/dist/components/p-N-wb2wDd.js +236 -0
  196. package/dist/components/p-N-wb2wDd.js.map +1 -0
  197. package/dist/components/p-Urxy5c9-.js +154 -0
  198. package/dist/components/p-Urxy5c9-.js.map +1 -0
  199. package/dist/components/p-nz95LAGz.js +33 -0
  200. package/dist/components/p-nz95LAGz.js.map +1 -0
  201. package/dist/components/profile-history.d.ts +11 -0
  202. package/dist/components/profile-history.js +9 -0
  203. package/dist/components/profile-history.js.map +1 -0
  204. package/dist/components/profile-info.d.ts +11 -0
  205. package/dist/components/profile-info.js +9 -0
  206. package/dist/components/profile-info.js.map +1 -0
  207. package/dist/components/profile-media.d.ts +11 -0
  208. package/dist/components/profile-media.js +9 -0
  209. package/dist/components/profile-media.js.map +1 -0
  210. package/dist/components/profile-tabs.d.ts +11 -0
  211. package/dist/components/profile-tabs.js +9 -0
  212. package/dist/components/profile-tabs.js.map +1 -0
  213. package/dist/components/search-box.d.ts +11 -0
  214. package/dist/components/search-box.js +9 -0
  215. package/dist/components/search-box.js.map +1 -0
  216. package/dist/components/tag-popper.d.ts +11 -0
  217. package/dist/components/tag-popper.js +9 -0
  218. package/dist/components/tag-popper.js.map +1 -0
  219. package/dist/esm/app-globals-DQuL1Twl.js +6 -0
  220. package/dist/esm/app-globals-DQuL1Twl.js.map +1 -0
  221. package/dist/esm/autocomplete-select.channel-box.chat-app.chat-header.chat-input.dialog-editor.dialog-preview-media.dialog-search.menu-list.message-box.message-content.message-item.profile-history.profile-info.profile-media.profile-tabs.search-box.tag-popper.entry.js.map +1 -0
  222. package/dist/esm/autocomplete-select_18.entry.js +41414 -0
  223. package/dist/esm/autocomplete-select_18.entry.js.map +1 -0
  224. package/dist/esm/index-C0ey1F02.js +1917 -0
  225. package/dist/esm/index-C0ey1F02.js.map +1 -0
  226. package/dist/esm/index-DasOEma2.js +2197 -0
  227. package/dist/esm/index-DasOEma2.js.map +1 -0
  228. package/dist/esm/index.js +4 -0
  229. package/dist/esm/index.js.map +1 -0
  230. package/dist/esm/loader.js +14 -0
  231. package/dist/esm/loader.js.map +1 -0
  232. package/dist/esm/zignal-stencil-library.js +24 -0
  233. package/dist/esm/zignal-stencil-library.js.map +1 -0
  234. package/dist/index.cjs.js +1 -0
  235. package/dist/index.js +1 -0
  236. package/dist/types/api/index.d.ts +12 -0
  237. package/dist/types/api/index.type.d.ts +98 -0
  238. package/dist/types/components/autocomplete-select/autocomplete-select.d.ts +19 -0
  239. package/dist/types/components/channel-box/channel-box.d.ts +11 -0
  240. package/dist/types/components/chat-app/chat-app.d.ts +25 -0
  241. package/dist/types/components/chat-header/chat-header.d.ts +3 -0
  242. package/dist/types/components/chat-input/chat-input.d.ts +9 -0
  243. package/dist/types/components/dialog-editor/dialog-editor.d.ts +16 -0
  244. package/dist/types/components/dialog-preview-media/dialog-preview-media.d.ts +8 -0
  245. package/dist/types/components/dialog-search/dialog-search.d.ts +18 -0
  246. package/dist/types/components/internal-component/chat-item.d.ts +6 -0
  247. package/dist/types/components/internal-component/icon.d.ts +7 -0
  248. package/dist/types/components/internal-component/tag-or-status-item.d.ts +7 -0
  249. package/dist/types/components/internal-component/z-button.d.ts +15 -0
  250. package/dist/types/components/internal-component/z-icon-button.d.ts +13 -0
  251. package/dist/types/components/internal-component/z-input-area.d.ts +13 -0
  252. package/dist/types/components/internal-component/z-input.d.ts +13 -0
  253. package/dist/types/components/internal-component/z-select-input.d.ts +13 -0
  254. package/dist/types/components/menu-list/menu-list.d.ts +17 -0
  255. package/dist/types/components/message-box/message-box.d.ts +19 -0
  256. package/dist/types/components/message-content/message-content.d.ts +8 -0
  257. package/dist/types/components/message-item/message-item.d.ts +9 -0
  258. package/dist/types/components/profile-history/profile-history.d.ts +3 -0
  259. package/dist/types/components/profile-info/profile-info.d.ts +29 -0
  260. package/dist/types/components/profile-media/profile-media.d.ts +3 -0
  261. package/dist/types/components/profile-tabs/profile-tabs.d.ts +8 -0
  262. package/dist/types/components/search-box/search-box.d.ts +10 -0
  263. package/dist/types/components/tag-popper/tag-popper.d.ts +13 -0
  264. package/dist/types/components.d.ts +498 -0
  265. package/dist/types/global/i18n.d.ts +2 -0
  266. package/dist/types/index.d.ts +11 -0
  267. package/dist/types/stencil-public-runtime.d.ts +1709 -0
  268. package/dist/types/store/store.d.ts +16 -0
  269. package/dist/types/store/store.type.d.ts +132 -0
  270. package/dist/types/utils/axios.d.ts +1 -0
  271. package/dist/types/utils/constant.d.ts +1 -0
  272. package/dist/types/utils/dayjs.d.ts +31 -0
  273. package/dist/types/utils/socket.d.ts +6 -0
  274. package/dist/types/utils/utils.d.ts +2 -0
  275. package/dist/zignal-stencil-library/index.esm.js +2 -0
  276. package/dist/zignal-stencil-library/index.esm.js.map +1 -0
  277. package/dist/zignal-stencil-library/loader.esm.js.map +1 -0
  278. package/dist/zignal-stencil-library/p-C0ey1F02.js +3 -0
  279. package/dist/zignal-stencil-library/p-C0ey1F02.js.map +1 -0
  280. package/dist/zignal-stencil-library/p-DQuL1Twl.js +2 -0
  281. package/dist/zignal-stencil-library/p-DQuL1Twl.js.map +1 -0
  282. package/dist/zignal-stencil-library/p-DasOEma2.js +2 -0
  283. package/dist/zignal-stencil-library/p-DasOEma2.js.map +1 -0
  284. package/dist/zignal-stencil-library/p-ee2ebe92.entry.js +9777 -0
  285. package/dist/zignal-stencil-library/p-ee2ebe92.entry.js.map +1 -0
  286. package/dist/zignal-stencil-library/zignal-stencil-library.css +1 -0
  287. package/dist/zignal-stencil-library/zignal-stencil-library.esm.js +2 -0
  288. package/dist/zignal-stencil-library/zignal-stencil-library.esm.js.map +1 -0
  289. package/loader/cdn.js +1 -0
  290. package/loader/index.cjs.js +1 -0
  291. package/loader/index.d.ts +24 -0
  292. package/loader/index.es2017.js +1 -0
  293. package/loader/index.js +2 -0
  294. package/package.json +73 -0
@@ -0,0 +1,4084 @@
1
+ import { k as baseURL } from './p-Bf89k6bd.js';
2
+ import { c as configStore } from './p-N-wb2wDd.js';
3
+
4
+ const PACKET_TYPES = Object.create(null); // no Map = no polyfill
5
+ PACKET_TYPES["open"] = "0";
6
+ PACKET_TYPES["close"] = "1";
7
+ PACKET_TYPES["ping"] = "2";
8
+ PACKET_TYPES["pong"] = "3";
9
+ PACKET_TYPES["message"] = "4";
10
+ PACKET_TYPES["upgrade"] = "5";
11
+ PACKET_TYPES["noop"] = "6";
12
+ const PACKET_TYPES_REVERSE = Object.create(null);
13
+ Object.keys(PACKET_TYPES).forEach((key) => {
14
+ PACKET_TYPES_REVERSE[PACKET_TYPES[key]] = key;
15
+ });
16
+ const ERROR_PACKET = { type: "error", data: "parser error" };
17
+
18
+ const withNativeBlob$1 = typeof Blob === "function" ||
19
+ (typeof Blob !== "undefined" &&
20
+ Object.prototype.toString.call(Blob) === "[object BlobConstructor]");
21
+ const withNativeArrayBuffer$2 = typeof ArrayBuffer === "function";
22
+ // ArrayBuffer.isView method is not defined in IE10
23
+ const isView$1 = (obj) => {
24
+ return typeof ArrayBuffer.isView === "function"
25
+ ? ArrayBuffer.isView(obj)
26
+ : obj && obj.buffer instanceof ArrayBuffer;
27
+ };
28
+ const encodePacket = ({ type, data }, supportsBinary, callback) => {
29
+ if (withNativeBlob$1 && data instanceof Blob) {
30
+ if (supportsBinary) {
31
+ return callback(data);
32
+ }
33
+ else {
34
+ return encodeBlobAsBase64(data, callback);
35
+ }
36
+ }
37
+ else if (withNativeArrayBuffer$2 &&
38
+ (data instanceof ArrayBuffer || isView$1(data))) {
39
+ if (supportsBinary) {
40
+ return callback(data);
41
+ }
42
+ else {
43
+ return encodeBlobAsBase64(new Blob([data]), callback);
44
+ }
45
+ }
46
+ // plain string
47
+ return callback(PACKET_TYPES[type] + (data || ""));
48
+ };
49
+ const encodeBlobAsBase64 = (data, callback) => {
50
+ const fileReader = new FileReader();
51
+ fileReader.onload = function () {
52
+ const content = fileReader.result.split(",")[1];
53
+ callback("b" + (content || ""));
54
+ };
55
+ return fileReader.readAsDataURL(data);
56
+ };
57
+ function toArray(data) {
58
+ if (data instanceof Uint8Array) {
59
+ return data;
60
+ }
61
+ else if (data instanceof ArrayBuffer) {
62
+ return new Uint8Array(data);
63
+ }
64
+ else {
65
+ return new Uint8Array(data.buffer, data.byteOffset, data.byteLength);
66
+ }
67
+ }
68
+ let TEXT_ENCODER;
69
+ function encodePacketToBinary(packet, callback) {
70
+ if (withNativeBlob$1 && packet.data instanceof Blob) {
71
+ return packet.data.arrayBuffer().then(toArray).then(callback);
72
+ }
73
+ else if (withNativeArrayBuffer$2 &&
74
+ (packet.data instanceof ArrayBuffer || isView$1(packet.data))) {
75
+ return callback(toArray(packet.data));
76
+ }
77
+ encodePacket(packet, false, (encoded) => {
78
+ if (!TEXT_ENCODER) {
79
+ TEXT_ENCODER = new TextEncoder();
80
+ }
81
+ callback(TEXT_ENCODER.encode(encoded));
82
+ });
83
+ }
84
+
85
+ // imported from https://github.com/socketio/base64-arraybuffer
86
+ const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
87
+ // Use a lookup table to find the index.
88
+ const lookup$1 = typeof Uint8Array === 'undefined' ? [] : new Uint8Array(256);
89
+ for (let i = 0; i < chars.length; i++) {
90
+ lookup$1[chars.charCodeAt(i)] = i;
91
+ }
92
+ const decode$1 = (base64) => {
93
+ let bufferLength = base64.length * 0.75, len = base64.length, i, p = 0, encoded1, encoded2, encoded3, encoded4;
94
+ if (base64[base64.length - 1] === '=') {
95
+ bufferLength--;
96
+ if (base64[base64.length - 2] === '=') {
97
+ bufferLength--;
98
+ }
99
+ }
100
+ const arraybuffer = new ArrayBuffer(bufferLength), bytes = new Uint8Array(arraybuffer);
101
+ for (i = 0; i < len; i += 4) {
102
+ encoded1 = lookup$1[base64.charCodeAt(i)];
103
+ encoded2 = lookup$1[base64.charCodeAt(i + 1)];
104
+ encoded3 = lookup$1[base64.charCodeAt(i + 2)];
105
+ encoded4 = lookup$1[base64.charCodeAt(i + 3)];
106
+ bytes[p++] = (encoded1 << 2) | (encoded2 >> 4);
107
+ bytes[p++] = ((encoded2 & 15) << 4) | (encoded3 >> 2);
108
+ bytes[p++] = ((encoded3 & 3) << 6) | (encoded4 & 63);
109
+ }
110
+ return arraybuffer;
111
+ };
112
+
113
+ const withNativeArrayBuffer$1 = typeof ArrayBuffer === "function";
114
+ const decodePacket = (encodedPacket, binaryType) => {
115
+ if (typeof encodedPacket !== "string") {
116
+ return {
117
+ type: "message",
118
+ data: mapBinary(encodedPacket, binaryType),
119
+ };
120
+ }
121
+ const type = encodedPacket.charAt(0);
122
+ if (type === "b") {
123
+ return {
124
+ type: "message",
125
+ data: decodeBase64Packet(encodedPacket.substring(1), binaryType),
126
+ };
127
+ }
128
+ const packetType = PACKET_TYPES_REVERSE[type];
129
+ if (!packetType) {
130
+ return ERROR_PACKET;
131
+ }
132
+ return encodedPacket.length > 1
133
+ ? {
134
+ type: PACKET_TYPES_REVERSE[type],
135
+ data: encodedPacket.substring(1),
136
+ }
137
+ : {
138
+ type: PACKET_TYPES_REVERSE[type],
139
+ };
140
+ };
141
+ const decodeBase64Packet = (data, binaryType) => {
142
+ if (withNativeArrayBuffer$1) {
143
+ const decoded = decode$1(data);
144
+ return mapBinary(decoded, binaryType);
145
+ }
146
+ else {
147
+ return { base64: true, data }; // fallback for old browsers
148
+ }
149
+ };
150
+ const mapBinary = (data, binaryType) => {
151
+ switch (binaryType) {
152
+ case "blob":
153
+ if (data instanceof Blob) {
154
+ // from WebSocket + binaryType "blob"
155
+ return data;
156
+ }
157
+ else {
158
+ // from HTTP long-polling or WebTransport
159
+ return new Blob([data]);
160
+ }
161
+ case "arraybuffer":
162
+ default:
163
+ if (data instanceof ArrayBuffer) {
164
+ // from HTTP long-polling (base64) or WebSocket + binaryType "arraybuffer"
165
+ return data;
166
+ }
167
+ else {
168
+ // from WebTransport (Uint8Array)
169
+ return data.buffer;
170
+ }
171
+ }
172
+ };
173
+
174
+ const SEPARATOR = String.fromCharCode(30); // see https://en.wikipedia.org/wiki/Delimiter#ASCII_delimited_text
175
+ const encodePayload = (packets, callback) => {
176
+ // some packets may be added to the array while encoding, so the initial length must be saved
177
+ const length = packets.length;
178
+ const encodedPackets = new Array(length);
179
+ let count = 0;
180
+ packets.forEach((packet, i) => {
181
+ // force base64 encoding for binary packets
182
+ encodePacket(packet, false, (encodedPacket) => {
183
+ encodedPackets[i] = encodedPacket;
184
+ if (++count === length) {
185
+ callback(encodedPackets.join(SEPARATOR));
186
+ }
187
+ });
188
+ });
189
+ };
190
+ const decodePayload = (encodedPayload, binaryType) => {
191
+ const encodedPackets = encodedPayload.split(SEPARATOR);
192
+ const packets = [];
193
+ for (let i = 0; i < encodedPackets.length; i++) {
194
+ const decodedPacket = decodePacket(encodedPackets[i], binaryType);
195
+ packets.push(decodedPacket);
196
+ if (decodedPacket.type === "error") {
197
+ break;
198
+ }
199
+ }
200
+ return packets;
201
+ };
202
+ function createPacketEncoderStream() {
203
+ return new TransformStream({
204
+ transform(packet, controller) {
205
+ encodePacketToBinary(packet, (encodedPacket) => {
206
+ const payloadLength = encodedPacket.length;
207
+ let header;
208
+ // inspired by the WebSocket format: https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API/Writing_WebSocket_servers#decoding_payload_length
209
+ if (payloadLength < 126) {
210
+ header = new Uint8Array(1);
211
+ new DataView(header.buffer).setUint8(0, payloadLength);
212
+ }
213
+ else if (payloadLength < 65536) {
214
+ header = new Uint8Array(3);
215
+ const view = new DataView(header.buffer);
216
+ view.setUint8(0, 126);
217
+ view.setUint16(1, payloadLength);
218
+ }
219
+ else {
220
+ header = new Uint8Array(9);
221
+ const view = new DataView(header.buffer);
222
+ view.setUint8(0, 127);
223
+ view.setBigUint64(1, BigInt(payloadLength));
224
+ }
225
+ // first bit indicates whether the payload is plain text (0) or binary (1)
226
+ if (packet.data && typeof packet.data !== "string") {
227
+ header[0] |= 0x80;
228
+ }
229
+ controller.enqueue(header);
230
+ controller.enqueue(encodedPacket);
231
+ });
232
+ },
233
+ });
234
+ }
235
+ let TEXT_DECODER;
236
+ function totalLength(chunks) {
237
+ return chunks.reduce((acc, chunk) => acc + chunk.length, 0);
238
+ }
239
+ function concatChunks(chunks, size) {
240
+ if (chunks[0].length === size) {
241
+ return chunks.shift();
242
+ }
243
+ const buffer = new Uint8Array(size);
244
+ let j = 0;
245
+ for (let i = 0; i < size; i++) {
246
+ buffer[i] = chunks[0][j++];
247
+ if (j === chunks[0].length) {
248
+ chunks.shift();
249
+ j = 0;
250
+ }
251
+ }
252
+ if (chunks.length && j < chunks[0].length) {
253
+ chunks[0] = chunks[0].slice(j);
254
+ }
255
+ return buffer;
256
+ }
257
+ function createPacketDecoderStream(maxPayload, binaryType) {
258
+ if (!TEXT_DECODER) {
259
+ TEXT_DECODER = new TextDecoder();
260
+ }
261
+ const chunks = [];
262
+ let state = 0 /* State.READ_HEADER */;
263
+ let expectedLength = -1;
264
+ let isBinary = false;
265
+ return new TransformStream({
266
+ transform(chunk, controller) {
267
+ chunks.push(chunk);
268
+ while (true) {
269
+ if (state === 0 /* State.READ_HEADER */) {
270
+ if (totalLength(chunks) < 1) {
271
+ break;
272
+ }
273
+ const header = concatChunks(chunks, 1);
274
+ isBinary = (header[0] & 0x80) === 0x80;
275
+ expectedLength = header[0] & 0x7f;
276
+ if (expectedLength < 126) {
277
+ state = 3 /* State.READ_PAYLOAD */;
278
+ }
279
+ else if (expectedLength === 126) {
280
+ state = 1 /* State.READ_EXTENDED_LENGTH_16 */;
281
+ }
282
+ else {
283
+ state = 2 /* State.READ_EXTENDED_LENGTH_64 */;
284
+ }
285
+ }
286
+ else if (state === 1 /* State.READ_EXTENDED_LENGTH_16 */) {
287
+ if (totalLength(chunks) < 2) {
288
+ break;
289
+ }
290
+ const headerArray = concatChunks(chunks, 2);
291
+ expectedLength = new DataView(headerArray.buffer, headerArray.byteOffset, headerArray.length).getUint16(0);
292
+ state = 3 /* State.READ_PAYLOAD */;
293
+ }
294
+ else if (state === 2 /* State.READ_EXTENDED_LENGTH_64 */) {
295
+ if (totalLength(chunks) < 8) {
296
+ break;
297
+ }
298
+ const headerArray = concatChunks(chunks, 8);
299
+ const view = new DataView(headerArray.buffer, headerArray.byteOffset, headerArray.length);
300
+ const n = view.getUint32(0);
301
+ if (n > Math.pow(2, 53 - 32) - 1) {
302
+ // the maximum safe integer in JavaScript is 2^53 - 1
303
+ controller.enqueue(ERROR_PACKET);
304
+ break;
305
+ }
306
+ expectedLength = n * Math.pow(2, 32) + view.getUint32(4);
307
+ state = 3 /* State.READ_PAYLOAD */;
308
+ }
309
+ else {
310
+ if (totalLength(chunks) < expectedLength) {
311
+ break;
312
+ }
313
+ const data = concatChunks(chunks, expectedLength);
314
+ controller.enqueue(decodePacket(isBinary ? data : TEXT_DECODER.decode(data), binaryType));
315
+ state = 0 /* State.READ_HEADER */;
316
+ }
317
+ if (expectedLength === 0 || expectedLength > maxPayload) {
318
+ controller.enqueue(ERROR_PACKET);
319
+ break;
320
+ }
321
+ }
322
+ },
323
+ });
324
+ }
325
+ const protocol$1 = 4;
326
+
327
+ /**
328
+ * Initialize a new `Emitter`.
329
+ *
330
+ * @api public
331
+ */
332
+
333
+ function Emitter(obj) {
334
+ if (obj) return mixin(obj);
335
+ }
336
+
337
+ /**
338
+ * Mixin the emitter properties.
339
+ *
340
+ * @param {Object} obj
341
+ * @return {Object}
342
+ * @api private
343
+ */
344
+
345
+ function mixin(obj) {
346
+ for (var key in Emitter.prototype) {
347
+ obj[key] = Emitter.prototype[key];
348
+ }
349
+ return obj;
350
+ }
351
+
352
+ /**
353
+ * Listen on the given `event` with `fn`.
354
+ *
355
+ * @param {String} event
356
+ * @param {Function} fn
357
+ * @return {Emitter}
358
+ * @api public
359
+ */
360
+
361
+ Emitter.prototype.on =
362
+ Emitter.prototype.addEventListener = function(event, fn){
363
+ this._callbacks = this._callbacks || {};
364
+ (this._callbacks['$' + event] = this._callbacks['$' + event] || [])
365
+ .push(fn);
366
+ return this;
367
+ };
368
+
369
+ /**
370
+ * Adds an `event` listener that will be invoked a single
371
+ * time then automatically removed.
372
+ *
373
+ * @param {String} event
374
+ * @param {Function} fn
375
+ * @return {Emitter}
376
+ * @api public
377
+ */
378
+
379
+ Emitter.prototype.once = function(event, fn){
380
+ function on() {
381
+ this.off(event, on);
382
+ fn.apply(this, arguments);
383
+ }
384
+
385
+ on.fn = fn;
386
+ this.on(event, on);
387
+ return this;
388
+ };
389
+
390
+ /**
391
+ * Remove the given callback for `event` or all
392
+ * registered callbacks.
393
+ *
394
+ * @param {String} event
395
+ * @param {Function} fn
396
+ * @return {Emitter}
397
+ * @api public
398
+ */
399
+
400
+ Emitter.prototype.off =
401
+ Emitter.prototype.removeListener =
402
+ Emitter.prototype.removeAllListeners =
403
+ Emitter.prototype.removeEventListener = function(event, fn){
404
+ this._callbacks = this._callbacks || {};
405
+
406
+ // all
407
+ if (0 == arguments.length) {
408
+ this._callbacks = {};
409
+ return this;
410
+ }
411
+
412
+ // specific event
413
+ var callbacks = this._callbacks['$' + event];
414
+ if (!callbacks) return this;
415
+
416
+ // remove all handlers
417
+ if (1 == arguments.length) {
418
+ delete this._callbacks['$' + event];
419
+ return this;
420
+ }
421
+
422
+ // remove specific handler
423
+ var cb;
424
+ for (var i = 0; i < callbacks.length; i++) {
425
+ cb = callbacks[i];
426
+ if (cb === fn || cb.fn === fn) {
427
+ callbacks.splice(i, 1);
428
+ break;
429
+ }
430
+ }
431
+
432
+ // Remove event specific arrays for event types that no
433
+ // one is subscribed for to avoid memory leak.
434
+ if (callbacks.length === 0) {
435
+ delete this._callbacks['$' + event];
436
+ }
437
+
438
+ return this;
439
+ };
440
+
441
+ /**
442
+ * Emit `event` with the given args.
443
+ *
444
+ * @param {String} event
445
+ * @param {Mixed} ...
446
+ * @return {Emitter}
447
+ */
448
+
449
+ Emitter.prototype.emit = function(event){
450
+ this._callbacks = this._callbacks || {};
451
+
452
+ var args = new Array(arguments.length - 1)
453
+ , callbacks = this._callbacks['$' + event];
454
+
455
+ for (var i = 1; i < arguments.length; i++) {
456
+ args[i - 1] = arguments[i];
457
+ }
458
+
459
+ if (callbacks) {
460
+ callbacks = callbacks.slice(0);
461
+ for (var i = 0, len = callbacks.length; i < len; ++i) {
462
+ callbacks[i].apply(this, args);
463
+ }
464
+ }
465
+
466
+ return this;
467
+ };
468
+
469
+ // alias used for reserved events (protected method)
470
+ Emitter.prototype.emitReserved = Emitter.prototype.emit;
471
+
472
+ /**
473
+ * Return array of callbacks for `event`.
474
+ *
475
+ * @param {String} event
476
+ * @return {Array}
477
+ * @api public
478
+ */
479
+
480
+ Emitter.prototype.listeners = function(event){
481
+ this._callbacks = this._callbacks || {};
482
+ return this._callbacks['$' + event] || [];
483
+ };
484
+
485
+ /**
486
+ * Check if this emitter has `event` handlers.
487
+ *
488
+ * @param {String} event
489
+ * @return {Boolean}
490
+ * @api public
491
+ */
492
+
493
+ Emitter.prototype.hasListeners = function(event){
494
+ return !! this.listeners(event).length;
495
+ };
496
+
497
+ const nextTick = (() => {
498
+ const isPromiseAvailable = typeof Promise === "function" && typeof Promise.resolve === "function";
499
+ if (isPromiseAvailable) {
500
+ return (cb) => Promise.resolve().then(cb);
501
+ }
502
+ else {
503
+ return (cb, setTimeoutFn) => setTimeoutFn(cb, 0);
504
+ }
505
+ })();
506
+ const globalThisShim = (() => {
507
+ if (typeof self !== "undefined") {
508
+ return self;
509
+ }
510
+ else if (typeof window !== "undefined") {
511
+ return window;
512
+ }
513
+ else {
514
+ return Function("return this")();
515
+ }
516
+ })();
517
+ const defaultBinaryType = "arraybuffer";
518
+ function createCookieJar() { }
519
+
520
+ function pick(obj, ...attr) {
521
+ return attr.reduce((acc, k) => {
522
+ if (obj.hasOwnProperty(k)) {
523
+ acc[k] = obj[k];
524
+ }
525
+ return acc;
526
+ }, {});
527
+ }
528
+ // Keep a reference to the real timeout functions so they can be used when overridden
529
+ const NATIVE_SET_TIMEOUT = globalThisShim.setTimeout;
530
+ const NATIVE_CLEAR_TIMEOUT = globalThisShim.clearTimeout;
531
+ function installTimerFunctions(obj, opts) {
532
+ if (opts.useNativeTimers) {
533
+ obj.setTimeoutFn = NATIVE_SET_TIMEOUT.bind(globalThisShim);
534
+ obj.clearTimeoutFn = NATIVE_CLEAR_TIMEOUT.bind(globalThisShim);
535
+ }
536
+ else {
537
+ obj.setTimeoutFn = globalThisShim.setTimeout.bind(globalThisShim);
538
+ obj.clearTimeoutFn = globalThisShim.clearTimeout.bind(globalThisShim);
539
+ }
540
+ }
541
+ // base64 encoded buffers are about 33% bigger (https://en.wikipedia.org/wiki/Base64)
542
+ const BASE64_OVERHEAD = 1.33;
543
+ // we could also have used `new Blob([obj]).size`, but it isn't supported in IE9
544
+ function byteLength(obj) {
545
+ if (typeof obj === "string") {
546
+ return utf8Length(obj);
547
+ }
548
+ // arraybuffer or blob
549
+ return Math.ceil((obj.byteLength || obj.size) * BASE64_OVERHEAD);
550
+ }
551
+ function utf8Length(str) {
552
+ let c = 0, length = 0;
553
+ for (let i = 0, l = str.length; i < l; i++) {
554
+ c = str.charCodeAt(i);
555
+ if (c < 0x80) {
556
+ length += 1;
557
+ }
558
+ else if (c < 0x800) {
559
+ length += 2;
560
+ }
561
+ else if (c < 0xd800 || c >= 0xe000) {
562
+ length += 3;
563
+ }
564
+ else {
565
+ i++;
566
+ length += 4;
567
+ }
568
+ }
569
+ return length;
570
+ }
571
+ /**
572
+ * Generates a random 8-characters string.
573
+ */
574
+ function randomString() {
575
+ return (Date.now().toString(36).substring(3) +
576
+ Math.random().toString(36).substring(2, 5));
577
+ }
578
+
579
+ // imported from https://github.com/galkn/querystring
580
+ /**
581
+ * Compiles a querystring
582
+ * Returns string representation of the object
583
+ *
584
+ * @param {Object}
585
+ * @api private
586
+ */
587
+ function encode(obj) {
588
+ let str = '';
589
+ for (let i in obj) {
590
+ if (obj.hasOwnProperty(i)) {
591
+ if (str.length)
592
+ str += '&';
593
+ str += encodeURIComponent(i) + '=' + encodeURIComponent(obj[i]);
594
+ }
595
+ }
596
+ return str;
597
+ }
598
+ /**
599
+ * Parses a simple querystring into an object
600
+ *
601
+ * @param {String} qs
602
+ * @api private
603
+ */
604
+ function decode(qs) {
605
+ let qry = {};
606
+ let pairs = qs.split('&');
607
+ for (let i = 0, l = pairs.length; i < l; i++) {
608
+ let pair = pairs[i].split('=');
609
+ qry[decodeURIComponent(pair[0])] = decodeURIComponent(pair[1]);
610
+ }
611
+ return qry;
612
+ }
613
+
614
+ class TransportError extends Error {
615
+ constructor(reason, description, context) {
616
+ super(reason);
617
+ this.description = description;
618
+ this.context = context;
619
+ this.type = "TransportError";
620
+ }
621
+ }
622
+ class Transport extends Emitter {
623
+ /**
624
+ * Transport abstract constructor.
625
+ *
626
+ * @param {Object} opts - options
627
+ * @protected
628
+ */
629
+ constructor(opts) {
630
+ super();
631
+ this.writable = false;
632
+ installTimerFunctions(this, opts);
633
+ this.opts = opts;
634
+ this.query = opts.query;
635
+ this.socket = opts.socket;
636
+ this.supportsBinary = !opts.forceBase64;
637
+ }
638
+ /**
639
+ * Emits an error.
640
+ *
641
+ * @param {String} reason
642
+ * @param description
643
+ * @param context - the error context
644
+ * @return {Transport} for chaining
645
+ * @protected
646
+ */
647
+ onError(reason, description, context) {
648
+ super.emitReserved("error", new TransportError(reason, description, context));
649
+ return this;
650
+ }
651
+ /**
652
+ * Opens the transport.
653
+ */
654
+ open() {
655
+ this.readyState = "opening";
656
+ this.doOpen();
657
+ return this;
658
+ }
659
+ /**
660
+ * Closes the transport.
661
+ */
662
+ close() {
663
+ if (this.readyState === "opening" || this.readyState === "open") {
664
+ this.doClose();
665
+ this.onClose();
666
+ }
667
+ return this;
668
+ }
669
+ /**
670
+ * Sends multiple packets.
671
+ *
672
+ * @param {Array} packets
673
+ */
674
+ send(packets) {
675
+ if (this.readyState === "open") {
676
+ this.write(packets);
677
+ }
678
+ }
679
+ /**
680
+ * Called upon open
681
+ *
682
+ * @protected
683
+ */
684
+ onOpen() {
685
+ this.readyState = "open";
686
+ this.writable = true;
687
+ super.emitReserved("open");
688
+ }
689
+ /**
690
+ * Called with data.
691
+ *
692
+ * @param {String} data
693
+ * @protected
694
+ */
695
+ onData(data) {
696
+ const packet = decodePacket(data, this.socket.binaryType);
697
+ this.onPacket(packet);
698
+ }
699
+ /**
700
+ * Called with a decoded packet.
701
+ *
702
+ * @protected
703
+ */
704
+ onPacket(packet) {
705
+ super.emitReserved("packet", packet);
706
+ }
707
+ /**
708
+ * Called upon close.
709
+ *
710
+ * @protected
711
+ */
712
+ onClose(details) {
713
+ this.readyState = "closed";
714
+ super.emitReserved("close", details);
715
+ }
716
+ /**
717
+ * Pauses the transport, in order not to lose packets during an upgrade.
718
+ *
719
+ * @param onPause
720
+ */
721
+ pause(onPause) { }
722
+ createUri(schema, query = {}) {
723
+ return (schema +
724
+ "://" +
725
+ this._hostname() +
726
+ this._port() +
727
+ this.opts.path +
728
+ this._query(query));
729
+ }
730
+ _hostname() {
731
+ const hostname = this.opts.hostname;
732
+ return hostname.indexOf(":") === -1 ? hostname : "[" + hostname + "]";
733
+ }
734
+ _port() {
735
+ if (this.opts.port &&
736
+ ((this.opts.secure && Number(this.opts.port !== 443)) ||
737
+ (!this.opts.secure && Number(this.opts.port) !== 80))) {
738
+ return ":" + this.opts.port;
739
+ }
740
+ else {
741
+ return "";
742
+ }
743
+ }
744
+ _query(query) {
745
+ const encodedQuery = encode(query);
746
+ return encodedQuery.length ? "?" + encodedQuery : "";
747
+ }
748
+ }
749
+
750
+ class Polling extends Transport {
751
+ constructor() {
752
+ super(...arguments);
753
+ this._polling = false;
754
+ }
755
+ get name() {
756
+ return "polling";
757
+ }
758
+ /**
759
+ * Opens the socket (triggers polling). We write a PING message to determine
760
+ * when the transport is open.
761
+ *
762
+ * @protected
763
+ */
764
+ doOpen() {
765
+ this._poll();
766
+ }
767
+ /**
768
+ * Pauses polling.
769
+ *
770
+ * @param {Function} onPause - callback upon buffers are flushed and transport is paused
771
+ * @package
772
+ */
773
+ pause(onPause) {
774
+ this.readyState = "pausing";
775
+ const pause = () => {
776
+ this.readyState = "paused";
777
+ onPause();
778
+ };
779
+ if (this._polling || !this.writable) {
780
+ let total = 0;
781
+ if (this._polling) {
782
+ total++;
783
+ this.once("pollComplete", function () {
784
+ --total || pause();
785
+ });
786
+ }
787
+ if (!this.writable) {
788
+ total++;
789
+ this.once("drain", function () {
790
+ --total || pause();
791
+ });
792
+ }
793
+ }
794
+ else {
795
+ pause();
796
+ }
797
+ }
798
+ /**
799
+ * Starts polling cycle.
800
+ *
801
+ * @private
802
+ */
803
+ _poll() {
804
+ this._polling = true;
805
+ this.doPoll();
806
+ this.emitReserved("poll");
807
+ }
808
+ /**
809
+ * Overloads onData to detect payloads.
810
+ *
811
+ * @protected
812
+ */
813
+ onData(data) {
814
+ const callback = (packet) => {
815
+ // if its the first message we consider the transport open
816
+ if ("opening" === this.readyState && packet.type === "open") {
817
+ this.onOpen();
818
+ }
819
+ // if its a close packet, we close the ongoing requests
820
+ if ("close" === packet.type) {
821
+ this.onClose({ description: "transport closed by the server" });
822
+ return false;
823
+ }
824
+ // otherwise bypass onData and handle the message
825
+ this.onPacket(packet);
826
+ };
827
+ // decode payload
828
+ decodePayload(data, this.socket.binaryType).forEach(callback);
829
+ // if an event did not trigger closing
830
+ if ("closed" !== this.readyState) {
831
+ // if we got data we're not polling
832
+ this._polling = false;
833
+ this.emitReserved("pollComplete");
834
+ if ("open" === this.readyState) {
835
+ this._poll();
836
+ }
837
+ }
838
+ }
839
+ /**
840
+ * For polling, send a close packet.
841
+ *
842
+ * @protected
843
+ */
844
+ doClose() {
845
+ const close = () => {
846
+ this.write([{ type: "close" }]);
847
+ };
848
+ if ("open" === this.readyState) {
849
+ close();
850
+ }
851
+ else {
852
+ // in case we're trying to close while
853
+ // handshaking is in progress (GH-164)
854
+ this.once("open", close);
855
+ }
856
+ }
857
+ /**
858
+ * Writes a packets payload.
859
+ *
860
+ * @param {Array} packets - data packets
861
+ * @protected
862
+ */
863
+ write(packets) {
864
+ this.writable = false;
865
+ encodePayload(packets, (data) => {
866
+ this.doWrite(data, () => {
867
+ this.writable = true;
868
+ this.emitReserved("drain");
869
+ });
870
+ });
871
+ }
872
+ /**
873
+ * Generates uri for connection.
874
+ *
875
+ * @private
876
+ */
877
+ uri() {
878
+ const schema = this.opts.secure ? "https" : "http";
879
+ const query = this.query || {};
880
+ // cache busting is forced
881
+ if (false !== this.opts.timestampRequests) {
882
+ query[this.opts.timestampParam] = randomString();
883
+ }
884
+ if (!this.supportsBinary && !query.sid) {
885
+ query.b64 = 1;
886
+ }
887
+ return this.createUri(schema, query);
888
+ }
889
+ }
890
+
891
+ // imported from https://github.com/component/has-cors
892
+ let value = false;
893
+ try {
894
+ value = typeof XMLHttpRequest !== 'undefined' &&
895
+ 'withCredentials' in new XMLHttpRequest();
896
+ }
897
+ catch (err) {
898
+ // if XMLHttp support is disabled in IE then it will throw
899
+ // when trying to create
900
+ }
901
+ const hasCORS = value;
902
+
903
+ function empty() { }
904
+ class BaseXHR extends Polling {
905
+ /**
906
+ * XHR Polling constructor.
907
+ *
908
+ * @param {Object} opts
909
+ * @package
910
+ */
911
+ constructor(opts) {
912
+ super(opts);
913
+ if (typeof location !== "undefined") {
914
+ const isSSL = "https:" === location.protocol;
915
+ let port = location.port;
916
+ // some user agents have empty `location.port`
917
+ if (!port) {
918
+ port = isSSL ? "443" : "80";
919
+ }
920
+ this.xd =
921
+ (typeof location !== "undefined" &&
922
+ opts.hostname !== location.hostname) ||
923
+ port !== opts.port;
924
+ }
925
+ }
926
+ /**
927
+ * Sends data.
928
+ *
929
+ * @param {String} data to send.
930
+ * @param {Function} called upon flush.
931
+ * @private
932
+ */
933
+ doWrite(data, fn) {
934
+ const req = this.request({
935
+ method: "POST",
936
+ data: data,
937
+ });
938
+ req.on("success", fn);
939
+ req.on("error", (xhrStatus, context) => {
940
+ this.onError("xhr post error", xhrStatus, context);
941
+ });
942
+ }
943
+ /**
944
+ * Starts a poll cycle.
945
+ *
946
+ * @private
947
+ */
948
+ doPoll() {
949
+ const req = this.request();
950
+ req.on("data", this.onData.bind(this));
951
+ req.on("error", (xhrStatus, context) => {
952
+ this.onError("xhr poll error", xhrStatus, context);
953
+ });
954
+ this.pollXhr = req;
955
+ }
956
+ }
957
+ class Request extends Emitter {
958
+ /**
959
+ * Request constructor
960
+ *
961
+ * @param {Object} options
962
+ * @package
963
+ */
964
+ constructor(createRequest, uri, opts) {
965
+ super();
966
+ this.createRequest = createRequest;
967
+ installTimerFunctions(this, opts);
968
+ this._opts = opts;
969
+ this._method = opts.method || "GET";
970
+ this._uri = uri;
971
+ this._data = undefined !== opts.data ? opts.data : null;
972
+ this._create();
973
+ }
974
+ /**
975
+ * Creates the XHR object and sends the request.
976
+ *
977
+ * @private
978
+ */
979
+ _create() {
980
+ var _a;
981
+ const opts = pick(this._opts, "agent", "pfx", "key", "passphrase", "cert", "ca", "ciphers", "rejectUnauthorized", "autoUnref");
982
+ opts.xdomain = !!this._opts.xd;
983
+ const xhr = (this._xhr = this.createRequest(opts));
984
+ try {
985
+ xhr.open(this._method, this._uri, true);
986
+ try {
987
+ if (this._opts.extraHeaders) {
988
+ // @ts-ignore
989
+ xhr.setDisableHeaderCheck && xhr.setDisableHeaderCheck(true);
990
+ for (let i in this._opts.extraHeaders) {
991
+ if (this._opts.extraHeaders.hasOwnProperty(i)) {
992
+ xhr.setRequestHeader(i, this._opts.extraHeaders[i]);
993
+ }
994
+ }
995
+ }
996
+ }
997
+ catch (e) { }
998
+ if ("POST" === this._method) {
999
+ try {
1000
+ xhr.setRequestHeader("Content-type", "text/plain;charset=UTF-8");
1001
+ }
1002
+ catch (e) { }
1003
+ }
1004
+ try {
1005
+ xhr.setRequestHeader("Accept", "*/*");
1006
+ }
1007
+ catch (e) { }
1008
+ (_a = this._opts.cookieJar) === null || _a === void 0 ? void 0 : _a.addCookies(xhr);
1009
+ // ie6 check
1010
+ if ("withCredentials" in xhr) {
1011
+ xhr.withCredentials = this._opts.withCredentials;
1012
+ }
1013
+ if (this._opts.requestTimeout) {
1014
+ xhr.timeout = this._opts.requestTimeout;
1015
+ }
1016
+ xhr.onreadystatechange = () => {
1017
+ var _a;
1018
+ if (xhr.readyState === 3) {
1019
+ (_a = this._opts.cookieJar) === null || _a === void 0 ? void 0 : _a.parseCookies(
1020
+ // @ts-ignore
1021
+ xhr.getResponseHeader("set-cookie"));
1022
+ }
1023
+ if (4 !== xhr.readyState)
1024
+ return;
1025
+ if (200 === xhr.status || 1223 === xhr.status) {
1026
+ this._onLoad();
1027
+ }
1028
+ else {
1029
+ // make sure the `error` event handler that's user-set
1030
+ // does not throw in the same tick and gets caught here
1031
+ this.setTimeoutFn(() => {
1032
+ this._onError(typeof xhr.status === "number" ? xhr.status : 0);
1033
+ }, 0);
1034
+ }
1035
+ };
1036
+ xhr.send(this._data);
1037
+ }
1038
+ catch (e) {
1039
+ // Need to defer since .create() is called directly from the constructor
1040
+ // and thus the 'error' event can only be only bound *after* this exception
1041
+ // occurs. Therefore, also, we cannot throw here at all.
1042
+ this.setTimeoutFn(() => {
1043
+ this._onError(e);
1044
+ }, 0);
1045
+ return;
1046
+ }
1047
+ if (typeof document !== "undefined") {
1048
+ this._index = Request.requestsCount++;
1049
+ Request.requests[this._index] = this;
1050
+ }
1051
+ }
1052
+ /**
1053
+ * Called upon error.
1054
+ *
1055
+ * @private
1056
+ */
1057
+ _onError(err) {
1058
+ this.emitReserved("error", err, this._xhr);
1059
+ this._cleanup(true);
1060
+ }
1061
+ /**
1062
+ * Cleans up house.
1063
+ *
1064
+ * @private
1065
+ */
1066
+ _cleanup(fromError) {
1067
+ if ("undefined" === typeof this._xhr || null === this._xhr) {
1068
+ return;
1069
+ }
1070
+ this._xhr.onreadystatechange = empty;
1071
+ if (fromError) {
1072
+ try {
1073
+ this._xhr.abort();
1074
+ }
1075
+ catch (e) { }
1076
+ }
1077
+ if (typeof document !== "undefined") {
1078
+ delete Request.requests[this._index];
1079
+ }
1080
+ this._xhr = null;
1081
+ }
1082
+ /**
1083
+ * Called upon load.
1084
+ *
1085
+ * @private
1086
+ */
1087
+ _onLoad() {
1088
+ const data = this._xhr.responseText;
1089
+ if (data !== null) {
1090
+ this.emitReserved("data", data);
1091
+ this.emitReserved("success");
1092
+ this._cleanup();
1093
+ }
1094
+ }
1095
+ /**
1096
+ * Aborts the request.
1097
+ *
1098
+ * @package
1099
+ */
1100
+ abort() {
1101
+ this._cleanup();
1102
+ }
1103
+ }
1104
+ Request.requestsCount = 0;
1105
+ Request.requests = {};
1106
+ /**
1107
+ * Aborts pending requests when unloading the window. This is needed to prevent
1108
+ * memory leaks (e.g. when using IE) and to ensure that no spurious error is
1109
+ * emitted.
1110
+ */
1111
+ if (typeof document !== "undefined") {
1112
+ // @ts-ignore
1113
+ if (typeof attachEvent === "function") {
1114
+ // @ts-ignore
1115
+ attachEvent("onunload", unloadHandler);
1116
+ }
1117
+ else if (typeof addEventListener === "function") {
1118
+ const terminationEvent = "onpagehide" in globalThisShim ? "pagehide" : "unload";
1119
+ addEventListener(terminationEvent, unloadHandler, false);
1120
+ }
1121
+ }
1122
+ function unloadHandler() {
1123
+ for (let i in Request.requests) {
1124
+ if (Request.requests.hasOwnProperty(i)) {
1125
+ Request.requests[i].abort();
1126
+ }
1127
+ }
1128
+ }
1129
+ const hasXHR2 = (function () {
1130
+ const xhr = newRequest({
1131
+ xdomain: false,
1132
+ });
1133
+ return xhr && xhr.responseType !== null;
1134
+ })();
1135
+ /**
1136
+ * HTTP long-polling based on the built-in `XMLHttpRequest` object.
1137
+ *
1138
+ * Usage: browser
1139
+ *
1140
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest
1141
+ */
1142
+ class XHR extends BaseXHR {
1143
+ constructor(opts) {
1144
+ super(opts);
1145
+ const forceBase64 = opts && opts.forceBase64;
1146
+ this.supportsBinary = hasXHR2 && !forceBase64;
1147
+ }
1148
+ request(opts = {}) {
1149
+ Object.assign(opts, { xd: this.xd }, this.opts);
1150
+ return new Request(newRequest, this.uri(), opts);
1151
+ }
1152
+ }
1153
+ function newRequest(opts) {
1154
+ const xdomain = opts.xdomain;
1155
+ // XMLHttpRequest can be disabled on IE
1156
+ try {
1157
+ if ("undefined" !== typeof XMLHttpRequest && (!xdomain || hasCORS)) {
1158
+ return new XMLHttpRequest();
1159
+ }
1160
+ }
1161
+ catch (e) { }
1162
+ if (!xdomain) {
1163
+ try {
1164
+ return new globalThisShim[["Active"].concat("Object").join("X")]("Microsoft.XMLHTTP");
1165
+ }
1166
+ catch (e) { }
1167
+ }
1168
+ }
1169
+
1170
+ // detect ReactNative environment
1171
+ const isReactNative = typeof navigator !== "undefined" &&
1172
+ typeof navigator.product === "string" &&
1173
+ navigator.product.toLowerCase() === "reactnative";
1174
+ class BaseWS extends Transport {
1175
+ get name() {
1176
+ return "websocket";
1177
+ }
1178
+ doOpen() {
1179
+ const uri = this.uri();
1180
+ const protocols = this.opts.protocols;
1181
+ // React Native only supports the 'headers' option, and will print a warning if anything else is passed
1182
+ const opts = isReactNative
1183
+ ? {}
1184
+ : pick(this.opts, "agent", "perMessageDeflate", "pfx", "key", "passphrase", "cert", "ca", "ciphers", "rejectUnauthorized", "localAddress", "protocolVersion", "origin", "maxPayload", "family", "checkServerIdentity");
1185
+ if (this.opts.extraHeaders) {
1186
+ opts.headers = this.opts.extraHeaders;
1187
+ }
1188
+ try {
1189
+ this.ws = this.createSocket(uri, protocols, opts);
1190
+ }
1191
+ catch (err) {
1192
+ return this.emitReserved("error", err);
1193
+ }
1194
+ this.ws.binaryType = this.socket.binaryType;
1195
+ this.addEventListeners();
1196
+ }
1197
+ /**
1198
+ * Adds event listeners to the socket
1199
+ *
1200
+ * @private
1201
+ */
1202
+ addEventListeners() {
1203
+ this.ws.onopen = () => {
1204
+ if (this.opts.autoUnref) {
1205
+ this.ws._socket.unref();
1206
+ }
1207
+ this.onOpen();
1208
+ };
1209
+ this.ws.onclose = (closeEvent) => this.onClose({
1210
+ description: "websocket connection closed",
1211
+ context: closeEvent,
1212
+ });
1213
+ this.ws.onmessage = (ev) => this.onData(ev.data);
1214
+ this.ws.onerror = (e) => this.onError("websocket error", e);
1215
+ }
1216
+ write(packets) {
1217
+ this.writable = false;
1218
+ // encodePacket efficient as it uses WS framing
1219
+ // no need for encodePayload
1220
+ for (let i = 0; i < packets.length; i++) {
1221
+ const packet = packets[i];
1222
+ const lastPacket = i === packets.length - 1;
1223
+ encodePacket(packet, this.supportsBinary, (data) => {
1224
+ // Sometimes the websocket has already been closed but the browser didn't
1225
+ // have a chance of informing us about it yet, in that case send will
1226
+ // throw an error
1227
+ try {
1228
+ this.doWrite(packet, data);
1229
+ }
1230
+ catch (e) {
1231
+ }
1232
+ if (lastPacket) {
1233
+ // fake drain
1234
+ // defer to next tick to allow Socket to clear writeBuffer
1235
+ nextTick(() => {
1236
+ this.writable = true;
1237
+ this.emitReserved("drain");
1238
+ }, this.setTimeoutFn);
1239
+ }
1240
+ });
1241
+ }
1242
+ }
1243
+ doClose() {
1244
+ if (typeof this.ws !== "undefined") {
1245
+ this.ws.onerror = () => { };
1246
+ this.ws.close();
1247
+ this.ws = null;
1248
+ }
1249
+ }
1250
+ /**
1251
+ * Generates uri for connection.
1252
+ *
1253
+ * @private
1254
+ */
1255
+ uri() {
1256
+ const schema = this.opts.secure ? "wss" : "ws";
1257
+ const query = this.query || {};
1258
+ // append timestamp to URI
1259
+ if (this.opts.timestampRequests) {
1260
+ query[this.opts.timestampParam] = randomString();
1261
+ }
1262
+ // communicate binary support capabilities
1263
+ if (!this.supportsBinary) {
1264
+ query.b64 = 1;
1265
+ }
1266
+ return this.createUri(schema, query);
1267
+ }
1268
+ }
1269
+ const WebSocketCtor = globalThisShim.WebSocket || globalThisShim.MozWebSocket;
1270
+ /**
1271
+ * WebSocket transport based on the built-in `WebSocket` object.
1272
+ *
1273
+ * Usage: browser, Node.js (since v21), Deno, Bun
1274
+ *
1275
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/WebSocket
1276
+ * @see https://caniuse.com/mdn-api_websocket
1277
+ * @see https://nodejs.org/api/globals.html#websocket
1278
+ */
1279
+ class WS extends BaseWS {
1280
+ createSocket(uri, protocols, opts) {
1281
+ return !isReactNative
1282
+ ? protocols
1283
+ ? new WebSocketCtor(uri, protocols)
1284
+ : new WebSocketCtor(uri)
1285
+ : new WebSocketCtor(uri, protocols, opts);
1286
+ }
1287
+ doWrite(_packet, data) {
1288
+ this.ws.send(data);
1289
+ }
1290
+ }
1291
+
1292
+ /**
1293
+ * WebTransport transport based on the built-in `WebTransport` object.
1294
+ *
1295
+ * Usage: browser, Node.js (with the `@fails-components/webtransport` package)
1296
+ *
1297
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/WebTransport
1298
+ * @see https://caniuse.com/webtransport
1299
+ */
1300
+ class WT extends Transport {
1301
+ get name() {
1302
+ return "webtransport";
1303
+ }
1304
+ doOpen() {
1305
+ try {
1306
+ // @ts-ignore
1307
+ this._transport = new WebTransport(this.createUri("https"), this.opts.transportOptions[this.name]);
1308
+ }
1309
+ catch (err) {
1310
+ return this.emitReserved("error", err);
1311
+ }
1312
+ this._transport.closed
1313
+ .then(() => {
1314
+ this.onClose();
1315
+ })
1316
+ .catch((err) => {
1317
+ this.onError("webtransport error", err);
1318
+ });
1319
+ // note: we could have used async/await, but that would require some additional polyfills
1320
+ this._transport.ready.then(() => {
1321
+ this._transport.createBidirectionalStream().then((stream) => {
1322
+ const decoderStream = createPacketDecoderStream(Number.MAX_SAFE_INTEGER, this.socket.binaryType);
1323
+ const reader = stream.readable.pipeThrough(decoderStream).getReader();
1324
+ const encoderStream = createPacketEncoderStream();
1325
+ encoderStream.readable.pipeTo(stream.writable);
1326
+ this._writer = encoderStream.writable.getWriter();
1327
+ const read = () => {
1328
+ reader
1329
+ .read()
1330
+ .then(({ done, value }) => {
1331
+ if (done) {
1332
+ return;
1333
+ }
1334
+ this.onPacket(value);
1335
+ read();
1336
+ })
1337
+ .catch((err) => {
1338
+ });
1339
+ };
1340
+ read();
1341
+ const packet = { type: "open" };
1342
+ if (this.query.sid) {
1343
+ packet.data = `{"sid":"${this.query.sid}"}`;
1344
+ }
1345
+ this._writer.write(packet).then(() => this.onOpen());
1346
+ });
1347
+ });
1348
+ }
1349
+ write(packets) {
1350
+ this.writable = false;
1351
+ for (let i = 0; i < packets.length; i++) {
1352
+ const packet = packets[i];
1353
+ const lastPacket = i === packets.length - 1;
1354
+ this._writer.write(packet).then(() => {
1355
+ if (lastPacket) {
1356
+ nextTick(() => {
1357
+ this.writable = true;
1358
+ this.emitReserved("drain");
1359
+ }, this.setTimeoutFn);
1360
+ }
1361
+ });
1362
+ }
1363
+ }
1364
+ doClose() {
1365
+ var _a;
1366
+ (_a = this._transport) === null || _a === void 0 ? void 0 : _a.close();
1367
+ }
1368
+ }
1369
+
1370
+ const transports = {
1371
+ websocket: WS,
1372
+ webtransport: WT,
1373
+ polling: XHR,
1374
+ };
1375
+
1376
+ // imported from https://github.com/galkn/parseuri
1377
+ /**
1378
+ * Parses a URI
1379
+ *
1380
+ * Note: we could also have used the built-in URL object, but it isn't supported on all platforms.
1381
+ *
1382
+ * See:
1383
+ * - https://developer.mozilla.org/en-US/docs/Web/API/URL
1384
+ * - https://caniuse.com/url
1385
+ * - https://www.rfc-editor.org/rfc/rfc3986#appendix-B
1386
+ *
1387
+ * History of the parse() method:
1388
+ * - first commit: https://github.com/socketio/socket.io-client/commit/4ee1d5d94b3906a9c052b459f1a818b15f38f91c
1389
+ * - export into its own module: https://github.com/socketio/engine.io-client/commit/de2c561e4564efeb78f1bdb1ba39ef81b2822cb3
1390
+ * - reimport: https://github.com/socketio/engine.io-client/commit/df32277c3f6d622eec5ed09f493cae3f3391d242
1391
+ *
1392
+ * @author Steven Levithan <stevenlevithan.com> (MIT license)
1393
+ * @api private
1394
+ */
1395
+ const re = /^(?:(?![^:@\/?#]+:[^:@\/]*@)(http|https|ws|wss):\/\/)?((?:(([^:@\/?#]*)(?::([^:@\/?#]*))?)?@)?((?:[a-f0-9]{0,4}:){2,7}[a-f0-9]{0,4}|[^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/;
1396
+ const parts = [
1397
+ 'source', 'protocol', 'authority', 'userInfo', 'user', 'password', 'host', 'port', 'relative', 'path', 'directory', 'file', 'query', 'anchor'
1398
+ ];
1399
+ function parse(str) {
1400
+ if (str.length > 8000) {
1401
+ throw "URI too long";
1402
+ }
1403
+ const src = str, b = str.indexOf('['), e = str.indexOf(']');
1404
+ if (b != -1 && e != -1) {
1405
+ str = str.substring(0, b) + str.substring(b, e).replace(/:/g, ';') + str.substring(e, str.length);
1406
+ }
1407
+ let m = re.exec(str || ''), uri = {}, i = 14;
1408
+ while (i--) {
1409
+ uri[parts[i]] = m[i] || '';
1410
+ }
1411
+ if (b != -1 && e != -1) {
1412
+ uri.source = src;
1413
+ uri.host = uri.host.substring(1, uri.host.length - 1).replace(/;/g, ':');
1414
+ uri.authority = uri.authority.replace('[', '').replace(']', '').replace(/;/g, ':');
1415
+ uri.ipv6uri = true;
1416
+ }
1417
+ uri.pathNames = pathNames(uri, uri['path']);
1418
+ uri.queryKey = queryKey(uri, uri['query']);
1419
+ return uri;
1420
+ }
1421
+ function pathNames(obj, path) {
1422
+ const regx = /\/{2,9}/g, names = path.replace(regx, "/").split("/");
1423
+ if (path.slice(0, 1) == '/' || path.length === 0) {
1424
+ names.splice(0, 1);
1425
+ }
1426
+ if (path.slice(-1) == '/') {
1427
+ names.splice(names.length - 1, 1);
1428
+ }
1429
+ return names;
1430
+ }
1431
+ function queryKey(uri, query) {
1432
+ const data = {};
1433
+ query.replace(/(?:^|&)([^&=]*)=?([^&]*)/g, function ($0, $1, $2) {
1434
+ if ($1) {
1435
+ data[$1] = $2;
1436
+ }
1437
+ });
1438
+ return data;
1439
+ }
1440
+
1441
+ const withEventListeners = typeof addEventListener === "function" &&
1442
+ typeof removeEventListener === "function";
1443
+ const OFFLINE_EVENT_LISTENERS = [];
1444
+ if (withEventListeners) {
1445
+ // within a ServiceWorker, any event handler for the 'offline' event must be added on the initial evaluation of the
1446
+ // script, so we create one single event listener here which will forward the event to the socket instances
1447
+ addEventListener("offline", () => {
1448
+ OFFLINE_EVENT_LISTENERS.forEach((listener) => listener());
1449
+ }, false);
1450
+ }
1451
+ /**
1452
+ * This class provides a WebSocket-like interface to connect to an Engine.IO server. The connection will be established
1453
+ * with one of the available low-level transports, like HTTP long-polling, WebSocket or WebTransport.
1454
+ *
1455
+ * This class comes without upgrade mechanism, which means that it will keep the first low-level transport that
1456
+ * successfully establishes the connection.
1457
+ *
1458
+ * In order to allow tree-shaking, there are no transports included, that's why the `transports` option is mandatory.
1459
+ *
1460
+ * @example
1461
+ * import { SocketWithoutUpgrade, WebSocket } from "engine.io-client";
1462
+ *
1463
+ * const socket = new SocketWithoutUpgrade({
1464
+ * transports: [WebSocket]
1465
+ * });
1466
+ *
1467
+ * socket.on("open", () => {
1468
+ * socket.send("hello");
1469
+ * });
1470
+ *
1471
+ * @see SocketWithUpgrade
1472
+ * @see Socket
1473
+ */
1474
+ class SocketWithoutUpgrade extends Emitter {
1475
+ /**
1476
+ * Socket constructor.
1477
+ *
1478
+ * @param {String|Object} uri - uri or options
1479
+ * @param {Object} opts - options
1480
+ */
1481
+ constructor(uri, opts) {
1482
+ super();
1483
+ this.binaryType = defaultBinaryType;
1484
+ this.writeBuffer = [];
1485
+ this._prevBufferLen = 0;
1486
+ this._pingInterval = -1;
1487
+ this._pingTimeout = -1;
1488
+ this._maxPayload = -1;
1489
+ /**
1490
+ * The expiration timestamp of the {@link _pingTimeoutTimer} object is tracked, in case the timer is throttled and the
1491
+ * callback is not fired on time. This can happen for example when a laptop is suspended or when a phone is locked.
1492
+ */
1493
+ this._pingTimeoutTime = Infinity;
1494
+ if (uri && "object" === typeof uri) {
1495
+ opts = uri;
1496
+ uri = null;
1497
+ }
1498
+ if (uri) {
1499
+ const parsedUri = parse(uri);
1500
+ opts.hostname = parsedUri.host;
1501
+ opts.secure =
1502
+ parsedUri.protocol === "https" || parsedUri.protocol === "wss";
1503
+ opts.port = parsedUri.port;
1504
+ if (parsedUri.query)
1505
+ opts.query = parsedUri.query;
1506
+ }
1507
+ else if (opts.host) {
1508
+ opts.hostname = parse(opts.host).host;
1509
+ }
1510
+ installTimerFunctions(this, opts);
1511
+ this.secure =
1512
+ null != opts.secure
1513
+ ? opts.secure
1514
+ : typeof location !== "undefined" && "https:" === location.protocol;
1515
+ if (opts.hostname && !opts.port) {
1516
+ // if no port is specified manually, use the protocol default
1517
+ opts.port = this.secure ? "443" : "80";
1518
+ }
1519
+ this.hostname =
1520
+ opts.hostname ||
1521
+ (typeof location !== "undefined" ? location.hostname : "localhost");
1522
+ this.port =
1523
+ opts.port ||
1524
+ (typeof location !== "undefined" && location.port
1525
+ ? location.port
1526
+ : this.secure
1527
+ ? "443"
1528
+ : "80");
1529
+ this.transports = [];
1530
+ this._transportsByName = {};
1531
+ opts.transports.forEach((t) => {
1532
+ const transportName = t.prototype.name;
1533
+ this.transports.push(transportName);
1534
+ this._transportsByName[transportName] = t;
1535
+ });
1536
+ this.opts = Object.assign({
1537
+ path: "/engine.io",
1538
+ agent: false,
1539
+ withCredentials: false,
1540
+ upgrade: true,
1541
+ timestampParam: "t",
1542
+ rememberUpgrade: false,
1543
+ addTrailingSlash: true,
1544
+ rejectUnauthorized: true,
1545
+ perMessageDeflate: {
1546
+ threshold: 1024,
1547
+ },
1548
+ transportOptions: {},
1549
+ closeOnBeforeunload: false,
1550
+ }, opts);
1551
+ this.opts.path =
1552
+ this.opts.path.replace(/\/$/, "") +
1553
+ (this.opts.addTrailingSlash ? "/" : "");
1554
+ if (typeof this.opts.query === "string") {
1555
+ this.opts.query = decode(this.opts.query);
1556
+ }
1557
+ if (withEventListeners) {
1558
+ if (this.opts.closeOnBeforeunload) {
1559
+ // Firefox closes the connection when the "beforeunload" event is emitted but not Chrome. This event listener
1560
+ // ensures every browser behaves the same (no "disconnect" event at the Socket.IO level when the page is
1561
+ // closed/reloaded)
1562
+ this._beforeunloadEventListener = () => {
1563
+ if (this.transport) {
1564
+ // silently close the transport
1565
+ this.transport.removeAllListeners();
1566
+ this.transport.close();
1567
+ }
1568
+ };
1569
+ addEventListener("beforeunload", this._beforeunloadEventListener, false);
1570
+ }
1571
+ if (this.hostname !== "localhost") {
1572
+ this._offlineEventListener = () => {
1573
+ this._onClose("transport close", {
1574
+ description: "network connection lost",
1575
+ });
1576
+ };
1577
+ OFFLINE_EVENT_LISTENERS.push(this._offlineEventListener);
1578
+ }
1579
+ }
1580
+ if (this.opts.withCredentials) {
1581
+ this._cookieJar = createCookieJar();
1582
+ }
1583
+ this._open();
1584
+ }
1585
+ /**
1586
+ * Creates transport of the given type.
1587
+ *
1588
+ * @param {String} name - transport name
1589
+ * @return {Transport}
1590
+ * @private
1591
+ */
1592
+ createTransport(name) {
1593
+ const query = Object.assign({}, this.opts.query);
1594
+ // append engine.io protocol identifier
1595
+ query.EIO = protocol$1;
1596
+ // transport name
1597
+ query.transport = name;
1598
+ // session id if we already have one
1599
+ if (this.id)
1600
+ query.sid = this.id;
1601
+ const opts = Object.assign({}, this.opts, {
1602
+ query,
1603
+ socket: this,
1604
+ hostname: this.hostname,
1605
+ secure: this.secure,
1606
+ port: this.port,
1607
+ }, this.opts.transportOptions[name]);
1608
+ return new this._transportsByName[name](opts);
1609
+ }
1610
+ /**
1611
+ * Initializes transport to use and starts probe.
1612
+ *
1613
+ * @private
1614
+ */
1615
+ _open() {
1616
+ if (this.transports.length === 0) {
1617
+ // Emit error on next tick so it can be listened to
1618
+ this.setTimeoutFn(() => {
1619
+ this.emitReserved("error", "No transports available");
1620
+ }, 0);
1621
+ return;
1622
+ }
1623
+ const transportName = this.opts.rememberUpgrade &&
1624
+ SocketWithoutUpgrade.priorWebsocketSuccess &&
1625
+ this.transports.indexOf("websocket") !== -1
1626
+ ? "websocket"
1627
+ : this.transports[0];
1628
+ this.readyState = "opening";
1629
+ const transport = this.createTransport(transportName);
1630
+ transport.open();
1631
+ this.setTransport(transport);
1632
+ }
1633
+ /**
1634
+ * Sets the current transport. Disables the existing one (if any).
1635
+ *
1636
+ * @private
1637
+ */
1638
+ setTransport(transport) {
1639
+ if (this.transport) {
1640
+ this.transport.removeAllListeners();
1641
+ }
1642
+ // set up transport
1643
+ this.transport = transport;
1644
+ // set up transport listeners
1645
+ transport
1646
+ .on("drain", this._onDrain.bind(this))
1647
+ .on("packet", this._onPacket.bind(this))
1648
+ .on("error", this._onError.bind(this))
1649
+ .on("close", (reason) => this._onClose("transport close", reason));
1650
+ }
1651
+ /**
1652
+ * Called when connection is deemed open.
1653
+ *
1654
+ * @private
1655
+ */
1656
+ onOpen() {
1657
+ this.readyState = "open";
1658
+ SocketWithoutUpgrade.priorWebsocketSuccess =
1659
+ "websocket" === this.transport.name;
1660
+ this.emitReserved("open");
1661
+ this.flush();
1662
+ }
1663
+ /**
1664
+ * Handles a packet.
1665
+ *
1666
+ * @private
1667
+ */
1668
+ _onPacket(packet) {
1669
+ if ("opening" === this.readyState ||
1670
+ "open" === this.readyState ||
1671
+ "closing" === this.readyState) {
1672
+ this.emitReserved("packet", packet);
1673
+ // Socket is live - any packet counts
1674
+ this.emitReserved("heartbeat");
1675
+ switch (packet.type) {
1676
+ case "open":
1677
+ this.onHandshake(JSON.parse(packet.data));
1678
+ break;
1679
+ case "ping":
1680
+ this._sendPacket("pong");
1681
+ this.emitReserved("ping");
1682
+ this.emitReserved("pong");
1683
+ this._resetPingTimeout();
1684
+ break;
1685
+ case "error":
1686
+ const err = new Error("server error");
1687
+ // @ts-ignore
1688
+ err.code = packet.data;
1689
+ this._onError(err);
1690
+ break;
1691
+ case "message":
1692
+ this.emitReserved("data", packet.data);
1693
+ this.emitReserved("message", packet.data);
1694
+ break;
1695
+ }
1696
+ }
1697
+ }
1698
+ /**
1699
+ * Called upon handshake completion.
1700
+ *
1701
+ * @param {Object} data - handshake obj
1702
+ * @private
1703
+ */
1704
+ onHandshake(data) {
1705
+ this.emitReserved("handshake", data);
1706
+ this.id = data.sid;
1707
+ this.transport.query.sid = data.sid;
1708
+ this._pingInterval = data.pingInterval;
1709
+ this._pingTimeout = data.pingTimeout;
1710
+ this._maxPayload = data.maxPayload;
1711
+ this.onOpen();
1712
+ // In case open handler closes socket
1713
+ if ("closed" === this.readyState)
1714
+ return;
1715
+ this._resetPingTimeout();
1716
+ }
1717
+ /**
1718
+ * Sets and resets ping timeout timer based on server pings.
1719
+ *
1720
+ * @private
1721
+ */
1722
+ _resetPingTimeout() {
1723
+ this.clearTimeoutFn(this._pingTimeoutTimer);
1724
+ const delay = this._pingInterval + this._pingTimeout;
1725
+ this._pingTimeoutTime = Date.now() + delay;
1726
+ this._pingTimeoutTimer = this.setTimeoutFn(() => {
1727
+ this._onClose("ping timeout");
1728
+ }, delay);
1729
+ if (this.opts.autoUnref) {
1730
+ this._pingTimeoutTimer.unref();
1731
+ }
1732
+ }
1733
+ /**
1734
+ * Called on `drain` event
1735
+ *
1736
+ * @private
1737
+ */
1738
+ _onDrain() {
1739
+ this.writeBuffer.splice(0, this._prevBufferLen);
1740
+ // setting prevBufferLen = 0 is very important
1741
+ // for example, when upgrading, upgrade packet is sent over,
1742
+ // and a nonzero prevBufferLen could cause problems on `drain`
1743
+ this._prevBufferLen = 0;
1744
+ if (0 === this.writeBuffer.length) {
1745
+ this.emitReserved("drain");
1746
+ }
1747
+ else {
1748
+ this.flush();
1749
+ }
1750
+ }
1751
+ /**
1752
+ * Flush write buffers.
1753
+ *
1754
+ * @private
1755
+ */
1756
+ flush() {
1757
+ if ("closed" !== this.readyState &&
1758
+ this.transport.writable &&
1759
+ !this.upgrading &&
1760
+ this.writeBuffer.length) {
1761
+ const packets = this._getWritablePackets();
1762
+ this.transport.send(packets);
1763
+ // keep track of current length of writeBuffer
1764
+ // splice writeBuffer and callbackBuffer on `drain`
1765
+ this._prevBufferLen = packets.length;
1766
+ this.emitReserved("flush");
1767
+ }
1768
+ }
1769
+ /**
1770
+ * Ensure the encoded size of the writeBuffer is below the maxPayload value sent by the server (only for HTTP
1771
+ * long-polling)
1772
+ *
1773
+ * @private
1774
+ */
1775
+ _getWritablePackets() {
1776
+ const shouldCheckPayloadSize = this._maxPayload &&
1777
+ this.transport.name === "polling" &&
1778
+ this.writeBuffer.length > 1;
1779
+ if (!shouldCheckPayloadSize) {
1780
+ return this.writeBuffer;
1781
+ }
1782
+ let payloadSize = 1; // first packet type
1783
+ for (let i = 0; i < this.writeBuffer.length; i++) {
1784
+ const data = this.writeBuffer[i].data;
1785
+ if (data) {
1786
+ payloadSize += byteLength(data);
1787
+ }
1788
+ if (i > 0 && payloadSize > this._maxPayload) {
1789
+ return this.writeBuffer.slice(0, i);
1790
+ }
1791
+ payloadSize += 2; // separator + packet type
1792
+ }
1793
+ return this.writeBuffer;
1794
+ }
1795
+ /**
1796
+ * Checks whether the heartbeat timer has expired but the socket has not yet been notified.
1797
+ *
1798
+ * Note: this method is private for now because it does not really fit the WebSocket API, but if we put it in the
1799
+ * `write()` method then the message would not be buffered by the Socket.IO client.
1800
+ *
1801
+ * @return {boolean}
1802
+ * @private
1803
+ */
1804
+ /* private */ _hasPingExpired() {
1805
+ if (!this._pingTimeoutTime)
1806
+ return true;
1807
+ const hasExpired = Date.now() > this._pingTimeoutTime;
1808
+ if (hasExpired) {
1809
+ this._pingTimeoutTime = 0;
1810
+ nextTick(() => {
1811
+ this._onClose("ping timeout");
1812
+ }, this.setTimeoutFn);
1813
+ }
1814
+ return hasExpired;
1815
+ }
1816
+ /**
1817
+ * Sends a message.
1818
+ *
1819
+ * @param {String} msg - message.
1820
+ * @param {Object} options.
1821
+ * @param {Function} fn - callback function.
1822
+ * @return {Socket} for chaining.
1823
+ */
1824
+ write(msg, options, fn) {
1825
+ this._sendPacket("message", msg, options, fn);
1826
+ return this;
1827
+ }
1828
+ /**
1829
+ * Sends a message. Alias of {@link Socket#write}.
1830
+ *
1831
+ * @param {String} msg - message.
1832
+ * @param {Object} options.
1833
+ * @param {Function} fn - callback function.
1834
+ * @return {Socket} for chaining.
1835
+ */
1836
+ send(msg, options, fn) {
1837
+ this._sendPacket("message", msg, options, fn);
1838
+ return this;
1839
+ }
1840
+ /**
1841
+ * Sends a packet.
1842
+ *
1843
+ * @param {String} type: packet type.
1844
+ * @param {String} data.
1845
+ * @param {Object} options.
1846
+ * @param {Function} fn - callback function.
1847
+ * @private
1848
+ */
1849
+ _sendPacket(type, data, options, fn) {
1850
+ if ("function" === typeof data) {
1851
+ fn = data;
1852
+ data = undefined;
1853
+ }
1854
+ if ("function" === typeof options) {
1855
+ fn = options;
1856
+ options = null;
1857
+ }
1858
+ if ("closing" === this.readyState || "closed" === this.readyState) {
1859
+ return;
1860
+ }
1861
+ options = options || {};
1862
+ options.compress = false !== options.compress;
1863
+ const packet = {
1864
+ type: type,
1865
+ data: data,
1866
+ options: options,
1867
+ };
1868
+ this.emitReserved("packetCreate", packet);
1869
+ this.writeBuffer.push(packet);
1870
+ if (fn)
1871
+ this.once("flush", fn);
1872
+ this.flush();
1873
+ }
1874
+ /**
1875
+ * Closes the connection.
1876
+ */
1877
+ close() {
1878
+ const close = () => {
1879
+ this._onClose("forced close");
1880
+ this.transport.close();
1881
+ };
1882
+ const cleanupAndClose = () => {
1883
+ this.off("upgrade", cleanupAndClose);
1884
+ this.off("upgradeError", cleanupAndClose);
1885
+ close();
1886
+ };
1887
+ const waitForUpgrade = () => {
1888
+ // wait for upgrade to finish since we can't send packets while pausing a transport
1889
+ this.once("upgrade", cleanupAndClose);
1890
+ this.once("upgradeError", cleanupAndClose);
1891
+ };
1892
+ if ("opening" === this.readyState || "open" === this.readyState) {
1893
+ this.readyState = "closing";
1894
+ if (this.writeBuffer.length) {
1895
+ this.once("drain", () => {
1896
+ if (this.upgrading) {
1897
+ waitForUpgrade();
1898
+ }
1899
+ else {
1900
+ close();
1901
+ }
1902
+ });
1903
+ }
1904
+ else if (this.upgrading) {
1905
+ waitForUpgrade();
1906
+ }
1907
+ else {
1908
+ close();
1909
+ }
1910
+ }
1911
+ return this;
1912
+ }
1913
+ /**
1914
+ * Called upon transport error
1915
+ *
1916
+ * @private
1917
+ */
1918
+ _onError(err) {
1919
+ SocketWithoutUpgrade.priorWebsocketSuccess = false;
1920
+ if (this.opts.tryAllTransports &&
1921
+ this.transports.length > 1 &&
1922
+ this.readyState === "opening") {
1923
+ this.transports.shift();
1924
+ return this._open();
1925
+ }
1926
+ this.emitReserved("error", err);
1927
+ this._onClose("transport error", err);
1928
+ }
1929
+ /**
1930
+ * Called upon transport close.
1931
+ *
1932
+ * @private
1933
+ */
1934
+ _onClose(reason, description) {
1935
+ if ("opening" === this.readyState ||
1936
+ "open" === this.readyState ||
1937
+ "closing" === this.readyState) {
1938
+ // clear timers
1939
+ this.clearTimeoutFn(this._pingTimeoutTimer);
1940
+ // stop event from firing again for transport
1941
+ this.transport.removeAllListeners("close");
1942
+ // ensure transport won't stay open
1943
+ this.transport.close();
1944
+ // ignore further transport communication
1945
+ this.transport.removeAllListeners();
1946
+ if (withEventListeners) {
1947
+ if (this._beforeunloadEventListener) {
1948
+ removeEventListener("beforeunload", this._beforeunloadEventListener, false);
1949
+ }
1950
+ if (this._offlineEventListener) {
1951
+ const i = OFFLINE_EVENT_LISTENERS.indexOf(this._offlineEventListener);
1952
+ if (i !== -1) {
1953
+ OFFLINE_EVENT_LISTENERS.splice(i, 1);
1954
+ }
1955
+ }
1956
+ }
1957
+ // set ready state
1958
+ this.readyState = "closed";
1959
+ // clear session id
1960
+ this.id = null;
1961
+ // emit close event
1962
+ this.emitReserved("close", reason, description);
1963
+ // clean buffers after, so users can still
1964
+ // grab the buffers on `close` event
1965
+ this.writeBuffer = [];
1966
+ this._prevBufferLen = 0;
1967
+ }
1968
+ }
1969
+ }
1970
+ SocketWithoutUpgrade.protocol = protocol$1;
1971
+ /**
1972
+ * This class provides a WebSocket-like interface to connect to an Engine.IO server. The connection will be established
1973
+ * with one of the available low-level transports, like HTTP long-polling, WebSocket or WebTransport.
1974
+ *
1975
+ * This class comes with an upgrade mechanism, which means that once the connection is established with the first
1976
+ * low-level transport, it will try to upgrade to a better transport.
1977
+ *
1978
+ * In order to allow tree-shaking, there are no transports included, that's why the `transports` option is mandatory.
1979
+ *
1980
+ * @example
1981
+ * import { SocketWithUpgrade, WebSocket } from "engine.io-client";
1982
+ *
1983
+ * const socket = new SocketWithUpgrade({
1984
+ * transports: [WebSocket]
1985
+ * });
1986
+ *
1987
+ * socket.on("open", () => {
1988
+ * socket.send("hello");
1989
+ * });
1990
+ *
1991
+ * @see SocketWithoutUpgrade
1992
+ * @see Socket
1993
+ */
1994
+ class SocketWithUpgrade extends SocketWithoutUpgrade {
1995
+ constructor() {
1996
+ super(...arguments);
1997
+ this._upgrades = [];
1998
+ }
1999
+ onOpen() {
2000
+ super.onOpen();
2001
+ if ("open" === this.readyState && this.opts.upgrade) {
2002
+ for (let i = 0; i < this._upgrades.length; i++) {
2003
+ this._probe(this._upgrades[i]);
2004
+ }
2005
+ }
2006
+ }
2007
+ /**
2008
+ * Probes a transport.
2009
+ *
2010
+ * @param {String} name - transport name
2011
+ * @private
2012
+ */
2013
+ _probe(name) {
2014
+ let transport = this.createTransport(name);
2015
+ let failed = false;
2016
+ SocketWithoutUpgrade.priorWebsocketSuccess = false;
2017
+ const onTransportOpen = () => {
2018
+ if (failed)
2019
+ return;
2020
+ transport.send([{ type: "ping", data: "probe" }]);
2021
+ transport.once("packet", (msg) => {
2022
+ if (failed)
2023
+ return;
2024
+ if ("pong" === msg.type && "probe" === msg.data) {
2025
+ this.upgrading = true;
2026
+ this.emitReserved("upgrading", transport);
2027
+ if (!transport)
2028
+ return;
2029
+ SocketWithoutUpgrade.priorWebsocketSuccess =
2030
+ "websocket" === transport.name;
2031
+ this.transport.pause(() => {
2032
+ if (failed)
2033
+ return;
2034
+ if ("closed" === this.readyState)
2035
+ return;
2036
+ cleanup();
2037
+ this.setTransport(transport);
2038
+ transport.send([{ type: "upgrade" }]);
2039
+ this.emitReserved("upgrade", transport);
2040
+ transport = null;
2041
+ this.upgrading = false;
2042
+ this.flush();
2043
+ });
2044
+ }
2045
+ else {
2046
+ const err = new Error("probe error");
2047
+ // @ts-ignore
2048
+ err.transport = transport.name;
2049
+ this.emitReserved("upgradeError", err);
2050
+ }
2051
+ });
2052
+ };
2053
+ function freezeTransport() {
2054
+ if (failed)
2055
+ return;
2056
+ // Any callback called by transport should be ignored since now
2057
+ failed = true;
2058
+ cleanup();
2059
+ transport.close();
2060
+ transport = null;
2061
+ }
2062
+ // Handle any error that happens while probing
2063
+ const onerror = (err) => {
2064
+ const error = new Error("probe error: " + err);
2065
+ // @ts-ignore
2066
+ error.transport = transport.name;
2067
+ freezeTransport();
2068
+ this.emitReserved("upgradeError", error);
2069
+ };
2070
+ function onTransportClose() {
2071
+ onerror("transport closed");
2072
+ }
2073
+ // When the socket is closed while we're probing
2074
+ function onclose() {
2075
+ onerror("socket closed");
2076
+ }
2077
+ // When the socket is upgraded while we're probing
2078
+ function onupgrade(to) {
2079
+ if (transport && to.name !== transport.name) {
2080
+ freezeTransport();
2081
+ }
2082
+ }
2083
+ // Remove all listeners on the transport and on self
2084
+ const cleanup = () => {
2085
+ transport.removeListener("open", onTransportOpen);
2086
+ transport.removeListener("error", onerror);
2087
+ transport.removeListener("close", onTransportClose);
2088
+ this.off("close", onclose);
2089
+ this.off("upgrading", onupgrade);
2090
+ };
2091
+ transport.once("open", onTransportOpen);
2092
+ transport.once("error", onerror);
2093
+ transport.once("close", onTransportClose);
2094
+ this.once("close", onclose);
2095
+ this.once("upgrading", onupgrade);
2096
+ if (this._upgrades.indexOf("webtransport") !== -1 &&
2097
+ name !== "webtransport") {
2098
+ // favor WebTransport
2099
+ this.setTimeoutFn(() => {
2100
+ if (!failed) {
2101
+ transport.open();
2102
+ }
2103
+ }, 200);
2104
+ }
2105
+ else {
2106
+ transport.open();
2107
+ }
2108
+ }
2109
+ onHandshake(data) {
2110
+ this._upgrades = this._filterUpgrades(data.upgrades);
2111
+ super.onHandshake(data);
2112
+ }
2113
+ /**
2114
+ * Filters upgrades, returning only those matching client transports.
2115
+ *
2116
+ * @param {Array} upgrades - server upgrades
2117
+ * @private
2118
+ */
2119
+ _filterUpgrades(upgrades) {
2120
+ const filteredUpgrades = [];
2121
+ for (let i = 0; i < upgrades.length; i++) {
2122
+ if (~this.transports.indexOf(upgrades[i]))
2123
+ filteredUpgrades.push(upgrades[i]);
2124
+ }
2125
+ return filteredUpgrades;
2126
+ }
2127
+ }
2128
+ /**
2129
+ * This class provides a WebSocket-like interface to connect to an Engine.IO server. The connection will be established
2130
+ * with one of the available low-level transports, like HTTP long-polling, WebSocket or WebTransport.
2131
+ *
2132
+ * This class comes with an upgrade mechanism, which means that once the connection is established with the first
2133
+ * low-level transport, it will try to upgrade to a better transport.
2134
+ *
2135
+ * @example
2136
+ * import { Socket } from "engine.io-client";
2137
+ *
2138
+ * const socket = new Socket();
2139
+ *
2140
+ * socket.on("open", () => {
2141
+ * socket.send("hello");
2142
+ * });
2143
+ *
2144
+ * @see SocketWithoutUpgrade
2145
+ * @see SocketWithUpgrade
2146
+ */
2147
+ let Socket$1 = class Socket extends SocketWithUpgrade {
2148
+ constructor(uri, opts = {}) {
2149
+ const o = typeof uri === "object" ? uri : opts;
2150
+ if (!o.transports ||
2151
+ (o.transports && typeof o.transports[0] === "string")) {
2152
+ o.transports = (o.transports || ["polling", "websocket", "webtransport"])
2153
+ .map((transportName) => transports[transportName])
2154
+ .filter((t) => !!t);
2155
+ }
2156
+ super(uri, o);
2157
+ }
2158
+ };
2159
+
2160
+ /**
2161
+ * URL parser.
2162
+ *
2163
+ * @param uri - url
2164
+ * @param path - the request path of the connection
2165
+ * @param loc - An object meant to mimic window.location.
2166
+ * Defaults to window.location.
2167
+ * @public
2168
+ */
2169
+ function url(uri, path = "", loc) {
2170
+ let obj = uri;
2171
+ // default to window.location
2172
+ loc = loc || (typeof location !== "undefined" && location);
2173
+ if (null == uri)
2174
+ uri = loc.protocol + "//" + loc.host;
2175
+ // relative path support
2176
+ if (typeof uri === "string") {
2177
+ if ("/" === uri.charAt(0)) {
2178
+ if ("/" === uri.charAt(1)) {
2179
+ uri = loc.protocol + uri;
2180
+ }
2181
+ else {
2182
+ uri = loc.host + uri;
2183
+ }
2184
+ }
2185
+ if (!/^(https?|wss?):\/\//.test(uri)) {
2186
+ if ("undefined" !== typeof loc) {
2187
+ uri = loc.protocol + "//" + uri;
2188
+ }
2189
+ else {
2190
+ uri = "https://" + uri;
2191
+ }
2192
+ }
2193
+ // parse
2194
+ obj = parse(uri);
2195
+ }
2196
+ // make sure we treat `localhost:80` and `localhost` equally
2197
+ if (!obj.port) {
2198
+ if (/^(http|ws)$/.test(obj.protocol)) {
2199
+ obj.port = "80";
2200
+ }
2201
+ else if (/^(http|ws)s$/.test(obj.protocol)) {
2202
+ obj.port = "443";
2203
+ }
2204
+ }
2205
+ obj.path = obj.path || "/";
2206
+ const ipv6 = obj.host.indexOf(":") !== -1;
2207
+ const host = ipv6 ? "[" + obj.host + "]" : obj.host;
2208
+ // define unique id
2209
+ obj.id = obj.protocol + "://" + host + ":" + obj.port + path;
2210
+ // define href
2211
+ obj.href =
2212
+ obj.protocol +
2213
+ "://" +
2214
+ host +
2215
+ (loc && loc.port === obj.port ? "" : ":" + obj.port);
2216
+ return obj;
2217
+ }
2218
+
2219
+ const withNativeArrayBuffer = typeof ArrayBuffer === "function";
2220
+ const isView = (obj) => {
2221
+ return typeof ArrayBuffer.isView === "function"
2222
+ ? ArrayBuffer.isView(obj)
2223
+ : obj.buffer instanceof ArrayBuffer;
2224
+ };
2225
+ const toString = Object.prototype.toString;
2226
+ const withNativeBlob = typeof Blob === "function" ||
2227
+ (typeof Blob !== "undefined" &&
2228
+ toString.call(Blob) === "[object BlobConstructor]");
2229
+ const withNativeFile = typeof File === "function" ||
2230
+ (typeof File !== "undefined" &&
2231
+ toString.call(File) === "[object FileConstructor]");
2232
+ /**
2233
+ * Returns true if obj is a Buffer, an ArrayBuffer, a Blob or a File.
2234
+ *
2235
+ * @private
2236
+ */
2237
+ function isBinary(obj) {
2238
+ return ((withNativeArrayBuffer && (obj instanceof ArrayBuffer || isView(obj))) ||
2239
+ (withNativeBlob && obj instanceof Blob) ||
2240
+ (withNativeFile && obj instanceof File));
2241
+ }
2242
+ function hasBinary(obj, toJSON) {
2243
+ if (!obj || typeof obj !== "object") {
2244
+ return false;
2245
+ }
2246
+ if (Array.isArray(obj)) {
2247
+ for (let i = 0, l = obj.length; i < l; i++) {
2248
+ if (hasBinary(obj[i])) {
2249
+ return true;
2250
+ }
2251
+ }
2252
+ return false;
2253
+ }
2254
+ if (isBinary(obj)) {
2255
+ return true;
2256
+ }
2257
+ if (obj.toJSON &&
2258
+ typeof obj.toJSON === "function" &&
2259
+ arguments.length === 1) {
2260
+ return hasBinary(obj.toJSON(), true);
2261
+ }
2262
+ for (const key in obj) {
2263
+ if (Object.prototype.hasOwnProperty.call(obj, key) && hasBinary(obj[key])) {
2264
+ return true;
2265
+ }
2266
+ }
2267
+ return false;
2268
+ }
2269
+
2270
+ /**
2271
+ * Replaces every Buffer | ArrayBuffer | Blob | File in packet with a numbered placeholder.
2272
+ *
2273
+ * @param {Object} packet - socket.io event packet
2274
+ * @return {Object} with deconstructed packet and list of buffers
2275
+ * @public
2276
+ */
2277
+ function deconstructPacket(packet) {
2278
+ const buffers = [];
2279
+ const packetData = packet.data;
2280
+ const pack = packet;
2281
+ pack.data = _deconstructPacket(packetData, buffers);
2282
+ pack.attachments = buffers.length; // number of binary 'attachments'
2283
+ return { packet: pack, buffers: buffers };
2284
+ }
2285
+ function _deconstructPacket(data, buffers) {
2286
+ if (!data)
2287
+ return data;
2288
+ if (isBinary(data)) {
2289
+ const placeholder = { _placeholder: true, num: buffers.length };
2290
+ buffers.push(data);
2291
+ return placeholder;
2292
+ }
2293
+ else if (Array.isArray(data)) {
2294
+ const newData = new Array(data.length);
2295
+ for (let i = 0; i < data.length; i++) {
2296
+ newData[i] = _deconstructPacket(data[i], buffers);
2297
+ }
2298
+ return newData;
2299
+ }
2300
+ else if (typeof data === "object" && !(data instanceof Date)) {
2301
+ const newData = {};
2302
+ for (const key in data) {
2303
+ if (Object.prototype.hasOwnProperty.call(data, key)) {
2304
+ newData[key] = _deconstructPacket(data[key], buffers);
2305
+ }
2306
+ }
2307
+ return newData;
2308
+ }
2309
+ return data;
2310
+ }
2311
+ /**
2312
+ * Reconstructs a binary packet from its placeholder packet and buffers
2313
+ *
2314
+ * @param {Object} packet - event packet with placeholders
2315
+ * @param {Array} buffers - binary buffers to put in placeholder positions
2316
+ * @return {Object} reconstructed packet
2317
+ * @public
2318
+ */
2319
+ function reconstructPacket(packet, buffers) {
2320
+ packet.data = _reconstructPacket(packet.data, buffers);
2321
+ delete packet.attachments; // no longer useful
2322
+ return packet;
2323
+ }
2324
+ function _reconstructPacket(data, buffers) {
2325
+ if (!data)
2326
+ return data;
2327
+ if (data && data._placeholder === true) {
2328
+ const isIndexValid = typeof data.num === "number" &&
2329
+ data.num >= 0 &&
2330
+ data.num < buffers.length;
2331
+ if (isIndexValid) {
2332
+ return buffers[data.num]; // appropriate buffer (should be natural order anyway)
2333
+ }
2334
+ else {
2335
+ throw new Error("illegal attachments");
2336
+ }
2337
+ }
2338
+ else if (Array.isArray(data)) {
2339
+ for (let i = 0; i < data.length; i++) {
2340
+ data[i] = _reconstructPacket(data[i], buffers);
2341
+ }
2342
+ }
2343
+ else if (typeof data === "object") {
2344
+ for (const key in data) {
2345
+ if (Object.prototype.hasOwnProperty.call(data, key)) {
2346
+ data[key] = _reconstructPacket(data[key], buffers);
2347
+ }
2348
+ }
2349
+ }
2350
+ return data;
2351
+ }
2352
+
2353
+ /**
2354
+ * These strings must not be used as event names, as they have a special meaning.
2355
+ */
2356
+ const RESERVED_EVENTS$1 = [
2357
+ "connect",
2358
+ "connect_error",
2359
+ "disconnect",
2360
+ "disconnecting",
2361
+ "newListener",
2362
+ "removeListener", // used by the Node.js EventEmitter
2363
+ ];
2364
+ /**
2365
+ * Protocol version.
2366
+ *
2367
+ * @public
2368
+ */
2369
+ const protocol = 5;
2370
+ var PacketType;
2371
+ (function (PacketType) {
2372
+ PacketType[PacketType["CONNECT"] = 0] = "CONNECT";
2373
+ PacketType[PacketType["DISCONNECT"] = 1] = "DISCONNECT";
2374
+ PacketType[PacketType["EVENT"] = 2] = "EVENT";
2375
+ PacketType[PacketType["ACK"] = 3] = "ACK";
2376
+ PacketType[PacketType["CONNECT_ERROR"] = 4] = "CONNECT_ERROR";
2377
+ PacketType[PacketType["BINARY_EVENT"] = 5] = "BINARY_EVENT";
2378
+ PacketType[PacketType["BINARY_ACK"] = 6] = "BINARY_ACK";
2379
+ })(PacketType || (PacketType = {}));
2380
+ /**
2381
+ * A socket.io Encoder instance
2382
+ */
2383
+ class Encoder {
2384
+ /**
2385
+ * Encoder constructor
2386
+ *
2387
+ * @param {function} replacer - custom replacer to pass down to JSON.parse
2388
+ */
2389
+ constructor(replacer) {
2390
+ this.replacer = replacer;
2391
+ }
2392
+ /**
2393
+ * Encode a packet as a single string if non-binary, or as a
2394
+ * buffer sequence, depending on packet type.
2395
+ *
2396
+ * @param {Object} obj - packet object
2397
+ */
2398
+ encode(obj) {
2399
+ if (obj.type === PacketType.EVENT || obj.type === PacketType.ACK) {
2400
+ if (hasBinary(obj)) {
2401
+ return this.encodeAsBinary({
2402
+ type: obj.type === PacketType.EVENT
2403
+ ? PacketType.BINARY_EVENT
2404
+ : PacketType.BINARY_ACK,
2405
+ nsp: obj.nsp,
2406
+ data: obj.data,
2407
+ id: obj.id,
2408
+ });
2409
+ }
2410
+ }
2411
+ return [this.encodeAsString(obj)];
2412
+ }
2413
+ /**
2414
+ * Encode packet as string.
2415
+ */
2416
+ encodeAsString(obj) {
2417
+ // first is type
2418
+ let str = "" + obj.type;
2419
+ // attachments if we have them
2420
+ if (obj.type === PacketType.BINARY_EVENT ||
2421
+ obj.type === PacketType.BINARY_ACK) {
2422
+ str += obj.attachments + "-";
2423
+ }
2424
+ // if we have a namespace other than `/`
2425
+ // we append it followed by a comma `,`
2426
+ if (obj.nsp && "/" !== obj.nsp) {
2427
+ str += obj.nsp + ",";
2428
+ }
2429
+ // immediately followed by the id
2430
+ if (null != obj.id) {
2431
+ str += obj.id;
2432
+ }
2433
+ // json data
2434
+ if (null != obj.data) {
2435
+ str += JSON.stringify(obj.data, this.replacer);
2436
+ }
2437
+ return str;
2438
+ }
2439
+ /**
2440
+ * Encode packet as 'buffer sequence' by removing blobs, and
2441
+ * deconstructing packet into object with placeholders and
2442
+ * a list of buffers.
2443
+ */
2444
+ encodeAsBinary(obj) {
2445
+ const deconstruction = deconstructPacket(obj);
2446
+ const pack = this.encodeAsString(deconstruction.packet);
2447
+ const buffers = deconstruction.buffers;
2448
+ buffers.unshift(pack); // add packet info to beginning of data list
2449
+ return buffers; // write all the buffers
2450
+ }
2451
+ }
2452
+ // see https://stackoverflow.com/questions/8511281/check-if-a-value-is-an-object-in-javascript
2453
+ function isObject(value) {
2454
+ return Object.prototype.toString.call(value) === "[object Object]";
2455
+ }
2456
+ /**
2457
+ * A socket.io Decoder instance
2458
+ *
2459
+ * @return {Object} decoder
2460
+ */
2461
+ class Decoder extends Emitter {
2462
+ /**
2463
+ * Decoder constructor
2464
+ *
2465
+ * @param {function} reviver - custom reviver to pass down to JSON.stringify
2466
+ */
2467
+ constructor(reviver) {
2468
+ super();
2469
+ this.reviver = reviver;
2470
+ }
2471
+ /**
2472
+ * Decodes an encoded packet string into packet JSON.
2473
+ *
2474
+ * @param {String} obj - encoded packet
2475
+ */
2476
+ add(obj) {
2477
+ let packet;
2478
+ if (typeof obj === "string") {
2479
+ if (this.reconstructor) {
2480
+ throw new Error("got plaintext data when reconstructing a packet");
2481
+ }
2482
+ packet = this.decodeString(obj);
2483
+ const isBinaryEvent = packet.type === PacketType.BINARY_EVENT;
2484
+ if (isBinaryEvent || packet.type === PacketType.BINARY_ACK) {
2485
+ packet.type = isBinaryEvent ? PacketType.EVENT : PacketType.ACK;
2486
+ // binary packet's json
2487
+ this.reconstructor = new BinaryReconstructor(packet);
2488
+ // no attachments, labeled binary but no binary data to follow
2489
+ if (packet.attachments === 0) {
2490
+ super.emitReserved("decoded", packet);
2491
+ }
2492
+ }
2493
+ else {
2494
+ // non-binary full packet
2495
+ super.emitReserved("decoded", packet);
2496
+ }
2497
+ }
2498
+ else if (isBinary(obj) || obj.base64) {
2499
+ // raw binary data
2500
+ if (!this.reconstructor) {
2501
+ throw new Error("got binary data when not reconstructing a packet");
2502
+ }
2503
+ else {
2504
+ packet = this.reconstructor.takeBinaryData(obj);
2505
+ if (packet) {
2506
+ // received final buffer
2507
+ this.reconstructor = null;
2508
+ super.emitReserved("decoded", packet);
2509
+ }
2510
+ }
2511
+ }
2512
+ else {
2513
+ throw new Error("Unknown type: " + obj);
2514
+ }
2515
+ }
2516
+ /**
2517
+ * Decode a packet String (JSON data)
2518
+ *
2519
+ * @param {String} str
2520
+ * @return {Object} packet
2521
+ */
2522
+ decodeString(str) {
2523
+ let i = 0;
2524
+ // look up type
2525
+ const p = {
2526
+ type: Number(str.charAt(0)),
2527
+ };
2528
+ if (PacketType[p.type] === undefined) {
2529
+ throw new Error("unknown packet type " + p.type);
2530
+ }
2531
+ // look up attachments if type binary
2532
+ if (p.type === PacketType.BINARY_EVENT ||
2533
+ p.type === PacketType.BINARY_ACK) {
2534
+ const start = i + 1;
2535
+ while (str.charAt(++i) !== "-" && i != str.length) { }
2536
+ const buf = str.substring(start, i);
2537
+ if (buf != Number(buf) || str.charAt(i) !== "-") {
2538
+ throw new Error("Illegal attachments");
2539
+ }
2540
+ p.attachments = Number(buf);
2541
+ }
2542
+ // look up namespace (if any)
2543
+ if ("/" === str.charAt(i + 1)) {
2544
+ const start = i + 1;
2545
+ while (++i) {
2546
+ const c = str.charAt(i);
2547
+ if ("," === c)
2548
+ break;
2549
+ if (i === str.length)
2550
+ break;
2551
+ }
2552
+ p.nsp = str.substring(start, i);
2553
+ }
2554
+ else {
2555
+ p.nsp = "/";
2556
+ }
2557
+ // look up id
2558
+ const next = str.charAt(i + 1);
2559
+ if ("" !== next && Number(next) == next) {
2560
+ const start = i + 1;
2561
+ while (++i) {
2562
+ const c = str.charAt(i);
2563
+ if (null == c || Number(c) != c) {
2564
+ --i;
2565
+ break;
2566
+ }
2567
+ if (i === str.length)
2568
+ break;
2569
+ }
2570
+ p.id = Number(str.substring(start, i + 1));
2571
+ }
2572
+ // look up json data
2573
+ if (str.charAt(++i)) {
2574
+ const payload = this.tryParse(str.substr(i));
2575
+ if (Decoder.isPayloadValid(p.type, payload)) {
2576
+ p.data = payload;
2577
+ }
2578
+ else {
2579
+ throw new Error("invalid payload");
2580
+ }
2581
+ }
2582
+ return p;
2583
+ }
2584
+ tryParse(str) {
2585
+ try {
2586
+ return JSON.parse(str, this.reviver);
2587
+ }
2588
+ catch (e) {
2589
+ return false;
2590
+ }
2591
+ }
2592
+ static isPayloadValid(type, payload) {
2593
+ switch (type) {
2594
+ case PacketType.CONNECT:
2595
+ return isObject(payload);
2596
+ case PacketType.DISCONNECT:
2597
+ return payload === undefined;
2598
+ case PacketType.CONNECT_ERROR:
2599
+ return typeof payload === "string" || isObject(payload);
2600
+ case PacketType.EVENT:
2601
+ case PacketType.BINARY_EVENT:
2602
+ return (Array.isArray(payload) &&
2603
+ (typeof payload[0] === "number" ||
2604
+ (typeof payload[0] === "string" &&
2605
+ RESERVED_EVENTS$1.indexOf(payload[0]) === -1)));
2606
+ case PacketType.ACK:
2607
+ case PacketType.BINARY_ACK:
2608
+ return Array.isArray(payload);
2609
+ }
2610
+ }
2611
+ /**
2612
+ * Deallocates a parser's resources
2613
+ */
2614
+ destroy() {
2615
+ if (this.reconstructor) {
2616
+ this.reconstructor.finishedReconstruction();
2617
+ this.reconstructor = null;
2618
+ }
2619
+ }
2620
+ }
2621
+ /**
2622
+ * A manager of a binary event's 'buffer sequence'. Should
2623
+ * be constructed whenever a packet of type BINARY_EVENT is
2624
+ * decoded.
2625
+ *
2626
+ * @param {Object} packet
2627
+ * @return {BinaryReconstructor} initialized reconstructor
2628
+ */
2629
+ class BinaryReconstructor {
2630
+ constructor(packet) {
2631
+ this.packet = packet;
2632
+ this.buffers = [];
2633
+ this.reconPack = packet;
2634
+ }
2635
+ /**
2636
+ * Method to be called when binary data received from connection
2637
+ * after a BINARY_EVENT packet.
2638
+ *
2639
+ * @param {Buffer | ArrayBuffer} binData - the raw binary data received
2640
+ * @return {null | Object} returns null if more binary data is expected or
2641
+ * a reconstructed packet object if all buffers have been received.
2642
+ */
2643
+ takeBinaryData(binData) {
2644
+ this.buffers.push(binData);
2645
+ if (this.buffers.length === this.reconPack.attachments) {
2646
+ // done with buffer list
2647
+ const packet = reconstructPacket(this.reconPack, this.buffers);
2648
+ this.finishedReconstruction();
2649
+ return packet;
2650
+ }
2651
+ return null;
2652
+ }
2653
+ /**
2654
+ * Cleans up binary packet reconstruction variables.
2655
+ */
2656
+ finishedReconstruction() {
2657
+ this.reconPack = null;
2658
+ this.buffers = [];
2659
+ }
2660
+ }
2661
+
2662
+ var parser = /*#__PURE__*/Object.freeze({
2663
+ __proto__: null,
2664
+ Decoder: Decoder,
2665
+ Encoder: Encoder,
2666
+ get PacketType () { return PacketType; },
2667
+ protocol: protocol
2668
+ });
2669
+
2670
+ function on(obj, ev, fn) {
2671
+ obj.on(ev, fn);
2672
+ return function subDestroy() {
2673
+ obj.off(ev, fn);
2674
+ };
2675
+ }
2676
+
2677
+ /**
2678
+ * Internal events.
2679
+ * These events can't be emitted by the user.
2680
+ */
2681
+ const RESERVED_EVENTS = Object.freeze({
2682
+ connect: 1,
2683
+ connect_error: 1,
2684
+ disconnect: 1,
2685
+ disconnecting: 1,
2686
+ // EventEmitter reserved events: https://nodejs.org/api/events.html#events_event_newlistener
2687
+ newListener: 1,
2688
+ removeListener: 1,
2689
+ });
2690
+ /**
2691
+ * A Socket is the fundamental class for interacting with the server.
2692
+ *
2693
+ * A Socket belongs to a certain Namespace (by default /) and uses an underlying {@link Manager} to communicate.
2694
+ *
2695
+ * @example
2696
+ * const socket = io();
2697
+ *
2698
+ * socket.on("connect", () => {
2699
+ * console.log("connected");
2700
+ * });
2701
+ *
2702
+ * // send an event to the server
2703
+ * socket.emit("foo", "bar");
2704
+ *
2705
+ * socket.on("foobar", () => {
2706
+ * // an event was received from the server
2707
+ * });
2708
+ *
2709
+ * // upon disconnection
2710
+ * socket.on("disconnect", (reason) => {
2711
+ * console.log(`disconnected due to ${reason}`);
2712
+ * });
2713
+ */
2714
+ class Socket extends Emitter {
2715
+ /**
2716
+ * `Socket` constructor.
2717
+ */
2718
+ constructor(io, nsp, opts) {
2719
+ super();
2720
+ /**
2721
+ * Whether the socket is currently connected to the server.
2722
+ *
2723
+ * @example
2724
+ * const socket = io();
2725
+ *
2726
+ * socket.on("connect", () => {
2727
+ * console.log(socket.connected); // true
2728
+ * });
2729
+ *
2730
+ * socket.on("disconnect", () => {
2731
+ * console.log(socket.connected); // false
2732
+ * });
2733
+ */
2734
+ this.connected = false;
2735
+ /**
2736
+ * Whether the connection state was recovered after a temporary disconnection. In that case, any missed packets will
2737
+ * be transmitted by the server.
2738
+ */
2739
+ this.recovered = false;
2740
+ /**
2741
+ * Buffer for packets received before the CONNECT packet
2742
+ */
2743
+ this.receiveBuffer = [];
2744
+ /**
2745
+ * Buffer for packets that will be sent once the socket is connected
2746
+ */
2747
+ this.sendBuffer = [];
2748
+ /**
2749
+ * The queue of packets to be sent with retry in case of failure.
2750
+ *
2751
+ * Packets are sent one by one, each waiting for the server acknowledgement, in order to guarantee the delivery order.
2752
+ * @private
2753
+ */
2754
+ this._queue = [];
2755
+ /**
2756
+ * A sequence to generate the ID of the {@link QueuedPacket}.
2757
+ * @private
2758
+ */
2759
+ this._queueSeq = 0;
2760
+ this.ids = 0;
2761
+ /**
2762
+ * A map containing acknowledgement handlers.
2763
+ *
2764
+ * The `withError` attribute is used to differentiate handlers that accept an error as first argument:
2765
+ *
2766
+ * - `socket.emit("test", (err, value) => { ... })` with `ackTimeout` option
2767
+ * - `socket.timeout(5000).emit("test", (err, value) => { ... })`
2768
+ * - `const value = await socket.emitWithAck("test")`
2769
+ *
2770
+ * From those that don't:
2771
+ *
2772
+ * - `socket.emit("test", (value) => { ... });`
2773
+ *
2774
+ * In the first case, the handlers will be called with an error when:
2775
+ *
2776
+ * - the timeout is reached
2777
+ * - the socket gets disconnected
2778
+ *
2779
+ * In the second case, the handlers will be simply discarded upon disconnection, since the client will never receive
2780
+ * an acknowledgement from the server.
2781
+ *
2782
+ * @private
2783
+ */
2784
+ this.acks = {};
2785
+ this.flags = {};
2786
+ this.io = io;
2787
+ this.nsp = nsp;
2788
+ if (opts && opts.auth) {
2789
+ this.auth = opts.auth;
2790
+ }
2791
+ this._opts = Object.assign({}, opts);
2792
+ if (this.io._autoConnect)
2793
+ this.open();
2794
+ }
2795
+ /**
2796
+ * Whether the socket is currently disconnected
2797
+ *
2798
+ * @example
2799
+ * const socket = io();
2800
+ *
2801
+ * socket.on("connect", () => {
2802
+ * console.log(socket.disconnected); // false
2803
+ * });
2804
+ *
2805
+ * socket.on("disconnect", () => {
2806
+ * console.log(socket.disconnected); // true
2807
+ * });
2808
+ */
2809
+ get disconnected() {
2810
+ return !this.connected;
2811
+ }
2812
+ /**
2813
+ * Subscribe to open, close and packet events
2814
+ *
2815
+ * @private
2816
+ */
2817
+ subEvents() {
2818
+ if (this.subs)
2819
+ return;
2820
+ const io = this.io;
2821
+ this.subs = [
2822
+ on(io, "open", this.onopen.bind(this)),
2823
+ on(io, "packet", this.onpacket.bind(this)),
2824
+ on(io, "error", this.onerror.bind(this)),
2825
+ on(io, "close", this.onclose.bind(this)),
2826
+ ];
2827
+ }
2828
+ /**
2829
+ * Whether the Socket will try to reconnect when its Manager connects or reconnects.
2830
+ *
2831
+ * @example
2832
+ * const socket = io();
2833
+ *
2834
+ * console.log(socket.active); // true
2835
+ *
2836
+ * socket.on("disconnect", (reason) => {
2837
+ * if (reason === "io server disconnect") {
2838
+ * // the disconnection was initiated by the server, you need to manually reconnect
2839
+ * console.log(socket.active); // false
2840
+ * }
2841
+ * // else the socket will automatically try to reconnect
2842
+ * console.log(socket.active); // true
2843
+ * });
2844
+ */
2845
+ get active() {
2846
+ return !!this.subs;
2847
+ }
2848
+ /**
2849
+ * "Opens" the socket.
2850
+ *
2851
+ * @example
2852
+ * const socket = io({
2853
+ * autoConnect: false
2854
+ * });
2855
+ *
2856
+ * socket.connect();
2857
+ */
2858
+ connect() {
2859
+ if (this.connected)
2860
+ return this;
2861
+ this.subEvents();
2862
+ if (!this.io["_reconnecting"])
2863
+ this.io.open(); // ensure open
2864
+ if ("open" === this.io._readyState)
2865
+ this.onopen();
2866
+ return this;
2867
+ }
2868
+ /**
2869
+ * Alias for {@link connect()}.
2870
+ */
2871
+ open() {
2872
+ return this.connect();
2873
+ }
2874
+ /**
2875
+ * Sends a `message` event.
2876
+ *
2877
+ * This method mimics the WebSocket.send() method.
2878
+ *
2879
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/send
2880
+ *
2881
+ * @example
2882
+ * socket.send("hello");
2883
+ *
2884
+ * // this is equivalent to
2885
+ * socket.emit("message", "hello");
2886
+ *
2887
+ * @return self
2888
+ */
2889
+ send(...args) {
2890
+ args.unshift("message");
2891
+ this.emit.apply(this, args);
2892
+ return this;
2893
+ }
2894
+ /**
2895
+ * Override `emit`.
2896
+ * If the event is in `events`, it's emitted normally.
2897
+ *
2898
+ * @example
2899
+ * socket.emit("hello", "world");
2900
+ *
2901
+ * // all serializable datastructures are supported (no need to call JSON.stringify)
2902
+ * socket.emit("hello", 1, "2", { 3: ["4"], 5: Uint8Array.from([6]) });
2903
+ *
2904
+ * // with an acknowledgement from the server
2905
+ * socket.emit("hello", "world", (val) => {
2906
+ * // ...
2907
+ * });
2908
+ *
2909
+ * @return self
2910
+ */
2911
+ emit(ev, ...args) {
2912
+ var _a, _b, _c;
2913
+ if (RESERVED_EVENTS.hasOwnProperty(ev)) {
2914
+ throw new Error('"' + ev.toString() + '" is a reserved event name');
2915
+ }
2916
+ args.unshift(ev);
2917
+ if (this._opts.retries && !this.flags.fromQueue && !this.flags.volatile) {
2918
+ this._addToQueue(args);
2919
+ return this;
2920
+ }
2921
+ const packet = {
2922
+ type: PacketType.EVENT,
2923
+ data: args,
2924
+ };
2925
+ packet.options = {};
2926
+ packet.options.compress = this.flags.compress !== false;
2927
+ // event ack callback
2928
+ if ("function" === typeof args[args.length - 1]) {
2929
+ const id = this.ids++;
2930
+ const ack = args.pop();
2931
+ this._registerAckCallback(id, ack);
2932
+ packet.id = id;
2933
+ }
2934
+ const isTransportWritable = (_b = (_a = this.io.engine) === null || _a === void 0 ? void 0 : _a.transport) === null || _b === void 0 ? void 0 : _b.writable;
2935
+ const isConnected = this.connected && !((_c = this.io.engine) === null || _c === void 0 ? void 0 : _c._hasPingExpired());
2936
+ const discardPacket = this.flags.volatile && !isTransportWritable;
2937
+ if (discardPacket) ;
2938
+ else if (isConnected) {
2939
+ this.notifyOutgoingListeners(packet);
2940
+ this.packet(packet);
2941
+ }
2942
+ else {
2943
+ this.sendBuffer.push(packet);
2944
+ }
2945
+ this.flags = {};
2946
+ return this;
2947
+ }
2948
+ /**
2949
+ * @private
2950
+ */
2951
+ _registerAckCallback(id, ack) {
2952
+ var _a;
2953
+ const timeout = (_a = this.flags.timeout) !== null && _a !== void 0 ? _a : this._opts.ackTimeout;
2954
+ if (timeout === undefined) {
2955
+ this.acks[id] = ack;
2956
+ return;
2957
+ }
2958
+ // @ts-ignore
2959
+ const timer = this.io.setTimeoutFn(() => {
2960
+ delete this.acks[id];
2961
+ for (let i = 0; i < this.sendBuffer.length; i++) {
2962
+ if (this.sendBuffer[i].id === id) {
2963
+ this.sendBuffer.splice(i, 1);
2964
+ }
2965
+ }
2966
+ ack.call(this, new Error("operation has timed out"));
2967
+ }, timeout);
2968
+ const fn = (...args) => {
2969
+ // @ts-ignore
2970
+ this.io.clearTimeoutFn(timer);
2971
+ ack.apply(this, args);
2972
+ };
2973
+ fn.withError = true;
2974
+ this.acks[id] = fn;
2975
+ }
2976
+ /**
2977
+ * Emits an event and waits for an acknowledgement
2978
+ *
2979
+ * @example
2980
+ * // without timeout
2981
+ * const response = await socket.emitWithAck("hello", "world");
2982
+ *
2983
+ * // with a specific timeout
2984
+ * try {
2985
+ * const response = await socket.timeout(1000).emitWithAck("hello", "world");
2986
+ * } catch (err) {
2987
+ * // the server did not acknowledge the event in the given delay
2988
+ * }
2989
+ *
2990
+ * @return a Promise that will be fulfilled when the server acknowledges the event
2991
+ */
2992
+ emitWithAck(ev, ...args) {
2993
+ return new Promise((resolve, reject) => {
2994
+ const fn = (arg1, arg2) => {
2995
+ return arg1 ? reject(arg1) : resolve(arg2);
2996
+ };
2997
+ fn.withError = true;
2998
+ args.push(fn);
2999
+ this.emit(ev, ...args);
3000
+ });
3001
+ }
3002
+ /**
3003
+ * Add the packet to the queue.
3004
+ * @param args
3005
+ * @private
3006
+ */
3007
+ _addToQueue(args) {
3008
+ let ack;
3009
+ if (typeof args[args.length - 1] === "function") {
3010
+ ack = args.pop();
3011
+ }
3012
+ const packet = {
3013
+ id: this._queueSeq++,
3014
+ tryCount: 0,
3015
+ pending: false,
3016
+ args,
3017
+ flags: Object.assign({ fromQueue: true }, this.flags),
3018
+ };
3019
+ args.push((err, ...responseArgs) => {
3020
+ if (packet !== this._queue[0]) {
3021
+ // the packet has already been acknowledged
3022
+ return;
3023
+ }
3024
+ const hasError = err !== null;
3025
+ if (hasError) {
3026
+ if (packet.tryCount > this._opts.retries) {
3027
+ this._queue.shift();
3028
+ if (ack) {
3029
+ ack(err);
3030
+ }
3031
+ }
3032
+ }
3033
+ else {
3034
+ this._queue.shift();
3035
+ if (ack) {
3036
+ ack(null, ...responseArgs);
3037
+ }
3038
+ }
3039
+ packet.pending = false;
3040
+ return this._drainQueue();
3041
+ });
3042
+ this._queue.push(packet);
3043
+ this._drainQueue();
3044
+ }
3045
+ /**
3046
+ * Send the first packet of the queue, and wait for an acknowledgement from the server.
3047
+ * @param force - whether to resend a packet that has not been acknowledged yet
3048
+ *
3049
+ * @private
3050
+ */
3051
+ _drainQueue(force = false) {
3052
+ if (!this.connected || this._queue.length === 0) {
3053
+ return;
3054
+ }
3055
+ const packet = this._queue[0];
3056
+ if (packet.pending && !force) {
3057
+ return;
3058
+ }
3059
+ packet.pending = true;
3060
+ packet.tryCount++;
3061
+ this.flags = packet.flags;
3062
+ this.emit.apply(this, packet.args);
3063
+ }
3064
+ /**
3065
+ * Sends a packet.
3066
+ *
3067
+ * @param packet
3068
+ * @private
3069
+ */
3070
+ packet(packet) {
3071
+ packet.nsp = this.nsp;
3072
+ this.io._packet(packet);
3073
+ }
3074
+ /**
3075
+ * Called upon engine `open`.
3076
+ *
3077
+ * @private
3078
+ */
3079
+ onopen() {
3080
+ if (typeof this.auth == "function") {
3081
+ this.auth((data) => {
3082
+ this._sendConnectPacket(data);
3083
+ });
3084
+ }
3085
+ else {
3086
+ this._sendConnectPacket(this.auth);
3087
+ }
3088
+ }
3089
+ /**
3090
+ * Sends a CONNECT packet to initiate the Socket.IO session.
3091
+ *
3092
+ * @param data
3093
+ * @private
3094
+ */
3095
+ _sendConnectPacket(data) {
3096
+ this.packet({
3097
+ type: PacketType.CONNECT,
3098
+ data: this._pid
3099
+ ? Object.assign({ pid: this._pid, offset: this._lastOffset }, data)
3100
+ : data,
3101
+ });
3102
+ }
3103
+ /**
3104
+ * Called upon engine or manager `error`.
3105
+ *
3106
+ * @param err
3107
+ * @private
3108
+ */
3109
+ onerror(err) {
3110
+ if (!this.connected) {
3111
+ this.emitReserved("connect_error", err);
3112
+ }
3113
+ }
3114
+ /**
3115
+ * Called upon engine `close`.
3116
+ *
3117
+ * @param reason
3118
+ * @param description
3119
+ * @private
3120
+ */
3121
+ onclose(reason, description) {
3122
+ this.connected = false;
3123
+ delete this.id;
3124
+ this.emitReserved("disconnect", reason, description);
3125
+ this._clearAcks();
3126
+ }
3127
+ /**
3128
+ * Clears the acknowledgement handlers upon disconnection, since the client will never receive an acknowledgement from
3129
+ * the server.
3130
+ *
3131
+ * @private
3132
+ */
3133
+ _clearAcks() {
3134
+ Object.keys(this.acks).forEach((id) => {
3135
+ const isBuffered = this.sendBuffer.some((packet) => String(packet.id) === id);
3136
+ if (!isBuffered) {
3137
+ // note: handlers that do not accept an error as first argument are ignored here
3138
+ const ack = this.acks[id];
3139
+ delete this.acks[id];
3140
+ if (ack.withError) {
3141
+ ack.call(this, new Error("socket has been disconnected"));
3142
+ }
3143
+ }
3144
+ });
3145
+ }
3146
+ /**
3147
+ * Called with socket packet.
3148
+ *
3149
+ * @param packet
3150
+ * @private
3151
+ */
3152
+ onpacket(packet) {
3153
+ const sameNamespace = packet.nsp === this.nsp;
3154
+ if (!sameNamespace)
3155
+ return;
3156
+ switch (packet.type) {
3157
+ case PacketType.CONNECT:
3158
+ if (packet.data && packet.data.sid) {
3159
+ this.onconnect(packet.data.sid, packet.data.pid);
3160
+ }
3161
+ else {
3162
+ this.emitReserved("connect_error", new Error("It seems you are trying to reach a Socket.IO server in v2.x with a v3.x client, but they are not compatible (more information here: https://socket.io/docs/v3/migrating-from-2-x-to-3-0/)"));
3163
+ }
3164
+ break;
3165
+ case PacketType.EVENT:
3166
+ case PacketType.BINARY_EVENT:
3167
+ this.onevent(packet);
3168
+ break;
3169
+ case PacketType.ACK:
3170
+ case PacketType.BINARY_ACK:
3171
+ this.onack(packet);
3172
+ break;
3173
+ case PacketType.DISCONNECT:
3174
+ this.ondisconnect();
3175
+ break;
3176
+ case PacketType.CONNECT_ERROR:
3177
+ this.destroy();
3178
+ const err = new Error(packet.data.message);
3179
+ // @ts-ignore
3180
+ err.data = packet.data.data;
3181
+ this.emitReserved("connect_error", err);
3182
+ break;
3183
+ }
3184
+ }
3185
+ /**
3186
+ * Called upon a server event.
3187
+ *
3188
+ * @param packet
3189
+ * @private
3190
+ */
3191
+ onevent(packet) {
3192
+ const args = packet.data || [];
3193
+ if (null != packet.id) {
3194
+ args.push(this.ack(packet.id));
3195
+ }
3196
+ if (this.connected) {
3197
+ this.emitEvent(args);
3198
+ }
3199
+ else {
3200
+ this.receiveBuffer.push(Object.freeze(args));
3201
+ }
3202
+ }
3203
+ emitEvent(args) {
3204
+ if (this._anyListeners && this._anyListeners.length) {
3205
+ const listeners = this._anyListeners.slice();
3206
+ for (const listener of listeners) {
3207
+ listener.apply(this, args);
3208
+ }
3209
+ }
3210
+ super.emit.apply(this, args);
3211
+ if (this._pid && args.length && typeof args[args.length - 1] === "string") {
3212
+ this._lastOffset = args[args.length - 1];
3213
+ }
3214
+ }
3215
+ /**
3216
+ * Produces an ack callback to emit with an event.
3217
+ *
3218
+ * @private
3219
+ */
3220
+ ack(id) {
3221
+ const self = this;
3222
+ let sent = false;
3223
+ return function (...args) {
3224
+ // prevent double callbacks
3225
+ if (sent)
3226
+ return;
3227
+ sent = true;
3228
+ self.packet({
3229
+ type: PacketType.ACK,
3230
+ id: id,
3231
+ data: args,
3232
+ });
3233
+ };
3234
+ }
3235
+ /**
3236
+ * Called upon a server acknowledgement.
3237
+ *
3238
+ * @param packet
3239
+ * @private
3240
+ */
3241
+ onack(packet) {
3242
+ const ack = this.acks[packet.id];
3243
+ if (typeof ack !== "function") {
3244
+ return;
3245
+ }
3246
+ delete this.acks[packet.id];
3247
+ // @ts-ignore FIXME ack is incorrectly inferred as 'never'
3248
+ if (ack.withError) {
3249
+ packet.data.unshift(null);
3250
+ }
3251
+ // @ts-ignore
3252
+ ack.apply(this, packet.data);
3253
+ }
3254
+ /**
3255
+ * Called upon server connect.
3256
+ *
3257
+ * @private
3258
+ */
3259
+ onconnect(id, pid) {
3260
+ this.id = id;
3261
+ this.recovered = pid && this._pid === pid;
3262
+ this._pid = pid; // defined only if connection state recovery is enabled
3263
+ this.connected = true;
3264
+ this.emitBuffered();
3265
+ this.emitReserved("connect");
3266
+ this._drainQueue(true);
3267
+ }
3268
+ /**
3269
+ * Emit buffered events (received and emitted).
3270
+ *
3271
+ * @private
3272
+ */
3273
+ emitBuffered() {
3274
+ this.receiveBuffer.forEach((args) => this.emitEvent(args));
3275
+ this.receiveBuffer = [];
3276
+ this.sendBuffer.forEach((packet) => {
3277
+ this.notifyOutgoingListeners(packet);
3278
+ this.packet(packet);
3279
+ });
3280
+ this.sendBuffer = [];
3281
+ }
3282
+ /**
3283
+ * Called upon server disconnect.
3284
+ *
3285
+ * @private
3286
+ */
3287
+ ondisconnect() {
3288
+ this.destroy();
3289
+ this.onclose("io server disconnect");
3290
+ }
3291
+ /**
3292
+ * Called upon forced client/server side disconnections,
3293
+ * this method ensures the manager stops tracking us and
3294
+ * that reconnections don't get triggered for this.
3295
+ *
3296
+ * @private
3297
+ */
3298
+ destroy() {
3299
+ if (this.subs) {
3300
+ // clean subscriptions to avoid reconnections
3301
+ this.subs.forEach((subDestroy) => subDestroy());
3302
+ this.subs = undefined;
3303
+ }
3304
+ this.io["_destroy"](this);
3305
+ }
3306
+ /**
3307
+ * Disconnects the socket manually. In that case, the socket will not try to reconnect.
3308
+ *
3309
+ * If this is the last active Socket instance of the {@link Manager}, the low-level connection will be closed.
3310
+ *
3311
+ * @example
3312
+ * const socket = io();
3313
+ *
3314
+ * socket.on("disconnect", (reason) => {
3315
+ * // console.log(reason); prints "io client disconnect"
3316
+ * });
3317
+ *
3318
+ * socket.disconnect();
3319
+ *
3320
+ * @return self
3321
+ */
3322
+ disconnect() {
3323
+ if (this.connected) {
3324
+ this.packet({ type: PacketType.DISCONNECT });
3325
+ }
3326
+ // remove socket from pool
3327
+ this.destroy();
3328
+ if (this.connected) {
3329
+ // fire events
3330
+ this.onclose("io client disconnect");
3331
+ }
3332
+ return this;
3333
+ }
3334
+ /**
3335
+ * Alias for {@link disconnect()}.
3336
+ *
3337
+ * @return self
3338
+ */
3339
+ close() {
3340
+ return this.disconnect();
3341
+ }
3342
+ /**
3343
+ * Sets the compress flag.
3344
+ *
3345
+ * @example
3346
+ * socket.compress(false).emit("hello");
3347
+ *
3348
+ * @param compress - if `true`, compresses the sending data
3349
+ * @return self
3350
+ */
3351
+ compress(compress) {
3352
+ this.flags.compress = compress;
3353
+ return this;
3354
+ }
3355
+ /**
3356
+ * Sets a modifier for a subsequent event emission that the event message will be dropped when this socket is not
3357
+ * ready to send messages.
3358
+ *
3359
+ * @example
3360
+ * socket.volatile.emit("hello"); // the server may or may not receive it
3361
+ *
3362
+ * @returns self
3363
+ */
3364
+ get volatile() {
3365
+ this.flags.volatile = true;
3366
+ return this;
3367
+ }
3368
+ /**
3369
+ * Sets a modifier for a subsequent event emission that the callback will be called with an error when the
3370
+ * given number of milliseconds have elapsed without an acknowledgement from the server:
3371
+ *
3372
+ * @example
3373
+ * socket.timeout(5000).emit("my-event", (err) => {
3374
+ * if (err) {
3375
+ * // the server did not acknowledge the event in the given delay
3376
+ * }
3377
+ * });
3378
+ *
3379
+ * @returns self
3380
+ */
3381
+ timeout(timeout) {
3382
+ this.flags.timeout = timeout;
3383
+ return this;
3384
+ }
3385
+ /**
3386
+ * Adds a listener that will be fired when any event is emitted. The event name is passed as the first argument to the
3387
+ * callback.
3388
+ *
3389
+ * @example
3390
+ * socket.onAny((event, ...args) => {
3391
+ * console.log(`got ${event}`);
3392
+ * });
3393
+ *
3394
+ * @param listener
3395
+ */
3396
+ onAny(listener) {
3397
+ this._anyListeners = this._anyListeners || [];
3398
+ this._anyListeners.push(listener);
3399
+ return this;
3400
+ }
3401
+ /**
3402
+ * Adds a listener that will be fired when any event is emitted. The event name is passed as the first argument to the
3403
+ * callback. The listener is added to the beginning of the listeners array.
3404
+ *
3405
+ * @example
3406
+ * socket.prependAny((event, ...args) => {
3407
+ * console.log(`got event ${event}`);
3408
+ * });
3409
+ *
3410
+ * @param listener
3411
+ */
3412
+ prependAny(listener) {
3413
+ this._anyListeners = this._anyListeners || [];
3414
+ this._anyListeners.unshift(listener);
3415
+ return this;
3416
+ }
3417
+ /**
3418
+ * Removes the listener that will be fired when any event is emitted.
3419
+ *
3420
+ * @example
3421
+ * const catchAllListener = (event, ...args) => {
3422
+ * console.log(`got event ${event}`);
3423
+ * }
3424
+ *
3425
+ * socket.onAny(catchAllListener);
3426
+ *
3427
+ * // remove a specific listener
3428
+ * socket.offAny(catchAllListener);
3429
+ *
3430
+ * // or remove all listeners
3431
+ * socket.offAny();
3432
+ *
3433
+ * @param listener
3434
+ */
3435
+ offAny(listener) {
3436
+ if (!this._anyListeners) {
3437
+ return this;
3438
+ }
3439
+ if (listener) {
3440
+ const listeners = this._anyListeners;
3441
+ for (let i = 0; i < listeners.length; i++) {
3442
+ if (listener === listeners[i]) {
3443
+ listeners.splice(i, 1);
3444
+ return this;
3445
+ }
3446
+ }
3447
+ }
3448
+ else {
3449
+ this._anyListeners = [];
3450
+ }
3451
+ return this;
3452
+ }
3453
+ /**
3454
+ * Returns an array of listeners that are listening for any event that is specified. This array can be manipulated,
3455
+ * e.g. to remove listeners.
3456
+ */
3457
+ listenersAny() {
3458
+ return this._anyListeners || [];
3459
+ }
3460
+ /**
3461
+ * Adds a listener that will be fired when any event is emitted. The event name is passed as the first argument to the
3462
+ * callback.
3463
+ *
3464
+ * Note: acknowledgements sent to the server are not included.
3465
+ *
3466
+ * @example
3467
+ * socket.onAnyOutgoing((event, ...args) => {
3468
+ * console.log(`sent event ${event}`);
3469
+ * });
3470
+ *
3471
+ * @param listener
3472
+ */
3473
+ onAnyOutgoing(listener) {
3474
+ this._anyOutgoingListeners = this._anyOutgoingListeners || [];
3475
+ this._anyOutgoingListeners.push(listener);
3476
+ return this;
3477
+ }
3478
+ /**
3479
+ * Adds a listener that will be fired when any event is emitted. The event name is passed as the first argument to the
3480
+ * callback. The listener is added to the beginning of the listeners array.
3481
+ *
3482
+ * Note: acknowledgements sent to the server are not included.
3483
+ *
3484
+ * @example
3485
+ * socket.prependAnyOutgoing((event, ...args) => {
3486
+ * console.log(`sent event ${event}`);
3487
+ * });
3488
+ *
3489
+ * @param listener
3490
+ */
3491
+ prependAnyOutgoing(listener) {
3492
+ this._anyOutgoingListeners = this._anyOutgoingListeners || [];
3493
+ this._anyOutgoingListeners.unshift(listener);
3494
+ return this;
3495
+ }
3496
+ /**
3497
+ * Removes the listener that will be fired when any event is emitted.
3498
+ *
3499
+ * @example
3500
+ * const catchAllListener = (event, ...args) => {
3501
+ * console.log(`sent event ${event}`);
3502
+ * }
3503
+ *
3504
+ * socket.onAnyOutgoing(catchAllListener);
3505
+ *
3506
+ * // remove a specific listener
3507
+ * socket.offAnyOutgoing(catchAllListener);
3508
+ *
3509
+ * // or remove all listeners
3510
+ * socket.offAnyOutgoing();
3511
+ *
3512
+ * @param [listener] - the catch-all listener (optional)
3513
+ */
3514
+ offAnyOutgoing(listener) {
3515
+ if (!this._anyOutgoingListeners) {
3516
+ return this;
3517
+ }
3518
+ if (listener) {
3519
+ const listeners = this._anyOutgoingListeners;
3520
+ for (let i = 0; i < listeners.length; i++) {
3521
+ if (listener === listeners[i]) {
3522
+ listeners.splice(i, 1);
3523
+ return this;
3524
+ }
3525
+ }
3526
+ }
3527
+ else {
3528
+ this._anyOutgoingListeners = [];
3529
+ }
3530
+ return this;
3531
+ }
3532
+ /**
3533
+ * Returns an array of listeners that are listening for any event that is specified. This array can be manipulated,
3534
+ * e.g. to remove listeners.
3535
+ */
3536
+ listenersAnyOutgoing() {
3537
+ return this._anyOutgoingListeners || [];
3538
+ }
3539
+ /**
3540
+ * Notify the listeners for each packet sent
3541
+ *
3542
+ * @param packet
3543
+ *
3544
+ * @private
3545
+ */
3546
+ notifyOutgoingListeners(packet) {
3547
+ if (this._anyOutgoingListeners && this._anyOutgoingListeners.length) {
3548
+ const listeners = this._anyOutgoingListeners.slice();
3549
+ for (const listener of listeners) {
3550
+ listener.apply(this, packet.data);
3551
+ }
3552
+ }
3553
+ }
3554
+ }
3555
+
3556
+ /**
3557
+ * Initialize backoff timer with `opts`.
3558
+ *
3559
+ * - `min` initial timeout in milliseconds [100]
3560
+ * - `max` max timeout [10000]
3561
+ * - `jitter` [0]
3562
+ * - `factor` [2]
3563
+ *
3564
+ * @param {Object} opts
3565
+ * @api public
3566
+ */
3567
+ function Backoff(opts) {
3568
+ opts = opts || {};
3569
+ this.ms = opts.min || 100;
3570
+ this.max = opts.max || 10000;
3571
+ this.factor = opts.factor || 2;
3572
+ this.jitter = opts.jitter > 0 && opts.jitter <= 1 ? opts.jitter : 0;
3573
+ this.attempts = 0;
3574
+ }
3575
+ /**
3576
+ * Return the backoff duration.
3577
+ *
3578
+ * @return {Number}
3579
+ * @api public
3580
+ */
3581
+ Backoff.prototype.duration = function () {
3582
+ var ms = this.ms * Math.pow(this.factor, this.attempts++);
3583
+ if (this.jitter) {
3584
+ var rand = Math.random();
3585
+ var deviation = Math.floor(rand * this.jitter * ms);
3586
+ ms = (Math.floor(rand * 10) & 1) == 0 ? ms - deviation : ms + deviation;
3587
+ }
3588
+ return Math.min(ms, this.max) | 0;
3589
+ };
3590
+ /**
3591
+ * Reset the number of attempts.
3592
+ *
3593
+ * @api public
3594
+ */
3595
+ Backoff.prototype.reset = function () {
3596
+ this.attempts = 0;
3597
+ };
3598
+ /**
3599
+ * Set the minimum duration
3600
+ *
3601
+ * @api public
3602
+ */
3603
+ Backoff.prototype.setMin = function (min) {
3604
+ this.ms = min;
3605
+ };
3606
+ /**
3607
+ * Set the maximum duration
3608
+ *
3609
+ * @api public
3610
+ */
3611
+ Backoff.prototype.setMax = function (max) {
3612
+ this.max = max;
3613
+ };
3614
+ /**
3615
+ * Set the jitter
3616
+ *
3617
+ * @api public
3618
+ */
3619
+ Backoff.prototype.setJitter = function (jitter) {
3620
+ this.jitter = jitter;
3621
+ };
3622
+
3623
+ class Manager extends Emitter {
3624
+ constructor(uri, opts) {
3625
+ var _a;
3626
+ super();
3627
+ this.nsps = {};
3628
+ this.subs = [];
3629
+ if (uri && "object" === typeof uri) {
3630
+ opts = uri;
3631
+ uri = undefined;
3632
+ }
3633
+ opts = opts || {};
3634
+ opts.path = opts.path || "/socket.io";
3635
+ this.opts = opts;
3636
+ installTimerFunctions(this, opts);
3637
+ this.reconnection(opts.reconnection !== false);
3638
+ this.reconnectionAttempts(opts.reconnectionAttempts || Infinity);
3639
+ this.reconnectionDelay(opts.reconnectionDelay || 1000);
3640
+ this.reconnectionDelayMax(opts.reconnectionDelayMax || 5000);
3641
+ this.randomizationFactor((_a = opts.randomizationFactor) !== null && _a !== void 0 ? _a : 0.5);
3642
+ this.backoff = new Backoff({
3643
+ min: this.reconnectionDelay(),
3644
+ max: this.reconnectionDelayMax(),
3645
+ jitter: this.randomizationFactor(),
3646
+ });
3647
+ this.timeout(null == opts.timeout ? 20000 : opts.timeout);
3648
+ this._readyState = "closed";
3649
+ this.uri = uri;
3650
+ const _parser = opts.parser || parser;
3651
+ this.encoder = new _parser.Encoder();
3652
+ this.decoder = new _parser.Decoder();
3653
+ this._autoConnect = opts.autoConnect !== false;
3654
+ if (this._autoConnect)
3655
+ this.open();
3656
+ }
3657
+ reconnection(v) {
3658
+ if (!arguments.length)
3659
+ return this._reconnection;
3660
+ this._reconnection = !!v;
3661
+ if (!v) {
3662
+ this.skipReconnect = true;
3663
+ }
3664
+ return this;
3665
+ }
3666
+ reconnectionAttempts(v) {
3667
+ if (v === undefined)
3668
+ return this._reconnectionAttempts;
3669
+ this._reconnectionAttempts = v;
3670
+ return this;
3671
+ }
3672
+ reconnectionDelay(v) {
3673
+ var _a;
3674
+ if (v === undefined)
3675
+ return this._reconnectionDelay;
3676
+ this._reconnectionDelay = v;
3677
+ (_a = this.backoff) === null || _a === void 0 ? void 0 : _a.setMin(v);
3678
+ return this;
3679
+ }
3680
+ randomizationFactor(v) {
3681
+ var _a;
3682
+ if (v === undefined)
3683
+ return this._randomizationFactor;
3684
+ this._randomizationFactor = v;
3685
+ (_a = this.backoff) === null || _a === void 0 ? void 0 : _a.setJitter(v);
3686
+ return this;
3687
+ }
3688
+ reconnectionDelayMax(v) {
3689
+ var _a;
3690
+ if (v === undefined)
3691
+ return this._reconnectionDelayMax;
3692
+ this._reconnectionDelayMax = v;
3693
+ (_a = this.backoff) === null || _a === void 0 ? void 0 : _a.setMax(v);
3694
+ return this;
3695
+ }
3696
+ timeout(v) {
3697
+ if (!arguments.length)
3698
+ return this._timeout;
3699
+ this._timeout = v;
3700
+ return this;
3701
+ }
3702
+ /**
3703
+ * Starts trying to reconnect if reconnection is enabled and we have not
3704
+ * started reconnecting yet
3705
+ *
3706
+ * @private
3707
+ */
3708
+ maybeReconnectOnOpen() {
3709
+ // Only try to reconnect if it's the first time we're connecting
3710
+ if (!this._reconnecting &&
3711
+ this._reconnection &&
3712
+ this.backoff.attempts === 0) {
3713
+ // keeps reconnection from firing twice for the same reconnection loop
3714
+ this.reconnect();
3715
+ }
3716
+ }
3717
+ /**
3718
+ * Sets the current transport `socket`.
3719
+ *
3720
+ * @param {Function} fn - optional, callback
3721
+ * @return self
3722
+ * @public
3723
+ */
3724
+ open(fn) {
3725
+ if (~this._readyState.indexOf("open"))
3726
+ return this;
3727
+ this.engine = new Socket$1(this.uri, this.opts);
3728
+ const socket = this.engine;
3729
+ const self = this;
3730
+ this._readyState = "opening";
3731
+ this.skipReconnect = false;
3732
+ // emit `open`
3733
+ const openSubDestroy = on(socket, "open", function () {
3734
+ self.onopen();
3735
+ fn && fn();
3736
+ });
3737
+ const onError = (err) => {
3738
+ this.cleanup();
3739
+ this._readyState = "closed";
3740
+ this.emitReserved("error", err);
3741
+ if (fn) {
3742
+ fn(err);
3743
+ }
3744
+ else {
3745
+ // Only do this if there is no fn to handle the error
3746
+ this.maybeReconnectOnOpen();
3747
+ }
3748
+ };
3749
+ // emit `error`
3750
+ const errorSub = on(socket, "error", onError);
3751
+ if (false !== this._timeout) {
3752
+ const timeout = this._timeout;
3753
+ // set timer
3754
+ const timer = this.setTimeoutFn(() => {
3755
+ openSubDestroy();
3756
+ onError(new Error("timeout"));
3757
+ socket.close();
3758
+ }, timeout);
3759
+ if (this.opts.autoUnref) {
3760
+ timer.unref();
3761
+ }
3762
+ this.subs.push(() => {
3763
+ this.clearTimeoutFn(timer);
3764
+ });
3765
+ }
3766
+ this.subs.push(openSubDestroy);
3767
+ this.subs.push(errorSub);
3768
+ return this;
3769
+ }
3770
+ /**
3771
+ * Alias for open()
3772
+ *
3773
+ * @return self
3774
+ * @public
3775
+ */
3776
+ connect(fn) {
3777
+ return this.open(fn);
3778
+ }
3779
+ /**
3780
+ * Called upon transport open.
3781
+ *
3782
+ * @private
3783
+ */
3784
+ onopen() {
3785
+ // clear old subs
3786
+ this.cleanup();
3787
+ // mark as open
3788
+ this._readyState = "open";
3789
+ this.emitReserved("open");
3790
+ // add new subs
3791
+ const socket = this.engine;
3792
+ this.subs.push(on(socket, "ping", this.onping.bind(this)), on(socket, "data", this.ondata.bind(this)), on(socket, "error", this.onerror.bind(this)), on(socket, "close", this.onclose.bind(this)),
3793
+ // @ts-ignore
3794
+ on(this.decoder, "decoded", this.ondecoded.bind(this)));
3795
+ }
3796
+ /**
3797
+ * Called upon a ping.
3798
+ *
3799
+ * @private
3800
+ */
3801
+ onping() {
3802
+ this.emitReserved("ping");
3803
+ }
3804
+ /**
3805
+ * Called with data.
3806
+ *
3807
+ * @private
3808
+ */
3809
+ ondata(data) {
3810
+ try {
3811
+ this.decoder.add(data);
3812
+ }
3813
+ catch (e) {
3814
+ this.onclose("parse error", e);
3815
+ }
3816
+ }
3817
+ /**
3818
+ * Called when parser fully decodes a packet.
3819
+ *
3820
+ * @private
3821
+ */
3822
+ ondecoded(packet) {
3823
+ // the nextTick call prevents an exception in a user-provided event listener from triggering a disconnection due to a "parse error"
3824
+ nextTick(() => {
3825
+ this.emitReserved("packet", packet);
3826
+ }, this.setTimeoutFn);
3827
+ }
3828
+ /**
3829
+ * Called upon socket error.
3830
+ *
3831
+ * @private
3832
+ */
3833
+ onerror(err) {
3834
+ this.emitReserved("error", err);
3835
+ }
3836
+ /**
3837
+ * Creates a new socket for the given `nsp`.
3838
+ *
3839
+ * @return {Socket}
3840
+ * @public
3841
+ */
3842
+ socket(nsp, opts) {
3843
+ let socket = this.nsps[nsp];
3844
+ if (!socket) {
3845
+ socket = new Socket(this, nsp, opts);
3846
+ this.nsps[nsp] = socket;
3847
+ }
3848
+ else if (this._autoConnect && !socket.active) {
3849
+ socket.connect();
3850
+ }
3851
+ return socket;
3852
+ }
3853
+ /**
3854
+ * Called upon a socket close.
3855
+ *
3856
+ * @param socket
3857
+ * @private
3858
+ */
3859
+ _destroy(socket) {
3860
+ const nsps = Object.keys(this.nsps);
3861
+ for (const nsp of nsps) {
3862
+ const socket = this.nsps[nsp];
3863
+ if (socket.active) {
3864
+ return;
3865
+ }
3866
+ }
3867
+ this._close();
3868
+ }
3869
+ /**
3870
+ * Writes a packet.
3871
+ *
3872
+ * @param packet
3873
+ * @private
3874
+ */
3875
+ _packet(packet) {
3876
+ const encodedPackets = this.encoder.encode(packet);
3877
+ for (let i = 0; i < encodedPackets.length; i++) {
3878
+ this.engine.write(encodedPackets[i], packet.options);
3879
+ }
3880
+ }
3881
+ /**
3882
+ * Clean up transport subscriptions and packet buffer.
3883
+ *
3884
+ * @private
3885
+ */
3886
+ cleanup() {
3887
+ this.subs.forEach((subDestroy) => subDestroy());
3888
+ this.subs.length = 0;
3889
+ this.decoder.destroy();
3890
+ }
3891
+ /**
3892
+ * Close the current socket.
3893
+ *
3894
+ * @private
3895
+ */
3896
+ _close() {
3897
+ this.skipReconnect = true;
3898
+ this._reconnecting = false;
3899
+ this.onclose("forced close");
3900
+ }
3901
+ /**
3902
+ * Alias for close()
3903
+ *
3904
+ * @private
3905
+ */
3906
+ disconnect() {
3907
+ return this._close();
3908
+ }
3909
+ /**
3910
+ * Called when:
3911
+ *
3912
+ * - the low-level engine is closed
3913
+ * - the parser encountered a badly formatted packet
3914
+ * - all sockets are disconnected
3915
+ *
3916
+ * @private
3917
+ */
3918
+ onclose(reason, description) {
3919
+ var _a;
3920
+ this.cleanup();
3921
+ (_a = this.engine) === null || _a === void 0 ? void 0 : _a.close();
3922
+ this.backoff.reset();
3923
+ this._readyState = "closed";
3924
+ this.emitReserved("close", reason, description);
3925
+ if (this._reconnection && !this.skipReconnect) {
3926
+ this.reconnect();
3927
+ }
3928
+ }
3929
+ /**
3930
+ * Attempt a reconnection.
3931
+ *
3932
+ * @private
3933
+ */
3934
+ reconnect() {
3935
+ if (this._reconnecting || this.skipReconnect)
3936
+ return this;
3937
+ const self = this;
3938
+ if (this.backoff.attempts >= this._reconnectionAttempts) {
3939
+ this.backoff.reset();
3940
+ this.emitReserved("reconnect_failed");
3941
+ this._reconnecting = false;
3942
+ }
3943
+ else {
3944
+ const delay = this.backoff.duration();
3945
+ this._reconnecting = true;
3946
+ const timer = this.setTimeoutFn(() => {
3947
+ if (self.skipReconnect)
3948
+ return;
3949
+ this.emitReserved("reconnect_attempt", self.backoff.attempts);
3950
+ // check again for the case socket closed in above events
3951
+ if (self.skipReconnect)
3952
+ return;
3953
+ self.open((err) => {
3954
+ if (err) {
3955
+ self._reconnecting = false;
3956
+ self.reconnect();
3957
+ this.emitReserved("reconnect_error", err);
3958
+ }
3959
+ else {
3960
+ self.onreconnect();
3961
+ }
3962
+ });
3963
+ }, delay);
3964
+ if (this.opts.autoUnref) {
3965
+ timer.unref();
3966
+ }
3967
+ this.subs.push(() => {
3968
+ this.clearTimeoutFn(timer);
3969
+ });
3970
+ }
3971
+ }
3972
+ /**
3973
+ * Called upon successful reconnect.
3974
+ *
3975
+ * @private
3976
+ */
3977
+ onreconnect() {
3978
+ const attempt = this.backoff.attempts;
3979
+ this._reconnecting = false;
3980
+ this.backoff.reset();
3981
+ this.emitReserved("reconnect", attempt);
3982
+ }
3983
+ }
3984
+
3985
+ /**
3986
+ * Managers cache.
3987
+ */
3988
+ const cache = {};
3989
+ function lookup(uri, opts) {
3990
+ if (typeof uri === "object") {
3991
+ opts = uri;
3992
+ uri = undefined;
3993
+ }
3994
+ opts = opts || {};
3995
+ const parsed = url(uri, opts.path || "/socket.io");
3996
+ const source = parsed.source;
3997
+ const id = parsed.id;
3998
+ const path = parsed.path;
3999
+ const sameNamespace = cache[id] && path in cache[id]["nsps"];
4000
+ const newConnection = opts.forceNew ||
4001
+ opts["force new connection"] ||
4002
+ false === opts.multiplex ||
4003
+ sameNamespace;
4004
+ let io;
4005
+ if (newConnection) {
4006
+ io = new Manager(source, opts);
4007
+ }
4008
+ else {
4009
+ if (!cache[id]) {
4010
+ cache[id] = new Manager(source, opts);
4011
+ }
4012
+ io = cache[id];
4013
+ }
4014
+ if (parsed.query && !opts.query) {
4015
+ opts.query = parsed.queryKey;
4016
+ }
4017
+ return io.socket(parsed.path, opts);
4018
+ }
4019
+ // so that "lookup" can be used both as a function (e.g. `io(...)`) and as a
4020
+ // namespace (e.g. `io.connect(...)`), for backward compatibility
4021
+ Object.assign(lookup, {
4022
+ Manager,
4023
+ Socket,
4024
+ io: lookup,
4025
+ connect: lookup,
4026
+ });
4027
+
4028
+ let socket = null;
4029
+ function initSocket(options = {}) {
4030
+ if (!socket) {
4031
+ const url = baseURL + '/chat';
4032
+ socket = lookup(url, {
4033
+ transports: ['websocket'],
4034
+ autoConnect: true,
4035
+ reconnection: true,
4036
+ reconnectionDelay: 1000,
4037
+ reconnectionDelayMax: 10000,
4038
+ reconnectionAttempts: 10,
4039
+ timeout: 20000,
4040
+ forceNew: true,
4041
+ auth: {
4042
+ accessToken: configStore.get('accessToken'),
4043
+ },
4044
+ ...options
4045
+ });
4046
+ socket.on('connect', () => {
4047
+ console.log('[socket] Connected:', socket?.id);
4048
+ });
4049
+ socket.on('disconnect', () => {
4050
+ console.log('[socket] Disconnected');
4051
+ });
4052
+ }
4053
+ return socket;
4054
+ }
4055
+ function disconnectSocket() {
4056
+ if (socket) {
4057
+ socket.removeAllListeners();
4058
+ socket.disconnect();
4059
+ socket = null;
4060
+ }
4061
+ }
4062
+ function joinRoom(room) {
4063
+ if (socket && socket.connected) {
4064
+ socket.emit('subscribe', { room });
4065
+ console.log(`[socket] Joined room: ${room}`);
4066
+ }
4067
+ else {
4068
+ console.warn('[socket] Cannot join room: socket not connected');
4069
+ }
4070
+ }
4071
+ function leaveRoom(room) {
4072
+ if (socket && socket.connected) {
4073
+ socket.emit('unsubscribe', { room });
4074
+ console.log(`[socket] Leaved room: ${room}`);
4075
+ }
4076
+ else {
4077
+ console.warn('[socket] Cannot leaved room: socket not connected');
4078
+ }
4079
+ }
4080
+
4081
+ export { disconnectSocket as d, initSocket as i, joinRoom as j, leaveRoom as l };
4082
+ //# sourceMappingURL=p-LCZ_Vyoy.js.map
4083
+
4084
+ //# sourceMappingURL=p-LCZ_Vyoy.js.map