@terreno/syncdb 57.14.0 → 57.16.1

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 (36) hide show
  1. package/README.md +6 -5
  2. package/dist/client.d.ts.map +1 -1
  3. package/dist/client.js +200 -80
  4. package/dist/client.js.map +1 -1
  5. package/dist/client.test.js +86 -0
  6. package/dist/client.test.js.map +1 -1
  7. package/dist/persisters/encryptedIndexedDbPersister.d.ts +4 -3
  8. package/dist/persisters/encryptedIndexedDbPersister.d.ts.map +1 -1
  9. package/dist/persisters/encryptedIndexedDbPersister.js +21 -16
  10. package/dist/persisters/encryptedIndexedDbPersister.js.map +1 -1
  11. package/dist/persisters/encryptedIndexedDbPersister.test.js +40 -1
  12. package/dist/persisters/encryptedIndexedDbPersister.test.js.map +1 -1
  13. package/dist/sync/bootstrap.d.ts +34 -3
  14. package/dist/sync/bootstrap.d.ts.map +1 -1
  15. package/dist/sync/bootstrap.js +224 -71
  16. package/dist/sync/bootstrap.js.map +1 -1
  17. package/dist/sync/bootstrap.test.js +210 -2
  18. package/dist/sync/bootstrap.test.js.map +1 -1
  19. package/dist/sync/httpChannel.d.ts +14 -1
  20. package/dist/sync/httpChannel.d.ts.map +1 -1
  21. package/dist/sync/httpChannel.js +34 -0
  22. package/dist/sync/httpChannel.js.map +1 -1
  23. package/dist/sync/httpChannel.test.js +54 -0
  24. package/dist/sync/httpChannel.test.js.map +1 -1
  25. package/dist/types.d.ts +14 -0
  26. package/dist/types.d.ts.map +1 -1
  27. package/package.json +1 -1
  28. package/src/client.test.ts +111 -1
  29. package/src/client.ts +232 -86
  30. package/src/persisters/encryptedIndexedDbPersister.test.ts +44 -1
  31. package/src/persisters/encryptedIndexedDbPersister.ts +24 -17
  32. package/src/sync/bootstrap.test.ts +269 -3
  33. package/src/sync/bootstrap.ts +336 -82
  34. package/src/sync/httpChannel.test.ts +67 -0
  35. package/src/sync/httpChannel.ts +50 -0
  36. package/src/types.ts +12 -0
@@ -1,9 +1,16 @@
1
1
  import {describe, expect, it} from "bun:test";
2
2
 
3
3
  import {createSyncStore, type SyncStore} from "../storage/store";
4
- import type {SyncSnapshotResponse} from "../types";
5
- import {type BootstrapProgress, bootstrapStream} from "./bootstrap";
6
- import {getCursor, markStreamBootstrapped, setCursor, setSnapshotCursor} from "./cursor";
4
+ import type {SyncSnapshotBatchRequest, SyncSnapshotResponse} from "../types";
5
+ import {type BootstrapProgress, bootstrapStream, bootstrapStreamsBatch} from "./bootstrap";
6
+ import {
7
+ getCursor,
8
+ getSnapshotCursor,
9
+ isStreamBootstrapped,
10
+ markStreamBootstrapped,
11
+ setCursor,
12
+ setSnapshotCursor,
13
+ } from "./cursor";
7
14
  import {applyDelta} from "./deltaApplier";
8
15
  import type {FetchSnapshotPageArgs} from "./httpChannel";
9
16
 
@@ -414,6 +421,40 @@ describe("bootstrapStream", () => {
414
421
  expect(second.calls.map((call) => call.cursor)).toEqual([40]);
415
422
  });
416
423
 
