dexie-cloud-addon 4.0.8 → 4.1.0-alpha.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.
Files changed (55) hide show
  1. package/dist/modern/DexieCloudOptions.d.ts +1 -0
  2. package/dist/modern/WSObservable.d.ts +9 -6
  3. package/dist/modern/db/DexieCloudDB.d.ts +2 -0
  4. package/dist/modern/db/entities/PersistedSyncState.d.ts +7 -0
  5. package/dist/modern/dexie-cloud-addon.js +1721 -61
  6. package/dist/modern/dexie-cloud-addon.js.map +1 -1
  7. package/dist/modern/dexie-cloud-addon.min.js +1 -1
  8. package/dist/modern/dexie-cloud-addon.min.js.map +1 -1
  9. package/dist/modern/service-worker.js +1721 -61
  10. package/dist/modern/service-worker.js.map +1 -1
  11. package/dist/modern/service-worker.min.js +1 -1
  12. package/dist/modern/service-worker.min.js.map +1 -1
  13. package/dist/modern/sync/DEXIE_CLOUD_SYNCER_ID.d.ts +1 -0
  14. package/dist/modern/sync/syncWithServer.d.ts +2 -2
  15. package/dist/modern/yjs/Y.d.ts +3 -0
  16. package/dist/modern/yjs/YDexieCloudSyncState.d.ts +4 -0
  17. package/dist/modern/yjs/YTable.d.ts +2 -0
  18. package/dist/modern/yjs/applyYMessages.d.ts +5 -0
  19. package/dist/modern/yjs/awareness.d.ts +4 -0
  20. package/dist/modern/yjs/createYClientUpdateObservable.d.ts +4 -0
  21. package/dist/modern/yjs/createYHandler.d.ts +5 -0
  22. package/dist/modern/yjs/downloadYDocsFromServer.d.ts +3 -0
  23. package/dist/modern/yjs/getUpdatesTable.d.ts +3 -0
  24. package/dist/modern/yjs/listUpdatesSince.d.ts +2 -0
  25. package/dist/modern/yjs/listYClientMessagesAndStateVector.d.ts +26 -0
  26. package/dist/modern/yjs/updateYSyncStates.d.ts +6 -0
  27. package/dist/umd/DexieCloudOptions.d.ts +1 -0
  28. package/dist/umd/WSObservable.d.ts +9 -6
  29. package/dist/umd/db/DexieCloudDB.d.ts +2 -0
  30. package/dist/umd/db/entities/PersistedSyncState.d.ts +7 -0
  31. package/dist/umd/dexie-cloud-addon.js +1719 -59
  32. package/dist/umd/dexie-cloud-addon.js.map +1 -1
  33. package/dist/umd/dexie-cloud-addon.min.js +1 -1
  34. package/dist/umd/dexie-cloud-addon.min.js.map +1 -1
  35. package/dist/umd/service-worker.js +1719 -59
  36. package/dist/umd/service-worker.js.map +1 -1
  37. package/dist/umd/service-worker.min.js +1 -1
  38. package/dist/umd/service-worker.min.js.map +1 -1
  39. package/dist/umd/sync/DEXIE_CLOUD_SYNCER_ID.d.ts +1 -0
  40. package/dist/umd/sync/syncWithServer.d.ts +2 -2
  41. package/dist/umd/yjs/Y.d.ts +3 -0
  42. package/dist/umd/yjs/YDexieCloudSyncState.d.ts +4 -0
  43. package/dist/umd/yjs/YTable.d.ts +2 -0
  44. package/dist/umd/yjs/applyYMessages.d.ts +5 -0
  45. package/dist/umd/yjs/awareness.d.ts +4 -0
  46. package/dist/umd/yjs/createYClientUpdateObservable.d.ts +4 -0
  47. package/dist/umd/yjs/createYHandler.d.ts +5 -0
  48. package/dist/umd/yjs/downloadYDocsFromServer.d.ts +3 -0
  49. package/dist/umd/yjs/getUpdatesTable.d.ts +3 -0
  50. package/dist/umd/yjs/listUpdatesSince.d.ts +2 -0
  51. package/dist/umd/yjs/listYClientMessagesAndStateVector.d.ts +26 -0
  52. package/dist/umd/yjs/updateYSyncStates.d.ts +6 -0
  53. package/package.json +5 -4
  54. package/dist/modern/helpers/dbOnClosed.d.ts +0 -2
  55. package/dist/umd/helpers/dbOnClosed.d.ts +0 -2
@@ -8,7 +8,7 @@
8
8
  *
9
9
  * ==========================================================================
10
10
  *
11
- * Version 4.0.8, Tue Jun 04 2024
11
+ * Version 4.1.0-alpha.3, Fri Oct 11 2024
12
12
  *
13
13
  * https://dexie.org
14
14
  *
@@ -16,8 +16,8 @@
16
16
  *
17
17
  */
18
18
 
19
- import Dexie, { PropModification, cmp, liveQuery } from 'dexie';
20
- import { Observable as Observable$1, BehaviorSubject, firstValueFrom, Subject, from as from$1, filter as filter$1, fromEvent, of, merge, Subscription as Subscription$1, throwError, combineLatest, map as map$1, share, timer as timer$1 } from 'rxjs';
19
+ import Dexie, { PropModification, cmp, DexieYProvider, liveQuery } from 'dexie';
20
+ import { Observable as Observable$1, BehaviorSubject, firstValueFrom, Subject, from as from$1, filter as filter$1, fromEvent, of, merge, mergeMap as mergeMap$1, Subscription as Subscription$1, throwError, combineLatest, map as map$1, share, timer as timer$1 } from 'rxjs';
21
21
 
