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

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.f466a3d56e",
4
4
  "description": "Append-only replicatable log",
5
5
  "homepage": "https://dxos.org",
6
6
  "bugs": "https://github.com/dxos/dxos/issues",
@@ -8,7 +8,7 @@
8
8
  "type": "git",
9
9
  "url": "https://github.com/dxos/dxos"
10
10
  },
11
- "license": "MIT",
11
+ "license": "FSL-1.1-Apache-2.0",
12
12
  "author": "info@dxos.org",
13
13
  "sideEffects": true,
14
14
  "type": "module",
@@ -25,32 +25,27 @@
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"
38
31
  ],
39
32
  "dependencies": {
40
- "@dxos/errors": "^0.8.3",
41
33
  "@effect/sql": "0.49.0",
42
34
  "@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"
35
+ "effect": "3.20.0",
36
+ "@dxos/async": "0.8.4-main.f466a3d56e",
37
+ "@dxos/errors": "0.8.4-main.f466a3d56e",
38
+ "@dxos/context": "0.8.4-main.f466a3d56e",
39
+ "@dxos/invariant": "0.8.4-main.f466a3d56e",
40
+ "@dxos/effect": "0.8.4-main.f466a3d56e",
41
+ "@dxos/log": "0.8.4-main.f466a3d56e",
42
+ "@dxos/keys": "0.8.4-main.f466a3d56e",
43
+ "@dxos/protocols": "0.8.4-main.f466a3d56e",
44
+ "@dxos/sql-sqlite": "0.8.4-main.f466a3d56e",
45
+ "@dxos/util": "0.8.4-main.f466a3d56e"
46
+ },
47
+ "devDependencies": {
48
+ "@effect/vitest": "0.29.0"
54
49
  },
55
50
  "publishConfig": {
56
51
  "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;
@@ -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');
@@ -359,7 +360,8 @@ describe('Feed V2', () => {
359
360
  },
360
361
  ]);
361
362
  expect(blocks.length).toBe(1);
362
- expect(blocks[0].position).toBeNull();
363
+ expect(blocks[0].position).toBeDefined();
364
+ expect(blocks[0].position).toBeGreaterThanOrEqual(0);
363
365
  expect(blocks[0].sequence).toBe(0);
364
366
  expect(blocks[0].actorId).toBe(ALICE);
365
367
  expect(blocks[0].prevActorId).toBeNull();
@@ -367,16 +369,35 @@ describe('Feed V2', () => {
367
369
  expect(blocks[0].timestamp).toBeGreaterThan(0);
368
370
  expect(blocks[0].data).toEqual(new Uint8Array([1]));
369
371
 
370
- // Query by feedId
372
+ // Query by feedId: persisted position matches returned block position.
371
373
  const queryRes = yield* feed.query({
372
374
  query: { feedIds: [feedId] },
373
375
  position: -1,
374
376
  spaceId,
375
377
  feedNamespace: WellKnownNamespaces.data,
376
- }); // Use position '-1' to get everything
377
- expect(queryRes.blocks.length).toBe(1);
378
+ });
378
379
  expect(queryRes.blocks.length).toBe(1);
379
- expect(queryRes.blocks[0]).toMatchObject({ ...blocks[0], feedId, position: expect.any(Number) });
380
+ expect(queryRes.blocks[0].position).toBe(blocks[0].position);
381
+ expect(queryRes.blocks[0]).toMatchObject({ ...blocks[0], feedId });
382
+ }).pipe(Effect.provide(TestLayer)),
383
+ );
384
+
385
+ it.effect('appendLocal returns blocks with positions from append when assignPositions is true', () =>
386
+ Effect.gen(function* () {
387
+ const spaceId = SpaceId.random();
388
+ const feed = new FeedStore({ localActorId: ALICE, assignPositions: true });
389
+ yield* feed.migrate();
390
+
391
+ const blocks = yield* feed.appendLocal([
392
+ { spaceId, feedId: 'feed-a', feedNamespace: WellKnownNamespaces.data, data: new Uint8Array([1]) },
393
+ { spaceId, feedId: 'feed-b', feedNamespace: WellKnownNamespaces.data, data: new Uint8Array([2]) },
394
+ { spaceId, feedId: 'feed-a', feedNamespace: WellKnownNamespaces.data, data: new Uint8Array([3]) },
395
+ ]);
396
+
397
+ expect(blocks.length).toBe(3);
398
+ expect(blocks.every((block) => block.position != null && block.position >= 0)).toBe(true);
399
+ expect(blocks[0].position).toBeLessThan(blocks[1].position!);
400
+ expect(blocks[1].position).toBeLessThan(blocks[2].position!);
380
401
  }).pipe(Effect.provide(TestLayer)),
381
402
  );
382
403
 
@@ -390,13 +411,6 @@ describe('Feed V2', () => {
390
411
 
391
412
  const nsData = WellKnownNamespaces.data;
392
413
  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
414
 
401
415
  // First append in each (spaceId, feedNamespace) pair starts at 0.
402
416
  const feedAData = ObjectId.random();
@@ -447,23 +461,12 @@ describe('Feed V2', () => {
447
461
  },
448
462
  ]);
449
463
 
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);
464
+ // appendLocal returns blocks with positions matching persisted values.
465
+ expect(firstAData[0].position).toBe(0);
466
+ expect(firstATrace[0].position).toBe(0);
467
+ expect(firstBData[0].position).toBe(0);
468
+ expect(firstBHalo[0].position).toBe(0);
469
+ expect(secondAData[0].position).toBe(1);
467
470
  }).pipe(Effect.provide(TestLayer)),
468
471
  );
469
472
 
@@ -519,87 +522,193 @@ describe('Feed V2', () => {
519
522
  }).pipe(Effect.provide(TestLayer)),
520
523
  );
521
524
 
522
- 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', () =>
523
608
  Effect.gen(function* () {
524
609
  const spaceId = SpaceId.random();
525
- const feedId = ObjectId.random();
526
- const feed = new FeedStore({ localActorId: ALICE, assignPositions: false });
610
+ const feedId = 'feed-delete-count';
611
+ const feed = new FeedStore({ localActorId: ALICE, assignPositions: true });
527
612
  yield* feed.migrate();
528
613
 
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);
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
+ ]);
558
618
 
559
- expect(error).toBeInstanceOf(DuplicateBlockPositionError);
560
- expect(error.message).toContain('Non-unique block position detected');
619
+ const deletedZero = yield* feed.deleteOldestBlocks({
620
+ spaceId,
621
+ feedNamespace: WellKnownNamespaces.data,
622
+ feedId,
623
+ count: 0,
624
+ });
625
+ expect(deletedZero).toBe(0);
626
+
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);
561
649
  }).pipe(Effect.provide(TestLayer)),
562
650
  );
563
651
 
564
- it.effect('append fails on non-unique Lamport timestamp in request', () =>
652
+ it.effect('append ignores duplicate Lamport timestamp and preserves original data', () =>
565
653
  Effect.gen(function* () {
566
654
  const spaceId = SpaceId.random();
567
655
  const feedId = ObjectId.random();
568
656
  const feed = new FeedStore({ localActorId: ALICE, assignPositions: false });
569
657
  yield* feed.migrate();
570
658
 
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);
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
+ });
600
707
 
601
- expect(error).toBeInstanceOf(DuplicateBlockLamportTimestampError);
602
- 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]));
603
712
  }).pipe(Effect.provide(TestLayer)),
604
713
  );
605
714
  });