dexie-cloud-addon 4.0.7 → 4.1.0-alpha.2

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 (57) hide show
  1. package/dist/modern/DexieCloudOptions.d.ts +4 -2
  2. package/dist/modern/WSObservable.d.ts +7 -4
  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 +1709 -72
  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 +1709 -72
  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/YDexieCloudSyncState.d.ts +4 -0
  16. package/dist/modern/yjs/YTable.d.ts +2 -0
  17. package/dist/modern/yjs/applyYMessages.d.ts +5 -0
  18. package/dist/modern/yjs/awareness.d.ts +4 -0
  19. package/dist/modern/yjs/createYClientUpdateObservable.d.ts +4 -0
  20. package/dist/modern/yjs/createYHandler.d.ts +5 -0
  21. package/dist/modern/yjs/downloadYDocsFromServer.d.ts +3 -0
  22. package/dist/modern/yjs/getUpdatesTable.d.ts +3 -0
  23. package/dist/modern/yjs/listUpdatesSince.d.ts +2 -0
  24. package/dist/modern/yjs/listYClientMessages.d.ts +3 -0
  25. package/dist/modern/yjs/listYClientMessagesAndStateVector.d.ts +24 -0
  26. package/dist/modern/yjs/updateYSyncStates.d.ts +6 -0
  27. package/dist/modern/yjs/y.d.ts +3 -0
  28. package/dist/umd/DexieCloudOptions.d.ts +4 -2
  29. package/dist/umd/WSObservable.d.ts +7 -4
  30. package/dist/umd/db/DexieCloudDB.d.ts +2 -0
  31. package/dist/umd/db/entities/PersistedSyncState.d.ts +7 -0
  32. package/dist/umd/dexie-cloud-addon.js +1707 -70
  33. package/dist/umd/dexie-cloud-addon.js.map +1 -1
  34. package/dist/umd/dexie-cloud-addon.min.js +1 -1
  35. package/dist/umd/dexie-cloud-addon.min.js.map +1 -1
  36. package/dist/umd/service-worker.js +1707 -70
  37. package/dist/umd/service-worker.js.map +1 -1
  38. package/dist/umd/service-worker.min.js +1 -1
  39. package/dist/umd/service-worker.min.js.map +1 -1
  40. package/dist/umd/sync/DEXIE_CLOUD_SYNCER_ID.d.ts +1 -0
  41. package/dist/umd/sync/syncWithServer.d.ts +2 -2
  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/listYClientMessages.d.ts +3 -0
  52. package/dist/umd/yjs/listYClientMessagesAndStateVector.d.ts +24 -0
  53. package/dist/umd/yjs/updateYSyncStates.d.ts +6 -0
  54. package/dist/umd/yjs/y.d.ts +3 -0
  55. package/package.json +5 -4
  56. package/dist/modern/helpers/dbOnClosed.d.ts +0 -2
  57. package/dist/umd/helpers/dbOnClosed.d.ts +0 -2
@@ -8,7 +8,7 @@
8
8
  *
9
9
  * ==========================================================================
10
10
  *
11
- * Version 4.0.7, Sun May 26 2024
11
+ * Version 4.1.0-alpha.2, Mon Oct 07 2024
12
12
  *
13
13
  * https://dexie.org
14
14
  *
@@ -470,6 +470,1075 @@
470
470
  : url.pathname.split('/')[1];
471
471
  }
472
472
 
