@paramms/chat-widget 1.6.1 → 1.6.3

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