@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.
- package/README.md +162 -4
- package/dist/{cursor-generator-CGz1TO_N.js → cursor-generator-DySXZ93b.js} +1 -2
- package/dist/http/index.d.ts +1 -1
- package/dist/http/index.js +2 -2
- package/dist/{http-EnNOUsIK.js → http-B10ao6xE.js} +9 -11
- package/dist/{index-CFv8vZvy.d.ts → index-p68har8W.d.ts} +322 -44
- package/dist/index.d.ts +2 -2
- package/dist/index.js +278 -51
- package/dist/internal/index.d.ts +24 -3
- package/dist/internal/index.js +3 -3
- package/dist/{layer-KvknRFUT.js → layer-8Y1cg6UL.js} +22 -33
- package/dist/{layer-D8ybaCL2.js → layer-DQZj5tXr.js} +39 -35
- package/dist/{owner-records-CX6yU28i.js → owner-records-C0p8jeC1.js} +1 -2
- package/dist/{owner-records-QDlssl__.d.ts → owner-records-H7VEuBpy.d.ts} +10 -23
- package/dist/{program-DJ8NpHIf.js → program-CYtMjdvD.js} +38 -55
- package/dist/protocol/index.d.ts +2 -4
- package/dist/protocol/index.js +4 -5
- package/dist/remote/index.d.ts +2 -4
- package/dist/remote/index.js +10 -14
- package/dist/{rolldown-runtime-D7D4PA-g.js → rolldown-runtime-D8jQpDG7.js} +1 -2
- package/dist/route-internal-DNL0oGTN.js +93 -0
- package/dist/{streamsy-OjRubIPC.js → streamsy-D9OCCdw0.js} +64 -102
- package/dist/testing/index.d.ts +2 -10
- package/dist/testing/index.js +85 -27
- package/package.json +1 -1
- package/dist/owner-BQzMpfC1.js +0 -36
package/dist/testing/index.js
CHANGED
|
@@ -1,12 +1,11 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { n as CommitBoundary, t as layer } from "../layer-
|
|
3
|
-
import { a as MutationRejected, t as layer$1 } from "../layer-
|
|
4
|
-
import { t as OwnerRecords } from "../owner-records-
|
|
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
|
-
|
|
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
|
-
]
|
|
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, {})
|
|
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
|
-
})
|
|
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
|
-
})
|
|
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, {})
|
|
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) =>
|
|
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
|
|
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, {})
|
|
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
|
|
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(
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
package/dist/owner-BQzMpfC1.js
DELETED
|
@@ -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 };
|