@dxos/feed 0.8.4-main.ef1bc66f44 → 0.8.4-main.fcfe5033a5

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.ef1bc66f44",
3
+ "version": "0.8.4-main.fcfe5033a5",
4
4
  "description": "Append-only replicatable log",
5
5
  "homepage": "https://dxos.org",
6
6
  "bugs": "https://github.com/dxos/dxos/issues",
@@ -37,20 +37,22 @@
37
37
  "src"
38
38
  ],
39
39
  "dependencies": {
40
- "@dxos/errors": "^0.8.3",
41
40
  "@effect/sql": "0.49.0",
42
41
  "@effect/sql-sqlite-node": "0.50.1",
43
- "@effect/vitest": "0.27.0",
44
- "effect": "3.19.16",
45
- "@dxos/async": "0.8.4-main.ef1bc66f44",
46
- "@dxos/invariant": "0.8.4-main.ef1bc66f44",
47
- "@dxos/keys": "0.8.4-main.ef1bc66f44",
48
- "@dxos/context": "0.8.4-main.ef1bc66f44",
49
- "@dxos/log": "0.8.4-main.ef1bc66f44",
50
- "@dxos/protocols": "0.8.4-main.ef1bc66f44",
51
- "@dxos/util": "0.8.4-main.ef1bc66f44",
52
- "@dxos/sql-sqlite": "0.8.4-main.ef1bc66f44",
53
- "@dxos/effect": "0.8.4-main.ef1bc66f44"
42
+ "effect": "3.20.0",
43
+ "@dxos/async": "0.8.4-main.fcfe5033a5",
44
+ "@dxos/effect": "0.8.4-main.fcfe5033a5",
45
+ "@dxos/context": "0.8.4-main.fcfe5033a5",
46
+ "@dxos/errors": "0.8.4-main.fcfe5033a5",
47
+ "@dxos/keys": "0.8.4-main.fcfe5033a5",
48
+ "@dxos/log": "0.8.4-main.fcfe5033a5",
49
+ "@dxos/protocols": "0.8.4-main.fcfe5033a5",
50
+ "@dxos/sql-sqlite": "0.8.4-main.fcfe5033a5",
51
+ "@dxos/util": "0.8.4-main.fcfe5033a5",
52
+ "@dxos/invariant": "0.8.4-main.fcfe5033a5"
53
+ },
54
+ "devDependencies": {
55
+ "@effect/vitest": "0.27.0"
54
56
  },
55
57
  "publishConfig": {
56
58
  "access": "public"
package/src/errors.ts CHANGED
@@ -4,20 +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(`Non-unique block position detected (feedId=${args.feedId} position=${args.position}).`);
10
- }
11
- }
12
-
13
- export class DuplicateBlockLamportTimestampError extends BaseError.extend('DuplicateBlockLamportTimestampError') {
14
- constructor(args: { feedId: string; actorId: string; sequence: number }) {
15
- super(
16
- `Non-unique block Lamport timestamp detected (feedId=${args.feedId} actorId=${args.actorId} sequence=${args.sequence}).`,
17
- );
18
- }
19
- }
20
-
21
7
  export class PositionConflictError extends BaseError.extend('PositionConflictError') {
22
8
  constructor(args: {
23
9
  feedId?: string;
@@ -26,8 +12,9 @@ export class PositionConflictError extends BaseError.extend('PositionConflictErr
26
12
  currentPosition: number;
27
13
  requestedPosition: number | null;
28
14
  }) {
29
- super(
30
- `Block already has position ${args.currentPosition}, cannot set to ${args.requestedPosition} (feedId=${args.feedId} actorId=${args.actorId} sequence=${args.sequence}).`,
31
- );
15
+ super({
16
+ message: `Block already has position ${args.currentPosition}, cannot set to ${args.requestedPosition} (feedId=${args.feedId} actorId=${args.actorId} sequence=${args.sequence}).`,
17
+ context: args,
18
+ });
32
19
  }
33
20
  }
@@ -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;
@@ -359,7 +358,8 @@ describe('Feed V2', () => {
359
358
  },
360
359
  ]);
