@syncular/client 0.0.6-102 → 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 +3 -3
- package/src/pull-engine.test.ts +306 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@syncular/client",
|
|
3
|
-
"version": "0.0.6-
|
|
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-
|
|
50
|
-
"@syncular/transport-http": "0.0.6-
|
|
49
|
+
"@syncular/core": "0.0.6-104",
|
|
50
|
+
"@syncular/transport-http": "0.0.6-104"
|
|
51
51
|
},
|
|
52
52
|
"peerDependencies": {
|
|
53
53
|
"kysely": "*"
|
package/src/pull-engine.test.ts
CHANGED
|
@@ -379,4 +379,310 @@ describe('applyPullResponse chunk streaming', () => {
|
|
|
379
379
|
.executeTakeFirst();
|
|
380
380
|
expect(Number(stateAfterFailure?.total ?? 0)).toBe(0);
|
|
381
381
|
});
|
|
382
|
+
|
|
383
|
+
it('uses custom sha256 override for streamed chunk integrity checks', async () => {
|
|
384
|
+
const rows = Array.from({ length: 256 }, (_, index) => ({
|
|
385
|
+
id: `${index + 1}`,
|
|
386
|
+
name: `Item ${index + 1}`,
|
|
387
|
+
}));
|
|
388
|
+
const chunk = new Uint8Array(gzipSync(encodeSnapshotRows(rows)));
|
|
389
|
+
|
|
390
|
+
const transport: SyncTransport = {
|
|
391
|
+
async sync() {
|
|
392
|
+
return {};
|
|
393
|
+
},
|
|
394
|
+
async fetchSnapshotChunk() {
|
|
395
|
+
throw new Error('fetchSnapshotChunk should not be used');
|
|
396
|
+
},
|
|
397
|
+
async fetchSnapshotChunkStream() {
|
|
398
|
+
return createStreamFromBytes(chunk, 137);
|
|
399
|
+
},
|
|
400
|
+
};
|
|
401
|
+
|
|
402
|
+
const handlers: ClientHandlerCollection<TestDb> = [
|
|
403
|
+
createClientHandler({
|
|
404
|
+
table: 'items',
|
|
405
|
+
scopes: ['items:{id}'],
|
|
406
|
+
}),
|
|
407
|
+
];
|
|
408
|
+
|
|
409
|
+
let sha256CallCount = 0;
|
|
410
|
+
const options = {
|
|
411
|
+
clientId: 'client-1',
|
|
412
|
+
subscriptions: [
|
|
413
|
+
{
|
|
414
|
+
id: 'items-sub',
|
|
415
|
+
table: 'items',
|
|
416
|
+
scopes: {},
|
|
417
|
+
},
|
|
418
|
+
],
|
|
419
|
+
stateId: 'default',
|
|
420
|
+
sha256: async () => {
|
|
421
|
+
sha256CallCount += 1;
|
|
422
|
+
return 'expected-hash';
|
|
423
|
+
},
|
|
424
|
+
};
|
|
425
|
+
|
|
426
|
+
const response: SyncPullResponse = {
|
|
427
|
+
ok: true,
|
|
428
|
+
subscriptions: [
|
|
429
|
+
{
|
|
430
|
+
id: 'items-sub',
|
|
431
|
+
status: 'active',
|
|
432
|
+
scopes: {},
|
|
433
|
+
bootstrap: true,
|
|
434
|
+
bootstrapState: null,
|
|
435
|
+
nextCursor: 8,
|
|
436
|
+
commits: [],
|
|
437
|
+
snapshots: [
|
|
438
|
+
{
|
|
439
|
+
table: 'items',
|
|
440
|
+
rows: [],
|
|
441
|
+
chunks: [
|
|
442
|
+
{
|
|
443
|
+
id: 'chunk-1',
|
|
444
|
+
byteLength: chunk.length,
|
|
445
|
+
sha256: 'expected-hash',
|
|
446
|
+
encoding: 'json-row-frame-v1',
|
|
447
|
+
compression: 'gzip',
|
|
448
|
+
},
|
|
449
|
+
],
|
|
450
|
+
isFirstPage: true,
|
|
451
|
+
isLastPage: true,
|
|
452
|
+
},
|
|
453
|
+
],
|
|
454
|
+
},
|
|
455
|
+
],
|
|
456
|
+
};
|
|
457
|
+
|
|
458
|
+
const pullState = await buildPullRequest(db, options);
|
|
459
|
+
await applyPullResponse(
|
|
460
|
+
db,
|
|
461
|
+
transport,
|
|
462
|
+
handlers,
|
|
463
|
+
options,
|
|
464
|
+
pullState,
|
|
465
|
+
response
|
|
466
|
+
);
|
|
467
|
+
|
|
468
|
+
expect(sha256CallCount).toBe(1);
|
|
469
|
+
|
|
470
|
+
const countResult = await sql<{ count: number }>`
|
|
471
|
+
select count(*) as count
|
|
472
|
+
from ${sql.table('items')}
|
|
473
|
+
`.execute(db);
|
|
474
|
+
expect(Number(countResult.rows[0]?.count ?? 0)).toBe(rows.length);
|
|
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
|
+
});
|
|
382
688
|
});
|