@variamosple/variamos-components 0.1.2 → 0.2.1

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