361
360
  expect(blocks.length).toBe(1);
362
- expect(blocks[0].position).toBeNull();
361
+ expect(blocks[0].position).toBeDefined();
362
+ expect(blocks[0].position).toBeGreaterThanOrEqual(0);
363
363
  expect(blocks[0].sequence).toBe(0);
364
364
  expect(blocks[0].actorId).toBe(ALICE);
365
365
  expect(blocks[0].prevActorId).toBeNull();
@@ -367,16 +367,35 @@ describe('Feed V2', () => {
367
367
  expect(blocks[0].timestamp).toBeGreaterThan(0);
368
368
  expect(blocks[0].data).toEqual(new Uint8Array([1]));
369
369
 
370
- // Query by feedId
370
+ // Query by feedId: persisted position matches returned block position.
371
371
  const queryRes = yield* feed.query({
372
372
  query: { feedIds: [feedId] },
373
373
  position: -1,
374
374
  spaceId,
375
375
  feedNamespace: WellKnownNamespaces.data,
376
- }); // Use position '-1' to get everything
377
- expect(queryRes.blocks.length).toBe(1);
376
+ });
378
377
  expect(queryRes.blocks.length).toBe(1);
379
- expect(queryRes.blocks[0]).toMatchObject({ ...blocks[0], feedId, position: expect.any(Number) });
378
+ expect(queryRes.blocks[0].position).toBe(blocks[0].position);
379
+ expect(queryRes.blocks[0]).toMatchObject({ ...blocks[0], feedId });
380
+ }).pipe(Effect.provide(TestLayer)),
381
+ );
382
+
383
+ it.effect('appendLocal returns blocks with positions from append when assignPositions is true', () =>
384
+ Effect.gen(function* () {
385
+ const spaceId = SpaceId.random();
386
+ const feed = new FeedStore({ localActorId: ALICE, assignPositions: true });
387
+ yield* feed.migrate();
388
+
389
+ const blocks = yield* feed.appendLocal([
390
+ { spaceId, feedId: 'feed-a', feedNamespace: WellKnownNamespaces.data, data: new Uint8Array([1]) },
391
+ { spaceId, feedId: 'feed-b', feedNamespace: WellKnownNamespaces.data, data: new Uint8Array([2]) },
392
+ { spaceId, feedId: 'feed-a', feedNamespace: WellKnownNamespaces.data, data: new Uint8Array([3]) },
393
+ ]);
394
+
395
+ expect(blocks.length).toBe(3);
396
+ expect(blocks.every((block) => block.position != null && block.position >= 0)).toBe(true);
397
+ expect(blocks[0].position).toBeLessThan(blocks[1].position!);
398
+ expect(blocks[1].position).toBeLessThan(blocks[2].position!);
380
399
  }).pipe(Effect.provide(TestLayer)),
381
400
  );
382
401
 
@@ -390,13 +409,6 @@ describe('Feed V2', () => {
390
409
 
391
410
  const nsData = WellKnownNamespaces.data;
392
411
  const nsTrace = WellKnownNamespaces.trace;
393
- const getPersistedPosition = (spaceId: SpaceId, feedNamespace: string, feedId: string) =>
394
- feed.query({
395
- query: { feedIds: [feedId] },
396
- position: -1,
397
- spaceId,
398
- feedNamespace,
399
- });
400
412
 
401
413
  // First append in each (spaceId, feedNamespace) pair starts at 0.
402
414
  const feedAData = ObjectId.random();
@@ -447,23 +459,12 @@ describe('Feed V2', () => {
447
459
  },
448
460
  ]);
449
461
 
450
- expect(firstAData[0].position).toBeNull();
451
- expect(firstATrace[0].position).toBeNull();
452
- expect(firstBData[0].position).toBeNull();
453
- expect(firstBHalo[0].position).toBeNull();
454
- expect(secondAData[0].position).toBeNull();
455
-
456
- const posAData = yield* getPersistedPosition(spaceA, nsData, feedAData);
457
- const posATrace = yield* getPersistedPosition(spaceA, nsTrace, feedATrace);
458
- const posBData = yield* getPersistedPosition(spaceB, nsData, feedBData);
459
- const posBTrace = yield* getPersistedPosition(spaceB, nsTrace, feedBTrace);
460
- const posAData2 = yield* getPersistedPosition(spaceA, nsData, feedAData2);
461
-
462
- expect(posAData.blocks[0].position).toBe(0);
463
- expect(posATrace.blocks[0].position).toBe(0);
464
- expect(posBData.blocks[0].position).toBe(0);
465
- expect(posBTrace.blocks[0].position).toBe(0);
466
- expect(posAData2.blocks[0].position).toBe(1);
462
+ // appendLocal returns blocks with positions matching persisted values.
463
+ expect(firstAData[0].position).toBe(0);
464
+ expect(firstATrace[0].position).toBe(0);
465
+ expect(firstBData[0].position).toBe(0);
466
+ expect(firstBHalo[0].position).toBe(0);
467
+ expect(secondAData[0].position).toBe(1);
467
468
  }).pipe(Effect.provide(TestLayer)),
468
469
  );
469
470
 
@@ -519,87 +520,193 @@ describe('Feed V2', () => {
519
520
  }).pipe(Effect.provide(TestLayer)),
520
521
  );
521
522
 
