@paramms/chat-widget 1.0.35 → 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/codec.js ADDED
@@ -0,0 +1,1135 @@
1
+ var F = Object.defineProperty;
2
+ var P = (n, e, t) => e in n ? F(n, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : n[e] = t;
3
+ var h = (n, e, t) => P(n, typeof e != "symbol" ? e + "" : e, t);
4
+ function R(n, e) {
5
+ var c;
6
+ const t = n.trim().replace(/\/+$/, ""), s = (c = t.match(/^(https|http|wss|ws):\/\//)) == null ? void 0 : c[1], i = s ? s === "https" || s === "wss" : !0, r = (s ? t.slice(s.length + 3) : t).replace(/\/ws$/, ""), o = e ? e.trim().replace(/\/+$/, "") : `${i ? "https" : "http"}://${r}`;
7
+ return { wsUrl: `${i ? "wss" : "ws"}://${r}/ws`, httpBase: o };
8
+ }
9
+ function H(n) {
10
+ return R(n).httpBase;
11
+ }
12
+ function b(n, e, t, s) {
13
+ const i = `beforeSeq=${t}&limit=${s}`;
14
+ return [
15
+ `${n}/conversations/${e}/messages?${i}`,
16
+ `${n}/conversations/${e}/history?${i}`
17
+ ];
18
+ }
19
+ async function z(n, e, t, s, i = 20) {
20
+ for (const r of b(n, e, s, i))
21
+ try {
22
+ const o = await fetch(r, { headers: { authorization: `Bearer ${t}` } });
23
+ if (!o.ok) continue;
24
+ const a = await o.json();
25
+ return { messages: a.messages ?? [], hasMore: a.hasMore ?? !1 };
26
+ } catch {
27
+ }
28
+ return null;
29
+ }
30
+ async function Te(n, e, t, s, i, r) {
31
+ const o = r ? r.replace(/\/+$/, "") : H(n), a = await z(o, t, e, Number.MAX_SAFE_INTEGER);
32
+ if (!a || (a.messages.length ? (s.apply({ type: "sync", conversationId: t, messages: a.messages }), s.apply({ type: "history", conversationId: t, messages: [], hasMore: a.hasMore }), i.render(s)) : s.apply({ type: "history", conversationId: t, messages: [], hasMore: !1 }), !a.hasMore)) return;
33
+ let c = !1;
34
+ const u = async () => {
35
+ if (c || !s.hasMoreHistory) return;
36
+ c = !0;
37
+ const v = s.messages()[0];
38
+ if (!v) {
39
+ c = !1;
40
+ return;
41
+ }
42
+ const S = await z(o, t, e, v.seq);
43
+ S && (s.apply({ type: "history", conversationId: t, messages: S.messages, hasMore: S.hasMore }), i.render(s)), c = !1;
44
+ }, l = i.getScrollEl();
45
+ if (!l) return;
46
+ let d = !1;
47
+ setTimeout(() => {
48
+ d = !0;
49
+ }, 300);
50
+ const M = () => {
51
+ d && l.scrollTop < 80 && s.hasMoreHistory && !c && u();
52
+ };
53
+ l.addEventListener("scroll", M, { passive: !0 }), i.setScrollCleanup(() => l.removeEventListener("scroll", M));
54
+ }
55
+ const y = "oc_uid";
56
+ function K(n) {
57
+ try {
58
+ const e = document.cookie.match(new RegExp(`(?:^|; )${n}=([^;]*)`));
59
+ return e ? decodeURIComponent(e[1]) : null;
60
+ } catch {
61
+ return null;
62
+ }
63
+ }
64
+ function N(n, e) {
65
+ try {
66
+ const s = location.protocol === "https:" ? "; Secure" : "";
67
+ document.cookie = `${n}=${encodeURIComponent(e)}; Max-Age=31536000; Path=/; SameSite=Lax${s}`;
68
+ } catch {
69
+ }
70
+ }
71
+ function W() {
72
+ return `g_${Math.random().toString(36).slice(2)}${Date.now().toString(36)}`;
73
+ }
74
+ function ke() {
75
+ let n = null;
76
+ try {
77
+ n = localStorage.getItem(y);
78
+ } catch {
79
+ }
80
+ n || (n = K(y));
81
+ const e = n ?? W();
82
+ try {
83
+ localStorage.setItem(y, e);
84
+ } catch {
85
+ }
86
+ return N(y, e), e;
87
+ }
88
+ function V(n) {
89
+ const e = n.length;
90
+ let t = 0, s = 0;
91
+ for (; s < e; ) {
92
+ let i = n.charCodeAt(s++);
93
+ if (i & 4294967168)
94
+ if (!(i & 4294965248))
95
+ t += 2;
96
+ else {
97
+ if (i >= 55296 && i <= 56319 && s < e) {
98
+ const r = n.charCodeAt(s);
99
+ (r & 64512) === 56320 && (++s, i = ((i & 1023) << 10) + (r & 1023) + 65536);
100
+ }
101
+ i & 4294901760 ? t += 4 : t += 3;
102
+ }
103
+ else {
104
+ t++;
105
+ continue;
106
+ }
107
+ }
108
+ return t;
109
+ }
110
+ function X(n, e, t) {
111
+ const s = n.length;
112
+ let i = t, r = 0;
113
+ for (; r < s; ) {
114
+ let o = n.charCodeAt(r++);
115
+ if (o & 4294967168)
116
+ if (!(o & 4294965248))
117
+ e[i++] = o >> 6 & 31 | 192;
118
+ else {
119
+ if (o >= 55296 && o <= 56319 && r < s) {
120
+ const a = n.charCodeAt(r);
121
+ (a & 64512) === 56320 && (++r, o = ((o & 1023) << 10) + (a & 1023) + 65536);
122
+ }
123
+ o & 4294901760 ? (e[i++] = o >> 18 & 7 | 240, e[i++] = o >> 12 & 63 | 128, e[i++] = o >> 6 & 63 | 128) : (e[i++] = o >> 12 & 15 | 224, e[i++] = o >> 6 & 63 | 128);
124
+ }
125
+ else {
126
+ e[i++] = o;
127
+ continue;
128
+ }
129
+ e[i++] = o & 63 | 128;
130
+ }
131
+ }
132
+ const Y = new TextEncoder(), q = 50;
133
+ function G(n, e, t) {
134
+ Y.encodeInto(n, e.subarray(t));
135
+ }
136
+ function O(n, e, t) {
137
+ n.length > q ? G(n, e, t) : X(n, e, t);
138
+ }
139
+ const J = 4096;
140
+ function D(n, e, t) {
141
+ let s = e;
142
+ const i = s + t, r = [];
143
+ let o = "";
144
+ for (; s < i; ) {
145
+ const a = n[s++];
146
+ if (!(a & 128))
147
+ r.push(a);
148
+ else if ((a & 224) === 192) {
149
+ const c = n[s++] & 63;
150
+ r.push((a & 31) << 6 | c);
151
+ } else if ((a & 240) === 224) {
152
+ const c = n[s++] & 63, u = n[s++] & 63;
153
+ r.push((a & 31) << 12 | c << 6 | u);
154
+ } else if ((a & 248) === 240) {
155
+ const c = n[s++] & 63, u = n[s++] & 63, l = n[s++] & 63;
156
+ let d = (a & 7) << 18 | c << 12 | u << 6 | l;
157
+ d > 65535 && (d -= 65536, r.push(d >>> 10 & 1023 | 55296), d = 56320 | d & 1023), r.push(d);
158
+ } else
159
+ r.push(a);
160
+ r.length >= J && (o += String.fromCharCode(...r), r.length = 0);
161
+ }
162
+ return r.length > 0 && (o += String.fromCharCode(...r)), o;
163
+ }
164
+ const Z = new TextDecoder(), Q = 200;
165
+ function j(n, e, t) {
166
+ const s = n.subarray(e, e + t);
167
+ return Z.decode(s);
168
+ }
169
+ function ee(n, e, t) {
170
+ return t > Q ? j(n, e, t) : D(n, e, t);
171
+ }
172
+ class U {
173
+ constructor(e, t) {
174
+ h(this, "type");
175
+ h(this, "data");
176
+ this.type = e, this.data = t;
177
+ }
178
+ }
179
+ class f extends Error {
180
+ constructor(e) {
181
+ super(e);
182
+ const t = Object.create(f.prototype);
183
+ Object.setPrototypeOf(this, t), Object.defineProperty(this, "name", {
184
+ configurable: !0,
185
+ enumerable: !1,
186
+ value: f.name
187
+ });
188
+ }
189
+ }
190
+ const x = 4294967295;
191
+ function te(n, e, t) {
192
+ const s = t / 4294967296, i = t;
193
+ n.setUint32(e, s), n.setUint32(e + 4, i);
194
+ }
195
+ function $(n, e, t) {
196
+ const s = Math.floor(t / 4294967296), i = t;
197
+ n.setUint32(e, s), n.setUint32(e + 4, i);
198
+ }
199
+ function C(n, e) {
200
+ const t = n.getInt32(e), s = n.getUint32(e + 4);
201
+ return t * 4294967296 + s;
202
+ }
203
+ function se(n, e) {
204
+ const t = n.getUint32(e), s = n.getUint32(e + 4);
205
+ return t * 4294967296 + s;
206
+ }
207
+ const ie = -1, ne = 4294967296 - 1, re = 17179869184 - 1;
208
+ function he({ sec: n, nsec: e }) {
209
+ if (n >= 0 && e >= 0 && n <= re)
210
+ if (e === 0 && n <= ne) {
211
+ const t = new Uint8Array(4);
212
+ return new DataView(t.buffer).setUint32(0, n), t;
213
+ } else {
214
+ const t = n / 4294967296, s = n & 4294967295, i = new Uint8Array(8), r = new DataView(i.buffer);
215
+ return r.setUint32(0, e << 2 | t & 3), r.setUint32(4, s), i;
216
+ }
217
+ else {
218
+ const t = new Uint8Array(12), s = new DataView(t.buffer);
219
+ return s.setUint32(0, e), $(s, 4, n), t;
220
+ }
221
+ }
222
+ function oe(n) {
223
+ const e = n.getTime(), t = Math.floor(e / 1e3), s = (e - t * 1e3) * 1e6, i = Math.floor(s / 1e9);
224
+ return {
225
+ sec: t + i,
226
+ nsec: s - i * 1e9
227
+ };
228
+ }
229
+ function ae(n) {
230
+ if (n instanceof Date) {
231
+ const e = oe(n);
232
+ return he(e);
233
+ } else
234
+ return null;
235
+ }
236
+ function ce(n) {
237
+ const e = new DataView(n.buffer, n.byteOffset, n.byteLength);
238
+ switch (n.byteLength) {
239
+ case 4:
240
+ return { sec: e.getUint32(0), nsec: 0 };
241
+ case 8: {
242
+ const t = e.getUint32(0), s = e.getUint32(4), i = (t & 3) * 4294967296 + s, r = t >>> 2;
243
+ return { sec: i, nsec: r };
244
+ }
245
+ case 12: {
246
+ const t = C(e, 4), s = e.getUint32(0);
247
+ return { sec: t, nsec: s };
248
+ }
249
+ default:
250
+ throw new f(`Unrecognized data size for timestamp (expected 4, 8, or 12): ${n.length}`);
251
+ }
252
+ }
253
+ function fe(n) {
254
+ const e = ce(n);
255
+ return new Date(e.sec * 1e3 + e.nsec / 1e6);
256
+ }
257
+ const de = {
258
+ type: ie,
259
+ encode: ae,
260
+ decode: fe
261
+ }, p = class p {
262
+ constructor() {
263
+ // ensures ExtensionCodecType<X> matches ExtensionCodec<X>
264
+ // this will make type errors a lot more clear
265
+ // eslint-disable-next-line @typescript-eslint/naming-convention
266
+ h(this, "__brand");
267
+ // built-in extensions
268
+ h(this, "builtInEncoders", []);
269
+ h(this, "builtInDecoders", []);
270
+ // custom extensions
271
+ h(this, "encoders", []);
272
+ h(this, "decoders", []);
273
+ this.register(de);
274
+ }
275
+ register({ type: e, encode: t, decode: s }) {
276
+ if (e >= 0)
277
+ this.encoders[e] = t, this.decoders[e] = s;
278
+ else {
279
+ const i = -1 - e;
280
+ this.builtInEncoders[i] = t, this.builtInDecoders[i] = s;
281
+ }
282
+ }
283
+ tryToEncode(e, t) {
284
+ for (let s = 0; s < this.builtInEncoders.length; s++) {
285
+ const i = this.builtInEncoders[s];
286
+ if (i != null) {
287
+ const r = i(e, t);
288
+ if (r != null) {
289
+ const o = -1 - s;
290
+ return new U(o, r);
291
+ }
292
+ }
293
+ }
294
+ for (let s = 0; s < this.encoders.length; s++) {
295
+ const i = this.encoders[s];
296
+ if (i != null) {
297
+ const r = i(e, t);
298
+ if (r != null) {
299
+ const o = s;
300
+ return new U(o, r);
301
+ }
302
+ }
303
+ }
304
+ return e instanceof U ? e : null;
305
+ }
306
+ decode(e, t, s) {
307
+ const i = t < 0 ? this.builtInDecoders[-1 - t] : this.decoders[t];
308
+ return i ? i(e, t, s) : new U(t, e);
309
+ }
310
+ };
311
+ h(p, "defaultCodec", new p());
312
+ let m = p;
313
+ function le(n) {
314
+ return n instanceof ArrayBuffer || typeof SharedArrayBuffer < "u" && n instanceof SharedArrayBuffer;
315
+ }
316
+ function B(n) {
317
+ return n instanceof Uint8Array ? n : ArrayBuffer.isView(n) ? new Uint8Array(n.buffer, n.byteOffset, n.byteLength) : le(n) ? new Uint8Array(n) : Uint8Array.from(n);
318
+ }
319
+ const ue = 100, xe = 2048;
320
+ class I {
321
+ constructor(e) {
322
+ h(this, "extensionCodec");
323
+ h(this, "context");
324
+ h(this, "useBigInt64");
325
+ h(this, "maxDepth");
326
+ h(this, "initialBufferSize");
327
+ h(this, "sortKeys");
328
+ h(this, "forceFloat32");
329
+ h(this, "ignoreUndefined");
330
+ h(this, "forceIntegerToFloat");
331
+ h(this, "pos");
332
+ h(this, "view");
333
+ h(this, "bytes");
334
+ h(this, "entered", !1);
335
+ this.extensionCodec = (e == null ? void 0 : e.extensionCodec) ?? m.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) ?? ue, this.initialBufferSize = (e == null ? void 0 : e.initialBufferSize) ?? xe, 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);
336
+ }
337
+ clone() {
338
+ return new I({
339
+ extensionCodec: this.extensionCodec,
340
+ context: this.context,
341
+ useBigInt64: this.useBigInt64,
342
+ maxDepth: this.maxDepth,
343
+ initialBufferSize: this.initialBufferSize,
344
+ sortKeys: this.sortKeys,
345
+ forceFloat32: this.forceFloat32,
346
+ ignoreUndefined: this.ignoreUndefined,
347
+ forceIntegerToFloat: this.forceIntegerToFloat
348
+ });
349
+ }
350
+ reinitializeState() {
351
+ this.pos = 0;
352
+ }
353
+ /**
354
+ * 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}.
355
+ *
356
+ * @returns Encodes the object and returns a shared reference the encoder's internal buffer.
357
+ */
358
+ encodeSharedRef(e) {
359
+ if (this.entered)
360
+ return this.clone().encodeSharedRef(e);
361
+ try {
362
+ return this.entered = !0, this.reinitializeState(), this.doEncode(e, 1), this.bytes.subarray(0, this.pos);
363
+ } finally {
364
+ this.entered = !1;
365
+ }
366
+ }
367
+ /**
368
+ * @returns Encodes the object and returns a copy of the encoder's internal buffer.
369
+ */
370
+ encode(e) {
371
+ if (this.entered)
372
+ return this.clone().encode(e);
373
+ try {
374
+ return this.entered = !0, this.reinitializeState(), this.doEncode(e, 1), this.bytes.slice(0, this.pos);
375
+ } finally {
376
+ this.entered = !1;
377
+ }
378
+ }
379
+ doEncode(e, t) {
380
+ if (t > this.maxDepth)
381
+ throw new Error(`Too deep objects in depth ${t}`);
382
+ 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);
383
+ }
384
+ ensureBufferSizeToWrite(e) {
385
+ const t = this.pos + e;
386
+ this.view.byteLength < t && this.resizeBuffer(t * 2);
387
+ }
388
+ resizeBuffer(e) {
389
+ const t = new ArrayBuffer(e), s = new Uint8Array(t), i = new DataView(t);
390
+ s.set(this.bytes), this.view = i, this.bytes = s;
391
+ }
392
+ encodeNil() {
393
+ this.writeU8(192);
394
+ }
395
+ encodeBoolean(e) {
396
+ e === !1 ? this.writeU8(194) : this.writeU8(195);
397
+ }
398
+ encodeNumber(e) {
399
+ !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);
400
+ }
401
+ encodeNumberAsFloat(e) {
402
+ this.forceFloat32 ? (this.writeU8(202), this.writeF32(e)) : (this.writeU8(203), this.writeF64(e));
403
+ }
404
+ encodeBigInt64(e) {
405
+ e >= BigInt(0) ? (this.writeU8(207), this.writeBigUint64(e)) : (this.writeU8(211), this.writeBigInt64(e));
406
+ }
407
+ writeStringHeader(e) {
408
+ if (e < 32)
409
+ this.writeU8(160 + e);
410
+ else if (e < 256)
411
+ this.writeU8(217), this.writeU8(e);
412
+ else if (e < 65536)
413
+ this.writeU8(218), this.writeU16(e);
414
+ else if (e < 4294967296)
415
+ this.writeU8(219), this.writeU32(e);
416
+ else
417
+ throw new Error(`Too long string: ${e} bytes in UTF-8`);
418
+ }
419
+ encodeString(e) {
420
+ const s = V(e);
421
+ this.ensureBufferSizeToWrite(5 + s), this.writeStringHeader(s), O(e, this.bytes, this.pos), this.pos += s;
422
+ }
423
+ encodeObject(e, t) {
424
+ const s = this.extensionCodec.tryToEncode(e, this.context);
425
+ if (s != null)
426
+ this.encodeExtension(s);
427
+ else if (Array.isArray(e))
428
+ this.encodeArray(e, t);
429
+ else if (ArrayBuffer.isView(e))
430
+ this.encodeBinary(e);
431
+ else if (typeof e == "object")
432
+ this.encodeMap(e, t);
433
+ else
434
+ throw new Error(`Unrecognized object: ${Object.prototype.toString.apply(e)}`);
435
+ }
436
+ encodeBinary(e) {
437
+ const t = e.byteLength;
438
+ if (t < 256)
439
+ this.writeU8(196), this.writeU8(t);
440
+ else if (t < 65536)
441
+ this.writeU8(197), this.writeU16(t);
442
+ else if (t < 4294967296)
443
+ this.writeU8(198), this.writeU32(t);
444
+ else
445
+ throw new Error(`Too large binary: ${t}`);
446
+ const s = B(e);
447
+ this.writeU8a(s);
448
+ }
449
+ encodeArray(e, t) {
450
+ const s = e.length;
451
+ if (s < 16)
452
+ this.writeU8(144 + s);
453
+ else if (s < 65536)
454
+ this.writeU8(220), this.writeU16(s);
455
+ else if (s < 4294967296)
456
+ this.writeU8(221), this.writeU32(s);
457
+ else
458
+ throw new Error(`Too large array: ${s}`);
459
+ for (const i of e)
460
+ this.doEncode(i, t + 1);
461
+ }
462
+ countWithoutUndefined(e, t) {
463
+ let s = 0;
464
+ for (const i of t)
465
+ e[i] !== void 0 && s++;
466
+ return s;
467
+ }
468
+ encodeMap(e, t) {
469
+ const s = Object.keys(e);
470
+ this.sortKeys && s.sort();
471
+ const i = this.ignoreUndefined ? this.countWithoutUndefined(e, s) : s.length;
472
+ if (i < 16)
473
+ this.writeU8(128 + i);
474
+ else if (i < 65536)
475
+ this.writeU8(222), this.writeU16(i);
476
+ else if (i < 4294967296)
477
+ this.writeU8(223), this.writeU32(i);
478
+ else
479
+ throw new Error(`Too large map object: ${i}`);
480
+ for (const r of s) {
481
+ const o = e[r];
482
+ this.ignoreUndefined && o === void 0 || (this.encodeString(r), this.doEncode(o, t + 1));
483
+ }
484
+ }
485
+ encodeExtension(e) {
486
+ if (typeof e.data == "function") {
487
+ const s = e.data(this.pos + 6), i = s.length;
488
+ if (i >= 4294967296)
489
+ throw new Error(`Too large extension object: ${i}`);
490
+ this.writeU8(201), this.writeU32(i), this.writeI8(e.type), this.writeU8a(s);
491
+ return;
492
+ }
493
+ const t = e.data.length;
494
+ if (t === 1)
495
+ this.writeU8(212);
496
+ else if (t === 2)
497
+ this.writeU8(213);
498
+ else if (t === 4)
499
+ this.writeU8(214);
500
+ else if (t === 8)
501
+ this.writeU8(215);
502
+ else if (t === 16)
503
+ this.writeU8(216);
504
+ else if (t < 256)
505
+ this.writeU8(199), this.writeU8(t);
506
+ else if (t < 65536)
507
+ this.writeU8(200), this.writeU16(t);
508
+ else if (t < 4294967296)
509
+ this.writeU8(201), this.writeU32(t);
510
+ else
511
+ throw new Error(`Too large extension object: ${t}`);
512
+ this.writeI8(e.type), this.writeU8a(e.data);
513
+ }
514
+ writeU8(e) {
515
+ this.ensureBufferSizeToWrite(1), this.view.setUint8(this.pos, e), this.pos++;
516
+ }
517
+ writeU8a(e) {
518
+ const t = e.length;
519
+ this.ensureBufferSizeToWrite(t), this.bytes.set(e, this.pos), this.pos += t;
520
+ }
521
+ writeI8(e) {
522
+ this.ensureBufferSizeToWrite(1), this.view.setInt8(this.pos, e), this.pos++;
523
+ }
524
+ writeU16(e) {
525
+ this.ensureBufferSizeToWrite(2), this.view.setUint16(this.pos, e), this.pos += 2;
526
+ }
527
+ writeI16(e) {
528
+ this.ensureBufferSizeToWrite(2), this.view.setInt16(this.pos, e), this.pos += 2;
529
+ }
530
+ writeU32(e) {
531
+ this.ensureBufferSizeToWrite(4), this.view.setUint32(this.pos, e), this.pos += 4;
532
+ }
533
+ writeI32(e) {
534
+ this.ensureBufferSizeToWrite(4), this.view.setInt32(this.pos, e), this.pos += 4;
535
+ }
536
+ writeF32(e) {
537
+ this.ensureBufferSizeToWrite(4), this.view.setFloat32(this.pos, e), this.pos += 4;
538
+ }
539
+ writeF64(e) {
540
+ this.ensureBufferSizeToWrite(8), this.view.setFloat64(this.pos, e), this.pos += 8;
541
+ }
542
+ writeU64(e) {
543
+ this.ensureBufferSizeToWrite(8), te(this.view, this.pos, e), this.pos += 8;
544
+ }
545
+ writeI64(e) {
546
+ this.ensureBufferSizeToWrite(8), $(this.view, this.pos, e), this.pos += 8;
547
+ }
548
+ writeBigUint64(e) {
549
+ this.ensureBufferSizeToWrite(8), this.view.setBigUint64(this.pos, e), this.pos += 8;
550
+ }
551
+ writeBigInt64(e) {
552
+ this.ensureBufferSizeToWrite(8), this.view.setBigInt64(this.pos, e), this.pos += 8;
553
+ }
554
+ }
555
+ function we(n, e) {
556
+ return new I(e).encodeSharedRef(n);
557
+ }
558
+ function E(n) {
559
+ return `${n < 0 ? "-" : ""}0x${Math.abs(n).toString(16).padStart(2, "0")}`;
560
+ }
561
+ const ge = 16, ye = 16;
562
+ class Ue {
563
+ constructor(e = ge, t = ye) {
564
+ h(this, "hit", 0);
565
+ h(this, "miss", 0);
566
+ h(this, "caches");
567
+ h(this, "maxKeyLength");
568
+ h(this, "maxLengthPerKey");
569
+ this.maxKeyLength = e, this.maxLengthPerKey = t, this.caches = [];
570
+ for (let s = 0; s < this.maxKeyLength; s++)
571
+ this.caches.push([]);
572
+ }
573
+ canBeCached(e) {
574
+ return e > 0 && e <= this.maxKeyLength;
575
+ }
576
+ find(e, t, s) {
577
+ const i = this.caches[s - 1];
578
+ e: for (const r of i) {
579
+ const o = r.bytes;
580
+ for (let a = 0; a < s; a++)
581
+ if (o[a] !== e[t + a])
582
+ continue e;
583
+ return r.str;
584
+ }
585
+ return null;
586
+ }
587
+ store(e, t) {
588
+ const s = this.caches[e.length - 1], i = { bytes: e, str: t };
589
+ s.length >= this.maxLengthPerKey ? s[Math.random() * s.length | 0] = i : s.push(i);
590
+ }
591
+ decode(e, t, s) {
592
+ const i = this.find(e, t, s);
593
+ if (i != null)
594
+ return this.hit++, i;
595
+ this.miss++;
596
+ const r = D(e, t, s), o = Uint8Array.prototype.slice.call(e, t, t + s);
597
+ return this.store(o, r), r;
598
+ }
599
+ }
600
+ const A = "array", g = "map_key", _ = "map_value", me = (n) => {
601
+ if (typeof n == "string" || typeof n == "number")
602
+ return n;
603
+ throw new f("The type of key must be string or number but " + typeof n);
604
+ };
605
+ class pe {
606
+ constructor() {
607
+ h(this, "stack", []);
608
+ h(this, "stackHeadPosition", -1);
609
+ }
610
+ get length() {
611
+ return this.stackHeadPosition + 1;
612
+ }
613
+ top() {
614
+ return this.stack[this.stackHeadPosition];
615
+ }
616
+ pushArrayState(e) {
617
+ const t = this.getUninitializedStateFromPool();
618
+ t.type = A, t.position = 0, t.size = e, t.array = new Array(e);
619
+ }
620
+ pushMapState(e) {
621
+ const t = this.getUninitializedStateFromPool();
622
+ t.type = g, t.readCount = 0, t.size = e, t.map = {};
623
+ }
624
+ getUninitializedStateFromPool() {
625
+ if (this.stackHeadPosition++, this.stackHeadPosition === this.stack.length) {
626
+ const e = {
627
+ type: void 0,
628
+ size: 0,
629
+ array: void 0,
630
+ position: 0,
631
+ readCount: 0,
632
+ map: void 0,
633
+ key: null
634
+ };
635
+ this.stack.push(e);
636
+ }
637
+ return this.stack[this.stackHeadPosition];
638
+ }
639
+ release(e) {
640
+ if (this.stack[this.stackHeadPosition] !== e)
641
+ throw new Error("Invalid stack state. Released state is not on top of the stack.");
642
+ if (e.type === A) {
643
+ const s = e;
644
+ s.size = 0, s.array = void 0, s.position = 0, s.type = void 0;
645
+ }
646
+ if (e.type === g || e.type === _) {
647
+ const s = e;
648
+ s.size = 0, s.map = void 0, s.readCount = 0, s.type = void 0;
649
+ }
650
+ this.stackHeadPosition--;
651
+ }
652
+ reset() {
653
+ this.stack.length = 0, this.stackHeadPosition = -1;
654
+ }
655
+ }
656
+ const w = -1, T = new DataView(new ArrayBuffer(0)), Se = new Uint8Array(T.buffer);
657
+ try {
658
+ T.getInt8(0);
659
+ } catch (n) {
660
+ if (!(n instanceof RangeError))
661
+ throw new Error("This module is not supported in the current JavaScript engine because DataView does not throw RangeError on out-of-bounds access");
662
+ }
663
+ const L = new RangeError("Insufficient data"), Ee = new Ue();
664
+ class k {
665
+ constructor(e) {
666
+ h(this, "extensionCodec");
667
+ h(this, "context");
668
+ h(this, "useBigInt64");
669
+ h(this, "rawStrings");
670
+ h(this, "maxStrLength");
671
+ h(this, "maxBinLength");
672
+ h(this, "maxArrayLength");
673
+ h(this, "maxMapLength");
674
+ h(this, "maxExtLength");
675
+ h(this, "keyDecoder");
676
+ h(this, "mapKeyConverter");
677
+ h(this, "totalPos", 0);
678
+ h(this, "pos", 0);
679
+ h(this, "view", T);
680
+ h(this, "bytes", Se);
681
+ h(this, "headByte", w);
682
+ h(this, "stack", new pe());
683
+ h(this, "entered", !1);
684
+ this.extensionCodec = (e == null ? void 0 : e.extensionCodec) ?? m.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) ?? x, this.maxBinLength = (e == null ? void 0 : e.maxBinLength) ?? x, this.maxArrayLength = (e == null ? void 0 : e.maxArrayLength) ?? x, this.maxMapLength = (e == null ? void 0 : e.maxMapLength) ?? x, this.maxExtLength = (e == null ? void 0 : e.maxExtLength) ?? x, this.keyDecoder = (e == null ? void 0 : e.keyDecoder) !== void 0 ? e.keyDecoder : Ee, this.mapKeyConverter = (e == null ? void 0 : e.mapKeyConverter) ?? me;
685
+ }
686
+ clone() {
687
+ return new k({
688
+ extensionCodec: this.extensionCodec,
689
+ context: this.context,
690
+ useBigInt64: this.useBigInt64,
691
+ rawStrings: this.rawStrings,
692
+ maxStrLength: this.maxStrLength,
693
+ maxBinLength: this.maxBinLength,
694
+ maxArrayLength: this.maxArrayLength,
695
+ maxMapLength: this.maxMapLength,
696
+ maxExtLength: this.maxExtLength,
697
+ keyDecoder: this.keyDecoder
698
+ });
699
+ }
700
+ reinitializeState() {
701
+ this.totalPos = 0, this.headByte = w, this.stack.reset();
702
+ }
703
+ setBuffer(e) {
704
+ const t = B(e);
705
+ this.bytes = t, this.view = new DataView(t.buffer, t.byteOffset, t.byteLength), this.pos = 0;
706
+ }
707
+ appendBuffer(e) {
708
+ if (this.headByte === w && !this.hasRemaining(1))
709
+ this.setBuffer(e);
710
+ else {
711
+ const t = this.bytes.subarray(this.pos), s = B(e), i = new Uint8Array(t.length + s.length);
712
+ i.set(t), i.set(s, t.length), this.setBuffer(i);
713
+ }
714
+ }
715
+ hasRemaining(e) {
716
+ return this.view.byteLength - this.pos >= e;
717
+ }
718
+ createExtraByteError(e) {
719
+ const { view: t, pos: s } = this;
720
+ return new RangeError(`Extra ${t.byteLength - s} of ${t.byteLength} byte(s) found at buffer[${e}]`);
721
+ }
722
+ /**
723
+ * @throws {@link DecodeError}
724
+ * @throws {@link RangeError}
725
+ */
726
+ decode(e) {
727
+ if (this.entered)
728
+ return this.clone().decode(e);
729
+ try {
730
+ this.entered = !0, this.reinitializeState(), this.setBuffer(e);
731
+ const t = this.doDecodeSync();
732
+ if (this.hasRemaining(1))
733
+ throw this.createExtraByteError(this.pos);
734
+ return t;
735
+ } finally {
736
+ this.entered = !1;
737
+ }
738
+ }
739
+ *decodeMulti(e) {
740
+ if (this.entered) {
741
+ yield* this.clone().decodeMulti(e);
742
+ return;
743
+ }
744
+ try {
745
+ for (this.entered = !0, this.reinitializeState(), this.setBuffer(e); this.hasRemaining(1); )
746
+ yield this.doDecodeSync();
747
+ } finally {
748
+ this.entered = !1;
749
+ }
750
+ }
751
+ async decodeAsync(e) {
752
+ if (this.entered)
753
+ return this.clone().decodeAsync(e);
754
+ try {
755
+ this.entered = !0;
756
+ let t = !1, s;
757
+ for await (const a of e) {
758
+ if (t)
759
+ throw this.entered = !1, this.createExtraByteError(this.totalPos);
760
+ this.appendBuffer(a);
761
+ try {
762
+ s = this.doDecodeSync(), t = !0;
763
+ } catch (c) {
764
+ if (!(c instanceof RangeError))
765
+ throw c;
766
+ }
767
+ this.totalPos += this.pos;
768
+ }
769
+ if (t) {
770
+ if (this.hasRemaining(1))
771
+ throw this.createExtraByteError(this.totalPos);
772
+ return s;
773
+ }
774
+ const { headByte: i, pos: r, totalPos: o } = this;
775
+ throw new RangeError(`Insufficient data in parsing ${E(i)} at ${o} (${r} in the current buffer)`);
776
+ } finally {
777
+ this.entered = !1;
778
+ }
779
+ }
780
+ decodeArrayStream(e) {
781
+ return this.decodeMultiAsync(e, !0);
782
+ }
783
+ decodeStream(e) {
784
+ return this.decodeMultiAsync(e, !1);
785
+ }
786
+ async *decodeMultiAsync(e, t) {
787
+ if (this.entered) {
788
+ yield* this.clone().decodeMultiAsync(e, t);
789
+ return;
790
+ }
791
+ try {
792
+ this.entered = !0;
793
+ let s = t, i = -1;
794
+ for await (const r of e) {
795
+ if (t && i === 0)
796
+ throw this.createExtraByteError(this.totalPos);
797
+ this.appendBuffer(r), s && (i = this.readArraySize(), s = !1, this.complete());
798
+ try {
799
+ for (; yield this.doDecodeSync(), --i !== 0; )
800
+ ;
801
+ } catch (o) {
802
+ if (!(o instanceof RangeError))
803
+ throw o;
804
+ }
805
+ this.totalPos += this.pos;
806
+ }
807
+ } finally {
808
+ this.entered = !1;
809
+ }
810
+ }
811
+ doDecodeSync() {
812
+ e: for (; ; ) {
813
+ const e = this.readHeadByte();
814
+ let t;
815
+ if (e >= 224)
816
+ t = e - 256;
817
+ else if (e < 192)
818
+ if (e < 128)
819
+ t = e;
820
+ else if (e < 144) {
821
+ const i = e - 128;
822
+ if (i !== 0) {
823
+ this.pushMapState(i), this.complete();
824
+ continue e;
825
+ } else
826
+ t = {};
827
+ } else if (e < 160) {
828
+ const i = e - 144;
829
+ if (i !== 0) {
830
+ this.pushArrayState(i), this.complete();
831
+ continue e;
832
+ } else
833
+ t = [];
834
+ } else {
835
+ const i = e - 160;
836
+ t = this.decodeString(i, 0);
837
+ }
838
+ else if (e === 192)
839
+ t = null;
840
+ else if (e === 194)
841
+ t = !1;
842
+ else if (e === 195)
843
+ t = !0;
844
+ else if (e === 202)
845
+ t = this.readF32();
846
+ else if (e === 203)
847
+ t = this.readF64();
848
+ else if (e === 204)
849
+ t = this.readU8();
850
+ else if (e === 205)
851
+ t = this.readU16();
852
+ else if (e === 206)
853
+ t = this.readU32();
854
+ else if (e === 207)
855
+ this.useBigInt64 ? t = this.readU64AsBigInt() : t = this.readU64();
856
+ else if (e === 208)
857
+ t = this.readI8();
858
+ else if (e === 209)
859
+ t = this.readI16();
860
+ else if (e === 210)
861
+ t = this.readI32();
862
+ else if (e === 211)
863
+ this.useBigInt64 ? t = this.readI64AsBigInt() : t = this.readI64();
864
+ else if (e === 217) {
865
+ const i = this.lookU8();
866
+ t = this.decodeString(i, 1);
867
+ } else if (e === 218) {
868
+ const i = this.lookU16();
869
+ t = this.decodeString(i, 2);
870
+ } else if (e === 219) {
871
+ const i = this.lookU32();
872
+ t = this.decodeString(i, 4);
873
+ } else if (e === 220) {
874
+ const i = this.readU16();
875
+ if (i !== 0) {
876
+ this.pushArrayState(i), this.complete();
877
+ continue e;
878
+ } else
879
+ t = [];
880
+ } else if (e === 221) {
881
+ const i = this.readU32();
882
+ if (i !== 0) {
883
+ this.pushArrayState(i), this.complete();
884
+ continue e;
885
+ } else
886
+ t = [];
887
+ } else if (e === 222) {
888
+ const i = this.readU16();
889
+ if (i !== 0) {
890
+ this.pushMapState(i), this.complete();
891
+ continue e;
892
+ } else
893
+ t = {};
894
+ } else if (e === 223) {
895
+ const i = this.readU32();
896
+ if (i !== 0) {
897
+ this.pushMapState(i), this.complete();
898
+ continue e;
899
+ } else
900
+ t = {};
901
+ } else if (e === 196) {
902
+ const i = this.lookU8();
903
+ t = this.decodeBinary(i, 1);
904
+ } else if (e === 197) {
905
+ const i = this.lookU16();
906
+ t = this.decodeBinary(i, 2);
907
+ } else if (e === 198) {
908
+ const i = this.lookU32();
909
+ t = this.decodeBinary(i, 4);
910
+ } else if (e === 212)
911
+ t = this.decodeExtension(1, 0);
912
+ else if (e === 213)
913
+ t = this.decodeExtension(2, 0);
914
+ else if (e === 214)
915
+ t = this.decodeExtension(4, 0);
916
+ else if (e === 215)
917
+ t = this.decodeExtension(8, 0);
918
+ else if (e === 216)
919
+ t = this.decodeExtension(16, 0);
920
+ else if (e === 199) {
921
+ const i = this.lookU8();
922
+ t = this.decodeExtension(i, 1);
923
+ } else if (e === 200) {
924
+ const i = this.lookU16();
925
+ t = this.decodeExtension(i, 2);
926
+ } else if (e === 201) {
927
+ const i = this.lookU32();
928
+ t = this.decodeExtension(i, 4);
929
+ } else
930
+ throw new f(`Unrecognized type byte: ${E(e)}`);
931
+ this.complete();
932
+ const s = this.stack;
933
+ for (; s.length > 0; ) {
934
+ const i = s.top();
935
+ if (i.type === A)
936
+ if (i.array[i.position] = t, i.position++, i.position === i.size)
937
+ t = i.array, s.release(i);
938
+ else
939
+ continue e;
940
+ else if (i.type === g) {
941
+ if (t === "__proto__")
942
+ throw new f("The key __proto__ is not allowed");
943
+ i.key = this.mapKeyConverter(t), i.type = _;
944
+ continue e;
945
+ } else if (i.map[i.key] = t, i.readCount++, i.readCount === i.size)
946
+ t = i.map, s.release(i);
947
+ else {
948
+ i.key = null, i.type = g;
949
+ continue e;
950
+ }
951
+ }
952
+ return t;
953
+ }
954
+ }
955
+ readHeadByte() {
956
+ return this.headByte === w && (this.headByte = this.readU8()), this.headByte;
957
+ }
958
+ complete() {
959
+ this.headByte = w;
960
+ }
961
+ readArraySize() {
962
+ const e = this.readHeadByte();
963
+ switch (e) {
964
+ case 220:
965
+ return this.readU16();
966
+ case 221:
967
+ return this.readU32();
968
+ default: {
969
+ if (e < 160)
970
+ return e - 144;
971
+ throw new f(`Unrecognized array type byte: ${E(e)}`);
972
+ }
973
+ }
974
+ }
975
+ pushMapState(e) {
976
+ if (e > this.maxMapLength)
977
+ throw new f(`Max length exceeded: map length (${e}) > maxMapLengthLength (${this.maxMapLength})`);
978
+ this.stack.pushMapState(e);
979
+ }
980
+ pushArrayState(e) {
981
+ if (e > this.maxArrayLength)
982
+ throw new f(`Max length exceeded: array length (${e}) > maxArrayLength (${this.maxArrayLength})`);
983
+ this.stack.pushArrayState(e);
984
+ }
985
+ decodeString(e, t) {
986
+ return !this.rawStrings || this.stateIsMapKey() ? this.decodeUtf8String(e, t) : this.decodeBinary(e, t);
987
+ }
988
+ /**
989
+ * @throws {@link RangeError}
990
+ */
991
+ decodeUtf8String(e, t) {
992
+ var r;
993
+ if (e > this.maxStrLength)
994
+ throw new f(`Max length exceeded: UTF-8 byte length (${e}) > maxStrLength (${this.maxStrLength})`);
995
+ if (this.bytes.byteLength < this.pos + t + e)
996
+ throw L;
997
+ const s = this.pos + t;
998
+ let i;
999
+ return this.stateIsMapKey() && ((r = this.keyDecoder) != null && r.canBeCached(e)) ? i = this.keyDecoder.decode(this.bytes, s, e) : i = ee(this.bytes, s, e), this.pos += t + e, i;
1000
+ }
1001
+ stateIsMapKey() {
1002
+ return this.stack.length > 0 ? this.stack.top().type === g : !1;
1003
+ }
1004
+ /**
1005
+ * @throws {@link RangeError}
1006
+ */
1007
+ decodeBinary(e, t) {
1008
+ if (e > this.maxBinLength)
1009
+ throw new f(`Max length exceeded: bin length (${e}) > maxBinLength (${this.maxBinLength})`);
1010
+ if (!this.hasRemaining(e + t))
1011
+ throw L;
1012
+ const s = this.pos + t, i = this.bytes.subarray(s, s + e);
1013
+ return this.pos += t + e, i;
1014
+ }
1015
+ decodeExtension(e, t) {
1016
+ if (e > this.maxExtLength)
1017
+ throw new f(`Max length exceeded: ext length (${e}) > maxExtLength (${this.maxExtLength})`);
1018
+ const s = this.view.getInt8(this.pos + t), i = this.decodeBinary(
1019
+ e,
1020
+ t + 1
1021
+ /* extType */
1022
+ );
1023
+ return this.extensionCodec.decode(i, s, this.context);
1024
+ }
1025
+ lookU8() {
1026
+ return this.view.getUint8(this.pos);
1027
+ }
1028
+ lookU16() {
1029
+ return this.view.getUint16(this.pos);
1030
+ }
1031
+ lookU32() {
1032
+ return this.view.getUint32(this.pos);
1033
+ }
1034
+ readU8() {
1035
+ const e = this.view.getUint8(this.pos);
1036
+ return this.pos++, e;
1037
+ }
1038
+ readI8() {
1039
+ const e = this.view.getInt8(this.pos);
1040
+ return this.pos++, e;
1041
+ }
1042
+ readU16() {
1043
+ const e = this.view.getUint16(this.pos);
1044
+ return this.pos += 2, e;
1045
+ }
1046
+ readI16() {
1047
+ const e = this.view.getInt16(this.pos);
1048
+ return this.pos += 2, e;
1049
+ }
1050
+ readU32() {
1051
+ const e = this.view.getUint32(this.pos);
1052
+ return this.pos += 4, e;
1053
+ }
1054
+ readI32() {
1055
+ const e = this.view.getInt32(this.pos);
1056
+ return this.pos += 4, e;
1057
+ }
1058
+ readU64() {
1059
+ const e = se(this.view, this.pos);
1060
+ return this.pos += 8, e;
1061
+ }
1062
+ readI64() {
1063
+ const e = C(this.view, this.pos);
1064
+ return this.pos += 8, e;
1065
+ }
1066
+ readU64AsBigInt() {
1067
+ const e = this.view.getBigUint64(this.pos);
1068
+ return this.pos += 8, e;
1069
+ }
1070
+ readI64AsBigInt() {
1071
+ const e = this.view.getBigInt64(this.pos);
1072
+ return this.pos += 8, e;
1073
+ }
1074
+ readF32() {
1075
+ const e = this.view.getFloat32(this.pos);
1076
+ return this.pos += 4, e;
1077
+ }
1078
+ readF64() {
1079
+ const e = this.view.getFloat64(this.pos);
1080
+ return this.pos += 8, e;
1081
+ }
1082
+ }
1083
+ function Be(n, e) {
1084
+ return new k(e).decode(n);
1085
+ }
1086
+ const Ae = /* @__PURE__ */ new Set([
1087
+ "auth",
1088
+ "open",
1089
+ "send",
1090
+ "sync",
1091
+ "history",
1092
+ "read",
1093
+ "typing",
1094
+ "react",
1095
+ "edit",
1096
+ "delete",
1097
+ "invoke",
1098
+ "pubkey",
1099
+ "ping",
1100
+ "annotate",
1101
+ "annotate_clear",
1102
+ "uploadPrekeys",
1103
+ "fetchPrekey",
1104
+ "assign",
1105
+ "tag",
1106
+ "note",
1107
+ "agent_status",
1108
+ "subscribe_inbox",
1109
+ "unsubscribe_inbox"
1110
+ ]);
1111
+ function Me(n) {
1112
+ return Ae.has(n.type);
1113
+ }
1114
+ function ve(n) {
1115
+ return we(n);
1116
+ }
1117
+ function ze(n) {
1118
+ let e;
1119
+ try {
1120
+ e = Be(n);
1121
+ } catch {
1122
+ return null;
1123
+ }
1124
+ return typeof e != "object" || e === null || typeof e.type != "string" ? null : e;
1125
+ }
1126
+ export {
1127
+ Te as a,
1128
+ ze as d,
1129
+ ve as e,
1130
+ H as h,
1131
+ Me as i,
1132
+ ke as p,
1133
+ R as r
1134
+ };
1135
+ //# sourceMappingURL=codec.js.map