@streamsy/core 0.4.0 → 0.6.0

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.
@@ -1,12 +1,11 @@
1
- import { I as next, M as Storage, N as ZERO_OFFSET, Q as StreamId, q as ProducerId, z as StorageFault } from "../streamsy-OjRubIPC.js";
2
- import { n as CommitBoundary, t as layer } from "../layer-D8ybaCL2.js";
3
- import { a as MutationRejected, t as layer$1 } from "../layer-KvknRFUT.js";
4
- import { t as OwnerRecords } from "../owner-records-CX6yU28i.js";
5
- import { Cause, Context, Deferred, Effect, Exit, Fiber, Layer, Match, Option, Predicate, Queue, Ref, Stream } from "effect";
1
+ import { B as next, I as Storage, L as ZERO_OFFSET, U as StorageFault, Z as ProducerId, nt as StreamId } from "../streamsy-D9OCCdw0.js";
2
+ import { n as CommitBoundary, t as layer } from "../layer-DQZj5tXr.js";
3
+ import { a as MutationRejected, t as layer$1 } from "../layer-8Y1cg6UL.js";
4
+ import { t as OwnerRecords } from "../owner-records-C0p8jeC1.js";
5
+ import { Array, Cause, Context, Deferred, Effect, Exit, Fiber, Layer, Match, Option, Predicate, PubSub, Queue, Record, Ref, Scope, Stream } from "effect";
6
6
  import { describe, expect, it } from "vitest";
7
7
  import { TestClock } from "effect/testing";
8
- //#region src/testing/storage-contract.ts
9
- /** Executable Bun edge: a fresh scoped Layer and virtual clock for every case. */
8
+ // src/testing/storage-contract.ts
10
9
  const id = StreamId.make("s");
11
10
  const child = StreamId.make("child");
12
11
  const producerId = ProducerId.make("p");
@@ -162,6 +161,20 @@ const StorageContract = {
162
161
  expect(yield* records.read("checkpoint")).toEqual(Option.some("1"));
163
162
  expect(Option.isSome(yield* storage.record(id))).toBe(true);
164
163
  }));