473
+ /**
474
+ * Common Math expressions.
475
+ *
476
+ * @module math
477
+ */
478
+
479
+ const floor = Math.floor;
480
+ const abs = Math.abs;
481
+
482
+ /**
483
+ * @function
484
+ * @param {number} a
485
+ * @param {number} b
486
+ * @return {number} The smaller element of a and b
487
+ */
488
+ const min = (a, b) => a < b ? a : b;
489
+
490
+ /**
491
+ * @function
492
+ * @param {number} a
493
+ * @param {number} b
494
+ * @return {number} The bigger element of a and b
495
+ */
496
+ const max = (a, b) => a > b ? a : b;
497
+
498
+ /**
499
+ * @param {number} n
500
+ * @return {boolean} Wether n is negative. This function also differentiates between -0 and +0
501
+ */
502
+ const isNegativeZero = n => n !== 0 ? n < 0 : 1 / n < 0;
503
+
504
+ /* eslint-env browser */
505
+
506
+ const BIT7 = 64;
507
+ const BIT8 = 128;
508
+ const BITS6 = 63;
509
+ const BITS7 = 127;
510
+ /**
511
+ * @type {number}
512
+ */
513
+ const BITS31 = 0x7FFFFFFF;
514
+
515
+ /**
516
+ * Utility helpers for working with numbers.
517
+ *
518
+ * @module number
519
+ */
520
+
521
+
522
+ const MAX_SAFE_INTEGER = Number.MAX_SAFE_INTEGER;
523
+
524
+ /* c8 ignore next */
525
+ const isInteger = Number.isInteger || (num => typeof num === 'number' && isFinite(num) && floor(num) === num);
526
+
527
+ /**
528
+ * Utility module to work with Arrays.
529
+ *
530
+ * @module array
531
+ */
532
+
533
+
534
+ const isArray = Array.isArray;
535
+
536
+ /**
537
+ * @param {string} str
538
+ * @return {Uint8Array}
539
+ */
540
+ const _encodeUtf8Polyfill = str => {
541
+ const encodedString = unescape(encodeURIComponent(str));
542
+ const len = encodedString.length;
543
+ const buf = new Uint8Array(len);
544
+ for (let i = 0; i < len; i++) {
545
+ buf[i] = /** @type {number} */ (encodedString.codePointAt(i));
546
+ }
547
+ return buf
548
+ };
549
+
550
+ /* c8 ignore next */
551
+ const utf8TextEncoder = /** @type {TextEncoder} */ (typeof TextEncoder !== 'undefined' ? new TextEncoder() : null);
552
+
553
+ /**
554
+ * @param {string} str
555
+ * @return {Uint8Array}
556
+ */
557
+ const _encodeUtf8Native = str => utf8TextEncoder.encode(str);
558
+
559
+ /**
560
+ * @param {string} str
561
+ * @return {Uint8Array}
562
+ */
563
+ /* c8 ignore next */
564
+ const encodeUtf8 = utf8TextEncoder ? _encodeUtf8Native : _encodeUtf8Polyfill;
565
+
566
+ /* c8 ignore next */
567
+ let utf8TextDecoder = typeof TextDecoder === 'undefined' ? null : new TextDecoder('utf-8', { fatal: true, ignoreBOM: true });
568
+
569
+ /* c8 ignore start */
570
+ if (utf8TextDecoder && utf8TextDecoder.decode(new Uint8Array()).length === 1) {
571
+ // Safari doesn't handle BOM correctly.
572
+ // This fixes a bug in Safari 13.0.5 where it produces a BOM the first time it is called.
573
+ // utf8TextDecoder.decode(new Uint8Array()).length === 1 on the first call and
574
+ // utf8TextDecoder.decode(new Uint8Array()).length === 1 on the second call
575
+ // Another issue is that from then on no BOM chars are recognized anymore
576
+ /* c8 ignore next */
577
+ utf8TextDecoder = null;
578
+ }
579
+
580
+ /**
581
+ * Efficient schema-less binary encoding with support for variable length encoding.
582
+ *
583
+ * Use [lib0/encoding] with [lib0/decoding]. Every encoding function has a corresponding decoding function.
584
+ *
585
+ * Encodes numbers in little-endian order (least to most significant byte order)
586
+ * and is compatible with Golang's binary encoding (https://golang.org/pkg/encoding/binary/)
587
+ * which is also used in Protocol Buffers.
588
+ *
589
+ * ```js
590
+ * // encoding step
591
+ * const encoder = encoding.createEncoder()
592
+ * encoding.writeVarUint(encoder, 256)
593
+ * encoding.writeVarString(encoder, 'Hello world!')
594
+ * const buf = encoding.toUint8Array(encoder)
595
+ * ```
596
+ *
597
+ * ```js
598
+ * // decoding step
599
+ * const decoder = decoding.createDecoder(buf)
600
+ * decoding.readVarUint(decoder) // => 256
601
+ * decoding.readVarString(decoder) // => 'Hello world!'
602
+ * decoding.hasContent(decoder) // => false - all data is read
603
+ * ```
604
+ *
605
+ * @module encoding
606
+ */
607
+
608
+
609
+ /**
610
+ * A BinaryEncoder handles the encoding to an Uint8Array.
611
+ */
612
+ class Encoder {
613
+ constructor () {
614
+ this.cpos = 0;
615
+ this.cbuf = new Uint8Array(100);
616
+ /**
617
+ * @type {Array<Uint8Array>}
618
+ */
619
+ this.bufs = [];
620
+ }
621
+ }
622
+
623
+ /**
624
+ * The current length of the encoded data.
625
+ *
626
+ * @function
627
+ * @param {Encoder} encoder
628
+ * @return {number}
629
+ */
630
+ const length = encoder => {
631
+ let len = encoder.cpos;
632
+ for (let i = 0; i < encoder.bufs.length; i++) {
633
+ len += encoder.bufs[i].length;
634
+ }
635
+ return len
636
+ };
637
+
638
+ /**
639
+ * Transform to Uint8Array.
640
+ *
641
+ * @function
642
+ * @param {Encoder} encoder
643
+ * @return {Uint8Array} The created ArrayBuffer.
644
+ */
645
+ const toUint8Array = encoder => {
646
+ const uint8arr = new Uint8Array(length(encoder));
647
+ let curPos = 0;
648
+ for (let i = 0; i < encoder.bufs.length; i++) {
649
+ const d = encoder.bufs[i];
650
+ uint8arr.set(d, curPos);
651
+ curPos += d.length;
652
+ }
653
+ uint8arr.set(new Uint8Array(encoder.cbuf.buffer, 0, encoder.cpos), curPos);
654
+ return uint8arr
655
+ };
656
+
657
+ /**
658
+ * Verify that it is possible to write `len` bytes wtihout checking. If
659
+ * necessary, a new Buffer with the required length is attached.
660
+ *
661
+ * @param {Encoder} encoder
662
+ * @param {number} len
663
+ */
664
+ const verifyLen = (encoder, len) => {
665
+ const bufferLen = encoder.cbuf.length;
666
+ if (bufferLen - encoder.cpos < len) {
667
+ encoder.bufs.push(new Uint8Array(encoder.cbuf.buffer, 0, encoder.cpos));
668
+ encoder.cbuf = new Uint8Array(max(bufferLen, len) * 2);
669
+ encoder.cpos = 0;
670
+ }
671
+ };
672
+
673
+ /**
674
+ * Write one byte to the encoder.
675
+ *
676
+ * @function
677
+ * @param {Encoder} encoder
678
+ * @param {number} num The byte that is to be encoded.
679
+ */
680
+ const write = (encoder, num) => {
681
+ const bufferLen = encoder.cbuf.length;
682
+ if (encoder.cpos === bufferLen) {
683
+ encoder.bufs.push(encoder.cbuf);
684
+ encoder.cbuf = new Uint8Array(bufferLen * 2);
685
+ encoder.cpos = 0;
686
+ }
687
+ encoder.cbuf[encoder.cpos++] = num;
688
+ };
689
+
690
+ /**
691
+ * Write a variable length unsigned integer. Max encodable integer is 2^53.
692
+ *
693
+ * @function
694
+ * @param {Encoder} encoder
695
+ * @param {number} num The number that is to be encoded.
696
+ */
697
+ const writeVarUint = (encoder, num) => {
698
+ while (num > BITS7) {
699
+ write(encoder, BIT8 | (BITS7 & num));
700
+ num = floor(num / 128); // shift >>> 7
701
+ }
702
+ write(encoder, BITS7 & num);
703
+ };
704
+
705
+ /**
706
+ * Write a variable length integer.
707
+ *
708
+ * We use the 7th bit instead for signaling that this is a negative number.
709
+ *
710
+ * @function
711
+ * @param {Encoder} encoder
712
+ * @param {number} num The number that is to be encoded.
713
+ */
714
+ const writeVarInt = (encoder, num) => {
715
+ const isNegative = isNegativeZero(num);
716
+ if (isNegative) {
717
+ num = -num;
718
+ }
719
+ // |- whether to continue reading |- whether is negative |- number
720
+ write(encoder, (num > BITS6 ? BIT8 : 0) | (isNegative ? BIT7 : 0) | (BITS6 & num));
721
+ num = floor(num / 64); // shift >>> 6
722
+ // We don't need to consider the case of num === 0 so we can use a different
723
+ // pattern here than above.
724
+ while (num > 0) {
725
+ write(encoder, (num > BITS7 ? BIT8 : 0) | (BITS7 & num));
726
+ num = floor(num / 128); // shift >>> 7
727
+ }
728
+ };
729
+
730
+ /**
731
+ * A cache to store strings temporarily
732
+ */
733
+ const _strBuffer = new Uint8Array(30000);
734
+ const _maxStrBSize = _strBuffer.length / 3;
735
+
736
+ /**
737
+ * Write a variable length string.
738
+ *
739
+ * @function
740
+ * @param {Encoder} encoder
741
+ * @param {String} str The string that is to be encoded.
742
+ */
743
+ const _writeVarStringNative = (encoder, str) => {
744
+ if (str.length < _maxStrBSize) {
745
+ // We can encode the string into the existing buffer
746
+ /* c8 ignore next */
747
+ const written = utf8TextEncoder.encodeInto(str, _strBuffer).written || 0;
748
+ writeVarUint(encoder, written);
749
+ for (let i = 0; i < written; i++) {
750
+ write(encoder, _strBuffer[i]);
751
+ }
752
+ } else {
753
+ writeVarUint8Array(encoder, encodeUtf8(str));
754
+ }
755
+ };
756
+
757
+ /**
758
+ * Write a variable length string.
759
+ *
760
+ * @function
761
+ * @param {Encoder} encoder
762
+ * @param {String} str The string that is to be encoded.
763
+ */
764
+ const _writeVarStringPolyfill = (encoder, str) => {
765
+ const encodedString = unescape(encodeURIComponent(str));
766
+ const len = encodedString.length;
767
+ writeVarUint(encoder, len);
768
+ for (let i = 0; i < len; i++) {
769
+ write(encoder, /** @type {number} */ (encodedString.codePointAt(i)));
770
+ }
771
+ };
772
+
773
+ /**
774
+ * Write a variable length string.
775
+ *
776
+ * @function
777
+ * @param {Encoder} encoder
778
+ * @param {String} str The string that is to be encoded.
779
+ */
780
+ /* c8 ignore next */
781
+ const writeVarString = (utf8TextEncoder && /** @type {any} */ (utf8TextEncoder).encodeInto) ? _writeVarStringNative : _writeVarStringPolyfill;
782
+
783
+ /**
784
+ * Append fixed-length Uint8Array to the encoder.
785
+ *
786
+ * @function
787
+ * @param {Encoder} encoder
788
+ * @param {Uint8Array} uint8Array
789
+ */
790
+ const writeUint8Array = (encoder, uint8Array) => {
791
+ const bufferLen = encoder.cbuf.length;
792
+ const cpos = encoder.cpos;
793
+ const leftCopyLen = min(bufferLen - cpos, uint8Array.length);
794
+ const rightCopyLen = uint8Array.length - leftCopyLen;
795
+ encoder.cbuf.set(uint8Array.subarray(0, leftCopyLen), cpos);
796
+ encoder.cpos += leftCopyLen;
797
+ if (rightCopyLen > 0) {
798
+ // Still something to write, write right half..
799
+ // Append new buffer
800
+ encoder.bufs.push(encoder.cbuf);
801
+ // must have at least size of remaining buffer
802
+ encoder.cbuf = new Uint8Array(max(bufferLen * 2, rightCopyLen));
803
+ // copy array
804
+ encoder.cbuf.set(uint8Array.subarray(leftCopyLen));
805
+ encoder.cpos = rightCopyLen;
806
+ }
807
+ };
808
+
809
+ /**
810
+ * Append an Uint8Array to Encoder.
811
+ *
812
+ * @function
813
+ * @param {Encoder} encoder
814
+ * @param {Uint8Array} uint8Array
815
+ */
816
+ const writeVarUint8Array = (encoder, uint8Array) => {
817
+ writeVarUint(encoder, uint8Array.byteLength);
818
+ writeUint8Array(encoder, uint8Array);
819
+ };
820
+
821
+ /**
822
+ * Create an DataView of the next `len` bytes. Use it to write data after
823
+ * calling this function.
824
+ *
825
+ * ```js
826
+ * // write float32 using DataView
827
+ * const dv = writeOnDataView(encoder, 4)
828
+ * dv.setFloat32(0, 1.1)
829
+ * // read float32 using DataView
830
+ * const dv = readFromDataView(encoder, 4)
831
+ * dv.getFloat32(0) // => 1.100000023841858 (leaving it to the reader to find out why this is the correct result)
832
+ * ```
833
+ *
834
+ * @param {Encoder} encoder
835
+ * @param {number} len
836
+ * @return {DataView}
837
+ */
838
+ const writeOnDataView = (encoder, len) => {
839
+ verifyLen(encoder, len);
840
+ const dview = new DataView(encoder.cbuf.buffer, encoder.cpos, len);
841
+ encoder.cpos += len;
842
+ return dview
843
+ };
844
+
845
+ /**
846
+ * @param {Encoder} encoder
847
+ * @param {number} num
848
+ */
849
+ const writeFloat32 = (encoder, num) => writeOnDataView(encoder, 4).setFloat32(0, num, false);
850
+
851
+ /**
852
+ * @param {Encoder} encoder
853
+ * @param {number} num
854
+ */
855
+ const writeFloat64 = (encoder, num) => writeOnDataView(encoder, 8).setFloat64(0, num, false);
856
+
857
+ /**
858
+ * @param {Encoder} encoder
859
+ * @param {bigint} num
860
+ */
861
+ const writeBigInt64 = (encoder, num) => /** @type {any} */ (writeOnDataView(encoder, 8)).setBigInt64(0, num, false);
862
+
863
+ /**
864
+ * @param {Encoder} encoder
865
+ * @param {bigint} num
866
+ */
867
+ const writeBigUint64 = (encoder, num) => /** @type {any} */ (writeOnDataView(encoder, 8)).setBigUint64(0, num, false);
868
+
869
+ const floatTestBed = new DataView(new ArrayBuffer(4));
870
+ /**
871
+ * Check if a number can be encoded as a 32 bit float.
872
+ *
873
+ * @param {number} num
874
+ * @return {boolean}
875
+ */
876
+ const isFloat32 = num => {
877
+ floatTestBed.setFloat32(0, num);
878
+ return floatTestBed.getFloat32(0) === num
879
+ };
880
+
881
+ /**
882
+ * Encode data with efficient binary format.
883
+ *
884
+ * Differences to JSON:
885
+ * • Transforms data to a binary format (not to a string)
886
+ * • Encodes undefined, NaN, and ArrayBuffer (these can't be represented in JSON)
887
+ * • Numbers are efficiently encoded either as a variable length integer, as a
888
+ * 32 bit float, as a 64 bit float, or as a 64 bit bigint.
889
+ *
890
+ * Encoding table:
891
+ *
892
+ * | Data Type | Prefix | Encoding Method | Comment |
893
+ * | ------------------- | -------- | ------------------ | ------- |
894
+ * | undefined | 127 | | Functions, symbol, and everything that cannot be identified is encoded as undefined |
895
+ * | null | 126 | | |
896
+ * | integer | 125 | writeVarInt | Only encodes 32 bit signed integers |
897
+ * | float32 | 124 | writeFloat32 | |
898
+ * | float64 | 123 | writeFloat64 | |
899
+ * | bigint | 122 | writeBigInt64 | |
900
+ * | boolean (false) | 121 | | True and false are different data types so we save the following byte |
901
+ * | boolean (true) | 120 | | - 0b01111000 so the last bit determines whether true or false |
902
+ * | string | 119 | writeVarString | |
903
+ * | object<string,any> | 118 | custom | Writes {length} then {length} key-value pairs |
904
+ * | array<any> | 117 | custom | Writes {length} then {length} json values |
905
+ * | Uint8Array | 116 | writeVarUint8Array | We use Uint8Array for any kind of binary data |
906
+ *
907
+ * Reasons for the decreasing prefix:
908
+ * We need the first bit for extendability (later we may want to encode the
909
+ * prefix with writeVarUint). The remaining 7 bits are divided as follows:
910
+ * [0-30] the beginning of the data range is used for custom purposes
911
+ * (defined by the function that uses this library)
912
+ * [31-127] the end of the data range is used for data encoding by
913
+ * lib0/encoding.js
914
+ *
915
+ * @param {Encoder} encoder
916
+ * @param {undefined|null|number|bigint|boolean|string|Object<string,any>|Array<any>|Uint8Array} data
917
+ */
918
+ const writeAny = (encoder, data) => {
919
+ switch (typeof data) {
920
+ case 'string':
921
+ // TYPE 119: STRING
922
+ write(encoder, 119);
923
+ writeVarString(encoder, data);
924
+ break
925
+ case 'number':
926
+ if (isInteger(data) && abs(data) <= BITS31) {
927
+ // TYPE 125: INTEGER
928
+ write(encoder, 125);
929
+ writeVarInt(encoder, data);
930
+ } else if (isFloat32(data)) {
931
+ // TYPE 124: FLOAT32
932
+ write(encoder, 124);
933
+ writeFloat32(encoder, data);
934
+ } else {
935
+ // TYPE 123: FLOAT64
936
+ write(encoder, 123);
937
+ writeFloat64(encoder, data);
938
+ }
939
+ break
940
+ case 'bigint':
941
+ // TYPE 122: BigInt
942
+ write(encoder, 122);
943
+ writeBigInt64(encoder, data);
944
+ break
945
+ case 'object':
946
+ if (data === null) {
947
+ // TYPE 126: null
948
+ write(encoder, 126);
949
+ } else if (isArray(data)) {
950
+ // TYPE 117: Array
951
+ write(encoder, 117);
952
+ writeVarUint(encoder, data.length);
953
+ for (let i = 0; i < data.length; i++) {
954
+ writeAny(encoder, data[i]);
955
+ }
956
+ } else if (data instanceof Uint8Array) {
957
+ // TYPE 116: ArrayBuffer
958
+ write(encoder, 116);
959
+ writeVarUint8Array(encoder, data);
960
+ } else {
961
+ // TYPE 118: Object
962
+ write(encoder, 118);
963
+ const keys = Object.keys(data);
964
+ writeVarUint(encoder, keys.length);
965
+ for (let i = 0; i < keys.length; i++) {
966
+ const key = keys[i];
967
+ writeVarString(encoder, key);
968
+ writeAny(encoder, data[key]);
969
+ }
970
+ }
971
+ break
972
+ case 'boolean':
973
+ // TYPE 120/121: boolean (true/false)
974
+ write(encoder, data ? 120 : 121);
975
+ break
976
+ default:
977
+ // TYPE 127: undefined
978
+ write(encoder, 127);
979
+ }
980
+ };
981
+
982
+ function encodeYMessage(msg) {
983
+ const encoder = new Encoder();
984
+ writeVarString(encoder, msg.type);
985
+ writeVarString(encoder, msg.table);
986
+ writeVarString(encoder, msg.prop);
987
+ switch (msg.type) {
988
+ case 'u-ack':
989
+ case 'u-reject':
990
+ writeBigUint64(encoder, BigInt(msg.i));
991
+ break;
992
+ default:
993
+ writeAny(encoder, msg.k);
994
+ switch (msg.type) {
995
+ case 'aware':
996
+ writeVarUint8Array(encoder, msg.u);
997
+ break;
998
+ case 'doc-open':
999
+ writeAny(encoder, msg.serverRev);
1000
+ writeAny(encoder, msg.sv);
1001
+ break;
1002
+ case 'doc-close':
1003
+ break;
1004
+ case 'sv':
1005
+ writeVarUint8Array(encoder, msg.sv);
1006
+ break;
1007
+ case 'u-c':
1008
+ writeVarUint8Array(encoder, msg.u);
1009
+ writeBigUint64(encoder, BigInt(msg.i));
1010
+ break;
1011
+ case 'u-s':
1012
+ writeVarUint8Array(encoder, msg.u);
1013
+ break;
1014
+ }
1015
+ }
1016
+ return toUint8Array(encoder);
1017
+ }
1018
+
1019
+ /**
1020
+ * Error helpers.
1021
+ *
1022
+ * @module error
1023
+ */
1024
+
1025
+ /**
1026
+ * @param {string} s
1027
+ * @return {Error}
1028
+ */
1029
+ /* c8 ignore next */
1030
+ const create = s => new Error(s);
1031
+
1032
+ /**
1033
+ * Efficient schema-less binary decoding with support for variable length encoding.
1034
+ *
1035
+ * Use [lib0/decoding] with [lib0/encoding]. Every encoding function has a corresponding decoding function.
1036
+ *
1037
+ * Encodes numbers in little-endian order (least to most significant byte order)
1038
+ * and is compatible with Golang's binary encoding (https://golang.org/pkg/encoding/binary/)
1039
+ * which is also used in Protocol Buffers.
1040
+ *
1041
+ * ```js
1042
+ * // encoding step
1043
+ * const encoder = encoding.createEncoder()
1044
+ * encoding.writeVarUint(encoder, 256)
1045
+ * encoding.writeVarString(encoder, 'Hello world!')
1046
+ * const buf = encoding.toUint8Array(encoder)
1047
+ * ```
1048
+ *
1049
+ * ```js
1050
+ * // decoding step
1051
+ * const decoder = decoding.createDecoder(buf)
1052
+ * decoding.readVarUint(decoder) // => 256
1053
+ * decoding.readVarString(decoder) // => 'Hello world!'
1054
+ * decoding.hasContent(decoder) // => false - all data is read
1055
+ * ```
1056
+ *
1057
+ * @module decoding
1058
+ */
1059
+
1060
+
1061
+ const errorUnexpectedEndOfArray = create('Unexpected end of array');
1062
+ const errorIntegerOutOfRange = create('Integer out of Range');
1063
+
1064
+ /**
1065
+ * A Decoder handles the decoding of an Uint8Array.
1066
+ */
1067
+ class Decoder {
1068
+ /**
1069
+ * @param {Uint8Array} uint8Array Binary data to decode
1070
+ */
1071
+ constructor (uint8Array) {
1072
+ /**
1073
+ * Decoding target.
1074
+ *
1075
+ * @type {Uint8Array}
1076
+ */
1077
+ this.arr = uint8Array;
1078
+ /**
1079
+ * Current decoding position.
1080
+ *
1081
+ * @type {number}
1082
+ */
1083
+ this.pos = 0;
1084
+ }
1085
+ }
1086
+
1087
+ /**
1088
+ * @function
1089
+ * @param {Decoder} decoder
1090
+ * @return {boolean}
1091
+ */
1092
+ const hasContent = decoder => decoder.pos !== decoder.arr.length;
1093
+
1094
+ /**
1095
+ * Create an Uint8Array view of the next `len` bytes and advance the position by `len`.
1096
+ *
1097
+ * Important: The Uint8Array still points to the underlying ArrayBuffer. Make sure to discard the result as soon as possible to prevent any memory leaks.
1098
+ * Use `buffer.copyUint8Array` to copy the result into a new Uint8Array.
1099
+ *
1100
+ * @function
1101
+ * @param {Decoder} decoder The decoder instance
1102
+ * @param {number} len The length of bytes to read
1103
+ * @return {Uint8Array}
1104
+ */
1105
+ const readUint8Array = (decoder, len) => {
1106
+ const view = new Uint8Array(decoder.arr.buffer, decoder.pos + decoder.arr.byteOffset, len);
1107
+ decoder.pos += len;
1108
+ return view
1109
+ };
1110
+
1111
+ /**
1112
+ * Read variable length Uint8Array.
1113
+ *
1114
+ * Important: The Uint8Array still points to the underlying ArrayBuffer. Make sure to discard the result as soon as possible to prevent any memory leaks.
1115
+ * Use `buffer.copyUint8Array` to copy the result into a new Uint8Array.
1116
+ *
1117
+ * @function
1118
+ * @param {Decoder} decoder
1119
+ * @return {Uint8Array}
1120
+ */
1121
+ const readVarUint8Array = decoder => readUint8Array(decoder, readVarUint(decoder));
1122
+
1123
+ /**
1124
+ * Read one byte as unsigned integer.
1125
+ * @function
1126
+ * @param {Decoder} decoder The decoder instance
1127
+ * @return {number} Unsigned 8-bit integer
1128
+ */
1129
+ const readUint8 = decoder => decoder.arr[decoder.pos++];
1130
+
1131
+ /**
1132
+ * Read unsigned integer (32bit) with variable length.
1133
+ * 1/8th of the storage is used as encoding overhead.
1134
+ * * numbers < 2^7 is stored in one bytlength
1135
+ * * numbers < 2^14 is stored in two bylength
1136
+ *
1137
+ * @function
1138
+ * @param {Decoder} decoder
1139
+ * @return {number} An unsigned integer.length
1140
+ */
1141
+ const readVarUint = decoder => {
1142
+ let num = 0;
1143
+ let mult = 1;
1144
+ const len = decoder.arr.length;
1145
+ while (decoder.pos < len) {
1146
+ const r = decoder.arr[decoder.pos++];
1147
+ // num = num | ((r & binary.BITS7) << len)
1148
+ num = num + (r & BITS7) * mult; // shift $r << (7*#iterations) and add it to num
1149
+ mult *= 128; // next iteration, shift 7 "more" to the left
1150
+ if (r < BIT8) {
1151
+ return num
1152
+ }
1153
+ /* c8 ignore start */
1154
+ if (num > MAX_SAFE_INTEGER) {
1155
+ throw errorIntegerOutOfRange
1156
+ }
1157
+ /* c8 ignore stop */
1158
+ }
1159
+ throw errorUnexpectedEndOfArray
1160
+ };
1161
+
1162
+ /**
1163
+ * Read signed integer (32bit) with variable length.
1164
+ * 1/8th of the storage is used as encoding overhead.
1165
+ * * numbers < 2^7 is stored in one bytlength
1166
+ * * numbers < 2^14 is stored in two bylength
1167
+ * @todo This should probably create the inverse ~num if number is negative - but this would be a breaking change.
1168
+ *
1169
+ * @function
1170
+ * @param {Decoder} decoder
1171
+ * @return {number} An unsigned integer.length
1172
+ */
1173
+ const readVarInt = decoder => {
1174
+ let r = decoder.arr[decoder.pos++];
1175
+ let num = r & BITS6;
1176
+ let mult = 64;
1177
+ const sign = (r & BIT7) > 0 ? -1 : 1;
1178
+ if ((r & BIT8) === 0) {
1179
+ // don't continue reading
1180
+ return sign * num
1181
+ }
1182
+ const len = decoder.arr.length;
1183
+ while (decoder.pos < len) {
1184
+ r = decoder.arr[decoder.pos++];
1185
+ // num = num | ((r & binary.BITS7) << len)
1186
+ num = num + (r & BITS7) * mult;
1187
+ mult *= 128;
1188
+ if (r < BIT8) {
1189
+ return sign * num
1190
+ }
1191
+ /* c8 ignore start */
1192
+ if (num > MAX_SAFE_INTEGER) {
1193
+ throw errorIntegerOutOfRange
1194
+ }
1195
+ /* c8 ignore stop */
1196
+ }
1197
+ throw errorUnexpectedEndOfArray
1198
+ };
1199
+
1200
+ /**
1201
+ * We don't test this function anymore as we use native decoding/encoding by default now.
1202
+ * Better not modify this anymore..
1203
+ *
1204
+ * Transforming utf8 to a string is pretty expensive. The code performs 10x better
1205
+ * when String.fromCodePoint is fed with all characters as arguments.
1206
+ * But most environments have a maximum number of arguments per functions.
1207
+ * For effiency reasons we apply a maximum of 10000 characters at once.
1208
+ *
1209
+ * @function
1210
+ * @param {Decoder} decoder
1211
+ * @return {String} The read String.
1212
+ */
1213
+ /* c8 ignore start */
1214
+ const _readVarStringPolyfill = decoder => {
1215
+ let remainingLen = readVarUint(decoder);
1216
+ if (remainingLen === 0) {
1217
+ return ''
1218
+ } else {
1219
+ let encodedString = String.fromCodePoint(readUint8(decoder)); // remember to decrease remainingLen
1220
+ if (--remainingLen < 100) { // do not create a Uint8Array for small strings
1221
+ while (remainingLen--) {
1222
+ encodedString += String.fromCodePoint(readUint8(decoder));
1223
+ }
1224
+ } else {
1225
+ while (remainingLen > 0) {
1226
+ const nextLen = remainingLen < 10000 ? remainingLen : 10000;
1227
+ // this is dangerous, we create a fresh array view from the existing buffer
1228
+ const bytes = decoder.arr.subarray(decoder.pos, decoder.pos + nextLen);
1229
+ decoder.pos += nextLen;
1230
+ // Starting with ES5.1 we can supply a generic array-like object as arguments
1231
+ encodedString += String.fromCodePoint.apply(null, /** @type {any} */ (bytes));
1232
+ remainingLen -= nextLen;
1233
+ }
1234
+ }
1235
+ return decodeURIComponent(escape(encodedString))
1236
+ }
1237
+ };
1238
+ /* c8 ignore stop */
1239
+
1240
+ /**
1241
+ * @function
1242
+ * @param {Decoder} decoder
1243
+ * @return {String} The read String
1244
+ */
1245
+ const _readVarStringNative = decoder =>
1246
+ /** @type any */ (utf8TextDecoder).decode(readVarUint8Array(decoder));
1247
+
1248
+ /**
1249
+ * Read string of variable length
1250
+ * * varUint is used to store the length of the string
1251
+ *
1252
+ * @function
1253
+ * @param {Decoder} decoder
1254
+ * @return {String} The read String
1255
+ *
1256
+ */
1257
+ /* c8 ignore next */
1258
+ const readVarString = utf8TextDecoder ? _readVarStringNative : _readVarStringPolyfill;
1259
+
1260
+ /**
1261
+ * @param {Decoder} decoder
1262
+ * @param {number} len
1263
+ * @return {DataView}
1264
+ */
1265
+ const readFromDataView = (decoder, len) => {
1266
+ const dv = new DataView(decoder.arr.buffer, decoder.arr.byteOffset + decoder.pos, len);
1267
+ decoder.pos += len;
1268
+ return dv
1269
+ };
1270
+
1271
+ /**
1272
+ * @param {Decoder} decoder
1273
+ */
1274
+ const readFloat32 = decoder => readFromDataView(decoder, 4).getFloat32(0, false);
1275
+
1276
+ /**
1277
+ * @param {Decoder} decoder
1278
+ */
1279
+ const readFloat64 = decoder => readFromDataView(decoder, 8).getFloat64(0, false);
1280
+
1281
+ /**
1282
+ * @param {Decoder} decoder
1283
+ */
1284
+ const readBigInt64 = decoder => /** @type {any} */ (readFromDataView(decoder, 8)).getBigInt64(0, false);
1285
+
1286
+ /**
1287
+ * @param {Decoder} decoder
1288
+ */
1289
+ const readBigUint64 = decoder => /** @type {any} */ (readFromDataView(decoder, 8)).getBigUint64(0, false);
1290
+
1291
+ /**
1292
+ * @type {Array<function(Decoder):any>}
1293
+ */
1294
+ const readAnyLookupTable = [
1295
+ decoder => undefined, // CASE 127: undefined
1296
+ decoder => null, // CASE 126: null
1297
+ readVarInt, // CASE 125: integer
1298
+ readFloat32, // CASE 124: float32
1299
+ readFloat64, // CASE 123: float64
1300
+ readBigInt64, // CASE 122: bigint
1301
+ decoder => false, // CASE 121: boolean (false)
1302
+ decoder => true, // CASE 120: boolean (true)
1303
+ readVarString, // CASE 119: string
1304
+ decoder => { // CASE 118: object<string,any>
1305
+ const len = readVarUint(decoder);
1306
+ /**
1307
+ * @type {Object<string,any>}
1308
+ */
1309
+ const obj = {};
1310
+ for (let i = 0; i < len; i++) {
1311
+ const key = readVarString(decoder);
1312
+ obj[key] = readAny(decoder);
1313
+ }
1314
+ return obj
1315
+ },
1316
+ decoder => { // CASE 117: array<any>
1317
+ const len = readVarUint(decoder);
1318
+ const arr = [];
1319
+ for (let i = 0; i < len; i++) {
1320
+ arr.push(readAny(decoder));
1321
+ }
1322
+ return arr
1323
+ },
1324
+ readVarUint8Array // CASE 116: Uint8Array
1325
+ ];
1326
+
1327
+ /**
1328
+ * @param {Decoder} decoder
1329
+ */
1330
+ const readAny = decoder => readAnyLookupTable[127 - readUint8(decoder)](decoder);
1331
+
1332
+ function decodeYMessage(a) {
1333
+ const decoder = new Decoder(a);
1334
+ const type = readVarString(decoder);
1335
+ const table = readVarString(decoder);
1336
+ const prop = readVarString(decoder);
1337
+ switch (type) {
1338
+ case 'u-ack':
1339
+ case 'u-reject':
1340
+ return {
1341
+ type,
1342
+ table,
1343
+ prop,
1344
+ i: Number(readBigUint64(decoder)),
1345
+ };
1346
+ default: {
1347
+ const k = readAny(decoder);
1348
+ switch (type) {
1349
+ case 'in-sync':
1350
+ return { type, table, prop, k };
1351
+ case 'aware':
1352
+ return {
1353
+ type,
1354
+ table,
1355
+ prop,
1356
+ k,
1357
+ u: readVarUint8Array(decoder),
1358
+ };
1359
+ case 'doc-open':
1360
+ return {
1361
+ type,
1362
+ table,
1363
+ prop,
1364
+ k,
1365
+ serverRev: readAny(decoder),
1366
+ sv: readAny(decoder),
1367
+ };
1368
+ case 'doc-close':
1369
+ return { type, table, prop, k };
1370
+ case 'sv':
1371
+ return {
1372
+ type,
1373
+ table,
1374
+ prop,
1375
+ k,
1376
+ sv: readVarUint8Array(decoder),
1377
+ };
1378
+ case 'u-c':
1379
+ return {
1380
+ type,
1381
+ table,
1382
+ prop,
1383
+ k,
1384
+ u: readVarUint8Array(decoder),
1385
+ i: Number(readBigUint64(decoder)),
1386
+ };
1387
+ case 'u-s':
1388
+ return {
1389
+ type,
1390
+ table,
1391
+ prop,
1392
+ k,
1393
+ u: readVarUint8Array(decoder)
1394
+ };
1395
+ default:
1396
+ throw new TypeError(`Unknown message type: ${type}`);
1397
+ }
1398
+ }
1399
+ }
1400
+ }
1401
+
1402
+ async function asyncIterablePipeline(source, ...stages) {
1403
+ var _a, e_1, _b, _c;
1404
+ // Chain generators by sending outdata from one to another
1405
+ let result = source(); // Start with the source generator
1406
+ for (let i = 0; i < stages.length; i++) {
1407
+ result = stages[i](result); // Pass on the result to next generator
1408
+ }
1409
+ try {
1410
+ // Start running the machine. If the last stage is a sink, it will consume the data and never emit anything
1411
+ // to us here...
1412
+ 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) {
1413
+ _c = result_1_1.value;
1414
+ _d = false;
1415
+ const chunk = _c;
1416
+ }
1417
+ }
1418
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
1419
+ finally {
1420
+ try {
1421
+ if (!_d && !_a && (_b = result_1.return)) await _b.call(result_1);
1422
+ }
1423
+ finally { if (e_1) throw e_1.error; }
1424
+ }
1425
+ }
1426
+
1427
+ function consumeChunkedBinaryStream(source) {
1428
+ return __asyncGenerator(this, arguments, function* consumeChunkedBinaryStream_1() {
1429
+ var _a, e_1, _b, _c;
1430
+ let state = 0;
1431
+ let sizeBuf = new Uint8Array(4);
1432
+ let sizeBufPos = 0;
1433
+ let bufs = [];
1434
+ let len = 0;
1435
+ try {
1436
+ 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) {
1437
+ _c = source_1_1.value;
1438
+ _d = false;
1439
+ const chunk = _c;
1440
+ const dw = new DataView(chunk.buffer, chunk.byteOffset, chunk.byteLength);
1441
+ let pos = 0;
1442
+ while (pos < chunk.byteLength) {
1443
+ switch (state) {
1444
+ case 0:
1445
+ // Beginning of a size header
1446
+ if (pos + 4 > chunk.byteLength) {
1447
+ for (const b of chunk.slice(pos)) {
1448
+ if (sizeBufPos === 4)
1449
+ break;
1450
+ sizeBuf[sizeBufPos++] = b;
1451
+ ++pos;
1452
+ }
1453
+ if (sizeBufPos < 4) {
1454
+ // Need more bytes in order to read length.
1455
+ // Will go out from while loop as well because pos is defenitely = chunk.byteLength here.
1456
+ break;
1457
+ }
1458
+ }
1459
+ else if (sizeBufPos > 0 && sizeBufPos < 4) {
1460
+ for (const b of chunk.slice(pos, pos + 4 - sizeBufPos)) {
1461
+ sizeBuf[sizeBufPos++] = b;
1462
+ ++pos;
1463
+ }
1464
+ }
1465
+ // Intentional fall-through...
1466
+ case 1:
1467
+ len =
1468
+ sizeBufPos === 4
1469
+ ? new DataView(sizeBuf.buffer, 0, 4).getUint32(0, false)
1470
+ : dw.getUint32(pos, false);
1471
+ if (sizeBufPos)
1472
+ sizeBufPos = 0; // in this case pos is already forwarded
1473
+ else
1474
+ pos += 4; // else pos is not yet forwarded - that's why we do it now
1475
+ // Intentional fall-through...
1476
+ case 2:
1477
+ // Eat the chunk
1478
+ if (pos >= chunk.byteLength) {
1479
+ state = 2;
1480
+ break;
1481
+ }
1482
+ if (pos + len > chunk.byteLength) {
1483
+ bufs.push(chunk.slice(pos));
1484
+ len -= (chunk.byteLength - pos);
1485
+ state = 2;
1486
+ pos = chunk.byteLength; // will break while loop.
1487
+ }
1488
+ else {
1489
+ if (bufs.length > 0) {
1490
+ const concats = new Uint8Array(bufs.reduce((p, c) => p + c.byteLength, len));
1491
+ let p = 0;
1492
+ for (const buf of bufs) {
1493
+ concats.set(buf, p);
1494
+ p += buf.byteLength;
1495
+ }
1496
+ concats.set(chunk.slice(pos, pos + len), p);
1497
+ bufs = [];
1498
+ yield yield __await(concats);
1499
+ }
1500
+ else {
1501
+ yield yield __await(chunk.slice(pos, pos + len));
1502
+ }
1503
+ pos += len;
1504
+ state = 0;
1505
+ }
1506
+ break;
1507
+ }
1508
+ }
1509
+ }
1510
+ }
1511
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
1512
+ finally {
1513
+ try {
1514
+ if (!_d && !_a && (_b = source_1.return)) yield __await(_b.call(source_1));
1515
+ }
1516
+ finally { if (e_1) throw e_1.error; }
1517
+ }
1518
+ });
1519
+ }
1520
+
1521
+ function getFetchResponseBodyGenerator(res) {
1522
+ return function () {
1523
+ return __asyncGenerator(this, arguments, function* () {
1524
+ if (!res.body)
1525
+ throw new Error("Response body is not readable");
1526
+ const reader = res.body.getReader();
1527
+ try {
1528
+ while (true) {
1529
+ const { done, value } = yield __await(reader.read());
1530
+ if (done)
1531
+ return yield __await(void 0);
1532
+ yield yield __await(value);
1533
+ }
1534
+ }
1535
+ finally {
1536
+ reader.releaseLock();
1537
+ }
1538
+ });
1539
+ };
1540
+ }
1541
+
473
1542
  function isFunction(value) {
474
1543
  return typeof value === 'function';
475
1544
  }
