@dxos/feed 0.8.4-main.3eb6e50203 → 0.8.4-main.3fbcb4aa9b

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": "@dxos/feed",
3
- "version": "0.8.4-main.3eb6e50203",
3
+ "version": "0.8.4-main.3fbcb4aa9b",
4
4
  "description": "Append-only replicatable log",
5
5
  "homepage": "https://dxos.org",
6
6
  "bugs": "https://github.com/dxos/dxos/issues",
@@ -25,13 +25,6 @@
25
25
  }
26
26
  },
27
27
  "types": "dist/types/src/index.d.ts",
28
- "typesVersions": {
29
- "*": {
30
- "testing": [
31
- "dist/types/src/testing/index.d.ts"
32
- ]
33
- }
34
- },
35
28
  "files": [
36
29
  "dist",
37
30
  "src"
@@ -39,18 +32,20 @@
39
32
  "dependencies": {
40
33
  "@effect/sql": "0.49.0",
41
34
  "@effect/sql-sqlite-node": "0.50.1",
42
- "@effect/vitest": "0.27.0",
43
- "effect": "3.19.16",
44
- "@dxos/async": "0.8.4-main.3eb6e50203",
45
- "@dxos/context": "0.8.4-main.3eb6e50203",
46
- "@dxos/errors": "0.8.4-main.3eb6e50203",
47
- "@dxos/invariant": "0.8.4-main.3eb6e50203",
48
- "@dxos/log": "0.8.4-main.3eb6e50203",
49
- "@dxos/keys": "0.8.4-main.3eb6e50203",
50
- "@dxos/sql-sqlite": "0.8.4-main.3eb6e50203",
51
- "@dxos/util": "0.8.4-main.3eb6e50203",
52
- "@dxos/protocols": "0.8.4-main.3eb6e50203",
53
- "@dxos/effect": "0.8.4-main.3eb6e50203"
35
+ "effect": "3.20.0",
36
+ "@dxos/context": "0.8.4-main.3fbcb4aa9b",
37
+ "@dxos/async": "0.8.4-main.3fbcb4aa9b",
38
+ "@dxos/errors": "0.8.4-main.3fbcb4aa9b",
39
+ "@dxos/invariant": "0.8.4-main.3fbcb4aa9b",
40
+ "@dxos/keys": "0.8.4-main.3fbcb4aa9b",
41
+ "@dxos/log": "0.8.4-main.3fbcb4aa9b",
42
+ "@dxos/sql-sqlite": "0.8.4-main.3fbcb4aa9b",
43
+ "@dxos/protocols": "0.8.4-main.3fbcb4aa9b",
44
+ "@dxos/util": "0.8.4-main.3fbcb4aa9b",
45
+ "@dxos/effect": "0.8.4-main.3fbcb4aa9b"
46
+ },
47
+ "devDependencies": {
48
+ "@effect/vitest": "0.29.0"
54
49
  },
55
50
  "publishConfig": {
56
51
  "access": "public"
package/src/errors.ts CHANGED
@@ -4,24 +4,6 @@
4
4
 
5
5
  import { BaseError } from '@dxos/errors';
6
6
 
7
- export class DuplicateBlockPositionError extends BaseError.extend('DuplicateBlockPositionError') {
8
- constructor(args: { feedId: string; position: number }) {
9
- super({
10
- message: `Non-unique block position detected (feedId=${args.feedId} position=${args.position}).`,
11
- context: args,
12
- });
13
- }
14
- }
15
-
16
- export class DuplicateBlockLamportTimestampError extends BaseError.extend('DuplicateBlockLamportTimestampError') {
17
- constructor(args: { feedId: string; actorId: string; sequence: number }) {
18
- super({
19
- message: `Non-unique block Lamport timestamp detected (feedId=${args.feedId} actorId=${args.actorId} sequence=${args.sequence}).`,
20
- context: args,
21
- });
22
- }
23
- }
24
-
25
7
  export class PositionConflictError extends BaseError.extend('PositionConflictError') {
26
8
  constructor(args: {
27
9
  feedId?: string;
@@ -12,7 +12,6 @@ import { log } from '@dxos/log';
12
12
  import { FeedProtocol } from '@dxos/protocols';
13
13
  import { SqlTransaction } from '@dxos/sql-sqlite';
14
14
 
15
- import { DuplicateBlockLamportTimestampError, DuplicateBlockPositionError } from './errors';
16
15
  import { FeedStore } from './feed-store';
17
16
 
18
17
  const Block = FeedProtocol.Block;
@@ -313,7 +312,9 @@ describe('Feed V2', () => {
313
312
  // Ideally we use Cause.isDie(cause) -> error
314
313
  // But for quick check:
315
314
  let error = cause.value || cause.defect || cause;
316
- if (cause._tag === 'Die') error = cause.value || cause.defect;
315
+ if (cause._tag === 'Die') {
316
+ error = cause.value || cause.defect;
317
+ }
317
318
 
318
319
  expect(error).toBeDefined();
319
320
  expect(error.message).toBe('Cursor token mismatch');
@@ -521,87 +522,193 @@ describe('Feed V2', () => {
521
522
  }).pipe(Effect.provide(TestLayer)),
522
523
  );
523
524
 
524
- it.effect('append fails on non-unique position in request', () =>
525
+ it.effect('countBlocks and deleteOldestBlocks operate per feed within space and namespace', () =>
526
+ Effect.gen(function* () {
527
+ const feed = new FeedStore({ localActorId: ALICE, assignPositions: true });
528
+ yield* feed.migrate();
529
+
530
+ const spaceA = SpaceId.random();
531
+ const spaceB = SpaceId.random();
532
+ const nsData = WellKnownNamespaces.data;
533
+ const nsTrace = WellKnownNamespaces.trace;
534
+ const targetFeedId = 'feed-target';
535
+
536
+ // Target feed in target namespace/space.
537
+ yield* feed.appendLocal([
538
+ { spaceId: spaceA, feedId: targetFeedId, feedNamespace: nsData, data: new Uint8Array([1]) },
539
+ { spaceId: spaceA, feedId: targetFeedId, feedNamespace: nsData, data: new Uint8Array([2]) },
540
+ { spaceId: spaceA, feedId: targetFeedId, feedNamespace: nsData, data: new Uint8Array([3]) },
541
+ { spaceId: spaceA, feedId: targetFeedId, feedNamespace: nsData, data: new Uint8Array([4]) },
542
+ { spaceId: spaceA, feedId: targetFeedId, feedNamespace: nsData, data: new Uint8Array([5]) },
543
+ ]);
544
+
545
+ // Data that must not be affected by target cleanup.
546
+ yield* feed.appendLocal([
547
+ { spaceId: spaceA, feedId: 'feed-other', feedNamespace: nsData, data: new Uint8Array([11]) },
548
+ { spaceId: spaceA, feedId: 'feed-trace', feedNamespace: nsTrace, data: new Uint8Array([12]) },
549
+ { spaceId: spaceB, feedId: targetFeedId, feedNamespace: nsData, data: new Uint8Array([13]) },
550
+ ]);
551
+
552
+ const beforeCount = yield* feed.countBlocks({
553
+ spaceId: spaceA,
554
+ feedNamespace: nsData,
555
+ feedId: targetFeedId,
556
+ });
557
+ expect(beforeCount).toBe(5);
558
+
559
+ const deleted = yield* feed.deleteOldestBlocks({
560
+ spaceId: spaceA,
561
+ feedNamespace: nsData,
562
+ feedId: targetFeedId,
563
+ count: 3,
564
+ });
565
+ expect(deleted).toBe(3);
566
+
567
+ const afterCount = yield* feed.countBlocks({
568
+ spaceId: spaceA,
569
+ feedNamespace: nsData,
570
+ feedId: targetFeedId,
571
+ });
572
+ expect(afterCount).toBe(2);
573
+
574
+ const targetRemaining = yield* feed.query({
575
+ requestId: 'req-target-remaining',
576
+ spaceId: spaceA,
577
+ feedNamespace: nsData,
578
+ query: { feedIds: [targetFeedId] },
579
+ position: -1,
580
+ });
581
+ expect(targetRemaining.blocks.length).toBe(2);
582
+ expect(targetRemaining.blocks.map((block) => block.data[0])).toEqual([4, 5]);
583
+
584
+ const unaffectedFeed = yield* feed.countBlocks({
585
+ spaceId: spaceA,
586
+ feedNamespace: nsData,
587
+ feedId: 'feed-other',
588
+ });
589
+ expect(unaffectedFeed).toBe(1);
590
+
591
+ const unaffectedNamespace = yield* feed.countBlocks({
592
+ spaceId: spaceA,
593
+ feedNamespace: nsTrace,
594
+ feedId: 'feed-trace',
595
+ });
596
+ expect(unaffectedNamespace).toBe(1);
597
+
598
+ const unaffectedSpace = yield* feed.countBlocks({
599
+ spaceId: spaceB,
600
+ feedNamespace: nsData,
601
+ feedId: targetFeedId,
602
+ });
603
+ expect(unaffectedSpace).toBe(1);
604
+ }).pipe(Effect.provide(TestLayer)),
605
+ );
606
+
607
+ it.effect('deleteOldestBlocks handles non-positive and oversized delete counts', () =>
525
608
  Effect.gen(function* () {
526
609
  const spaceId = SpaceId.random();
527
- const feedId = ObjectId.random();
528
- const feed = new FeedStore({ localActorId: ALICE, assignPositions: false });
610
+ const feedId = 'feed-delete-count';
611
+ const feed = new FeedStore({ localActorId: ALICE, assignPositions: true });
529
612
  yield* feed.migrate();
530
613
 
531
- const error = yield* feed
532
- .append({
533
- requestId: 'req-dup-position',
534
- spaceId,
535
- feedNamespace: WellKnownNamespaces.data,
536
- blocks: [
537
- Block.make({
538
- feedId,
539
- actorId: 'actor-1',
540
- sequence: 1,
541
- prevActorId: null,
542
- prevSequence: null,
543
- position: 10,
544
- timestamp: Date.now(),
545
- data: new Uint8Array([1]),
546
- }),
547
- Block.make({
548
- feedId,
549
- actorId: 'actor-2',
550
- sequence: 1,
551
- prevActorId: null,
552
- prevSequence: null,
553
- position: 10,
554
- timestamp: Date.now(),
555
- data: new Uint8Array([2]),
556
- }),
557
- ],
558
- })
559
- .pipe(Effect.flip);
614
+ yield* feed.appendLocal([
615
+ { spaceId, feedId, feedNamespace: WellKnownNamespaces.data, data: new Uint8Array([1]) },
616
+ { spaceId, feedId, feedNamespace: WellKnownNamespaces.data, data: new Uint8Array([2]) },
617
+ ]);
618
+
619
+ const deletedZero = yield* feed.deleteOldestBlocks({
620
+ spaceId,
621
+ feedNamespace: WellKnownNamespaces.data,
622
+ feedId,
623
+ count: 0,
624
+ });
625
+ expect(deletedZero).toBe(0);
560
626
 
561
- expect(error).toBeInstanceOf(DuplicateBlockPositionError);
562
- expect(error.message).toContain('Non-unique block position detected');
627
+ const deletedNegative = yield* feed.deleteOldestBlocks({
628
+ spaceId,
629
+ feedNamespace: WellKnownNamespaces.data,
630
+ feedId,
631
+ count: -5,
632
+ });
633
+ expect(deletedNegative).toBe(0);
634
+
635
+ const deletedOversized = yield* feed.deleteOldestBlocks({
636
+ spaceId,
637
+ feedNamespace: WellKnownNamespaces.data,
638
+ feedId,
639
+ count: 10,
640
+ });
641
+ expect(deletedOversized).toBe(2);
642
+
643
+ const remainingCount = yield* feed.countBlocks({
644
+ spaceId,
645
+ feedNamespace: WellKnownNamespaces.data,
646
+ feedId,
647
+ });
648
+ expect(remainingCount).toBe(0);
563
649
  }).pipe(Effect.provide(TestLayer)),
564
650
  );
565
651
 
566
- it.effect('append fails on non-unique Lamport timestamp in request', () =>
652
+ it.effect('append ignores duplicate Lamport timestamp and preserves original data', () =>
567
653
  Effect.gen(function* () {
568
654
  const spaceId = SpaceId.random();
569
655
  const feedId = ObjectId.random();
570
656
  const feed = new FeedStore({ localActorId: ALICE, assignPositions: false });
571
657
  yield* feed.migrate();
572
658
 
573
- const error = yield* feed
574
- .append({
575
- requestId: 'req-dup-lamport',
576
- spaceId,
577
- feedNamespace: WellKnownNamespaces.data,
578
- blocks: [
579
- Block.make({
580
- feedId,
581
- actorId: 'actor-1',
582
- sequence: 5,
583
- prevActorId: null,
584
- prevSequence: null,
585
- position: 1,
586
- timestamp: Date.now(),
587
- data: new Uint8Array([1]),
588
- }),
589
- Block.make({
590
- feedId,
591
- actorId: 'actor-1',
592
- sequence: 5,
593
- prevActorId: null,
594
- prevSequence: null,
595
- position: 2,
596
- timestamp: Date.now(),
597
- data: new Uint8Array([2]),
598
- }),
599
- ],
600
- })
601
- .pipe(Effect.flip);
659
+ const actorId = 'actor-1';
660
+ const sequence = 5;
661
+ const timestamp = Date.now();
662
+
663
+ yield* feed.append({
664
+ requestId: 'req-first',
665
+ spaceId,
666
+ feedNamespace: WellKnownNamespaces.data,
667
+ blocks: [
668
+ Block.make({
669
+ feedId,
670
+ actorId,
671
+ sequence,
672
+ prevActorId: null,
673
+ prevSequence: null,
674
+ position: null,
675
+ timestamp,
676
+ data: new Uint8Array([1]),
677
+ }),
678
+ ],
679
+ });
680
+
681
+ // Same Lamport tuple with different data and position should be ignored.
682
+ yield* feed.append({
683
+ requestId: 'req-duplicate',
684
+ spaceId,
685
+ feedNamespace: WellKnownNamespaces.data,
686
+ blocks: [
687
+ Block.make({
688
+ feedId,
689
+ actorId,
690
+ sequence,
691
+ prevActorId: null,
692
+ prevSequence: null,
693
+ position: null,
694
+ timestamp: timestamp + 1,
695
+ data: new Uint8Array([2]),
696
+ }),
697
+ ],
698
+ });
699
+
700
+ const queryRes = yield* feed.query({
701
+ requestId: 'req-query',
702
+ spaceId,
703
+ feedNamespace: WellKnownNamespaces.data,
704
+ query: { feedIds: [feedId] },
705
+ position: -1,
706
+ });
602
707
 
603
- expect(error).toBeInstanceOf(DuplicateBlockLamportTimestampError);
604
- expect(error.message).toContain('Non-unique block Lamport timestamp detected');
708
+ expect(queryRes.blocks.length).toBe(1);
709
+ expect(queryRes.blocks[0].actorId).toBe(actorId);
710
+ expect(queryRes.blocks[0].sequence).toBe(sequence);
711
+ expect(queryRes.blocks[0].data).toEqual(new Uint8Array([1]));
605
712
  }).pipe(Effect.provide(TestLayer)),
606
713
  );
607
714
  });