22
22
  /******************************************************************************
23
23
  Copyright (c) Microsoft Corporation.
@@ -2242,6 +2242,1075 @@ function getDbNameFromDbUrl(dbUrl) {
2242
2242
  : url.pathname.split('/')[1];
2243
2243
  }
2244
2244
 
2245
+ /**
2246
+ * Common Math expressions.
2247
+ *
2248
+ * @module math
2249
+ */
2250
+
2251
+ const floor = Math.floor;
2252
+ const abs = Math.abs;
2253
+
2254
+ /**
2255
+ * @function
2256
+ * @param {number} a
2257
+ * @param {number} b
2258
+ * @return {number} The smaller element of a and b
2259
+ */
2260
+ const min = (a, b) => a < b ? a : b;
2261
+
2262
+ /**
2263
+ * @function
2264
+ * @param {number} a
2265
+ * @param {number} b
2266
+ * @return {number} The bigger element of a and b
2267
+ */
2268
+ const max = (a, b) => a > b ? a : b;
2269
+
2270
+ /**
2271
+ * @param {number} n
2272
+ * @return {boolean} Wether n is negative. This function also differentiates between -0 and +0
2273
+ */
2274
+ const isNegativeZero = n => n !== 0 ? n < 0 : 1 / n < 0;
2275
+
2276
+ /* eslint-env browser */
2277
+
2278
+ const BIT7 = 64;
2279
+ const BIT8 = 128;
2280
+ const BITS6 = 63;
2281
+ const BITS7 = 127;
2282
+ /**
2283
+ * @type {number}
2284
+ */
2285
+ const BITS31 = 0x7FFFFFFF;
2286
+
2287
+ /**
2288
+ * Utility helpers for working with numbers.
2289
+ *
2290
+ * @module number
2291
+ */
2292
+
2293
+
2294
+ const MAX_SAFE_INTEGER = Number.MAX_SAFE_INTEGER;
2295
+
2296
+ /* c8 ignore next */
2297
+ const isInteger = Number.isInteger || (num => typeof num === 'number' && isFinite(num) && floor(num) === num);
2298
+
2299
+ /**
2300
+ * Utility module to work with Arrays.
2301
+ *
2302
+ * @module array
2303
+ */
2304
+
2305
+
2306
+ const isArray = Array.isArray;
2307
+
2308
+ /**
2309
+ * @param {string} str
2310
+ * @return {Uint8Array}
2311
+ */
2312
+ const _encodeUtf8Polyfill = str => {
2313
+ const encodedString = unescape(encodeURIComponent(str));
2314
+ const len = encodedString.length;
2315
+ const buf = new Uint8Array(len);
2316
+ for (let i = 0; i < len; i++) {
2317
+ buf[i] = /** @type {number} */ (encodedString.codePointAt(i));
2318
+ }
2319
+ return buf
2320
+ };
2321
+
2322
+ /* c8 ignore next */
2323
+ const utf8TextEncoder = /** @type {TextEncoder} */ (typeof TextEncoder !== 'undefined' ? new TextEncoder() : null);
2324
+
2325
+ /**
2326
+ * @param {string} str
2327
+ * @return {Uint8Array}
2328
+ */
2329
+ const _encodeUtf8Native = str => utf8TextEncoder.encode(str);
2330
+
2331
+ /**
2332
+ * @param {string} str
2333
+ * @return {Uint8Array}
2334
+ */
2335
+ /* c8 ignore next */
2336
+ const encodeUtf8 = utf8TextEncoder ? _encodeUtf8Native : _encodeUtf8Polyfill;
2337
+
2338
+ /* c8 ignore next */
2339
+ let utf8TextDecoder = typeof TextDecoder === 'undefined' ? null : new TextDecoder('utf-8', { fatal: true, ignoreBOM: true });
2340
+
2341
+ /* c8 ignore start */
2342
+ if (utf8TextDecoder && utf8TextDecoder.decode(new Uint8Array()).length === 1) {
2343
+ // Safari doesn't handle BOM correctly.
2344
+ // This fixes a bug in Safari 13.0.5 where it produces a BOM the first time it is called.
2345
+ // utf8TextDecoder.decode(new Uint8Array()).length === 1 on the first call and
2346
+ // utf8TextDecoder.decode(new Uint8Array()).length === 1 on the second call
2347
+ // Another issue is that from then on no BOM chars are recognized anymore
2348
+ /* c8 ignore next */
2349
+ utf8TextDecoder = null;
2350
+ }
2351
+
2352
+ /**
2353
+ * Efficient schema-less binary encoding with support for variable length encoding.
2354
+ *
2355
+ * Use [lib0/encoding] with [lib0/decoding]. Every encoding function has a corresponding decoding function.
2356
+ *
2357
+ * Encodes numbers in little-endian order (least to most significant byte order)
2358
+ * and is compatible with Golang's binary encoding (https://golang.org/pkg/encoding/binary/)
2359
+ * which is also used in Protocol Buffers.
2360
+ *
2361
+ * ```js
2362
+ * // encoding step
2363
+ * const encoder = encoding.createEncoder()
2364
+ * encoding.writeVarUint(encoder, 256)
2365
+ * encoding.writeVarString(encoder, 'Hello world!')
2366
+ * const buf = encoding.toUint8Array(encoder)
2367
+ * ```
2368
+ *
2369
+ * ```js
2370
+ * // decoding step
2371
+ * const decoder = decoding.createDecoder(buf)
2372
+ * decoding.readVarUint(decoder) // => 256
2373
+ * decoding.readVarString(decoder) // => 'Hello world!'
2374
+ * decoding.hasContent(decoder) // => false - all data is read
2375
+ * ```
2376
+ *
2377
+ * @module encoding
2378
+ */
2379
+
2380
+
2381
+ /**
2382
+ * A BinaryEncoder handles the encoding to an Uint8Array.
2383
+ */
2384
+ class Encoder {
2385
+ constructor () {
2386
+ this.cpos = 0;
2387
+ this.cbuf = new Uint8Array(100);
2388
+ /**
2389
+ * @type {Array<Uint8Array>}
2390
+ */
2391
+ this.bufs = [];
2392
+ }
2393
+ }
2394
+
2395
+ /**
2396
+ * The current length of the encoded data.
2397
+ *
2398
+ * @function
2399
+ * @param {Encoder} encoder
2400
+ * @return {number}
2401
+ */
2402
+ const length = encoder => {
2403
+ let len = encoder.cpos;
2404
+ for (let i = 0; i < encoder.bufs.length; i++) {
2405
+ len += encoder.bufs[i].length;
2406
+ }
2407
+ return len
2408
+ };
2409
+
2410
+ /**
2411
+ * Transform to Uint8Array.
2412
+ *
2413
+ * @function
2414
+ * @param {Encoder} encoder
2415
+ * @return {Uint8Array} The created ArrayBuffer.
2416
+ */
2417
+ const toUint8Array = encoder => {
2418
+ const uint8arr = new Uint8Array(length(encoder));
2419
+ let curPos = 0;
2420
+ for (let i = 0; i < encoder.bufs.length; i++) {
2421
+ const d = encoder.bufs[i];
2422
+ uint8arr.set(d, curPos);
2423
+ curPos += d.length;
2424
+ }
2425
+ uint8arr.set(new Uint8Array(encoder.cbuf.buffer, 0, encoder.cpos), curPos);
2426
+ return uint8arr
2427
+ };
2428
+
2429
+ /**
2430
+ * Verify that it is possible to write `len` bytes wtihout checking. If
2431
+ * necessary, a new Buffer with the required length is attached.
2432
+ *
2433
+ * @param {Encoder} encoder
2434
+ * @param {number} len
2435
+ */
2436
+ const verifyLen = (encoder, len) => {
2437
+ const bufferLen = encoder.cbuf.length;
2438
+ if (bufferLen - encoder.cpos < len) {
2439
+ encoder.bufs.push(new Uint8Array(encoder.cbuf.buffer, 0, encoder.cpos));
2440
+ encoder.cbuf = new Uint8Array(max(bufferLen, len) * 2);
2441
+ encoder.cpos = 0;
2442
+ }
2443
+ };
2444
+
2445
+ /**
2446
+ * Write one byte to the encoder.
2447
+ *
2448
+ * @function
2449
+ * @param {Encoder} encoder
2450
+ * @param {number} num The byte that is to be encoded.
2451
+ */
2452
+ const write = (encoder, num) => {
2453
+ const bufferLen = encoder.cbuf.length;
2454
+ if (encoder.cpos === bufferLen) {
2455
+ encoder.bufs.push(encoder.cbuf);
2456
+ encoder.cbuf = new Uint8Array(bufferLen * 2);
2457
+ encoder.cpos = 0;
2458
+ }
2459
+ encoder.cbuf[encoder.cpos++] = num;
2460
+ };
2461
+
2462
+ /**
2463
+ * Write a variable length unsigned integer. Max encodable integer is 2^53.
2464
+ *
2465
+ * @function
2466
+ * @param {Encoder} encoder
2467
+ * @param {number} num The number that is to be encoded.
2468
+ */
2469
+ const writeVarUint = (encoder, num) => {
2470
+ while (num > BITS7) {
2471
+ write(encoder, BIT8 | (BITS7 & num));
2472
+ num = floor(num / 128); // shift >>> 7
2473
+ }
2474
+ write(encoder, BITS7 & num);
2475
+ };
2476
+
2477
+ /**
2478
+ * Write a variable length integer.
2479
+ *
2480
+ * We use the 7th bit instead for signaling that this is a negative number.
2481
+ *
2482
+ * @function
2483
+ * @param {Encoder} encoder
2484
+ * @param {number} num The number that is to be encoded.
2485
+ */
2486
+ const writeVarInt = (encoder, num) => {
2487
+ const isNegative = isNegativeZero(num);
2488
+ if (isNegative) {
2489
+ num = -num;
2490
+ }
2491
+ // |- whether to continue reading |- whether is negative |- number
2492
+ write(encoder, (num > BITS6 ? BIT8 : 0) | (isNegative ? BIT7 : 0) | (BITS6 & num));
2493
+ num = floor(num / 64); // shift >>> 6
2494
+ // We don't need to consider the case of num === 0 so we can use a different
2495
+ // pattern here than above.
2496
+ while (num > 0) {
2497
+ write(encoder, (num > BITS7 ? BIT8 : 0) | (BITS7 & num));
2498
+ num = floor(num / 128); // shift >>> 7
2499
+ }
2500
+ };
2501
+
2502
+ /**
2503
+ * A cache to store strings temporarily
2504
+ */
2505
+ const _strBuffer = new Uint8Array(30000);
2506
+ const _maxStrBSize = _strBuffer.length / 3;
2507
+
2508
+ /**
2509
+ * Write a variable length string.
2510
+ *
2511
+ * @function
2512
+ * @param {Encoder} encoder
2513
+ * @param {String} str The string that is to be encoded.
2514
+ */
2515
+ const _writeVarStringNative = (encoder, str) => {
2516
+ if (str.length < _maxStrBSize) {
2517
+ // We can encode the string into the existing buffer
2518
+ /* c8 ignore next */
2519
+ const written = utf8TextEncoder.encodeInto(str, _strBuffer).written || 0;
2520
+ writeVarUint(encoder, written);
2521
+ for (let i = 0; i < written; i++) {
2522
+ write(encoder, _strBuffer[i]);
2523
+ }
2524
+ } else {
2525
+ writeVarUint8Array(encoder, encodeUtf8(str));
2526
+ }
2527
+ };
2528
+
2529
+ /**
2530
+ * Write a variable length string.
2531
+ *
2532
+ * @function
2533
+ * @param {Encoder} encoder
2534
+ * @param {String} str The string that is to be encoded.
2535
+ */
2536
+ const _writeVarStringPolyfill = (encoder, str) => {
2537
+ const encodedString = unescape(encodeURIComponent(str));
2538
+ const len = encodedString.length;
2539
+ writeVarUint(encoder, len);
2540
+ for (let i = 0; i < len; i++) {
2541
+ write(encoder, /** @type {number} */ (encodedString.codePointAt(i)));
2542
+ }
2543
+ };
2544
+
2545
+ /**
2546
+ * Write a variable length string.
2547
+ *
2548
+ * @function
2549
+ * @param {Encoder} encoder
2550
+ * @param {String} str The string that is to be encoded.
2551
+ */
2552
+ /* c8 ignore next */
2553
+ const writeVarString = (utf8TextEncoder && /** @type {any} */ (utf8TextEncoder).encodeInto) ? _writeVarStringNative : _writeVarStringPolyfill;
2554
+
2555
+ /**
2556
+ * Append fixed-length Uint8Array to the encoder.
2557
+ *
2558
+ * @function
2559
+ * @param {Encoder} encoder
2560
+ * @param {Uint8Array} uint8Array
2561
+ */
2562
+ const writeUint8Array = (encoder, uint8Array) => {
2563
+ const bufferLen = encoder.cbuf.length;
2564
+ const cpos = encoder.cpos;
2565
+ const leftCopyLen = min(bufferLen - cpos, uint8Array.length);
2566
+ const rightCopyLen = uint8Array.length - leftCopyLen;
2567
+ encoder.cbuf.set(uint8Array.subarray(0, leftCopyLen), cpos);
2568
+ encoder.cpos += leftCopyLen;
2569
+ if (rightCopyLen > 0) {
2570
+ // Still something to write, write right half..
2571
+ // Append new buffer
2572
+ encoder.bufs.push(encoder.cbuf);
2573
+ // must have at least size of remaining buffer
2574
+ encoder.cbuf = new Uint8Array(max(bufferLen * 2, rightCopyLen));
2575
+ // copy array
2576
+ encoder.cbuf.set(uint8Array.subarray(leftCopyLen));
2577
+ encoder.cpos = rightCopyLen;
2578
+ }
2579
+ };
2580
+
2581
+ /**
2582
+ * Append an Uint8Array to Encoder.
2583
+ *
2584
+ * @function
2585
+ * @param {Encoder} encoder
2586
+ * @param {Uint8Array} uint8Array
2587
+ */
2588
+ const writeVarUint8Array = (encoder, uint8Array) => {
2589
+ writeVarUint(encoder, uint8Array.byteLength);
2590
+ writeUint8Array(encoder, uint8Array);
2591
+ };
2592
+
2593
+ /**
2594
+ * Create an DataView of the next `len` bytes. Use it to write data after
2595
+ * calling this function.
2596
+ *
2597
+ * ```js
2598
+ * // write float32 using DataView
2599
+ * const dv = writeOnDataView(encoder, 4)
2600
+ * dv.setFloat32(0, 1.1)
2601
+ * // read float32 using DataView
2602
+ * const dv = readFromDataView(encoder, 4)
2603
+ * dv.getFloat32(0) // => 1.100000023841858 (leaving it to the reader to find out why this is the correct result)
2604
+ * ```
2605
+ *
2606
+ * @param {Encoder} encoder
2607
+ * @param {number} len
2608
+ * @return {DataView}
2609
+ */
2610
+ const writeOnDataView = (encoder, len) => {
2611
+ verifyLen(encoder, len);
2612
+ const dview = new DataView(encoder.cbuf.buffer, encoder.cpos, len);
2613
+ encoder.cpos += len;
2614
+ return dview
2615
+ };
2616
+
2617
+ /**
2618
+ * @param {Encoder} encoder
2619
+ * @param {number} num
2620
+ */
2621
+ const writeFloat32 = (encoder, num) => writeOnDataView(encoder, 4).setFloat32(0, num, false);
2622
+
2623
+ /**
2624
+ * @param {Encoder} encoder
2625
+ * @param {number} num
2626
+ */
2627
+ const writeFloat64 = (encoder, num) => writeOnDataView(encoder, 8).setFloat64(0, num, false);
2628
+
2629
+ /**
2630
+ * @param {Encoder} encoder
2631
+ * @param {bigint} num
2632
+ */
2633
+ const writeBigInt64 = (encoder, num) => /** @type {any} */ (writeOnDataView(encoder, 8)).setBigInt64(0, num, false);
2634
+
2635
+ /**
2636
+ * @param {Encoder} encoder
2637
+ * @param {bigint} num
2638
+ */
2639
+ const writeBigUint64 = (encoder, num) => /** @type {any} */ (writeOnDataView(encoder, 8)).setBigUint64(0, num, false);
2640
+
2641
+ const floatTestBed = new DataView(new ArrayBuffer(4));
2642
+ /**
2643
+ * Check if a number can be encoded as a 32 bit float.
2644
+ *
2645
+ * @param {number} num
2646
+ * @return {boolean}
2647
+ */
2648
+ const isFloat32 = num => {
2649
+ floatTestBed.setFloat32(0, num);
2650
+ return floatTestBed.getFloat32(0) === num
2651
+ };
2652
+
2653
+ /**
2654
+ * Encode data with efficient binary format.
2655
+ *
2656
+ * Differences to JSON:
2657
+ * • Transforms data to a binary format (not to a string)
2658
+ * • Encodes undefined, NaN, and ArrayBuffer (these can't be represented in JSON)
2659
+ * • Numbers are efficiently encoded either as a variable length integer, as a
2660
+ * 32 bit float, as a 64 bit float, or as a 64 bit bigint.
2661
+ *
2662
+ * Encoding table:
2663
+ *
2664
+ * | Data Type | Prefix | Encoding Method | Comment |
2665
+ * | ------------------- | -------- | ------------------ | ------- |
2666
+ * | undefined | 127 | | Functions, symbol, and everything that cannot be identified is encoded as undefined |
2667
+ * | null | 126 | | |
2668
+ * | integer | 125 | writeVarInt | Only encodes 32 bit signed integers |
2669
+ * | float32 | 124 | writeFloat32 | |
2670
+ * | float64 | 123 | writeFloat64 | |
2671
+ * | bigint | 122 | writeBigInt64 | |
2672
+ * | boolean (false) | 121 | | True and false are different data types so we save the following byte |
2673
+ * | boolean (true) | 120 | | - 0b01111000 so the last bit determines whether true or false |
2674
+ * | string | 119 | writeVarString | |
2675
+ * | object<string,any> | 118 | custom | Writes {length} then {length} key-value pairs |
2676
+ * | array<any> | 117 | custom | Writes {length} then {length} json values |
2677
+ * | Uint8Array | 116 | writeVarUint8Array | We use Uint8Array for any kind of binary data |
2678
+ *
2679
+ * Reasons for the decreasing prefix:
2680
+ * We need the first bit for extendability (later we may want to encode the
2681
+ * prefix with writeVarUint). The remaining 7 bits are divided as follows:
2682
+ * [0-30] the beginning of the data range is used for custom purposes
2683
+ * (defined by the function that uses this library)
2684
+ * [31-127] the end of the data range is used for data encoding by
2685
+ * lib0/encoding.js
2686
+ *
2687
+ * @param {Encoder} encoder
2688
+ * @param {undefined|null|number|bigint|boolean|string|Object<string,any>|Array<any>|Uint8Array} data
2689
+ */
2690
+ const writeAny = (encoder, data) => {
2691
+ switch (typeof data) {
2692
+ case 'string':
2693
+ // TYPE 119: STRING
2694
+ write(encoder, 119);
2695
+ writeVarString(encoder, data);
2696
+ break
2697
+ case 'number':
2698
+ if (isInteger(data) && abs(data) <= BITS31) {
2699
+ // TYPE 125: INTEGER
2700
+ write(encoder, 125);
2701
+ writeVarInt(encoder, data);
2702
+ } else if (isFloat32(data)) {
2703
+ // TYPE 124: FLOAT32
2704
+ write(encoder, 124);
2705
+ writeFloat32(encoder, data);
2706
+ } else {
2707
+ // TYPE 123: FLOAT64
2708
+ write(encoder, 123);
2709
+ writeFloat64(encoder, data);
2710
+ }
2711
+ break
2712
+ case 'bigint':
2713
+ // TYPE 122: BigInt
2714
+ write(encoder, 122);
2715
+ writeBigInt64(encoder, data);
2716
+ break
2717
+ case 'object':
2718
+ if (data === null) {
2719
+ // TYPE 126: null
2720
+ write(encoder, 126);
2721
+ } else if (isArray(data)) {
2722
+ // TYPE 117: Array
2723
+ write(encoder, 117);
2724
+ writeVarUint(encoder, data.length);
2725
+ for (let i = 0; i < data.length; i++) {
2726
+ writeAny(encoder, data[i]);
2727
+ }
2728
+ } else if (data instanceof Uint8Array) {
2729
+ // TYPE 116: ArrayBuffer
2730
+ write(encoder, 116);
2731
+ writeVarUint8Array(encoder, data);
2732
+ } else {
2733
+ // TYPE 118: Object
2734
+ write(encoder, 118);
2735
+ const keys = Object.keys(data);
2736
+ writeVarUint(encoder, keys.length);
2737
+ for (let i = 0; i < keys.length; i++) {
2738
+ const key = keys[i];
2739
+ writeVarString(encoder, key);
2740
+ writeAny(encoder, data[key]);
2741
+ }
2742
+ }
2743
+ break
2744
+ case 'boolean':
2745
+ // TYPE 120/121: boolean (true/false)
2746
+ write(encoder, data ? 120 : 121);
2747
+ break
2748
+ default:
2749
+ // TYPE 127: undefined
2750
+ write(encoder, 127);
2751
+ }
2752
+ };
2753
+
2754
+ function encodeYMessage(msg) {
2755
+ const encoder = new Encoder();
2756
+ writeVarString(encoder, msg.type);
2757
+ writeVarString(encoder, msg.table);
2758
+ writeVarString(encoder, msg.prop);
2759
+ switch (msg.type) {
2760
+ case 'u-ack':
2761
+ case 'u-reject':
2762
+ writeBigUint64(encoder, BigInt(msg.i));
2763
+ break;
2764
+ default:
2765
+ writeAny(encoder, msg.k);
2766
+ switch (msg.type) {
2767
+ case 'aware':
2768
+ writeVarUint8Array(encoder, msg.u);
2769
+ break;
2770
+ case 'doc-open':
2771
+ writeAny(encoder, msg.serverRev);
2772
+ writeAny(encoder, msg.sv);
2773
+ break;
2774
+ case 'doc-close':
2775
+ break;
2776
+ case 'sv':
2777
+ writeVarUint8Array(encoder, msg.sv);
2778
+ break;
2779
+ case 'u-c':
2780
+ writeVarUint8Array(encoder, msg.u);
2781
+ writeBigUint64(encoder, BigInt(msg.i));
2782
+ break;
2783
+ case 'u-s':
2784
+ writeVarUint8Array(encoder, msg.u);
2785
+ break;
2786
+ }
2787
+ }
2788
+ return toUint8Array(encoder);
2789
+ }
2790
+
2791
+ /**
2792
+ * Error helpers.
2793
+ *
2794
+ * @module error
2795
+ */
2796
+
2797
+ /**
2798
+ * @param {string} s
2799
+ * @return {Error}
2800
+ */
2801
+ /* c8 ignore next */
2802
+ const create = s => new Error(s);
2803
+
2804
+ /**
2805
+ * Efficient schema-less binary decoding with support for variable length encoding.
2806
+ *
2807
+ * Use [lib0/decoding] with [lib0/encoding]. Every encoding function has a corresponding decoding function.
2808
+ *
2809
+ * Encodes numbers in little-endian order (least to most significant byte order)
2810
+ * and is compatible with Golang's binary encoding (https://golang.org/pkg/encoding/binary/)
2811
+ * which is also used in Protocol Buffers.
2812
+ *
2813
+ * ```js
2814
+ * // encoding step
2815
+ * const encoder = encoding.createEncoder()
2816
+ * encoding.writeVarUint(encoder, 256)
2817
+ * encoding.writeVarString(encoder, 'Hello world!')
2818
+ * const buf = encoding.toUint8Array(encoder)
2819
+ * ```
2820
+ *
2821
+ * ```js
2822
+ * // decoding step
2823
+ * const decoder = decoding.createDecoder(buf)
2824
+ * decoding.readVarUint(decoder) // => 256
2825
+ * decoding.readVarString(decoder) // => 'Hello world!'
2826
+ * decoding.hasContent(decoder) // => false - all data is read
2827
+ * ```
2828
+ *
2829
+ * @module decoding
2830
+ */
2831
+
2832
+
2833
+ const errorUnexpectedEndOfArray = create('Unexpected end of array');
2834
+ const errorIntegerOutOfRange = create('Integer out of Range');
2835
+
2836
+ /**
2837
+ * A Decoder handles the decoding of an Uint8Array.
2838
+ */
2839
+ class Decoder {
2840
+ /**
2841
+ * @param {Uint8Array} uint8Array Binary data to decode
2842
+ */
2843
+ constructor (uint8Array) {
2844
+ /**
2845
+ * Decoding target.
2846
+ *
2847
+ * @type {Uint8Array}
2848
+ */
2849
+ this.arr = uint8Array;
2850
+ /**
2851
+ * Current decoding position.
2852
+ *
2853
+ * @type {number}
2854
+ */
2855
+ this.pos = 0;
2856
+ }
2857
+ }
2858
+
2859
+ /**
2860
+ * @function
2861
+ * @param {Decoder} decoder
2862
+ * @return {boolean}
2863
+ */
2864
+ const hasContent = decoder => decoder.pos !== decoder.arr.length;
2865
+
2866
+ /**
2867
+ * Create an Uint8Array view of the next `len` bytes and advance the position by `len`.
2868
+ *
2869
+ * Important: The Uint8Array still points to the underlying ArrayBuffer. Make sure to discard the result as soon as possible to prevent any memory leaks.
2870
+ * Use `buffer.copyUint8Array` to copy the result into a new Uint8Array.
2871
+ *
2872
+ * @function
2873
+ * @param {Decoder} decoder The decoder instance
2874
+ * @param {number} len The length of bytes to read
2875
+ * @return {Uint8Array}
2876
+ */
2877
+ const readUint8Array = (decoder, len) => {
2878
+ const view = new Uint8Array(decoder.arr.buffer, decoder.pos + decoder.arr.byteOffset, len);
2879
+ decoder.pos += len;
2880
+ return view
2881
+ };
2882
+
2883
+ /**
2884
+ * Read variable length Uint8Array.
2885
+ *
2886
+ * Important: The Uint8Array still points to the underlying ArrayBuffer. Make sure to discard the result as soon as possible to prevent any memory leaks.
2887
+ * Use `buffer.copyUint8Array` to copy the result into a new Uint8Array.
2888
+ *
2889
+ * @function
2890
+ * @param {Decoder} decoder
2891
+ * @return {Uint8Array}
2892
+ */
2893
+ const readVarUint8Array = decoder => readUint8Array(decoder, readVarUint(decoder));
2894
+
2895
+ /**
2896
+ * Read one byte as unsigned integer.
2897
+ * @function
2898
+ * @param {Decoder} decoder The decoder instance
2899
+ * @return {number} Unsigned 8-bit integer
2900
+ */
2901
+ const readUint8 = decoder => decoder.arr[decoder.pos++];
2902
+
2903
+ /**
2904
+ * Read unsigned integer (32bit) with variable length.
2905
+ * 1/8th of the storage is used as encoding overhead.
2906
+ * * numbers < 2^7 is stored in one bytlength
2907
+ * * numbers < 2^14 is stored in two bylength
2908
+ *
2909
+ * @function
2910
+ * @param {Decoder} decoder
2911
+ * @return {number} An unsigned integer.length
2912
+ */
2913
+ const readVarUint = decoder => {
2914
+ let num = 0;
2915
+ let mult = 1;
2916
+ const len = decoder.arr.length;
2917
+ while (decoder.pos < len) {
2918
+ const r = decoder.arr[decoder.pos++];
2919
+ // num = num | ((r & binary.BITS7) << len)
2920
+ num = num + (r & BITS7) * mult; // shift $r << (7*#iterations) and add it to num
2921
+ mult *= 128; // next iteration, shift 7 "more" to the left
2922
+ if (r < BIT8) {
2923
+ return num
2924
+ }
2925
+ /* c8 ignore start */
2926
+ if (num > MAX_SAFE_INTEGER) {
2927
+ throw errorIntegerOutOfRange
2928
+ }
2929
+ /* c8 ignore stop */
2930
+ }
2931
+ throw errorUnexpectedEndOfArray
2932
+ };
2933
+
2934
+ /**
2935
+ * Read signed integer (32bit) with variable length.
2936
+ * 1/8th of the storage is used as encoding overhead.
2937
+ * * numbers < 2^7 is stored in one bytlength
2938
+ * * numbers < 2^14 is stored in two bylength
2939
+ * @todo This should probably create the inverse ~num if number is negative - but this would be a breaking change.
2940
+ *
2941
+ * @function
2942
+ * @param {Decoder} decoder
2943
+ * @return {number} An unsigned integer.length
2944
+ */
2945
+ const readVarInt = decoder => {
2946
+ let r = decoder.arr[decoder.pos++];
2947
+ let num = r & BITS6;
2948
+ let mult = 64;
2949
+ const sign = (r & BIT7) > 0 ? -1 : 1;
2950
+ if ((r & BIT8) === 0) {
2951
+ // don't continue reading
2952
+ return sign * num
2953
+ }
2954
+ const len = decoder.arr.length;
2955
+ while (decoder.pos < len) {
2956
+ r = decoder.arr[decoder.pos++];
2957
+ // num = num | ((r & binary.BITS7) << len)
2958
+ num = num + (r & BITS7) * mult;
2959
+ mult *= 128;
2960
+ if (r < BIT8) {
2961
+ return sign * num
2962
+ }
2963
+ /* c8 ignore start */
2964
+ if (num > MAX_SAFE_INTEGER) {
2965
+ throw errorIntegerOutOfRange
2966
+ }
2967
+ /* c8 ignore stop */
2968
+ }
2969
+ throw errorUnexpectedEndOfArray
2970
+ };
2971
+
2972
+ /**
2973
+ * We don't test this function anymore as we use native decoding/encoding by default now.
2974
+ * Better not modify this anymore..
2975
+ *
2976
+ * Transforming utf8 to a string is pretty expensive. The code performs 10x better
2977
+ * when String.fromCodePoint is fed with all characters as arguments.
2978
+ * But most environments have a maximum number of arguments per functions.
2979
+ * For effiency reasons we apply a maximum of 10000 characters at once.
2980
+ *
2981
+ * @function
2982
+ * @param {Decoder} decoder
2983
+ * @return {String} The read String.
2984
+ */
2985
+ /* c8 ignore start */
2986
+ const _readVarStringPolyfill = decoder => {
2987
+ let remainingLen = readVarUint(decoder);
2988
+ if (remainingLen === 0) {
2989
+ return ''
2990
+ } else {
2991
+ let encodedString = String.fromCodePoint(readUint8(decoder)); // remember to decrease remainingLen
2992
+ if (--remainingLen < 100) { // do not create a Uint8Array for small strings
2993
+ while (remainingLen--) {
2994
+ encodedString += String.fromCodePoint(readUint8(decoder));
2995
+ }
2996
+ } else {
2997
+ while (remainingLen > 0) {
2998
+ const nextLen = remainingLen < 10000 ? remainingLen : 10000;
2999
+ // this is dangerous, we create a fresh array view from the existing buffer
3000
+ const bytes = decoder.arr.subarray(decoder.pos, decoder.pos + nextLen);
3001
+ decoder.pos += nextLen;
3002
+ // Starting with ES5.1 we can supply a generic array-like object as arguments
3003
+ encodedString += String.fromCodePoint.apply(null, /** @type {any} */ (bytes));
3004
+ remainingLen -= nextLen;
3005
+ }
3006
+ }
3007
+ return decodeURIComponent(escape(encodedString))
3008
+ }
3009
+ };
3010
+ /* c8 ignore stop */
3011
+
3012
+ /**
3013
+ * @function
3014
+ * @param {Decoder} decoder
3015
+ * @return {String} The read String
3016
+ */
3017
+ const _readVarStringNative = decoder =>
3018
+ /** @type any */ (utf8TextDecoder).decode(readVarUint8Array(decoder));
3019
+
3020
+ /**
3021
+ * Read string of variable length
3022
+ * * varUint is used to store the length of the string
3023
+ *
3024
+ * @function
3025
+ * @param {Decoder} decoder
3026
+ * @return {String} The read String
3027
+ *
3028
+ */
3029
+ /* c8 ignore next */
3030
+ const readVarString = utf8TextDecoder ? _readVarStringNative : _readVarStringPolyfill;
3031
+
3032
+ /**
3033
+ * @param {Decoder} decoder
3034
+ * @param {number} len
3035
+ * @return {DataView}
3036
+ */
3037
+ const readFromDataView = (decoder, len) => {
3038
+ const dv = new DataView(decoder.arr.buffer, decoder.arr.byteOffset + decoder.pos, len);
3039
+ decoder.pos += len;
3040
+ return dv
3041
+ };
3042
+
3043
+ /**
3044
+ * @param {Decoder} decoder
3045
+ */
3046
+ const readFloat32 = decoder => readFromDataView(decoder, 4).getFloat32(0, false);
3047
+
3048
+ /**
3049
+ * @param {Decoder} decoder
3050
+ */
3051
+ const readFloat64 = decoder => readFromDataView(decoder, 8).getFloat64(0, false);
3052
+
3053
+ /**
3054
+ * @param {Decoder} decoder
3055
+ */
3056
+ const readBigInt64 = decoder => /** @type {any} */ (readFromDataView(decoder, 8)).getBigInt64(0, false);
3057
+
3058
+ /**
3059
+ * @param {Decoder} decoder
3060
+ */
3061
+ const readBigUint64 = decoder => /** @type {any} */ (readFromDataView(decoder, 8)).getBigUint64(0, false);
3062
+
3063
+ /**
3064
+ * @type {Array<function(Decoder):any>}
3065
+ */
3066
+ const readAnyLookupTable = [
3067
+ decoder => undefined, // CASE 127: undefined
3068
+ decoder => null, // CASE 126: null
3069
+ readVarInt, // CASE 125: integer
3070
+ readFloat32, // CASE 124: float32
3071
+ readFloat64, // CASE 123: float64
3072
+ readBigInt64, // CASE 122: bigint
3073
+ decoder => false, // CASE 121: boolean (false)
3074
+ decoder => true, // CASE 120: boolean (true)
3075
+ readVarString, // CASE 119: string
3076
+ decoder => { // CASE 118: object<string,any>
3077
+ const len = readVarUint(decoder);
3078
+ /**
3079
+ * @type {Object<string,any>}
3080
+ */
3081
+ const obj = {};
3082
+ for (let i = 0; i < len; i++) {
3083
+ const key = readVarString(decoder);
3084
+ obj[key] = readAny(decoder);
3085
+ }
3086
+ return obj
3087
+ },
3088
+ decoder => { // CASE 117: array<any>
3089
+ const len = readVarUint(decoder);
3090
+ const arr = [];
3091
+ for (let i = 0; i < len; i++) {
3092
+ arr.push(readAny(decoder));
3093
+ }
3094
+ return arr
3095
+ },
3096
+ readVarUint8Array // CASE 116: Uint8Array
3097
+ ];
3098
+
3099
+ /**
3100
+ * @param {Decoder} decoder
3101
+ */
3102
+ const readAny = decoder => readAnyLookupTable[127 - readUint8(decoder)](decoder);
3103
+
3104
+ function decodeYMessage(a) {
3105
+ const decoder = new Decoder(a);
3106
+ const type = readVarString(decoder);
3107
+ const table = readVarString(decoder);
3108
+ const prop = readVarString(decoder);
3109
+ switch (type) {
3110
+ case 'u-ack':
3111
+ case 'u-reject':
3112
+ return {
3113
+ type,
3114
+ table,
3115
+ prop,
3116
+ i: Number(readBigUint64(decoder)),
3117
+ };
3118
+ default: {
3119
+ const k = readAny(decoder);
3120
+ switch (type) {
3121
+ case 'in-sync':
3122
+ return { type, table, prop, k };
3123
+ case 'aware':
3124
+ return {
3125
+ type,
3126
+ table,
3127
+ prop,
3128
+ k,
3129
+ u: readVarUint8Array(decoder),
3130
+ };
3131
+ case 'doc-open':
3132
+ return {
3133
+ type,
3134
+ table,
3135
+ prop,
3136
+ k,
3137
+ serverRev: readAny(decoder),
3138
+ sv: readAny(decoder),
3139
+ };
3140
+ case 'doc-close':
3141
+ return { type, table, prop, k };
3142
+ case 'sv':
3143
+ return {
3144
+ type,
3145
+ table,
3146
+ prop,
3147
+ k,
3148
+ sv: readVarUint8Array(decoder),
3149
+ };
3150
+ case 'u-c':
3151
+ return {
3152
+ type,
3153
+ table,
3154
+ prop,
3155
+ k,
3156
+ u: readVarUint8Array(decoder),
3157
+ i: Number(readBigUint64(decoder)),
3158
+ };
3159
+ case 'u-s':
3160
+ return {
3161
+ type,
3162
+ table,
3163
+ prop,
3164
+ k,
3165
+ u: readVarUint8Array(decoder)
3166
+ };
3167
+ default:
3168
+ throw new TypeError(`Unknown message type: ${type}`);
3169
+ }
3170
+ }
3171
+ }
3172
+ }
3173
+
3174
+ async function asyncIterablePipeline(source, ...stages) {
3175
+ var _a, e_1, _b, _c;
3176
+ // Chain generators by sending outdata from one to another
3177
+ let result = source(); // Start with the source generator
3178
+ for (let i = 0; i < stages.length; i++) {
3179
+ result = stages[i](result); // Pass on the result to next generator
3180
+ }
3181
+ try {
3182
+ // Start running the machine. If the last stage is a sink, it will consume the data and never emit anything
3183
+ // to us here...
3184
+ for (var _d = true, result_1 = __asyncValues(result), result_1_1; result_1_1 = await result_1.next(), _a = result_1_1.done, !_a; _d = true) {
3185
+ _c = result_1_1.value;
3186
+ _d = false;
3187
+ const chunk = _c;
3188
+ }
3189
+ }
3190
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
3191
+ finally {
3192
+ try {
3193
+ if (!_d && !_a && (_b = result_1.return)) await _b.call(result_1);
3194
+ }
3195
+ finally { if (e_1) throw e_1.error; }
3196
+ }
3197
+ }
3198
+
3199
+ function consumeChunkedBinaryStream(source) {
3200
+ return __asyncGenerator(this, arguments, function* consumeChunkedBinaryStream_1() {
3201
+ var _a, e_1, _b, _c;
3202
+ let state = 0;
3203
+ let sizeBuf = new Uint8Array(4);
3204
+ let sizeBufPos = 0;
3205
+ let bufs = [];
3206
+ let len = 0;
3207
+ try {
3208
+ for (var _d = true, source_1 = __asyncValues(source), source_1_1; source_1_1 = yield __await(source_1.next()), _a = source_1_1.done, !_a; _d = true) {
3209
+ _c = source_1_1.value;
3210
+ _d = false;
3211
+ const chunk = _c;
3212
+ const dw = new DataView(chunk.buffer, chunk.byteOffset, chunk.byteLength);
3213
+ let pos = 0;
3214
+ while (pos < chunk.byteLength) {
3215
+ switch (state) {
3216
+ case 0:
3217
+ // Beginning of a size header
3218
+ if (pos + 4 > chunk.byteLength) {
3219
+ for (const b of chunk.slice(pos)) {
3220
+ if (sizeBufPos === 4)
3221
+ break;
3222
+ sizeBuf[sizeBufPos++] = b;
3223
+ ++pos;
3224
+ }
3225
+ if (sizeBufPos < 4) {
3226
+ // Need more bytes in order to read length.
3227
+ // Will go out from while loop as well because pos is defenitely = chunk.byteLength here.
3228
+ break;
3229
+ }
3230
+ }
3231
+ else if (sizeBufPos > 0 && sizeBufPos < 4) {
3232
+ for (const b of chunk.slice(pos, pos + 4 - sizeBufPos)) {
3233
+ sizeBuf[sizeBufPos++] = b;
3234
+ ++pos;
3235
+ }
3236
+ }
3237
+ // Intentional fall-through...
3238
+ case 1:
3239
+ len =
3240
+ sizeBufPos === 4
3241
+ ? new DataView(sizeBuf.buffer, 0, 4).getUint32(0, false)
3242
+ : dw.getUint32(pos, false);
3243
+ if (sizeBufPos)
3244
+ sizeBufPos = 0; // in this case pos is already forwarded
3245
+ else
3246
+ pos += 4; // else pos is not yet forwarded - that's why we do it now
3247
+ // Intentional fall-through...
3248
+ case 2:
3249
+ // Eat the chunk
3250
+ if (pos >= chunk.byteLength) {
3251
+ state = 2;
3252
+ break;
3253
+ }
3254
+ if (pos + len > chunk.byteLength) {
3255
+ bufs.push(chunk.slice(pos));
3256
+ len -= (chunk.byteLength - pos);
3257
+ state = 2;
3258
+ pos = chunk.byteLength; // will break while loop.
3259
+ }
3260
+ else {
3261
+ if (bufs.length > 0) {
3262
+ const concats = new Uint8Array(bufs.reduce((p, c) => p + c.byteLength, len));
3263
+ let p = 0;
3264
+ for (const buf of bufs) {
3265
+ concats.set(buf, p);
3266
+ p += buf.byteLength;
3267
+ }
3268
+ concats.set(chunk.slice(pos, pos + len), p);
3269
+ bufs = [];
3270
+ yield yield __await(concats);
3271
+ }
3272
+ else {
3273
+ yield yield __await(chunk.slice(pos, pos + len));
3274
+ }
3275
+ pos += len;
3276
+ state = 0;
3277
+ }
3278
+ break;
3279
+ }
3280
+ }
3281
+ }
3282
+ }
3283
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
3284
+ finally {
3285
+ try {
3286
+ if (!_d && !_a && (_b = source_1.return)) yield __await(_b.call(source_1));
3287
+ }
3288
+ finally { if (e_1) throw e_1.error; }
3289
+ }
3290
+ });
3291
+ }
3292
+
3293
+ function getFetchResponseBodyGenerator(res) {
3294
+ return function () {
3295
+ return __asyncGenerator(this, arguments, function* () {
3296
+ if (!res.body)
3297
+ throw new Error("Response body is not readable");
3298
+ const reader = res.body.getReader();
3299
+ try {
3300
+ while (true) {
3301
+ const { done, value } = yield __await(reader.read());
3302
+ if (done)
3303
+ return yield __await(void 0);
3304
+ yield yield __await(value);
3305
+ }
3306
+ }
3307
+ finally {
3308
+ reader.releaseLock();
3309
+ }
3310
+ });
3311
+ };
3312
+ }
3313
+
2245
3314
  function listSyncifiedChanges(tablesToSyncify, currentUser, schema, alreadySyncedRealms) {
2246
3315
  return __awaiter(this, void 0, void 0, function* () {
2247
3316
  const txid = `upload-${randomString$1(8)}`;
@@ -3308,7 +4377,7 @@ function updateSyncRateLimitDelays(db, res) {
3308
4377
  }
3309
4378
 
3310
4379
  //import {BisonWebStreamReader} from "dreambase-library/dist/typeson-simplified/BisonWebStreamReader";
3311
- function syncWithServer(changes, syncState, baseRevs, db, databaseUrl, schema, clientIdentity, currentUser) {
4380
+ function syncWithServer(changes, y, syncState, baseRevs, db, databaseUrl, schema, clientIdentity, currentUser) {
3312
4381
  return __awaiter(this, void 0, void 0, function* () {
3313
4382
  //
3314
4383
  // Push changes to server using fetch
@@ -3346,6 +4415,7 @@ function syncWithServer(changes, syncState, baseRevs, db, databaseUrl, schema, c
3346
4415
  : undefined,
3347
4416
  baseRevs,
3348
4417
  changes: encodeIdsForServer(db.dx.core.schema, currentUser, changes),
4418
+ y,
3349
4419
  };
3350
4420
  console.debug('Sync request', syncRequest);
3351
4421
  db.syncStateChangedEvent.next({
@@ -3559,6 +4629,357 @@ function applyServerChanges(changes, db) {
3559
4629
  });
3560
4630
  }
3561
4631
 
4632
+ const DEXIE_CLOUD_SYNCER_ID = 'dexie-cloud-syncer';
4633
+
4634
+ function listUpdatesSince(yTable, sinceIncluding) {
4635
+ return yTable
4636
+ .where('i')
4637
+ .between(sinceIncluding, Infinity, true)
4638
+ .toArray();
4639
+ }
4640
+
4641
+ function $Y(db) {
4642
+ const $Y = db.dx._options.Y;
4643
+ if (!$Y)
4644
+ throw new Error('Y library not supplied to Dexie constructor');
4645
+ return $Y;
4646
+ }
4647
+
4648
+ /** Queries the local database for YMessages to send to server.
4649
+ *
4650
+ * There are 2 messages that this function can provide:
4651
+ * YUpdateFromClientRequest ( for local updates )
4652
+ * YStateVector ( for state vector of foreign updates so that server can reduce the number of udpates to send back )
4653
+ *
4654
+ * Notice that we do not do a step 1 sync phase here to get a state vector from the server. Reason we can avoid
4655
+ * the 2-step sync is that we are client-server and not client-client here and we keep track of the client changes
4656
+ * sent to server by letting server acknowledge them. There is always a chance that some client update has already
4657
+ * been sent and that the client failed to receive the ack. However, if this happens it does not matter - the change
4658
+ * would be sent again and Yjs handles duplicate changes anyway. And it's rare so we earn the cost of roundtrips by
4659
+ * avoiding the step1 sync and instead keep track of this in the `unsentFrom` property of the SyncState.
4660
+ *
4661
+ * @param db
4662
+ * @returns
4663
+ */
4664
+ function listYClientMessagesAndStateVector(db, tablesToSync) {
4665
+ return __awaiter(this, void 0, void 0, function* () {
4666
+ const result = [];
4667
+ const lastUpdateIds = {};
4668
+ for (const table of tablesToSync) {
4669
+ if (table.schema.yProps) {
4670
+ for (const yProp of table.schema.yProps) {
4671
+ const Y = $Y(db); // This is how we retrieve the user-provided Y library
4672
+ const yTable = db.table(yProp.updatesTable); // the updates-table for this combo of table+propName
4673
+ const syncState = (yield yTable.get(DEXIE_CLOUD_SYNCER_ID));
4674
+ // unsentFrom = the `i` value of updates that aren't yet sent to server (or at least not acked by the server yet)
4675
+ const unsentFrom = (syncState === null || syncState === void 0 ? void 0 : syncState.unsentFrom) || 1;
4676
+ // receivedUntil = the `i` value of updates that both we and the server knows we already have (we know it by the outcome from last syncWithServer() because server keep track of its revision numbers
4677
+ const receivedUntil = (syncState === null || syncState === void 0 ? void 0 : syncState.receivedUntil) || 0;
4678
+ // Compute the least value of these two (but since receivedUntil is inclusive we need to add +1 to it)
4679
+ const unsyncedFrom = Math.min(unsentFrom, receivedUntil + 1);
4680
+ // Query all these updates for all docs of this table+prop combination
4681
+ const updates = yield listUpdatesSince(yTable, unsyncedFrom);
4682
+ if (updates.length > 0)
4683
+ lastUpdateIds[yTable.name] = updates[updates.length - 1].i;
4684
+ // Now sort them by document and whether they are local or not + ignore local updates already sent:
4685
+ const perDoc = {};
4686
+ for (const update of updates) {
4687
+ // Sort updates into buckets of the doc primary key + the flag (whether it's local or foreign)
4688
+ const isLocal = ((update.f || 0) & 0x01) === 0x01;
4689
+ if (isLocal && update.i < unsentFrom)
4690
+ continue; // This local update has already been sent and acked.
4691
+ const docKey = JSON.stringify(update.k) + '/' + isLocal;
4692
+ let entry = perDoc[docKey];
4693
+ if (!entry) {
4694
+ perDoc[docKey] = entry = {
4695
+ i: update.i,
4696
+ k: update.k,
4697
+ isLocal,
4698
+ u: [],
4699
+ };
4700
+ entry.u.push(update.u);
4701
+ }
4702
+ else {
4703
+ entry.u.push(update.u);
4704
+ entry.i = Math.max(update.i, entry.i);
4705
+ }
4706
+ }
4707
+ // Now, go through all these and:
4708
+ // * For local updates, compute a merged update per document.
4709
+ // * For foreign updates, compute a state vector to pass to server, so that server can
4710
+ // avoid re-sending updates that we already have (they might have been sent of websocket
4711
+ // and when that happens, we do not mark them in any way nor do we update receivedUntil -
4712
+ // we only update receivedUntil after a "full sync" (syncWithServer()))
4713
+ for (const { k, isLocal, u, i } of Object.values(perDoc)) {
4714
+ const mergedUpdate = u.length === 1 ? u[0] : Y.mergeUpdatesV2(u);
4715
+ if (isLocal) {
4716
+ result.push({
4717
+ type: 'u-c',
4718
+ table: table.name,
4719
+ prop: yProp.prop,
4720
+ k,
4721
+ u: mergedUpdate,
4722
+ i,
4723
+ });
4724
+ }
4725
+ else {
4726
+ const stateVector = Y.encodeStateVectorFromUpdateV2(mergedUpdate);
4727
+ result.push({
4728
+ type: 'sv',
4729
+ table: table.name,
4730
+ prop: yProp.prop,
4731
+ k,
4732
+ sv: stateVector,
4733
+ });
4734
+ }
4735
+ }
4736
+ }
4737
+ }
4738
+ }
4739
+ return {
4740
+ yMessages: result,
4741
+ lastUpdateIds
4742
+ };
4743
+ });
4744
+ }
4745
+
4746
+ function getUpdatesTable(db, table, ydocProp) {
4747
+ var _a, _b, _c;
4748
+ const utbl = (_c = (_b = (_a = db.table(table)) === null || _a === void 0 ? void 0 : _a.schema.yProps) === null || _b === void 0 ? void 0 : _b.find(p => p.prop === ydocProp)) === null || _c === void 0 ? void 0 : _c.updatesTable;
4749
+ if (!utbl)
4750
+ throw new Error(`No updatesTable found for ${table}.${ydocProp}`);
4751
+ return db.table(utbl);
4752
+ }
4753
+
4754
+ function applyYServerMessages(yMessages, db) {
4755
+ return __awaiter(this, void 0, void 0, function* () {
4756
+ const result = {};
4757
+ for (const m of yMessages) {
4758
+ switch (m.type) {
4759
+ case 'u-s': {
4760
+ const utbl = getUpdatesTable(db, m.table, m.prop);
4761
+ result[utbl.name] = yield utbl.add({
4762
+ k: m.k,
4763
+ u: m.u,
4764
+ });
4765
+ break;
4766
+ }
4767
+ case 'u-ack': {
4768
+ const utbl = getUpdatesTable(db, m.table, m.prop);
4769
+ yield db.transaction('rw', utbl, (tx) => __awaiter(this, void 0, void 0, function* () {
4770
+ let syncer = (yield tx
4771
+ .table(utbl.name)
4772
+ .get(DEXIE_CLOUD_SYNCER_ID));
4773
+ yield tx.table(utbl.name).put(Object.assign(Object.assign({}, (syncer || { i: DEXIE_CLOUD_SYNCER_ID })), { unsentFrom: Math.max((syncer === null || syncer === void 0 ? void 0 : syncer.unsentFrom) || 1, m.i + 1) }));
4774
+ }));
4775
+ break;
4776
+ }
4777
+ case 'u-reject': {
4778
+ // Acces control or constraint rejected the update.
4779
+ // We delete it. It's not going to be sent again.
4780
+ // What's missing is a way to notify consumers, such as Tiptap editor, that the update was rejected.
4781
+ // This is only an issue when the document is open. We could find the open document and
4782
+ // in a perfect world, we should send a reverse update to the open document to undo the change.
4783
+ // See my question in https://discuss.yjs.dev/t/generate-an-inverse-update/2765
4784
+ console.debug(`Y update rejected. Deleting it.`);
4785
+ const utbl = getUpdatesTable(db, m.table, m.prop);
4786
+ yield utbl.delete(m.i);
4787
+ break;
4788
+ }
4789
+ case 'in-sync': {
4790
+ const doc = DexieYProvider.getDocCache(db.dx).find(m.table, m.k, m.prop);
4791
+ if (doc && !doc.isSynced) {
4792
+ doc.emit('sync', [true]);
4793
+ }
4794
+ break;
4795
+ }
4796
+ }
4797
+ }
4798
+ return result;
4799
+ });
4800
+ }
4801
+
4802
+ function updateYSyncStates(lastUpdateIdsBeforeSync, receivedUntilsAfterSync, db, serverRevision) {
4803
+ var _a, _b, _c, _d, _e;
4804
+ return __awaiter(this, void 0, void 0, function* () {
4805
+ // We want to update unsentFrom for each yTable to the value specified in first argument
4806
+ // because we got those values before we synced with server and here we are back from server
4807
+ // that has successfully received all those messages - no matter if the last update was a client or server update,
4808
+ // we can safely store unsentFrom to a value of the last update + 1 here.
4809
+ // We also want to update receivedUntil for each yTable to the value specified in the second argument,
4810
+ // because that contains the highest resulted id of each update from server after storing it.
4811
+ // We could do these two tasks separately, but that would require two update calls on the same YSyncState, so
4812
+ // to optimize the dexie calls, we merge these two maps into a single one so we can do a single update request
4813
+ // per yTable.
4814
+ const mergedSpec = {};
4815
+ for (const [yTable, lastUpdateId] of Object.entries(lastUpdateIdsBeforeSync)) {
4816
+ (_a = mergedSpec[yTable]) !== null && _a !== void 0 ? _a : (mergedSpec[yTable] = {});
4817
+ mergedSpec[yTable].unsentFrom = lastUpdateId + 1;
4818
+ }
4819
+ for (const [yTable, lastUpdateId] of Object.entries(receivedUntilsAfterSync)) {
4820
+ (_b = mergedSpec[yTable]) !== null && _b !== void 0 ? _b : (mergedSpec[yTable] = {});
4821
+ mergedSpec[yTable].receivedUntil = lastUpdateId;
4822
+ }
4823
+ // Now go through all yTables and update their YSyncStates:
4824
+ const allYTables = Object.values(db.dx._dbSchema)
4825
+ .filter((tblSchema) => tblSchema.yProps)
4826
+ .map((tblSchema) => tblSchema.yProps.map((yProp) => yProp.updatesTable))
4827
+ .flat();
4828
+ for (const yTable of allYTables) {
4829
+ const mergedEntry = mergedSpec[yTable];
4830
+ const unsentFrom = (_c = mergedEntry === null || mergedEntry === void 0 ? void 0 : mergedEntry.unsentFrom) !== null && _c !== void 0 ? _c : 1;
4831
+ const receivedUntil = (_e = (_d = mergedEntry === null || mergedEntry === void 0 ? void 0 : mergedEntry.receivedUntil) !== null && _d !== void 0 ? _d :
4832
+ // from local because we are in the same parent transaction (in sync.ts) that
4833
+ // applied all updates from the server
4834
+ (yield db
4835
+ .table(yTable)
4836
+ .where('i')
4837
+ .between(1, Infinity) // Because i might be string DEXIE_CLOUD_SYNCER_ID if not a number.
4838
+ .reverse()
4839
+ .limit(1)
4840
+ .primaryKeys())[0]) !== null && _e !== void 0 ? _e : 0;
4841
+ // We're already in a transaction, but for the sake of
4842
+ // code readability and correctness, let's launch an atomic sub transaction:
4843
+ yield db.transaction('rw', yTable, () => __awaiter(this, void 0, void 0, function* () {
4844
+ const state = yield db
4845
+ .table(yTable)
4846
+ .get(DEXIE_CLOUD_SYNCER_ID);
4847
+ if (!state) {
4848
+ yield db.table(yTable).add({
4849
+ i: DEXIE_CLOUD_SYNCER_ID,
4850
+ unsentFrom,
4851
+ receivedUntil,
4852
+ serverRev: serverRevision,
4853
+ });
4854
+ }
4855
+ else {
4856
+ state.unsentFrom = Math.max(unsentFrom, state.unsentFrom || 1);
4857
+ state.receivedUntil = Math.max(receivedUntil, state.receivedUntil || 0);
4858
+ state.serverRev = serverRevision;
4859
+ yield db.table(yTable).put(state);
4860
+ }
4861
+ }));
4862
+ }
4863
+ });
4864
+ }
4865
+
4866
+ const BINSTREAM_TYPE_REALMID = 1;
4867
+ const BINSTREAM_TYPE_TABLE_AND_PROP = 2;
4868
+ const BINSTREAM_TYPE_DOCUMENT = 3;
4869
+ function downloadYDocsFromServer(db, databaseUrl, { yDownloadedRealms, realms }) {
4870
+ return __awaiter(this, void 0, void 0, function* () {
4871
+ if (yDownloadedRealms &&
4872
+ realms &&
4873
+ realms.every((realmId) => yDownloadedRealms[realmId] === '*')) {
4874
+ return; // Already done!
4875
+ }
4876
+ console.debug('Downloading Y.Docs from added realms');
4877
+ const user = yield loadAccessToken(db);
4878
+ const headers = {
4879
+ 'Content-Type': 'application/json',
4880
+ Accept: 'application/octet-stream',
4881
+ };
4882
+ if (user) {
4883
+ headers.Authorization = `Bearer ${user.accessToken}`;
4884
+ }
4885
+ const res = yield fetch(`${databaseUrl}/y/download`, {
4886
+ body: TSON.stringify({ downloadedRealms: yDownloadedRealms || {} }),
4887
+ method: 'POST',
4888
+ headers,
4889
+ credentials: 'include',
4890
+ });
4891
+ if (!res.ok) {
4892
+ throw new Error(`Failed to download Yjs documents from server. Status: ${res.status}`);
4893
+ }
4894
+ yield asyncIterablePipeline(getFetchResponseBodyGenerator(res), consumeChunkedBinaryStream, consumeDownloadChunks);
4895
+ function consumeDownloadChunks(chunks) {
4896
+ return __asyncGenerator(this, arguments, function* consumeDownloadChunks_1() {
4897
+ var _a, e_1, _b, _c;
4898
+ let currentRealmId = null;
4899
+ let currentTable = null;
4900
+ let currentProp = null;
4901
+ let docsToInsert = [];
4902
+ function storeCollectedDocs(completedRealm) {
4903
+ return __awaiter(this, void 0, void 0, function* () {
4904
+ const lastDoc = docsToInsert[docsToInsert.length - 1];
4905
+ if (docsToInsert.length > 0) {
4906
+ if (!currentRealmId || !currentTable || !currentProp) {
4907
+ throw new Error(`Protocol error from ${databaseUrl}/y/download`);
4908
+ }
4909
+ const yTable = getUpdatesTable(db, currentTable, currentProp);
4910
+ yield yTable.bulkAdd(docsToInsert);
4911
+ docsToInsert = [];
4912
+ }
4913
+ if (currentRealmId &&
4914
+ ((currentTable && currentProp && lastDoc) || completedRealm)) {
4915
+ yield db.$syncState.update('syncState', (syncState) => {
4916
+ const yDownloadedRealms = syncState.yDownloadedRealms || {};
4917
+ yDownloadedRealms[currentRealmId] = completedRealm
4918
+ ? '*'
4919
+ : {
4920
+ tbl: currentTable,
4921
+ prop: currentProp,
4922
+ key: lastDoc.k,
4923
+ };
4924
+ syncState.yDownloadedRealms = yDownloadedRealms;
4925
+ });
4926
+ }
4927
+ });
4928
+ }
4929
+ try {
4930
+ try {
4931
+ for (var _d = true, chunks_1 = __asyncValues(chunks), chunks_1_1; chunks_1_1 = yield __await(chunks_1.next()), _a = chunks_1_1.done, !_a; _d = true) {
4932
+ _c = chunks_1_1.value;
4933
+ _d = false;
4934
+ const chunk = _c;
4935
+ const decoder = new Decoder(chunk);
4936
+ while (hasContent(decoder)) {
4937
+ switch (readUint8(decoder)) {
4938
+ case BINSTREAM_TYPE_REALMID:
4939
+ yield __await(storeCollectedDocs(true));
4940
+ currentRealmId = readVarString(decoder);
4941
+ break;
4942
+ case BINSTREAM_TYPE_TABLE_AND_PROP:
4943
+ yield __await(storeCollectedDocs(false)); // still on same realm
4944
+ currentTable = readVarString(decoder);
4945
+ currentProp = readVarString(decoder);
4946
+ break;
4947
+ case BINSTREAM_TYPE_DOCUMENT: {
4948
+ const k = readAny(decoder);
4949
+ const u = readVarUint8Array(decoder);
4950
+ docsToInsert.push({
4951
+ k,
4952
+ u,
4953
+ });
4954
+ break;
4955
+ }
4956
+ }
4957
+ }
4958
+ yield __await(storeCollectedDocs(false)); // Chunk full - migth still be on same realm
4959
+ }
4960
+ }
4961
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
4962
+ finally {
4963
+ try {
4964
+ if (!_d && !_a && (_b = chunks_1.return)) yield __await(_b.call(chunks_1));
4965
+ }
4966
+ finally { if (e_1) throw e_1.error; }
4967
+ }
4968
+ yield __await(storeCollectedDocs(true)); // Everything downloaded - finalize last downloaded realm to "*"
4969
+ }
4970
+ catch (error) {
4971
+ if (!(error instanceof Dexie.DexieError)) {
4972
+ // Network error might have happened.
4973
+ // Store what we've collected so far:
4974
+ yield __await(storeCollectedDocs(false));
4975
+ }
4976
+ throw error;
4977
+ }
4978
+ });
4979
+ }
4980
+ });
4981
+ }
4982
+
3562
4983
  const CURRENT_SYNC_WORKER = 'currentSyncWorker';
3563
4984
  function sync(db, options, schema, syncOptions) {
3564
4985
  return _sync
@@ -3647,10 +5068,11 @@ function _sync(db, options, schema, { isInitialSync, cancelToken, justCheckIfNee
3647
5068
  //
3648
5069
  // List changes to sync
3649
5070
  //
3650
- const [clientChangeSet, syncState, baseRevs] = yield db.transaction('r', db.tables, () => __awaiter(this, void 0, void 0, function* () {
5071
+ const [clientChangeSet, syncState, baseRevs, { yMessages, lastUpdateIds }] = yield db.transaction('r', db.tables, () => __awaiter(this, void 0, void 0, function* () {
3651
5072
  const syncState = yield db.getPersistedSyncState();
3652
5073
  const baseRevs = yield db.$baseRevs.toArray();
3653
5074
  let clientChanges = yield listClientChanges(mutationTables);
5075
+ const yResults = yield listYClientMessagesAndStateVector(db, tablesToSync);
3654
5076
  throwIfCancelled(cancelToken);
3655
5077
  if (doSyncify) {
3656
5078
  const alreadySyncedRealms = [
@@ -3660,11 +5082,11 @@ function _sync(db, options, schema, { isInitialSync, cancelToken, justCheckIfNee
3660
5082
  const syncificationInserts = yield listSyncifiedChanges(tablesToSyncify, currentUser, schema, alreadySyncedRealms);
3661
5083
  throwIfCancelled(cancelToken);
3662
5084
  clientChanges = clientChanges.concat(syncificationInserts);
3663
- return [clientChanges, syncState, baseRevs];
5085
+ return [clientChanges, syncState, baseRevs, yResults];
3664
5086
  }
3665
- return [clientChanges, syncState, baseRevs];
5087
+ return [clientChanges, syncState, baseRevs, yResults];
3666
5088
  }));
3667
- const pushSyncIsNeeded = clientChangeSet.some((set) => set.muts.some((mut) => mut.keys.length > 0));
5089
+ const pushSyncIsNeeded = clientChangeSet.some((set) => set.muts.some((mut) => mut.keys.length > 0)) || yMessages.some(m => m.type === 'u-c');
3668
5090
  if (justCheckIfNeeded) {
3669
5091
  console.debug('Sync is needed:', pushSyncIsNeeded);
3670
5092
  return pushSyncIsNeeded;
@@ -3679,12 +5101,12 @@ function _sync(db, options, schema, { isInitialSync, cancelToken, justCheckIfNee
3679
5101
  // Push changes to server
3680
5102
  //
3681
5103
  throwIfCancelled(cancelToken);
3682
- const res = yield syncWithServer(clientChangeSet, syncState, baseRevs, db, databaseUrl, schema, clientIdentity, currentUser);
5104
+ const res = yield syncWithServer(clientChangeSet, yMessages, syncState, baseRevs, db, databaseUrl, schema, clientIdentity, currentUser);
3683
5105
  console.debug('Sync response', res);
3684
5106
  //
3685
5107
  // Apply changes locally and clear old change entries:
3686
5108
  //
3687
- const done = yield db.transaction('rw', db.tables, (tx) => __awaiter(this, void 0, void 0, function* () {
5109
+ const { done, newSyncState } = yield db.transaction('rw', db.tables, (tx) => __awaiter(this, void 0, void 0, function* () {
3688
5110
  // @ts-ignore
3689
5111
  tx.idbtrans.disableChangeTracking = true;
3690
5112
  // @ts-ignore
@@ -3776,17 +5198,35 @@ function _sync(db, options, schema, { isInitialSync, cancelToken, justCheckIfNee
3776
5198
  // apply server changes
3777
5199
  //
3778
5200
  yield applyServerChanges(filteredChanges, db);
5201
+ if (res.yMessages) {
5202
+ //
5203
+ // apply yMessages
5204
+ //
5205
+ const receivedUntils = yield applyYServerMessages(res.yMessages, db);
5206
+ //
5207
+ // update Y SyncStates
5208
+ //
5209
+ yield updateYSyncStates(lastUpdateIds, receivedUntils, db, res.serverRevision);
5210
+ }
3779
5211
  //
3780
- // Update syncState
5212
+ // Update regular syncState
3781
5213
  //
3782
5214
  db.$syncState.put(newSyncState, 'syncState');
3783
- return addedClientChanges.length === 0;
5215
+ return {
5216
+ done: addedClientChanges.length === 0,
5217
+ newSyncState
5218
+ };
3784
5219
  }));
3785
5220
  if (!done) {
3786
5221
  console.debug('MORE SYNC NEEDED. Go for it again!');
3787
5222
  yield checkSyncRateLimitDelay(db);
3788
5223
  return yield _sync(db, options, schema, { isInitialSync, cancelToken });
3789
5224
  }
5225
+ const usingYProps = Object.values(schema).some(tbl => { var _a; return (_a = tbl.yProps) === null || _a === void 0 ? void 0 : _a.length; });
5226
+ const serverSupportsYprops = !!res.yMessages;
5227
+ if (usingYProps && serverSupportsYprops) {
5228
+ yield downloadYDocsFromServer(db, databaseUrl, newSyncState);
5229
+ }
3790
5230
  console.debug('SYNC DONE', { isInitialSync });
3791
5231
  db.syncCompleteEvent.next();
3792
5232
  return false; // Not needed anymore
@@ -3839,6 +5279,18 @@ function deleteObjectsFromRemovedRealms(db, res, prevState) {
3839
5279
  }
3840
5280
  }
3841
5281
  }
5282
+ if (rejectedRealms.size > 0) {
5283
+ // Remove rejected/deleted realms from yDownloadedRealms because of the following use case:
5284
+ // 1. User becomes added to the realm
5285
+ // 2. User syncs and all documents of the realm is downloaded (downloadYDocsFromServer.ts)
5286
+ // 3. User leaves the realm and all docs are deleted locally (built-in-trigger of deleting their rows in this file)
5287
+ // 4. User is yet again added to the realm. At this point, we must make sure the docs are not considered already downloaded.
5288
+ const updateSpec = {};
5289
+ for (const realmId of rejectedRealms) {
5290
+ updateSpec[`yDownloadedRealms.${realmId}`] = undefined; // Setting to undefined will delete the property
5291
+ }
5292
+ yield db.$syncState.update('syncState', updateSpec);
5293
+ }
3842
5294
  });
3843
5295
  }
3844
5296
  function filterServerChangesThroughAddedClientChanges(serverChanges, addedClientChanges) {
@@ -4148,6 +5600,7 @@ function DexieCloudDB(dx) {
4148
5600
  };
4149
5601
  Object.assign(db, helperMethods);
4150
5602
  db.messageConsumer = MessagesFromServerConsumer(db);
5603
+ db.messageProducer = new Subject();
4151
5604
  wm$1.set(dx.cloud, db);
4152
5605
  }
4153
5606
  return db;
@@ -4483,24 +5936,6 @@ const safariVersion = isSafari
4483
5936
  const DISABLE_SERVICEWORKER_STRATEGY = (isSafari && safariVersion <= 605) || // Disable for Safari for now.
4484
5937
  isFirefox; // Disable for Firefox for now. Seems to have a bug in reading CryptoKeys from IDB from service workers
4485
5938
 
4486
- /* Helper function to subscribe to database close no matter if it was unexpectedly closed or manually using db.close()
4487
- */
4488
- function dbOnClosed(db, handler) {
4489
- db.on.close.subscribe(handler);
4490
- // @ts-ignore
4491
- const origClose = db._close;
4492
- // @ts-ignore
4493
- db._close = function () {
4494
- origClose.call(this);
4495
- handler();
4496
- };
4497
- return () => {
4498
- db.on.close.unsubscribe(handler);
4499
- // @ts-ignore
4500
- db._close = origClose;
4501
- };
4502
- }
4503
-
4504
5939
  const IS_SERVICE_WORKER = typeof self !== "undefined" && "clients" in self && !self.document;
4505
5940
 
4506
5941
  function throwVersionIncrementNeeded() {
@@ -4966,13 +6401,18 @@ function createMutationTrackingMiddleware({ currentUserObservable, db, }) {
4966
6401
  values = values.filter((_, idx) => !failures[idx]);
4967
6402
  }
4968
6403
  const ts = Date.now();
6404
+ // Canonicalize req.criteria.index to null if it's on the primary key.
6405
+ const criteria = 'criteria' in req && req.criteria
6406
+ ? Object.assign(Object.assign({}, req.criteria), { index: req.criteria.index === schema.primaryKey.keyPath // Use null to inform server that criteria is on primary key
6407
+ ? null // This will disable the server from trying to log consistent operations where it shouldnt.
6408
+ : req.criteria.index }) : undefined;
4969
6409
  const mut = req.type === 'delete'
4970
6410
  ? {
4971
6411
  type: 'delete',
4972
6412
  ts,
4973
6413
  opNo,
4974
6414
  keys,
4975
- criteria: req.criteria,
6415
+ criteria,
4976
6416
  txid,
4977
6417
  userId,
4978
6418
  }
@@ -4986,14 +6426,14 @@ function createMutationTrackingMiddleware({ currentUserObservable, db, }) {
4986
6426
  userId,
4987
6427
  values,
4988
6428
  }
4989
- : req.criteria && req.changeSpec
6429
+ : criteria && req.changeSpec
4990
6430
  ? {
4991
6431
  // Common changeSpec for all keys
4992
6432
  type: 'modify',
4993
6433
  ts,
4994
6434
  opNo,
4995
6435
  keys,
4996
- criteria: req.criteria,
6436
+ criteria,
4997
6437
  changeSpec: req.changeSpec,
4998
6438
  txid,
4999
6439
  userId,
@@ -5021,7 +6461,7 @@ function createMutationTrackingMiddleware({ currentUserObservable, db, }) {
5021
6461
  if ('isAdditionalChunk' in req && req.isAdditionalChunk) {
5022
6462
  mut.isAdditionalChunk = true;
5023
6463
  }
5024
- return keys.length > 0 || ('criteria' in req && req.criteria)
6464
+ return keys.length > 0 || criteria
5025
6465
  ? mutsTable
5026
6466
  .mutate({ type: 'add', trans, values: [mut] }) // Log entry
5027
6467
  .then(() => res) // Return original response
@@ -5035,6 +6475,7 @@ function createMutationTrackingMiddleware({ currentUserObservable, db, }) {
5035
6475
 
5036
6476
  function overrideParseStoresSpec(origFunc, dexie) {
5037
6477
  return function (stores, dbSchema) {
6478
+ var _a;
5038
6479
  const storesClone = Object.assign(Object.assign({}, DEXIE_CLOUD_SCHEMA), stores);
5039
6480
  // Merge indexes of DEXIE_CLOUD_SCHEMA with stores
5040
6481
  Object.keys(DEXIE_CLOUD_SCHEMA).forEach((tableName) => {
@@ -5095,6 +6536,14 @@ function overrideParseStoresSpec(origFunc, dexie) {
5095
6536
  }
5096
6537
  });
5097
6538
  const rv = origFunc.call(this, storesClone, dbSchema);
6539
+ for (const [tableName, spec] of Object.entries(dbSchema)) {
6540
+ if ((_a = spec.yProps) === null || _a === void 0 ? void 0 : _a.length) {
6541
+ const cloudTableSchema = cloudSchema[tableName];
6542
+ if (cloudTableSchema) {
6543
+ cloudTableSchema.yProps = spec.yProps.map((yProp) => yProp.prop);
6544
+ }
6545
+ }
6546
+ }
5098
6547
  return rv;
5099
6548
  };
5100
6549
  }
@@ -5180,31 +6629,82 @@ class TokenExpiredError extends Error {
5180
6629
  }
5181
6630
  }
5182
6631
 
6632
+ function createYClientUpdateObservable(db) {
6633
+ const yTableRecords = flatten(db.tables
6634
+ .filter((table) => { var _a, _b; return ((_b = (_a = db.cloud.schema) === null || _a === void 0 ? void 0 : _a[table.name]) === null || _b === void 0 ? void 0 : _b.markedForSync) && table.schema.yProps; })
6635
+ .map((table) => table.schema.yProps.map((p) => ({
6636
+ table: table.name,
6637
+ ydocProp: p.prop,
6638
+ updatesTable: p.updatesTable,
6639
+ }))));
6640
+ return merge(...yTableRecords.map(({ table, ydocProp, updatesTable }) => {
6641
+ let currentUnsentFrom = 1;
6642
+ return liveQuery(() => __awaiter(this, void 0, void 0, function* () {
6643
+ const yTbl = db.table(updatesTable);
6644
+ const unsentFrom = yield yTbl
6645
+ .where({ i: DEXIE_CLOUD_SYNCER_ID })
6646
+ .first()
6647
+ .then((syncer) => (syncer === null || syncer === void 0 ? void 0 : syncer.unsentFrom) || 1);
6648
+ currentUnsentFrom = Math.max(currentUnsentFrom, unsentFrom);
6649
+ const addedUpdates = yield listUpdatesSince(yTbl, currentUnsentFrom);
6650
+ // Update currentUnsentFrom to only listen for updates that will be newer than the ones we emitted.
6651
+ currentUnsentFrom = Math.max(currentUnsentFrom, ...addedUpdates.map((update) => update.i + 1));
6652
+ return addedUpdates
6653
+ .filter((update) => update.f && update.f & 1) // Only include local updates
6654
+ .map((update) => {
6655
+ return {
6656
+ type: 'u-c',
6657
+ table,
6658
+ prop: ydocProp,
6659
+ k: update.k,
6660
+ u: update.u,
6661
+ i: update.i,
6662
+ };
6663
+ });
6664
+ }));
6665
+ })).pipe(
6666
+ // Flatten the array of messages.
6667
+ // If messageProducer emits empty array, nothing is emitted
6668
+ // but if messageProducer emits array of messages, they are
6669
+ // emitted one by one.
6670
+ mergeMap$1((messages) => messages));
6671
+ }
6672
+
6673
+ function getAwarenessLibrary(db) {
6674
+ var _a, _b;
6675
+ if (!((_a = db.cloud.options) === null || _a === void 0 ? void 0 : _a.awarenessProtocol)) {
6676
+ throw new Dexie.MissingAPIError('awarenessProtocol was not provided to db.cloud.configure(). Please import * as awarenessProtocol from "y-protocols/awareness".');
6677
+ }
6678
+ return (_b = db.cloud.options) === null || _b === void 0 ? void 0 : _b.awarenessProtocol;
6679
+ }
6680
+ const awarenessWeakMap = new WeakMap();
6681
+ const getDocAwareness = (doc) => awarenessWeakMap.get(doc);
6682
+
5183
6683
  const SERVER_PING_TIMEOUT = 20000;
5184
6684
  const CLIENT_PING_INTERVAL = 30000;
5185
6685
  const FAIL_RETRY_WAIT_TIME = 60000;
5186
6686
  class WSObservable extends Observable$1 {
5187
- constructor(databaseUrl, rev, realmSetHash, clientIdentity, messageProducer, webSocketStatus, token, tokenExpiration) {
5188
- super((subscriber) => new WSConnection(databaseUrl, rev, realmSetHash, clientIdentity, token, tokenExpiration, subscriber, messageProducer, webSocketStatus));
6687
+ constructor(db, rev, realmSetHash, clientIdentity, messageProducer, webSocketStatus, user) {
6688
+ super((subscriber) => new WSConnection(db, rev, realmSetHash, clientIdentity, user, subscriber, messageProducer, webSocketStatus));
5189
6689
  }
5190
6690
  }
5191
6691
  let counter = 0;
5192
6692
  class WSConnection extends Subscription$1 {
5193
- constructor(databaseUrl, rev, realmSetHash, clientIdentity, token, tokenExpiration, subscriber, messageProducer, webSocketStatus) {
6693
+ constructor(db, rev, realmSetHash, clientIdentity, user, subscriber, messageProducer, webSocketStatus) {
5194
6694
  super(() => this.teardown());
5195
6695
  this.id = ++counter;
6696
+ this.subscriptions = new Set();
5196
6697
  this.reconnecting = false;
5197
- console.debug('New WebSocket Connection', this.id, token ? 'authorized' : 'unauthorized');
5198
- this.databaseUrl = databaseUrl;
6698
+ console.debug('New WebSocket Connection', this.id, user.accessToken ? 'authorized' : 'unauthorized');
6699
+ this.db = db;
6700
+ this.databaseUrl = db.cloud.options.databaseUrl;
5199
6701
  this.rev = rev;
5200
6702
  this.realmSetHash = realmSetHash;
5201
6703
  this.clientIdentity = clientIdentity;
5202
- this.token = token;
5203
- this.tokenExpiration = tokenExpiration;
6704
+ this.user = user;
5204
6705
  this.subscriber = subscriber;
5205
6706
  this.lastUserActivity = new Date();
5206
6707
  this.messageProducer = messageProducer;
5207
- this.messageProducerSubscription = null;
5208
6708
  this.webSocketStatus = webSocketStatus;
5209
6709
  this.connect();
5210
6710
  }
@@ -5225,10 +6725,10 @@ class WSConnection extends Subscription$1 {
5225
6725
  catch (_a) { }
5226
6726
  }
5227
6727
  this.ws = null;
5228
- if (this.messageProducerSubscription) {
5229
- this.messageProducerSubscription.unsubscribe();
5230
- this.messageProducerSubscription = null;
6728
+ for (const sub of this.subscriptions) {
6729
+ sub.unsubscribe();
5231
6730
  }
6731
+ this.subscriptions.clear();
5232
6732
  }
5233
6733
  reconnect() {
5234
6734
  if (this.reconnecting)
@@ -5261,7 +6761,8 @@ class WSConnection extends Subscription$1 {
5261
6761
  //console.debug('SyncStatus: DUBB: Ooops it was closed!');
5262
6762
  return;
5263
6763
  }
5264
- if (this.tokenExpiration && this.tokenExpiration < new Date()) {
6764
+ const tokenExpiration = this.user.accessTokenExpiration;
6765
+ if (tokenExpiration && tokenExpiration < new Date()) {
5265
6766
  this.subscriber.error(new TokenExpiredError()); // Will be handled in connectWebSocket.ts.
5266
6767
  return;
5267
6768
  }
@@ -5316,13 +6817,13 @@ class WSConnection extends Subscription$1 {
5316
6817
  searchParams.set('rev', this.rev);
5317
6818
  searchParams.set('realmsHash', this.realmSetHash);
5318
6819
  searchParams.set('clientId', this.clientIdentity);
5319
- if (this.token) {
5320
- searchParams.set('token', this.token);
6820
+ if (this.user.accessToken) {
6821
+ searchParams.set('token', this.user.accessToken);
5321
6822
  }
5322
6823
  // Connect the WebSocket to given url:
5323
6824
  console.debug('dexie-cloud WebSocket create');
5324
6825
  const ws = (this.ws = new WebSocket(`${wsUrl}/changes?${searchParams}`));
5325
- //ws.binaryType = "arraybuffer"; // For future when subscribing to actual changes.
6826
+ ws.binaryType = "arraybuffer";
5326
6827
  ws.onclose = (event) => {
5327
6828
  if (!this.pinger)
5328
6829
  return;
@@ -5335,14 +6836,30 @@ class WSConnection extends Subscription$1 {
5335
6836
  console.debug('dexie-cloud WebSocket onmessage', event.data);
5336
6837
  this.lastServerActivity = new Date();
5337
6838
  try {
5338
- const msg = TSON.parse(event.data);
6839
+ const msg = typeof event.data === 'string'
6840
+ ? TSON.parse(event.data)
6841
+ : decodeYMessage(new Uint8Array(event.data));
5339
6842
  if (msg.type === 'error') {
5340
6843
  throw new Error(`Error message from dexie-cloud: ${msg.error}`);
5341
6844
  }
5342
- if (msg.type === 'rev') {
6845
+ else if (msg.type === 'rev') {
5343
6846
  this.rev = msg.rev; // No meaning but seems reasonable.
5344
6847
  }
5345
- if (msg.type !== 'pong') {
6848
+ else if (msg.type === 'aware') {
6849
+ const docCache = DexieYProvider.getDocCache(this.db.dx);
6850
+ const doc = docCache.find(msg.table, msg.k, msg.prop);
6851
+ if (doc) {
6852
+ const awareness = getDocAwareness(doc);
6853
+ if (awareness) {
6854
+ const awap = getAwarenessLibrary(this.db);
6855
+ awap.applyAwarenessUpdate(awareness, msg.u, 'server');
6856
+ }
6857
+ }
6858
+ }
6859
+ else if (msg.type === 'u-ack' || msg.type === 'u-reject' || msg.type === 'u-s' || msg.type === 'in-sync') {
6860
+ applyYServerMessages([msg], this.db);
6861
+ }
6862
+ else if (msg.type !== 'pong') {
5346
6863
  this.subscriber.next(msg);
5347
6864
  }
5348
6865
  }
@@ -5370,16 +6887,26 @@ class WSConnection extends Subscription$1 {
5370
6887
  }
5371
6888
  };
5372
6889
  });
5373
- this.messageProducerSubscription = this.messageProducer.subscribe((msg) => {
5374
- var _a;
6890
+ this.subscriptions.add(this.messageProducer.subscribe((msg) => {
6891
+ var _a, _b;
5375
6892
  if (!this.closed) {
5376
6893
  if (msg.type === 'ready' &&
5377
6894
  this.webSocketStatus.value !== 'connected') {
5378
6895
  this.webSocketStatus.next('connected');
5379
6896
  }
5380
- (_a = this.ws) === null || _a === void 0 ? void 0 : _a.send(TSON.stringify(msg));
6897
+ if (msg.type === 'ready') {
6898
+ (_a = this.ws) === null || _a === void 0 ? void 0 : _a.send(TSON.stringify(msg));
6899
+ }
6900
+ else {
6901
+ // If it's not a "ready" message, it's an YMessage.
6902
+ // YMessages can be sent binary encoded.
6903
+ (_b = this.ws) === null || _b === void 0 ? void 0 : _b.send(encodeYMessage(msg));
6904
+ }
5381
6905
  }
5382
- });
6906
+ }));
6907
+ if (this.user.isLoggedIn && !isEagerSyncDisabled(this.db)) {
6908
+ this.subscriptions.add(createYClientUpdateObservable(this.db).subscribe(this.db.messageProducer));
6909
+ }
5383
6910
  }
5384
6911
  catch (error) {
5385
6912
  this.pauseUntil = new Date(Date.now() + FAIL_RETRY_WAIT_TIME);
@@ -5421,7 +6948,7 @@ function connectWebSocket(db) {
5421
6948
  if (!((_a = db.cloud.options) === null || _a === void 0 ? void 0 : _a.databaseUrl)) {
5422
6949
  throw new Error(`No database URL to connect WebSocket to`);
5423
6950
  }
5424
- const messageProducer = db.messageConsumer.readyToServe.pipe(filter((isReady) => isReady), // When consumer is ready for new messages, produce such a message to inform server about it
6951
+ const readyForChangesMessage = db.messageConsumer.readyToServe.pipe(filter((isReady) => isReady), // When consumer is ready for new messages, produce such a message to inform server about it
5425
6952
  switchMap(() => db.getPersistedSyncState()), // We need the info on which server revision we are at:
5426
6953
  filter((syncState) => syncState && syncState.serverRevision), // We wont send anything to server before inital sync has taken place
5427
6954
  switchMap((syncState) => __awaiter(this, void 0, void 0, function* () {
@@ -5432,6 +6959,7 @@ function connectWebSocket(db) {
5432
6959
  realmSetHash: yield computeRealmSetHash(syncState)
5433
6960
  });
5434
6961
  })));
6962
+ const messageProducer = merge(readyForChangesMessage, db.messageProducer);
5435
6963
  function createObservable() {
5436
6964
  return db.cloud.persistedSyncState.pipe(filter((syncState) => syncState === null || syncState === void 0 ? void 0 : syncState.serverRevision), // Don't connect before there's no initial sync performed.
5437
6965
  take(1), // Don't continue waking up whenever syncState change
@@ -5458,7 +6986,7 @@ function connectWebSocket(db) {
5458
6986
  // If no new entries, server won't bother the client. If new entries, server sends only those
5459
6987
  // and the baseRev of the last from same client-ID.
5460
6988
  if (userLogin) {
5461
- return new WSObservable(db.cloud.options.databaseUrl, db.cloud.persistedSyncState.value.serverRevision, realmSetHash, db.cloud.persistedSyncState.value.clientIdentity, messageProducer, db.cloud.webSocketStatus, userLogin.accessToken, userLogin.accessTokenExpiration);
6989
+ return new WSObservable(db, db.cloud.persistedSyncState.value.serverRevision, realmSetHash, db.cloud.persistedSyncState.value.clientIdentity, messageProducer, db.cloud.webSocketStatus, userLogin);
5462
6990
  }
5463
6991
  else {
5464
6992
  return from$1([]);
@@ -6264,6 +7792,130 @@ const getInvitesObservable = associate((db) => {
6264
7792
  })), []);
6265
7793
  });
6266
7794
 
7795
+ function createYHandler(db) {
7796
+ const awap = getAwarenessLibrary(db);
7797
+ return (provider) => {
7798
+ var _a;
7799
+ const doc = provider.doc;
7800
+ const { parentTable, parentId, parentProp, updatesTable } = doc.meta;
7801
+ if (!((_a = db.cloud.schema) === null || _a === void 0 ? void 0 : _a[parentTable].markedForSync)) {
7802
+ return; // The table that holds the doc is not marked for sync - leave it to dexie. No syncing, no awareness.
7803
+ }
7804
+ let awareness = new awap.Awareness(doc);
7805
+ awarenessWeakMap.set(doc, awareness);
7806
+ provider.awareness = awareness;
7807
+ awareness.on('update', ({ added, updated, removed }, origin) => {
7808
+ // Send the update
7809
+ const changedClients = added.concat(updated).concat(removed);
7810
+ const user = db.cloud.currentUser.value;
7811
+ if (origin !== 'server' && user.isLoggedIn && !isEagerSyncDisabled(db)) {
7812
+ const update = awap.encodeAwarenessUpdate(awareness, changedClients);
7813
+ db.messageProducer.next({
7814
+ type: 'aware',
7815
+ table: parentTable,
7816
+ prop: parentProp,
7817
+ k: doc.meta.parentId,
7818
+ u: update,
7819
+ });
7820
+ if (provider.destroyed) {
7821
+ // We're called from awareness.on('destroy') that did
7822
+ // removeAwarenessStates.
7823
+ // It's time to also send the doc-close message that dexie-cloud understands
7824
+ // and uses to stop subscribing for updates and awareness updates and brings
7825
+ // down the cached information in memory on the WS connection for this.
7826
+ db.messageProducer.next({
7827
+ type: 'doc-close',
7828
+ table: parentTable,
7829
+ prop: parentProp,
7830
+ k: doc.meta.parentId
7831
+ });
7832
+ }
7833
+ }
7834
+ });
7835
+ awareness.on('destroy', () => {
7836
+ // Signal to server that this provider is destroyed (the update event will be triggered, which
7837
+ // in turn will trigger db.messageProducer that will send the message to the server if WS is connected)
7838
+ awap.removeAwarenessStates(awareness, [doc.clientID], 'provider destroyed');
7839
+ });
7840
+ // Now wait til document is loaded and then open the document on the server
7841
+ provider.on('load', () => __awaiter(this, void 0, void 0, function* () {
7842
+ if (provider.destroyed)
7843
+ return;
7844
+ let connected = false;
7845
+ let currentFlowId = 1;
7846
+ const subscription = db.cloud.webSocketStatus.subscribe((wsStatus) => {
7847
+ if (provider.destroyed)
7848
+ return;
7849
+ // Keep "connected" state in a variable so we can check it after async operations
7850
+ connected = wsStatus === 'connected';
7851
+ // We are or got connected. Open the document on the server.
7852
+ const user = db.cloud.currentUser.value;
7853
+ if (wsStatus === "connected" && user.isLoggedIn && !isEagerSyncDisabled(db)) {
7854
+ ++currentFlowId;
7855
+ openDocumentOnServer().catch(error => {
7856
+ console.warn(`Error catched in createYHandler.ts: ${error}`);
7857
+ });
7858
+ }
7859
+ });
7860
+ // Wait until WebSocket is connected
7861
+ provider.addCleanupHandler(subscription);
7862
+ /** Sends an 'doc-open' message to server whenever websocket becomes
7863
+ * connected, or if it is already connected.
7864
+ * The flow is aborted in case websocket is disconnected while querying
7865
+ * information required to compute the state vector. Flow is also
7866
+ * aborted in case document or provider has been destroyed during
7867
+ * the async parts of the task.
7868
+ *
7869
+ * The state vector is only computed from the updates that have occured
7870
+ * after the last full sync - which could very often be zero - in which
7871
+ * case no state vector is sent (then the server already knows us by
7872
+ * revision)
7873
+ *
7874
+ * When server gets the doc-open message, it will authorized us for
7875
+ * whether we are allowed to read / write to this document, and then
7876
+ * keep the cached information in memory on the WS connection for this
7877
+ * particular document, as well as subscribe to updates and awareness updates
7878
+ * from other clients on the document.
7879
+ */
7880
+ function openDocumentOnServer(wsStatus) {
7881
+ return __awaiter(this, void 0, void 0, function* () {
7882
+ const myFlow = currentFlowId; // So we can abort when a new flow is started
7883
+ const yTbl = db.table(updatesTable);
7884
+ const syncState = yield yTbl.get(DEXIE_CLOUD_SYNCER_ID);
7885
+ // After every await, check if we still should be working on this task.
7886
+ if (provider.destroyed || currentFlowId !== myFlow || !connected)
7887
+ return;
7888
+ const receivedUntil = (syncState === null || syncState === void 0 ? void 0 : syncState.receivedUntil) || 0;
7889
+ const docOpenMsg = {
7890
+ type: 'doc-open',
7891
+ table: parentTable,
7892
+ prop: parentProp,
7893
+ k: parentId,
7894
+ serverRev: syncState === null || syncState === void 0 ? void 0 : syncState.serverRev,
7895
+ };
7896
+ const serverUpdatesSinceLastSync = yield yTbl
7897
+ .where('i')
7898
+ .between(receivedUntil, Infinity, false)
7899
+ .filter((update) => cmp(update.k, parentId) === 0 && // Only updates for this document
7900
+ ((update.f || 0) & 1) === 0 // Don't include local changes
7901
+ )
7902
+ .toArray();
7903
+ // After every await, check if we still should be working on this task.
7904
+ if (provider.destroyed || currentFlowId !== myFlow || !connected)
7905
+ return;
7906
+ if (serverUpdatesSinceLastSync.length > 0) {
7907
+ const Y = $Y(db); // Get the Yjs library from Dexie constructor options
7908
+ const mergedUpdate = Y.mergeUpdatesV2(serverUpdatesSinceLastSync.map((update) => update.u));
7909
+ const stateVector = Y.encodeStateVectorFromUpdateV2(mergedUpdate);
7910
+ docOpenMsg.sv = stateVector;
7911
+ }
7912
+ db.messageProducer.next(docOpenMsg);
7913
+ });
7914
+ }
7915
+ }));
7916
+ };
7917
+ }
7918
+
6267
7919
  const DEFAULT_OPTIONS = {
6268
7920
  nameSuffix: true,
6269
7921
  };
@@ -6293,8 +7945,9 @@ function dexieCloud(dexie) {
6293
7945
  if (closed)
6294
7946
  throw new Dexie.DatabaseClosedError();
6295
7947
  }
6296
- dbOnClosed(dexie, () => {
7948
+ dexie.once('close', () => {
6297
7949
  subscriptions.forEach((subscription) => subscription.unsubscribe());
7950
+ subscriptions.splice(0, subscriptions.length);
6298
7951
  closed = true;
6299
7952
  localSyncWorker && localSyncWorker.stop();
6300
7953
  localSyncWorker = null;
@@ -6303,7 +7956,7 @@ function dexieCloud(dexie) {
6303
7956
  const syncComplete = new Subject();
6304
7957
  dexie.cloud = {
6305
7958
  // @ts-ignore
6306
- version: "4.0.8",
7959
+ version: "4.1.0-alpha.3",
6307
7960
  options: Object.assign({}, DEFAULT_OPTIONS),
6308
7961
  schema: null,
6309
7962
  get currentUserId() {
@@ -6449,6 +8102,7 @@ function dexieCloud(dexie) {
6449
8102
  throw new Error(`Internal error`); // options cannot be null if configuredProgramatically is set.
6450
8103
  const newPersistedOptions = Object.assign({}, options);
6451
8104
  delete newPersistedOptions.fetchTokens;
8105
+ delete newPersistedOptions.awarenessProtocol;
6452
8106
  yield db.$syncState.put(newPersistedOptions, 'options');
6453
8107
  }
6454
8108
  if (((_h = db.cloud.options) === null || _h === void 0 ? void 0 : _h.tryUseServiceWorker) &&
@@ -6526,6 +8180,12 @@ function dexieCloud(dexie) {
6526
8180
  currentUserEmitter.pipe(skip(1), take(1)),
6527
8181
  db.cloud.persistedSyncState.pipe(skip(1), take(1)),
6528
8182
  ]));
8183
+ const yHandler = createYHandler(db);
8184
+ db.dx.on('y', yHandler);
8185
+ db.dx.once('close', () => {
8186
+ var _a;
8187
+ (_a = db.dx.on.y) === null || _a === void 0 ? void 0 : _a.unsubscribe(yHandler);
8188
+ });
6529
8189
  }
6530
8190
  // HERE: If requireAuth, do athentication now.
6531
8191
  let changedUser = false;
@@ -6598,7 +8258,7 @@ function dexieCloud(dexie) {
6598
8258
  }
6599
8259
  }
6600
8260
  // @ts-ignore
6601
- dexieCloud.version = "4.0.8";
8261
+ dexieCloud.version = "4.1.0-alpha.3";
6602
8262
  Dexie.Cloud = dexieCloud;
6603
8263
 
6604
8264
  // In case the SW lives for a while, let it reuse already opened connections: