@hocuspocus/provider 4.0.0-rc.6 → 4.0.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.
@@ -1,1002 +1,15 @@
1
1
  import { WsReadyStates, awarenessStatesToArray, makeRoutingKey, parseRoutingKey, readAuthMessage, writeAuthentication } from "@hocuspocus/common";
2
+ import * as awarenessProtocol from "y-protocols/awareness";
3
+ import { Awareness, encodeAwarenessUpdate, removeAwarenessStates } from "y-protocols/awareness";
2
4
  import * as Y from "yjs";
3
5
  import { retry } from "@lifeomic/attempt";
6
+ import { createDecoder, peekVarString, readVarInt, readVarString, readVarUint, readVarUint8Array } from "lib0/decoding";
7
+ import * as encoding from "lib0/encoding";
8
+ import { createEncoder, length, toUint8Array, writeVarString, writeVarUint, writeVarUint8Array } from "lib0/encoding";
9
+ import * as time from "lib0/time";
10
+ import * as syncProtocol from "y-protocols/sync";
11
+ import { messageYjsSyncStep2, readSyncMessage, writeUpdate } from "y-protocols/sync";
4
12
 
5
- //#region node_modules/.pnpm/lib0@0.2.117/node_modules/lib0/math.js
6
- /**
7
- * Common Math expressions.
8
- *
9
- * @module math
10
- */
11
- const floor = Math.floor;
12
- /**
13
- * @function
14
- * @param {number} a
15
- * @param {number} b
16
- * @return {number} The smaller element of a and b
17
- */
18
- const min = (a, b) => a < b ? a : b;
19
- /**
20
- * @function
21
- * @param {number} a
22
- * @param {number} b
23
- * @return {number} The bigger element of a and b
24
- */
25
- const max = (a, b) => a > b ? a : b;
26
- const isNaN$1 = Number.isNaN;
27
-
28
- //#endregion
29
- //#region node_modules/.pnpm/lib0@0.2.117/node_modules/lib0/binary.js
30
- const BIT7 = 64;
31
- const BIT8 = 128;
32
- const BIT18 = 1 << 17;
33
- const BIT19 = 1 << 18;
34
- const BIT20 = 1 << 19;
35
- const BIT21 = 1 << 20;
36
- const BIT22 = 1 << 21;
37
- const BIT23 = 1 << 22;
38
- const BIT24 = 1 << 23;
39
- const BIT25 = 1 << 24;
40
- const BIT26 = 1 << 25;
41
- const BIT27 = 1 << 26;
42
- const BIT28 = 1 << 27;
43
- const BIT29 = 1 << 28;
44
- const BIT30 = 1 << 29;
45
- const BIT31 = 1 << 30;
46
- const BIT32 = 1 << 31;
47
- const BITS6 = 63;
48
- const BITS7 = 127;
49
- const BITS17 = BIT18 - 1;
50
- const BITS18 = BIT19 - 1;
51
- const BITS19 = BIT20 - 1;
52
- const BITS20 = BIT21 - 1;
53
- const BITS21 = BIT22 - 1;
54
- const BITS22 = BIT23 - 1;
55
- const BITS23 = BIT24 - 1;
56
- const BITS24 = BIT25 - 1;
57
- const BITS25 = BIT26 - 1;
58
- const BITS26 = BIT27 - 1;
59
- const BITS27 = BIT28 - 1;
60
- const BITS28 = BIT29 - 1;
61
- const BITS29 = BIT30 - 1;
62
- const BITS30 = BIT31 - 1;
63
- /**
64
- * @type {number}
65
- */
66
- const BITS31 = 2147483647;
67
- /**
68
- * @type {number}
69
- */
70
- const BITS32 = 4294967295;
71
-
72
- //#endregion
73
- //#region node_modules/.pnpm/lib0@0.2.117/node_modules/lib0/number.js
74
- /**
75
- * Utility helpers for working with numbers.
76
- *
77
- * @module number
78
- */
79
- const MAX_SAFE_INTEGER = Number.MAX_SAFE_INTEGER;
80
- const MIN_SAFE_INTEGER = Number.MIN_SAFE_INTEGER;
81
- const LOWEST_INT32 = 1 << 31;
82
- const HIGHEST_INT32 = BITS31;
83
- const HIGHEST_UINT32 = BITS32;
84
- /* c8 ignore next */
85
- const isInteger = Number.isInteger || ((num) => typeof num === "number" && isFinite(num) && floor(num) === num);
86
- const isNaN = Number.isNaN;
87
- const parseInt = Number.parseInt;
88
-
89
- //#endregion
90
- //#region node_modules/.pnpm/lib0@0.2.117/node_modules/lib0/set.js
91
- /**
92
- * Utility module to work with sets.
93
- *
94
- * @module set
95
- */
96
- const create$2 = () => /* @__PURE__ */ new Set();
97
-
98
- //#endregion
99
- //#region node_modules/.pnpm/lib0@0.2.117/node_modules/lib0/array.js
100
- /**
101
- * Transforms something array-like to an actual Array.
102
- *
103
- * @function
104
- * @template T
105
- * @param {ArrayLike<T>|Iterable<T>} arraylike
106
- * @return {T}
107
- */
108
- const from = Array.from;
109
- const isArray$1 = Array.isArray;
110
-
111
- //#endregion
112
- //#region node_modules/.pnpm/lib0@0.2.117/node_modules/lib0/string.js
113
- /**
114
- * Utility module to work with strings.
115
- *
116
- * @module string
117
- */
118
- const fromCharCode = String.fromCharCode;
119
- const fromCodePoint = String.fromCodePoint;
120
- /**
121
- * The largest utf16 character.
122
- * Corresponds to Uint8Array([255, 255]) or charcodeof(2x2^8)
123
- */
124
- const MAX_UTF16_CHARACTER = fromCharCode(65535);
125
- /**
126
- * @param {string} str
127
- * @return {Uint8Array<ArrayBuffer>}
128
- */
129
- const _encodeUtf8Polyfill = (str) => {
130
- const encodedString = unescape(encodeURIComponent(str));
131
- const len = encodedString.length;
132
- const buf = new Uint8Array(len);
133
- for (let i = 0; i < len; i++) buf[i] = encodedString.codePointAt(i);
134
- return buf;
135
- };
136
- /* c8 ignore next */
137
- const utf8TextEncoder = typeof TextEncoder !== "undefined" ? new TextEncoder() : null;
138
- /**
139
- * @param {string} str
140
- * @return {Uint8Array<ArrayBuffer>}
141
- */
142
- const _encodeUtf8Native = (str) => utf8TextEncoder.encode(str);
143
- /**
144
- * @param {string} str
145
- * @return {Uint8Array}
146
- */
147
- /* c8 ignore next */
148
- const encodeUtf8 = utf8TextEncoder ? _encodeUtf8Native : _encodeUtf8Polyfill;
149
- /* c8 ignore next */
150
- let utf8TextDecoder = typeof TextDecoder === "undefined" ? null : new TextDecoder("utf-8", {
151
- fatal: true,
152
- ignoreBOM: true
153
- });
154
- /* c8 ignore start */
155
- if (utf8TextDecoder && utf8TextDecoder.decode(new Uint8Array()).length === 1)
156
- /* c8 ignore next */
157
- utf8TextDecoder = null;
158
-
159
- //#endregion
160
- //#region node_modules/.pnpm/lib0@0.2.117/node_modules/lib0/encoding.js
161
- /**
162
- * Efficient schema-less binary encoding with support for variable length encoding.
163
- *
164
- * Use [lib0/encoding] with [lib0/decoding]. Every encoding function has a corresponding decoding function.
165
- *
166
- * Encodes numbers in little-endian order (least to most significant byte order)
167
- * and is compatible with Golang's binary encoding (https://golang.org/pkg/encoding/binary/)
168
- * which is also used in Protocol Buffers.
169
- *
170
- * ```js
171
- * // encoding step
172
- * const encoder = encoding.createEncoder()
173
- * encoding.writeVarUint(encoder, 256)
174
- * encoding.writeVarString(encoder, 'Hello world!')
175
- * const buf = encoding.toUint8Array(encoder)
176
- * ```
177
- *
178
- * ```js
179
- * // decoding step
180
- * const decoder = decoding.createDecoder(buf)
181
- * decoding.readVarUint(decoder) // => 256
182
- * decoding.readVarString(decoder) // => 'Hello world!'
183
- * decoding.hasContent(decoder) // => false - all data is read
184
- * ```
185
- *
186
- * @module encoding
187
- */
188
- /**
189
- * A BinaryEncoder handles the encoding to an Uint8Array.
190
- */
191
- var Encoder = class {
192
- constructor() {
193
- this.cpos = 0;
194
- this.cbuf = new Uint8Array(100);
195
- /**
196
- * @type {Array<Uint8Array>}
197
- */
198
- this.bufs = [];
199
- }
200
- };
201
- /**
202
- * @function
203
- * @return {Encoder}
204
- */
205
- const createEncoder = () => new Encoder();
206
- /**
207
- * The current length of the encoded data.
208
- *
209
- * @function
210
- * @param {Encoder} encoder
211
- * @return {number}
212
- */
213
- const length = (encoder) => {
214
- let len = encoder.cpos;
215
- for (let i = 0; i < encoder.bufs.length; i++) len += encoder.bufs[i].length;
216
- return len;
217
- };
218
- /**
219
- * Transform to Uint8Array.
220
- *
221
- * @function
222
- * @param {Encoder} encoder
223
- * @return {Uint8Array<ArrayBuffer>} The created ArrayBuffer.
224
- */
225
- const toUint8Array = (encoder) => {
226
- const uint8arr = new Uint8Array(length(encoder));
227
- let curPos = 0;
228
- for (let i = 0; i < encoder.bufs.length; i++) {
229
- const d = encoder.bufs[i];
230
- uint8arr.set(d, curPos);
231
- curPos += d.length;
232
- }
233
- uint8arr.set(new Uint8Array(encoder.cbuf.buffer, 0, encoder.cpos), curPos);
234
- return uint8arr;
235
- };
236
- /**
237
- * Write one byte to the encoder.
238
- *
239
- * @function
240
- * @param {Encoder} encoder
241
- * @param {number} num The byte that is to be encoded.
242
- */
243
- const write = (encoder, num) => {
244
- const bufferLen = encoder.cbuf.length;
245
- if (encoder.cpos === bufferLen) {
246
- encoder.bufs.push(encoder.cbuf);
247
- encoder.cbuf = new Uint8Array(bufferLen * 2);
248
- encoder.cpos = 0;
249
- }
250
- encoder.cbuf[encoder.cpos++] = num;
251
- };
252
- /**
253
- * Write a variable length unsigned integer. Max encodable integer is 2^53.
254
- *
255
- * @function
256
- * @param {Encoder} encoder
257
- * @param {number} num The number that is to be encoded.
258
- */
259
- const writeVarUint = (encoder, num) => {
260
- while (num > BITS7) {
261
- write(encoder, BIT8 | BITS7 & num);
262
- num = floor(num / 128);
263
- }
264
- write(encoder, BITS7 & num);
265
- };
266
- /**
267
- * A cache to store strings temporarily
268
- */
269
- const _strBuffer = new Uint8Array(3e4);
270
- const _maxStrBSize = _strBuffer.length / 3;
271
- /**
272
- * Write a variable length string.
273
- *
274
- * @function
275
- * @param {Encoder} encoder
276
- * @param {String} str The string that is to be encoded.
277
- */
278
- const _writeVarStringNative = (encoder, str) => {
279
- if (str.length < _maxStrBSize) {
280
- /* c8 ignore next */
281
- const written = utf8TextEncoder.encodeInto(str, _strBuffer).written || 0;
282
- writeVarUint(encoder, written);
283
- for (let i = 0; i < written; i++) write(encoder, _strBuffer[i]);
284
- } else writeVarUint8Array(encoder, encodeUtf8(str));
285
- };
286
- /**
287
- * Write a variable length string.
288
- *
289
- * @function
290
- * @param {Encoder} encoder
291
- * @param {String} str The string that is to be encoded.
292
- */
293
- const _writeVarStringPolyfill = (encoder, str) => {
294
- const encodedString = unescape(encodeURIComponent(str));
295
- const len = encodedString.length;
296
- writeVarUint(encoder, len);
297
- for (let i = 0; i < len; i++) write(encoder, encodedString.codePointAt(i));
298
- };
299
- /**
300
- * Write a variable length string.
301
- *
302
- * @function
303
- * @param {Encoder} encoder
304
- * @param {String} str The string that is to be encoded.
305
- */
306
- /* c8 ignore next */
307
- const writeVarString = utf8TextEncoder && utf8TextEncoder.encodeInto ? _writeVarStringNative : _writeVarStringPolyfill;
308
- /**
309
- * Append fixed-length Uint8Array to the encoder.
310
- *
311
- * @function
312
- * @param {Encoder} encoder
313
- * @param {Uint8Array} uint8Array
314
- */
315
- const writeUint8Array = (encoder, uint8Array) => {
316
- const bufferLen = encoder.cbuf.length;
317
- const cpos = encoder.cpos;
318
- const leftCopyLen = min(bufferLen - cpos, uint8Array.length);
319
- const rightCopyLen = uint8Array.length - leftCopyLen;
320
- encoder.cbuf.set(uint8Array.subarray(0, leftCopyLen), cpos);
321
- encoder.cpos += leftCopyLen;
322
- if (rightCopyLen > 0) {
323
- encoder.bufs.push(encoder.cbuf);
324
- encoder.cbuf = new Uint8Array(max(bufferLen * 2, rightCopyLen));
325
- encoder.cbuf.set(uint8Array.subarray(leftCopyLen));
326
- encoder.cpos = rightCopyLen;
327
- }
328
- };
329
- /**
330
- * Append an Uint8Array to Encoder.
331
- *
332
- * @function
333
- * @param {Encoder} encoder
334
- * @param {Uint8Array} uint8Array
335
- */
336
- const writeVarUint8Array = (encoder, uint8Array) => {
337
- writeVarUint(encoder, uint8Array.byteLength);
338
- writeUint8Array(encoder, uint8Array);
339
- };
340
-
341
- //#endregion
342
- //#region node_modules/.pnpm/lib0@0.2.117/node_modules/lib0/error.js
343
- /**
344
- * Error helpers.
345
- *
346
- * @module error
347
- */
348
- /**
349
- * @param {string} s
350
- * @return {Error}
351
- */
352
- /* c8 ignore next */
353
- const create$1 = (s) => new Error(s);
354
-
355
- //#endregion
356
- //#region node_modules/.pnpm/lib0@0.2.117/node_modules/lib0/decoding.js
357
- /**
358
- * Efficient schema-less binary decoding with support for variable length encoding.
359
- *
360
- * Use [lib0/decoding] with [lib0/encoding]. Every encoding function has a corresponding decoding function.
361
- *
362
- * Encodes numbers in little-endian order (least to most significant byte order)
363
- * and is compatible with Golang's binary encoding (https://golang.org/pkg/encoding/binary/)
364
- * which is also used in Protocol Buffers.
365
- *
366
- * ```js
367
- * // encoding step
368
- * const encoder = encoding.createEncoder()
369
- * encoding.writeVarUint(encoder, 256)
370
- * encoding.writeVarString(encoder, 'Hello world!')
371
- * const buf = encoding.toUint8Array(encoder)
372
- * ```
373
- *
374
- * ```js
375
- * // decoding step
376
- * const decoder = decoding.createDecoder(buf)
377
- * decoding.readVarUint(decoder) // => 256
378
- * decoding.readVarString(decoder) // => 'Hello world!'
379
- * decoding.hasContent(decoder) // => false - all data is read
380
- * ```
381
- *
382
- * @module decoding
383
- */
384
- const errorUnexpectedEndOfArray = create$1("Unexpected end of array");
385
- const errorIntegerOutOfRange = create$1("Integer out of Range");
386
- /**
387
- * A Decoder handles the decoding of an Uint8Array.
388
- * @template {ArrayBufferLike} [Buf=ArrayBufferLike]
389
- */
390
- var Decoder = class {
391
- /**
392
- * @param {Uint8Array<Buf>} uint8Array Binary data to decode
393
- */
394
- constructor(uint8Array) {
395
- /**
396
- * Decoding target.
397
- *
398
- * @type {Uint8Array<Buf>}
399
- */
400
- this.arr = uint8Array;
401
- /**
402
- * Current decoding position.
403
- *
404
- * @type {number}
405
- */
406
- this.pos = 0;
407
- }
408
- };
409
- /**
410
- * @function
411
- * @template {ArrayBufferLike} Buf
412
- * @param {Uint8Array<Buf>} uint8Array
413
- * @return {Decoder<Buf>}
414
- */
415
- const createDecoder = (uint8Array) => new Decoder(uint8Array);
416
- /**
417
- * Create an Uint8Array view of the next `len` bytes and advance the position by `len`.
418
- *
419
- * Important: The Uint8Array still points to the underlying ArrayBuffer. Make sure to discard the result as soon as possible to prevent any memory leaks.
420
- * Use `buffer.copyUint8Array` to copy the result into a new Uint8Array.
421
- *
422
- * @function
423
- * @template {ArrayBufferLike} Buf
424
- * @param {Decoder<Buf>} decoder The decoder instance
425
- * @param {number} len The length of bytes to read
426
- * @return {Uint8Array<Buf>}
427
- */
428
- const readUint8Array = (decoder, len) => {
429
- const view = new Uint8Array(decoder.arr.buffer, decoder.pos + decoder.arr.byteOffset, len);
430
- decoder.pos += len;
431
- return view;
432
- };
433
- /**
434
- * Read variable length Uint8Array.
435
- *
436
- * Important: The Uint8Array still points to the underlying ArrayBuffer. Make sure to discard the result as soon as possible to prevent any memory leaks.
437
- * Use `buffer.copyUint8Array` to copy the result into a new Uint8Array.
438
- *
439
- * @function
440
- * @template {ArrayBufferLike} Buf
441
- * @param {Decoder<Buf>} decoder
442
- * @return {Uint8Array<Buf>}
443
- */
444
- const readVarUint8Array = (decoder) => readUint8Array(decoder, readVarUint(decoder));
445
- /**
446
- * Read one byte as unsigned integer.
447
- * @function
448
- * @param {Decoder} decoder The decoder instance
449
- * @return {number} Unsigned 8-bit integer
450
- */
451
- const readUint8 = (decoder) => decoder.arr[decoder.pos++];
452
- /**
453
- * Read unsigned integer (32bit) with variable length.
454
- * 1/8th of the storage is used as encoding overhead.
455
- * * numbers < 2^7 is stored in one bytlength
456
- * * numbers < 2^14 is stored in two bylength
457
- *
458
- * @function
459
- * @param {Decoder} decoder
460
- * @return {number} An unsigned integer.length
461
- */
462
- const readVarUint = (decoder) => {
463
- let num = 0;
464
- let mult = 1;
465
- const len = decoder.arr.length;
466
- while (decoder.pos < len) {
467
- const r = decoder.arr[decoder.pos++];
468
- num = num + (r & BITS7) * mult;
469
- mult *= 128;
470
- if (r < BIT8) return num;
471
- /* c8 ignore start */
472
- if (num > MAX_SAFE_INTEGER) throw errorIntegerOutOfRange;
473
- }
474
- throw errorUnexpectedEndOfArray;
475
- };
476
- /**
477
- * Read signed integer (32bit) with variable length.
478
- * 1/8th of the storage is used as encoding overhead.
479
- * * numbers < 2^7 is stored in one bytlength
480
- * * numbers < 2^14 is stored in two bylength
481
- * @todo This should probably create the inverse ~num if number is negative - but this would be a breaking change.
482
- *
483
- * @function
484
- * @param {Decoder} decoder
485
- * @return {number} An unsigned integer.length
486
- */
487
- const readVarInt = (decoder) => {
488
- let r = decoder.arr[decoder.pos++];
489
- let num = r & BITS6;
490
- let mult = 64;
491
- const sign = (r & BIT7) > 0 ? -1 : 1;
492
- if ((r & BIT8) === 0) return sign * num;
493
- const len = decoder.arr.length;
494
- while (decoder.pos < len) {
495
- r = decoder.arr[decoder.pos++];
496
- num = num + (r & BITS7) * mult;
497
- mult *= 128;
498
- if (r < BIT8) return sign * num;
499
- /* c8 ignore start */
500
- if (num > MAX_SAFE_INTEGER) throw errorIntegerOutOfRange;
501
- }
502
- throw errorUnexpectedEndOfArray;
503
- };
504
- /**
505
- * We don't test this function anymore as we use native decoding/encoding by default now.
506
- * Better not modify this anymore..
507
- *
508
- * Transforming utf8 to a string is pretty expensive. The code performs 10x better
509
- * when String.fromCodePoint is fed with all characters as arguments.
510
- * But most environments have a maximum number of arguments per functions.
511
- * For effiency reasons we apply a maximum of 10000 characters at once.
512
- *
513
- * @function
514
- * @param {Decoder} decoder
515
- * @return {String} The read String.
516
- */
517
- /* c8 ignore start */
518
- const _readVarStringPolyfill = (decoder) => {
519
- let remainingLen = readVarUint(decoder);
520
- if (remainingLen === 0) return "";
521
- else {
522
- let encodedString = String.fromCodePoint(readUint8(decoder));
523
- if (--remainingLen < 100) while (remainingLen--) encodedString += String.fromCodePoint(readUint8(decoder));
524
- else while (remainingLen > 0) {
525
- const nextLen = remainingLen < 1e4 ? remainingLen : 1e4;
526
- const bytes = decoder.arr.subarray(decoder.pos, decoder.pos + nextLen);
527
- decoder.pos += nextLen;
528
- encodedString += String.fromCodePoint.apply(null, bytes);
529
- remainingLen -= nextLen;
530
- }
531
- return decodeURIComponent(escape(encodedString));
532
- }
533
- };
534
- /* c8 ignore stop */
535
- /**
536
- * @function
537
- * @param {Decoder} decoder
538
- * @return {String} The read String
539
- */
540
- const _readVarStringNative = (decoder) => utf8TextDecoder.decode(readVarUint8Array(decoder));
541
- /**
542
- * Read string of variable length
543
- * * varUint is used to store the length of the string
544
- *
545
- * @function
546
- * @param {Decoder} decoder
547
- * @return {String} The read String
548
- *
549
- */
550
- /* c8 ignore next */
551
- const readVarString = utf8TextDecoder ? _readVarStringNative : _readVarStringPolyfill;
552
- /**
553
- * Look ahead and read varString without incrementing position
554
- *
555
- * @function
556
- * @param {Decoder} decoder
557
- * @return {string}
558
- */
559
- const peekVarString = (decoder) => {
560
- const pos = decoder.pos;
561
- const s = readVarString(decoder);
562
- decoder.pos = pos;
563
- return s;
564
- };
565
-
566
- //#endregion
567
- //#region node_modules/.pnpm/lib0@0.2.117/node_modules/lib0/time.js
568
- /**
569
- * Return current unix time.
570
- *
571
- * @return {number}
572
- */
573
- const getUnixTime = Date.now;
574
-
575
- //#endregion
576
- //#region node_modules/.pnpm/lib0@0.2.117/node_modules/lib0/map.js
577
- /**
578
- * Utility module to work with key-value stores.
579
- *
580
- * @module map
581
- */
582
- /**
583
- * @template K
584
- * @template V
585
- * @typedef {Map<K,V>} GlobalMap
586
- */
587
- /**
588
- * Creates a new Map instance.
589
- *
590
- * @function
591
- * @return {Map<any, any>}
592
- *
593
- * @function
594
- */
595
- const create = () => /* @__PURE__ */ new Map();
596
- /**
597
- * Get map property. Create T if property is undefined and set T on map.
598
- *
599
- * ```js
600
- * const listeners = map.setIfUndefined(events, 'eventName', set.create)
601
- * listeners.add(listener)
602
- * ```
603
- *
604
- * @function
605
- * @template {Map<any, any>} MAP
606
- * @template {MAP extends Map<any,infer V> ? function():V : unknown} CF
607
- * @param {MAP} map
608
- * @param {MAP extends Map<infer K,any> ? K : unknown} key
609
- * @param {CF} createT
610
- * @return {ReturnType<CF>}
611
- */
612
- const setIfUndefined = (map, key, createT) => {
613
- let set = map.get(key);
614
- if (set === void 0) map.set(key, set = createT());
615
- return set;
616
- };
617
-
618
- //#endregion
619
- //#region node_modules/.pnpm/lib0@0.2.117/node_modules/lib0/observable.js
620
- /**
621
- * Observable class prototype.
622
- *
623
- * @module observable
624
- */
625
- /* c8 ignore start */
626
- /**
627
- * Handles named events.
628
- *
629
- * @deprecated
630
- * @template N
631
- */
632
- var Observable = class {
633
- constructor() {
634
- /**
635
- * Some desc.
636
- * @type {Map<N, any>}
637
- */
638
- this._observers = create();
639
- }
640
- /**
641
- * @param {N} name
642
- * @param {function} f
643
- */
644
- on(name, f) {
645
- setIfUndefined(this._observers, name, create$2).add(f);
646
- }
647
- /**
648
- * @param {N} name
649
- * @param {function} f
650
- */
651
- once(name, f) {
652
- /**
653
- * @param {...any} args
654
- */
655
- const _f = (...args) => {
656
- this.off(name, _f);
657
- f(...args);
658
- };
659
- this.on(name, _f);
660
- }
661
- /**
662
- * @param {N} name
663
- * @param {function} f
664
- */
665
- off(name, f) {
666
- const observers = this._observers.get(name);
667
- if (observers !== void 0) {
668
- observers.delete(f);
669
- if (observers.size === 0) this._observers.delete(name);
670
- }
671
- }
672
- /**
673
- * Emit a named event. All registered event listeners that listen to the
674
- * specified name will receive the event.
675
- *
676
- * @todo This should catch exceptions
677
- *
678
- * @param {N} name The event name.
679
- * @param {Array<any>} args The arguments that are applied to the event listener.
680
- */
681
- emit(name, args) {
682
- return from((this._observers.get(name) || create()).values()).forEach((f) => f(...args));
683
- }
684
- destroy() {
685
- this._observers = create();
686
- }
687
- };
688
- /* c8 ignore end */
689
-
690
- //#endregion
691
- //#region node_modules/.pnpm/lib0@0.2.117/node_modules/lib0/trait/equality.js
692
- const EqualityTraitSymbol = Symbol("Equality");
693
-
694
- //#endregion
695
- //#region node_modules/.pnpm/lib0@0.2.117/node_modules/lib0/object.js
696
- /**
697
- * @param {Object<string,any>} obj
698
- */
699
- const keys = Object.keys;
700
- /**
701
- * @param {Object<string,any>} obj
702
- * @return {number}
703
- */
704
- const size = (obj) => keys(obj).length;
705
- /**
706
- * Calls `Object.prototype.hasOwnProperty`.
707
- *
708
- * @param {any} obj
709
- * @param {string|number|symbol} key
710
- * @return {boolean}
711
- */
712
- const hasProperty = (obj, key) => Object.prototype.hasOwnProperty.call(obj, key);
713
-
714
- //#endregion
715
- //#region node_modules/.pnpm/lib0@0.2.117/node_modules/lib0/function.js
716
- /**
717
- * Common functions and function call helpers.
718
- *
719
- * @module function
720
- */
721
- /* c8 ignore start */
722
- /**
723
- * @param {any} a
724
- * @param {any} b
725
- * @return {boolean}
726
- */
727
- const equalityDeep = (a, b) => {
728
- if (a === b) return true;
729
- if (a == null || b == null || a.constructor !== b.constructor && (a.constructor || Object) !== (b.constructor || Object)) return false;
730
- if (a[EqualityTraitSymbol] != null) return a[EqualityTraitSymbol](b);
731
- switch (a.constructor) {
732
- case ArrayBuffer:
733
- a = new Uint8Array(a);
734
- b = new Uint8Array(b);
735
- case Uint8Array:
736
- if (a.byteLength !== b.byteLength) return false;
737
- for (let i = 0; i < a.length; i++) if (a[i] !== b[i]) return false;
738
- break;
739
- case Set:
740
- if (a.size !== b.size) return false;
741
- for (const value of a) if (!b.has(value)) return false;
742
- break;
743
- case Map:
744
- if (a.size !== b.size) return false;
745
- for (const key of a.keys()) if (!b.has(key) || !equalityDeep(a.get(key), b.get(key))) return false;
746
- break;
747
- case void 0:
748
- case Object:
749
- if (size(a) !== size(b)) return false;
750
- for (const key in a) if (!hasProperty(a, key) || !equalityDeep(a[key], b[key])) return false;
751
- break;
752
- case Array:
753
- if (a.length !== b.length) return false;
754
- for (let i = 0; i < a.length; i++) if (!equalityDeep(a[i], b[i])) return false;
755
- break;
756
- default: return false;
757
- }
758
- return true;
759
- };
760
- /* c8 ignore stop */
761
- const isArray = isArray$1;
762
-
763
- //#endregion
764
- //#region node_modules/.pnpm/y-protocols@1.0.7_yjs@13.6.29/node_modules/y-protocols/awareness.js
765
- /**
766
- * @module awareness-protocol
767
- */
768
- const outdatedTimeout = 3e4;
769
- /**
770
- * @typedef {Object} MetaClientState
771
- * @property {number} MetaClientState.clock
772
- * @property {number} MetaClientState.lastUpdated unix timestamp
773
- */
774
- /**
775
- * The Awareness class implements a simple shared state protocol that can be used for non-persistent data like awareness information
776
- * (cursor, username, status, ..). Each client can update its own local state and listen to state changes of
777
- * remote clients. Every client may set a state of a remote peer to `null` to mark the client as offline.
778
- *
779
- * Each client is identified by a unique client id (something we borrow from `doc.clientID`). A client can override
780
- * its own state by propagating a message with an increasing timestamp (`clock`). If such a message is received, it is
781
- * applied if the known state of that client is older than the new state (`clock < newClock`). If a client thinks that
782
- * a remote client is offline, it may propagate a message with
783
- * `{ clock: currentClientClock, state: null, client: remoteClient }`. If such a
784
- * message is received, and the known clock of that client equals the received clock, it will override the state with `null`.
785
- *
786
- * Before a client disconnects, it should propagate a `null` state with an updated clock.
787
- *
788
- * Awareness states must be updated every 30 seconds. Otherwise the Awareness instance will delete the client state.
789
- *
790
- * @extends {Observable<string>}
791
- */
792
- var Awareness = class extends Observable {
793
- /**
794
- * @param {Y.Doc} doc
795
- */
796
- constructor(doc) {
797
- super();
798
- this.doc = doc;
799
- /**
800
- * @type {number}
801
- */
802
- this.clientID = doc.clientID;
803
- /**
804
- * Maps from client id to client state
805
- * @type {Map<number, Object<string, any>>}
806
- */
807
- this.states = /* @__PURE__ */ new Map();
808
- /**
809
- * @type {Map<number, MetaClientState>}
810
- */
811
- this.meta = /* @__PURE__ */ new Map();
812
- this._checkInterval = setInterval(() => {
813
- const now = getUnixTime();
814
- if (this.getLocalState() !== null && outdatedTimeout / 2 <= now - this.meta.get(this.clientID).lastUpdated) this.setLocalState(this.getLocalState());
815
- /**
816
- * @type {Array<number>}
817
- */
818
- const remove = [];
819
- this.meta.forEach((meta, clientid) => {
820
- if (clientid !== this.clientID && outdatedTimeout <= now - meta.lastUpdated && this.states.has(clientid)) remove.push(clientid);
821
- });
822
- if (remove.length > 0) removeAwarenessStates(this, remove, "timeout");
823
- }, floor(outdatedTimeout / 10));
824
- doc.on("destroy", () => {
825
- this.destroy();
826
- });
827
- this.setLocalState({});
828
- }
829
- destroy() {
830
- this.emit("destroy", [this]);
831
- this.setLocalState(null);
832
- super.destroy();
833
- clearInterval(this._checkInterval);
834
- }
835
- /**
836
- * @return {Object<string,any>|null}
837
- */
838
- getLocalState() {
839
- return this.states.get(this.clientID) || null;
840
- }
841
- /**
842
- * @param {Object<string,any>|null} state
843
- */
844
- setLocalState(state) {
845
- const clientID = this.clientID;
846
- const currLocalMeta = this.meta.get(clientID);
847
- const clock = currLocalMeta === void 0 ? 0 : currLocalMeta.clock + 1;
848
- const prevState = this.states.get(clientID);
849
- if (state === null) this.states.delete(clientID);
850
- else this.states.set(clientID, state);
851
- this.meta.set(clientID, {
852
- clock,
853
- lastUpdated: getUnixTime()
854
- });
855
- const added = [];
856
- const updated = [];
857
- const filteredUpdated = [];
858
- const removed = [];
859
- if (state === null) removed.push(clientID);
860
- else if (prevState == null) {
861
- if (state != null) added.push(clientID);
862
- } else {
863
- updated.push(clientID);
864
- if (!equalityDeep(prevState, state)) filteredUpdated.push(clientID);
865
- }
866
- if (added.length > 0 || filteredUpdated.length > 0 || removed.length > 0) this.emit("change", [{
867
- added,
868
- updated: filteredUpdated,
869
- removed
870
- }, "local"]);
871
- this.emit("update", [{
872
- added,
873
- updated,
874
- removed
875
- }, "local"]);
876
- }
877
- /**
878
- * @param {string} field
879
- * @param {any} value
880
- */
881
- setLocalStateField(field, value) {
882
- const state = this.getLocalState();
883
- if (state !== null) this.setLocalState({
884
- ...state,
885
- [field]: value
886
- });
887
- }
888
- /**
889
- * @return {Map<number,Object<string,any>>}
890
- */
891
- getStates() {
892
- return this.states;
893
- }
894
- };
895
- /**
896
- * Mark (remote) clients as inactive and remove them from the list of active peers.
897
- * This change will be propagated to remote clients.
898
- *
899
- * @param {Awareness} awareness
900
- * @param {Array<number>} clients
901
- * @param {any} origin
902
- */
903
- const removeAwarenessStates = (awareness, clients, origin) => {
904
- const removed = [];
905
- for (let i = 0; i < clients.length; i++) {
906
- const clientID = clients[i];
907
- if (awareness.states.has(clientID)) {
908
- awareness.states.delete(clientID);
909
- if (clientID === awareness.clientID) {
910
- const curMeta = awareness.meta.get(clientID);
911
- awareness.meta.set(clientID, {
912
- clock: curMeta.clock + 1,
913
- lastUpdated: getUnixTime()
914
- });
915
- }
916
- removed.push(clientID);
917
- }
918
- }
919
- if (removed.length > 0) {
920
- awareness.emit("change", [{
921
- added: [],
922
- updated: [],
923
- removed
924
- }, origin]);
925
- awareness.emit("update", [{
926
- added: [],
927
- updated: [],
928
- removed
929
- }, origin]);
930
- }
931
- };
932
- /**
933
- * @param {Awareness} awareness
934
- * @param {Array<number>} clients
935
- * @return {Uint8Array}
936
- */
937
- const encodeAwarenessUpdate = (awareness, clients, states = awareness.states) => {
938
- const len = clients.length;
939
- const encoder = createEncoder();
940
- writeVarUint(encoder, len);
941
- for (let i = 0; i < len; i++) {
942
- const clientID = clients[i];
943
- const state = states.get(clientID) || null;
944
- const clock = awareness.meta.get(clientID).clock;
945
- writeVarUint(encoder, clientID);
946
- writeVarUint(encoder, clock);
947
- writeVarString(encoder, JSON.stringify(state));
948
- }
949
- return toUint8Array(encoder);
950
- };
951
- /**
952
- * @param {Awareness} awareness
953
- * @param {Uint8Array} update
954
- * @param {any} origin This will be added to the emitted change event
955
- */
956
- const applyAwarenessUpdate = (awareness, update, origin) => {
957
- const decoder = createDecoder(update);
958
- const timestamp = getUnixTime();
959
- const added = [];
960
- const updated = [];
961
- const filteredUpdated = [];
962
- const removed = [];
963
- const len = readVarUint(decoder);
964
- for (let i = 0; i < len; i++) {
965
- const clientID = readVarUint(decoder);
966
- let clock = readVarUint(decoder);
967
- const state = JSON.parse(readVarString(decoder));
968
- const clientMeta = awareness.meta.get(clientID);
969
- const prevState = awareness.states.get(clientID);
970
- const currClock = clientMeta === void 0 ? 0 : clientMeta.clock;
971
- if (currClock < clock || currClock === clock && state === null && awareness.states.has(clientID)) {
972
- if (state === null) if (clientID === awareness.clientID && awareness.getLocalState() != null) clock++;
973
- else awareness.states.delete(clientID);
974
- else awareness.states.set(clientID, state);
975
- awareness.meta.set(clientID, {
976
- clock,
977
- lastUpdated: timestamp
978
- });
979
- if (clientMeta === void 0 && state !== null) added.push(clientID);
980
- else if (clientMeta !== void 0 && state === null) removed.push(clientID);
981
- else if (state !== null) {
982
- if (!equalityDeep(state, prevState)) filteredUpdated.push(clientID);
983
- updated.push(clientID);
984
- }
985
- }
986
- }
987
- if (added.length > 0 || filteredUpdated.length > 0 || removed.length > 0) awareness.emit("change", [{
988
- added,
989
- updated: filteredUpdated,
990
- removed
991
- }, origin]);
992
- if (added.length > 0 || updated.length > 0 || removed.length > 0) awareness.emit("update", [{
993
- added,
994
- updated,
995
- removed
996
- }, origin]);
997
- };
998
-
999
- //#endregion
1000
13
  //#region packages/provider/src/EventEmitter.ts