@@ -3617,7 +4686,7 @@
3617
4686
  }
3618
4687
 
3619
4688
  //import {BisonWebStreamReader} from "dreambase-library/dist/typeson-simplified/BisonWebStreamReader";
3620
- function syncWithServer(changes, syncState, baseRevs, db, databaseUrl, schema, clientIdentity, currentUser) {
4689
+ function syncWithServer(changes, y, syncState, baseRevs, db, databaseUrl, schema, clientIdentity, currentUser) {
3621
4690
  return __awaiter(this, void 0, void 0, function* () {
3622
4691
  //
3623
4692
  // Push changes to server using fetch
@@ -3655,6 +4724,7 @@
3655
4724
  : undefined,
3656
4725
  baseRevs,
3657
4726
  changes: encodeIdsForServer(db.dx.core.schema, currentUser, changes),
4727
+ y,
3658
4728
  };
3659
4729
  console.debug('Sync request', syncRequest);
3660
4730
  db.syncStateChangedEvent.next({
@@ -3868,6 +4938,339 @@
3868
4938
  });
3869
4939
  }
3870
4940
 
4941
+ const DEXIE_CLOUD_SYNCER_ID = 'dexie-cloud-syncer';
4942
+
4943
+ function listUpdatesSince(yTable, sinceIncluding) {
4944
+ return yTable
4945
+ .where('i')
4946
+ .between(sinceIncluding, Infinity, true)
4947
+ .toArray();
4948
+ }
4949
+
4950
+ function $Y(db) {
4951
+ const $Y = db.dx._options.Y;
4952
+ if (!$Y)
4953
+ throw new Error('Y library not supplied to Dexie constructor');
4954
+ return $Y;
4955
+ }
4956
+
4957
+ /** Queries the local database for YMessages to send to server.
4958
+ *
4959
+ * There are 2 messages that this function can provide:
4960
+ * YUpdateFromClientRequest ( for local updates )
4961
+ * YStateVector ( for state vector of foreign updates so that server can reduce the number of udpates to send back )
4962
+ *
4963
+ * Notice that we do not do a step 1 sync phase here to get a state vector from the server. Reason we can avoid
4964
+ * the 2-step sync is that we are client-server and not client-client here and we keep track of the client changes
4965
+ * sent to server by letting server acknowledge them. There is always a chance that some client update has already
4966
+ * been sent and that the client failed to receive the ack. However, if this happens it does not matter - the change
4967
+ * would be sent again and Yjs handles duplicate changes anyway. And it's rare so we earn the cost of roundtrips by
4968
+ * avoiding the step1 sync and instead keep track of this in the `unsentFrom` property of the SyncState.
4969
+ *
4970
+ * @param db
4971
+ * @returns
4972
+ */
4973
+ function listYClientMessagesAndStateVector(db) {
4974
+ var _a;
4975
+ return __awaiter(this, void 0, void 0, function* () {
4976
+ const result = [];
4977
+ const lastUpdateIds = {};
4978
+ for (const table of db.tables) {
4979
+ if (table.schema.yProps && ((_a = db.cloud.schema) === null || _a === void 0 ? void 0 : _a[table.name].markedForSync)) {
4980
+ for (const yProp of table.schema.yProps) {
4981
+ const Y = $Y(db); // This is how we retrieve the user-provided Y library
4982
+ const yTable = db.table(yProp.updatesTable); // the updates-table for this combo of table+propName
4983
+ const syncState = (yield yTable.get(DEXIE_CLOUD_SYNCER_ID));
4984
+ // unsentFrom = the `i` value of updates that aren't yet sent to server (or at least not acked by the server yet)
4985
+ const unsentFrom = (syncState === null || syncState === void 0 ? void 0 : syncState.unsentFrom) || 1;
4986
+ // 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
4987
+ const receivedUntil = (syncState === null || syncState === void 0 ? void 0 : syncState.receivedUntil) || 0;
4988
+ // Compute the least value of these two (but since receivedUntil is inclusive we need to add +1 to it)
4989
+ const unsyncedFrom = Math.min(unsentFrom, receivedUntil + 1);
4990
+ // Query all these updates for all docs of this table+prop combination
4991
+ const updates = yield listUpdatesSince(yTable, unsyncedFrom);
4992
+ if (updates.length > 0)
4993
+ lastUpdateIds[yTable.name] = updates[updates.length - 1].i;
4994
+ // Now sort them by document and whether they are local or not + ignore local updates already sent:
4995
+ const perDoc = {};
4996
+ for (const update of updates) {
4997
+ // Sort updates into buckets of the doc primary key + the flag (whether it's local or foreign)
4998
+ const isLocal = ((update.f || 0) & 0x01) === 0x01;
4999
+ if (isLocal && update.i < unsentFrom)
5000
+ continue; // This local update has already been sent and acked.
5001
+ const docKey = JSON.stringify(update.k) + '/' + isLocal;
5002
+ let entry = perDoc[docKey];
5003
+ if (!entry) {
5004
+ perDoc[docKey] = entry = {
5005
+ i: update.i,
5006
+ k: update.k,
5007
+ isLocal,
5008
+ u: [],
5009
+ };
5010
+ entry.u.push(update.u);
5011
+ }
5012
+ else {
5013
+ entry.u.push(update.u);
5014
+ entry.i = Math.max(update.i, entry.i);
5015
+ }
5016
+ }
5017
+ // Now, go through all these and:
5018
+ // * For local updates, compute a merged update per document.
5019
+ // * For foreign updates, compute a state vector to pass to server, so that server can
5020
+ // avoid re-sending updates that we already have (they might have been sent of websocket
5021
+ // and when that happens, we do not mark them in any way nor do we update receivedUntil -
5022
+ // we only update receivedUntil after a "full sync" (syncWithServer()))
5023
+ for (const { k, isLocal, u, i } of Object.values(perDoc)) {
5024
+ const mergedUpdate = u.length === 1 ? u[0] : Y.mergeUpdatesV2(u);
5025
+ if (isLocal) {
5026
+ result.push({
5027
+ type: 'u-c',
5028
+ table: table.name,
5029
+ prop: yProp.prop,
5030
+ k,
5031
+ u: mergedUpdate,
5032
+ i,
5033
+ });
5034
+ }
5035
+ else {
5036
+ const stateVector = Y.encodeStateVectorFromUpdateV2(mergedUpdate);
5037
+ result.push({
5038
+ type: 'sv',
5039
+ table: table.name,
5040
+ prop: yProp.prop,
5041
+ k,
5042
+ sv: stateVector,
5043
+ });
5044
+ }
5045
+ }
5046
+ }
5047
+ }
5048
+ }
5049
+ return {
5050
+ yMessages: result,
5051
+ lastUpdateIds
5052
+ };
5053
+ });
5054
+ }
5055
+
5056
+ function getUpdatesTable(db, table, ydocProp) {
5057
+ var _a, _b, _c;
5058
+ 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;
5059
+ if (!utbl)
5060
+ throw new Error(`No updatesTable found for ${table}.${ydocProp}`);
5061
+ return db.table(utbl);
5062
+ }
5063
+
5064
+ function applyYServerMessages(yMessages, db) {
5065
+ return __awaiter(this, void 0, void 0, function* () {
5066
+ const result = {};
5067
+ for (const m of yMessages) {
5068
+ switch (m.type) {
5069
+ case 'u-s': {
5070
+ const utbl = getUpdatesTable(db, m.table, m.prop);
5071
+ result[utbl.name] = yield utbl.add({
5072
+ k: m.k,
5073
+ u: m.u,
5074
+ });
5075
+ break;
5076
+ }
5077
+ case 'u-ack': {
5078
+ const utbl = getUpdatesTable(db, m.table, m.prop);
5079
+ yield db.transaction('rw', utbl, (tx) => __awaiter(this, void 0, void 0, function* () {
5080
+ let syncer = (yield tx
5081
+ .table(utbl.name)
5082
+ .get(DEXIE_CLOUD_SYNCER_ID));
5083
+ 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) }));
5084
+ }));
5085
+ break;
5086
+ }
5087
+ case 'u-reject': {
5088
+ // Acces control or constraint rejected the update.
5089
+ // We delete it. It's not going to be sent again.
5090
+ // What's missing is a way to notify consumers, such as Tiptap editor, that the update was rejected.
5091
+ // This is only an issue when the document is open. We could find the open document and
5092
+ // in a perfect world, we should send a reverse update to the open document to undo the change.
5093
+ // See my question in https://discuss.yjs.dev/t/generate-an-inverse-update/2765
5094
+ console.debug(`Y update rejected. Deleting it.`);
5095
+ const utbl = getUpdatesTable(db, m.table, m.prop);
5096
+ yield utbl.delete(m.i);
5097
+ break;
5098
+ }
5099
+ case 'in-sync': {
5100
+ const doc = Dexie.DexieYProvider.getDocCache(db.dx).find(m.table, m.k, m.prop);
5101
+ if (doc && !doc.isSynced) {
5102
+ doc.emit('sync', [true]);
5103
+ }
5104
+ break;
5105
+ }
5106
+ }
5107
+ }
5108
+ return result;
5109
+ });
5110
+ }
5111
+
5112
+ function updateYSyncStates(lastUpdateIdsBeforeSync, receivedUntilsAfterSync, db, serverRevision) {
5113
+ var _a, _b;
5114
+ return __awaiter(this, void 0, void 0, function* () {
5115
+ // We want to update unsentFrom for each yTable to the value specified in first argument
5116
+ // because we got those values before we synced with server and here we are back from server
5117
+ // that has successfully received all those messages - no matter if the last update was a client or server update,
5118
+ // we can safely store unsentFrom to a value of the last update + 1 here.
5119
+ // We also want to update receivedUntil for each yTable to the value specified in the second argument,
5120
+ // because that contains the highest resulted id of each update from server after storing it.
5121
+ // We could do these two tasks separately, but that would require two update calls on the same YSyncState, so
5122
+ // to optimize the dexie calls, we merge these two maps into a single one so we can do a single update request
5123
+ // per yTable.
5124
+ const mergedSpec = {};
5125
+ for (const [yTable, lastUpdateId] of Object.entries(lastUpdateIdsBeforeSync)) {
5126
+ (_a = mergedSpec[yTable]) !== null && _a !== void 0 ? _a : (mergedSpec[yTable] = {});
5127
+ mergedSpec[yTable].unsentFrom = lastUpdateId + 1;
5128
+ }
5129
+ for (const [yTable, lastUpdateId] of Object.entries(receivedUntilsAfterSync)) {
5130
+ (_b = mergedSpec[yTable]) !== null && _b !== void 0 ? _b : (mergedSpec[yTable] = {});
5131
+ mergedSpec[yTable].receivedUntil = lastUpdateId;
5132
+ }
5133
+ // Now go through the merged map and update YSyncStates accordingly:
5134
+ for (const [yTable, { unsentFrom, receivedUntil }] of Object.entries(mergedSpec)) {
5135
+ // We're already in a transaction, but for the sake of
5136
+ // code readability and correctness, let's launch an atomic sub transaction:
5137
+ yield db.transaction('rw', yTable, () => __awaiter(this, void 0, void 0, function* () {
5138
+ const state = yield db.table(yTable).get(DEXIE_CLOUD_SYNCER_ID);
5139
+ if (!state) {
5140
+ yield db.table(yTable).add({
5141
+ i: DEXIE_CLOUD_SYNCER_ID,
5142
+ unsentFrom: unsentFrom || 1,
5143
+ receivedUntil: receivedUntil || 0,
5144
+ serverRev: serverRevision,
5145
+ });
5146
+ }
5147
+ else {
5148
+ if (unsentFrom) {
5149
+ state.unsentFrom = Math.max(unsentFrom, state.unsentFrom || 1);
5150
+ }
5151
+ if (receivedUntil) {
5152
+ state.receivedUntil = Math.max(receivedUntil, state.receivedUntil || 0);
5153
+ state.serverRev = serverRevision;
5154
+ }
5155
+ yield db.table(yTable).put(state);
5156
+ }
5157
+ }));
5158
+ }
5159
+ });
5160
+ }
5161
+
5162
+ const BINSTREAM_TYPE_REALMID = 1;
5163
+ const BINSTREAM_TYPE_TABLE_AND_PROP = 2;
5164
+ const BINSTREAM_TYPE_DOCUMENT = 3;
5165
+ function downloadYDocsFromServer(db, databaseUrl, { yDownloadedRealms, realms }) {
5166
+ return __awaiter(this, void 0, void 0, function* () {
5167
+ if (yDownloadedRealms && realms && realms.every(realmId => yDownloadedRealms[realmId] === '*')) {
5168
+ return; // Already done!
5169
+ }
5170
+ console.debug('Downloading Y.Docs from added realms');
5171
+ const user = yield loadAccessToken(db);
5172
+ const headers = {
5173
+ 'Content-Type': 'application/json',
5174
+ Accept: 'application/octet-stream',
5175
+ };
5176
+ if (user) {
5177
+ headers.Authorization = `Bearer ${user.accessToken}`;
5178
+ }
5179
+ const res = yield fetch(`${databaseUrl}/y/download`, {
5180
+ body: TSON.stringify({ downloadedRealms: yDownloadedRealms || {} }),
5181
+ method: 'POST',
5182
+ headers,
5183
+ credentials: 'include',
5184
+ });
5185
+ if (!res.ok) {
5186
+ throw new Error(`Failed to download Yjs documents from server. Status: ${res.status}`);
5187
+ }
5188
+ yield asyncIterablePipeline(getFetchResponseBodyGenerator(res), consumeChunkedBinaryStream, consumeDownloadChunks);
5189
+ function consumeDownloadChunks(chunks) {
5190
+ return __asyncGenerator(this, arguments, function* consumeDownloadChunks_1() {
5191
+ var _a, e_1, _b, _c;
5192
+ let currentRealmId = null;
5193
+ let currentTable = null;
5194
+ let currentProp = null;
5195
+ let docsToInsert = [];
5196
+ function storeCollectedDocs(completedRealm) {
5197
+ return __awaiter(this, void 0, void 0, function* () {
5198
+ const lastDoc = docsToInsert[docsToInsert.length - 1];
5199
+ if (docsToInsert.length > 0) {
5200
+ if (!currentRealmId || !currentTable || !currentProp) {
5201
+ throw new Error(`Protocol error from ${databaseUrl}/y/download`);
5202
+ }
5203
+ const yTable = getUpdatesTable(db, currentTable, currentProp);
5204
+ yield yTable.bulkAdd(docsToInsert);
5205
+ docsToInsert = [];
5206
+ }
5207
+ if (currentRealmId && currentTable && currentProp && (lastDoc || completedRealm)) {
5208
+ yield db.$syncState.update('syncState', completedRealm
5209
+ ? '*'
5210
+ : {
5211
+ [`yDownloadedRealms.${currentRealmId}`]: {
5212
+ tbl: currentTable,
5213
+ prop: currentProp,
5214
+ key: lastDoc.k,
5215
+ },
5216
+ });
5217
+ }
5218
+ });
5219
+ }
5220
+ try {
5221
+ try {
5222
+ 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) {
5223
+ _c = chunks_1_1.value;
5224
+ _d = false;
5225
+ const chunk = _c;
5226
+ const decoder = new Decoder(chunk);
5227
+ while (hasContent(decoder)) {
5228
+ switch (readUint8(decoder)) {
5229
+ case BINSTREAM_TYPE_REALMID:
5230
+ yield __await(storeCollectedDocs(true));
5231
+ currentRealmId = readVarString(decoder);
5232
+ break;
5233
+ case BINSTREAM_TYPE_TABLE_AND_PROP:
5234
+ yield __await(storeCollectedDocs(false)); // still on same realm
5235
+ currentTable = readVarString(decoder);
5236
+ currentProp = readVarString(decoder);
5237
+ break;
5238
+ case BINSTREAM_TYPE_DOCUMENT: {
5239
+ const k = readAny(decoder);
5240
+ const u = readVarUint8Array(decoder);
5241
+ docsToInsert.push({
5242
+ k,
5243
+ u,
5244
+ });
5245
+ break;
5246
+ }
5247
+ }
5248
+ }
5249
+ yield __await(storeCollectedDocs(false)); // Chunk full - migth still be on same realm
5250
+ }
5251
+ }
5252
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
5253
+ finally {
5254
+ try {
5255
+ if (!_d && !_a && (_b = chunks_1.return)) yield __await(_b.call(chunks_1));
5256
+ }
5257
+ finally { if (e_1) throw e_1.error; }
5258
+ }
5259
+ yield __await(storeCollectedDocs(true)); // Everything downloaded - finalize last downloaded realm to "*"
5260
+ }
5261
+ catch (error) {
5262
+ if (!(error instanceof Dexie.DexieError)) {
5263
+ // Network error might have happened.
5264
+ // Store what we've collected so far:
5265
+ yield __await(storeCollectedDocs(false));
5266
+ }
5267
+ throw error;
5268
+ }
5269
+ });
5270
+ }
5271
+ });
5272
+ }
5273
+
3871
5274
  const CURRENT_SYNC_WORKER = 'currentSyncWorker';