424
+ it("writes nothing when superseded while a page was in flight", async () => {
425
+ const store = makeStore();
426
+ const channel = makeChannel({0: page({cursor: 1, entities: entitiesFor(1, 1)})});
427
+ await bootstrapStream({
428
+ channel,
429
+ collection: "todos",
430
+ isSuperseded: () => true,
431
+ store,
432
+ stream: STREAM,
433
+ });
434
+ expect(store.getEntity({collection: "todos", id: "e1"})).toBeUndefined();
435
+ expect(getCursor({store, stream: STREAM})).toBe(0);
436
+ });
437
+
438
+ it("stops mid-page when superseded during a chunk yield", async () => {
439
+ const store = makeStore();
440
+ const channel = makeChannel({0: page({cursor: 4, entities: entitiesFor(1, 4)})});
441
+ let superseded = false;
442
+ await bootstrapStream({
443
+ applyChunkSize: 2,
444
+ channel,
445
+ collection: "todos",
446
+ isSuperseded: () => superseded,
447
+ store,
448
+ stream: STREAM,
449
+ yieldToHost: async () => {
450
+ superseded = true;
451
+ },
452
+ });
453
+ expect(store.getEntity({collection: "todos", id: "e2"})).toBeDefined();
454
+ expect(store.getEntity({collection: "todos", id: "e3"})).toBeUndefined();
455
+ expect(getCursor({store, stream: STREAM})).toBe(0);
456
+ });
457
+
417
458
  it("reports progress per page and forwards the limit", async () => {
418
459
  const store = makeStore();
419
460
  store.upsertEntity({
@@ -484,3 +525,228 @@ describe("bootstrapStream", () => {
484
525
  expect(store.listEntities({collection: "todos"})).toHaveLength(5);
485
526
  });
486
527
  });
528
+
529
+ const NOTES_STREAM = "notes|owner:u1";
530
+
531
+ /** Entities with seqs `from..to` (inclusive) for a snapshot page. */
532
+ const entitiesFor = (from: number, to: number): SyncSnapshotResponse["entities"] =>
533
+ Array.from({length: to - from + 1}, (_unused, index) => ({
534
+ data: {title: `row ${from + index}`},
535
+ deleted: false,
536
+ id: `e${from + index}`,
537
+ seq: from + index,
538
+ }));
539
+
540
+ /**
541
+ * Batch fetch stub: each stream's page comes from `pageFor(stream, cursor)`; every
542
+ * request is recorded along with the order of fetches relative to other events.
543
+ */
544
+ const makeBatchFetch = (
545
+ pageFor: (args: {stream: string; cursor: number; legacyCursor?: string}) => SyncSnapshotResponse,
546
+ events: string[] = []
547
+ ): {
548
+ fetchSnapshotBatch: (
549
+ request: SyncSnapshotBatchRequest
550
+ ) => Promise<{pages: SyncSnapshotResponse[]}>;
551
+ requests: SyncSnapshotBatchRequest[];
552
+ } => {
553
+ const requests: SyncSnapshotBatchRequest[] = [];
554
+ return {
555
+ fetchSnapshotBatch: async (request) => {
556
+ requests.push(request);
557
+ events.push(`fetch:${request.streams.map((item) => item.stream).join(",")}`);
558
+ return {pages: request.streams.map(pageFor)};
559
+ },
560
+ requests,
561
+ };
562
+ };
563
+
564
+ describe("bootstrapStreamsBatch", () => {
565
+ const streams = [
566
+ {collection: "todos", stream: STREAM},
567
+ {collection: "notes", stream: NOTES_STREAM},
568
+ ];
569
+
570
+ it("pages every stream to its head, re-requesting only streams with more", async () => {
571
+ const store = makeStore();
572
+ const {fetchSnapshotBatch, requests} = makeBatchFetch(({stream, cursor}) => {
573
+ if (stream === NOTES_STREAM) {
574
+ return page({cursor: 1, entities: entitiesFor(1, 1), stream});
575
+ }
576
+ return cursor === 0
577
+ ? page({cursor: 2, entities: entitiesFor(1, 2), frontierSeq: 3, hasMore: true})
578
+ : page({cursor: 3, entities: entitiesFor(3, 3)});
579
+ });
580
+
581
+ const completed = await bootstrapStreamsBatch({
582
+ fetchSnapshotBatch,
583
+ limit: 2000,
584
+ store,
585
+ streams,
586
+ });
587
+
588
+ expect(completed).toBe(true);
589
+ expect(requests).toEqual([
590
+ {
591
+ limit: 2000,
592
+ streams: [
593
+ {cursor: 0, legacyCursor: undefined, stream: STREAM},
594
+ {cursor: 0, legacyCursor: undefined, stream: NOTES_STREAM},
595
+ ],
596
+ },
597
+ {limit: 2000, streams: [{cursor: 2, legacyCursor: undefined, stream: STREAM}]},
598
+ ]);
599
+ expect(store.getEntity({collection: "todos", id: "e3"})?.stream).toBe(STREAM);
600
+ expect(store.getEntity({collection: "notes", id: "e1"})?.stream).toBe(NOTES_STREAM);
601
+ expect(getCursor({store, stream: STREAM})).toBe(3);
602
+ expect(getCursor({store, stream: NOTES_STREAM})).toBe(1);
603
+ expect(isStreamBootstrapped({store, stream: STREAM})).toBe(true);
604
+ expect(isStreamBootstrapped({store, stream: NOTES_STREAM})).toBe(true);
605
+ });
606
+
607
+ it("sends the next round before writing the current one", async () => {
608
+ const store = makeStore();
609
+ const events: string[] = [];
610
+ const {fetchSnapshotBatch} = makeBatchFetch(
611
+ ({cursor}) =>
612
+ cursor === 0
613
+ ? page({cursor: 1, entities: entitiesFor(1, 1), frontierSeq: 2, hasMore: true})
614
+ : page({cursor: 2, entities: entitiesFor(2, 2)}),
615
+ events
616
+ );
617
+
618
+ await bootstrapStreamsBatch({
619
+ fetchSnapshotBatch,
620
+ onProgress: (progress) => events.push(`write:${progress.cursor}`),
621
+ store,
622
+ streams: [{collection: "todos", stream: STREAM}],
623
+ });
624
+
625
+ expect(events).toEqual([`fetch:${STREAM}`, `fetch:${STREAM}`, "write:1", "write:2"]);
626
+ });
627
+
628
+ it("writes a large page in chunks, yielding between them, and advances the cursor last", async () => {
629
+ const store = makeStore();
630
+ const {fetchSnapshotBatch} = makeBatchFetch(() =>
631
+ page({cursor: 5, entities: entitiesFor(1, 5)})
632
+ );
633
+ let transactions = 0;
634
+ store.raw.addDidFinishTransactionListener(() => {
635
+ transactions += 1;
636
+ });
637
+ const cursorsAtYield: number[] = [];
638
+
639
+ await bootstrapStreamsBatch({
640
+ applyChunkSize: 2,
641
+ fetchSnapshotBatch,
642
+ store,
643
+ streams: [{collection: "todos", stream: STREAM}],
644
+ yieldToHost: async () => {
645
+ cursorsAtYield.push(getCursor({store, stream: STREAM}));
646
+ },
647
+ });
648
+
649
+ // Chunks of 2, 2, 1: two yields, and the cursor only moves in the final chunk.
650
+ expect(cursorsAtYield).toEqual([0, 0]);
651
+ expect(transactions).toBeGreaterThanOrEqual(3);
652
+ expect(getCursor({store, stream: STREAM})).toBe(5);
653
+ expect(getSnapshotCursor({store, stream: STREAM})).toBe(5);
654
+ expect(store.getEntity({collection: "todos", id: "e5"})?.seq).toBe(5);
655
+ });
656
+
657
+ it("stops without writing when superseded while a round was in flight", async () => {
658
+ const store = makeStore();
659
+ const {fetchSnapshotBatch} = makeBatchFetch(() =>
660
+ page({cursor: 1, entities: entitiesFor(1, 1)})
661
+ );
662
+
663
+ const completed = await bootstrapStreamsBatch({
664
+ fetchSnapshotBatch,
665
+ isSuperseded: () => true,
666
+ store,
667
+ streams: [{collection: "todos", stream: STREAM}],
668
+ });
669
+
670
+ expect(completed).toBe(false);
671
+ expect(store.getEntity({collection: "todos", id: "e1"})).toBeUndefined();
672
+ expect(getCursor({store, stream: STREAM})).toBe(0);
673
+ });
674
+
675
+ it("stops mid-page when superseded during a chunk yield, leaving the cursor unmoved", async () => {
676
+ const store = makeStore();
677
+ const {fetchSnapshotBatch} = makeBatchFetch(() =>
678
+ page({cursor: 5, entities: entitiesFor(1, 5)})
679
+ );
680
+ let superseded = false;
681
+
682
+ const completed = await bootstrapStreamsBatch({
683
+ applyChunkSize: 2,
684
+ fetchSnapshotBatch,
685
+ isSuperseded: () => superseded,
686
+ store,
687
+ streams: [{collection: "todos", stream: STREAM}],
688
+ yieldToHost: async () => {
689
+ // A sign-out or user switch wipes the store while the page is being written.
690
+ superseded = true;
691
+ },
692
+ });
693
+
694
+ expect(completed).toBe(false);
695
+ expect(store.getEntity({collection: "todos", id: "e2"})).toBeDefined();
696
+ expect(store.getEntity({collection: "todos", id: "e3"})).toBeUndefined();
697
+ expect(getCursor({store, stream: STREAM})).toBe(0);
698
+ expect(isStreamBootstrapped({store, stream: STREAM})).toBe(false);
699
+ });
700
+
701
+ it("C7: purges a stream whose cursor fell below the retained floor and re-pages it from 0", async () => {
702
+ const store = makeStore();
703
+ seedBootstrappedStream({seq: 4, store});
704
+ store.upsertEntity({
705
+ collection: "todos",
706
+ data: {title: "stale"},
707
+ id: "gone",
708
+ seq: 2,
709
+ stream: STREAM,
710
+ });
711
+ const {fetchSnapshotBatch, requests} = makeBatchFetch(({cursor}) =>
712
+ cursor === 4
713
+ ? page({cursor: 9, entities: [], oldestRetainedSeq: 6})
714
+ : page({cursor: 9, entities: entitiesFor(9, 9), oldestRetainedSeq: 6})
715
+ );
716
+
717
+ await bootstrapStreamsBatch({
718
+ fetchSnapshotBatch,
719
+ store,
720
+ streams: [{collection: "todos", stream: STREAM}],
721
+ });
722
+
723
+ expect(requests.map((request) => request.streams[0]?.cursor)).toEqual([4, 0]);
724
+ expect(store.getEntity({collection: "todos", id: "gone"})).toBeUndefined();
725
+ expect(store.getEntity({collection: "todos", id: "e9"})?.seq).toBe(9);
726
+ expect(getCursor({store, stream: STREAM})).toBe(9);
727
+ });
728
+
729
+ it("rejects a response that leaves out a requested stream", async () => {
730
+ const store = makeStore();
731
+ await expect(
732
+ bootstrapStreamsBatch({
733
+ fetchSnapshotBatch: async () => ({pages: [page({cursor: 0})]}),
734
+ store,
735
+ streams,
736
+ })
737
+ ).rejects.toThrow(`missing stream ${NOTES_STREAM}`);
738
+ });
739
+
740
+ it("caps each request at the server's 500-stream limit", async () => {
741
+ const store = makeStore();
742
+ const many = Array.from({length: 501}, (_unused, index) => ({
743
+ collection: "todos",
744
+ stream: `todos|owner:u${index}`,
745
+ }));
746
+ const {fetchSnapshotBatch, requests} = makeBatchFetch(({stream}) => page({cursor: 0, stream}));
747
+
748
+ await bootstrapStreamsBatch({fetchSnapshotBatch, store, streams: many});
749
+
750
+ expect(requests.map((request) => request.streams.length)).toEqual([500, 1]);
751
+ });
752
+ });