1001
14
  var EventEmitter = class {
1002
15
  constructor() {
@@ -1101,8 +114,8 @@ var CloseMessage = class extends OutgoingMessage {
1101
114
  this.description = "Ask the server to close the connection";
1102
115
  }
1103
116
  get(args) {
1104
- writeVarString(this.encoder, args.documentName);
1105
- writeVarUint(this.encoder, this.type);
117
+ encoding.writeVarString(this.encoder, args.documentName);
118
+ encoding.writeVarUint(this.encoder, this.type);
1106
119
  return this.encoder;
1107
120
  }
1108
121
  };
@@ -1291,7 +304,7 @@ var HocuspocusProviderWebsocket = class HocuspocusProviderWebsocket extends Even
1291
304
  }
1292
305
  onMessage(event) {
1293
306
  this.resolveConnectionAttempt();
1294
- this.lastMessageReceived = getUnixTime();
307
+ this.lastMessageReceived = time.getUnixTime();
1295
308
  const data = new Uint8Array(event.data);
1296
309
  if (data.length === 1 && data[0] === MessageType.Ping) {
1297
310
  this.sendPong();
@@ -1304,9 +317,9 @@ var HocuspocusProviderWebsocket = class HocuspocusProviderWebsocket extends Even
1304
317
  * Send application-level Pong response to server Ping
1305
318
  */
1306
319
  sendPong() {
1307
- const encoder = createEncoder();
1308
- writeVarUint(encoder, MessageType.Pong);
1309
- this.send(toUint8Array(encoder));
320
+ const encoder = encoding.createEncoder();
321
+ encoding.writeVarUint(encoder, MessageType.Pong);
322
+ this.send(encoding.toUint8Array(encoder));
1310
323
  }
1311
324
  resolveConnectionAttempt() {
1312
325
  if (this.connectionAttempt) {
@@ -1329,7 +342,7 @@ var HocuspocusProviderWebsocket = class HocuspocusProviderWebsocket extends Even
1329
342
  checkConnection() {
1330
343
  if (this.status !== WebSocketStatus.Connected) return;
1331
344
  if (!this.lastMessageReceived) return;
1332
- if (this.configuration.messageReconnectTimeout >= getUnixTime() - this.lastMessageReceived) return;
345
+ if (this.configuration.messageReconnectTimeout >= time.getUnixTime() - this.lastMessageReceived) return;
1333
346
  this.closeTries += 1;
1334
347
  if (this.closeTries > 2) {
1335
348
  this.onClose({ event: {
@@ -1409,126 +422,6 @@ var HocuspocusProviderWebsocket = class HocuspocusProviderWebsocket extends Even
1409
422
  }
1410
423
  };
1411
424
 
1412
- //#endregion
1413
- //#region node_modules/.pnpm/y-protocols@1.0.7_yjs@13.6.29/node_modules/y-protocols/sync.js
1414
- /**
1415
- * @module sync-protocol
1416
- */
1417
- /**
1418
- * @typedef {Map<number, number>} StateMap
1419
- */
1420
- /**
1421
- * Core Yjs defines two message types:
1422
- * • YjsSyncStep1: Includes the State Set of the sending client. When received, the client should reply with YjsSyncStep2.
1423
- * • YjsSyncStep2: Includes all missing structs and the complete delete set. When received, the client is assured that it
1424
- * received all information from the remote client.
1425
- *
1426
- * In a peer-to-peer network, you may want to introduce a SyncDone message type. Both parties should initiate the connection
1427
- * with SyncStep1. When a client received SyncStep2, it should reply with SyncDone. When the local client received both
1428
- * SyncStep2 and SyncDone, it is assured that it is synced to the remote client.
1429
- *
1430
- * In a client-server model, you want to handle this differently: The client should initiate the connection with SyncStep1.
1431
- * When the server receives SyncStep1, it should reply with SyncStep2 immediately followed by SyncStep1. The client replies
1432
- * with SyncStep2 when it receives SyncStep1. Optionally the server may send a SyncDone after it received SyncStep2, so the
1433
- * client knows that the sync is finished. There are two reasons for this more elaborated sync model: 1. This protocol can
1434
- * easily be implemented on top of http and websockets. 2. The server should only reply to requests, and not initiate them.
1435
- * Therefore it is necessary that the client initiates the sync.
1436
- *
1437
- * Construction of a message:
1438
- * [messageType : varUint, message definition..]
1439
- *
1440
- * Note: A message does not include information about the room name. This must to be handled by the upper layer protocol!
1441
- *
1442
- * stringify[messageType] stringifies a message definition (messageType is already read from the bufffer)
1443
- */
1444
- const messageYjsSyncStep1 = 0;
1445
- const messageYjsSyncStep2 = 1;
1446
- const messageYjsUpdate = 2;
1447
- /**
1448
- * Create a sync step 1 message based on the state of the current shared document.
1449
- *
1450
- * @param {encoding.Encoder} encoder
1451
- * @param {Y.Doc} doc
1452
- */
1453
- const writeSyncStep1 = (encoder, doc) => {
1454
- writeVarUint(encoder, messageYjsSyncStep1);
1455
- const sv = Y.encodeStateVector(doc);
1456
- writeVarUint8Array(encoder, sv);
1457
- };
1458
- /**
1459
- * @param {encoding.Encoder} encoder
1460
- * @param {Y.Doc} doc
1461
- * @param {Uint8Array} [encodedStateVector]
1462
- */
1463
- const writeSyncStep2 = (encoder, doc, encodedStateVector) => {
1464
- writeVarUint(encoder, messageYjsSyncStep2);
1465
- writeVarUint8Array(encoder, Y.encodeStateAsUpdate(doc, encodedStateVector));
1466
- };
1467
- /**
1468
- * Read SyncStep1 message and reply with SyncStep2.
1469
- *
1470
- * @param {decoding.Decoder} decoder The reply to the received message
1471
- * @param {encoding.Encoder} encoder The received message
1472
- * @param {Y.Doc} doc
1473
- */
1474
- const readSyncStep1 = (decoder, encoder, doc) => writeSyncStep2(encoder, doc, readVarUint8Array(decoder));
1475
- /**
1476
- * Read and apply Structs and then DeleteStore to a y instance.
1477
- *
1478
- * @param {decoding.Decoder} decoder
1479
- * @param {Y.Doc} doc
1480
- * @param {any} transactionOrigin
1481
- * @param {(error:Error)=>any} [errorHandler]
1482
- */
1483
- const readSyncStep2 = (decoder, doc, transactionOrigin, errorHandler) => {
1484
- try {
1485
- Y.applyUpdate(doc, readVarUint8Array(decoder), transactionOrigin);
1486
- } catch (error) {
1487
- if (errorHandler != null) errorHandler(error);
1488
- console.error("Caught error while handling a Yjs update", error);
1489
- }
1490
- };
1491
- /**
1492
- * @param {encoding.Encoder} encoder
1493
- * @param {Uint8Array} update
1494
- */
1495
- const writeUpdate = (encoder, update) => {
1496
- writeVarUint(encoder, messageYjsUpdate);
1497
- writeVarUint8Array(encoder, update);
1498
- };
1499
- /**
1500
- * Read and apply Structs and then DeleteStore to a y instance.
1501
- *
1502
- * @param {decoding.Decoder} decoder
1503
- * @param {Y.Doc} doc
1504
- * @param {any} transactionOrigin
1505
- * @param {(error:Error)=>any} [errorHandler]
1506
- */
1507
- const readUpdate = readSyncStep2;
1508
- /**
1509
- * @param {decoding.Decoder} decoder A message received from another client
1510
- * @param {encoding.Encoder} encoder The reply message. Does not need to be sent if empty.
1511
- * @param {Y.Doc} doc
1512
- * @param {any} transactionOrigin
1513
- * @param {(error:Error)=>any} [errorHandler] Optional error handler that catches errors when reading Yjs messages.
1514
- */
1515
- const readSyncMessage = (decoder, encoder, doc, transactionOrigin, errorHandler) => {
1516
- const messageType = readVarUint(decoder);
1517
- switch (messageType) {
1518
- case messageYjsSyncStep1:
1519
- readSyncStep1(decoder, encoder, doc);
1520
- break;
1521
- case messageYjsSyncStep2:
1522
- readSyncStep2(decoder, doc, transactionOrigin, errorHandler);
1523
- break;
1524
- case messageYjsUpdate:
1525
- readUpdate(decoder, doc, transactionOrigin, errorHandler);
1526
- break;
1527
- default: throw new Error("Unknown message type");
1528
- }
1529
- return messageType;
1530
- };
1531
-
1532
425
  //#endregion
1533
426
  //#region packages/provider/src/MessageReceiver.ts
1534
427
  var MessageReceiver = class {
@@ -1584,7 +477,7 @@ var MessageReceiver = class {
1584
477
  applyAwarenessMessage(provider) {
1585
478
  if (!provider.awareness) return;
1586
479
  const { message } = this;
1587
- applyAwarenessUpdate(provider.awareness, message.readVarUint8Array(), provider);
480
+ awarenessProtocol.applyAwarenessUpdate(provider.awareness, message.readVarUint8Array(), provider);
1588
481
  }
1589
482
  applyAuthMessage(provider) {
1590
483
  const { message } = this;
@@ -1594,7 +487,7 @@ var MessageReceiver = class {
1594
487
  if (!provider.awareness) return;
1595
488
  const { message } = this;
1596
489
  message.writeVarUint(MessageType.Awareness);
1597
- message.writeVarUint8Array(encodeAwarenessUpdate(provider.awareness, Array.from(provider.awareness.getStates().keys())));
490
+ message.writeVarUint8Array(awarenessProtocol.encodeAwarenessUpdate(provider.awareness, Array.from(provider.awareness.getStates().keys())));
1598
491
  }
1599
492
  };
1600
493
 
@@ -1615,7 +508,7 @@ var MessageSender = class {
1615
508
 
1616
509
  //#endregion
1617
510
  //#region packages/provider/src/version.ts
1618
- const version = "4.0.0-rc.6";
511
+ const version = "4.0.0";
1619
512
 
1620
513
  //#endregion
1621
514
  //#region packages/provider/src/OutgoingMessages/AuthenticationMessage.ts
@@ -1646,12 +539,12 @@ var AwarenessMessage = class extends OutgoingMessage {
1646
539
  get(args) {
1647
540
  if (typeof args.awareness === "undefined") throw new Error("The awareness message requires awareness as an argument");
1648
541
  if (typeof args.clients === "undefined") throw new Error("The awareness message requires clients as an argument");
1649
- writeVarString(this.encoder, args.documentName);
1650
- writeVarUint(this.encoder, this.type);
542
+ encoding.writeVarString(this.encoder, args.documentName);
543
+ encoding.writeVarUint(this.encoder, this.type);
1651
544
  let awarenessUpdate;
1652
545
  if (args.states === void 0) awarenessUpdate = encodeAwarenessUpdate(args.awareness, args.clients);
1653
546
  else awarenessUpdate = encodeAwarenessUpdate(args.awareness, args.clients, args.states);
1654
- writeVarUint8Array(this.encoder, awarenessUpdate);
547
+ encoding.writeVarUint8Array(this.encoder, awarenessUpdate);
1655
548
  return this.encoder;
1656
549
  }
1657
550
  };
@@ -1682,9 +575,9 @@ var SyncStepOneMessage = class extends OutgoingMessage {
1682
575
  }
1683
576
  get(args) {
1684
577
  if (typeof args.document === "undefined") throw new Error("The sync step one message requires document as an argument");
1685
- writeVarString(this.encoder, args.documentName);
1686
- writeVarUint(this.encoder, this.type);
1687
- writeSyncStep1(this.encoder, args.document);
578
+ encoding.writeVarString(this.encoder, args.documentName);
579
+ encoding.writeVarUint(this.encoder, this.type);
580
+ syncProtocol.writeSyncStep1(this.encoder, args.document);
1688
581
  return this.encoder;
1689
582
  }
1690
583
  };