@inditextech/weave-store-azure-web-pubsub 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,1153 @@
1
+ "use strict";
2
+ //#region rolldown:runtime
3
+ var __create = Object.create;
4
+ var __defProp = Object.defineProperty;
5
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
6
+ var __getOwnPropNames = Object.getOwnPropertyNames;
7
+ var __getProtoOf = Object.getPrototypeOf;
8
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
9
+ var __copyProps = (to, from$1, except, desc) => {
10
+ if (from$1 && typeof from$1 === "object" || typeof from$1 === "function") for (var keys$1 = __getOwnPropNames(from$1), i = 0, n = keys$1.length, key; i < n; i++) {
11
+ key = keys$1[i];
12
+ if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
13
+ get: ((k) => from$1[k]).bind(null, key),
14
+ enumerable: !(desc = __getOwnPropDesc(from$1, key)) || desc.enumerable
15
+ });
16
+ }
17
+ return to;
18
+ };
19
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
20
+ value: mod,
21
+ enumerable: true
22
+ }) : target, mod));
23
+
24
+ //#endregion
25
+ const yjs = __toESM(require("yjs"));
26
+
27
+ //#region src/constants.ts
28
+ const WEAVE_STORE_AZURE_WEB_PUBSUB = "store-azure-web-pubsub";
29
+ const WEAVE_STORE_AZURE_WEB_PUBSUB_CONNECTION_STATUS = {
30
+ ["CONNECTED"]: "connected",
31
+ ["DISCONNECTED"]: "disconnected"
32
+ };
33
+
34
+ //#endregion
35
+ //#region ../../node_modules/lib0/binary.js
36
+ const BIT8 = 128;
37
+ const BIT18 = 1 << 17;
38
+ const BIT19 = 1 << 18;
39
+ const BIT20 = 1 << 19;
40
+ const BIT21 = 1 << 20;
41
+ const BIT22 = 1 << 21;
42
+ const BIT23 = 1 << 22;
43
+ const BIT24 = 1 << 23;
44
+ const BIT25 = 1 << 24;
45
+ const BIT26 = 1 << 25;
46
+ const BIT27 = 1 << 26;
47
+ const BIT28 = 1 << 27;
48
+ const BIT29 = 1 << 28;
49
+ const BIT30 = 1 << 29;
50
+ const BIT31 = 1 << 30;
51
+ const BIT32 = 1 << 31;
52
+ const BITS7 = 127;
53
+ const BITS17 = BIT18 - 1;
54
+ const BITS18 = BIT19 - 1;
55
+ const BITS19 = BIT20 - 1;
56
+ const BITS20 = BIT21 - 1;
57
+ const BITS21 = BIT22 - 1;
58
+ const BITS22 = BIT23 - 1;
59
+ const BITS23 = BIT24 - 1;
60
+ const BITS24 = BIT25 - 1;
61
+ const BITS25 = BIT26 - 1;
62
+ const BITS26 = BIT27 - 1;
63
+ const BITS27 = BIT28 - 1;
64
+ const BITS28 = BIT29 - 1;
65
+ const BITS29 = BIT30 - 1;
66
+ const BITS30 = BIT31 - 1;
67
+ /**
68
+ * @type {number}
69
+ */
70
+ const BITS31 = 2147483647;
71
+ /**
72
+ * @type {number}
73
+ */
74
+ const BITS32 = 4294967295;
75
+
76
+ //#endregion
77
+ //#region ../../node_modules/lib0/math.js
78
+ /**
79
+ * Common Math expressions.
80
+ *
81
+ * @module math
82
+ */
83
+ const floor = Math.floor;
84
+ /**
85
+ * @function
86
+ * @param {number} a
87
+ * @param {number} b
88
+ * @return {number} The smaller element of a and b
89
+ */
90
+ const min = (a, b) => a < b ? a : b;
91
+ /**
92
+ * @function
93
+ * @param {number} a
94
+ * @param {number} b
95
+ * @return {number} The bigger element of a and b
96
+ */
97
+ const max = (a, b) => a > b ? a : b;
98
+ const isNaN$1 = Number.isNaN;
99
+
100
+ //#endregion
101
+ //#region ../../node_modules/lib0/number.js
102
+ const MAX_SAFE_INTEGER = Number.MAX_SAFE_INTEGER;
103
+ const MIN_SAFE_INTEGER = Number.MIN_SAFE_INTEGER;
104
+ const LOWEST_INT32 = 1 << 31;
105
+ const HIGHEST_INT32 = BITS31;
106
+ const HIGHEST_UINT32 = BITS32;
107
+ /* c8 ignore next */
108
+ const isInteger = Number.isInteger || ((num) => typeof num === "number" && isFinite(num) && floor(num) === num);
109
+ const isNaN = Number.isNaN;
110
+ const parseInt = Number.parseInt;
111
+
112
+ //#endregion
113
+ //#region ../../node_modules/lib0/set.js
114
+ /**
115
+ * Utility module to work with sets.
116
+ *
117
+ * @module set
118
+ */
119
+ const create$2 = () => new Set();
120
+
121
+ //#endregion
122
+ //#region ../../node_modules/lib0/array.js
123
+ /**
124
+ * Transforms something array-like to an actual Array.
125
+ *
126
+ * @function
127
+ * @template T
128
+ * @param {ArrayLike<T>|Iterable<T>} arraylike
129
+ * @return {T}
130
+ */
131
+ const from = Array.from;
132
+ const isArray$1 = Array.isArray;
133
+
134
+ //#endregion
135
+ //#region ../../node_modules/lib0/string.js
136
+ /**
137
+ * Utility module to work with strings.
138
+ *
139
+ * @module string
140
+ */
141
+ const fromCharCode = String.fromCharCode;
142
+ const fromCodePoint = String.fromCodePoint;
143
+ /**
144
+ * The largest utf16 character.
145
+ * Corresponds to Uint8Array([255, 255]) or charcodeof(2x2^8)
146
+ */
147
+ const MAX_UTF16_CHARACTER = fromCharCode(65535);
148
+ /**
149
+ * @param {string} str
150
+ * @return {Uint8Array}
151
+ */
152
+ const _encodeUtf8Polyfill = (str) => {
153
+ const encodedString = unescape(encodeURIComponent(str));
154
+ const len = encodedString.length;
155
+ const buf = new Uint8Array(len);
156
+ for (let i = 0; i < len; i++) buf[i] = encodedString.codePointAt(i);
157
+ return buf;
158
+ };
159
+ /* c8 ignore next */
160
+ const utf8TextEncoder = typeof TextEncoder !== "undefined" ? new TextEncoder() : null;
161
+ /**
162
+ * @param {string} str
163
+ * @return {Uint8Array}
164
+ */
165
+ const _encodeUtf8Native = (str) => utf8TextEncoder.encode(str);
166
+ /**
167
+ * @param {string} str
168
+ * @return {Uint8Array}
169
+ */
170
+ /* c8 ignore next */
171
+ const encodeUtf8 = utf8TextEncoder ? _encodeUtf8Native : _encodeUtf8Polyfill;
172
+ /* c8 ignore next */
173
+ let utf8TextDecoder = typeof TextDecoder === "undefined" ? null : new TextDecoder("utf-8", {
174
+ fatal: true,
175
+ ignoreBOM: true
176
+ });
177
+ /* c8 ignore start */
178
+ if (utf8TextDecoder && utf8TextDecoder.decode(new Uint8Array()).length === 1)
179
+ /* c8 ignore next */
180
+ utf8TextDecoder = null;
181
+
182
+ //#endregion
183
+ //#region ../../node_modules/lib0/error.js
184
+ /**
185
+ * Error helpers.
186
+ *
187
+ * @module error
188
+ */
189
+ /**
190
+ * @param {string} s
191
+ * @return {Error}
192
+ */
193
+ /* c8 ignore next */
194
+ const create$1 = (s) => new Error(s);
195
+
196
+ //#endregion
197
+ //#region ../../node_modules/lib0/encoding.js
198
+ /**
199
+ * A BinaryEncoder handles the encoding to an Uint8Array.
200
+ */
201
+ var Encoder = class {
202
+ constructor() {
203
+ this.cpos = 0;
204
+ this.cbuf = new Uint8Array(100);
205
+ /**
206
+ * @type {Array<Uint8Array>}
207
+ */
208
+ this.bufs = [];
209
+ }
210
+ };
211
+ /**
212
+ * @function
213
+ * @return {Encoder}
214
+ */
215
+ const createEncoder = () => new Encoder();
216
+ /**
217
+ * The current length of the encoded data.
218
+ *
219
+ * @function
220
+ * @param {Encoder} encoder
221
+ * @return {number}
222
+ */
223
+ const length$1 = (encoder) => {
224
+ let len = encoder.cpos;
225
+ for (let i = 0; i < encoder.bufs.length; i++) len += encoder.bufs[i].length;
226
+ return len;
227
+ };
228
+ /**
229
+ * Transform to Uint8Array.
230
+ *
231
+ * @function
232
+ * @param {Encoder} encoder
233
+ * @return {Uint8Array} The created ArrayBuffer.
234
+ */
235
+ const toUint8Array = (encoder) => {
236
+ const uint8arr = new Uint8Array(length$1(encoder));
237
+ let curPos = 0;
238
+ for (let i = 0; i < encoder.bufs.length; i++) {
239
+ const d = encoder.bufs[i];
240
+ uint8arr.set(d, curPos);
241
+ curPos += d.length;
242
+ }
243
+ uint8arr.set(new Uint8Array(encoder.cbuf.buffer, 0, encoder.cpos), curPos);
244
+ return uint8arr;
245
+ };
246
+ /**
247
+ * Write one byte to the encoder.
248
+ *
249
+ * @function
250
+ * @param {Encoder} encoder
251
+ * @param {number} num The byte that is to be encoded.
252
+ */
253
+ const write = (encoder, num) => {
254
+ const bufferLen = encoder.cbuf.length;
255
+ if (encoder.cpos === bufferLen) {
256
+ encoder.bufs.push(encoder.cbuf);
257
+ encoder.cbuf = new Uint8Array(bufferLen * 2);
258
+ encoder.cpos = 0;
259
+ }
260
+ encoder.cbuf[encoder.cpos++] = num;
261
+ };
262
+ /**
263
+ * Write a variable length unsigned integer. Max encodable integer is 2^53.
264
+ *
265
+ * @function
266
+ * @param {Encoder} encoder
267
+ * @param {number} num The number that is to be encoded.
268
+ */
269
+ const writeVarUint = (encoder, num) => {
270
+ while (num > BITS7) {
271
+ write(encoder, BIT8 | BITS7 & num);
272
+ num = floor(num / 128);
273
+ }
274
+ write(encoder, BITS7 & num);
275
+ };
276
+ /**
277
+ * A cache to store strings temporarily
278
+ */
279
+ const _strBuffer = new Uint8Array(3e4);
280
+ const _maxStrBSize = _strBuffer.length / 3;
281
+ /**
282
+ * Write a variable length string.
283
+ *
284
+ * @function
285
+ * @param {Encoder} encoder
286
+ * @param {String} str The string that is to be encoded.
287
+ */
288
+ const _writeVarStringNative = (encoder, str) => {
289
+ if (str.length < _maxStrBSize) {
290
+ /* c8 ignore next */
291
+ const written = utf8TextEncoder.encodeInto(str, _strBuffer).written || 0;
292
+ writeVarUint(encoder, written);
293
+ for (let i = 0; i < written; i++) write(encoder, _strBuffer[i]);
294
+ } else writeVarUint8Array(encoder, encodeUtf8(str));
295
+ };
296
+ /**
297
+ * Write a variable length string.
298
+ *
299
+ * @function
300
+ * @param {Encoder} encoder
301
+ * @param {String} str The string that is to be encoded.
302
+ */
303
+ const _writeVarStringPolyfill = (encoder, str) => {
304
+ const encodedString = unescape(encodeURIComponent(str));
305
+ const len = encodedString.length;
306
+ writeVarUint(encoder, len);
307
+ for (let i = 0; i < len; i++) write(
308
+ encoder,
309
+ /** @type {number} */
310
+ encodedString.codePointAt(i)
311
+ );
312
+ };
313
+ /**
314
+ * Write a variable length string.
315
+ *
316
+ * @function
317
+ * @param {Encoder} encoder
318
+ * @param {String} str The string that is to be encoded.
319
+ */
320
+ /* c8 ignore next */
321
+ const writeVarString = utf8TextEncoder && utf8TextEncoder.encodeInto ? _writeVarStringNative : _writeVarStringPolyfill;
322
+ /**
323
+ * Append fixed-length Uint8Array to the encoder.
324
+ *
325
+ * @function
326
+ * @param {Encoder} encoder
327
+ * @param {Uint8Array} uint8Array
328
+ */
329
+ const writeUint8Array = (encoder, uint8Array) => {
330
+ const bufferLen = encoder.cbuf.length;
331
+ const cpos = encoder.cpos;
332
+ const leftCopyLen = min(bufferLen - cpos, uint8Array.length);
333
+ const rightCopyLen = uint8Array.length - leftCopyLen;
334
+ encoder.cbuf.set(uint8Array.subarray(0, leftCopyLen), cpos);
335
+ encoder.cpos += leftCopyLen;
336
+ if (rightCopyLen > 0) {
337
+ encoder.bufs.push(encoder.cbuf);
338
+ encoder.cbuf = new Uint8Array(max(bufferLen * 2, rightCopyLen));
339
+ encoder.cbuf.set(uint8Array.subarray(leftCopyLen));
340
+ encoder.cpos = rightCopyLen;
341
+ }
342
+ };
343
+ /**
344
+ * Append an Uint8Array to Encoder.
345
+ *
346
+ * @function
347
+ * @param {Encoder} encoder
348
+ * @param {Uint8Array} uint8Array
349
+ */
350
+ const writeVarUint8Array = (encoder, uint8Array) => {
351
+ writeVarUint(encoder, uint8Array.byteLength);
352
+ writeUint8Array(encoder, uint8Array);
353
+ };
354
+ const floatTestBed = new DataView(new ArrayBuffer(4));
355
+
356
+ //#endregion
357
+ //#region ../../node_modules/lib0/decoding.js
358
+ const errorUnexpectedEndOfArray = create$1("Unexpected end of array");
359
+ const errorIntegerOutOfRange = create$1("Integer out of Range");
360
+ /**
361
+ * A Decoder handles the decoding of an Uint8Array.
362
+ */
363
+ var Decoder = class {
364
+ /**
365
+ * @param {Uint8Array} uint8Array Binary data to decode
366
+ */
367
+ constructor(uint8Array) {
368
+ /**
369
+ * Decoding target.
370
+ *
371
+ * @type {Uint8Array}
372
+ */
373
+ this.arr = uint8Array;
374
+ /**
375
+ * Current decoding position.
376
+ *
377
+ * @type {number}
378
+ */
379
+ this.pos = 0;
380
+ }
381
+ };
382
+ /**
383
+ * @function
384
+ * @param {Uint8Array} uint8Array
385
+ * @return {Decoder}
386
+ */
387
+ const createDecoder = (uint8Array) => new Decoder(uint8Array);
388
+ /**
389
+ * Create an Uint8Array view of the next `len` bytes and advance the position by `len`.
390
+ *
391
+ * Important: The Uint8Array still points to the underlying ArrayBuffer. Make sure to discard the result as soon as possible to prevent any memory leaks.
392
+ * Use `buffer.copyUint8Array` to copy the result into a new Uint8Array.
393
+ *
394
+ * @function
395
+ * @param {Decoder} decoder The decoder instance
396
+ * @param {number} len The length of bytes to read
397
+ * @return {Uint8Array}
398
+ */
399
+ const readUint8Array = (decoder, len) => {
400
+ const view = new Uint8Array(decoder.arr.buffer, decoder.pos + decoder.arr.byteOffset, len);
401
+ decoder.pos += len;
402
+ return view;
403
+ };
404
+ /**
405
+ * Read variable length Uint8Array.
406
+ *
407
+ * Important: The Uint8Array still points to the underlying ArrayBuffer. Make sure to discard the result as soon as possible to prevent any memory leaks.
408
+ * Use `buffer.copyUint8Array` to copy the result into a new Uint8Array.
409
+ *
410
+ * @function
411
+ * @param {Decoder} decoder
412
+ * @return {Uint8Array}
413
+ */
414
+ const readVarUint8Array = (decoder) => readUint8Array(decoder, readVarUint(decoder));
415
+ /**
416
+ * Read one byte as unsigned integer.
417
+ * @function
418
+ * @param {Decoder} decoder The decoder instance
419
+ * @return {number} Unsigned 8-bit integer
420
+ */
421
+ const readUint8 = (decoder) => decoder.arr[decoder.pos++];
422
+ /**
423
+ * Read unsigned integer (32bit) with variable length.
424
+ * 1/8th of the storage is used as encoding overhead.
425
+ * * numbers < 2^7 is stored in one bytlength
426
+ * * numbers < 2^14 is stored in two bylength
427
+ *
428
+ * @function
429
+ * @param {Decoder} decoder
430
+ * @return {number} An unsigned integer.length
431
+ */
432
+ const readVarUint = (decoder) => {
433
+ let num = 0;
434
+ let mult = 1;
435
+ const len = decoder.arr.length;
436
+ while (decoder.pos < len) {
437
+ const r = decoder.arr[decoder.pos++];
438
+ num = num + (r & BITS7) * mult;
439
+ mult *= 128;
440
+ if (r < BIT8) return num;
441
+ /* c8 ignore start */
442
+ if (num > MAX_SAFE_INTEGER) throw errorIntegerOutOfRange;
443
+ }
444
+ throw errorUnexpectedEndOfArray;
445
+ };
446
+ /**
447
+ * We don't test this function anymore as we use native decoding/encoding by default now.
448
+ * Better not modify this anymore..
449
+ *
450
+ * Transforming utf8 to a string is pretty expensive. The code performs 10x better
451
+ * when String.fromCodePoint is fed with all characters as arguments.
452
+ * But most environments have a maximum number of arguments per functions.
453
+ * For effiency reasons we apply a maximum of 10000 characters at once.
454
+ *
455
+ * @function
456
+ * @param {Decoder} decoder
457
+ * @return {String} The read String.
458
+ */
459
+ /* c8 ignore start */
460
+ const _readVarStringPolyfill = (decoder) => {
461
+ let remainingLen = readVarUint(decoder);
462
+ if (remainingLen === 0) return "";
463
+ else {
464
+ let encodedString = String.fromCodePoint(readUint8(decoder));
465
+ if (--remainingLen < 100) while (remainingLen--) encodedString += String.fromCodePoint(readUint8(decoder));
466
+ else while (remainingLen > 0) {
467
+ const nextLen = remainingLen < 1e4 ? remainingLen : 1e4;
468
+ const bytes = decoder.arr.subarray(decoder.pos, decoder.pos + nextLen);
469
+ decoder.pos += nextLen;
470
+ encodedString += String.fromCodePoint.apply(
471
+ null,
472
+ /** @type {any} */
473
+ bytes
474
+ );
475
+ remainingLen -= nextLen;
476
+ }
477
+ return decodeURIComponent(escape(encodedString));
478
+ }
479
+ };
480
+ /* c8 ignore stop */
481
+ /**
482
+ * @function
483
+ * @param {Decoder} decoder
484
+ * @return {String} The read String
485
+ */
486
+ const _readVarStringNative = (decoder) => utf8TextDecoder.decode(readVarUint8Array(decoder));
487
+ /**
488
+ * Read string of variable length
489
+ * * varUint is used to store the length of the string
490
+ *
491
+ * @function
492
+ * @param {Decoder} decoder
493
+ * @return {String} The read String
494
+ *
495
+ */
496
+ /* c8 ignore next */
497
+ const readVarString = utf8TextDecoder ? _readVarStringNative : _readVarStringPolyfill;
498
+
499
+ //#endregion
500
+ //#region ../../node_modules/y-protocols/sync.js
501
+ /**
502
+ * @typedef {Map<number, number>} StateMap
503
+ */
504
+ /**
505
+ * Core Yjs defines two message types:
506
+ * • YjsSyncStep1: Includes the State Set of the sending client. When received, the client should reply with YjsSyncStep2.
507
+ * • YjsSyncStep2: Includes all missing structs and the complete delete set. When received, the client is assured that it
508
+ * received all information from the remote client.
509
+ *
510
+ * In a peer-to-peer network, you may want to introduce a SyncDone message type. Both parties should initiate the connection
511
+ * with SyncStep1. When a client received SyncStep2, it should reply with SyncDone. When the local client received both
512
+ * SyncStep2 and SyncDone, it is assured that it is synced to the remote client.
513
+ *
514
+ * In a client-server model, you want to handle this differently: The client should initiate the connection with SyncStep1.
515
+ * When the server receives SyncStep1, it should reply with SyncStep2 immediately followed by SyncStep1. The client replies
516
+ * with SyncStep2 when it receives SyncStep1. Optionally the server may send a SyncDone after it received SyncStep2, so the
517
+ * client knows that the sync is finished. There are two reasons for this more elaborated sync model: 1. This protocol can
518
+ * easily be implemented on top of http and websockets. 2. The server should only reply to requests, and not initiate them.
519
+ * Therefore it is necessary that the client initiates the sync.
520
+ *
521
+ * Construction of a message:
522
+ * [messageType : varUint, message definition..]
523
+ *
524
+ * Note: A message does not include information about the room name. This must to be handled by the upper layer protocol!
525
+ *
526
+ * stringify[messageType] stringifies a message definition (messageType is already read from the bufffer)
527
+ */
528
+ const messageYjsSyncStep1 = 0;
529
+ const messageYjsSyncStep2 = 1;
530
+ const messageYjsUpdate = 2;
531
+ /**
532
+ * Create a sync step 1 message based on the state of the current shared document.
533
+ *
534
+ * @param {encoding.Encoder} encoder
535
+ * @param {Y.Doc} doc
536
+ */
537
+ const writeSyncStep1 = (encoder, doc) => {
538
+ writeVarUint(encoder, messageYjsSyncStep1);
539
+ const sv = yjs.encodeStateVector(doc);
540
+ writeVarUint8Array(encoder, sv);
541
+ };
542
+ /**
543
+ * @param {encoding.Encoder} encoder
544
+ * @param {Y.Doc} doc
545
+ * @param {Uint8Array} [encodedStateVector]
546
+ */
547
+ const writeSyncStep2 = (encoder, doc, encodedStateVector) => {
548
+ writeVarUint(encoder, messageYjsSyncStep2);
549
+ writeVarUint8Array(encoder, yjs.encodeStateAsUpdate(doc, encodedStateVector));
550
+ };
551
+ /**
552
+ * Read SyncStep1 message and reply with SyncStep2.
553
+ *
554
+ * @param {decoding.Decoder} decoder The reply to the received message
555
+ * @param {encoding.Encoder} encoder The received message
556
+ * @param {Y.Doc} doc
557
+ */
558
+ const readSyncStep1 = (decoder, encoder, doc) => writeSyncStep2(encoder, doc, readVarUint8Array(decoder));
559
+ /**
560
+ * Read and apply Structs and then DeleteStore to a y instance.
561
+ *
562
+ * @param {decoding.Decoder} decoder
563
+ * @param {Y.Doc} doc
564
+ * @param {any} transactionOrigin
565
+ */
566
+ const readSyncStep2 = (decoder, doc, transactionOrigin) => {
567
+ try {
568
+ yjs.applyUpdate(doc, readVarUint8Array(decoder), transactionOrigin);
569
+ } catch (error) {
570
+ console.error("Caught error while handling a Yjs update", error);
571
+ }
572
+ };
573
+ /**
574
+ * @param {encoding.Encoder} encoder
575
+ * @param {Uint8Array} update
576
+ */
577
+ const writeUpdate = (encoder, update) => {
578
+ writeVarUint(encoder, messageYjsUpdate);
579
+ writeVarUint8Array(encoder, update);
580
+ };
581
+ /**
582
+ * Read and apply Structs and then DeleteStore to a y instance.
583
+ *
584
+ * @param {decoding.Decoder} decoder
585
+ * @param {Y.Doc} doc
586
+ * @param {any} transactionOrigin
587
+ */
588
+ const readUpdate = readSyncStep2;
589
+ /**
590
+ * @param {decoding.Decoder} decoder A message received from another client
591
+ * @param {encoding.Encoder} encoder The reply message. Does not need to be sent if empty.
592
+ * @param {Y.Doc} doc
593
+ * @param {any} transactionOrigin
594
+ */
595
+ const readSyncMessage = (decoder, encoder, doc, transactionOrigin) => {
596
+ const messageType = readVarUint(decoder);
597
+ switch (messageType) {
598
+ case messageYjsSyncStep1:
599
+ readSyncStep1(decoder, encoder, doc);
600
+ break;
601
+ case messageYjsSyncStep2:
602
+ readSyncStep2(decoder, doc, transactionOrigin);
603
+ break;
604
+ case messageYjsUpdate:
605
+ readUpdate(decoder, doc, transactionOrigin);
606
+ break;
607
+ default: throw new Error("Unknown message type");
608
+ }
609
+ return messageType;
610
+ };
611
+
612
+ //#endregion
613
+ //#region ../../node_modules/lib0/time.js
614
+ /**
615
+ * Return current unix time.
616
+ *
617
+ * @return {number}
618
+ */
619
+ const getUnixTime = Date.now;
620
+
621
+ //#endregion
622
+ //#region ../../node_modules/lib0/map.js
623
+ /**
624
+ * Utility module to work with key-value stores.
625
+ *
626
+ * @module map
627
+ */
628
+ /**
629
+ * Creates a new Map instance.
630
+ *
631
+ * @function
632
+ * @return {Map<any, any>}
633
+ *
634
+ * @function
635
+ */
636
+ const create = () => new Map();
637
+ /**
638
+ * Get map property. Create T if property is undefined and set T on map.
639
+ *
640
+ * ```js
641
+ * const listeners = map.setIfUndefined(events, 'eventName', set.create)
642
+ * listeners.add(listener)
643
+ * ```
644
+ *
645
+ * @function
646
+ * @template {Map<any, any>} MAP
647
+ * @template {MAP extends Map<any,infer V> ? function():V : unknown} CF
648
+ * @param {MAP} map
649
+ * @param {MAP extends Map<infer K,any> ? K : unknown} key
650
+ * @param {CF} createT
651
+ * @return {ReturnType<CF>}
652
+ */
653
+ const setIfUndefined = (map, key, createT) => {
654
+ let set = map.get(key);
655
+ if (set === void 0) map.set(key, set = createT());
656
+ return set;
657
+ };
658
+
659
+ //#endregion
660
+ //#region ../../node_modules/lib0/observable.js
661
+ /* c8 ignore start */
662
+ /**
663
+ * Handles named events.
664
+ *
665
+ * @deprecated
666
+ * @template N
667
+ */
668
+ var Observable = class {
669
+ constructor() {
670
+ /**
671
+ * Some desc.
672
+ * @type {Map<N, any>}
673
+ */
674
+ this._observers = create();
675
+ }
676
+ /**
677
+ * @param {N} name
678
+ * @param {function} f
679
+ */
680
+ on(name, f) {
681
+ setIfUndefined(this._observers, name, create$2).add(f);
682
+ }
683
+ /**
684
+ * @param {N} name
685
+ * @param {function} f
686
+ */
687
+ once(name, f) {
688
+ /**
689
+ * @param {...any} args
690
+ */
691
+ const _f = (...args) => {
692
+ this.off(name, _f);
693
+ f(...args);
694
+ };
695
+ this.on(name, _f);
696
+ }
697
+ /**
698
+ * @param {N} name
699
+ * @param {function} f
700
+ */
701
+ off(name, f) {
702
+ const observers = this._observers.get(name);
703
+ if (observers !== void 0) {
704
+ observers.delete(f);
705
+ if (observers.size === 0) this._observers.delete(name);
706
+ }
707
+ }
708
+ /**
709
+ * Emit a named event. All registered event listeners that listen to the
710
+ * specified name will receive the event.
711
+ *
712
+ * @todo This should catch exceptions
713
+ *
714
+ * @param {N} name The event name.
715
+ * @param {Array<any>} args The arguments that are applied to the event listener.
716
+ */
717
+ emit(name, args) {
718
+ return from((this._observers.get(name) || create()).values()).forEach((f) => f(...args));
719
+ }
720
+ destroy() {
721
+ this._observers = create();
722
+ }
723
+ };
724
+ /* c8 ignore end */
725
+
726
+ //#endregion
727
+ //#region ../../node_modules/lib0/object.js
728
+ /**
729
+ * @param {Object<string,any>} obj
730
+ */
731
+ const keys = Object.keys;
732
+ /**
733
+ * @deprecated use object.size instead
734
+ * @param {Object<string,any>} obj
735
+ * @return {number}
736
+ */
737
+ const length = (obj) => keys(obj).length;
738
+ /**
739
+ * Calls `Object.prototype.hasOwnProperty`.
740
+ *
741
+ * @param {any} obj
742
+ * @param {string|symbol} key
743
+ * @return {boolean}
744
+ */
745
+ const hasProperty = (obj, key) => Object.prototype.hasOwnProperty.call(obj, key);
746
+
747
+ //#endregion
748
+ //#region ../../node_modules/lib0/traits.js
749
+ const EqualityTraitSymbol = Symbol("Equality");
750
+ /**
751
+ * @typedef {{ [EqualityTraitSymbol]:(other:EqualityTrait)=>boolean }} EqualityTrait
752
+ */
753
+
754
+ //#endregion
755
+ //#region ../../node_modules/lib0/function.js
756
+ /* c8 ignore start */
757
+ /**
758
+ * @param {any} a
759
+ * @param {any} b
760
+ * @return {boolean}
761
+ */
762
+ const equalityDeep = (a, b) => {
763
+ if (a === b) return true;
764
+ if (a == null || b == null || a.constructor !== b.constructor) return false;
765
+ if (a[EqualityTraitSymbol] != null) return a[EqualityTraitSymbol](b);
766
+ switch (a.constructor) {
767
+ case ArrayBuffer:
768
+ a = new Uint8Array(a);
769
+ b = new Uint8Array(b);
770
+ case Uint8Array: {
771
+ if (a.byteLength !== b.byteLength) return false;
772
+ for (let i = 0; i < a.length; i++) if (a[i] !== b[i]) return false;
773
+ break;
774
+ }
775
+ case Set: {
776
+ if (a.size !== b.size) return false;
777
+ for (const value of a) if (!b.has(value)) return false;
778
+ break;
779
+ }
780
+ case Map: {
781
+ if (a.size !== b.size) return false;
782
+ for (const key of a.keys()) if (!b.has(key) || !equalityDeep(a.get(key), b.get(key))) return false;
783
+ break;
784
+ }
785
+ case Object:
786
+ if (length(a) !== length(b)) return false;
787
+ for (const key in a) if (!hasProperty(a, key) || !equalityDeep(a[key], b[key])) return false;
788
+ break;
789
+ case Array:
790
+ if (a.length !== b.length) return false;
791
+ for (let i = 0; i < a.length; i++) if (!equalityDeep(a[i], b[i])) return false;
792
+ break;
793
+ default: return false;
794
+ }
795
+ return true;
796
+ };
797
+ /* c8 ignore stop */
798
+ const isArray = isArray$1;
799
+
800
+ //#endregion
801
+ //#region ../../node_modules/y-protocols/awareness.js
802
+ const outdatedTimeout = 3e4;
803
+ /**
804
+ * @typedef {Object} MetaClientState
805
+ * @property {number} MetaClientState.clock
806
+ * @property {number} MetaClientState.lastUpdated unix timestamp
807
+ */
808
+ /**
809
+ * The Awareness class implements a simple shared state protocol that can be used for non-persistent data like awareness information
810
+ * (cursor, username, status, ..). Each client can update its own local state and listen to state changes of
811
+ * remote clients. Every client may set a state of a remote peer to `null` to mark the client as offline.
812
+ *
813
+ * Each client is identified by a unique client id (something we borrow from `doc.clientID`). A client can override
814
+ * its own state by propagating a message with an increasing timestamp (`clock`). If such a message is received, it is
815
+ * applied if the known state of that client is older than the new state (`clock < newClock`). If a client thinks that
816
+ * a remote client is offline, it may propagate a message with
817
+ * `{ clock: currentClientClock, state: null, client: remoteClient }`. If such a
818
+ * message is received, and the known clock of that client equals the received clock, it will override the state with `null`.
819
+ *
820
+ * Before a client disconnects, it should propagate a `null` state with an updated clock.
821
+ *
822
+ * Awareness states must be updated every 30 seconds. Otherwise the Awareness instance will delete the client state.
823
+ *
824
+ * @extends {Observable<string>}
825
+ */
826
+ var Awareness = class extends Observable {
827
+ /**
828
+ * @param {Y.Doc} doc
829
+ */
830
+ constructor(doc) {
831
+ super();
832
+ this.doc = doc;
833
+ /**
834
+ * @type {number}
835
+ */
836
+ this.clientID = doc.clientID;
837
+ /**
838
+ * Maps from client id to client state
839
+ * @type {Map<number, Object<string, any>>}
840
+ */
841
+ this.states = new Map();
842
+ /**
843
+ * @type {Map<number, MetaClientState>}
844
+ */
845
+ this.meta = new Map();
846
+ this._checkInterval = setInterval(() => {
847
+ const now = getUnixTime();
848
+ if (this.getLocalState() !== null && outdatedTimeout / 2 <= now - this.meta.get(this.clientID).lastUpdated) this.setLocalState(this.getLocalState());
849
+ /**
850
+ * @type {Array<number>}
851
+ */
852
+ const remove = [];
853
+ this.meta.forEach((meta, clientid) => {
854
+ if (clientid !== this.clientID && outdatedTimeout <= now - meta.lastUpdated && this.states.has(clientid)) remove.push(clientid);
855
+ });
856
+ if (remove.length > 0) removeAwarenessStates(this, remove, "timeout");
857
+ }, floor(outdatedTimeout / 10));
858
+ doc.on("destroy", () => {
859
+ this.destroy();
860
+ });
861
+ this.setLocalState({});
862
+ }
863
+ destroy() {
864
+ this.emit("destroy", [this]);
865
+ this.setLocalState(null);
866
+ super.destroy();
867
+ clearInterval(this._checkInterval);
868
+ }
869
+ /**
870
+ * @return {Object<string,any>|null}
871
+ */
872
+ getLocalState() {
873
+ return this.states.get(this.clientID) || null;
874
+ }
875
+ /**
876
+ * @param {Object<string,any>|null} state
877
+ */
878
+ setLocalState(state) {
879
+ const clientID = this.clientID;
880
+ const currLocalMeta = this.meta.get(clientID);
881
+ const clock = currLocalMeta === void 0 ? 0 : currLocalMeta.clock + 1;
882
+ const prevState = this.states.get(clientID);
883
+ if (state === null) this.states.delete(clientID);
884
+ else this.states.set(clientID, state);
885
+ this.meta.set(clientID, {
886
+ clock,
887
+ lastUpdated: getUnixTime()
888
+ });
889
+ const added = [];
890
+ const updated = [];
891
+ const filteredUpdated = [];
892
+ const removed = [];
893
+ if (state === null) removed.push(clientID);
894
+ else if (prevState == null) {
895
+ if (state != null) added.push(clientID);
896
+ } else {
897
+ updated.push(clientID);
898
+ if (!equalityDeep(prevState, state)) filteredUpdated.push(clientID);
899
+ }
900
+ if (added.length > 0 || filteredUpdated.length > 0 || removed.length > 0) this.emit("change", [{
901
+ added,
902
+ updated: filteredUpdated,
903
+ removed
904
+ }, "local"]);
905
+ this.emit("update", [{
906
+ added,
907
+ updated,
908
+ removed
909
+ }, "local"]);
910
+ }
911
+ /**
912
+ * @param {string} field
913
+ * @param {any} value
914
+ */
915
+ setLocalStateField(field, value) {
916
+ const state = this.getLocalState();
917
+ if (state !== null) this.setLocalState({
918
+ ...state,
919
+ [field]: value
920
+ });
921
+ }
922
+ /**
923
+ * @return {Map<number,Object<string,any>>}
924
+ */
925
+ getStates() {
926
+ return this.states;
927
+ }
928
+ };
929
+ /**
930
+ * Mark (remote) clients as inactive and remove them from the list of active peers.
931
+ * This change will be propagated to remote clients.
932
+ *
933
+ * @param {Awareness} awareness
934
+ * @param {Array<number>} clients
935
+ * @param {any} origin
936
+ */
937
+ const removeAwarenessStates = (awareness, clients, origin) => {
938
+ const removed = [];
939
+ for (let i = 0; i < clients.length; i++) {
940
+ const clientID = clients[i];
941
+ if (awareness.states.has(clientID)) {
942
+ awareness.states.delete(clientID);
943
+ if (clientID === awareness.clientID) {
944
+ const curMeta = awareness.meta.get(clientID);
945
+ awareness.meta.set(clientID, {
946
+ clock: curMeta.clock + 1,
947
+ lastUpdated: getUnixTime()
948
+ });
949
+ }
950
+ removed.push(clientID);
951
+ }
952
+ }
953
+ if (removed.length > 0) {
954
+ awareness.emit("change", [{
955
+ added: [],
956
+ updated: [],
957
+ removed
958
+ }, origin]);
959
+ awareness.emit("update", [{
960
+ added: [],
961
+ updated: [],
962
+ removed
963
+ }, origin]);
964
+ }
965
+ };
966
+ /**
967
+ * @param {Awareness} awareness
968
+ * @param {Array<number>} clients
969
+ * @return {Uint8Array}
970
+ */
971
+ const encodeAwarenessUpdate = (awareness, clients, states = awareness.states) => {
972
+ const len = clients.length;
973
+ const encoder = createEncoder();
974
+ writeVarUint(encoder, len);
975
+ for (let i = 0; i < len; i++) {
976
+ const clientID = clients[i];
977
+ const state = states.get(clientID) || null;
978
+ const clock = awareness.meta.get(clientID).clock;
979
+ writeVarUint(encoder, clientID);
980
+ writeVarUint(encoder, clock);
981
+ writeVarString(encoder, JSON.stringify(state));
982
+ }
983
+ return toUint8Array(encoder);
984
+ };
985
+ /**
986
+ * @param {Awareness} awareness
987
+ * @param {Uint8Array} update
988
+ * @param {any} origin This will be added to the emitted change event
989
+ */
990
+ const applyAwarenessUpdate = (awareness, update, origin) => {
991
+ const decoder = createDecoder(update);
992
+ const timestamp = getUnixTime();
993
+ const added = [];
994
+ const updated = [];
995
+ const filteredUpdated = [];
996
+ const removed = [];
997
+ const len = readVarUint(decoder);
998
+ for (let i = 0; i < len; i++) {
999
+ const clientID = readVarUint(decoder);
1000
+ let clock = readVarUint(decoder);
1001
+ const state = JSON.parse(readVarString(decoder));
1002
+ const clientMeta = awareness.meta.get(clientID);
1003
+ const prevState = awareness.states.get(clientID);
1004
+ const currClock = clientMeta === void 0 ? 0 : clientMeta.clock;
1005
+ if (currClock < clock || currClock === clock && state === null && awareness.states.has(clientID)) {
1006
+ if (state === null) if (clientID === awareness.clientID && awareness.getLocalState() != null) clock++;
1007
+ else awareness.states.delete(clientID);
1008
+ else awareness.states.set(clientID, state);
1009
+ awareness.meta.set(clientID, {
1010
+ clock,
1011
+ lastUpdated: timestamp
1012
+ });
1013
+ if (clientMeta === void 0 && state !== null) added.push(clientID);
1014
+ else if (clientMeta !== void 0 && state === null) removed.push(clientID);
1015
+ else if (state !== null) {
1016
+ if (!equalityDeep(state, prevState)) filteredUpdated.push(clientID);
1017
+ updated.push(clientID);
1018
+ }
1019
+ }
1020
+ }
1021
+ if (added.length > 0 || filteredUpdated.length > 0 || removed.length > 0) awareness.emit("change", [{
1022
+ added,
1023
+ updated: filteredUpdated,
1024
+ removed
1025
+ }, origin]);
1026
+ if (added.length > 0 || updated.length > 0 || removed.length > 0) awareness.emit("update", [{
1027
+ added,
1028
+ updated,
1029
+ removed
1030
+ }, origin]);
1031
+ };
1032
+
1033
+ //#endregion
1034
+ Object.defineProperty(exports, 'Awareness', {
1035
+ enumerable: true,
1036
+ get: function () {
1037
+ return Awareness;
1038
+ }
1039
+ });
1040
+ Object.defineProperty(exports, 'WEAVE_STORE_AZURE_WEB_PUBSUB', {
1041
+ enumerable: true,
1042
+ get: function () {
1043
+ return WEAVE_STORE_AZURE_WEB_PUBSUB;
1044
+ }
1045
+ });
1046
+ Object.defineProperty(exports, 'WEAVE_STORE_AZURE_WEB_PUBSUB_CONNECTION_STATUS', {
1047
+ enumerable: true,
1048
+ get: function () {
1049
+ return WEAVE_STORE_AZURE_WEB_PUBSUB_CONNECTION_STATUS;
1050
+ }
1051
+ });
1052
+ Object.defineProperty(exports, '__toESM', {
1053
+ enumerable: true,
1054
+ get: function () {
1055
+ return __toESM;
1056
+ }
1057
+ });
1058
+ Object.defineProperty(exports, 'applyAwarenessUpdate', {
1059
+ enumerable: true,
1060
+ get: function () {
1061
+ return applyAwarenessUpdate;
1062
+ }
1063
+ });
1064
+ Object.defineProperty(exports, 'createDecoder', {
1065
+ enumerable: true,
1066
+ get: function () {
1067
+ return createDecoder;
1068
+ }
1069
+ });
1070
+ Object.defineProperty(exports, 'createEncoder', {
1071
+ enumerable: true,
1072
+ get: function () {
1073
+ return createEncoder;
1074
+ }
1075
+ });
1076
+ Object.defineProperty(exports, 'encodeAwarenessUpdate', {
1077
+ enumerable: true,
1078
+ get: function () {
1079
+ return encodeAwarenessUpdate;
1080
+ }
1081
+ });
1082
+ Object.defineProperty(exports, 'length', {
1083
+ enumerable: true,
1084
+ get: function () {
1085
+ return length$1;
1086
+ }
1087
+ });
1088
+ Object.defineProperty(exports, 'messageYjsSyncStep1', {
1089
+ enumerable: true,
1090
+ get: function () {
1091
+ return messageYjsSyncStep1;
1092
+ }
1093
+ });
1094
+ Object.defineProperty(exports, 'messageYjsSyncStep2', {
1095
+ enumerable: true,
1096
+ get: function () {
1097
+ return messageYjsSyncStep2;
1098
+ }
1099
+ });
1100
+ Object.defineProperty(exports, 'readSyncMessage', {
1101
+ enumerable: true,
1102
+ get: function () {
1103
+ return readSyncMessage;
1104
+ }
1105
+ });
1106
+ Object.defineProperty(exports, 'readVarUint', {
1107
+ enumerable: true,
1108
+ get: function () {
1109
+ return readVarUint;
1110
+ }
1111
+ });
1112
+ Object.defineProperty(exports, 'readVarUint8Array', {
1113
+ enumerable: true,
1114
+ get: function () {
1115
+ return readVarUint8Array;
1116
+ }
1117
+ });
1118
+ Object.defineProperty(exports, 'removeAwarenessStates', {
1119
+ enumerable: true,
1120
+ get: function () {
1121
+ return removeAwarenessStates;
1122
+ }
1123
+ });
1124
+ Object.defineProperty(exports, 'toUint8Array', {
1125
+ enumerable: true,
1126
+ get: function () {
1127
+ return toUint8Array;
1128
+ }
1129
+ });
1130
+ Object.defineProperty(exports, 'writeSyncStep1', {
1131
+ enumerable: true,
1132
+ get: function () {
1133
+ return writeSyncStep1;
1134
+ }
1135
+ });
1136
+ Object.defineProperty(exports, 'writeUpdate', {
1137
+ enumerable: true,
1138
+ get: function () {
1139
+ return writeUpdate;
1140
+ }
1141
+ });
1142
+ Object.defineProperty(exports, 'writeVarUint', {
1143
+ enumerable: true,
1144
+ get: function () {
1145
+ return writeVarUint;
1146
+ }
1147
+ });
1148
+ Object.defineProperty(exports, 'writeVarUint8Array', {
1149
+ enumerable: true,
1150
+ get: function () {
1151
+ return writeVarUint8Array;
1152
+ }
1153
+ });