@vivek058/livechatsdk 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/livechat.es.js +2328 -0
- package/dist/livechat.umd.js +1 -0
- package/package.json +27 -0
|
@@ -0,0 +1,2328 @@
|
|
|
1
|
+
const y = /* @__PURE__ */ Object.create(null);
|
|
2
|
+
y.open = "0";
|
|
3
|
+
y.close = "1";
|
|
4
|
+
y.ping = "2";
|
|
5
|
+
y.pong = "3";
|
|
6
|
+
y.message = "4";
|
|
7
|
+
y.upgrade = "5";
|
|
8
|
+
y.noop = "6";
|
|
9
|
+
const A = /* @__PURE__ */ Object.create(null);
|
|
10
|
+
Object.keys(y).forEach((n) => {
|
|
11
|
+
A[y[n]] = n;
|
|
12
|
+
});
|
|
13
|
+
const P = { type: "error", data: "parser error" }, G = typeof Blob == "function" || typeof Blob < "u" && Object.prototype.toString.call(Blob) === "[object BlobConstructor]", Z = typeof ArrayBuffer == "function", ee = (n) => typeof ArrayBuffer.isView == "function" ? ArrayBuffer.isView(n) : n && n.buffer instanceof ArrayBuffer, M = ({ type: n, data: e }, t, s) => G && e instanceof Blob ? t ? s(e) : z(e, s) : Z && (e instanceof ArrayBuffer || ee(e)) ? t ? s(e) : z(new Blob([e]), s) : s(y[n] + (e || "")), z = (n, e) => {
|
|
14
|
+
const t = new FileReader();
|
|
15
|
+
return t.onload = function() {
|
|
16
|
+
const s = t.result.split(",")[1];
|
|
17
|
+
e("b" + (s || ""));
|
|
18
|
+
}, t.readAsDataURL(n);
|
|
19
|
+
};
|
|
20
|
+
function J(n) {
|
|
21
|
+
return n instanceof Uint8Array ? n : n instanceof ArrayBuffer ? new Uint8Array(n) : new Uint8Array(n.buffer, n.byteOffset, n.byteLength);
|
|
22
|
+
}
|
|
23
|
+
let B;
|
|
24
|
+
function fe(n, e) {
|
|
25
|
+
if (G && n.data instanceof Blob)
|
|
26
|
+
return n.data.arrayBuffer().then(J).then(e);
|
|
27
|
+
if (Z && (n.data instanceof ArrayBuffer || ee(n.data)))
|
|
28
|
+
return e(J(n.data));
|
|
29
|
+
M(n, !1, (t) => {
|
|
30
|
+
B || (B = new TextEncoder()), e(B.encode(t));
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
const X = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/", E = typeof Uint8Array > "u" ? [] : new Uint8Array(256);
|
|
34
|
+
for (let n = 0; n < X.length; n++)
|
|
35
|
+
E[X.charCodeAt(n)] = n;
|
|
36
|
+
const le = (n) => {
|
|
37
|
+
let e = n.length * 0.75, t = n.length, s, i = 0, r, o, a, h;
|
|
38
|
+
n[n.length - 1] === "=" && (e--, n[n.length - 2] === "=" && e--);
|
|
39
|
+
const m = new ArrayBuffer(e), f = new Uint8Array(m);
|
|
40
|
+
for (s = 0; s < t; s += 4)
|
|
41
|
+
r = E[n.charCodeAt(s)], o = E[n.charCodeAt(s + 1)], a = E[n.charCodeAt(s + 2)], h = E[n.charCodeAt(s + 3)], f[i++] = r << 2 | o >> 4, f[i++] = (o & 15) << 4 | a >> 2, f[i++] = (a & 3) << 6 | h & 63;
|
|
42
|
+
return m;
|
|
43
|
+
}, pe = typeof ArrayBuffer == "function", H = (n, e) => {
|
|
44
|
+
if (typeof n != "string")
|
|
45
|
+
return {
|
|
46
|
+
type: "message",
|
|
47
|
+
data: te(n, e)
|
|
48
|
+
};
|
|
49
|
+
const t = n.charAt(0);
|
|
50
|
+
return t === "b" ? {
|
|
51
|
+
type: "message",
|
|
52
|
+
data: de(n.substring(1), e)
|
|
53
|
+
} : A[t] ? n.length > 1 ? {
|
|
54
|
+
type: A[t],
|
|
55
|
+
data: n.substring(1)
|
|
56
|
+
} : {
|
|
57
|
+
type: A[t]
|
|
58
|
+
} : P;
|
|
59
|
+
}, de = (n, e) => {
|
|
60
|
+
if (pe) {
|
|
61
|
+
const t = le(n);
|
|
62
|
+
return te(t, e);
|
|
63
|
+
} else
|
|
64
|
+
return { base64: !0, data: n };
|
|
65
|
+
}, te = (n, e) => e === "blob" ? n instanceof Blob ? n : new Blob([n]) : n instanceof ArrayBuffer ? n : n.buffer, se = "", ye = (n, e) => {
|
|
66
|
+
const t = n.length, s = new Array(t);
|
|
67
|
+
let i = 0;
|
|
68
|
+
n.forEach((r, o) => {
|
|
69
|
+
M(r, !1, (a) => {
|
|
70
|
+
s[o] = a, ++i === t && e(s.join(se));
|
|
71
|
+
});
|
|
72
|
+
});
|
|
73
|
+
}, me = (n, e) => {
|
|
74
|
+
const t = n.split(se), s = [];
|
|
75
|
+
for (let i = 0; i < t.length; i++) {
|
|
76
|
+
const r = H(t[i], e);
|
|
77
|
+
if (s.push(r), r.type === "error")
|
|
78
|
+
break;
|
|
79
|
+
}
|
|
80
|
+
return s;
|
|
81
|
+
};
|
|
82
|
+
function ge() {
|
|
83
|
+
return new TransformStream({
|
|
84
|
+
transform(n, e) {
|
|
85
|
+
fe(n, (t) => {
|
|
86
|
+
const s = t.length;
|
|
87
|
+
let i;
|
|
88
|
+
if (s < 126)
|
|
89
|
+
i = new Uint8Array(1), new DataView(i.buffer).setUint8(0, s);
|
|
90
|
+
else if (s < 65536) {
|
|
91
|
+
i = new Uint8Array(3);
|
|
92
|
+
const r = new DataView(i.buffer);
|
|
93
|
+
r.setUint8(0, 126), r.setUint16(1, s);
|
|
94
|
+
} else {
|
|
95
|
+
i = new Uint8Array(9);
|
|
96
|
+
const r = new DataView(i.buffer);
|
|
97
|
+
r.setUint8(0, 127), r.setBigUint64(1, BigInt(s));
|
|
98
|
+
}
|
|
99
|
+
n.data && typeof n.data != "string" && (i[0] |= 128), e.enqueue(i), e.enqueue(t);
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
let N;
|
|
105
|
+
function k(n) {
|
|
106
|
+
return n.reduce((e, t) => e + t.length, 0);
|
|
107
|
+
}
|
|
108
|
+
function T(n, e) {
|
|
109
|
+
if (n[0].length === e)
|
|
110
|
+
return n.shift();
|
|
111
|
+
const t = new Uint8Array(e);
|
|
112
|
+
let s = 0;
|
|
113
|
+
for (let i = 0; i < e; i++)
|
|
114
|
+
t[i] = n[0][s++], s === n[0].length && (n.shift(), s = 0);
|
|
115
|
+
return n.length && s < n[0].length && (n[0] = n[0].slice(s)), t;
|
|
116
|
+
}
|
|
117
|
+
function _e(n, e) {
|
|
118
|
+
N || (N = new TextDecoder());
|
|
119
|
+
const t = [];
|
|
120
|
+
let s = 0, i = -1, r = !1;
|
|
121
|
+
return new TransformStream({
|
|
122
|
+
transform(o, a) {
|
|
123
|
+
for (t.push(o); ; ) {
|
|
124
|
+
if (s === 0) {
|
|
125
|
+
if (k(t) < 1)
|
|
126
|
+
break;
|
|
127
|
+
const h = T(t, 1);
|
|
128
|
+
r = (h[0] & 128) === 128, i = h[0] & 127, i < 126 ? s = 3 : i === 126 ? s = 1 : s = 2;
|
|
129
|
+
} else if (s === 1) {
|
|
130
|
+
if (k(t) < 2)
|
|
131
|
+
break;
|
|
132
|
+
const h = T(t, 2);
|
|
133
|
+
i = new DataView(h.buffer, h.byteOffset, h.length).getUint16(0), s = 3;
|
|
134
|
+
} else if (s === 2) {
|
|
135
|
+
if (k(t) < 8)
|
|
136
|
+
break;
|
|
137
|
+
const h = T(t, 8), m = new DataView(h.buffer, h.byteOffset, h.length), f = m.getUint32(0);
|
|
138
|
+
if (f > Math.pow(2, 21) - 1) {
|
|
139
|
+
a.enqueue(P);
|
|
140
|
+
break;
|
|
141
|
+
}
|
|
142
|
+
i = f * Math.pow(2, 32) + m.getUint32(4), s = 3;
|
|
143
|
+
} else {
|
|
144
|
+
if (k(t) < i)
|
|
145
|
+
break;
|
|
146
|
+
const h = T(t, i);
|
|
147
|
+
a.enqueue(H(r ? h : N.decode(h), e)), s = 0;
|
|
148
|
+
}
|
|
149
|
+
if (i === 0 || i > n) {
|
|
150
|
+
a.enqueue(P);
|
|
151
|
+
break;
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
});
|
|
156
|
+
}
|
|
157
|
+
const ne = 4;
|
|
158
|
+
function u(n) {
|
|
159
|
+
if (n) return be(n);
|
|
160
|
+
}
|
|
161
|
+
function be(n) {
|
|
162
|
+
for (var e in u.prototype)
|
|
163
|
+
n[e] = u.prototype[e];
|
|
164
|
+
return n;
|
|
165
|
+
}
|
|
166
|
+
u.prototype.on = u.prototype.addEventListener = function(n, e) {
|
|
167
|
+
return this._callbacks = this._callbacks || {}, (this._callbacks["$" + n] = this._callbacks["$" + n] || []).push(e), this;
|
|
168
|
+
};
|
|
169
|
+
u.prototype.once = function(n, e) {
|
|
170
|
+
function t() {
|
|
171
|
+
this.off(n, t), e.apply(this, arguments);
|
|
172
|
+
}
|
|
173
|
+
return t.fn = e, this.on(n, t), this;
|
|
174
|
+
};
|
|
175
|
+
u.prototype.off = u.prototype.removeListener = u.prototype.removeAllListeners = u.prototype.removeEventListener = function(n, e) {
|
|
176
|
+
if (this._callbacks = this._callbacks || {}, arguments.length == 0)
|
|
177
|
+
return this._callbacks = {}, this;
|
|
178
|
+
var t = this._callbacks["$" + n];
|
|
179
|
+
if (!t) return this;
|
|
180
|
+
if (arguments.length == 1)
|
|
181
|
+
return delete this._callbacks["$" + n], this;
|
|
182
|
+
for (var s, i = 0; i < t.length; i++)
|
|
183
|
+
if (s = t[i], s === e || s.fn === e) {
|
|
184
|
+
t.splice(i, 1);
|
|
185
|
+
break;
|
|
186
|
+
}
|
|
187
|
+
return t.length === 0 && delete this._callbacks["$" + n], this;
|
|
188
|
+
};
|
|
189
|
+
u.prototype.emit = function(n) {
|
|
190
|
+
this._callbacks = this._callbacks || {};
|
|
191
|
+
for (var e = new Array(arguments.length - 1), t = this._callbacks["$" + n], s = 1; s < arguments.length; s++)
|
|
192
|
+
e[s - 1] = arguments[s];
|
|
193
|
+
if (t) {
|
|
194
|
+
t = t.slice(0);
|
|
195
|
+
for (var s = 0, i = t.length; s < i; ++s)
|
|
196
|
+
t[s].apply(this, e);
|
|
197
|
+
}
|
|
198
|
+
return this;
|
|
199
|
+
};
|
|
200
|
+
u.prototype.emitReserved = u.prototype.emit;
|
|
201
|
+
u.prototype.listeners = function(n) {
|
|
202
|
+
return this._callbacks = this._callbacks || {}, this._callbacks["$" + n] || [];
|
|
203
|
+
};
|
|
204
|
+
u.prototype.hasListeners = function(n) {
|
|
205
|
+
return !!this.listeners(n).length;
|
|
206
|
+
};
|
|
207
|
+
const x = typeof Promise == "function" && typeof Promise.resolve == "function" ? (e) => Promise.resolve().then(e) : (e, t) => t(e, 0), l = typeof self < "u" ? self : typeof window < "u" ? window : Function("return this")(), we = "arraybuffer";
|
|
208
|
+
function ie(n, ...e) {
|
|
209
|
+
return e.reduce((t, s) => (n.hasOwnProperty(s) && (t[s] = n[s]), t), {});
|
|
210
|
+
}
|
|
211
|
+
const ve = l.setTimeout, Ee = l.clearTimeout;
|
|
212
|
+
function S(n, e) {
|
|
213
|
+
e.useNativeTimers ? (n.setTimeoutFn = ve.bind(l), n.clearTimeoutFn = Ee.bind(l)) : (n.setTimeoutFn = l.setTimeout.bind(l), n.clearTimeoutFn = l.clearTimeout.bind(l));
|
|
214
|
+
}
|
|
215
|
+
const ke = 1.33;
|
|
216
|
+
function Te(n) {
|
|
217
|
+
return typeof n == "string" ? Ae(n) : Math.ceil((n.byteLength || n.size) * ke);
|
|
218
|
+
}
|
|
219
|
+
function Ae(n) {
|
|
220
|
+
let e = 0, t = 0;
|
|
221
|
+
for (let s = 0, i = n.length; s < i; s++)
|
|
222
|
+
e = n.charCodeAt(s), e < 128 ? t += 1 : e < 2048 ? t += 2 : e < 55296 || e >= 57344 ? t += 3 : (s++, t += 4);
|
|
223
|
+
return t;
|
|
224
|
+
}
|
|
225
|
+
function re() {
|
|
226
|
+
return Date.now().toString(36).substring(3) + Math.random().toString(36).substring(2, 5);
|
|
227
|
+
}
|
|
228
|
+
function Re(n) {
|
|
229
|
+
let e = "";
|
|
230
|
+
for (let t in n)
|
|
231
|
+
n.hasOwnProperty(t) && (e.length && (e += "&"), e += encodeURIComponent(t) + "=" + encodeURIComponent(n[t]));
|
|
232
|
+
return e;
|
|
233
|
+
}
|
|
234
|
+
function Oe(n) {
|
|
235
|
+
let e = {}, t = n.split("&");
|
|
236
|
+
for (let s = 0, i = t.length; s < i; s++) {
|
|
237
|
+
let r = t[s].split("=");
|
|
238
|
+
e[decodeURIComponent(r[0])] = decodeURIComponent(r[1]);
|
|
239
|
+
}
|
|
240
|
+
return e;
|
|
241
|
+
}
|
|
242
|
+
class Ce extends Error {
|
|
243
|
+
constructor(e, t, s) {
|
|
244
|
+
super(e), this.description = t, this.context = s, this.type = "TransportError";
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
class K extends u {
|
|
248
|
+
/**
|
|
249
|
+
* Transport abstract constructor.
|
|
250
|
+
*
|
|
251
|
+
* @param {Object} opts - options
|
|
252
|
+
* @protected
|
|
253
|
+
*/
|
|
254
|
+
constructor(e) {
|
|
255
|
+
super(), this.writable = !1, S(this, e), this.opts = e, this.query = e.query, this.socket = e.socket, this.supportsBinary = !e.forceBase64;
|
|
256
|
+
}
|
|
257
|
+
/**
|
|
258
|
+
* Emits an error.
|
|
259
|
+
*
|
|
260
|
+
* @param {String} reason
|
|
261
|
+
* @param description
|
|
262
|
+
* @param context - the error context
|
|
263
|
+
* @return {Transport} for chaining
|
|
264
|
+
* @protected
|
|
265
|
+
*/
|
|
266
|
+
onError(e, t, s) {
|
|
267
|
+
return super.emitReserved("error", new Ce(e, t, s)), this;
|
|
268
|
+
}
|
|
269
|
+
/**
|
|
270
|
+
* Opens the transport.
|
|
271
|
+
*/
|
|
272
|
+
open() {
|
|
273
|
+
return this.readyState = "opening", this.doOpen(), this;
|
|
274
|
+
}
|
|
275
|
+
/**
|
|
276
|
+
* Closes the transport.
|
|
277
|
+
*/
|
|
278
|
+
close() {
|
|
279
|
+
return (this.readyState === "opening" || this.readyState === "open") && (this.doClose(), this.onClose()), this;
|
|
280
|
+
}
|
|
281
|
+
/**
|
|
282
|
+
* Sends multiple packets.
|
|
283
|
+
*
|
|
284
|
+
* @param {Array} packets
|
|
285
|
+
*/
|
|
286
|
+
send(e) {
|
|
287
|
+
this.readyState === "open" && this.write(e);
|
|
288
|
+
}
|
|
289
|
+
/**
|
|
290
|
+
* Called upon open
|
|
291
|
+
*
|
|
292
|
+
* @protected
|
|
293
|
+
*/
|
|
294
|
+
onOpen() {
|
|
295
|
+
this.readyState = "open", this.writable = !0, super.emitReserved("open");
|
|
296
|
+
}
|
|
297
|
+
/**
|
|
298
|
+
* Called with data.
|
|
299
|
+
*
|
|
300
|
+
* @param {String} data
|
|
301
|
+
* @protected
|
|
302
|
+
*/
|
|
303
|
+
onData(e) {
|
|
304
|
+
const t = H(e, this.socket.binaryType);
|
|
305
|
+
this.onPacket(t);
|
|
306
|
+
}
|
|
307
|
+
/**
|
|
308
|
+
* Called with a decoded packet.
|
|
309
|
+
*
|
|
310
|
+
* @protected
|
|
311
|
+
*/
|
|
312
|
+
onPacket(e) {
|
|
313
|
+
super.emitReserved("packet", e);
|
|
314
|
+
}
|
|
315
|
+
/**
|
|
316
|
+
* Called upon close.
|
|
317
|
+
*
|
|
318
|
+
* @protected
|
|
319
|
+
*/
|
|
320
|
+
onClose(e) {
|
|
321
|
+
this.readyState = "closed", super.emitReserved("close", e);
|
|
322
|
+
}
|
|
323
|
+
/**
|
|
324
|
+
* Pauses the transport, in order not to lose packets during an upgrade.
|
|
325
|
+
*
|
|
326
|
+
* @param onPause
|
|
327
|
+
*/
|
|
328
|
+
pause(e) {
|
|
329
|
+
}
|
|
330
|
+
createUri(e, t = {}) {
|
|
331
|
+
return e + "://" + this._hostname() + this._port() + this.opts.path + this._query(t);
|
|
332
|
+
}
|
|
333
|
+
_hostname() {
|
|
334
|
+
const e = this.opts.hostname;
|
|
335
|
+
return e.indexOf(":") === -1 ? e : "[" + e + "]";
|
|
336
|
+
}
|
|
337
|
+
_port() {
|
|
338
|
+
return this.opts.port && (this.opts.secure && Number(this.opts.port) !== 443 || !this.opts.secure && Number(this.opts.port) !== 80) ? ":" + this.opts.port : "";
|
|
339
|
+
}
|
|
340
|
+
_query(e) {
|
|
341
|
+
const t = Re(e);
|
|
342
|
+
return t.length ? "?" + t : "";
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
class xe extends K {
|
|
346
|
+
constructor() {
|
|
347
|
+
super(...arguments), this._polling = !1;
|
|
348
|
+
}
|
|
349
|
+
get name() {
|
|
350
|
+
return "polling";
|
|
351
|
+
}
|
|
352
|
+
/**
|
|
353
|
+
* Opens the socket (triggers polling). We write a PING message to determine
|
|
354
|
+
* when the transport is open.
|
|
355
|
+
*
|
|
356
|
+
* @protected
|
|
357
|
+
*/
|
|
358
|
+
doOpen() {
|
|
359
|
+
this._poll();
|
|
360
|
+
}
|
|
361
|
+
/**
|
|
362
|
+
* Pauses polling.
|
|
363
|
+
*
|
|
364
|
+
* @param {Function} onPause - callback upon buffers are flushed and transport is paused
|
|
365
|
+
* @package
|
|
366
|
+
*/
|
|
367
|
+
pause(e) {
|
|
368
|
+
this.readyState = "pausing";
|
|
369
|
+
const t = () => {
|
|
370
|
+
this.readyState = "paused", e();
|
|
371
|
+
};
|
|
372
|
+
if (this._polling || !this.writable) {
|
|
373
|
+
let s = 0;
|
|
374
|
+
this._polling && (s++, this.once("pollComplete", function() {
|
|
375
|
+
--s || t();
|
|
376
|
+
})), this.writable || (s++, this.once("drain", function() {
|
|
377
|
+
--s || t();
|
|
378
|
+
}));
|
|
379
|
+
} else
|
|
380
|
+
t();
|
|
381
|
+
}
|
|
382
|
+
/**
|
|
383
|
+
* Starts polling cycle.
|
|
384
|
+
*
|
|
385
|
+
* @private
|
|
386
|
+
*/
|
|
387
|
+
_poll() {
|
|
388
|
+
this._polling = !0, this.doPoll(), this.emitReserved("poll");
|
|
389
|
+
}
|
|
390
|
+
/**
|
|
391
|
+
* Overloads onData to detect payloads.
|
|
392
|
+
*
|
|
393
|
+
* @protected
|
|
394
|
+
*/
|
|
395
|
+
onData(e) {
|
|
396
|
+
const t = (s) => {
|
|
397
|
+
if (this.readyState === "opening" && s.type === "open" && this.onOpen(), s.type === "close")
|
|
398
|
+
return this.onClose({ description: "transport closed by the server" }), !1;
|
|
399
|
+
this.onPacket(s);
|
|
400
|
+
};
|
|
401
|
+
me(e, this.socket.binaryType).forEach(t), this.readyState !== "closed" && (this._polling = !1, this.emitReserved("pollComplete"), this.readyState === "open" && this._poll());
|
|
402
|
+
}
|
|
403
|
+
/**
|
|
404
|
+
* For polling, send a close packet.
|
|
405
|
+
*
|
|
406
|
+
* @protected
|
|
407
|
+
*/
|
|
408
|
+
doClose() {
|
|
409
|
+
const e = () => {
|
|
410
|
+
this.write([{ type: "close" }]);
|
|
411
|
+
};
|
|
412
|
+
this.readyState === "open" ? e() : this.once("open", e);
|
|
413
|
+
}
|
|
414
|
+
/**
|
|
415
|
+
* Writes a packets payload.
|
|
416
|
+
*
|
|
417
|
+
* @param {Array} packets - data packets
|
|
418
|
+
* @protected
|
|
419
|
+
*/
|
|
420
|
+
write(e) {
|
|
421
|
+
this.writable = !1, ye(e, (t) => {
|
|
422
|
+
this.doWrite(t, () => {
|
|
423
|
+
this.writable = !0, this.emitReserved("drain");
|
|
424
|
+
});
|
|
425
|
+
});
|
|
426
|
+
}
|
|
427
|
+
/**
|
|
428
|
+
* Generates uri for connection.
|
|
429
|
+
*
|
|
430
|
+
* @private
|
|
431
|
+
*/
|
|
432
|
+
uri() {
|
|
433
|
+
const e = this.opts.secure ? "https" : "http", t = this.query || {};
|
|
434
|
+
return this.opts.timestampRequests !== !1 && (t[this.opts.timestampParam] = re()), !this.supportsBinary && !t.sid && (t.b64 = 1), this.createUri(e, t);
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
let oe = !1;
|
|
438
|
+
try {
|
|
439
|
+
oe = typeof XMLHttpRequest < "u" && "withCredentials" in new XMLHttpRequest();
|
|
440
|
+
} catch {
|
|
441
|
+
}
|
|
442
|
+
const Se = oe;
|
|
443
|
+
function Be() {
|
|
444
|
+
}
|
|
445
|
+
class Ne extends xe {
|
|
446
|
+
/**
|
|
447
|
+
* XHR Polling constructor.
|
|
448
|
+
*
|
|
449
|
+
* @param {Object} opts
|
|
450
|
+
* @package
|
|
451
|
+
*/
|
|
452
|
+
constructor(e) {
|
|
453
|
+
if (super(e), typeof location < "u") {
|
|
454
|
+
const t = location.protocol === "https:";
|
|
455
|
+
let s = location.port;
|
|
456
|
+
s || (s = t ? "443" : "80"), this.xd = typeof location < "u" && e.hostname !== location.hostname || s !== e.port;
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
/**
|
|
460
|
+
* Sends data.
|
|
461
|
+
*
|
|
462
|
+
* @param {String} data to send.
|
|
463
|
+
* @param {Function} called upon flush.
|
|
464
|
+
* @private
|
|
465
|
+
*/
|
|
466
|
+
doWrite(e, t) {
|
|
467
|
+
const s = this.request({
|
|
468
|
+
method: "POST",
|
|
469
|
+
data: e
|
|
470
|
+
});
|
|
471
|
+
s.on("success", t), s.on("error", (i, r) => {
|
|
472
|
+
this.onError("xhr post error", i, r);
|
|
473
|
+
});
|
|
474
|
+
}
|
|
475
|
+
/**
|
|
476
|
+
* Starts a poll cycle.
|
|
477
|
+
*
|
|
478
|
+
* @private
|
|
479
|
+
*/
|
|
480
|
+
doPoll() {
|
|
481
|
+
const e = this.request();
|
|
482
|
+
e.on("data", this.onData.bind(this)), e.on("error", (t, s) => {
|
|
483
|
+
this.onError("xhr poll error", t, s);
|
|
484
|
+
}), this.pollXhr = e;
|
|
485
|
+
}
|
|
486
|
+
}
|
|
487
|
+
class d extends u {
|
|
488
|
+
/**
|
|
489
|
+
* Request constructor
|
|
490
|
+
*
|
|
491
|
+
* @param {Object} options
|
|
492
|
+
* @package
|
|
493
|
+
*/
|
|
494
|
+
constructor(e, t, s) {
|
|
495
|
+
super(), this.createRequest = e, S(this, s), this._opts = s, this._method = s.method || "GET", this._uri = t, this._data = s.data !== void 0 ? s.data : null, this._create();
|
|
496
|
+
}
|
|
497
|
+
/**
|
|
498
|
+
* Creates the XHR object and sends the request.
|
|
499
|
+
*
|
|
500
|
+
* @private
|
|
501
|
+
*/
|
|
502
|
+
_create() {
|
|
503
|
+
var e;
|
|
504
|
+
const t = ie(this._opts, "agent", "pfx", "key", "passphrase", "cert", "ca", "ciphers", "rejectUnauthorized", "autoUnref");
|
|
505
|
+
t.xdomain = !!this._opts.xd;
|
|
506
|
+
const s = this._xhr = this.createRequest(t);
|
|
507
|
+
try {
|
|
508
|
+
s.open(this._method, this._uri, !0);
|
|
509
|
+
try {
|
|
510
|
+
if (this._opts.extraHeaders) {
|
|
511
|
+
s.setDisableHeaderCheck && s.setDisableHeaderCheck(!0);
|
|
512
|
+
for (let i in this._opts.extraHeaders)
|
|
513
|
+
this._opts.extraHeaders.hasOwnProperty(i) && s.setRequestHeader(i, this._opts.extraHeaders[i]);
|
|
514
|
+
}
|
|
515
|
+
} catch {
|
|
516
|
+
}
|
|
517
|
+
if (this._method === "POST")
|
|
518
|
+
try {
|
|
519
|
+
s.setRequestHeader("Content-type", "text/plain;charset=UTF-8");
|
|
520
|
+
} catch {
|
|
521
|
+
}
|
|
522
|
+
try {
|
|
523
|
+
s.setRequestHeader("Accept", "*/*");
|
|
524
|
+
} catch {
|
|
525
|
+
}
|
|
526
|
+
(e = this._opts.cookieJar) === null || e === void 0 || e.addCookies(s), "withCredentials" in s && (s.withCredentials = this._opts.withCredentials), this._opts.requestTimeout && (s.timeout = this._opts.requestTimeout), s.onreadystatechange = () => {
|
|
527
|
+
var i;
|
|
528
|
+
s.readyState === 3 && ((i = this._opts.cookieJar) === null || i === void 0 || i.parseCookies(
|
|
529
|
+
// @ts-ignore
|
|
530
|
+
s.getResponseHeader("set-cookie")
|
|
531
|
+
)), s.readyState === 4 && (s.status === 200 || s.status === 1223 ? this._onLoad() : this.setTimeoutFn(() => {
|
|
532
|
+
this._onError(typeof s.status == "number" ? s.status : 0);
|
|
533
|
+
}, 0));
|
|
534
|
+
}, s.send(this._data);
|
|
535
|
+
} catch (i) {
|
|
536
|
+
this.setTimeoutFn(() => {
|
|
537
|
+
this._onError(i);
|
|
538
|
+
}, 0);
|
|
539
|
+
return;
|
|
540
|
+
}
|
|
541
|
+
typeof document < "u" && (this._index = d.requestsCount++, d.requests[this._index] = this);
|
|
542
|
+
}
|
|
543
|
+
/**
|
|
544
|
+
* Called upon error.
|
|
545
|
+
*
|
|
546
|
+
* @private
|
|
547
|
+
*/
|
|
548
|
+
_onError(e) {
|
|
549
|
+
this.emitReserved("error", e, this._xhr), this._cleanup(!0);
|
|
550
|
+
}
|
|
551
|
+
/**
|
|
552
|
+
* Cleans up house.
|
|
553
|
+
*
|
|
554
|
+
* @private
|
|
555
|
+
*/
|
|
556
|
+
_cleanup(e) {
|
|
557
|
+
if (!(typeof this._xhr > "u" || this._xhr === null)) {
|
|
558
|
+
if (this._xhr.onreadystatechange = Be, e)
|
|
559
|
+
try {
|
|
560
|
+
this._xhr.abort();
|
|
561
|
+
} catch {
|
|
562
|
+
}
|
|
563
|
+
typeof document < "u" && delete d.requests[this._index], this._xhr = null;
|
|
564
|
+
}
|
|
565
|
+
}
|
|
566
|
+
/**
|
|
567
|
+
* Called upon load.
|
|
568
|
+
*
|
|
569
|
+
* @private
|
|
570
|
+
*/
|
|
571
|
+
_onLoad() {
|
|
572
|
+
const e = this._xhr.responseText;
|
|
573
|
+
e !== null && (this.emitReserved("data", e), this.emitReserved("success"), this._cleanup());
|
|
574
|
+
}
|
|
575
|
+
/**
|
|
576
|
+
* Aborts the request.
|
|
577
|
+
*
|
|
578
|
+
* @package
|
|
579
|
+
*/
|
|
580
|
+
abort() {
|
|
581
|
+
this._cleanup();
|
|
582
|
+
}
|
|
583
|
+
}
|
|
584
|
+
d.requestsCount = 0;
|
|
585
|
+
d.requests = {};
|
|
586
|
+
if (typeof document < "u") {
|
|
587
|
+
if (typeof attachEvent == "function")
|
|
588
|
+
attachEvent("onunload", Q);
|
|
589
|
+
else if (typeof addEventListener == "function") {
|
|
590
|
+
const n = "onpagehide" in l ? "pagehide" : "unload";
|
|
591
|
+
addEventListener(n, Q, !1);
|
|
592
|
+
}
|
|
593
|
+
}
|
|
594
|
+
function Q() {
|
|
595
|
+
for (let n in d.requests)
|
|
596
|
+
d.requests.hasOwnProperty(n) && d.requests[n].abort();
|
|
597
|
+
}
|
|
598
|
+
const Le = (function() {
|
|
599
|
+
const n = ae({
|
|
600
|
+
xdomain: !1
|
|
601
|
+
});
|
|
602
|
+
return n && n.responseType !== null;
|
|
603
|
+
})();
|
|
604
|
+
class Pe extends Ne {
|
|
605
|
+
constructor(e) {
|
|
606
|
+
super(e);
|
|
607
|
+
const t = e && e.forceBase64;
|
|
608
|
+
this.supportsBinary = Le && !t;
|
|
609
|
+
}
|
|
610
|
+
request(e = {}) {
|
|
611
|
+
return Object.assign(e, { xd: this.xd }, this.opts), new d(ae, this.uri(), e);
|
|
612
|
+
}
|
|
613
|
+
}
|
|
614
|
+
function ae(n) {
|
|
615
|
+
const e = n.xdomain;
|
|
616
|
+
try {
|
|
617
|
+
if (typeof XMLHttpRequest < "u" && (!e || Se))
|
|
618
|
+
return new XMLHttpRequest();
|
|
619
|
+
} catch {
|
|
620
|
+
}
|
|
621
|
+
if (!e)
|
|
622
|
+
try {
|
|
623
|
+
return new l[["Active"].concat("Object").join("X")]("Microsoft.XMLHTTP");
|
|
624
|
+
} catch {
|
|
625
|
+
}
|
|
626
|
+
}
|
|
627
|
+
const ce = typeof navigator < "u" && typeof navigator.product == "string" && navigator.product.toLowerCase() === "reactnative";
|
|
628
|
+
class qe extends K {
|
|
629
|
+
get name() {
|
|
630
|
+
return "websocket";
|
|
631
|
+
}
|
|
632
|
+
doOpen() {
|
|
633
|
+
const e = this.uri(), t = this.opts.protocols, s = ce ? {} : ie(this.opts, "agent", "perMessageDeflate", "pfx", "key", "passphrase", "cert", "ca", "ciphers", "rejectUnauthorized", "localAddress", "protocolVersion", "origin", "maxPayload", "family", "checkServerIdentity");
|
|
634
|
+
this.opts.extraHeaders && (s.headers = this.opts.extraHeaders);
|
|
635
|
+
try {
|
|
636
|
+
this.ws = this.createSocket(e, t, s);
|
|
637
|
+
} catch (i) {
|
|
638
|
+
return this.emitReserved("error", i);
|
|
639
|
+
}
|
|
640
|
+
this.ws.binaryType = this.socket.binaryType, this.addEventListeners();
|
|
641
|
+
}
|
|
642
|
+
/**
|
|
643
|
+
* Adds event listeners to the socket
|
|
644
|
+
*
|
|
645
|
+
* @private
|
|
646
|
+
*/
|
|
647
|
+
addEventListeners() {
|
|
648
|
+
this.ws.onopen = () => {
|
|
649
|
+
this.opts.autoUnref && this.ws._socket.unref(), this.onOpen();
|
|
650
|
+
}, this.ws.onclose = (e) => this.onClose({
|
|
651
|
+
description: "websocket connection closed",
|
|
652
|
+
context: e
|
|
653
|
+
}), this.ws.onmessage = (e) => this.onData(e.data), this.ws.onerror = (e) => this.onError("websocket error", e);
|
|
654
|
+
}
|
|
655
|
+
write(e) {
|
|
656
|
+
this.writable = !1;
|
|
657
|
+
for (let t = 0; t < e.length; t++) {
|
|
658
|
+
const s = e[t], i = t === e.length - 1;
|
|
659
|
+
M(s, this.supportsBinary, (r) => {
|
|
660
|
+
try {
|
|
661
|
+
this.doWrite(s, r);
|
|
662
|
+
} catch {
|
|
663
|
+
}
|
|
664
|
+
i && x(() => {
|
|
665
|
+
this.writable = !0, this.emitReserved("drain");
|
|
666
|
+
}, this.setTimeoutFn);
|
|
667
|
+
});
|
|
668
|
+
}
|
|
669
|
+
}
|
|
670
|
+
doClose() {
|
|
671
|
+
typeof this.ws < "u" && (this.ws.onerror = () => {
|
|
672
|
+
}, this.ws.close(), this.ws = null);
|
|
673
|
+
}
|
|
674
|
+
/**
|
|
675
|
+
* Generates uri for connection.
|
|
676
|
+
*
|
|
677
|
+
* @private
|
|
678
|
+
*/
|
|
679
|
+
uri() {
|
|
680
|
+
const e = this.opts.secure ? "wss" : "ws", t = this.query || {};
|
|
681
|
+
return this.opts.timestampRequests && (t[this.opts.timestampParam] = re()), this.supportsBinary || (t.b64 = 1), this.createUri(e, t);
|
|
682
|
+
}
|
|
683
|
+
}
|
|
684
|
+
const L = l.WebSocket || l.MozWebSocket;
|
|
685
|
+
class De extends qe {
|
|
686
|
+
createSocket(e, t, s) {
|
|
687
|
+
return ce ? new L(e, t, s) : t ? new L(e, t) : new L(e);
|
|
688
|
+
}
|
|
689
|
+
doWrite(e, t) {
|
|
690
|
+
this.ws.send(t);
|
|
691
|
+
}
|
|
692
|
+
}
|
|
693
|
+
class Ie extends K {
|
|
694
|
+
get name() {
|
|
695
|
+
return "webtransport";
|
|
696
|
+
}
|
|
697
|
+
doOpen() {
|
|
698
|
+
try {
|
|
699
|
+
this._transport = new WebTransport(this.createUri("https"), this.opts.transportOptions[this.name]);
|
|
700
|
+
} catch (e) {
|
|
701
|
+
return this.emitReserved("error", e);
|
|
702
|
+
}
|
|
703
|
+
this._transport.closed.then(() => {
|
|
704
|
+
this.onClose();
|
|
705
|
+
}).catch((e) => {
|
|
706
|
+
this.onError("webtransport error", e);
|
|
707
|
+
}), this._transport.ready.then(() => {
|
|
708
|
+
this._transport.createBidirectionalStream().then((e) => {
|
|
709
|
+
const t = _e(Number.MAX_SAFE_INTEGER, this.socket.binaryType), s = e.readable.pipeThrough(t).getReader(), i = ge();
|
|
710
|
+
i.readable.pipeTo(e.writable), this._writer = i.writable.getWriter();
|
|
711
|
+
const r = () => {
|
|
712
|
+
s.read().then(({ done: a, value: h }) => {
|
|
713
|
+
a || (this.onPacket(h), r());
|
|
714
|
+
}).catch((a) => {
|
|
715
|
+
});
|
|
716
|
+
};
|
|
717
|
+
r();
|
|
718
|
+
const o = { type: "open" };
|
|
719
|
+
this.query.sid && (o.data = `{"sid":"${this.query.sid}"}`), this._writer.write(o).then(() => this.onOpen());
|
|
720
|
+
});
|
|
721
|
+
});
|
|
722
|
+
}
|
|
723
|
+
write(e) {
|
|
724
|
+
this.writable = !1;
|
|
725
|
+
for (let t = 0; t < e.length; t++) {
|
|
726
|
+
const s = e[t], i = t === e.length - 1;
|
|
727
|
+
this._writer.write(s).then(() => {
|
|
728
|
+
i && x(() => {
|
|
729
|
+
this.writable = !0, this.emitReserved("drain");
|
|
730
|
+
}, this.setTimeoutFn);
|
|
731
|
+
});
|
|
732
|
+
}
|
|
733
|
+
}
|
|
734
|
+
doClose() {
|
|
735
|
+
var e;
|
|
736
|
+
(e = this._transport) === null || e === void 0 || e.close();
|
|
737
|
+
}
|
|
738
|
+
}
|
|
739
|
+
const Ue = {
|
|
740
|
+
websocket: De,
|
|
741
|
+
webtransport: Ie,
|
|
742
|
+
polling: Pe
|
|
743
|
+
}, Fe = /^(?:(?![^:@\/?#]+:[^:@\/]*@)(http|https|ws|wss):\/\/)?((?:(([^:@\/?#]*)(?::([^:@\/?#]*))?)?@)?((?:[a-f0-9]{0,4}:){2,7}[a-f0-9]{0,4}|[^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/, Ve = [
|
|
744
|
+
"source",
|
|
745
|
+
"protocol",
|
|
746
|
+
"authority",
|
|
747
|
+
"userInfo",
|
|
748
|
+
"user",
|
|
749
|
+
"password",
|
|
750
|
+
"host",
|
|
751
|
+
"port",
|
|
752
|
+
"relative",
|
|
753
|
+
"path",
|
|
754
|
+
"directory",
|
|
755
|
+
"file",
|
|
756
|
+
"query",
|
|
757
|
+
"anchor"
|
|
758
|
+
];
|
|
759
|
+
function q(n) {
|
|
760
|
+
if (n.length > 8e3)
|
|
761
|
+
throw "URI too long";
|
|
762
|
+
const e = n, t = n.indexOf("["), s = n.indexOf("]");
|
|
763
|
+
t != -1 && s != -1 && (n = n.substring(0, t) + n.substring(t, s).replace(/:/g, ";") + n.substring(s, n.length));
|
|
764
|
+
let i = Fe.exec(n || ""), r = {}, o = 14;
|
|
765
|
+
for (; o--; )
|
|
766
|
+
r[Ve[o]] = i[o] || "";
|
|
767
|
+
return t != -1 && s != -1 && (r.source = e, r.host = r.host.substring(1, r.host.length - 1).replace(/;/g, ":"), r.authority = r.authority.replace("[", "").replace("]", "").replace(/;/g, ":"), r.ipv6uri = !0), r.pathNames = Me(r, r.path), r.queryKey = He(r, r.query), r;
|
|
768
|
+
}
|
|
769
|
+
function Me(n, e) {
|
|
770
|
+
const t = /\/{2,9}/g, s = e.replace(t, "/").split("/");
|
|
771
|
+
return (e.slice(0, 1) == "/" || e.length === 0) && s.splice(0, 1), e.slice(-1) == "/" && s.splice(s.length - 1, 1), s;
|
|
772
|
+
}
|
|
773
|
+
function He(n, e) {
|
|
774
|
+
const t = {};
|
|
775
|
+
return e.replace(/(?:^|&)([^&=]*)=?([^&]*)/g, function(s, i, r) {
|
|
776
|
+
i && (t[i] = r);
|
|
777
|
+
}), t;
|
|
778
|
+
}
|
|
779
|
+
const D = typeof addEventListener == "function" && typeof removeEventListener == "function", R = [];
|
|
780
|
+
D && addEventListener("offline", () => {
|
|
781
|
+
R.forEach((n) => n());
|
|
782
|
+
}, !1);
|
|
783
|
+
class _ extends u {
|
|
784
|
+
/**
|
|
785
|
+
* Socket constructor.
|
|
786
|
+
*
|
|
787
|
+
* @param {String|Object} uri - uri or options
|
|
788
|
+
* @param {Object} opts - options
|
|
789
|
+
*/
|
|
790
|
+
constructor(e, t) {
|
|
791
|
+
if (super(), this.binaryType = we, this.writeBuffer = [], this._prevBufferLen = 0, this._pingInterval = -1, this._pingTimeout = -1, this._maxPayload = -1, this._pingTimeoutTime = 1 / 0, e && typeof e == "object" && (t = e, e = null), e) {
|
|
792
|
+
const s = q(e);
|
|
793
|
+
t.hostname = s.host, t.secure = s.protocol === "https" || s.protocol === "wss", t.port = s.port, s.query && (t.query = s.query);
|
|
794
|
+
} else t.host && (t.hostname = q(t.host).host);
|
|
795
|
+
S(this, t), this.secure = t.secure != null ? t.secure : typeof location < "u" && location.protocol === "https:", t.hostname && !t.port && (t.port = this.secure ? "443" : "80"), this.hostname = t.hostname || (typeof location < "u" ? location.hostname : "localhost"), this.port = t.port || (typeof location < "u" && location.port ? location.port : this.secure ? "443" : "80"), this.transports = [], this._transportsByName = {}, t.transports.forEach((s) => {
|
|
796
|
+
const i = s.prototype.name;
|
|
797
|
+
this.transports.push(i), this._transportsByName[i] = s;
|
|
798
|
+
}), this.opts = Object.assign({
|
|
799
|
+
path: "/engine.io",
|
|
800
|
+
agent: !1,
|
|
801
|
+
withCredentials: !1,
|
|
802
|
+
upgrade: !0,
|
|
803
|
+
timestampParam: "t",
|
|
804
|
+
rememberUpgrade: !1,
|
|
805
|
+
addTrailingSlash: !0,
|
|
806
|
+
rejectUnauthorized: !0,
|
|
807
|
+
perMessageDeflate: {
|
|
808
|
+
threshold: 1024
|
|
809
|
+
},
|
|
810
|
+
transportOptions: {},
|
|
811
|
+
closeOnBeforeunload: !1
|
|
812
|
+
}, t), this.opts.path = this.opts.path.replace(/\/$/, "") + (this.opts.addTrailingSlash ? "/" : ""), typeof this.opts.query == "string" && (this.opts.query = Oe(this.opts.query)), D && (this.opts.closeOnBeforeunload && (this._beforeunloadEventListener = () => {
|
|
813
|
+
this.transport && (this.transport.removeAllListeners(), this.transport.close());
|
|
814
|
+
}, addEventListener("beforeunload", this._beforeunloadEventListener, !1)), this.hostname !== "localhost" && (this._offlineEventListener = () => {
|
|
815
|
+
this._onClose("transport close", {
|
|
816
|
+
description: "network connection lost"
|
|
817
|
+
});
|
|
818
|
+
}, R.push(this._offlineEventListener))), this.opts.withCredentials && (this._cookieJar = void 0), this._open();
|
|
819
|
+
}
|
|
820
|
+
/**
|
|
821
|
+
* Creates transport of the given type.
|
|
822
|
+
*
|
|
823
|
+
* @param {String} name - transport name
|
|
824
|
+
* @return {Transport}
|
|
825
|
+
* @private
|
|
826
|
+
*/
|
|
827
|
+
createTransport(e) {
|
|
828
|
+
const t = Object.assign({}, this.opts.query);
|
|
829
|
+
t.EIO = ne, t.transport = e, this.id && (t.sid = this.id);
|
|
830
|
+
const s = Object.assign({}, this.opts, {
|
|
831
|
+
query: t,
|
|
832
|
+
socket: this,
|
|
833
|
+
hostname: this.hostname,
|
|
834
|
+
secure: this.secure,
|
|
835
|
+
port: this.port
|
|
836
|
+
}, this.opts.transportOptions[e]);
|
|
837
|
+
return new this._transportsByName[e](s);
|
|
838
|
+
}
|
|
839
|
+
/**
|
|
840
|
+
* Initializes transport to use and starts probe.
|
|
841
|
+
*
|
|
842
|
+
* @private
|
|
843
|
+
*/
|
|
844
|
+
_open() {
|
|
845
|
+
if (this.transports.length === 0) {
|
|
846
|
+
this.setTimeoutFn(() => {
|
|
847
|
+
this.emitReserved("error", "No transports available");
|
|
848
|
+
}, 0);
|
|
849
|
+
return;
|
|
850
|
+
}
|
|
851
|
+
const e = this.opts.rememberUpgrade && _.priorWebsocketSuccess && this.transports.indexOf("websocket") !== -1 ? "websocket" : this.transports[0];
|
|
852
|
+
this.readyState = "opening";
|
|
853
|
+
const t = this.createTransport(e);
|
|
854
|
+
t.open(), this.setTransport(t);
|
|
855
|
+
}
|
|
856
|
+
/**
|
|
857
|
+
* Sets the current transport. Disables the existing one (if any).
|
|
858
|
+
*
|
|
859
|
+
* @private
|
|
860
|
+
*/
|
|
861
|
+
setTransport(e) {
|
|
862
|
+
this.transport && this.transport.removeAllListeners(), this.transport = e, e.on("drain", this._onDrain.bind(this)).on("packet", this._onPacket.bind(this)).on("error", this._onError.bind(this)).on("close", (t) => this._onClose("transport close", t));
|
|
863
|
+
}
|
|
864
|
+
/**
|
|
865
|
+
* Called when connection is deemed open.
|
|
866
|
+
*
|
|
867
|
+
* @private
|
|
868
|
+
*/
|
|
869
|
+
onOpen() {
|
|
870
|
+
this.readyState = "open", _.priorWebsocketSuccess = this.transport.name === "websocket", this.emitReserved("open"), this.flush();
|
|
871
|
+
}
|
|
872
|
+
/**
|
|
873
|
+
* Handles a packet.
|
|
874
|
+
*
|
|
875
|
+
* @private
|
|
876
|
+
*/
|
|
877
|
+
_onPacket(e) {
|
|
878
|
+
if (this.readyState === "opening" || this.readyState === "open" || this.readyState === "closing")
|
|
879
|
+
switch (this.emitReserved("packet", e), this.emitReserved("heartbeat"), e.type) {
|
|
880
|
+
case "open":
|
|
881
|
+
this.onHandshake(JSON.parse(e.data));
|
|
882
|
+
break;
|
|
883
|
+
case "ping":
|
|
884
|
+
this._sendPacket("pong"), this.emitReserved("ping"), this.emitReserved("pong"), this._resetPingTimeout();
|
|
885
|
+
break;
|
|
886
|
+
case "error":
|
|
887
|
+
const t = new Error("server error");
|
|
888
|
+
t.code = e.data, this._onError(t);
|
|
889
|
+
break;
|
|
890
|
+
case "message":
|
|
891
|
+
this.emitReserved("data", e.data), this.emitReserved("message", e.data);
|
|
892
|
+
break;
|
|
893
|
+
}
|
|
894
|
+
}
|
|
895
|
+
/**
|
|
896
|
+
* Called upon handshake completion.
|
|
897
|
+
*
|
|
898
|
+
* @param {Object} data - handshake obj
|
|
899
|
+
* @private
|
|
900
|
+
*/
|
|
901
|
+
onHandshake(e) {
|
|
902
|
+
this.emitReserved("handshake", e), this.id = e.sid, this.transport.query.sid = e.sid, this._pingInterval = e.pingInterval, this._pingTimeout = e.pingTimeout, this._maxPayload = e.maxPayload, this.onOpen(), this.readyState !== "closed" && this._resetPingTimeout();
|
|
903
|
+
}
|
|
904
|
+
/**
|
|
905
|
+
* Sets and resets ping timeout timer based on server pings.
|
|
906
|
+
*
|
|
907
|
+
* @private
|
|
908
|
+
*/
|
|
909
|
+
_resetPingTimeout() {
|
|
910
|
+
this.clearTimeoutFn(this._pingTimeoutTimer);
|
|
911
|
+
const e = this._pingInterval + this._pingTimeout;
|
|
912
|
+
this._pingTimeoutTime = Date.now() + e, this._pingTimeoutTimer = this.setTimeoutFn(() => {
|
|
913
|
+
this._onClose("ping timeout");
|
|
914
|
+
}, e), this.opts.autoUnref && this._pingTimeoutTimer.unref();
|
|
915
|
+
}
|
|
916
|
+
/**
|
|
917
|
+
* Called on `drain` event
|
|
918
|
+
*
|
|
919
|
+
* @private
|
|
920
|
+
*/
|
|
921
|
+
_onDrain() {
|
|
922
|
+
this.writeBuffer.splice(0, this._prevBufferLen), this._prevBufferLen = 0, this.writeBuffer.length === 0 ? this.emitReserved("drain") : this.flush();
|
|
923
|
+
}
|
|
924
|
+
/**
|
|
925
|
+
* Flush write buffers.
|
|
926
|
+
*
|
|
927
|
+
* @private
|
|
928
|
+
*/
|
|
929
|
+
flush() {
|
|
930
|
+
if (this.readyState !== "closed" && this.transport.writable && !this.upgrading && this.writeBuffer.length) {
|
|
931
|
+
const e = this._getWritablePackets();
|
|
932
|
+
this.transport.send(e), this._prevBufferLen = e.length, this.emitReserved("flush");
|
|
933
|
+
}
|
|
934
|
+
}
|
|
935
|
+
/**
|
|
936
|
+
* Ensure the encoded size of the writeBuffer is below the maxPayload value sent by the server (only for HTTP
|
|
937
|
+
* long-polling)
|
|
938
|
+
*
|
|
939
|
+
* @private
|
|
940
|
+
*/
|
|
941
|
+
_getWritablePackets() {
|
|
942
|
+
if (!(this._maxPayload && this.transport.name === "polling" && this.writeBuffer.length > 1))
|
|
943
|
+
return this.writeBuffer;
|
|
944
|
+
let t = 1;
|
|
945
|
+
for (let s = 0; s < this.writeBuffer.length; s++) {
|
|
946
|
+
const i = this.writeBuffer[s].data;
|
|
947
|
+
if (i && (t += Te(i)), s > 0 && t > this._maxPayload)
|
|
948
|
+
return this.writeBuffer.slice(0, s);
|
|
949
|
+
t += 2;
|
|
950
|
+
}
|
|
951
|
+
return this.writeBuffer;
|
|
952
|
+
}
|
|
953
|
+
/**
|
|
954
|
+
* Checks whether the heartbeat timer has expired but the socket has not yet been notified.
|
|
955
|
+
*
|
|
956
|
+
* Note: this method is private for now because it does not really fit the WebSocket API, but if we put it in the
|
|
957
|
+
* `write()` method then the message would not be buffered by the Socket.IO client.
|
|
958
|
+
*
|
|
959
|
+
* @return {boolean}
|
|
960
|
+
* @private
|
|
961
|
+
*/
|
|
962
|
+
/* private */
|
|
963
|
+
_hasPingExpired() {
|
|
964
|
+
if (!this._pingTimeoutTime)
|
|
965
|
+
return !0;
|
|
966
|
+
const e = Date.now() > this._pingTimeoutTime;
|
|
967
|
+
return e && (this._pingTimeoutTime = 0, x(() => {
|
|
968
|
+
this._onClose("ping timeout");
|
|
969
|
+
}, this.setTimeoutFn)), e;
|
|
970
|
+
}
|
|
971
|
+
/**
|
|
972
|
+
* Sends a message.
|
|
973
|
+
*
|
|
974
|
+
* @param {String} msg - message.
|
|
975
|
+
* @param {Object} options.
|
|
976
|
+
* @param {Function} fn - callback function.
|
|
977
|
+
* @return {Socket} for chaining.
|
|
978
|
+
*/
|
|
979
|
+
write(e, t, s) {
|
|
980
|
+
return this._sendPacket("message", e, t, s), this;
|
|
981
|
+
}
|
|
982
|
+
/**
|
|
983
|
+
* Sends a message. Alias of {@link Socket#write}.
|
|
984
|
+
*
|
|
985
|
+
* @param {String} msg - message.
|
|
986
|
+
* @param {Object} options.
|
|
987
|
+
* @param {Function} fn - callback function.
|
|
988
|
+
* @return {Socket} for chaining.
|
|
989
|
+
*/
|
|
990
|
+
send(e, t, s) {
|
|
991
|
+
return this._sendPacket("message", e, t, s), this;
|
|
992
|
+
}
|
|
993
|
+
/**
|
|
994
|
+
* Sends a packet.
|
|
995
|
+
*
|
|
996
|
+
* @param {String} type: packet type.
|
|
997
|
+
* @param {String} data.
|
|
998
|
+
* @param {Object} options.
|
|
999
|
+
* @param {Function} fn - callback function.
|
|
1000
|
+
* @private
|
|
1001
|
+
*/
|
|
1002
|
+
_sendPacket(e, t, s, i) {
|
|
1003
|
+
if (typeof t == "function" && (i = t, t = void 0), typeof s == "function" && (i = s, s = null), this.readyState === "closing" || this.readyState === "closed")
|
|
1004
|
+
return;
|
|
1005
|
+
s = s || {}, s.compress = s.compress !== !1;
|
|
1006
|
+
const r = {
|
|
1007
|
+
type: e,
|
|
1008
|
+
data: t,
|
|
1009
|
+
options: s
|
|
1010
|
+
};
|
|
1011
|
+
this.emitReserved("packetCreate", r), this.writeBuffer.push(r), i && this.once("flush", i), this.flush();
|
|
1012
|
+
}
|
|
1013
|
+
/**
|
|
1014
|
+
* Closes the connection.
|
|
1015
|
+
*/
|
|
1016
|
+
close() {
|
|
1017
|
+
const e = () => {
|
|
1018
|
+
this._onClose("forced close"), this.transport.close();
|
|
1019
|
+
}, t = () => {
|
|
1020
|
+
this.off("upgrade", t), this.off("upgradeError", t), e();
|
|
1021
|
+
}, s = () => {
|
|
1022
|
+
this.once("upgrade", t), this.once("upgradeError", t);
|
|
1023
|
+
};
|
|
1024
|
+
return (this.readyState === "opening" || this.readyState === "open") && (this.readyState = "closing", this.writeBuffer.length ? this.once("drain", () => {
|
|
1025
|
+
this.upgrading ? s() : e();
|
|
1026
|
+
}) : this.upgrading ? s() : e()), this;
|
|
1027
|
+
}
|
|
1028
|
+
/**
|
|
1029
|
+
* Called upon transport error
|
|
1030
|
+
*
|
|
1031
|
+
* @private
|
|
1032
|
+
*/
|
|
1033
|
+
_onError(e) {
|
|
1034
|
+
if (_.priorWebsocketSuccess = !1, this.opts.tryAllTransports && this.transports.length > 1 && this.readyState === "opening")
|
|
1035
|
+
return this.transports.shift(), this._open();
|
|
1036
|
+
this.emitReserved("error", e), this._onClose("transport error", e);
|
|
1037
|
+
}
|
|
1038
|
+
/**
|
|
1039
|
+
* Called upon transport close.
|
|
1040
|
+
*
|
|
1041
|
+
* @private
|
|
1042
|
+
*/
|
|
1043
|
+
_onClose(e, t) {
|
|
1044
|
+
if (this.readyState === "opening" || this.readyState === "open" || this.readyState === "closing") {
|
|
1045
|
+
if (this.clearTimeoutFn(this._pingTimeoutTimer), this.transport.removeAllListeners("close"), this.transport.close(), this.transport.removeAllListeners(), D && (this._beforeunloadEventListener && removeEventListener("beforeunload", this._beforeunloadEventListener, !1), this._offlineEventListener)) {
|
|
1046
|
+
const s = R.indexOf(this._offlineEventListener);
|
|
1047
|
+
s !== -1 && R.splice(s, 1);
|
|
1048
|
+
}
|
|
1049
|
+
this.readyState = "closed", this.id = null, this.emitReserved("close", e, t), this.writeBuffer = [], this._prevBufferLen = 0;
|
|
1050
|
+
}
|
|
1051
|
+
}
|
|
1052
|
+
}
|
|
1053
|
+
_.protocol = ne;
|
|
1054
|
+
class Ke extends _ {
|
|
1055
|
+
constructor() {
|
|
1056
|
+
super(...arguments), this._upgrades = [];
|
|
1057
|
+
}
|
|
1058
|
+
onOpen() {
|
|
1059
|
+
if (super.onOpen(), this.readyState === "open" && this.opts.upgrade)
|
|
1060
|
+
for (let e = 0; e < this._upgrades.length; e++)
|
|
1061
|
+
this._probe(this._upgrades[e]);
|
|
1062
|
+
}
|
|
1063
|
+
/**
|
|
1064
|
+
* Probes a transport.
|
|
1065
|
+
*
|
|
1066
|
+
* @param {String} name - transport name
|
|
1067
|
+
* @private
|
|
1068
|
+
*/
|
|
1069
|
+
_probe(e) {
|
|
1070
|
+
let t = this.createTransport(e), s = !1;
|
|
1071
|
+
_.priorWebsocketSuccess = !1;
|
|
1072
|
+
const i = () => {
|
|
1073
|
+
s || (t.send([{ type: "ping", data: "probe" }]), t.once("packet", (g) => {
|
|
1074
|
+
if (!s)
|
|
1075
|
+
if (g.type === "pong" && g.data === "probe") {
|
|
1076
|
+
if (this.upgrading = !0, this.emitReserved("upgrading", t), !t)
|
|
1077
|
+
return;
|
|
1078
|
+
_.priorWebsocketSuccess = t.name === "websocket", this.transport.pause(() => {
|
|
1079
|
+
s || this.readyState !== "closed" && (f(), this.setTransport(t), t.send([{ type: "upgrade" }]), this.emitReserved("upgrade", t), t = null, this.upgrading = !1, this.flush());
|
|
1080
|
+
});
|
|
1081
|
+
} else {
|
|
1082
|
+
const w = new Error("probe error");
|
|
1083
|
+
w.transport = t.name, this.emitReserved("upgradeError", w);
|
|
1084
|
+
}
|
|
1085
|
+
}));
|
|
1086
|
+
};
|
|
1087
|
+
function r() {
|
|
1088
|
+
s || (s = !0, f(), t.close(), t = null);
|
|
1089
|
+
}
|
|
1090
|
+
const o = (g) => {
|
|
1091
|
+
const w = new Error("probe error: " + g);
|
|
1092
|
+
w.transport = t.name, r(), this.emitReserved("upgradeError", w);
|
|
1093
|
+
};
|
|
1094
|
+
function a() {
|
|
1095
|
+
o("transport closed");
|
|
1096
|
+
}
|
|
1097
|
+
function h() {
|
|
1098
|
+
o("socket closed");
|
|
1099
|
+
}
|
|
1100
|
+
function m(g) {
|
|
1101
|
+
t && g.name !== t.name && r();
|
|
1102
|
+
}
|
|
1103
|
+
const f = () => {
|
|
1104
|
+
t.removeListener("open", i), t.removeListener("error", o), t.removeListener("close", a), this.off("close", h), this.off("upgrading", m);
|
|
1105
|
+
};
|
|
1106
|
+
t.once("open", i), t.once("error", o), t.once("close", a), this.once("close", h), this.once("upgrading", m), this._upgrades.indexOf("webtransport") !== -1 && e !== "webtransport" ? this.setTimeoutFn(() => {
|
|
1107
|
+
s || t.open();
|
|
1108
|
+
}, 200) : t.open();
|
|
1109
|
+
}
|
|
1110
|
+
onHandshake(e) {
|
|
1111
|
+
this._upgrades = this._filterUpgrades(e.upgrades), super.onHandshake(e);
|
|
1112
|
+
}
|
|
1113
|
+
/**
|
|
1114
|
+
* Filters upgrades, returning only those matching client transports.
|
|
1115
|
+
*
|
|
1116
|
+
* @param {Array} upgrades - server upgrades
|
|
1117
|
+
* @private
|
|
1118
|
+
*/
|
|
1119
|
+
_filterUpgrades(e) {
|
|
1120
|
+
const t = [];
|
|
1121
|
+
for (let s = 0; s < e.length; s++)
|
|
1122
|
+
~this.transports.indexOf(e[s]) && t.push(e[s]);
|
|
1123
|
+
return t;
|
|
1124
|
+
}
|
|
1125
|
+
}
|
|
1126
|
+
let We = class extends Ke {
|
|
1127
|
+
constructor(e, t = {}) {
|
|
1128
|
+
const s = typeof e == "object" ? e : t;
|
|
1129
|
+
(!s.transports || s.transports && typeof s.transports[0] == "string") && (s.transports = (s.transports || ["polling", "websocket", "webtransport"]).map((i) => Ue[i]).filter((i) => !!i)), super(e, s);
|
|
1130
|
+
}
|
|
1131
|
+
};
|
|
1132
|
+
function Ye(n, e = "", t) {
|
|
1133
|
+
let s = n;
|
|
1134
|
+
t = t || typeof location < "u" && location, n == null && (n = t.protocol + "//" + t.host), typeof n == "string" && (n.charAt(0) === "/" && (n.charAt(1) === "/" ? n = t.protocol + n : n = t.host + n), /^(https?|wss?):\/\//.test(n) || (typeof t < "u" ? n = t.protocol + "//" + n : n = "https://" + n), s = q(n)), s.port || (/^(http|ws)$/.test(s.protocol) ? s.port = "80" : /^(http|ws)s$/.test(s.protocol) && (s.port = "443")), s.path = s.path || "/";
|
|
1135
|
+
const r = s.host.indexOf(":") !== -1 ? "[" + s.host + "]" : s.host;
|
|
1136
|
+
return s.id = s.protocol + "://" + r + ":" + s.port + e, s.href = s.protocol + "://" + r + (t && t.port === s.port ? "" : ":" + s.port), s;
|
|
1137
|
+
}
|
|
1138
|
+
const $e = typeof ArrayBuffer == "function", ze = (n) => typeof ArrayBuffer.isView == "function" ? ArrayBuffer.isView(n) : n.buffer instanceof ArrayBuffer, he = Object.prototype.toString, Je = typeof Blob == "function" || typeof Blob < "u" && he.call(Blob) === "[object BlobConstructor]", Xe = typeof File == "function" || typeof File < "u" && he.call(File) === "[object FileConstructor]";
|
|
1139
|
+
function W(n) {
|
|
1140
|
+
return $e && (n instanceof ArrayBuffer || ze(n)) || Je && n instanceof Blob || Xe && n instanceof File;
|
|
1141
|
+
}
|
|
1142
|
+
function O(n, e) {
|
|
1143
|
+
if (!n || typeof n != "object")
|
|
1144
|
+
return !1;
|
|
1145
|
+
if (Array.isArray(n)) {
|
|
1146
|
+
for (let t = 0, s = n.length; t < s; t++)
|
|
1147
|
+
if (O(n[t]))
|
|
1148
|
+
return !0;
|
|
1149
|
+
return !1;
|
|
1150
|
+
}
|
|
1151
|
+
if (W(n))
|
|
1152
|
+
return !0;
|
|
1153
|
+
if (n.toJSON && typeof n.toJSON == "function" && arguments.length === 1)
|
|
1154
|
+
return O(n.toJSON(), !0);
|
|
1155
|
+
for (const t in n)
|
|
1156
|
+
if (Object.prototype.hasOwnProperty.call(n, t) && O(n[t]))
|
|
1157
|
+
return !0;
|
|
1158
|
+
return !1;
|
|
1159
|
+
}
|
|
1160
|
+
function Qe(n) {
|
|
1161
|
+
const e = [], t = n.data, s = n;
|
|
1162
|
+
return s.data = I(t, e), s.attachments = e.length, { packet: s, buffers: e };
|
|
1163
|
+
}
|
|
1164
|
+
function I(n, e) {
|
|
1165
|
+
if (!n)
|
|
1166
|
+
return n;
|
|
1167
|
+
if (W(n)) {
|
|
1168
|
+
const t = { _placeholder: !0, num: e.length };
|
|
1169
|
+
return e.push(n), t;
|
|
1170
|
+
} else if (Array.isArray(n)) {
|
|
1171
|
+
const t = new Array(n.length);
|
|
1172
|
+
for (let s = 0; s < n.length; s++)
|
|
1173
|
+
t[s] = I(n[s], e);
|
|
1174
|
+
return t;
|
|
1175
|
+
} else if (typeof n == "object" && !(n instanceof Date)) {
|
|
1176
|
+
const t = {};
|
|
1177
|
+
for (const s in n)
|
|
1178
|
+
Object.prototype.hasOwnProperty.call(n, s) && (t[s] = I(n[s], e));
|
|
1179
|
+
return t;
|
|
1180
|
+
}
|
|
1181
|
+
return n;
|
|
1182
|
+
}
|
|
1183
|
+
function je(n, e) {
|
|
1184
|
+
return n.data = U(n.data, e), delete n.attachments, n;
|
|
1185
|
+
}
|
|
1186
|
+
function U(n, e) {
|
|
1187
|
+
if (!n)
|
|
1188
|
+
return n;
|
|
1189
|
+
if (n && n._placeholder === !0) {
|
|
1190
|
+
if (typeof n.num == "number" && n.num >= 0 && n.num < e.length)
|
|
1191
|
+
return e[n.num];
|
|
1192
|
+
throw new Error("illegal attachments");
|
|
1193
|
+
} else if (Array.isArray(n))
|
|
1194
|
+
for (let t = 0; t < n.length; t++)
|
|
1195
|
+
n[t] = U(n[t], e);
|
|
1196
|
+
else if (typeof n == "object")
|
|
1197
|
+
for (const t in n)
|
|
1198
|
+
Object.prototype.hasOwnProperty.call(n, t) && (n[t] = U(n[t], e));
|
|
1199
|
+
return n;
|
|
1200
|
+
}
|
|
1201
|
+
const Ge = [
|
|
1202
|
+
"connect",
|
|
1203
|
+
// used on the client side
|
|
1204
|
+
"connect_error",
|
|
1205
|
+
// used on the client side
|
|
1206
|
+
"disconnect",
|
|
1207
|
+
// used on both sides
|
|
1208
|
+
"disconnecting",
|
|
1209
|
+
// used on the server side
|
|
1210
|
+
"newListener",
|
|
1211
|
+
// used by the Node.js EventEmitter
|
|
1212
|
+
"removeListener"
|
|
1213
|
+
// used by the Node.js EventEmitter
|
|
1214
|
+
];
|
|
1215
|
+
var c;
|
|
1216
|
+
(function(n) {
|
|
1217
|
+
n[n.CONNECT = 0] = "CONNECT", n[n.DISCONNECT = 1] = "DISCONNECT", n[n.EVENT = 2] = "EVENT", n[n.ACK = 3] = "ACK", n[n.CONNECT_ERROR = 4] = "CONNECT_ERROR", n[n.BINARY_EVENT = 5] = "BINARY_EVENT", n[n.BINARY_ACK = 6] = "BINARY_ACK";
|
|
1218
|
+
})(c || (c = {}));
|
|
1219
|
+
class Ze {
|
|
1220
|
+
/**
|
|
1221
|
+
* Encoder constructor
|
|
1222
|
+
*
|
|
1223
|
+
* @param {function} replacer - custom replacer to pass down to JSON.parse
|
|
1224
|
+
*/
|
|
1225
|
+
constructor(e) {
|
|
1226
|
+
this.replacer = e;
|
|
1227
|
+
}
|
|
1228
|
+
/**
|
|
1229
|
+
* Encode a packet as a single string if non-binary, or as a
|
|
1230
|
+
* buffer sequence, depending on packet type.
|
|
1231
|
+
*
|
|
1232
|
+
* @param {Object} obj - packet object
|
|
1233
|
+
*/
|
|
1234
|
+
encode(e) {
|
|
1235
|
+
return (e.type === c.EVENT || e.type === c.ACK) && O(e) ? this.encodeAsBinary({
|
|
1236
|
+
type: e.type === c.EVENT ? c.BINARY_EVENT : c.BINARY_ACK,
|
|
1237
|
+
nsp: e.nsp,
|
|
1238
|
+
data: e.data,
|
|
1239
|
+
id: e.id
|
|
1240
|
+
}) : [this.encodeAsString(e)];
|
|
1241
|
+
}
|
|
1242
|
+
/**
|
|
1243
|
+
* Encode packet as string.
|
|
1244
|
+
*/
|
|
1245
|
+
encodeAsString(e) {
|
|
1246
|
+
let t = "" + e.type;
|
|
1247
|
+
return (e.type === c.BINARY_EVENT || e.type === c.BINARY_ACK) && (t += e.attachments + "-"), e.nsp && e.nsp !== "/" && (t += e.nsp + ","), e.id != null && (t += e.id), e.data != null && (t += JSON.stringify(e.data, this.replacer)), t;
|
|
1248
|
+
}
|
|
1249
|
+
/**
|
|
1250
|
+
* Encode packet as 'buffer sequence' by removing blobs, and
|
|
1251
|
+
* deconstructing packet into object with placeholders and
|
|
1252
|
+
* a list of buffers.
|
|
1253
|
+
*/
|
|
1254
|
+
encodeAsBinary(e) {
|
|
1255
|
+
const t = Qe(e), s = this.encodeAsString(t.packet), i = t.buffers;
|
|
1256
|
+
return i.unshift(s), i;
|
|
1257
|
+
}
|
|
1258
|
+
}
|
|
1259
|
+
class Y extends u {
|
|
1260
|
+
/**
|
|
1261
|
+
* Decoder constructor
|
|
1262
|
+
*
|
|
1263
|
+
* @param {function} reviver - custom reviver to pass down to JSON.stringify
|
|
1264
|
+
*/
|
|
1265
|
+
constructor(e) {
|
|
1266
|
+
super(), this.reviver = e;
|
|
1267
|
+
}
|
|
1268
|
+
/**
|
|
1269
|
+
* Decodes an encoded packet string into packet JSON.
|
|
1270
|
+
*
|
|
1271
|
+
* @param {String} obj - encoded packet
|
|
1272
|
+
*/
|
|
1273
|
+
add(e) {
|
|
1274
|
+
let t;
|
|
1275
|
+
if (typeof e == "string") {
|
|
1276
|
+
if (this.reconstructor)
|
|
1277
|
+
throw new Error("got plaintext data when reconstructing a packet");
|
|
1278
|
+
t = this.decodeString(e);
|
|
1279
|
+
const s = t.type === c.BINARY_EVENT;
|
|
1280
|
+
s || t.type === c.BINARY_ACK ? (t.type = s ? c.EVENT : c.ACK, this.reconstructor = new et(t), t.attachments === 0 && super.emitReserved("decoded", t)) : super.emitReserved("decoded", t);
|
|
1281
|
+
} else if (W(e) || e.base64)
|
|
1282
|
+
if (this.reconstructor)
|
|
1283
|
+
t = this.reconstructor.takeBinaryData(e), t && (this.reconstructor = null, super.emitReserved("decoded", t));
|
|
1284
|
+
else
|
|
1285
|
+
throw new Error("got binary data when not reconstructing a packet");
|
|
1286
|
+
else
|
|
1287
|
+
throw new Error("Unknown type: " + e);
|
|
1288
|
+
}
|
|
1289
|
+
/**
|
|
1290
|
+
* Decode a packet String (JSON data)
|
|
1291
|
+
*
|
|
1292
|
+
* @param {String} str
|
|
1293
|
+
* @return {Object} packet
|
|
1294
|
+
*/
|
|
1295
|
+
decodeString(e) {
|
|
1296
|
+
let t = 0;
|
|
1297
|
+
const s = {
|
|
1298
|
+
type: Number(e.charAt(0))
|
|
1299
|
+
};
|
|
1300
|
+
if (c[s.type] === void 0)
|
|
1301
|
+
throw new Error("unknown packet type " + s.type);
|
|
1302
|
+
if (s.type === c.BINARY_EVENT || s.type === c.BINARY_ACK) {
|
|
1303
|
+
const r = t + 1;
|
|
1304
|
+
for (; e.charAt(++t) !== "-" && t != e.length; )
|
|
1305
|
+
;
|
|
1306
|
+
const o = e.substring(r, t);
|
|
1307
|
+
if (o != Number(o) || e.charAt(t) !== "-")
|
|
1308
|
+
throw new Error("Illegal attachments");
|
|
1309
|
+
s.attachments = Number(o);
|
|
1310
|
+
}
|
|
1311
|
+
if (e.charAt(t + 1) === "/") {
|
|
1312
|
+
const r = t + 1;
|
|
1313
|
+
for (; ++t && !(e.charAt(t) === "," || t === e.length); )
|
|
1314
|
+
;
|
|
1315
|
+
s.nsp = e.substring(r, t);
|
|
1316
|
+
} else
|
|
1317
|
+
s.nsp = "/";
|
|
1318
|
+
const i = e.charAt(t + 1);
|
|
1319
|
+
if (i !== "" && Number(i) == i) {
|
|
1320
|
+
const r = t + 1;
|
|
1321
|
+
for (; ++t; ) {
|
|
1322
|
+
const o = e.charAt(t);
|
|
1323
|
+
if (o == null || Number(o) != o) {
|
|
1324
|
+
--t;
|
|
1325
|
+
break;
|
|
1326
|
+
}
|
|
1327
|
+
if (t === e.length)
|
|
1328
|
+
break;
|
|
1329
|
+
}
|
|
1330
|
+
s.id = Number(e.substring(r, t + 1));
|
|
1331
|
+
}
|
|
1332
|
+
if (e.charAt(++t)) {
|
|
1333
|
+
const r = this.tryParse(e.substr(t));
|
|
1334
|
+
if (Y.isPayloadValid(s.type, r))
|
|
1335
|
+
s.data = r;
|
|
1336
|
+
else
|
|
1337
|
+
throw new Error("invalid payload");
|
|
1338
|
+
}
|
|
1339
|
+
return s;
|
|
1340
|
+
}
|
|
1341
|
+
tryParse(e) {
|
|
1342
|
+
try {
|
|
1343
|
+
return JSON.parse(e, this.reviver);
|
|
1344
|
+
} catch {
|
|
1345
|
+
return !1;
|
|
1346
|
+
}
|
|
1347
|
+
}
|
|
1348
|
+
static isPayloadValid(e, t) {
|
|
1349
|
+
switch (e) {
|
|
1350
|
+
case c.CONNECT:
|
|
1351
|
+
return j(t);
|
|
1352
|
+
case c.DISCONNECT:
|
|
1353
|
+
return t === void 0;
|
|
1354
|
+
case c.CONNECT_ERROR:
|
|
1355
|
+
return typeof t == "string" || j(t);
|
|
1356
|
+
case c.EVENT:
|
|
1357
|
+
case c.BINARY_EVENT:
|
|
1358
|
+
return Array.isArray(t) && (typeof t[0] == "number" || typeof t[0] == "string" && Ge.indexOf(t[0]) === -1);
|
|
1359
|
+
case c.ACK:
|
|
1360
|
+
case c.BINARY_ACK:
|
|
1361
|
+
return Array.isArray(t);
|
|
1362
|
+
}
|
|
1363
|
+
}
|
|
1364
|
+
/**
|
|
1365
|
+
* Deallocates a parser's resources
|
|
1366
|
+
*/
|
|
1367
|
+
destroy() {
|
|
1368
|
+
this.reconstructor && (this.reconstructor.finishedReconstruction(), this.reconstructor = null);
|
|
1369
|
+
}
|
|
1370
|
+
}
|
|
1371
|
+
class et {
|
|
1372
|
+
constructor(e) {
|
|
1373
|
+
this.packet = e, this.buffers = [], this.reconPack = e;
|
|
1374
|
+
}
|
|
1375
|
+
/**
|
|
1376
|
+
* Method to be called when binary data received from connection
|
|
1377
|
+
* after a BINARY_EVENT packet.
|
|
1378
|
+
*
|
|
1379
|
+
* @param {Buffer | ArrayBuffer} binData - the raw binary data received
|
|
1380
|
+
* @return {null | Object} returns null if more binary data is expected or
|
|
1381
|
+
* a reconstructed packet object if all buffers have been received.
|
|
1382
|
+
*/
|
|
1383
|
+
takeBinaryData(e) {
|
|
1384
|
+
if (this.buffers.push(e), this.buffers.length === this.reconPack.attachments) {
|
|
1385
|
+
const t = je(this.reconPack, this.buffers);
|
|
1386
|
+
return this.finishedReconstruction(), t;
|
|
1387
|
+
}
|
|
1388
|
+
return null;
|
|
1389
|
+
}
|
|
1390
|
+
/**
|
|
1391
|
+
* Cleans up binary packet reconstruction variables.
|
|
1392
|
+
*/
|
|
1393
|
+
finishedReconstruction() {
|
|
1394
|
+
this.reconPack = null, this.buffers = [];
|
|
1395
|
+
}
|
|
1396
|
+
}
|
|
1397
|
+
function j(n) {
|
|
1398
|
+
return Object.prototype.toString.call(n) === "[object Object]";
|
|
1399
|
+
}
|
|
1400
|
+
const tt = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
|
|
1401
|
+
__proto__: null,
|
|
1402
|
+
Decoder: Y,
|
|
1403
|
+
Encoder: Ze,
|
|
1404
|
+
get PacketType() {
|
|
1405
|
+
return c;
|
|
1406
|
+
}
|
|
1407
|
+
}, Symbol.toStringTag, { value: "Module" }));
|
|
1408
|
+
function p(n, e, t) {
|
|
1409
|
+
return n.on(e, t), function() {
|
|
1410
|
+
n.off(e, t);
|
|
1411
|
+
};
|
|
1412
|
+
}
|
|
1413
|
+
const st = Object.freeze({
|
|
1414
|
+
connect: 1,
|
|
1415
|
+
connect_error: 1,
|
|
1416
|
+
disconnect: 1,
|
|
1417
|
+
disconnecting: 1,
|
|
1418
|
+
// EventEmitter reserved events: https://nodejs.org/api/events.html#events_event_newlistener
|
|
1419
|
+
newListener: 1,
|
|
1420
|
+
removeListener: 1
|
|
1421
|
+
});
|
|
1422
|
+
class ue extends u {
|
|
1423
|
+
/**
|
|
1424
|
+
* `Socket` constructor.
|
|
1425
|
+
*/
|
|
1426
|
+
constructor(e, t, s) {
|
|
1427
|
+
super(), this.connected = !1, this.recovered = !1, this.receiveBuffer = [], this.sendBuffer = [], this._queue = [], this._queueSeq = 0, this.ids = 0, this.acks = {}, this.flags = {}, this.io = e, this.nsp = t, s && s.auth && (this.auth = s.auth), this._opts = Object.assign({}, s), this.io._autoConnect && this.open();
|
|
1428
|
+
}
|
|
1429
|
+
/**
|
|
1430
|
+
* Whether the socket is currently disconnected
|
|
1431
|
+
*
|
|
1432
|
+
* @example
|
|
1433
|
+
* const socket = io();
|
|
1434
|
+
*
|
|
1435
|
+
* socket.on("connect", () => {
|
|
1436
|
+
* console.log(socket.disconnected); // false
|
|
1437
|
+
* });
|
|
1438
|
+
*
|
|
1439
|
+
* socket.on("disconnect", () => {
|
|
1440
|
+
* console.log(socket.disconnected); // true
|
|
1441
|
+
* });
|
|
1442
|
+
*/
|
|
1443
|
+
get disconnected() {
|
|
1444
|
+
return !this.connected;
|
|
1445
|
+
}
|
|
1446
|
+
/**
|
|
1447
|
+
* Subscribe to open, close and packet events
|
|
1448
|
+
*
|
|
1449
|
+
* @private
|
|
1450
|
+
*/
|
|
1451
|
+
subEvents() {
|
|
1452
|
+
if (this.subs)
|
|
1453
|
+
return;
|
|
1454
|
+
const e = this.io;
|
|
1455
|
+
this.subs = [
|
|
1456
|
+
p(e, "open", this.onopen.bind(this)),
|
|
1457
|
+
p(e, "packet", this.onpacket.bind(this)),
|
|
1458
|
+
p(e, "error", this.onerror.bind(this)),
|
|
1459
|
+
p(e, "close", this.onclose.bind(this))
|
|
1460
|
+
];
|
|
1461
|
+
}
|
|
1462
|
+
/**
|
|
1463
|
+
* Whether the Socket will try to reconnect when its Manager connects or reconnects.
|
|
1464
|
+
*
|
|
1465
|
+
* @example
|
|
1466
|
+
* const socket = io();
|
|
1467
|
+
*
|
|
1468
|
+
* console.log(socket.active); // true
|
|
1469
|
+
*
|
|
1470
|
+
* socket.on("disconnect", (reason) => {
|
|
1471
|
+
* if (reason === "io server disconnect") {
|
|
1472
|
+
* // the disconnection was initiated by the server, you need to manually reconnect
|
|
1473
|
+
* console.log(socket.active); // false
|
|
1474
|
+
* }
|
|
1475
|
+
* // else the socket will automatically try to reconnect
|
|
1476
|
+
* console.log(socket.active); // true
|
|
1477
|
+
* });
|
|
1478
|
+
*/
|
|
1479
|
+
get active() {
|
|
1480
|
+
return !!this.subs;
|
|
1481
|
+
}
|
|
1482
|
+
/**
|
|
1483
|
+
* "Opens" the socket.
|
|
1484
|
+
*
|
|
1485
|
+
* @example
|
|
1486
|
+
* const socket = io({
|
|
1487
|
+
* autoConnect: false
|
|
1488
|
+
* });
|
|
1489
|
+
*
|
|
1490
|
+
* socket.connect();
|
|
1491
|
+
*/
|
|
1492
|
+
connect() {
|
|
1493
|
+
return this.connected ? this : (this.subEvents(), this.io._reconnecting || this.io.open(), this.io._readyState === "open" && this.onopen(), this);
|
|
1494
|
+
}
|
|
1495
|
+
/**
|
|
1496
|
+
* Alias for {@link connect()}.
|
|
1497
|
+
*/
|
|
1498
|
+
open() {
|
|
1499
|
+
return this.connect();
|
|
1500
|
+
}
|
|
1501
|
+
/**
|
|
1502
|
+
* Sends a `message` event.
|
|
1503
|
+
*
|
|
1504
|
+
* This method mimics the WebSocket.send() method.
|
|
1505
|
+
*
|
|
1506
|
+
* @see https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/send
|
|
1507
|
+
*
|
|
1508
|
+
* @example
|
|
1509
|
+
* socket.send("hello");
|
|
1510
|
+
*
|
|
1511
|
+
* // this is equivalent to
|
|
1512
|
+
* socket.emit("message", "hello");
|
|
1513
|
+
*
|
|
1514
|
+
* @return self
|
|
1515
|
+
*/
|
|
1516
|
+
send(...e) {
|
|
1517
|
+
return e.unshift("message"), this.emit.apply(this, e), this;
|
|
1518
|
+
}
|
|
1519
|
+
/**
|
|
1520
|
+
* Override `emit`.
|
|
1521
|
+
* If the event is in `events`, it's emitted normally.
|
|
1522
|
+
*
|
|
1523
|
+
* @example
|
|
1524
|
+
* socket.emit("hello", "world");
|
|
1525
|
+
*
|
|
1526
|
+
* // all serializable datastructures are supported (no need to call JSON.stringify)
|
|
1527
|
+
* socket.emit("hello", 1, "2", { 3: ["4"], 5: Uint8Array.from([6]) });
|
|
1528
|
+
*
|
|
1529
|
+
* // with an acknowledgement from the server
|
|
1530
|
+
* socket.emit("hello", "world", (val) => {
|
|
1531
|
+
* // ...
|
|
1532
|
+
* });
|
|
1533
|
+
*
|
|
1534
|
+
* @return self
|
|
1535
|
+
*/
|
|
1536
|
+
emit(e, ...t) {
|
|
1537
|
+
var s, i, r;
|
|
1538
|
+
if (st.hasOwnProperty(e))
|
|
1539
|
+
throw new Error('"' + e.toString() + '" is a reserved event name');
|
|
1540
|
+
if (t.unshift(e), this._opts.retries && !this.flags.fromQueue && !this.flags.volatile)
|
|
1541
|
+
return this._addToQueue(t), this;
|
|
1542
|
+
const o = {
|
|
1543
|
+
type: c.EVENT,
|
|
1544
|
+
data: t
|
|
1545
|
+
};
|
|
1546
|
+
if (o.options = {}, o.options.compress = this.flags.compress !== !1, typeof t[t.length - 1] == "function") {
|
|
1547
|
+
const f = this.ids++, g = t.pop();
|
|
1548
|
+
this._registerAckCallback(f, g), o.id = f;
|
|
1549
|
+
}
|
|
1550
|
+
const a = (i = (s = this.io.engine) === null || s === void 0 ? void 0 : s.transport) === null || i === void 0 ? void 0 : i.writable, h = this.connected && !(!((r = this.io.engine) === null || r === void 0) && r._hasPingExpired());
|
|
1551
|
+
return this.flags.volatile && !a || (h ? (this.notifyOutgoingListeners(o), this.packet(o)) : this.sendBuffer.push(o)), this.flags = {}, this;
|
|
1552
|
+
}
|
|
1553
|
+
/**
|
|
1554
|
+
* @private
|
|
1555
|
+
*/
|
|
1556
|
+
_registerAckCallback(e, t) {
|
|
1557
|
+
var s;
|
|
1558
|
+
const i = (s = this.flags.timeout) !== null && s !== void 0 ? s : this._opts.ackTimeout;
|
|
1559
|
+
if (i === void 0) {
|
|
1560
|
+
this.acks[e] = t;
|
|
1561
|
+
return;
|
|
1562
|
+
}
|
|
1563
|
+
const r = this.io.setTimeoutFn(() => {
|
|
1564
|
+
delete this.acks[e];
|
|
1565
|
+
for (let a = 0; a < this.sendBuffer.length; a++)
|
|
1566
|
+
this.sendBuffer[a].id === e && this.sendBuffer.splice(a, 1);
|
|
1567
|
+
t.call(this, new Error("operation has timed out"));
|
|
1568
|
+
}, i), o = (...a) => {
|
|
1569
|
+
this.io.clearTimeoutFn(r), t.apply(this, a);
|
|
1570
|
+
};
|
|
1571
|
+
o.withError = !0, this.acks[e] = o;
|
|
1572
|
+
}
|
|
1573
|
+
/**
|
|
1574
|
+
* Emits an event and waits for an acknowledgement
|
|
1575
|
+
*
|
|
1576
|
+
* @example
|
|
1577
|
+
* // without timeout
|
|
1578
|
+
* const response = await socket.emitWithAck("hello", "world");
|
|
1579
|
+
*
|
|
1580
|
+
* // with a specific timeout
|
|
1581
|
+
* try {
|
|
1582
|
+
* const response = await socket.timeout(1000).emitWithAck("hello", "world");
|
|
1583
|
+
* } catch (err) {
|
|
1584
|
+
* // the server did not acknowledge the event in the given delay
|
|
1585
|
+
* }
|
|
1586
|
+
*
|
|
1587
|
+
* @return a Promise that will be fulfilled when the server acknowledges the event
|
|
1588
|
+
*/
|
|
1589
|
+
emitWithAck(e, ...t) {
|
|
1590
|
+
return new Promise((s, i) => {
|
|
1591
|
+
const r = (o, a) => o ? i(o) : s(a);
|
|
1592
|
+
r.withError = !0, t.push(r), this.emit(e, ...t);
|
|
1593
|
+
});
|
|
1594
|
+
}
|
|
1595
|
+
/**
|
|
1596
|
+
* Add the packet to the queue.
|
|
1597
|
+
* @param args
|
|
1598
|
+
* @private
|
|
1599
|
+
*/
|
|
1600
|
+
_addToQueue(e) {
|
|
1601
|
+
let t;
|
|
1602
|
+
typeof e[e.length - 1] == "function" && (t = e.pop());
|
|
1603
|
+
const s = {
|
|
1604
|
+
id: this._queueSeq++,
|
|
1605
|
+
tryCount: 0,
|
|
1606
|
+
pending: !1,
|
|
1607
|
+
args: e,
|
|
1608
|
+
flags: Object.assign({ fromQueue: !0 }, this.flags)
|
|
1609
|
+
};
|
|
1610
|
+
e.push((i, ...r) => (this._queue[0], i !== null ? s.tryCount > this._opts.retries && (this._queue.shift(), t && t(i)) : (this._queue.shift(), t && t(null, ...r)), s.pending = !1, this._drainQueue())), this._queue.push(s), this._drainQueue();
|
|
1611
|
+
}
|
|
1612
|
+
/**
|
|
1613
|
+
* Send the first packet of the queue, and wait for an acknowledgement from the server.
|
|
1614
|
+
* @param force - whether to resend a packet that has not been acknowledged yet
|
|
1615
|
+
*
|
|
1616
|
+
* @private
|
|
1617
|
+
*/
|
|
1618
|
+
_drainQueue(e = !1) {
|
|
1619
|
+
if (!this.connected || this._queue.length === 0)
|
|
1620
|
+
return;
|
|
1621
|
+
const t = this._queue[0];
|
|
1622
|
+
t.pending && !e || (t.pending = !0, t.tryCount++, this.flags = t.flags, this.emit.apply(this, t.args));
|
|
1623
|
+
}
|
|
1624
|
+
/**
|
|
1625
|
+
* Sends a packet.
|
|
1626
|
+
*
|
|
1627
|
+
* @param packet
|
|
1628
|
+
* @private
|
|
1629
|
+
*/
|
|
1630
|
+
packet(e) {
|
|
1631
|
+
e.nsp = this.nsp, this.io._packet(e);
|
|
1632
|
+
}
|
|
1633
|
+
/**
|
|
1634
|
+
* Called upon engine `open`.
|
|
1635
|
+
*
|
|
1636
|
+
* @private
|
|
1637
|
+
*/
|
|
1638
|
+
onopen() {
|
|
1639
|
+
typeof this.auth == "function" ? this.auth((e) => {
|
|
1640
|
+
this._sendConnectPacket(e);
|
|
1641
|
+
}) : this._sendConnectPacket(this.auth);
|
|
1642
|
+
}
|
|
1643
|
+
/**
|
|
1644
|
+
* Sends a CONNECT packet to initiate the Socket.IO session.
|
|
1645
|
+
*
|
|
1646
|
+
* @param data
|
|
1647
|
+
* @private
|
|
1648
|
+
*/
|
|
1649
|
+
_sendConnectPacket(e) {
|
|
1650
|
+
this.packet({
|
|
1651
|
+
type: c.CONNECT,
|
|
1652
|
+
data: this._pid ? Object.assign({ pid: this._pid, offset: this._lastOffset }, e) : e
|
|
1653
|
+
});
|
|
1654
|
+
}
|
|
1655
|
+
/**
|
|
1656
|
+
* Called upon engine or manager `error`.
|
|
1657
|
+
*
|
|
1658
|
+
* @param err
|
|
1659
|
+
* @private
|
|
1660
|
+
*/
|
|
1661
|
+
onerror(e) {
|
|
1662
|
+
this.connected || this.emitReserved("connect_error", e);
|
|
1663
|
+
}
|
|
1664
|
+
/**
|
|
1665
|
+
* Called upon engine `close`.
|
|
1666
|
+
*
|
|
1667
|
+
* @param reason
|
|
1668
|
+
* @param description
|
|
1669
|
+
* @private
|
|
1670
|
+
*/
|
|
1671
|
+
onclose(e, t) {
|
|
1672
|
+
this.connected = !1, delete this.id, this.emitReserved("disconnect", e, t), this._clearAcks();
|
|
1673
|
+
}
|
|
1674
|
+
/**
|
|
1675
|
+
* Clears the acknowledgement handlers upon disconnection, since the client will never receive an acknowledgement from
|
|
1676
|
+
* the server.
|
|
1677
|
+
*
|
|
1678
|
+
* @private
|
|
1679
|
+
*/
|
|
1680
|
+
_clearAcks() {
|
|
1681
|
+
Object.keys(this.acks).forEach((e) => {
|
|
1682
|
+
if (!this.sendBuffer.some((s) => String(s.id) === e)) {
|
|
1683
|
+
const s = this.acks[e];
|
|
1684
|
+
delete this.acks[e], s.withError && s.call(this, new Error("socket has been disconnected"));
|
|
1685
|
+
}
|
|
1686
|
+
});
|
|
1687
|
+
}
|
|
1688
|
+
/**
|
|
1689
|
+
* Called with socket packet.
|
|
1690
|
+
*
|
|
1691
|
+
* @param packet
|
|
1692
|
+
* @private
|
|
1693
|
+
*/
|
|
1694
|
+
onpacket(e) {
|
|
1695
|
+
if (e.nsp === this.nsp)
|
|
1696
|
+
switch (e.type) {
|
|
1697
|
+
case c.CONNECT:
|
|
1698
|
+
e.data && e.data.sid ? this.onconnect(e.data.sid, e.data.pid) : 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/)"));
|
|
1699
|
+
break;
|
|
1700
|
+
case c.EVENT:
|
|
1701
|
+
case c.BINARY_EVENT:
|
|
1702
|
+
this.onevent(e);
|
|
1703
|
+
break;
|
|
1704
|
+
case c.ACK:
|
|
1705
|
+
case c.BINARY_ACK:
|
|
1706
|
+
this.onack(e);
|
|
1707
|
+
break;
|
|
1708
|
+
case c.DISCONNECT:
|
|
1709
|
+
this.ondisconnect();
|
|
1710
|
+
break;
|
|
1711
|
+
case c.CONNECT_ERROR:
|
|
1712
|
+
this.destroy();
|
|
1713
|
+
const s = new Error(e.data.message);
|
|
1714
|
+
s.data = e.data.data, this.emitReserved("connect_error", s);
|
|
1715
|
+
break;
|
|
1716
|
+
}
|
|
1717
|
+
}
|
|
1718
|
+
/**
|
|
1719
|
+
* Called upon a server event.
|
|
1720
|
+
*
|
|
1721
|
+
* @param packet
|
|
1722
|
+
* @private
|
|
1723
|
+
*/
|
|
1724
|
+
onevent(e) {
|
|
1725
|
+
const t = e.data || [];
|
|
1726
|
+
e.id != null && t.push(this.ack(e.id)), this.connected ? this.emitEvent(t) : this.receiveBuffer.push(Object.freeze(t));
|
|
1727
|
+
}
|
|
1728
|
+
emitEvent(e) {
|
|
1729
|
+
if (this._anyListeners && this._anyListeners.length) {
|
|
1730
|
+
const t = this._anyListeners.slice();
|
|
1731
|
+
for (const s of t)
|
|
1732
|
+
s.apply(this, e);
|
|
1733
|
+
}
|
|
1734
|
+
super.emit.apply(this, e), this._pid && e.length && typeof e[e.length - 1] == "string" && (this._lastOffset = e[e.length - 1]);
|
|
1735
|
+
}
|
|
1736
|
+
/**
|
|
1737
|
+
* Produces an ack callback to emit with an event.
|
|
1738
|
+
*
|
|
1739
|
+
* @private
|
|
1740
|
+
*/
|
|
1741
|
+
ack(e) {
|
|
1742
|
+
const t = this;
|
|
1743
|
+
let s = !1;
|
|
1744
|
+
return function(...i) {
|
|
1745
|
+
s || (s = !0, t.packet({
|
|
1746
|
+
type: c.ACK,
|
|
1747
|
+
id: e,
|
|
1748
|
+
data: i
|
|
1749
|
+
}));
|
|
1750
|
+
};
|
|
1751
|
+
}
|
|
1752
|
+
/**
|
|
1753
|
+
* Called upon a server acknowledgement.
|
|
1754
|
+
*
|
|
1755
|
+
* @param packet
|
|
1756
|
+
* @private
|
|
1757
|
+
*/
|
|
1758
|
+
onack(e) {
|
|
1759
|
+
const t = this.acks[e.id];
|
|
1760
|
+
typeof t == "function" && (delete this.acks[e.id], t.withError && e.data.unshift(null), t.apply(this, e.data));
|
|
1761
|
+
}
|
|
1762
|
+
/**
|
|
1763
|
+
* Called upon server connect.
|
|
1764
|
+
*
|
|
1765
|
+
* @private
|
|
1766
|
+
*/
|
|
1767
|
+
onconnect(e, t) {
|
|
1768
|
+
this.id = e, this.recovered = t && this._pid === t, this._pid = t, this.connected = !0, this.emitBuffered(), this._drainQueue(!0), this.emitReserved("connect");
|
|
1769
|
+
}
|
|
1770
|
+
/**
|
|
1771
|
+
* Emit buffered events (received and emitted).
|
|
1772
|
+
*
|
|
1773
|
+
* @private
|
|
1774
|
+
*/
|
|
1775
|
+
emitBuffered() {
|
|
1776
|
+
this.receiveBuffer.forEach((e) => this.emitEvent(e)), this.receiveBuffer = [], this.sendBuffer.forEach((e) => {
|
|
1777
|
+
this.notifyOutgoingListeners(e), this.packet(e);
|
|
1778
|
+
}), this.sendBuffer = [];
|
|
1779
|
+
}
|
|
1780
|
+
/**
|
|
1781
|
+
* Called upon server disconnect.
|
|
1782
|
+
*
|
|
1783
|
+
* @private
|
|
1784
|
+
*/
|
|
1785
|
+
ondisconnect() {
|
|
1786
|
+
this.destroy(), this.onclose("io server disconnect");
|
|
1787
|
+
}
|
|
1788
|
+
/**
|
|
1789
|
+
* Called upon forced client/server side disconnections,
|
|
1790
|
+
* this method ensures the manager stops tracking us and
|
|
1791
|
+
* that reconnections don't get triggered for this.
|
|
1792
|
+
*
|
|
1793
|
+
* @private
|
|
1794
|
+
*/
|
|
1795
|
+
destroy() {
|
|
1796
|
+
this.subs && (this.subs.forEach((e) => e()), this.subs = void 0), this.io._destroy(this);
|
|
1797
|
+
}
|
|
1798
|
+
/**
|
|
1799
|
+
* Disconnects the socket manually. In that case, the socket will not try to reconnect.
|
|
1800
|
+
*
|
|
1801
|
+
* If this is the last active Socket instance of the {@link Manager}, the low-level connection will be closed.
|
|
1802
|
+
*
|
|
1803
|
+
* @example
|
|
1804
|
+
* const socket = io();
|
|
1805
|
+
*
|
|
1806
|
+
* socket.on("disconnect", (reason) => {
|
|
1807
|
+
* // console.log(reason); prints "io client disconnect"
|
|
1808
|
+
* });
|
|
1809
|
+
*
|
|
1810
|
+
* socket.disconnect();
|
|
1811
|
+
*
|
|
1812
|
+
* @return self
|
|
1813
|
+
*/
|
|
1814
|
+
disconnect() {
|
|
1815
|
+
return this.connected && this.packet({ type: c.DISCONNECT }), this.destroy(), this.connected && this.onclose("io client disconnect"), this;
|
|
1816
|
+
}
|
|
1817
|
+
/**
|
|
1818
|
+
* Alias for {@link disconnect()}.
|
|
1819
|
+
*
|
|
1820
|
+
* @return self
|
|
1821
|
+
*/
|
|
1822
|
+
close() {
|
|
1823
|
+
return this.disconnect();
|
|
1824
|
+
}
|
|
1825
|
+
/**
|
|
1826
|
+
* Sets the compress flag.
|
|
1827
|
+
*
|
|
1828
|
+
* @example
|
|
1829
|
+
* socket.compress(false).emit("hello");
|
|
1830
|
+
*
|
|
1831
|
+
* @param compress - if `true`, compresses the sending data
|
|
1832
|
+
* @return self
|
|
1833
|
+
*/
|
|
1834
|
+
compress(e) {
|
|
1835
|
+
return this.flags.compress = e, this;
|
|
1836
|
+
}
|
|
1837
|
+
/**
|
|
1838
|
+
* Sets a modifier for a subsequent event emission that the event message will be dropped when this socket is not
|
|
1839
|
+
* ready to send messages.
|
|
1840
|
+
*
|
|
1841
|
+
* @example
|
|
1842
|
+
* socket.volatile.emit("hello"); // the server may or may not receive it
|
|
1843
|
+
*
|
|
1844
|
+
* @returns self
|
|
1845
|
+
*/
|
|
1846
|
+
get volatile() {
|
|
1847
|
+
return this.flags.volatile = !0, this;
|
|
1848
|
+
}
|
|
1849
|
+
/**
|
|
1850
|
+
* Sets a modifier for a subsequent event emission that the callback will be called with an error when the
|
|
1851
|
+
* given number of milliseconds have elapsed without an acknowledgement from the server:
|
|
1852
|
+
*
|
|
1853
|
+
* @example
|
|
1854
|
+
* socket.timeout(5000).emit("my-event", (err) => {
|
|
1855
|
+
* if (err) {
|
|
1856
|
+
* // the server did not acknowledge the event in the given delay
|
|
1857
|
+
* }
|
|
1858
|
+
* });
|
|
1859
|
+
*
|
|
1860
|
+
* @returns self
|
|
1861
|
+
*/
|
|
1862
|
+
timeout(e) {
|
|
1863
|
+
return this.flags.timeout = e, this;
|
|
1864
|
+
}
|
|
1865
|
+
/**
|
|
1866
|
+
* Adds a listener that will be fired when any event is emitted. The event name is passed as the first argument to the
|
|
1867
|
+
* callback.
|
|
1868
|
+
*
|
|
1869
|
+
* @example
|
|
1870
|
+
* socket.onAny((event, ...args) => {
|
|
1871
|
+
* console.log(`got ${event}`);
|
|
1872
|
+
* });
|
|
1873
|
+
*
|
|
1874
|
+
* @param listener
|
|
1875
|
+
*/
|
|
1876
|
+
onAny(e) {
|
|
1877
|
+
return this._anyListeners = this._anyListeners || [], this._anyListeners.push(e), this;
|
|
1878
|
+
}
|
|
1879
|
+
/**
|
|
1880
|
+
* Adds a listener that will be fired when any event is emitted. The event name is passed as the first argument to the
|
|
1881
|
+
* callback. The listener is added to the beginning of the listeners array.
|
|
1882
|
+
*
|
|
1883
|
+
* @example
|
|
1884
|
+
* socket.prependAny((event, ...args) => {
|
|
1885
|
+
* console.log(`got event ${event}`);
|
|
1886
|
+
* });
|
|
1887
|
+
*
|
|
1888
|
+
* @param listener
|
|
1889
|
+
*/
|
|
1890
|
+
prependAny(e) {
|
|
1891
|
+
return this._anyListeners = this._anyListeners || [], this._anyListeners.unshift(e), this;
|
|
1892
|
+
}
|
|
1893
|
+
/**
|
|
1894
|
+
* Removes the listener that will be fired when any event is emitted.
|
|
1895
|
+
*
|
|
1896
|
+
* @example
|
|
1897
|
+
* const catchAllListener = (event, ...args) => {
|
|
1898
|
+
* console.log(`got event ${event}`);
|
|
1899
|
+
* }
|
|
1900
|
+
*
|
|
1901
|
+
* socket.onAny(catchAllListener);
|
|
1902
|
+
*
|
|
1903
|
+
* // remove a specific listener
|
|
1904
|
+
* socket.offAny(catchAllListener);
|
|
1905
|
+
*
|
|
1906
|
+
* // or remove all listeners
|
|
1907
|
+
* socket.offAny();
|
|
1908
|
+
*
|
|
1909
|
+
* @param listener
|
|
1910
|
+
*/
|
|
1911
|
+
offAny(e) {
|
|
1912
|
+
if (!this._anyListeners)
|
|
1913
|
+
return this;
|
|
1914
|
+
if (e) {
|
|
1915
|
+
const t = this._anyListeners;
|
|
1916
|
+
for (let s = 0; s < t.length; s++)
|
|
1917
|
+
if (e === t[s])
|
|
1918
|
+
return t.splice(s, 1), this;
|
|
1919
|
+
} else
|
|
1920
|
+
this._anyListeners = [];
|
|
1921
|
+
return this;
|
|
1922
|
+
}
|
|
1923
|
+
/**
|
|
1924
|
+
* Returns an array of listeners that are listening for any event that is specified. This array can be manipulated,
|
|
1925
|
+
* e.g. to remove listeners.
|
|
1926
|
+
*/
|
|
1927
|
+
listenersAny() {
|
|
1928
|
+
return this._anyListeners || [];
|
|
1929
|
+
}
|
|
1930
|
+
/**
|
|
1931
|
+
* Adds a listener that will be fired when any event is emitted. The event name is passed as the first argument to the
|
|
1932
|
+
* callback.
|
|
1933
|
+
*
|
|
1934
|
+
* Note: acknowledgements sent to the server are not included.
|
|
1935
|
+
*
|
|
1936
|
+
* @example
|
|
1937
|
+
* socket.onAnyOutgoing((event, ...args) => {
|
|
1938
|
+
* console.log(`sent event ${event}`);
|
|
1939
|
+
* });
|
|
1940
|
+
*
|
|
1941
|
+
* @param listener
|
|
1942
|
+
*/
|
|
1943
|
+
onAnyOutgoing(e) {
|
|
1944
|
+
return this._anyOutgoingListeners = this._anyOutgoingListeners || [], this._anyOutgoingListeners.push(e), this;
|
|
1945
|
+
}
|
|
1946
|
+
/**
|
|
1947
|
+
* Adds a listener that will be fired when any event is emitted. The event name is passed as the first argument to the
|
|
1948
|
+
* callback. The listener is added to the beginning of the listeners array.
|
|
1949
|
+
*
|
|
1950
|
+
* Note: acknowledgements sent to the server are not included.
|
|
1951
|
+
*
|
|
1952
|
+
* @example
|
|
1953
|
+
* socket.prependAnyOutgoing((event, ...args) => {
|
|
1954
|
+
* console.log(`sent event ${event}`);
|
|
1955
|
+
* });
|
|
1956
|
+
*
|
|
1957
|
+
* @param listener
|
|
1958
|
+
*/
|
|
1959
|
+
prependAnyOutgoing(e) {
|
|
1960
|
+
return this._anyOutgoingListeners = this._anyOutgoingListeners || [], this._anyOutgoingListeners.unshift(e), this;
|
|
1961
|
+
}
|
|
1962
|
+
/**
|
|
1963
|
+
* Removes the listener that will be fired when any event is emitted.
|
|
1964
|
+
*
|
|
1965
|
+
* @example
|
|
1966
|
+
* const catchAllListener = (event, ...args) => {
|
|
1967
|
+
* console.log(`sent event ${event}`);
|
|
1968
|
+
* }
|
|
1969
|
+
*
|
|
1970
|
+
* socket.onAnyOutgoing(catchAllListener);
|
|
1971
|
+
*
|
|
1972
|
+
* // remove a specific listener
|
|
1973
|
+
* socket.offAnyOutgoing(catchAllListener);
|
|
1974
|
+
*
|
|
1975
|
+
* // or remove all listeners
|
|
1976
|
+
* socket.offAnyOutgoing();
|
|
1977
|
+
*
|
|
1978
|
+
* @param [listener] - the catch-all listener (optional)
|
|
1979
|
+
*/
|
|
1980
|
+
offAnyOutgoing(e) {
|
|
1981
|
+
if (!this._anyOutgoingListeners)
|
|
1982
|
+
return this;
|
|
1983
|
+
if (e) {
|
|
1984
|
+
const t = this._anyOutgoingListeners;
|
|
1985
|
+
for (let s = 0; s < t.length; s++)
|
|
1986
|
+
if (e === t[s])
|
|
1987
|
+
return t.splice(s, 1), this;
|
|
1988
|
+
} else
|
|
1989
|
+
this._anyOutgoingListeners = [];
|
|
1990
|
+
return this;
|
|
1991
|
+
}
|
|
1992
|
+
/**
|
|
1993
|
+
* Returns an array of listeners that are listening for any event that is specified. This array can be manipulated,
|
|
1994
|
+
* e.g. to remove listeners.
|
|
1995
|
+
*/
|
|
1996
|
+
listenersAnyOutgoing() {
|
|
1997
|
+
return this._anyOutgoingListeners || [];
|
|
1998
|
+
}
|
|
1999
|
+
/**
|
|
2000
|
+
* Notify the listeners for each packet sent
|
|
2001
|
+
*
|
|
2002
|
+
* @param packet
|
|
2003
|
+
*
|
|
2004
|
+
* @private
|
|
2005
|
+
*/
|
|
2006
|
+
notifyOutgoingListeners(e) {
|
|
2007
|
+
if (this._anyOutgoingListeners && this._anyOutgoingListeners.length) {
|
|
2008
|
+
const t = this._anyOutgoingListeners.slice();
|
|
2009
|
+
for (const s of t)
|
|
2010
|
+
s.apply(this, e.data);
|
|
2011
|
+
}
|
|
2012
|
+
}
|
|
2013
|
+
}
|
|
2014
|
+
function b(n) {
|
|
2015
|
+
n = n || {}, this.ms = n.min || 100, this.max = n.max || 1e4, this.factor = n.factor || 2, this.jitter = n.jitter > 0 && n.jitter <= 1 ? n.jitter : 0, this.attempts = 0;
|
|
2016
|
+
}
|
|
2017
|
+
b.prototype.duration = function() {
|
|
2018
|
+
var n = this.ms * Math.pow(this.factor, this.attempts++);
|
|
2019
|
+
if (this.jitter) {
|
|
2020
|
+
var e = Math.random(), t = Math.floor(e * this.jitter * n);
|
|
2021
|
+
n = (Math.floor(e * 10) & 1) == 0 ? n - t : n + t;
|
|
2022
|
+
}
|
|
2023
|
+
return Math.min(n, this.max) | 0;
|
|
2024
|
+
};
|
|
2025
|
+
b.prototype.reset = function() {
|
|
2026
|
+
this.attempts = 0;
|
|
2027
|
+
};
|
|
2028
|
+
b.prototype.setMin = function(n) {
|
|
2029
|
+
this.ms = n;
|
|
2030
|
+
};
|
|
2031
|
+
b.prototype.setMax = function(n) {
|
|
2032
|
+
this.max = n;
|
|
2033
|
+
};
|
|
2034
|
+
b.prototype.setJitter = function(n) {
|
|
2035
|
+
this.jitter = n;
|
|
2036
|
+
};
|
|
2037
|
+
class F extends u {
|
|
2038
|
+
constructor(e, t) {
|
|
2039
|
+
var s;
|
|
2040
|
+
super(), this.nsps = {}, this.subs = [], e && typeof e == "object" && (t = e, e = void 0), t = t || {}, t.path = t.path || "/socket.io", this.opts = t, S(this, t), this.reconnection(t.reconnection !== !1), this.reconnectionAttempts(t.reconnectionAttempts || 1 / 0), this.reconnectionDelay(t.reconnectionDelay || 1e3), this.reconnectionDelayMax(t.reconnectionDelayMax || 5e3), this.randomizationFactor((s = t.randomizationFactor) !== null && s !== void 0 ? s : 0.5), this.backoff = new b({
|
|
2041
|
+
min: this.reconnectionDelay(),
|
|
2042
|
+
max: this.reconnectionDelayMax(),
|
|
2043
|
+
jitter: this.randomizationFactor()
|
|
2044
|
+
}), this.timeout(t.timeout == null ? 2e4 : t.timeout), this._readyState = "closed", this.uri = e;
|
|
2045
|
+
const i = t.parser || tt;
|
|
2046
|
+
this.encoder = new i.Encoder(), this.decoder = new i.Decoder(), this._autoConnect = t.autoConnect !== !1, this._autoConnect && this.open();
|
|
2047
|
+
}
|
|
2048
|
+
reconnection(e) {
|
|
2049
|
+
return arguments.length ? (this._reconnection = !!e, e || (this.skipReconnect = !0), this) : this._reconnection;
|
|
2050
|
+
}
|
|
2051
|
+
reconnectionAttempts(e) {
|
|
2052
|
+
return e === void 0 ? this._reconnectionAttempts : (this._reconnectionAttempts = e, this);
|
|
2053
|
+
}
|
|
2054
|
+
reconnectionDelay(e) {
|
|
2055
|
+
var t;
|
|
2056
|
+
return e === void 0 ? this._reconnectionDelay : (this._reconnectionDelay = e, (t = this.backoff) === null || t === void 0 || t.setMin(e), this);
|
|
2057
|
+
}
|
|
2058
|
+
randomizationFactor(e) {
|
|
2059
|
+
var t;
|
|
2060
|
+
return e === void 0 ? this._randomizationFactor : (this._randomizationFactor = e, (t = this.backoff) === null || t === void 0 || t.setJitter(e), this);
|
|
2061
|
+
}
|
|
2062
|
+
reconnectionDelayMax(e) {
|
|
2063
|
+
var t;
|
|
2064
|
+
return e === void 0 ? this._reconnectionDelayMax : (this._reconnectionDelayMax = e, (t = this.backoff) === null || t === void 0 || t.setMax(e), this);
|
|
2065
|
+
}
|
|
2066
|
+
timeout(e) {
|
|
2067
|
+
return arguments.length ? (this._timeout = e, this) : this._timeout;
|
|
2068
|
+
}
|
|
2069
|
+
/**
|
|
2070
|
+
* Starts trying to reconnect if reconnection is enabled and we have not
|
|
2071
|
+
* started reconnecting yet
|
|
2072
|
+
*
|
|
2073
|
+
* @private
|
|
2074
|
+
*/
|
|
2075
|
+
maybeReconnectOnOpen() {
|
|
2076
|
+
!this._reconnecting && this._reconnection && this.backoff.attempts === 0 && this.reconnect();
|
|
2077
|
+
}
|
|
2078
|
+
/**
|
|
2079
|
+
* Sets the current transport `socket`.
|
|
2080
|
+
*
|
|
2081
|
+
* @param {Function} fn - optional, callback
|
|
2082
|
+
* @return self
|
|
2083
|
+
* @public
|
|
2084
|
+
*/
|
|
2085
|
+
open(e) {
|
|
2086
|
+
if (~this._readyState.indexOf("open"))
|
|
2087
|
+
return this;
|
|
2088
|
+
this.engine = new We(this.uri, this.opts);
|
|
2089
|
+
const t = this.engine, s = this;
|
|
2090
|
+
this._readyState = "opening", this.skipReconnect = !1;
|
|
2091
|
+
const i = p(t, "open", function() {
|
|
2092
|
+
s.onopen(), e && e();
|
|
2093
|
+
}), r = (a) => {
|
|
2094
|
+
this.cleanup(), this._readyState = "closed", this.emitReserved("error", a), e ? e(a) : this.maybeReconnectOnOpen();
|
|
2095
|
+
}, o = p(t, "error", r);
|
|
2096
|
+
if (this._timeout !== !1) {
|
|
2097
|
+
const a = this._timeout, h = this.setTimeoutFn(() => {
|
|
2098
|
+
i(), r(new Error("timeout")), t.close();
|
|
2099
|
+
}, a);
|
|
2100
|
+
this.opts.autoUnref && h.unref(), this.subs.push(() => {
|
|
2101
|
+
this.clearTimeoutFn(h);
|
|
2102
|
+
});
|
|
2103
|
+
}
|
|
2104
|
+
return this.subs.push(i), this.subs.push(o), this;
|
|
2105
|
+
}
|
|
2106
|
+
/**
|
|
2107
|
+
* Alias for open()
|
|
2108
|
+
*
|
|
2109
|
+
* @return self
|
|
2110
|
+
* @public
|
|
2111
|
+
*/
|
|
2112
|
+
connect(e) {
|
|
2113
|
+
return this.open(e);
|
|
2114
|
+
}
|
|
2115
|
+
/**
|
|
2116
|
+
* Called upon transport open.
|
|
2117
|
+
*
|
|
2118
|
+
* @private
|
|
2119
|
+
*/
|
|
2120
|
+
onopen() {
|
|
2121
|
+
this.cleanup(), this._readyState = "open", this.emitReserved("open");
|
|
2122
|
+
const e = this.engine;
|
|
2123
|
+
this.subs.push(
|
|
2124
|
+
p(e, "ping", this.onping.bind(this)),
|
|
2125
|
+
p(e, "data", this.ondata.bind(this)),
|
|
2126
|
+
p(e, "error", this.onerror.bind(this)),
|
|
2127
|
+
p(e, "close", this.onclose.bind(this)),
|
|
2128
|
+
// @ts-ignore
|
|
2129
|
+
p(this.decoder, "decoded", this.ondecoded.bind(this))
|
|
2130
|
+
);
|
|
2131
|
+
}
|
|
2132
|
+
/**
|
|
2133
|
+
* Called upon a ping.
|
|
2134
|
+
*
|
|
2135
|
+
* @private
|
|
2136
|
+
*/
|
|
2137
|
+
onping() {
|
|
2138
|
+
this.emitReserved("ping");
|
|
2139
|
+
}
|
|
2140
|
+
/**
|
|
2141
|
+
* Called with data.
|
|
2142
|
+
*
|
|
2143
|
+
* @private
|
|
2144
|
+
*/
|
|
2145
|
+
ondata(e) {
|
|
2146
|
+
try {
|
|
2147
|
+
this.decoder.add(e);
|
|
2148
|
+
} catch (t) {
|
|
2149
|
+
this.onclose("parse error", t);
|
|
2150
|
+
}
|
|
2151
|
+
}
|
|
2152
|
+
/**
|
|
2153
|
+
* Called when parser fully decodes a packet.
|
|
2154
|
+
*
|
|
2155
|
+
* @private
|
|
2156
|
+
*/
|
|
2157
|
+
ondecoded(e) {
|
|
2158
|
+
x(() => {
|
|
2159
|
+
this.emitReserved("packet", e);
|
|
2160
|
+
}, this.setTimeoutFn);
|
|
2161
|
+
}
|
|
2162
|
+
/**
|
|
2163
|
+
* Called upon socket error.
|
|
2164
|
+
*
|
|
2165
|
+
* @private
|
|
2166
|
+
*/
|
|
2167
|
+
onerror(e) {
|
|
2168
|
+
this.emitReserved("error", e);
|
|
2169
|
+
}
|
|
2170
|
+
/**
|
|
2171
|
+
* Creates a new socket for the given `nsp`.
|
|
2172
|
+
*
|
|
2173
|
+
* @return {Socket}
|
|
2174
|
+
* @public
|
|
2175
|
+
*/
|
|
2176
|
+
socket(e, t) {
|
|
2177
|
+
let s = this.nsps[e];
|
|
2178
|
+
return s ? this._autoConnect && !s.active && s.connect() : (s = new ue(this, e, t), this.nsps[e] = s), s;
|
|
2179
|
+
}
|
|
2180
|
+
/**
|
|
2181
|
+
* Called upon a socket close.
|
|
2182
|
+
*
|
|
2183
|
+
* @param socket
|
|
2184
|
+
* @private
|
|
2185
|
+
*/
|
|
2186
|
+
_destroy(e) {
|
|
2187
|
+
const t = Object.keys(this.nsps);
|
|
2188
|
+
for (const s of t)
|
|
2189
|
+
if (this.nsps[s].active)
|
|
2190
|
+
return;
|
|
2191
|
+
this._close();
|
|
2192
|
+
}
|
|
2193
|
+
/**
|
|
2194
|
+
* Writes a packet.
|
|
2195
|
+
*
|
|
2196
|
+
* @param packet
|
|
2197
|
+
* @private
|
|
2198
|
+
*/
|
|
2199
|
+
_packet(e) {
|
|
2200
|
+
const t = this.encoder.encode(e);
|
|
2201
|
+
for (let s = 0; s < t.length; s++)
|
|
2202
|
+
this.engine.write(t[s], e.options);
|
|
2203
|
+
}
|
|
2204
|
+
/**
|
|
2205
|
+
* Clean up transport subscriptions and packet buffer.
|
|
2206
|
+
*
|
|
2207
|
+
* @private
|
|
2208
|
+
*/
|
|
2209
|
+
cleanup() {
|
|
2210
|
+
this.subs.forEach((e) => e()), this.subs.length = 0, this.decoder.destroy();
|
|
2211
|
+
}
|
|
2212
|
+
/**
|
|
2213
|
+
* Close the current socket.
|
|
2214
|
+
*
|
|
2215
|
+
* @private
|
|
2216
|
+
*/
|
|
2217
|
+
_close() {
|
|
2218
|
+
this.skipReconnect = !0, this._reconnecting = !1, this.onclose("forced close");
|
|
2219
|
+
}
|
|
2220
|
+
/**
|
|
2221
|
+
* Alias for close()
|
|
2222
|
+
*
|
|
2223
|
+
* @private
|
|
2224
|
+
*/
|
|
2225
|
+
disconnect() {
|
|
2226
|
+
return this._close();
|
|
2227
|
+
}
|
|
2228
|
+
/**
|
|
2229
|
+
* Called when:
|
|
2230
|
+
*
|
|
2231
|
+
* - the low-level engine is closed
|
|
2232
|
+
* - the parser encountered a badly formatted packet
|
|
2233
|
+
* - all sockets are disconnected
|
|
2234
|
+
*
|
|
2235
|
+
* @private
|
|
2236
|
+
*/
|
|
2237
|
+
onclose(e, t) {
|
|
2238
|
+
var s;
|
|
2239
|
+
this.cleanup(), (s = this.engine) === null || s === void 0 || s.close(), this.backoff.reset(), this._readyState = "closed", this.emitReserved("close", e, t), this._reconnection && !this.skipReconnect && this.reconnect();
|
|
2240
|
+
}
|
|
2241
|
+
/**
|
|
2242
|
+
* Attempt a reconnection.
|
|
2243
|
+
*
|
|
2244
|
+
* @private
|
|
2245
|
+
*/
|
|
2246
|
+
reconnect() {
|
|
2247
|
+
if (this._reconnecting || this.skipReconnect)
|
|
2248
|
+
return this;
|
|
2249
|
+
const e = this;
|
|
2250
|
+
if (this.backoff.attempts >= this._reconnectionAttempts)
|
|
2251
|
+
this.backoff.reset(), this.emitReserved("reconnect_failed"), this._reconnecting = !1;
|
|
2252
|
+
else {
|
|
2253
|
+
const t = this.backoff.duration();
|
|
2254
|
+
this._reconnecting = !0;
|
|
2255
|
+
const s = this.setTimeoutFn(() => {
|
|
2256
|
+
e.skipReconnect || (this.emitReserved("reconnect_attempt", e.backoff.attempts), !e.skipReconnect && e.open((i) => {
|
|
2257
|
+
i ? (e._reconnecting = !1, e.reconnect(), this.emitReserved("reconnect_error", i)) : e.onreconnect();
|
|
2258
|
+
}));
|
|
2259
|
+
}, t);
|
|
2260
|
+
this.opts.autoUnref && s.unref(), this.subs.push(() => {
|
|
2261
|
+
this.clearTimeoutFn(s);
|
|
2262
|
+
});
|
|
2263
|
+
}
|
|
2264
|
+
}
|
|
2265
|
+
/**
|
|
2266
|
+
* Called upon successful reconnect.
|
|
2267
|
+
*
|
|
2268
|
+
* @private
|
|
2269
|
+
*/
|
|
2270
|
+
onreconnect() {
|
|
2271
|
+
const e = this.backoff.attempts;
|
|
2272
|
+
this._reconnecting = !1, this.backoff.reset(), this.emitReserved("reconnect", e);
|
|
2273
|
+
}
|
|
2274
|
+
}
|
|
2275
|
+
const v = {};
|
|
2276
|
+
function C(n, e) {
|
|
2277
|
+
typeof n == "object" && (e = n, n = void 0), e = e || {};
|
|
2278
|
+
const t = Ye(n, e.path || "/socket.io"), s = t.source, i = t.id, r = t.path, o = v[i] && r in v[i].nsps, a = e.forceNew || e["force new connection"] || e.multiplex === !1 || o;
|
|
2279
|
+
let h;
|
|
2280
|
+
return a ? h = new F(s, e) : (v[i] || (v[i] = new F(s, e)), h = v[i]), t.query && !e.query && (e.query = t.queryKey), h.socket(t.path, e);
|
|
2281
|
+
}
|
|
2282
|
+
Object.assign(C, {
|
|
2283
|
+
Manager: F,
|
|
2284
|
+
Socket: ue,
|
|
2285
|
+
io: C,
|
|
2286
|
+
connect: C
|
|
2287
|
+
});
|
|
2288
|
+
let V;
|
|
2289
|
+
function nt(n, e) {
|
|
2290
|
+
return V = C(n, {
|
|
2291
|
+
auth: { token: e }
|
|
2292
|
+
}), V;
|
|
2293
|
+
}
|
|
2294
|
+
function $() {
|
|
2295
|
+
return V;
|
|
2296
|
+
}
|
|
2297
|
+
function it(n) {
|
|
2298
|
+
const e = $(), t = document.createElement("div");
|
|
2299
|
+
t.innerHTML = "💬", t.style.position = "fixed", t.style.bottom = "20px", t.style.right = "20px", t.style.background = n.theme?.primary || "#22C55E", t.style.color = "#fff", t.style.padding = "15px", t.style.borderRadius = "50%", t.style.cursor = "pointer", t.style.zIndex = 9999;
|
|
2300
|
+
const s = document.createElement("div");
|
|
2301
|
+
s.style.display = "none", s.style.position = "fixed", s.style.bottom = "80px", s.style.right = "20px", s.style.width = "300px", s.style.height = "400px", s.style.background = "#fff", s.style.border = "1px solid #ddd", s.style.padding = "10px", s.style.overflowY = "auto";
|
|
2302
|
+
const i = document.createElement("div"), r = document.createElement("input"), o = document.createElement("button");
|
|
2303
|
+
r.placeholder = "Type message...", o.innerText = "Send", o.onclick = () => {
|
|
2304
|
+
r.value && (e.emit("sendMessage", { message: r.value }), r.value = "");
|
|
2305
|
+
}, e.on("receiveMessage", (a) => {
|
|
2306
|
+
const h = document.createElement("div");
|
|
2307
|
+
h.innerText = a.message, i.appendChild(h);
|
|
2308
|
+
}), s.appendChild(i), s.appendChild(r), s.appendChild(o), t.onclick = () => {
|
|
2309
|
+
s.style.display = s.style.display === "none" ? "block" : "none";
|
|
2310
|
+
}, document.body.appendChild(t), document.body.appendChild(s);
|
|
2311
|
+
}
|
|
2312
|
+
function rt(n) {
|
|
2313
|
+
nt(n.serverUrl, n.token), n.platform === "web" && n.showUI && it(n);
|
|
2314
|
+
}
|
|
2315
|
+
function ot(n) {
|
|
2316
|
+
$().emit("sendMessage", { message: n });
|
|
2317
|
+
}
|
|
2318
|
+
function at(n) {
|
|
2319
|
+
$().on("receiveMessage", n);
|
|
2320
|
+
}
|
|
2321
|
+
const ht = {
|
|
2322
|
+
init: rt,
|
|
2323
|
+
sendMessage: ot,
|
|
2324
|
+
onMessage: at
|
|
2325
|
+
};
|
|
2326
|
+
export {
|
|
2327
|
+
ht as default
|
|
2328
|
+
};
|