522
- it.effect('append fails on non-unique position in request', () =>
523
+ it.effect('countBlocks and deleteOldestBlocks operate per feed within space and namespace', () =>
524
+ Effect.gen(function* () {
525
+ const feed = new FeedStore({ localActorId: ALICE, assignPositions: true });
526
+ yield* feed.migrate();
527
+
528
+ const spaceA = SpaceId.random();
529
+ const spaceB = SpaceId.random();
530
+ const nsData = WellKnownNamespaces.data;
531
+ const nsTrace = WellKnownNamespaces.trace;
532
+ const targetFeedId = 'feed-target';
533
+
534
+ // Target feed in target namespace/space.
535
+ yield* feed.appendLocal([
536
+ { spaceId: spaceA, feedId: targetFeedId, feedNamespace: nsData, data: new Uint8Array([1]) },
537
+ { spaceId: spaceA, feedId: targetFeedId, feedNamespace: nsData, data: new Uint8Array([2]) },
538
+ { spaceId: spaceA, feedId: targetFeedId, feedNamespace: nsData, data: new Uint8Array([3]) },
539
+ { spaceId: spaceA, feedId: targetFeedId, feedNamespace: nsData, data: new Uint8Array([4]) },
540
+ { spaceId: spaceA, feedId: targetFeedId, feedNamespace: nsData, data: new Uint8Array([5]) },
541
+ ]);
542
+
543
+ // Data that must not be affected by target cleanup.
544
+ yield* feed.appendLocal([
545
+ { spaceId: spaceA, feedId: 'feed-other', feedNamespace: nsData, data: new Uint8Array([11]) },
546
+ { spaceId: spaceA, feedId: 'feed-trace', feedNamespace: nsTrace, data: new Uint8Array([12]) },
547
+ { spaceId: spaceB, feedId: targetFeedId, feedNamespace: nsData, data: new Uint8Array([13]) },
548
+ ]);
549
+
550
+ const beforeCount = yield* feed.countBlocks({
551
+ spaceId: spaceA,
552
+ feedNamespace: nsData,
553
+ feedId: targetFeedId,
554
+ });
555
+ expect(beforeCount).toBe(5);
556
+
557
+ const deleted = yield* feed.deleteOldestBlocks({
558
+ spaceId: spaceA,
559
+ feedNamespace: nsData,
560
+ feedId: targetFeedId,
561
+ count: 3,
562
+ });
563
+ expect(deleted).toBe(3);
564
+
565
+ const afterCount = yield* feed.countBlocks({
566
+ spaceId: spaceA,
567
+ feedNamespace: nsData,
568
+ feedId: targetFeedId,
569
+ });
570
+ expect(afterCount).toBe(2);
571
+
572
+ const targetRemaining = yield* feed.query({
573
+ requestId: 'req-target-remaining',
574
+ spaceId: spaceA,
575
+ feedNamespace: nsData,
576
+ query: { feedIds: [targetFeedId] },
577
+ position: -1,
578
+ });
579
+ expect(targetRemaining.blocks.length).toBe(2);
580
+ expect(targetRemaining.blocks.map((block) => block.data[0])).toEqual([4, 5]);
581
+
582
+ const unaffectedFeed = yield* feed.countBlocks({
583
+ spaceId: spaceA,
584
+ feedNamespace: nsData,
585
+ feedId: 'feed-other',
586
+ });
587
+ expect(unaffectedFeed).toBe(1);
588
+
589
+ const unaffectedNamespace = yield* feed.countBlocks({
590
+ spaceId: spaceA,
591
+ feedNamespace: nsTrace,
592
+ feedId: 'feed-trace',
593
+ });
594
+ expect(unaffectedNamespace).toBe(1);
595
+
596
+ const unaffectedSpace = yield* feed.countBlocks({
597
+ spaceId: spaceB,
598
+ feedNamespace: nsData,
599
+ feedId: targetFeedId,
600
+ });
601
+ expect(unaffectedSpace).toBe(1);
602
+ }).pipe(Effect.provide(TestLayer)),
603
+ );
604
+
605
+ it.effect('deleteOldestBlocks handles non-positive and oversized delete counts', () =>
523
606
  Effect.gen(function* () {
524
607
  const spaceId = SpaceId.random();
525
- const feedId = ObjectId.random();
526
- const feed = new FeedStore({ localActorId: ALICE, assignPositions: false });
608
+ const feedId = 'feed-delete-count';
609
+ const feed = new FeedStore({ localActorId: ALICE, assignPositions: true });
527
610
  yield* feed.migrate();
528
611
 
529
- const error = yield* feed
530
- .append({
531
- requestId: 'req-dup-position',
532
- spaceId,
533
- feedNamespace: WellKnownNamespaces.data,
534
- blocks: [
535
- Block.make({
536
- feedId,
537
- actorId: 'actor-1',
538
- sequence: 1,
539
- prevActorId: null,
540
- prevSequence: null,
541
- position: 10,
542
- timestamp: Date.now(),
543
- data: new Uint8Array([1]),
544
- }),
545
- Block.make({
546
- feedId,
547
- actorId: 'actor-2',
548
- sequence: 1,
549
- prevActorId: null,
550
- prevSequence: null,
551
- position: 10,
552
- timestamp: Date.now(),
553
- data: new Uint8Array([2]),
554
- }),
555
- ],
556
- })
557
- .pipe(Effect.flip);
612
+ yield* feed.appendLocal([
613
+ { spaceId, feedId, feedNamespace: WellKnownNamespaces.data, data: new Uint8Array([1]) },
614
+ { spaceId, feedId, feedNamespace: WellKnownNamespaces.data, data: new Uint8Array([2]) },
615
+ ]);
558
616
 
559
- expect(error).toBeInstanceOf(DuplicateBlockPositionError);
560
- expect(error.message).toContain('Non-unique block position detected');
617
+ const deletedZero = yield* feed.deleteOldestBlocks({
618
+ spaceId,
619
+ feedNamespace: WellKnownNamespaces.data,
620
+ feedId,
621
+ count: 0,
622
+ });
623
+ expect(deletedZero).toBe(0);
624
+
625
+ const deletedNegative = yield* feed.deleteOldestBlocks({
626
+ spaceId,
627
+ feedNamespace: WellKnownNamespaces.data,
628
+ feedId,
629
+ count: -5,
630
+ });
631
+ expect(deletedNegative).toBe(0);
632
+
633
+ const deletedOversized = yield* feed.deleteOldestBlocks({
634
+ spaceId,
635
+ feedNamespace: WellKnownNamespaces.data,
636
+ feedId,
637
+ count: 10,
638
+ });
639
+ expect(deletedOversized).toBe(2);
640
+
641
+ const remainingCount = yield* feed.countBlocks({
642
+ spaceId,
643
+ feedNamespace: WellKnownNamespaces.data,
644
+ feedId,
645
+ });
646
+ expect(remainingCount).toBe(0);
561
647
  }).pipe(Effect.provide(TestLayer)),
562
648
  );
563
649
 
564
- it.effect('append fails on non-unique Lamport timestamp in request', () =>
650
+ it.effect('append ignores duplicate Lamport timestamp and preserves original data', () =>
565
651
  Effect.gen(function* () {
566
652
  const spaceId = SpaceId.random();
567
653
  const feedId = ObjectId.random();
568
654
  const feed = new FeedStore({ localActorId: ALICE, assignPositions: false });
569
655
  yield* feed.migrate();
570
656
 
571
- const error = yield* feed
572
- .append({
573
- requestId: 'req-dup-lamport',
574
- spaceId,
575
- feedNamespace: WellKnownNamespaces.data,
576
- blocks: [
577
- Block.make({
578
- feedId,
579
- actorId: 'actor-1',
580
- sequence: 5,
581
- prevActorId: null,
582
- prevSequence: null,
583
- position: 1,
584
- timestamp: Date.now(),
585
- data: new Uint8Array([1]),
586
- }),
587
- Block.make({
588
- feedId,
589
- actorId: 'actor-1',
590
- sequence: 5,
591
- prevActorId: null,
592
- prevSequence: null,
593
- position: 2,
594
- timestamp: Date.now(),
595
- data: new Uint8Array([2]),
596
- }),
597
- ],
598
- })
599
- .pipe(Effect.flip);
657
+ const actorId = 'actor-1';
658
+ const sequence = 5;
659
+ const timestamp = Date.now();
660
+
661
+ yield* feed.append({
662
+ requestId: 'req-first',
663
+ spaceId,
664
+ feedNamespace: WellKnownNamespaces.data,
665
+ blocks: [
666
+ Block.make({
667
+ feedId,
668
+ actorId,
669
+ sequence,
670
+ prevActorId: null,
671
+ prevSequence: null,
672
+ position: null,
673
+ timestamp,
674
+ data: new Uint8Array([1]),
675
+ }),
676
+ ],
677
+ });
678
+
679
+ // Same Lamport tuple with different data and position should be ignored.
680
+ yield* feed.append({
681
+ requestId: 'req-duplicate',
682
+ spaceId,
683
+ feedNamespace: WellKnownNamespaces.data,
684
+ blocks: [
685
+ Block.make({
686
+ feedId,
687
+ actorId,
688
+ sequence,
689
+ prevActorId: null,
690
+ prevSequence: null,
691
+ position: null,
692
+ timestamp: timestamp + 1,
693
+ data: new Uint8Array([2]),
694
+ }),
695
+ ],
696
+ });
697
+
698
+ const queryRes = yield* feed.query({
699
+ requestId: 'req-query',
700
+ spaceId,
701
+ feedNamespace: WellKnownNamespaces.data,
702
+ query: { feedIds: [feedId] },
703
+ position: -1,
704
+ });
600
705
 
601
- expect(error).toBeInstanceOf(DuplicateBlockLamportTimestampError);
602
- expect(error.message).toContain('Non-unique block Lamport timestamp detected');
706
+ expect(queryRes.blocks.length).toBe(1);
707
+ expect(queryRes.blocks[0].actorId).toBe(actorId);
708
+ expect(queryRes.blocks[0].sequence).toBe(sequence);
709
+ expect(queryRes.blocks[0].data).toEqual(new Uint8Array([1]));
603
710
  }).pipe(Effect.provide(TestLayer)),
604
711
  );
605
712
  });