3872
5275
  function sync(db, options, schema, syncOptions) {
3873
5276
  return _sync
@@ -3956,10 +5359,11 @@
3956
5359
  //
3957
5360
  // List changes to sync
3958
5361
  //
3959
- const [clientChangeSet, syncState, baseRevs] = yield db.transaction('r', db.tables, () => __awaiter(this, void 0, void 0, function* () {
5362
+ const [clientChangeSet, syncState, baseRevs, { yMessages, lastUpdateIds }] = yield db.transaction('r', db.tables, () => __awaiter(this, void 0, void 0, function* () {
3960
5363
  const syncState = yield db.getPersistedSyncState();
3961
5364
  const baseRevs = yield db.$baseRevs.toArray();
3962
5365
  let clientChanges = yield listClientChanges(mutationTables);
5366
+ const yResults = yield listYClientMessagesAndStateVector(db);
3963
5367
  throwIfCancelled(cancelToken);
3964
5368
  if (doSyncify) {
3965
5369
  const alreadySyncedRealms = [
@@ -3969,11 +5373,11 @@
3969
5373
  const syncificationInserts = yield listSyncifiedChanges(tablesToSyncify, currentUser, schema, alreadySyncedRealms);
3970
5374
  throwIfCancelled(cancelToken);
3971
5375
  clientChanges = clientChanges.concat(syncificationInserts);
3972
- return [clientChanges, syncState, baseRevs];
5376
+ return [clientChanges, syncState, baseRevs, yResults];
3973
5377
  }
3974
- return [clientChanges, syncState, baseRevs];
5378
+ return [clientChanges, syncState, baseRevs, yResults];
3975
5379
  }));
3976
- const pushSyncIsNeeded = clientChangeSet.some((set) => set.muts.some((mut) => mut.keys.length > 0));
5380
+ const pushSyncIsNeeded = clientChangeSet.some((set) => set.muts.some((mut) => mut.keys.length > 0)) || yMessages.some(m => m.type === 'u-c');
3977
5381
  if (justCheckIfNeeded) {
3978
5382
  console.debug('Sync is needed:', pushSyncIsNeeded);
3979
5383
  return pushSyncIsNeeded;
@@ -3988,12 +5392,12 @@
3988
5392
  // Push changes to server
3989
5393
  //
3990
5394
  throwIfCancelled(cancelToken);
3991
- const res = yield syncWithServer(clientChangeSet, syncState, baseRevs, db, databaseUrl, schema, clientIdentity, currentUser);
5395
+ const res = yield syncWithServer(clientChangeSet, yMessages, syncState, baseRevs, db, databaseUrl, schema, clientIdentity, currentUser);
3992
5396
  console.debug('Sync response', res);
3993
5397
  //
3994
5398
  // Apply changes locally and clear old change entries:
3995
5399
  //
3996
- const done = yield db.transaction('rw', db.tables, (tx) => __awaiter(this, void 0, void 0, function* () {
5400
+ const { done, newSyncState } = yield db.transaction('rw', db.tables, (tx) => __awaiter(this, void 0, void 0, function* () {
3997
5401
  // @ts-ignore
3998
5402
  tx.idbtrans.disableChangeTracking = true;
3999
5403
  // @ts-ignore
@@ -4085,17 +5489,35 @@
4085
5489
  // apply server changes
4086
5490
  //
4087
5491
  yield applyServerChanges(filteredChanges, db);
5492
+ if (res.yMessages) {
5493
+ //
5494
+ // apply yMessages
5495
+ //
5496
+ const receivedUntils = yield applyYServerMessages(res.yMessages, db);
5497
+ //
5498
+ // update Y SyncStates
5499
+ //
5500
+ yield updateYSyncStates(lastUpdateIds, receivedUntils, db, res.serverRevision);
5501
+ }
4088
5502
  //
4089
- // Update syncState
5503
+ // Update regular syncState
4090
5504
  //
4091
5505
  db.$syncState.put(newSyncState, 'syncState');
4092
- return addedClientChanges.length === 0;
5506
+ return {
5507
+ done: addedClientChanges.length === 0,
5508
+ newSyncState
5509
+ };
4093
5510
  }));
4094
5511
  if (!done) {
4095
5512
  console.debug('MORE SYNC NEEDED. Go for it again!');
4096
5513
  yield checkSyncRateLimitDelay(db);
4097
5514
  return yield _sync(db, options, schema, { isInitialSync, cancelToken });
4098
5515
  }
5516
+ const usingYProps = Object.values(schema).some(tbl => { var _a; return (_a = tbl.yProps) === null || _a === void 0 ? void 0 : _a.length; });
5517
+ const serverSupportsYprops = !!res.yMessages;
5518
+ if (usingYProps && serverSupportsYprops) {
5519
+ yield downloadYDocsFromServer(db, databaseUrl, newSyncState);
5520
+ }
4099
5521
  console.debug('SYNC DONE', { isInitialSync });
4100
5522
  db.syncCompleteEvent.next();
4101
5523
  return false; // Not needed anymore
@@ -4148,6 +5570,18 @@
4148
5570
  }
4149
5571
  }
4150
5572
  }
5573
+ if (rejectedRealms.size > 0) {
5574
+ // Remove rejected/deleted realms from yDownloadedRealms because of the following use case:
5575
+ // 1. User becomes added to the realm
5576
+ // 2. User syncs and all documents of the realm is downloaded (downloadYDocsFromServer.ts)
5577
+ // 3. User leaves the realm and all docs are deleted locally (built-in-trigger of deleting their rows in this file)
5578
+ // 4. User is yet again added to the realm. At this point, we must make sure the docs are not considered already downloaded.
5579
+ const updateSpec = {};
5580
+ for (const realmId of rejectedRealms) {
5581
+ updateSpec[`yDownloadedRealms.${realmId}`] = undefined; // Setting to undefined will delete the property
5582
+ }
5583
+ yield db.$syncState.update('syncState', updateSpec);
5584
+ }
4151
5585
  });
4152
5586
  }
4153
5587
  function filterServerChangesThroughAddedClientChanges(serverChanges, addedClientChanges) {
@@ -4159,13 +5593,15 @@
4159
5593
  return toDBOperationSet(changes);
4160
5594
  }
4161
5595
 
5596
+ const LIMIT_NUM_MESSAGES_PER_TIME = 10; // Allow a maximum of 10 messages per...
5597
+ const TIME_WINDOW = 10000; // ...10 seconds.
5598
+ const PAUSE_PERIOD = 1000; // Pause for 1 second if reached
4162
5599
  function MessagesFromServerConsumer(db) {
4163
5600
  const queue = [];
4164
5601
  const readyToServe = new rxjs.BehaviorSubject(true);
4165
5602
  const event = new rxjs.BehaviorSubject(null);
4166
5603
  let isWorking = false;
4167
- let loopWarning = 0;
4168
- let loopDetection = [0, 0, 0, 0, 0, 0, 0, 0, 0, Date.now()];
5604
+ let loopDetection = new Array(LIMIT_NUM_MESSAGES_PER_TIME).fill(0);
4169
5605
  event.subscribe(() => __awaiter(this, void 0, void 0, function* () {
4170
5606
  if (isWorking)
4171
5607
  return;
@@ -4179,20 +5615,11 @@
4179
5615
  }
4180
5616
  finally {
4181
5617
  if (loopDetection[loopDetection.length - 1] - loopDetection[0] <
4182
- 10000) {
5618
+ TIME_WINDOW) {
4183
5619
  // Ten loops within 10 seconds. Slow down!
4184
- if (Date.now() - loopWarning < 5000) {
4185
- // Last time we did this, we ended up here too. Wait for a minute.
4186
- console.warn(`Slowing down websocket loop for one minute`);
4187
- loopWarning = Date.now() + 60000;
4188
- yield new Promise((resolve) => setTimeout(resolve, 60000));
4189
- }
4190
- else {
4191
- // This is a one-time event. Just pause 10 seconds.
4192
- console.warn(`Slowing down websocket loop for 10 seconds`);
4193
- loopWarning = Date.now() + 10000;
4194
- yield new Promise((resolve) => setTimeout(resolve, 10000));
4195
- }
5620
+ // This is a one-time event. Just pause 10 seconds.
5621
+ console.warn(`Slowing down websocket loop for ${PAUSE_PERIOD} milliseconds`);
5622
+ yield new Promise((resolve) => setTimeout(resolve, PAUSE_PERIOD));
4196
5623
  }
4197
5624
  isWorking = false;
4198
5625
  readyToServe.next(true);
@@ -4464,6 +5891,7 @@
4464
5891
  };
4465
5892
  Object.assign(db, helperMethods);
4466
5893
  db.messageConsumer = MessagesFromServerConsumer(db);
5894
+ db.messageProducer = new rxjs.Subject();
4467
5895
  wm.set(dx.cloud, db);
4468
5896
  }
4469
5897
  return db;
@@ -4493,24 +5921,6 @@
4493
5921
  const DISABLE_SERVICEWORKER_STRATEGY = (isSafari && safariVersion <= 605) || // Disable for Safari for now.
4494
5922
  isFirefox; // Disable for Firefox for now. Seems to have a bug in reading CryptoKeys from IDB from service workers
4495
5923
 
4496
- /* Helper function to subscribe to database close no matter if it was unexpectedly closed or manually using db.close()
4497
- */
4498
- function dbOnClosed(db, handler) {
4499
- db.on.close.subscribe(handler);
4500
- // @ts-ignore
4501
- const origClose = db._close;
4502
- // @ts-ignore
4503
- db._close = function () {
4504
- origClose.call(this);
4505
- handler();
4506
- };
4507
- return () => {
4508
- db.on.close.unsubscribe(handler);
4509
- // @ts-ignore
4510
- db._close = origClose;
4511
- };
4512
- }
4513
-
4514
5924
  const IS_SERVICE_WORKER = typeof self !== "undefined" && "clients" in self && !self.document;
4515
5925
 
4516
5926
  function throwVersionIncrementNeeded() {
@@ -4976,13 +6386,18 @@
4976
6386
  values = values.filter((_, idx) => !failures[idx]);
4977
6387
  }
4978
6388
  const ts = Date.now();
6389
+ // Canonicalize req.criteria.index to null if it's on the primary key.
6390
+ const criteria = 'criteria' in req && req.criteria
6391
+ ? Object.assign(Object.assign({}, req.criteria), { index: req.criteria.index === schema.primaryKey.keyPath // Use null to inform server that criteria is on primary key
6392
+ ? null // This will disable the server from trying to log consistent operations where it shouldnt.
6393
+ : req.criteria.index }) : undefined;
4979
6394
  const mut = req.type === 'delete'
4980
6395
  ? {
4981
6396
  type: 'delete',
4982
6397
  ts,
4983
6398
  opNo,
4984
6399
  keys,
4985
- criteria: req.criteria,
6400
+ criteria,
4986
6401
  txid,
4987
6402
  userId,
4988
6403
  }
@@ -4996,14 +6411,14 @@
4996
6411
  userId,
4997
6412
  values,
4998
6413
  }
4999
- : req.criteria && req.changeSpec
6414
+ : criteria && req.changeSpec
5000
6415
  ? {
5001
6416
  // Common changeSpec for all keys
5002
6417
  type: 'modify',
5003
6418
  ts,
5004
6419
  opNo,
5005
6420
  keys,
5006
- criteria: req.criteria,
6421
+ criteria,
5007
6422
  changeSpec: req.changeSpec,
5008
6423
  txid,
5009
6424
  userId,
@@ -5031,7 +6446,7 @@
5031
6446
  if ('isAdditionalChunk' in req && req.isAdditionalChunk) {
5032
6447
  mut.isAdditionalChunk = true;
5033
6448
  }
5034
- return keys.length > 0 || ('criteria' in req && req.criteria)
6449
+ return keys.length > 0 || criteria
5035
6450
  ? mutsTable
5036
6451
  .mutate({ type: 'add', trans, values: [mut] }) // Log entry
5037
6452
  .then(() => res) // Return original response
@@ -5045,6 +6460,7 @@
5045
6460
 
5046
6461
  function overrideParseStoresSpec(origFunc, dexie) {
5047
6462
  return function (stores, dbSchema) {
6463
+ var _a;
5048
6464
  const storesClone = Object.assign(Object.assign({}, DEXIE_CLOUD_SCHEMA), stores);
5049
6465
  // Merge indexes of DEXIE_CLOUD_SCHEMA with stores
5050
6466
  Object.keys(DEXIE_CLOUD_SCHEMA).forEach((tableName) => {
@@ -5105,6 +6521,14 @@
5105
6521
  }
5106
6522
  });
5107
6523
  const rv = origFunc.call(this, storesClone, dbSchema);
6524
+ for (const [tableName, spec] of Object.entries(dbSchema)) {
6525
+ if ((_a = spec.yProps) === null || _a === void 0 ? void 0 : _a.length) {
6526
+ const cloudTableSchema = cloudSchema[tableName];
6527
+ if (cloudTableSchema) {
6528
+ cloudTableSchema.yProps = spec.yProps.map((yProp) => yProp.prop);
6529
+ }
6530
+ }
6531
+ }
5108
6532
  return rv;
5109
6533
  };
5110
6534
  }
@@ -5190,22 +6614,70 @@
5190
6614
  }
5191
6615
  }
5192
6616
 
6617
+ function createYClientUpdateObservable(db) {
6618
+ const yTableRecords = flatten(db.tables
6619
+ .filter((table) => { var _a; return ((_a = db.cloud.schema) === null || _a === void 0 ? void 0 : _a[table.name].markedForSync) && table.schema.yProps; })
6620
+ .map((table) => table.schema.yProps.map((p) => ({
6621
+ table: table.name,
6622
+ ydocProp: p.prop,
6623
+ updatesTable: p.updatesTable,
6624
+ }))));
6625
+ return rxjs.merge(...yTableRecords.map(({ table, ydocProp, updatesTable }) => {
6626
+ let currentUnsentFrom = 1;
6627
+ return Dexie.liveQuery(() => __awaiter(this, void 0, void 0, function* () {
6628
+ const yTbl = db.table(updatesTable);
6629
+ const unsentFrom = yield yTbl
6630
+ .where({ i: DEXIE_CLOUD_SYNCER_ID })
6631
+ .first()
6632
+ .then((syncer) => (syncer === null || syncer === void 0 ? void 0 : syncer.unsentFrom) || 1);
6633
+ currentUnsentFrom = Math.max(currentUnsentFrom, unsentFrom);
6634
+ const addedUpdates = yield listUpdatesSince(yTbl, currentUnsentFrom);
6635
+ // Update currentUnsentFrom to only listen for updates that will be newer than the ones we emitted.
6636
+ currentUnsentFrom = Math.max(currentUnsentFrom, ...addedUpdates.map((update) => update.i + 1));
6637
+ return addedUpdates
6638
+ .filter((update) => update.f && update.f & 1) // Only include local updates
6639
+ .map((update) => {
6640
+ return {
6641
+ type: 'u-c',
6642
+ table,
6643
+ prop: ydocProp,
6644
+ k: update.k,
6645
+ u: update.u,
6646
+ i: update.i,
6647
+ };
6648
+ });
6649
+ }));
6650
+ })).pipe(rxjs.mergeMap((messages) => messages)); // Flattens the array of messages. If messageProducer emits empty array, nothing is emitted but if messageProducer emits array of messages, they are emitted one by one.
6651
+ }
6652
+
6653
+ function getAwarenessLibrary(db) {
6654
+ var _a, _b;
6655
+ if (!((_a = db.cloud.options) === null || _a === void 0 ? void 0 : _a.awarenessProtocol)) {
6656
+ throw new Dexie.MissingAPIError('awarenessProtocol was not provided to db.cloud.configure(). Please import * as awarenessProtocol from "y-protocols/awareness".');
6657
+ }
6658
+ return (_b = db.cloud.options) === null || _b === void 0 ? void 0 : _b.awarenessProtocol;
6659
+ }
6660
+ const awarenessWeakMap = new WeakMap();
6661
+ const getDocAwareness = (doc) => awarenessWeakMap.get(doc);
6662
+
5193
6663
  const SERVER_PING_TIMEOUT = 20000;
5194
6664
  const CLIENT_PING_INTERVAL = 30000;
5195
6665
  const FAIL_RETRY_WAIT_TIME = 60000;
5196
6666
  class WSObservable extends rxjs.Observable {
5197
- constructor(databaseUrl, rev, realmSetHash, clientIdentity, messageProducer, webSocketStatus, token, tokenExpiration) {
5198
- super((subscriber) => new WSConnection(databaseUrl, rev, realmSetHash, clientIdentity, token, tokenExpiration, subscriber, messageProducer, webSocketStatus));
6667
+ constructor(db, rev, realmSetHash, clientIdentity, messageProducer, webSocketStatus, token, tokenExpiration) {
6668
+ super((subscriber) => new WSConnection(db, rev, realmSetHash, clientIdentity, token, tokenExpiration, subscriber, messageProducer, webSocketStatus));
5199
6669
  }
5200
6670
  }
5201
6671
  let counter = 0;
5202
6672
  class WSConnection extends rxjs.Subscription {
5203
- constructor(databaseUrl, rev, realmSetHash, clientIdentity, token, tokenExpiration, subscriber, messageProducer, webSocketStatus) {
6673
+ constructor(db, rev, realmSetHash, clientIdentity, token, tokenExpiration, subscriber, messageProducer, webSocketStatus) {
5204
6674
  super(() => this.teardown());
5205
6675
  this.id = ++counter;
6676
+ this.subscriptions = new Set();
5206
6677
  this.reconnecting = false;
5207
6678
  console.debug('New WebSocket Connection', this.id, token ? 'authorized' : 'unauthorized');
5208
- this.databaseUrl = databaseUrl;
6679
+ this.db = db;
6680
+ this.databaseUrl = db.cloud.options.databaseUrl;
5209
6681
  this.rev = rev;
5210
6682
  this.realmSetHash = realmSetHash;
5211
6683
  this.clientIdentity = clientIdentity;
@@ -5214,7 +6686,6 @@
5214
6686
  this.subscriber = subscriber;
5215
6687
  this.lastUserActivity = new Date();
5216
6688
  this.messageProducer = messageProducer;
5217
- this.messageProducerSubscription = null;
5218
6689
  this.webSocketStatus = webSocketStatus;
5219
6690
  this.connect();
5220
6691
  }
@@ -5235,10 +6706,10 @@
5235
6706
  catch (_a) { }
5236
6707
  }
5237
6708
  this.ws = null;
5238
- if (this.messageProducerSubscription) {
5239
- this.messageProducerSubscription.unsubscribe();
5240
- this.messageProducerSubscription = null;
6709
+ for (const sub of this.subscriptions) {
6710
+ sub.unsubscribe();
5241
6711
  }
6712
+ this.subscriptions.clear();
5242
6713
  }
5243
6714
  reconnect() {
5244
6715
  if (this.reconnecting)
@@ -5332,7 +6803,7 @@
5332
6803
  // Connect the WebSocket to given url:
5333
6804
  console.debug('dexie-cloud WebSocket create');
5334
6805
  const ws = (this.ws = new WebSocket(`${wsUrl}/changes?${searchParams}`));
5335
- //ws.binaryType = "arraybuffer"; // For future when subscribing to actual changes.
6806
+ ws.binaryType = "arraybuffer";
5336
6807
  ws.onclose = (event) => {
5337
6808
  if (!this.pinger)
5338
6809
  return;
@@ -5345,14 +6816,30 @@
5345
6816
  console.debug('dexie-cloud WebSocket onmessage', event.data);
5346
6817
  this.lastServerActivity = new Date();
5347
6818
  try {
5348
- const msg = TSON.parse(event.data);
6819
+ const msg = typeof event.data === 'string'
6820
+ ? TSON.parse(event.data)
6821
+ : decodeYMessage(new Uint8Array(event.data));
5349
6822
  if (msg.type === 'error') {
5350
6823
  throw new Error(`Error message from dexie-cloud: ${msg.error}`);
5351
6824
  }
5352
- if (msg.type === 'rev') {
6825
+ else if (msg.type === 'rev') {
5353
6826
  this.rev = msg.rev; // No meaning but seems reasonable.
5354
6827
  }
5355
- if (msg.type !== 'pong') {
6828
+ else if (msg.type === 'aware') {
6829
+ const docCache = Dexie.DexieYProvider.getDocCache(this.db.dx);
6830
+ const doc = docCache.find(msg.table, msg.k, msg.prop);
6831
+ if (doc) {
6832
+ const awareness = getDocAwareness(doc);
6833
+ if (awareness) {
6834
+ const awap = getAwarenessLibrary(this.db);
6835
+ awap.applyAwarenessUpdate(awareness, msg.u, 'server');
6836
+ }
6837
+ }
6838
+ }
6839
+ else if (msg.type === 'u-ack' || msg.type === 'u-reject' || msg.type === 'u-s' || msg.type === 'in-sync') {
6840
+ applyYServerMessages([msg], this.db);
6841
+ }
6842
+ else if (msg.type !== 'pong') {
5356
6843
  this.subscriber.next(msg);
5357
6844
  }
5358
6845
  }
@@ -5380,16 +6867,24 @@
5380
6867
  }
5381
6868
  };
5382
6869
  });
5383
- this.messageProducerSubscription = this.messageProducer.subscribe((msg) => {
5384
- var _a;
6870
+ this.subscriptions.add(this.messageProducer.subscribe((msg) => {
6871
+ var _a, _b;
5385
6872
  if (!this.closed) {
5386
6873
  if (msg.type === 'ready' &&
5387
6874
  this.webSocketStatus.value !== 'connected') {
5388
6875
  this.webSocketStatus.next('connected');
5389
6876
  }
5390
- (_a = this.ws) === null || _a === void 0 ? void 0 : _a.send(TSON.stringify(msg));
6877
+ if (msg.type === 'ready') {
6878
+ (_a = this.ws) === null || _a === void 0 ? void 0 : _a.send(TSON.stringify(msg));
6879
+ }
6880
+ else {
6881
+ // If it's not a "ready" message, it's an YMessage.
6882
+ // YMessages can be sent binary encoded.
6883
+ (_b = this.ws) === null || _b === void 0 ? void 0 : _b.send(encodeYMessage(msg));
6884
+ }
5391
6885
  }
5392
- });
6886
+ }));
6887
+ this.subscriptions.add(createYClientUpdateObservable(this.db).subscribe(this.db.messageProducer));
5393
6888
  }
5394
6889
  catch (error) {
5395
6890
  this.pauseUntil = new Date(Date.now() + FAIL_RETRY_WAIT_TIME);
@@ -5431,7 +6926,7 @@
5431
6926
  if (!((_a = db.cloud.options) === null || _a === void 0 ? void 0 : _a.databaseUrl)) {
5432
6927
  throw new Error(`No database URL to connect WebSocket to`);
5433
6928
  }
5434
- 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
6929
+ 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
5435
6930
  switchMap(() => db.getPersistedSyncState()), // We need the info on which server revision we are at:
5436
6931
  filter((syncState) => syncState && syncState.serverRevision), // We wont send anything to server before inital sync has taken place
5437
6932
  switchMap((syncState) => __awaiter(this, void 0, void 0, function* () {
@@ -5442,6 +6937,7 @@
5442
6937
  realmSetHash: yield computeRealmSetHash(syncState)
5443
6938
  });
5444
6939
  })));
6940
+ const messageProducer = rxjs.merge(readyForChangesMessage, db.messageProducer);
5445
6941
  function createObservable() {
5446
6942
  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.
5447
6943
  take(1), // Don't continue waking up whenever syncState change
@@ -5468,7 +6964,7 @@
5468
6964
  // If no new entries, server won't bother the client. If new entries, server sends only those
5469
6965
  // and the baseRev of the last from same client-ID.
5470
6966
  if (userLogin) {
5471
- 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);
6967
+ return new WSObservable(db, db.cloud.persistedSyncState.value.serverRevision, realmSetHash, db.cloud.persistedSyncState.value.clientIdentity, messageProducer, db.cloud.webSocketStatus, userLogin.accessToken, userLogin.accessTokenExpiration);
5472
6968
  }
5473
6969
  else {
5474
6970
  return rxjs.from([]);
@@ -6274,6 +7770,128 @@
6274
7770
  })), []);