164
+ recordsTest("R3 committed reports a transaction's ids after its commit and nothing after a rollback", ({ boundary }) => Effect.gen(function* () {
165
+ const storage = yield* Storage;
166
+ const other = StreamId.make("other");
167
+ const committed = yield* storage.committed;
168
+ const failed = yield* Effect.exit(boundary.withTransaction(Effect.andThen(mutate(create()), Effect.fail("rollback"))));
169
+ expect(Exit.isFailure(failed)).toBe(true);
170
+ expect(yield* PubSub.takeUpTo(committed, Infinity)).toEqual([]);
171
+ yield* boundary.withTransaction(Effect.gen(function* () {
172
+ yield* mutate(create());
173
+ yield* mutate(create(record(other)));
174
+ expect(yield* PubSub.takeUpTo(committed, Infinity)).toEqual([]);
175
+ }));
176
+ expect(yield* PubSub.takeUpTo(committed, Infinity)).toEqual([[id, other]]);
177
+ }));
165
178
  test("01 Append advances tail and folds close atomically", Effect.gen(function* () {
166
179
  yield* mutate(create());
167
180
  applied(yield* mutate(append({ patch: {
@@ -246,12 +259,12 @@ const StorageContract = {
246
259
  yield* mutate(create());
247
260
  const three = next(two), four = next(three);
248
261
  yield* mutate(append({
249
- messages: [
262
+ messages: Array.map([
250
263
  four,
251
264
  one,
252
265
  three,
253
266
  two
254
- ].map((offset) => ({
267
+ ], (offset) => ({
255
268
  offset,
256
269
  data: encode(offset),
257
270
  timestamp: 0
@@ -259,21 +272,21 @@ const StorageContract = {
259
272
  patch: { currentOffset: four }
260
273
  }));
261
274
  const storage = yield* Storage;
262
- expect((yield* storage.messages(id, {})).map((m) => m.offset)).toEqual([
275
+ expect(Array.map(yield* storage.messages(id, {}), (m) => m.offset)).toEqual([
263
276
  one,
264
277
  two,
265
278
  three,
266
279
  four
267
280
  ]);
268
- expect((yield* storage.messages(id, {
281
+ expect(Array.map(yield* storage.messages(id, {
269
282
  after: one,
270
283
  until: three
271
- })).map((m) => m.offset)).toEqual([two, three]);
272
- expect((yield* storage.messages(id, {
284
+ }), (m) => m.offset)).toEqual([two, three]);
285
+ expect(Array.map(yield* storage.messages(id, {
273
286
  after: one,
274
287
  until: four,
275
288
  limit: 1
276
- })).map((m) => m.offset)).toEqual([two]);
289
+ }), (m) => m.offset)).toEqual([two]);
277
290
  expect(yield* storage.messages(id, { limit: 0 })).toEqual([]);
278
291
  }));
279
292
  test("08 first changes element differs from stale observed offset", Effect.gen(function* () {
@@ -457,7 +470,7 @@ const StorageContract = {
457
470
  yield* fork();
458
471
  applied(yield* mutate(remove()), options.expected.fork === "chain" ? "SoftDeleted" : "Purged");
459
472
  const storage = yield* Storage;
460
- expect((yield* storage.messages(child, {})).map((m) => new TextDecoder().decode(m.data))).toEqual(["a"]);
473
+ expect(Array.map(yield* storage.messages(child, {}), (m) => new TextDecoder().decode(m.data))).toEqual(["a"]);
461
474
  if (options.expected.fork === "chain") rejected(yield* Effect.flip(mutate(remove())), "gone");
462
475
  yield* mutate(remove(child));
463
476
  expect(yield* storage.record(id)).toEqual(Option.none());
@@ -631,7 +644,7 @@ const StorageContract = {
631
644
  }));
632
645
  applied(yield* fork(child, grandchild), "Created");
633
646
  applied(yield* fork(id, sibling, one), "Created");
634
- const texts = (streamId) => storage.messages(streamId, {}).pipe(Effect.map((messages) => messages.map((m) => new TextDecoder().decode(m.data))));
647
+ const texts = (streamId) => storage.messages(streamId, {}).pipe(Effect.map((messages) => Array.map(messages, (m) => new TextDecoder().decode(m.data))));
635
648
  expect(yield* texts(grandchild)).toEqual(["a", "child"]);
636
649
  expect(yield* texts(sibling)).toEqual(["a"]);
637
650
  const deletion = options.expected.fork === "chain" ? "SoftDeleted" : "Purged";
@@ -678,29 +691,77 @@ const StorageContract = {
678
691
  };
679
692
  const outcome = yield* storage.mutate({ operations: deleteFirst ? [remove(), creation] : [creation, remove()] });
680
693
  expect(outcome).toMatchObject({ _tag: "Applied" });
681
- if (Predicate.isTagged(outcome, "Applied")) expect(outcome.results.map(({ _tag: tag }) => tag)).toEqual(deleteFirst ? ["SoftDeleted", "Created"] : ["Created", "SoftDeleted"]);
694
+ if (Predicate.isTagged(outcome, "Applied")) expect(Array.map(outcome.results, ({ _tag: tag }) => tag)).toEqual(deleteFirst ? ["SoftDeleted", "Created"] : ["Created", "SoftDeleted"]);
682
695
  expect((yield* current).lifecycle.softDeleted).toBe(true);
683
- expect((yield* storage.messages(child, {})).map((m) => m.offset)).toEqual([one]);
696
+ expect(Array.map(yield* storage.messages(child, {}), (m) => m.offset)).toEqual([one]);
684
697
  yield* mutate(remove(child));
685
698
  expect(yield* storage.record(id)).toEqual(Option.none());
686
699
  }
687
700
  }), options.expected.fork === "chain" && options.expected.atomicScope === "store", "requires chain fork and store atomic scope");
701
+ test("34a committed broadcasts without replay and releases each subscription independently", Effect.gen(function* () {
702
+ const storage = yield* Storage;
703
+ let released = 0;
704
+ const scope = yield* Scope.fork(yield* Scope.Scope);
705
+ const first = yield* storage.committed.pipe(Effect.tap(() => Effect.addFinalizer(() => Effect.sync(() => {
706
+ released += 1;
707
+ }))), Scope.provide(scope));
708
+ yield* mutate(create());
709
+ expect(yield* PubSub.take(first)).toEqual([id]);
710
+ const second = yield* storage.committed;
711
+ expect(yield* PubSub.takeUpTo(second, Infinity)).toEqual([]);
712
+ yield* mutate(append());
713
+ expect(yield* PubSub.take(first)).toEqual([id]);
714
+ expect(yield* PubSub.take(second)).toEqual([id]);
715
+ yield* Scope.close(scope, Exit.void);
716
+ expect(released).toBe(1);
717
+ yield* mutate(remove());
718
+ expect(yield* PubSub.take(second)).toEqual([id]);
719
+ }));
720
+ test("35 committed reports the ids of create, append, delete and expiry after commit", Effect.gen(function* () {
721
+ const storage = yield* Storage;
722
+ const expiring = StreamId.make("expiring");
723
+ const committed = yield* storage.committed;
724
+ yield* mutate(create());
725
+ expect(yield* PubSub.takeUpTo(committed, Infinity)).toEqual([[id]]);
726
+ yield* mutate(append());
727
+ rejected(yield* Effect.flip(mutate(create())), "exists");
728
+ yield* mutate(remove());
729
+ yield* mutate(create({
730
+ ...record(expiring),
731
+ lifecycle: {
732
+ closed: false,
733
+ softDeleted: false,
734
+ expiresAtMs: 100
735
+ }
736
+ }));
737
+ yield* mutate({
738
+ _tag: "Delete",
739
+ streamId: expiring,
740
+ reason: "expiry",
741
+ expectedExpiresAtMs: 100
742
+ });
743
+ expect(yield* PubSub.takeUpTo(committed, Infinity)).toEqual([
744
+ [id],
745
+ [id],
746
+ [expiring],
747
+ [expiring]
748
+ ]);
749
+ }));
688
750
  });
689
751
  },
690
752
  /** The configured memory suite must exercise each shipped capability value. */
691
753
  assertModes(configurations) {
692
754
  it("configured contract runs cover both shipped capability modes", () => {
693
- for (const [key, expected] of Object.entries({
755
+ for (const [key, expected] of Record.toEntries({
694
756
  fork: ["chain", "copy"],
695
757
  atomicScope: ["store", "stream"],
696
758
  wake: ["push", "poll"],
697
759
  expiryIndex: ["indexed", "lazy"]
698
- })) expect(new Set(configurations.map((c) => Object.entries(c).find(([k]) => k === key)?.[1]))).toEqual(new Set(expected));
760
+ })) expect(new Set(Array.map(configurations, (c) => Option.getOrUndefined(Array.findFirst(Record.toEntries(c), ([k]) => k === key))?.[1]))).toEqual(new Set(expected));
699
761
  });
700
762
  }
701
763
  };
702
- //#endregion
703
- //#region src/testing/fault-injection.ts
764
+ // src/testing/fault-injection.ts
704
765
  /** One selected attempt per decorator; "after" injects only if that mutation applies. */
705
766
  function faultyStorage(source, options) {
706
767
  return Layer.effect(Storage, Effect.gen(function* () {
@@ -724,8 +785,7 @@ function faultyStorage(source, options) {
724
785
  });
725
786
  })).pipe(Layer.provide(source));
726
787
  }
727
- //#endregion
728
- //#region src/testing/streams-test.ts
788
+ // src/testing/streams-test.ts
729
789
  var StreamsTest = class extends Context.Service()("@streamsy/core/StreamsTest") {};
730
790
  /** Real protocol and memory, with deterministic observation at the scoped changes boundary. */
731
791
  const layerTest = (options = {}) => Layer.effectContext(Effect.gen(function* () {
@@ -749,9 +809,7 @@ const layerTest = (options = {}) => Layer.effectContext(Effect.gen(function* ()
749
809
  snapshot: Queue.take(snapshots)
750
810
  }));
751
811
  })).pipe(Layer.provide(layer(options)));
752
- //#endregion
753
- //#region src/testing/expect-failure-tag.ts
812
+ // src/testing/expect-failure-tag.ts
754
813
  /** An unexpected success stays a failure so the enclosing test cannot pass. */
755
814
  const expectFailureTag = (effect, tag) => effect.pipe(Effect.flip, Effect.tap((error) => Effect.sync(() => expect(error._tag).toBe(tag))));
756
- //#endregion
757
815
  export { StorageContract, StreamsTest, expectFailureTag, faultyStorage, layerTest };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@streamsy/core",
3
- "version": "0.4.0",
3
+ "version": "0.6.0",
4
4
  "description": "Effect-first Durable Streams protocol, storage contract, memory layer and toolkit.",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -1,36 +0,0 @@
1
- import { Context, Data, Effect, Predicate } from "effect";
2
- //#region src/internal/owner.ts
3
- /** Private bridge: a stream guard dies with this sentinel; the atomic runner translates it. */
4
- const ForeignOwnerType = Symbol.for("@streamsy/core/internal/ForeignOwner");
5
- var ForeignOwner = class extends Data.TaggedError("ForeignOwner") {
6
- stream;
7
- [ForeignOwnerType] = true;
8
- constructor(stream, knownOwner) {
9
- super({ message: knownOwner ? `Stream write to ${stream} does not share the checkpoint owner` : `Cannot establish a storage owner for ${stream}; the exposed Storage does not own the protocol wires` });
10
- this.stream = stream;
11
- }
12
- };
13
- const isForeignOwner = (value) => Predicate.hasProperty(value, ForeignOwnerType) && value[ForeignOwnerType] === true;
14
- const OwnerMetadata = Symbol.for("@streamsy/core/internal/CommitOwner");
15
- const attachOwner = (value, owner) => Object.defineProperty(value, OwnerMetadata, {
16
- value: owner,
17
- enumerable: false
18
- });
19
- const ownerOf = (value) => {
20
- if (!(OwnerMetadata in value)) return value;
21
- const owner = value[OwnerMetadata];
22
- return Predicate.isObjectKeyword(owner) ? owner : value;
23
- };
24
- const CurrentOwner = Context.Reference("@streamsy/core/internal/CurrentOwner", { defaultValue: () => void 0 });
25
- const checkWriteOwner = (owner, id, knownOwner) => Effect.flatMap(CurrentOwner, (expected) => expected === void 0 || expected === owner ? Effect.void : Effect.die(new ForeignOwner(id, knownOwner)));
26
- const Metadata = Symbol.for("@streamsy/core/internal/StorageOwner");
27
- const attachStorage = (wire, storage) => Object.defineProperty(wire, Metadata, {
28
- value: storage,
29
- enumerable: false
30
- });
31
- const storageOf = (wire) => {
32
- if (!(Metadata in wire)) return void 0;
33
- return wire[Metadata];
34
- };
35
- //#endregion
36
- export { isForeignOwner as a, checkWriteOwner as i, attachOwner as n, ownerOf as o, attachStorage as r, storageOf as s, CurrentOwner as t };