@syncular/client 0.0.6-103 → 0.0.6-104

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@syncular/client",
3
- "version": "0.0.6-103",
3
+ "version": "0.0.6-104",
4
4
  "description": "Client-side sync engine with offline-first support, outbox, and conflict resolution",
5
5
  "license": "Apache-2.0",
6
6
  "author": "Benjamin Kniffler",
@@ -46,8 +46,8 @@
46
46
  "release": "bunx syncular-publish"
47
47
  },
48
48
  "dependencies": {
49
- "@syncular/core": "0.0.6-103",
50
- "@syncular/transport-http": "0.0.6-103"
49
+ "@syncular/core": "0.0.6-104",
50
+ "@syncular/transport-http": "0.0.6-104"
51
51
  },
52
52
  "peerDependencies": {
53
53
  "kysely": "*"
@@ -473,4 +473,216 @@ describe('applyPullResponse chunk streaming', () => {
473
473
  `.execute(db);
474
474
  expect(Number(countResult.rows[0]?.count ?? 0)).toBe(rows.length);
475
475
  });
476
+
477
+ it('rolls back when a later chunk fails custom sha256 verification', async () => {
478
+ const firstRows = Array.from({ length: 512 }, (_, index) => ({
479
+ id: `${index + 1}`,
480
+ name: `Item ${index + 1}`,
481
+ }));
482
+ const secondRows = Array.from({ length: 512 }, (_, index) => ({
483
+ id: `${index + 513}`,
484
+ name: `Item ${index + 513}`,
485
+ }));
486
+
487
+ const firstChunk = new Uint8Array(gzipSync(encodeSnapshotRows(firstRows)));
488
+ const secondChunk = new Uint8Array(
489
+ gzipSync(encodeSnapshotRows(secondRows))
490
+ );
491
+
492
+ const transport: SyncTransport = {
493
+ async sync() {
494
+ return {};
495
+ },
496
+ async fetchSnapshotChunk() {
497
+ throw new Error('fetchSnapshotChunk should not be used');
498
+ },
499
+ async fetchSnapshotChunkStream({ chunkId }) {
500
+ if (chunkId === 'chunk-1') {
501
+ return createStreamFromBytes(firstChunk, 173);
502
+ }
503
+ if (chunkId === 'chunk-2') {
504
+ return createStreamFromBytes(secondChunk, 173);
505
+ }
506
+ throw new Error(`Unexpected chunk id: ${chunkId}`);
507
+ },
508
+ };
509
+
510
+ const handlers: ClientHandlerCollection<TestDb> = [
511
+ createClientHandler({
512
+ table: 'items',
513
+ scopes: ['items:{id}'],
514
+ }),
515
+ ];
516
+
517
+ let sha256CallCount = 0;
518
+ const options = {
519
+ clientId: 'client-1',
520
+ subscriptions: [
521
+ {
522
+ id: 'items-sub',
523
+ table: 'items',
524
+ scopes: {},
525
+ },
526
+ ],
527
+ stateId: 'default',
528
+ sha256: async () => {
529
+ sha256CallCount += 1;
530
+ return sha256CallCount === 1 ? 'hash-1' : 'bad-hash';
531
+ },
532
+ };
533
+
534
+ const response: SyncPullResponse = {
535
+ ok: true,
536
+ subscriptions: [
537
+ {
538
+ id: 'items-sub',
539
+ status: 'active',
540
+ scopes: {},
541
+ bootstrap: true,
542
+ bootstrapState: null,
543
+ nextCursor: 15,
544
+ commits: [],
545
+ snapshots: [
546
+ {
547
+ table: 'items',
548
+ rows: [],
549
+ chunks: [
550
+ {
551
+ id: 'chunk-1',
552
+ byteLength: firstChunk.length,
553
+ sha256: 'hash-1',
554
+ encoding: 'json-row-frame-v1',
555
+ compression: 'gzip',
556
+ },
557
+ {
558
+ id: 'chunk-2',
559
+ byteLength: secondChunk.length,
560
+ sha256: 'hash-2',
561
+ encoding: 'json-row-frame-v1',
562
+ compression: 'gzip',
563
+ },
564
+ ],
565
+ isFirstPage: true,
566
+ isLastPage: true,
567
+ },
568
+ ],
569
+ },
570
+ ],
571
+ };
572
+
573
+ const pullState = await buildPullRequest(db, options);
574
+ await expect(
575
+ applyPullResponse(db, transport, handlers, options, pullState, response)
576
+ ).rejects.toThrow('Snapshot chunk integrity check failed');
577
+
578
+ expect(sha256CallCount).toBe(2);
579
+
580
+ const countAfterFailure = await sql<{ count: number }>`
581
+ select count(*) as count
582
+ from ${sql.table('items')}
583
+ `.execute(db);
584
+ expect(Number(countAfterFailure.rows[0]?.count ?? 0)).toBe(0);
585
+
586
+ const stateAfterFailure = await db
587
+ .selectFrom('sync_subscription_state')
588
+ .select(({ fn }) => fn.countAll().as('total'))
589
+ .where('state_id', '=', 'default')
590
+ .where('subscription_id', '=', 'items-sub')
591
+ .executeTakeFirst();
592
+ expect(Number(stateAfterFailure?.total ?? 0)).toBe(0);
593
+ });
594
+
595
+ it('does not call custom sha256 override for chunks without hash references', async () => {
596
+ const rows = Array.from({ length: 128 }, (_, index) => ({
597
+ id: `${index + 1}`,
598
+ name: `Item ${index + 1}`,
599
+ }));
600
+ const chunk = new Uint8Array(gzipSync(encodeSnapshotRows(rows)));
601
+
602
+ const transport: SyncTransport = {
603
+ async sync() {
604
+ return {};
605
+ },
606
+ async fetchSnapshotChunk() {
607
+ throw new Error('fetchSnapshotChunk should not be used');
608
+ },
609
+ async fetchSnapshotChunkStream() {
610
+ return createStreamFromBytes(chunk, 89);
611
+ },
612
+ };
613
+
614
+ const handlers: ClientHandlerCollection<TestDb> = [
615
+ createClientHandler({
616
+ table: 'items',
617
+ scopes: ['items:{id}'],
618
+ }),
619
+ ];
620
+
621
+ let sha256CallCount = 0;
622
+ const options = {
623
+ clientId: 'client-1',
624
+ subscriptions: [
625
+ {
626
+ id: 'items-sub',
627
+ table: 'items',
628
+ scopes: {},
629
+ },
630
+ ],
631
+ stateId: 'default',
632
+ sha256: async () => {
633
+ sha256CallCount += 1;
634
+ return 'unused';
635
+ },
636
+ };
637
+
638
+ const response: SyncPullResponse = {
639
+ ok: true,
640
+ subscriptions: [
641
+ {
642
+ id: 'items-sub',
643
+ status: 'active',
644
+ scopes: {},
645
+ bootstrap: true,
646
+ bootstrapState: null,
647
+ nextCursor: 19,
648
+ commits: [],
649
+ snapshots: [
650
+ {
651
+ table: 'items',
652
+ rows: [],
653
+ chunks: [
654
+ {
655
+ id: 'chunk-1',
656
+ byteLength: chunk.length,
657
+ sha256: '',
658
+ encoding: 'json-row-frame-v1',
659
+ compression: 'gzip',
660
+ },
661
+ ],
662
+ isFirstPage: true,
663
+ isLastPage: true,
664
+ },
665
+ ],
666
+ },
667
+ ],
668
+ };
669
+
670
+ const pullState = await buildPullRequest(db, options);
671
+ await applyPullResponse(
672
+ db,
673
+ transport,
674
+ handlers,
675
+ options,
676
+ pullState,
677
+ response
678
+ );
679
+
680
+ expect(sha256CallCount).toBe(0);
681
+
682
+ const countResult = await sql<{ count: number }>`
683
+ select count(*) as count
684
+ from ${sql.table('items')}
685
+ `.execute(db);
686
+ expect(Number(countResult.rows[0]?.count ?? 0)).toBe(rows.length);
687
+ });
476
688
  });