6275
7771
  });
6276
7772
 
7773
+ function createYHandler(db) {
7774
+ const awap = getAwarenessLibrary(db);
7775
+ return (provider) => {
7776
+ var _a;
7777
+ const doc = provider.doc;
7778
+ const { parentTable, parentId, parentProp, updatesTable } = doc.meta;
7779
+ if (!((_a = db.cloud.schema) === null || _a === void 0 ? void 0 : _a[parentTable].markedForSync)) {
7780
+ return; // The table that holds the doc is not marked for sync - leave it to dexie. No syncing, no awareness.
7781
+ }
7782
+ let awareness = new awap.Awareness(doc);
7783
+ awarenessWeakMap.set(doc, awareness);
7784
+ provider.awareness = awareness;
7785
+ awareness.on('update', ({ added, updated, removed }, origin) => {
7786
+ // Send the update
7787
+ const changedClients = added.concat(updated).concat(removed);
7788
+ if (origin !== 'server') {
7789
+ const update = awap.encodeAwarenessUpdate(awareness, changedClients);
7790
+ db.messageProducer.next({
7791
+ type: 'aware',
7792
+ table: parentTable,
7793
+ prop: parentProp,
7794
+ k: doc.meta.parentId,
7795
+ u: update,
7796
+ });
7797
+ if (provider.destroyed) {
7798
+ // We're called from awareness.on('destroy') that did
7799
+ // removeAwarenessStates.
7800
+ // It's time to also send the doc-close message that dexie-cloud understands
7801
+ // and uses to stop subscribing for updates and awareness updates and brings
7802
+ // down the cached information in memory on the WS connection for this.
7803
+ db.messageProducer.next({
7804
+ type: 'doc-close',
7805
+ table: parentTable,
7806
+ prop: parentProp,
7807
+ k: doc.meta.parentId
7808
+ });
7809
+ }
7810
+ }
7811
+ });
7812
+ awareness.on('destroy', () => {
7813
+ // Signal to server that this provider is destroyed (the update event will be triggered, which
7814
+ // in turn will trigger db.messageProducer that will send the message to the server if WS is connected)
7815
+ awap.removeAwarenessStates(awareness, [doc.clientID], 'provider destroyed');
7816
+ });
7817
+ // Now wait til document is loaded and then open the document on the server
7818
+ provider.on('load', () => __awaiter(this, void 0, void 0, function* () {
7819
+ if (provider.destroyed)
7820
+ return;
7821
+ let connected = false;
7822
+ let currentFlowId = 1;
7823
+ const subscription = db.cloud.webSocketStatus.subscribe((wsStatus) => {
7824
+ if (provider.destroyed)
7825
+ return;
7826
+ // Keep "connected" state in a variable so we can check it after async operations
7827
+ connected = wsStatus === 'connected';
7828
+ // We are or got connected. Open the document on the server.
7829
+ if (wsStatus === "connected") {
7830
+ ++currentFlowId;
7831
+ openDocumentOnServer().catch(error => {
7832
+ console.warn(`Error catched in createYHandler.ts: ${error}`);
7833
+ });
7834
+ }
7835
+ });
7836
+ // Wait until WebSocket is connected
7837
+ provider.addCleanupHandler(subscription);
7838
+ /** Sends an 'doc-open' message to server whenever websocket becomes
7839
+ * connected, or if it is already connected.
7840
+ * The flow is aborted in case websocket is disconnected while querying
7841
+ * information required to compute the state vector. Flow is also
7842
+ * aborted in case document or provider has been destroyed during
7843
+ * the async parts of the task.
7844
+ *
7845
+ * The state vector is only computed from the updates that have occured
7846
+ * after the last full sync - which could very often be zero - in which
7847
+ * case no state vector is sent (then the server already knows us by
7848
+ * revision)
7849
+ *
7850
+ * When server gets the doc-open message, it will authorized us for
7851
+ * whether we are allowed to read / write to this document, and then
7852
+ * keep the cached information in memory on the WS connection for this
7853
+ * particular document, as well as subscribe to updates and awareness updates
7854
+ * from other clients on the document.
7855
+ */
7856
+ function openDocumentOnServer(wsStatus) {
7857
+ return __awaiter(this, void 0, void 0, function* () {
7858
+ const myFlow = currentFlowId; // So we can abort when a new flow is started
7859
+ const yTbl = db.table(updatesTable);
7860
+ const syncState = yield yTbl.get(DEXIE_CLOUD_SYNCER_ID);
7861
+ // After every await, check if we still should be working on this task.
7862
+ if (provider.destroyed || currentFlowId !== myFlow || !connected)
7863
+ return;
7864
+ const receivedUntil = (syncState === null || syncState === void 0 ? void 0 : syncState.receivedUntil) || 0;
7865
+ const docOpenMsg = {
7866
+ type: 'doc-open',
7867
+ table: parentTable,
7868
+ prop: parentProp,
7869
+ k: parentId,
7870
+ serverRev: syncState === null || syncState === void 0 ? void 0 : syncState.serverRev,
7871
+ };
7872
+ const serverUpdatesSinceLastSync = yield yTbl
7873
+ .where('i')
7874
+ .between(receivedUntil, Infinity, false)
7875
+ .filter((update) => Dexie.cmp(update.k, parentId) === 0 && // Only updates for this document
7876
+ ((update.f || 0) & 1) === 0 // Don't include local changes
7877
+ )
7878
+ .toArray();
7879
+ // After every await, check if we still should be working on this task.
7880
+ if (provider.destroyed || currentFlowId !== myFlow || !connected)
7881
+ return;
7882
+ if (serverUpdatesSinceLastSync.length > 0) {
7883
+ const Y = $Y(db); // Get the Yjs library from Dexie constructor options
7884
+ const mergedUpdate = Y.mergeUpdatesV2(serverUpdatesSinceLastSync.map((update) => update.u));
7885
+ const stateVector = Y.encodeStateVectorFromUpdateV2(mergedUpdate);
7886
+ docOpenMsg.sv = stateVector;
7887
+ }
7888
+ db.messageProducer.next(docOpenMsg);
7889
+ });
7890
+ }
7891
+ }));
7892
+ };
7893
+ }
7894
+
6277
7895
  function getTiedRealmId(objectId) {
6278
7896
  return 'rlm~' + objectId;
6279
7897
  }
