@paramms/chat-widget 1.0.36 → 1.0.37

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/outbox.js CHANGED
@@ -1,1071 +1,34 @@
1
- var D = Object.defineProperty;
2
- var z = (n, e, t) => e in n ? D(n, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : n[e] = t;
3
- var r = (n, e, t) => z(n, typeof e != "symbol" ? e + "" : e, t);
4
- const L = (n) => n, Ae = (n) => n, Te = (n) => n, ke = (n) => n, ve = (n) => n, be = (n) => n, Me = (n) => n, _e = (n) => n, Ce = L("anonymous");
5
- function F(n) {
6
- const e = n.length;
7
- let t = 0, s = 0;
8
- for (; s < e; ) {
9
- let i = n.charCodeAt(s++);
10
- if (i & 4294967168)
11
- if (!(i & 4294965248))
12
- t += 2;
13
- else {
14
- if (i >= 55296 && i <= 56319 && s < e) {
15
- const o = n.charCodeAt(s);
16
- (o & 64512) === 56320 && (++s, i = ((i & 1023) << 10) + (o & 1023) + 65536);
17
- }
18
- i & 4294901760 ? t += 4 : t += 3;
19
- }
20
- else {
21
- t++;
22
- continue;
23
- }
24
- }
25
- return t;
26
- }
27
- function R(n, e, t) {
28
- const s = n.length;
29
- let i = t, o = 0;
30
- for (; o < s; ) {
31
- let h = n.charCodeAt(o++);
32
- if (h & 4294967168)
33
- if (!(h & 4294965248))
34
- e[i++] = h >> 6 & 31 | 192;
35
- else {
36
- if (h >= 55296 && h <= 56319 && o < s) {
37
- const a = n.charCodeAt(o);
38
- (a & 64512) === 56320 && (++o, h = ((h & 1023) << 10) + (a & 1023) + 65536);
39
- }
40
- h & 4294901760 ? (e[i++] = h >> 18 & 7 | 240, e[i++] = h >> 12 & 63 | 128, e[i++] = h >> 6 & 63 | 128) : (e[i++] = h >> 12 & 15 | 224, e[i++] = h >> 6 & 63 | 128);
41
- }
42
- else {
43
- e[i++] = h;
44
- continue;
45
- }
46
- e[i++] = h & 63 | 128;
47
- }
48
- }
49
- const P = new TextEncoder(), H = 50;
50
- function $(n, e, t) {
51
- P.encodeInto(n, e.subarray(t));
52
- }
53
- function K(n, e, t) {
54
- n.length > H ? $(n, e, t) : R(n, e, t);
55
- }
56
- const N = 4096;
57
- function v(n, e, t) {
58
- let s = e;
59
- const i = s + t, o = [];
60
- let h = "";
61
- for (; s < i; ) {
62
- const a = n[s++];
63
- if (!(a & 128))
64
- o.push(a);
65
- else if ((a & 224) === 192) {
66
- const d = n[s++] & 63;
67
- o.push((a & 31) << 6 | d);
68
- } else if ((a & 240) === 224) {
69
- const d = n[s++] & 63, p = n[s++] & 63;
70
- o.push((a & 31) << 12 | d << 6 | p);
71
- } else if ((a & 248) === 240) {
72
- const d = n[s++] & 63, p = n[s++] & 63, C = n[s++] & 63;
73
- let l = (a & 7) << 18 | d << 12 | p << 6 | C;
74
- l > 65535 && (l -= 65536, o.push(l >>> 10 & 1023 | 55296), l = 56320 | l & 1023), o.push(l);
75
- } else
76
- o.push(a);
77
- o.length >= N && (h += String.fromCharCode(...o), o.length = 0);
78
- }
79
- return o.length > 0 && (h += String.fromCharCode(...o)), h;
80
- }
81
- const q = new TextDecoder(), O = 200;
82
- function V(n, e, t) {
83
- const s = n.subarray(e, e + t);
84
- return q.decode(s);
85
- }
86
- function W(n, e, t) {
87
- return t > O ? V(n, e, t) : v(n, e, t);
88
- }
89
- class g {
90
- constructor(e, t) {
91
- r(this, "type");
92
- r(this, "data");
93
- this.type = e, this.data = t;
94
- }
95
- }
96
- class c extends Error {
97
- constructor(e) {
98
- super(e);
99
- const t = Object.create(c.prototype);
100
- Object.setPrototypeOf(this, t), Object.defineProperty(this, "name", {
101
- configurable: !0,
102
- enumerable: !1,
103
- value: c.name
104
- });
105
- }
106
- }
107
- const f = 4294967295;
108
- function X(n, e, t) {
109
- const s = t / 4294967296, i = t;
110
- n.setUint32(e, s), n.setUint32(e + 4, i);
111
- }
112
- function b(n, e, t) {
113
- const s = Math.floor(t / 4294967296), i = t;
114
- n.setUint32(e, s), n.setUint32(e + 4, i);
115
- }
116
- function M(n, e) {
117
- const t = n.getInt32(e), s = n.getUint32(e + 4);
118
- return t * 4294967296 + s;
119
- }
120
- function Y(n, e) {
121
- const t = n.getUint32(e), s = n.getUint32(e + 4);
122
- return t * 4294967296 + s;
123
- }
124
- const J = -1, Z = 4294967296 - 1, G = 17179869184 - 1;
125
- function Q({ sec: n, nsec: e }) {
126
- if (n >= 0 && e >= 0 && n <= G)
127
- if (e === 0 && n <= Z) {
128
- const t = new Uint8Array(4);
129
- return new DataView(t.buffer).setUint32(0, n), t;
130
- } else {
131
- const t = n / 4294967296, s = n & 4294967295, i = new Uint8Array(8), o = new DataView(i.buffer);
132
- return o.setUint32(0, e << 2 | t & 3), o.setUint32(4, s), i;
133
- }
134
- else {
135
- const t = new Uint8Array(12), s = new DataView(t.buffer);
136
- return s.setUint32(0, e), b(s, 4, n), t;
137
- }
138
- }
139
- function j(n) {
140
- const e = n.getTime(), t = Math.floor(e / 1e3), s = (e - t * 1e3) * 1e6, i = Math.floor(s / 1e9);
141
- return {
142
- sec: t + i,
143
- nsec: s - i * 1e9
144
- };
145
- }
146
- function ee(n) {
147
- if (n instanceof Date) {
148
- const e = j(n);
149
- return Q(e);
150
- } else
151
- return null;
152
- }
153
- function te(n) {
154
- const e = new DataView(n.buffer, n.byteOffset, n.byteLength);
155
- switch (n.byteLength) {
156
- case 4:
157
- return { sec: e.getUint32(0), nsec: 0 };
158
- case 8: {
159
- const t = e.getUint32(0), s = e.getUint32(4), i = (t & 3) * 4294967296 + s, o = t >>> 2;
160
- return { sec: i, nsec: o };
161
- }
162
- case 12: {
163
- const t = M(e, 4), s = e.getUint32(0);
164
- return { sec: t, nsec: s };
165
- }
166
- default:
167
- throw new c(`Unrecognized data size for timestamp (expected 4, 8, or 12): ${n.length}`);
168
- }
169
- }
170
- function se(n) {
171
- const e = te(n);
172
- return new Date(e.sec * 1e3 + e.nsec / 1e6);
173
- }
174
- const ie = {
175
- type: J,
176
- encode: ee,
177
- decode: se
178
- }, w = class w {
179
- constructor() {
180
- // ensures ExtensionCodecType<X> matches ExtensionCodec<X>
181
- // this will make type errors a lot more clear
182
- // eslint-disable-next-line @typescript-eslint/naming-convention
183
- r(this, "__brand");
184
- // built-in extensions
185
- r(this, "builtInEncoders", []);
186
- r(this, "builtInDecoders", []);
187
- // custom extensions
188
- r(this, "encoders", []);
189
- r(this, "decoders", []);
190
- this.register(ie);
191
- }
192
- register({ type: e, encode: t, decode: s }) {
193
- if (e >= 0)
194
- this.encoders[e] = t, this.decoders[e] = s;
195
- else {
196
- const i = -1 - e;
197
- this.builtInEncoders[i] = t, this.builtInDecoders[i] = s;
198
- }
199
- }
200
- tryToEncode(e, t) {
201
- for (let s = 0; s < this.builtInEncoders.length; s++) {
202
- const i = this.builtInEncoders[s];
203
- if (i != null) {
204
- const o = i(e, t);
205
- if (o != null) {
206
- const h = -1 - s;
207
- return new g(h, o);
208
- }
209
- }
210
- }
211
- for (let s = 0; s < this.encoders.length; s++) {
212
- const i = this.encoders[s];
213
- if (i != null) {
214
- const o = i(e, t);
215
- if (o != null) {
216
- const h = s;
217
- return new g(h, o);
218
- }
219
- }
220
- }
221
- return e instanceof g ? e : null;
222
- }
223
- decode(e, t, s) {
224
- const i = t < 0 ? this.builtInDecoders[-1 - t] : this.decoders[t];
225
- return i ? i(e, t, s) : new g(t, e);
226
- }
227
- };
228
- r(w, "defaultCodec", new w());
229
- let y = w;
230
- function ne(n) {
231
- return n instanceof ArrayBuffer || typeof SharedArrayBuffer < "u" && n instanceof SharedArrayBuffer;
232
- }
233
- function S(n) {
234
- return n instanceof Uint8Array ? n : ArrayBuffer.isView(n) ? new Uint8Array(n.buffer, n.byteOffset, n.byteLength) : ne(n) ? new Uint8Array(n) : Uint8Array.from(n);
235
- }
236
- const re = 100, oe = 2048;
237
- class m {
238
- constructor(e) {
239
- r(this, "extensionCodec");
240
- r(this, "context");
241
- r(this, "useBigInt64");
242
- r(this, "maxDepth");
243
- r(this, "initialBufferSize");
244
- r(this, "sortKeys");
245
- r(this, "forceFloat32");
246
- r(this, "ignoreUndefined");
247
- r(this, "forceIntegerToFloat");
248
- r(this, "pos");
249
- r(this, "view");
250
- r(this, "bytes");
251
- r(this, "entered", !1);
252
- this.extensionCodec = (e == null ? void 0 : e.extensionCodec) ?? y.defaultCodec, this.context = e == null ? void 0 : e.context, this.useBigInt64 = (e == null ? void 0 : e.useBigInt64) ?? !1, this.maxDepth = (e == null ? void 0 : e.maxDepth) ?? re, this.initialBufferSize = (e == null ? void 0 : e.initialBufferSize) ?? oe, this.sortKeys = (e == null ? void 0 : e.sortKeys) ?? !1, this.forceFloat32 = (e == null ? void 0 : e.forceFloat32) ?? !1, this.ignoreUndefined = (e == null ? void 0 : e.ignoreUndefined) ?? !1, this.forceIntegerToFloat = (e == null ? void 0 : e.forceIntegerToFloat) ?? !1, this.pos = 0, this.view = new DataView(new ArrayBuffer(this.initialBufferSize)), this.bytes = new Uint8Array(this.view.buffer);
253
- }
254
- clone() {
255
- return new m({
256
- extensionCodec: this.extensionCodec,
257
- context: this.context,
258
- useBigInt64: this.useBigInt64,
259
- maxDepth: this.maxDepth,
260
- initialBufferSize: this.initialBufferSize,
261
- sortKeys: this.sortKeys,
262
- forceFloat32: this.forceFloat32,
263
- ignoreUndefined: this.ignoreUndefined,
264
- forceIntegerToFloat: this.forceIntegerToFloat
265
- });
266
- }
267
- reinitializeState() {
268
- this.pos = 0;
269
- }
270
- /**
271
- * This is almost equivalent to {@link Encoder#encode}, but it returns an reference of the encoder's internal buffer and thus much faster than {@link Encoder#encode}.
272
- *
273
- * @returns Encodes the object and returns a shared reference the encoder's internal buffer.
274
- */
275
- encodeSharedRef(e) {
276
- if (this.entered)
277
- return this.clone().encodeSharedRef(e);
278
- try {
279
- return this.entered = !0, this.reinitializeState(), this.doEncode(e, 1), this.bytes.subarray(0, this.pos);
280
- } finally {
281
- this.entered = !1;
282
- }
283
- }
284
- /**
285
- * @returns Encodes the object and returns a copy of the encoder's internal buffer.
286
- */
287
- encode(e) {
288
- if (this.entered)
289
- return this.clone().encode(e);
290
- try {
291
- return this.entered = !0, this.reinitializeState(), this.doEncode(e, 1), this.bytes.slice(0, this.pos);
292
- } finally {
293
- this.entered = !1;
294
- }
295
- }
296
- doEncode(e, t) {
297
- if (t > this.maxDepth)
298
- throw new Error(`Too deep objects in depth ${t}`);
299
- e == null ? this.encodeNil() : typeof e == "boolean" ? this.encodeBoolean(e) : typeof e == "number" ? this.forceIntegerToFloat ? this.encodeNumberAsFloat(e) : this.encodeNumber(e) : typeof e == "string" ? this.encodeString(e) : this.useBigInt64 && typeof e == "bigint" ? this.encodeBigInt64(e) : this.encodeObject(e, t);
300
- }
301
- ensureBufferSizeToWrite(e) {
302
- const t = this.pos + e;
303
- this.view.byteLength < t && this.resizeBuffer(t * 2);
304
- }
305
- resizeBuffer(e) {
306
- const t = new ArrayBuffer(e), s = new Uint8Array(t), i = new DataView(t);
307
- s.set(this.bytes), this.view = i, this.bytes = s;
308
- }
309
- encodeNil() {
310
- this.writeU8(192);
311
- }
312
- encodeBoolean(e) {
313
- e === !1 ? this.writeU8(194) : this.writeU8(195);
314
- }
315
- encodeNumber(e) {
316
- !this.forceIntegerToFloat && Number.isSafeInteger(e) ? e >= 0 ? e < 128 ? this.writeU8(e) : e < 256 ? (this.writeU8(204), this.writeU8(e)) : e < 65536 ? (this.writeU8(205), this.writeU16(e)) : e < 4294967296 ? (this.writeU8(206), this.writeU32(e)) : this.useBigInt64 ? this.encodeNumberAsFloat(e) : (this.writeU8(207), this.writeU64(e)) : e >= -32 ? this.writeU8(224 | e + 32) : e >= -128 ? (this.writeU8(208), this.writeI8(e)) : e >= -32768 ? (this.writeU8(209), this.writeI16(e)) : e >= -2147483648 ? (this.writeU8(210), this.writeI32(e)) : this.useBigInt64 ? this.encodeNumberAsFloat(e) : (this.writeU8(211), this.writeI64(e)) : this.encodeNumberAsFloat(e);
317
- }
318
- encodeNumberAsFloat(e) {
319
- this.forceFloat32 ? (this.writeU8(202), this.writeF32(e)) : (this.writeU8(203), this.writeF64(e));
320
- }
321
- encodeBigInt64(e) {
322
- e >= BigInt(0) ? (this.writeU8(207), this.writeBigUint64(e)) : (this.writeU8(211), this.writeBigInt64(e));
323
- }
324
- writeStringHeader(e) {
325
- if (e < 32)
326
- this.writeU8(160 + e);
327
- else if (e < 256)
328
- this.writeU8(217), this.writeU8(e);
329
- else if (e < 65536)
330
- this.writeU8(218), this.writeU16(e);
331
- else if (e < 4294967296)
332
- this.writeU8(219), this.writeU32(e);
333
- else
334
- throw new Error(`Too long string: ${e} bytes in UTF-8`);
335
- }
336
- encodeString(e) {
337
- const s = F(e);
338
- this.ensureBufferSizeToWrite(5 + s), this.writeStringHeader(s), K(e, this.bytes, this.pos), this.pos += s;
339
- }
340
- encodeObject(e, t) {
341
- const s = this.extensionCodec.tryToEncode(e, this.context);
342
- if (s != null)
343
- this.encodeExtension(s);
344
- else if (Array.isArray(e))
345
- this.encodeArray(e, t);
346
- else if (ArrayBuffer.isView(e))
347
- this.encodeBinary(e);
348
- else if (typeof e == "object")
349
- this.encodeMap(e, t);
350
- else
351
- throw new Error(`Unrecognized object: ${Object.prototype.toString.apply(e)}`);
352
- }
353
- encodeBinary(e) {
354
- const t = e.byteLength;
355
- if (t < 256)
356
- this.writeU8(196), this.writeU8(t);
357
- else if (t < 65536)
358
- this.writeU8(197), this.writeU16(t);
359
- else if (t < 4294967296)
360
- this.writeU8(198), this.writeU32(t);
361
- else
362
- throw new Error(`Too large binary: ${t}`);
363
- const s = S(e);
364
- this.writeU8a(s);
365
- }
366
- encodeArray(e, t) {
367
- const s = e.length;
368
- if (s < 16)
369
- this.writeU8(144 + s);
370
- else if (s < 65536)
371
- this.writeU8(220), this.writeU16(s);
372
- else if (s < 4294967296)
373
- this.writeU8(221), this.writeU32(s);
374
- else
375
- throw new Error(`Too large array: ${s}`);
376
- for (const i of e)
377
- this.doEncode(i, t + 1);
378
- }
379
- countWithoutUndefined(e, t) {
380
- let s = 0;
381
- for (const i of t)
382
- e[i] !== void 0 && s++;
383
- return s;
384
- }
385
- encodeMap(e, t) {
386
- const s = Object.keys(e);
387
- this.sortKeys && s.sort();
388
- const i = this.ignoreUndefined ? this.countWithoutUndefined(e, s) : s.length;
389
- if (i < 16)
390
- this.writeU8(128 + i);
391
- else if (i < 65536)
392
- this.writeU8(222), this.writeU16(i);
393
- else if (i < 4294967296)
394
- this.writeU8(223), this.writeU32(i);
395
- else
396
- throw new Error(`Too large map object: ${i}`);
397
- for (const o of s) {
398
- const h = e[o];
399
- this.ignoreUndefined && h === void 0 || (this.encodeString(o), this.doEncode(h, t + 1));
400
- }
401
- }
402
- encodeExtension(e) {
403
- if (typeof e.data == "function") {
404
- const s = e.data(this.pos + 6), i = s.length;
405
- if (i >= 4294967296)
406
- throw new Error(`Too large extension object: ${i}`);
407
- this.writeU8(201), this.writeU32(i), this.writeI8(e.type), this.writeU8a(s);
408
- return;
409
- }
410
- const t = e.data.length;
411
- if (t === 1)
412
- this.writeU8(212);
413
- else if (t === 2)
414
- this.writeU8(213);
415
- else if (t === 4)
416
- this.writeU8(214);
417
- else if (t === 8)
418
- this.writeU8(215);
419
- else if (t === 16)
420
- this.writeU8(216);
421
- else if (t < 256)
422
- this.writeU8(199), this.writeU8(t);
423
- else if (t < 65536)
424
- this.writeU8(200), this.writeU16(t);
425
- else if (t < 4294967296)
426
- this.writeU8(201), this.writeU32(t);
427
- else
428
- throw new Error(`Too large extension object: ${t}`);
429
- this.writeI8(e.type), this.writeU8a(e.data);
430
- }
431
- writeU8(e) {
432
- this.ensureBufferSizeToWrite(1), this.view.setUint8(this.pos, e), this.pos++;
433
- }
434
- writeU8a(e) {
435
- const t = e.length;
436
- this.ensureBufferSizeToWrite(t), this.bytes.set(e, this.pos), this.pos += t;
437
- }
438
- writeI8(e) {
439
- this.ensureBufferSizeToWrite(1), this.view.setInt8(this.pos, e), this.pos++;
440
- }
441
- writeU16(e) {
442
- this.ensureBufferSizeToWrite(2), this.view.setUint16(this.pos, e), this.pos += 2;
443
- }
444
- writeI16(e) {
445
- this.ensureBufferSizeToWrite(2), this.view.setInt16(this.pos, e), this.pos += 2;
446
- }
447
- writeU32(e) {
448
- this.ensureBufferSizeToWrite(4), this.view.setUint32(this.pos, e), this.pos += 4;
449
- }
450
- writeI32(e) {
451
- this.ensureBufferSizeToWrite(4), this.view.setInt32(this.pos, e), this.pos += 4;
452
- }
453
- writeF32(e) {
454
- this.ensureBufferSizeToWrite(4), this.view.setFloat32(this.pos, e), this.pos += 4;
455
- }
456
- writeF64(e) {
457
- this.ensureBufferSizeToWrite(8), this.view.setFloat64(this.pos, e), this.pos += 8;
458
- }
459
- writeU64(e) {
460
- this.ensureBufferSizeToWrite(8), X(this.view, this.pos, e), this.pos += 8;
461
- }
462
- writeI64(e) {
463
- this.ensureBufferSizeToWrite(8), b(this.view, this.pos, e), this.pos += 8;
464
- }
465
- writeBigUint64(e) {
466
- this.ensureBufferSizeToWrite(8), this.view.setBigUint64(this.pos, e), this.pos += 8;
467
- }
468
- writeBigInt64(e) {
469
- this.ensureBufferSizeToWrite(8), this.view.setBigInt64(this.pos, e), this.pos += 8;
470
- }
471
- }
472
- function he(n, e) {
473
- return new m(e).encodeSharedRef(n);
474
- }
475
- function U(n) {
476
- return `${n < 0 ? "-" : ""}0x${Math.abs(n).toString(16).padStart(2, "0")}`;
477
- }
478
- const ae = 16, ce = 16;
479
- class de {
480
- constructor(e = ae, t = ce) {
481
- r(this, "hit", 0);
482
- r(this, "miss", 0);
483
- r(this, "caches");
484
- r(this, "maxKeyLength");
485
- r(this, "maxLengthPerKey");
486
- this.maxKeyLength = e, this.maxLengthPerKey = t, this.caches = [];
487
- for (let s = 0; s < this.maxKeyLength; s++)
488
- this.caches.push([]);
489
- }
490
- canBeCached(e) {
491
- return e > 0 && e <= this.maxKeyLength;
492
- }
493
- find(e, t, s) {
494
- const i = this.caches[s - 1];
495
- e: for (const o of i) {
496
- const h = o.bytes;
497
- for (let a = 0; a < s; a++)
498
- if (h[a] !== e[t + a])
499
- continue e;
500
- return o.str;
501
- }
502
- return null;
503
- }
504
- store(e, t) {
505
- const s = this.caches[e.length - 1], i = { bytes: e, str: t };
506
- s.length >= this.maxLengthPerKey ? s[Math.random() * s.length | 0] = i : s.push(i);
507
- }
508
- decode(e, t, s) {
509
- const i = this.find(e, t, s);
510
- if (i != null)
511
- return this.hit++, i;
512
- this.miss++;
513
- const o = v(e, t, s), h = Uint8Array.prototype.slice.call(e, t, t + s);
514
- return this.store(h, o), o;
515
- }
516
- }
517
- const I = "array", x = "map_key", _ = "map_value", le = (n) => {
518
- if (typeof n == "string" || typeof n == "number")
519
- return n;
520
- throw new c("The type of key must be string or number but " + typeof n);
521
- };
522
- class fe {
523
- constructor() {
524
- r(this, "stack", []);
525
- r(this, "stackHeadPosition", -1);
526
- }
527
- get length() {
528
- return this.stackHeadPosition + 1;
529
- }
530
- top() {
531
- return this.stack[this.stackHeadPosition];
532
- }
533
- pushArrayState(e) {
534
- const t = this.getUninitializedStateFromPool();
535
- t.type = I, t.position = 0, t.size = e, t.array = new Array(e);
536
- }
537
- pushMapState(e) {
538
- const t = this.getUninitializedStateFromPool();
539
- t.type = x, t.readCount = 0, t.size = e, t.map = {};
540
- }
541
- getUninitializedStateFromPool() {
542
- if (this.stackHeadPosition++, this.stackHeadPosition === this.stack.length) {
543
- const e = {
544
- type: void 0,
545
- size: 0,
546
- array: void 0,
547
- position: 0,
548
- readCount: 0,
549
- map: void 0,
550
- key: null
551
- };
552
- this.stack.push(e);
553
- }
554
- return this.stack[this.stackHeadPosition];
555
- }
556
- release(e) {
557
- if (this.stack[this.stackHeadPosition] !== e)
558
- throw new Error("Invalid stack state. Released state is not on top of the stack.");
559
- if (e.type === I) {
560
- const s = e;
561
- s.size = 0, s.array = void 0, s.position = 0, s.type = void 0;
562
- }
563
- if (e.type === x || e.type === _) {
564
- const s = e;
565
- s.size = 0, s.map = void 0, s.readCount = 0, s.type = void 0;
566
- }
567
- this.stackHeadPosition--;
568
- }
569
- reset() {
570
- this.stack.length = 0, this.stackHeadPosition = -1;
571
- }
572
- }
573
- const u = -1, B = new DataView(new ArrayBuffer(0)), ue = new Uint8Array(B.buffer);
574
- try {
575
- B.getInt8(0);
576
- } catch (n) {
577
- if (!(n instanceof RangeError))
578
- throw new Error("This module is not supported in the current JavaScript engine because DataView does not throw RangeError on out-of-bounds access");
579
- }
580
- const A = new RangeError("Insufficient data"), xe = new de();
581
- class E {
582
- constructor(e) {
583
- r(this, "extensionCodec");
584
- r(this, "context");
585
- r(this, "useBigInt64");
586
- r(this, "rawStrings");
587
- r(this, "maxStrLength");
588
- r(this, "maxBinLength");
589
- r(this, "maxArrayLength");
590
- r(this, "maxMapLength");
591
- r(this, "maxExtLength");
592
- r(this, "keyDecoder");
593
- r(this, "mapKeyConverter");
594
- r(this, "totalPos", 0);
595
- r(this, "pos", 0);
596
- r(this, "view", B);
597
- r(this, "bytes", ue);
598
- r(this, "headByte", u);
599
- r(this, "stack", new fe());
600
- r(this, "entered", !1);
601
- this.extensionCodec = (e == null ? void 0 : e.extensionCodec) ?? y.defaultCodec, this.context = e == null ? void 0 : e.context, this.useBigInt64 = (e == null ? void 0 : e.useBigInt64) ?? !1, this.rawStrings = (e == null ? void 0 : e.rawStrings) ?? !1, this.maxStrLength = (e == null ? void 0 : e.maxStrLength) ?? f, this.maxBinLength = (e == null ? void 0 : e.maxBinLength) ?? f, this.maxArrayLength = (e == null ? void 0 : e.maxArrayLength) ?? f, this.maxMapLength = (e == null ? void 0 : e.maxMapLength) ?? f, this.maxExtLength = (e == null ? void 0 : e.maxExtLength) ?? f, this.keyDecoder = (e == null ? void 0 : e.keyDecoder) !== void 0 ? e.keyDecoder : xe, this.mapKeyConverter = (e == null ? void 0 : e.mapKeyConverter) ?? le;
602
- }
603
- clone() {
604
- return new E({
605
- extensionCodec: this.extensionCodec,
606
- context: this.context,
607
- useBigInt64: this.useBigInt64,
608
- rawStrings: this.rawStrings,
609
- maxStrLength: this.maxStrLength,
610
- maxBinLength: this.maxBinLength,
611
- maxArrayLength: this.maxArrayLength,
612
- maxMapLength: this.maxMapLength,
613
- maxExtLength: this.maxExtLength,
614
- keyDecoder: this.keyDecoder
615
- });
616
- }
617
- reinitializeState() {
618
- this.totalPos = 0, this.headByte = u, this.stack.reset();
619
- }
620
- setBuffer(e) {
621
- const t = S(e);
622
- this.bytes = t, this.view = new DataView(t.buffer, t.byteOffset, t.byteLength), this.pos = 0;
623
- }
624
- appendBuffer(e) {
625
- if (this.headByte === u && !this.hasRemaining(1))
626
- this.setBuffer(e);
627
- else {
628
- const t = this.bytes.subarray(this.pos), s = S(e), i = new Uint8Array(t.length + s.length);
629
- i.set(t), i.set(s, t.length), this.setBuffer(i);
630
- }
631
- }
632
- hasRemaining(e) {
633
- return this.view.byteLength - this.pos >= e;
634
- }
635
- createExtraByteError(e) {
636
- const { view: t, pos: s } = this;
637
- return new RangeError(`Extra ${t.byteLength - s} of ${t.byteLength} byte(s) found at buffer[${e}]`);
638
- }
639
- /**
640
- * @throws {@link DecodeError}
641
- * @throws {@link RangeError}
642
- */
643
- decode(e) {
644
- if (this.entered)
645
- return this.clone().decode(e);
646
- try {
647
- this.entered = !0, this.reinitializeState(), this.setBuffer(e);
648
- const t = this.doDecodeSync();
649
- if (this.hasRemaining(1))
650
- throw this.createExtraByteError(this.pos);
651
- return t;
652
- } finally {
653
- this.entered = !1;
654
- }
655
- }
656
- *decodeMulti(e) {
657
- if (this.entered) {
658
- yield* this.clone().decodeMulti(e);
659
- return;
660
- }
661
- try {
662
- for (this.entered = !0, this.reinitializeState(), this.setBuffer(e); this.hasRemaining(1); )
663
- yield this.doDecodeSync();
664
- } finally {
665
- this.entered = !1;
666
- }
667
- }
668
- async decodeAsync(e) {
669
- if (this.entered)
670
- return this.clone().decodeAsync(e);
671
- try {
672
- this.entered = !0;
673
- let t = !1, s;
674
- for await (const a of e) {
675
- if (t)
676
- throw this.entered = !1, this.createExtraByteError(this.totalPos);
677
- this.appendBuffer(a);
678
- try {
679
- s = this.doDecodeSync(), t = !0;
680
- } catch (d) {
681
- if (!(d instanceof RangeError))
682
- throw d;
683
- }
684
- this.totalPos += this.pos;
685
- }
686
- if (t) {
687
- if (this.hasRemaining(1))
688
- throw this.createExtraByteError(this.totalPos);
689
- return s;
690
- }
691
- const { headByte: i, pos: o, totalPos: h } = this;
692
- throw new RangeError(`Insufficient data in parsing ${U(i)} at ${h} (${o} in the current buffer)`);
693
- } finally {
694
- this.entered = !1;
695
- }
696
- }
697
- decodeArrayStream(e) {
698
- return this.decodeMultiAsync(e, !0);
699
- }
700
- decodeStream(e) {
701
- return this.decodeMultiAsync(e, !1);
702
- }
703
- async *decodeMultiAsync(e, t) {
704
- if (this.entered) {
705
- yield* this.clone().decodeMultiAsync(e, t);
706
- return;
707
- }
708
- try {
709
- this.entered = !0;
710
- let s = t, i = -1;
711
- for await (const o of e) {
712
- if (t && i === 0)
713
- throw this.createExtraByteError(this.totalPos);
714
- this.appendBuffer(o), s && (i = this.readArraySize(), s = !1, this.complete());
715
- try {
716
- for (; yield this.doDecodeSync(), --i !== 0; )
717
- ;
718
- } catch (h) {
719
- if (!(h instanceof RangeError))
720
- throw h;
721
- }
722
- this.totalPos += this.pos;
723
- }
724
- } finally {
725
- this.entered = !1;
726
- }
727
- }
728
- doDecodeSync() {
729
- e: for (; ; ) {
730
- const e = this.readHeadByte();
731
- let t;
732
- if (e >= 224)
733
- t = e - 256;
734
- else if (e < 192)
735
- if (e < 128)
736
- t = e;
737
- else if (e < 144) {
738
- const i = e - 128;
739
- if (i !== 0) {
740
- this.pushMapState(i), this.complete();
741
- continue e;
742
- } else
743
- t = {};
744
- } else if (e < 160) {
745
- const i = e - 144;
746
- if (i !== 0) {
747
- this.pushArrayState(i), this.complete();
748
- continue e;
749
- } else
750
- t = [];
751
- } else {
752
- const i = e - 160;
753
- t = this.decodeString(i, 0);
754
- }
755
- else if (e === 192)
756
- t = null;
757
- else if (e === 194)
758
- t = !1;
759
- else if (e === 195)
760
- t = !0;
761
- else if (e === 202)
762
- t = this.readF32();
763
- else if (e === 203)
764
- t = this.readF64();
765
- else if (e === 204)
766
- t = this.readU8();
767
- else if (e === 205)
768
- t = this.readU16();
769
- else if (e === 206)
770
- t = this.readU32();
771
- else if (e === 207)
772
- this.useBigInt64 ? t = this.readU64AsBigInt() : t = this.readU64();
773
- else if (e === 208)
774
- t = this.readI8();
775
- else if (e === 209)
776
- t = this.readI16();
777
- else if (e === 210)
778
- t = this.readI32();
779
- else if (e === 211)
780
- this.useBigInt64 ? t = this.readI64AsBigInt() : t = this.readI64();
781
- else if (e === 217) {
782
- const i = this.lookU8();
783
- t = this.decodeString(i, 1);
784
- } else if (e === 218) {
785
- const i = this.lookU16();
786
- t = this.decodeString(i, 2);
787
- } else if (e === 219) {
788
- const i = this.lookU32();
789
- t = this.decodeString(i, 4);
790
- } else if (e === 220) {
791
- const i = this.readU16();
792
- if (i !== 0) {
793
- this.pushArrayState(i), this.complete();
794
- continue e;
795
- } else
796
- t = [];
797
- } else if (e === 221) {
798
- const i = this.readU32();
799
- if (i !== 0) {
800
- this.pushArrayState(i), this.complete();
801
- continue e;
802
- } else
803
- t = [];
804
- } else if (e === 222) {
805
- const i = this.readU16();
806
- if (i !== 0) {
807
- this.pushMapState(i), this.complete();
808
- continue e;
809
- } else
810
- t = {};
811
- } else if (e === 223) {
812
- const i = this.readU32();
813
- if (i !== 0) {
814
- this.pushMapState(i), this.complete();
815
- continue e;
816
- } else
817
- t = {};
818
- } else if (e === 196) {
819
- const i = this.lookU8();
820
- t = this.decodeBinary(i, 1);
821
- } else if (e === 197) {
822
- const i = this.lookU16();
823
- t = this.decodeBinary(i, 2);
824
- } else if (e === 198) {
825
- const i = this.lookU32();
826
- t = this.decodeBinary(i, 4);
827
- } else if (e === 212)
828
- t = this.decodeExtension(1, 0);
829
- else if (e === 213)
830
- t = this.decodeExtension(2, 0);
831
- else if (e === 214)
832
- t = this.decodeExtension(4, 0);
833
- else if (e === 215)
834
- t = this.decodeExtension(8, 0);
835
- else if (e === 216)
836
- t = this.decodeExtension(16, 0);
837
- else if (e === 199) {
838
- const i = this.lookU8();
839
- t = this.decodeExtension(i, 1);
840
- } else if (e === 200) {
841
- const i = this.lookU16();
842
- t = this.decodeExtension(i, 2);
843
- } else if (e === 201) {
844
- const i = this.lookU32();
845
- t = this.decodeExtension(i, 4);
846
- } else
847
- throw new c(`Unrecognized type byte: ${U(e)}`);
848
- this.complete();
849
- const s = this.stack;
850
- for (; s.length > 0; ) {
851
- const i = s.top();
852
- if (i.type === I)
853
- if (i.array[i.position] = t, i.position++, i.position === i.size)
854
- t = i.array, s.release(i);
855
- else
856
- continue e;
857
- else if (i.type === x) {
858
- if (t === "__proto__")
859
- throw new c("The key __proto__ is not allowed");
860
- i.key = this.mapKeyConverter(t), i.type = _;
861
- continue e;
862
- } else if (i.map[i.key] = t, i.readCount++, i.readCount === i.size)
863
- t = i.map, s.release(i);
864
- else {
865
- i.key = null, i.type = x;
866
- continue e;
867
- }
868
- }
869
- return t;
870
- }
871
- }
872
- readHeadByte() {
873
- return this.headByte === u && (this.headByte = this.readU8()), this.headByte;
874
- }
875
- complete() {
876
- this.headByte = u;
877
- }
878
- readArraySize() {
879
- const e = this.readHeadByte();
880
- switch (e) {
881
- case 220:
882
- return this.readU16();
883
- case 221:
884
- return this.readU32();
885
- default: {
886
- if (e < 160)
887
- return e - 144;
888
- throw new c(`Unrecognized array type byte: ${U(e)}`);
889
- }
890
- }
891
- }
892
- pushMapState(e) {
893
- if (e > this.maxMapLength)
894
- throw new c(`Max length exceeded: map length (${e}) > maxMapLengthLength (${this.maxMapLength})`);
895
- this.stack.pushMapState(e);
896
- }
897
- pushArrayState(e) {
898
- if (e > this.maxArrayLength)
899
- throw new c(`Max length exceeded: array length (${e}) > maxArrayLength (${this.maxArrayLength})`);
900
- this.stack.pushArrayState(e);
901
- }
902
- decodeString(e, t) {
903
- return !this.rawStrings || this.stateIsMapKey() ? this.decodeUtf8String(e, t) : this.decodeBinary(e, t);
904
- }
905
- /**
906
- * @throws {@link RangeError}
907
- */
908
- decodeUtf8String(e, t) {
909
- var o;
910
- if (e > this.maxStrLength)
911
- throw new c(`Max length exceeded: UTF-8 byte length (${e}) > maxStrLength (${this.maxStrLength})`);
912
- if (this.bytes.byteLength < this.pos + t + e)
913
- throw A;
914
- const s = this.pos + t;
915
- let i;
916
- return this.stateIsMapKey() && ((o = this.keyDecoder) != null && o.canBeCached(e)) ? i = this.keyDecoder.decode(this.bytes, s, e) : i = W(this.bytes, s, e), this.pos += t + e, i;
917
- }
918
- stateIsMapKey() {
919
- return this.stack.length > 0 ? this.stack.top().type === x : !1;
920
- }
921
- /**
922
- * @throws {@link RangeError}
923
- */
924
- decodeBinary(e, t) {
925
- if (e > this.maxBinLength)
926
- throw new c(`Max length exceeded: bin length (${e}) > maxBinLength (${this.maxBinLength})`);
927
- if (!this.hasRemaining(e + t))
928
- throw A;
929
- const s = this.pos + t, i = this.bytes.subarray(s, s + e);
930
- return this.pos += t + e, i;
931
- }
932
- decodeExtension(e, t) {
933
- if (e > this.maxExtLength)
934
- throw new c(`Max length exceeded: ext length (${e}) > maxExtLength (${this.maxExtLength})`);
935
- const s = this.view.getInt8(this.pos + t), i = this.decodeBinary(
936
- e,
937
- t + 1
938
- /* extType */
939
- );
940
- return this.extensionCodec.decode(i, s, this.context);
941
- }
942
- lookU8() {
943
- return this.view.getUint8(this.pos);
944
- }
945
- lookU16() {
946
- return this.view.getUint16(this.pos);
947
- }
948
- lookU32() {
949
- return this.view.getUint32(this.pos);
950
- }
951
- readU8() {
952
- const e = this.view.getUint8(this.pos);
953
- return this.pos++, e;
954
- }
955
- readI8() {
956
- const e = this.view.getInt8(this.pos);
957
- return this.pos++, e;
958
- }
959
- readU16() {
960
- const e = this.view.getUint16(this.pos);
961
- return this.pos += 2, e;
962
- }
963
- readI16() {
964
- const e = this.view.getInt16(this.pos);
965
- return this.pos += 2, e;
966
- }
967
- readU32() {
968
- const e = this.view.getUint32(this.pos);
969
- return this.pos += 4, e;
970
- }
971
- readI32() {
972
- const e = this.view.getInt32(this.pos);
973
- return this.pos += 4, e;
974
- }
975
- readU64() {
976
- const e = Y(this.view, this.pos);
977
- return this.pos += 8, e;
978
- }
979
- readI64() {
980
- const e = M(this.view, this.pos);
981
- return this.pos += 8, e;
982
- }
983
- readU64AsBigInt() {
984
- const e = this.view.getBigUint64(this.pos);
985
- return this.pos += 8, e;
986
- }
987
- readI64AsBigInt() {
988
- const e = this.view.getBigInt64(this.pos);
989
- return this.pos += 8, e;
990
- }
991
- readF32() {
992
- const e = this.view.getFloat32(this.pos);
993
- return this.pos += 4, e;
994
- }
995
- readF64() {
996
- const e = this.view.getFloat64(this.pos);
997
- return this.pos += 8, e;
998
- }
999
- }
1000
- function ge(n, e) {
1001
- return new E(e).decode(n);
1002
- }
1003
- const ye = /* @__PURE__ */ new Set([
1004
- "auth",
1005
- "open",
1006
- "send",
1007
- "sync",
1008
- "history",
1009
- "read",
1010
- "typing",
1011
- "react",
1012
- "edit",
1013
- "delete",
1014
- "invoke",
1015
- "pubkey",
1016
- "ping",
1017
- "annotate",
1018
- "annotate_clear",
1019
- "uploadPrekeys",
1020
- "fetchPrekey",
1021
- "assign",
1022
- "tag",
1023
- "note",
1024
- "agent_status",
1025
- "subscribe_inbox",
1026
- "unsubscribe_inbox"
1027
- ]);
1028
- function we(n) {
1029
- return ye.has(n.type);
1030
- }
1031
- function pe(n) {
1032
- return he(n);
1033
- }
1034
- function Ue(n) {
1035
- let e;
1036
- try {
1037
- e = ge(n);
1038
- } catch {
1039
- return null;
1040
- }
1041
- return typeof e != "object" || e === null || typeof e.type != "string" ? null : e;
1042
- }
1043
- class De {
1044
- constructor(e) {
1045
- r(this, "socket", null);
1046
- r(this, "state", "idle");
1047
- r(this, "authed", !1);
1048
- r(this, "everAuthed", !1);
1049
- r(this, "attempt", 0);
1050
- r(this, "outbox", []);
1051
- r(this, "stopped", !1);
1052
- r(this, "timer", null);
1053
- r(this, "refreshing", !1);
1054
- this.opts = e;
1
+ var u = Object.defineProperty;
2
+ var l = (i, t, s) => t in i ? u(i, t, { enumerable: !0, configurable: !0, writable: !0, value: s }) : i[t] = s;
3
+ var n = (i, t, s) => l(i, typeof t != "symbol" ? t + "" : t, s);
4
+ import { d as p, i as g, e as y } from "./codec.js";
5
+ const I = (i) => i, C = (i) => i, _ = (i) => i, x = (i) => i, q = (i) => i, A = (i) => i, M = (i) => i, O = (i) => i, T = I("anonymous");
6
+ class R {
7
+ constructor(t) {
8
+ n(this, "socket", null);
9
+ n(this, "state", "idle");
10
+ n(this, "authed", !1);
11
+ n(this, "everAuthed", !1);
12
+ n(this, "attempt", 0);
13
+ n(this, "outbox", []);
14
+ n(this, "stopped", !1);
15
+ n(this, "timer", null);
16
+ n(this, "refreshing", !1);
17
+ this.opts = t;
1055
18
  }
1056
19
  connect() {
1057
- var s, i;
20
+ var e, o;
1058
21
  if (this.state === "connecting" || this.state === "open") return;
1059
- this.stopped = !1, this.state = "connecting", this.authed = !1, (i = (s = this.opts).onStatusChange) == null || i.call(s, this.attempt > 0 ? "reconnecting" : "connecting");
1060
- const t = (this.opts.socketFactory ?? me)(this.opts.url);
1061
- t.binaryType = "arraybuffer", this.socket = t, t.onopen = () => {
22
+ this.stopped = !1, this.state = "connecting", this.authed = !1, (o = (e = this.opts).onStatusChange) == null || o.call(e, this.attempt > 0 ? "reconnecting" : "connecting");
23
+ const s = (this.opts.socketFactory ?? k)(this.opts.url);
24
+ s.binaryType = "arraybuffer", this.socket = s, s.onopen = () => {
1062
25
  this.raw({ type: "auth", token: this.opts.token });
1063
- }, t.onmessage = (o) => {
1064
- const h = Ue(new Uint8Array(o.data));
1065
- !h || we(h) || this.handle(h);
1066
- }, t.onclose = () => this.onClosed(), t.onerror = () => {
26
+ }, s.onmessage = (a) => {
27
+ const h = p(new Uint8Array(a.data));
28
+ !h || g(h) || this.handle(h);
29
+ }, s.onclose = () => this.onClosed(), s.onerror = () => {
1067
30
  try {
1068
- t.close();
31
+ s.close();
1069
32
  } catch {
1070
33
  }
1071
34
  };
@@ -1073,213 +36,213 @@ class De {
1073
36
  /** Queue a frame; sent immediately if open, else flushed on (re)connect.
1074
37
  * The outbox is bounded so a prolonged outage can't grow memory without limit
1075
38
  * — oldest queued frames are dropped past the cap. */
1076
- send(e) {
39
+ send(t) {
1077
40
  if (this.state === "open" && this.authed) {
1078
- this.raw(e);
41
+ this.raw(t);
1079
42
  return;
1080
43
  }
1081
- const t = this.opts.maxOutbox ?? 1e3;
1082
- this.outbox.length >= t && (this.outbox = this.outbox.slice(this.outbox.length - (t >> 1))), this.outbox.push(e);
44
+ const s = this.opts.maxOutbox ?? 1e3;
45
+ this.outbox.length >= s && (this.outbox = this.outbox.slice(this.outbox.length - (s >> 1))), this.outbox.push(t);
1083
46
  }
1084
47
  close() {
1085
- var e;
48
+ var t;
1086
49
  this.stopped = !0, this.timer && clearTimeout(this.timer), this.state = "closed";
1087
50
  try {
1088
- (e = this.socket) == null || e.close();
51
+ (t = this.socket) == null || t.close();
1089
52
  } catch {
1090
53
  }
1091
54
  }
1092
- handle(e) {
1093
- var t, s, i, o;
1094
- if (e.type === "authed" && (this.attempt = 0, this.state = "open", this.authed = !0, this.everAuthed = !0, (s = (t = this.opts).onStatusChange) == null || s.call(t, "open"), this.raw(this.opts.open), this.flush()), e.type === "opened" && this.raw({ type: "sync", conversationId: e.conversation.id, sinceSeq: this.opts.getCursor() }), e.type === "error" && Se.has(e.code)) {
1095
- if (e.code === "UNAUTHORIZED" && this.opts.refreshToken && !this.refreshing) {
1096
- this.refreshing = !0, (o = (i = this.opts).onStatusChange) == null || o.call(i, "reconnecting", "Renewing session…"), this.opts.refreshToken().then((h) => {
1097
- var a;
55
+ handle(t) {
56
+ var s, e, o, a;
57
+ if (t.type === "authed" && (this.attempt = 0, this.state = "open", this.authed = !0, this.everAuthed = !0, (e = (s = this.opts).onStatusChange) == null || e.call(s, "open"), this.raw(this.opts.open), this.flush()), t.type === "opened" && this.raw({ type: "sync", conversationId: t.conversation.id, sinceSeq: this.opts.getCursor() }), t.type === "error" && b.has(t.code)) {
58
+ if (t.code === "UNAUTHORIZED" && this.opts.refreshToken && !this.refreshing) {
59
+ this.refreshing = !0, (a = (o = this.opts).onStatusChange) == null || a.call(o, "reconnecting", "Renewing session…"), this.opts.refreshToken().then((h) => {
60
+ var c;
1098
61
  if (this.refreshing = !1, !h) {
1099
- this.fatal(e);
62
+ this.fatal(t);
1100
63
  return;
1101
64
  }
1102
65
  this.opts.token = h;
1103
66
  try {
1104
- (a = this.socket) == null || a.close();
67
+ (c = this.socket) == null || c.close();
1105
68
  } catch {
1106
69
  }
1107
70
  }).catch(() => {
1108
- this.refreshing = !1, this.fatal(e);
71
+ this.refreshing = !1, this.fatal(t);
1109
72
  });
1110
73
  return;
1111
74
  }
1112
- this.fatal(e);
75
+ this.fatal(t);
1113
76
  return;
1114
77
  }
1115
- this.opts.onFrame(e);
78
+ this.opts.onFrame(t);
1116
79
  }
1117
- fatal(e) {
1118
- var t, s, i;
80
+ fatal(t) {
81
+ var s, e, o;
1119
82
  this.stopped = !0;
1120
83
  try {
1121
- (t = this.socket) == null || t.close();
84
+ (s = this.socket) == null || s.close();
1122
85
  } catch {
1123
86
  }
1124
- this.state = "closed", (i = (s = this.opts).onStatusChange) == null || i.call(s, "error", Ie(e.code)), this.opts.onFrame(e);
87
+ this.state = "closed", (o = (e = this.opts).onStatusChange) == null || o.call(e, "error", f(t.code)), this.opts.onFrame(t);
1125
88
  }
1126
89
  flush() {
1127
- const e = this.outbox;
90
+ const t = this.outbox;
1128
91
  this.outbox = [];
1129
- for (const t of e) this.raw(t);
92
+ for (const s of t) this.raw(s);
1130
93
  }
1131
- raw(e) {
1132
- var t;
94
+ raw(t) {
95
+ var s;
1133
96
  try {
1134
- (t = this.socket) == null || t.send(pe(e));
97
+ (s = this.socket) == null || s.send(y(t));
1135
98
  } catch {
1136
- this.outbox.push(e);
99
+ this.outbox.push(t);
1137
100
  }
1138
101
  }
1139
102
  onClosed() {
1140
- var i, o;
103
+ var o, a;
1141
104
  if (this.authed = !1, this.socket = null, this.stopped) {
1142
105
  this.state = "closed";
1143
106
  return;
1144
107
  }
1145
108
  this.state = "idle";
1146
- const e = this.opts.backoffBaseMs ?? 500, t = this.opts.backoffMaxMs ?? 15e3, s = Math.min(t, e * 2 ** this.attempt) * (0.5 + Math.random() * 0.5);
1147
- this.attempt++, this.attempt >= 3 && !this.everAuthed && ((o = (i = this.opts).onStatusChange) == null || o.call(i, "reconnecting", "Can't reach chat — retrying…")), this.timer = setTimeout(() => this.connect(), s);
109
+ const t = this.opts.backoffBaseMs ?? 500, s = this.opts.backoffMaxMs ?? 15e3, e = Math.min(s, t * 2 ** this.attempt) * (0.5 + Math.random() * 0.5);
110
+ this.attempt++, this.attempt >= 3 && !this.everAuthed && ((a = (o = this.opts).onStatusChange) == null || a.call(o, "reconnecting", "Can't reach chat — retrying…")), this.timer = setTimeout(() => this.connect(), e);
1148
111
  }
1149
112
  }
1150
- const Se = /* @__PURE__ */ new Set(["UNAUTHORIZED"]);
1151
- function Ie(n) {
1152
- switch (n) {
113
+ const b = /* @__PURE__ */ new Set(["UNAUTHORIZED"]);
114
+ function f(i) {
115
+ switch (i) {
1153
116
  case "UNAUTHORIZED":
1154
117
  return "Chat unavailable — sign-in/token was rejected";
1155
118
  default:
1156
119
  return "Chat unavailable";
1157
120
  }
1158
121
  }
1159
- function me(n) {
1160
- return new WebSocket(n);
1161
- }
1162
- class ze {
1163
- constructor(e) {
1164
- r(this, "conversationId");
1165
- r(this, "state", "");
1166
- r(this, "version", 0);
1167
- r(this, "hasMoreHistory", !1);
1168
- r(this, "lastReadByOthers", 0);
1169
- r(this, "assignedAgentId");
1170
- r(this, "accent");
1171
- r(this, "subject");
1172
- r(this, "name");
1173
- r(this, "e2e", !1);
1174
- r(this, "offline", !1);
1175
- r(this, "offlineMessage", "");
1176
- r(this, "preChat", null);
1177
- r(this, "whiteLabel", !1);
1178
- r(this, "typing", /* @__PURE__ */ new Set());
1179
- r(this, "online", /* @__PURE__ */ new Set());
122
+ function k(i) {
123
+ return new WebSocket(i);
124
+ }
125
+ class j {
126
+ constructor(t) {
127
+ n(this, "conversationId");
128
+ n(this, "state", "");
129
+ n(this, "version", 0);
130
+ n(this, "hasMoreHistory", !1);
131
+ n(this, "lastReadByOthers", 0);
132
+ n(this, "assignedAgentId");
133
+ n(this, "accent");
134
+ n(this, "subject");
135
+ n(this, "name");
136
+ n(this, "e2e", !1);
137
+ n(this, "offline", !1);
138
+ n(this, "offlineMessage", "");
139
+ n(this, "preChat", null);
140
+ n(this, "whiteLabel", !1);
141
+ n(this, "typing", /* @__PURE__ */ new Set());
142
+ n(this, "online", /* @__PURE__ */ new Set());
1180
143
  /** Co-browsing: shared whiteboard strokes for this conversation. */
1181
- r(this, "annotations", []);
144
+ n(this, "annotations", []);
1182
145
  /** Bumped on any annotation change so the renderer can cheaply detect updates. */
1183
- r(this, "annotationVersion", 0);
146
+ n(this, "annotationVersion", 0);
1184
147
  /** Live sentiment of the guest's latest message (agent-side only). */
1185
- r(this, "sentiment");
1186
- r(this, "sentimentScore");
1187
- r(this, "actions", []);
1188
- r(this, "byId", /* @__PURE__ */ new Map());
1189
- r(this, "keyByClient", /* @__PURE__ */ new Map());
1190
- r(this, "_maxSeq", 0);
1191
- r(this, "_sorted", null);
1192
- this.me = e;
148
+ n(this, "sentiment");
149
+ n(this, "sentimentScore");
150
+ n(this, "actions", []);
151
+ n(this, "byId", /* @__PURE__ */ new Map());
152
+ n(this, "keyByClient", /* @__PURE__ */ new Map());
153
+ n(this, "_maxSeq", 0);
154
+ n(this, "_sorted", null);
155
+ this.me = t;
1193
156
  }
1194
157
  messages() {
1195
- return this._sorted || (this._sorted = [...this.byId.values()].sort((e, t) => {
1196
- const s = e.status === "pending", i = t.status === "pending";
1197
- return s !== i ? s ? 1 : -1 : s && i ? e.ts - t.ts : e.seq - t.seq;
158
+ return this._sorted || (this._sorted = [...this.byId.values()].sort((t, s) => {
159
+ const e = t.status === "pending", o = s.status === "pending";
160
+ return e !== o ? e ? 1 : -1 : e && o ? t.ts - s.ts : t.seq - s.seq;
1198
161
  })), this._sorted;
1199
162
  }
1200
163
  visibleActions() {
1201
- return this.actions.filter((e) => !e.availableInStates || e.availableInStates.includes(this.state));
164
+ return this.actions.filter((t) => !t.availableInStates || t.availableInStates.includes(this.state));
1202
165
  }
1203
166
  highestSeq() {
1204
167
  return this._maxSeq;
1205
168
  }
1206
- addOptimistic(e, t) {
1207
- const s = {
1208
- id: e,
169
+ addOptimistic(t, s) {
170
+ const e = {
171
+ id: t,
1209
172
  conversationId: this.conversationId,
1210
173
  seq: 0,
1211
174
  senderId: this.me,
1212
175
  senderRole: "guest",
1213
- content: t,
176
+ content: s,
1214
177
  ts: Date.now(),
1215
- clientMsgId: e,
178
+ clientMsgId: t,
1216
179
  status: "pending"
1217
180
  };
1218
- return this.byId.set(e, s), this.keyByClient.set(e, e), this._sorted = null, s;
181
+ return this.byId.set(t, e), this.keyByClient.set(t, t), this._sorted = null, e;
1219
182
  }
1220
- apply(e) {
1221
- var t;
1222
- switch (e.type) {
183
+ apply(t) {
184
+ var s;
185
+ switch (t.type) {
1223
186
  case "opened":
1224
- this.conversationId = e.conversation.id, this.state = e.conversation.state, e.subject && (this.subject = e.subject), e.annotations && (this.annotations = e.annotations, this.annotationVersion++);
187
+ this.conversationId = t.conversation.id, this.state = t.conversation.state, t.subject && (this.subject = t.subject), t.annotations && (this.annotations = t.annotations, this.annotationVersion++);
1225
188
  return;
1226
189
  case "manifest":
1227
- this.actions = e.actions, this.version = e.version, e.name && (this.name = e.name), (t = e.theme) != null && t.accent && (this.accent = e.theme.accent), e.e2e && (this.e2e = !0), e.offline && (this.offline = !0), e.offlineMessage && (this.offlineMessage = e.offlineMessage), e.whiteLabel && (this.whiteLabel = !0), e.preChat && (this.preChat = e.preChat);
190
+ this.actions = t.actions, this.version = t.version, t.name && (this.name = t.name), (s = t.theme) != null && s.accent && (this.accent = t.theme.accent), t.e2e && (this.e2e = !0), t.offline && (this.offline = !0), t.offlineMessage && (this.offlineMessage = t.offlineMessage), t.whiteLabel && (this.whiteLabel = !0), t.preChat && (this.preChat = t.preChat);
1228
191
  return;
1229
192
  case "message":
1230
- this.upsert({ ...e.message });
193
+ this.upsert({ ...t.message });
1231
194
  return;
1232
195
  case "ack": {
1233
- const s = this.keyByClient.get(e.clientMsgId), i = s ? this.byId.get(s) : void 0;
1234
- if (i && s) {
1235
- this.byId.delete(s);
1236
- const o = { ...i, id: e.messageId, seq: e.seq, ts: e.ts, status: "sent" };
1237
- this.byId.set(e.messageId, o), this.keyByClient.set(e.clientMsgId, e.messageId), e.seq > this._maxSeq && (this._maxSeq = e.seq);
196
+ const e = this.keyByClient.get(t.clientMsgId), o = e ? this.byId.get(e) : void 0;
197
+ if (o && e) {
198
+ this.byId.delete(e);
199
+ const a = { ...o, id: t.messageId, seq: t.seq, ts: t.ts, status: "sent" };
200
+ this.byId.set(t.messageId, a), this.keyByClient.set(t.clientMsgId, t.messageId), t.seq > this._maxSeq && (this._maxSeq = t.seq);
1238
201
  }
1239
202
  this._sorted = null;
1240
203
  return;
1241
204
  }
1242
205
  case "delivered":
1243
- this.markOwnStatus(e.seq, "delivered");
206
+ this.markOwnStatus(t.seq, "delivered");
1244
207
  return;
1245
208
  case "read":
1246
- e.by !== this.me && (this.lastReadByOthers = Math.max(this.lastReadByOthers, e.seq), this.markOwnStatus(e.seq, "read"));
209
+ t.by !== this.me && (this.lastReadByOthers = Math.max(this.lastReadByOthers, t.seq), this.markOwnStatus(t.seq, "read"));
1247
210
  return;
1248
211
  case "sync":
1249
- for (const s of e.messages) this.upsert({ ...s });
212
+ for (const e of t.messages) this.upsert({ ...e });
1250
213
  return;
1251
214
  case "history":
1252
- for (const s of e.messages) this.upsert({ ...s });
1253
- this.hasMoreHistory = e.hasMore;
215
+ for (const e of t.messages) this.upsert({ ...e });
216
+ this.hasMoreHistory = t.hasMore;
1254
217
  return;
1255
218
  case "typing":
1256
- e.userId !== this.me && (e.isTyping ? this.typing.add(e.userId) : this.typing.delete(e.userId));
219
+ t.userId !== this.me && (t.isTyping ? this.typing.add(t.userId) : this.typing.delete(t.userId));
1257
220
  return;
1258
221
  case "reaction": {
1259
- const s = this.byId.get(e.messageId);
1260
- if (!s) return;
1261
- const i = { ...s.reactions ?? {} }, o = (i[e.emoji] ?? []).filter((h) => h !== e.by);
1262
- e.removed || o.push(e.by), o.length ? i[e.emoji] = o : delete i[e.emoji], this.byId.set(e.messageId, { ...s, reactions: i }), this._sorted = null;
222
+ const e = this.byId.get(t.messageId);
223
+ if (!e) return;
224
+ const o = { ...e.reactions ?? {} }, a = (o[t.emoji] ?? []).filter((h) => h !== t.by);
225
+ t.removed || a.push(t.by), a.length ? o[t.emoji] = a : delete o[t.emoji], this.byId.set(t.messageId, { ...e, reactions: o }), this._sorted = null;
1263
226
  return;
1264
227
  }
1265
228
  case "edited": {
1266
- const s = this.byId.get(e.messageId);
1267
- s && (this.byId.set(e.messageId, { ...s, content: e.content, editedAt: e.editedAt }), this._sorted = null);
229
+ const e = this.byId.get(t.messageId);
230
+ e && (this.byId.set(t.messageId, { ...e, content: t.content, editedAt: t.editedAt }), this._sorted = null);
1268
231
  return;
1269
232
  }
1270
233
  case "deleted": {
1271
- const s = this.byId.get(e.messageId);
1272
- s && (this.byId.set(e.messageId, { ...s, deletedAt: e.ts }), this._sorted = null);
234
+ const e = this.byId.get(t.messageId);
235
+ e && (this.byId.set(t.messageId, { ...e, deletedAt: t.ts }), this._sorted = null);
1273
236
  return;
1274
237
  }
1275
238
  case "state":
1276
- this.state = e.state;
239
+ this.state = t.state;
1277
240
  return;
1278
241
  case "assigned":
1279
- this.assignedAgentId = e.agentId ?? void 0;
242
+ this.assignedAgentId = t.agentId ?? void 0;
1280
243
  return;
1281
244
  case "presence":
1282
- e.status === "online" ? this.online.add(e.userId) : this.online.delete(e.userId);
245
+ t.status === "online" ? this.online.add(t.userId) : this.online.delete(t.userId);
1283
246
  return;
1284
247
  case "subjectState":
1285
248
  case "invoked":
@@ -1288,32 +251,32 @@ class ze {
1288
251
  case "pong":
1289
252
  return;
1290
253
  case "annotation":
1291
- this.annotations = [...this.annotations, e.stroke], this.annotationVersion++;
254
+ this.annotations = [...this.annotations, t.stroke], this.annotationVersion++;
1292
255
  return;
1293
256
  case "annotation_clear":
1294
257
  this.annotations = [], this.annotationVersion++;
1295
258
  return;
1296
259
  case "sentiment":
1297
- this.sentiment = e.label, this.sentimentScore = e.score;
260
+ this.sentiment = t.label, this.sentimentScore = t.score;
1298
261
  return;
1299
262
  default:
1300
263
  return;
1301
264
  }
1302
265
  }
1303
- upsert(e) {
1304
- const t = this.byId.get(e.id);
1305
- this.byId.set(e.id, t ? { ...t, ...e } : e), e.seq > this._maxSeq && (this._maxSeq = e.seq), this._sorted = null;
266
+ upsert(t) {
267
+ const s = this.byId.get(t.id);
268
+ this.byId.set(t.id, s ? { ...s, ...t } : t), t.seq > this._maxSeq && (this._maxSeq = t.seq), this._sorted = null;
1306
269
  }
1307
- markOwnStatus(e, t) {
1308
- const s = T(t);
1309
- let i = !1;
1310
- for (const [o, h] of this.byId)
1311
- h.senderId !== this.me || h.seq <= 0 || h.seq > e || T(h.status) >= s || (this.byId.set(o, { ...h, status: t }), i = !0);
1312
- i && (this._sorted = null);
270
+ markOwnStatus(t, s) {
271
+ const e = r(s);
272
+ let o = !1;
273
+ for (const [a, h] of this.byId)
274
+ h.senderId !== this.me || h.seq <= 0 || h.seq > t || r(h.status) >= e || (this.byId.set(a, { ...h, status: s }), o = !0);
275
+ o && (this._sorted = null);
1313
276
  }
1314
277
  }
1315
- function T(n) {
1316
- switch (n) {
278
+ function r(i) {
279
+ switch (i) {
1317
280
  case "read":
1318
281
  return 3;
1319
282
  case "delivered":
@@ -1324,60 +287,57 @@ function T(n) {
1324
287
  return 0;
1325
288
  }
1326
289
  }
1327
- const k = 200, Be = 7 * 864e5;
1328
- class Le {
1329
- constructor(e) {
1330
- r(this, "key");
1331
- this.key = `ocw_outbox_${e}`;
290
+ const d = 200, S = 7 * 864e5;
291
+ class m {
292
+ constructor(t) {
293
+ n(this, "key");
294
+ this.key = `ocw_outbox_${t}`;
1332
295
  }
1333
296
  /** All pending items, oldest first, with stale (>7d) entries dropped. */
1334
297
  load() {
1335
298
  try {
1336
- const e = localStorage.getItem(this.key);
1337
- if (!e) return [];
1338
- const t = JSON.parse(e), s = Date.now() - Be, i = t.filter((o) => o.ts >= s);
1339
- return i.length !== t.length && this.save(i), i;
299
+ const t = localStorage.getItem(this.key);
300
+ if (!t) return [];
301
+ const s = JSON.parse(t), e = Date.now() - S, o = s.filter((a) => a.ts >= e);
302
+ return o.length !== s.length && this.save(o), o;
1340
303
  } catch {
1341
304
  return [];
1342
305
  }
1343
306
  }
1344
- add(e) {
307
+ add(t) {
1345
308
  try {
1346
- const t = this.load();
1347
- t.push(e), this.save(t.length > k ? t.slice(t.length - k) : t);
309
+ const s = this.load();
310
+ s.push(t), this.save(s.length > d ? s.slice(s.length - d) : s);
1348
311
  } catch {
1349
312
  }
1350
313
  }
1351
314
  /** Remove an item once it's been acknowledged by the server. */
1352
- remove(e) {
315
+ remove(t) {
1353
316
  try {
1354
- const t = this.load().filter((s) => s.clientMsgId !== e);
1355
- this.save(t);
317
+ const s = this.load().filter((e) => e.clientMsgId !== t);
318
+ this.save(s);
1356
319
  } catch {
1357
320
  }
1358
321
  }
1359
- save(e) {
322
+ save(t) {
1360
323
  try {
1361
- localStorage.setItem(this.key, JSON.stringify(e));
324
+ localStorage.setItem(this.key, JSON.stringify(t));
1362
325
  } catch {
1363
326
  }
1364
327
  }
1365
328
  }
1366
329
  export {
1367
- Ce as A,
1368
- ze as C,
1369
- Le as P,
1370
- Ae as a,
1371
- De as b,
1372
- be as c,
1373
- _e as d,
1374
- Te as e,
1375
- ve as f,
1376
- Me as g,
1377
- ke as h,
1378
- L as i,
1379
- Ue as j,
1380
- pe as k,
1381
- we as l
330
+ T as A,
331
+ j as C,
332
+ m as P,
333
+ C as a,
334
+ R as b,
335
+ A as c,
336
+ O as d,
337
+ _ as e,
338
+ q as f,
339
+ M as g,
340
+ x as h,
341
+ I as i
1382
342
  };
1383
343
  //# sourceMappingURL=outbox.js.map