j1-template 2023.0.2 → 2023.0.3

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