@@ -6310,8 +7928,9 @@
6310
7928
  if (closed)
6311
7929
  throw new Dexie.DatabaseClosedError();
6312
7930
  }
6313
- dbOnClosed(dexie, () => {
7931
+ dexie.once('close', () => {
6314
7932
  subscriptions.forEach((subscription) => subscription.unsubscribe());
7933
+ subscriptions.splice(0, subscriptions.length);
6315
7934
  closed = true;
6316
7935
  localSyncWorker && localSyncWorker.stop();
6317
7936
  localSyncWorker = null;
@@ -6320,7 +7939,7 @@
6320
7939
  const syncComplete = new rxjs.Subject();
6321
7940
  dexie.cloud = {
6322
7941
  // @ts-ignore
6323
- version: "4.0.7",
7942
+ version: "4.1.0-alpha.2",
6324
7943
  options: Object.assign({}, DEFAULT_OPTIONS),
6325
7944
  schema: null,
6326
7945
  get currentUserId() {
@@ -6466,6 +8085,7 @@
6466
8085
  throw new Error(`Internal error`); // options cannot be null if configuredProgramatically is set.
6467
8086
  const newPersistedOptions = Object.assign({}, options);
6468
8087
  delete newPersistedOptions.fetchTokens;
8088
+ delete newPersistedOptions.awarenessProtocol;
6469
8089
  yield db.$syncState.put(newPersistedOptions, 'options');
6470
8090
  }
6471
8091
  if (((_h = db.cloud.options) === null || _h === void 0 ? void 0 : _h.tryUseServiceWorker) &&
@@ -6543,12 +8163,29 @@
6543
8163
  currentUserEmitter.pipe(skip(1), take(1)),
6544
8164
  db.cloud.persistedSyncState.pipe(skip(1), take(1)),
6545
8165
  ]));
8166
+ const yHandler = createYHandler(db);
8167
+ db.dx.on('y', yHandler);
8168
+ db.dx.once('close', () => {
8169
+ var _a;
8170
+ (_a = db.dx.on.y) === null || _a === void 0 ? void 0 : _a.unsubscribe(yHandler);
8171
+ });
6546
8172
  }
6547
8173
  // HERE: If requireAuth, do athentication now.
6548
8174
  let changedUser = false;
6549
8175
  const user = yield db.getCurrentUser();
6550
- if ((_c = db.cloud.options) === null || _c === void 0 ? void 0 : _c.requireAuth) {
6551
- if (!user.isLoggedIn) {
8176
+ const requireAuth = (_c = db.cloud.options) === null || _c === void 0 ? void 0 : _c.requireAuth;
8177
+ if (requireAuth) {
8178
+ if (typeof requireAuth === 'object') {
8179
+ // requireAuth contains login hints. Check if we already fulfil it:
8180
+ if (!user.isLoggedIn ||
8181
+ (requireAuth.userId && user.userId !== requireAuth.userId) ||
8182
+ (requireAuth.email && user.email !== requireAuth.email)) {
8183
+ // If not, login the configured user:
8184
+ changedUser = yield login(db, requireAuth);
8185
+ }
8186
+ }
8187
+ else if (!user.isLoggedIn) {
8188
+ // requireAuth is true and user is not logged in
6552
8189
  changedUser = yield login(db);
6553
8190
  }
6554
8191
  }
@@ -6604,7 +8241,7 @@
6604
8241
  }
6605
8242
  }
6606
8243
  // @ts-ignore
6607
- dexieCloud.version = "4.0.7";
8244
+ dexieCloud.version = "4.1.0-alpha.2";
6608
8245
  Dexie.Cloud = dexieCloud;
6609
8246
 
6610
8247
  exports.default = dexieCloud;