@streamsy/projection 0.13.0 → 0.14.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 +58 -35
- package/dist/{checkpoint-store-ViVETLzZ.js → checkpoint-store-BE0zelau.js} +80 -55
- package/dist/{checkpoint-store-B1199nDD.d.ts → checkpoint-store-Cy5myzCL.d.ts} +11 -7
- package/dist/checkpoint.d.ts +1 -1
- package/dist/checkpoint.js +1 -1
- package/dist/index.d.ts +68 -68
- package/dist/index.js +163 -110
- package/package.json +4 -4
package/dist/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { t as __exportAll } from "./rolldown-runtime-D8jQpDG7.js";
|
|
2
|
-
import { _ as
|
|
3
|
-
import { Array, Cause, Clock, Context, Data, DateTime, Deferred, Duration, Effect, Exit, Function, Layer, Option, Pipeable, Predicate, PubSub, Pull, Queue, Record, Ref, Result, Schedule, Schema, Semaphore, Stream } from "effect";
|
|
2
|
+
import { _ as NonRetryable, c as recordKey, d as canonicalParams, f as encodeKey, g as InvalidBinding, h as withParams, i as assertCheckpointKeyVersion, l as restore, m as unitOf, n as CheckpointStore, o as loadLatest, p as terminalOf, r as advance, s as rangesOf, t as CheckpointRecord, u as unsupportedBindingList, v as NotAttached, y as ProjectionFault } from "./checkpoint-store-BE0zelau.js";
|
|
3
|
+
import { Array, Cause, Clock, Context, Data, DateTime, Deferred, Duration, Effect, Exit, Function, Layer, Option, Order, Pipeable, Predicate, PubSub, Pull, Queue, Record, Ref, Result, Schedule, Schema, Semaphore, Stream } from "effect";
|
|
4
4
|
import { CommitBoundary, DurableState, Offset, Storage, StorageFault, StreamAddress, StreamId, Streamsy, ZERO_OFFSET, compare, isStreamsFault } from "@streamsy/core";
|
|
5
5
|
import { CommitHooks, CurrentOwner, MAX_PRODUCER_ID_LENGTH, OwnerRecords, ProjectionMarks, compileSegments, fitKey, isForeignOwner, memoryProjectionMarks, offsetsOf, ownerOf } from "@streamsy/core/internal";
|
|
6
6
|
import { Streams, StreamsWriter } from "@streamsy/core/protocol";
|
|
@@ -319,26 +319,18 @@ const checkpoint = make$1({
|
|
|
319
319
|
apply: (pending) => Effect.flatMap(pending.run, (result) => settle(pending, result))
|
|
320
320
|
});
|
|
321
321
|
// src/transaction.ts
|
|
322
|
-
/** The
|
|
322
|
+
/** The storage owner's transaction, used by Commit.transaction. */
|
|
323
323
|
var Transaction = class extends Context.Service()("@streamsy/projection/Transaction") {
|
|
324
|
-
/**
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
withTransaction: (body) => boundary.withTransaction(body).pipe(Effect.catchTag("SqlError", (cause) => Effect.fail(new ProjectionFault({
|
|
335
|
-
phase: "checkpoint",
|
|
336
|
-
reason: "storage-failure",
|
|
337
|
-
message: "The owner transaction failed",
|
|
338
|
-
cause
|
|
339
|
-
}))))
|
|
340
|
-
};
|
|
341
|
-
}));
|
|
324
|
+
/** Join the commit boundary on a memory or SQL storage host. */
|
|
325
|
+
static layer = Layer.effect(this, Effect.map(CommitBoundary, (boundary) => ({
|
|
326
|
+
owner: ownerOf(boundary),
|
|
327
|
+
withTransaction: (body) => boundary.withTransaction(body).pipe(Effect.catchTag("SqlError", (cause) => Effect.fail(new ProjectionFault({
|
|
328
|
+
phase: "checkpoint",
|
|
329
|
+
reason: "storage-failure",
|
|
330
|
+
message: "The owner transaction failed",
|
|
331
|
+
cause
|
|
332
|
+
}))))
|
|
333
|
+
})));
|
|
342
334
|
};
|
|
343
335
|
// src/per-generation.ts
|
|
344
336
|
const TypeId = Symbol.for("@streamsy/projection/PerGeneration");
|
|
@@ -429,7 +421,7 @@ const routedHandle = (name, route, names, params, key, occupied, routed) => ({ o
|
|
|
429
421
|
if (opened === name) return address;
|
|
430
422
|
if (opened !== void 0) return yield* processFault("invalid-output", `Routed outputs ${opened} and ${name} both name ${address.id}`);
|
|
431
423
|
const records = yield* requireRouteRecords(unit.store, "process");
|
|
432
|
-
if (
|
|
424
|
+
if ((yield* records.state(key, name, address.id)) !== "live") {
|
|
433
425
|
for (const [other, definition] of routed) if (other !== name && definition.matches(address.id) && (yield* records.has(key, other, address.id))) return yield* processFault("invalid-output", `Routed outputs ${other} and ${name} both name ${address.id}`);
|
|
434
426
|
const created = yield* createOutput(address, service.wire);
|
|
435
427
|
if (created.closed || created.nextOffset !== ZERO_OFFSET) return yield* processFault("invalid-output", `Generation-bound output ${address.id} must start empty and open`);
|
|
@@ -440,52 +432,86 @@ const routedHandle = (name, route, names, params, key, occupied, routed) => ({ o
|
|
|
440
432
|
}));
|
|
441
433
|
}) });
|
|
442
434
|
// src/commit/transaction.ts
|
|
443
|
-
const
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
435
|
+
const checkpointOwnerFault = (key) => new ProjectionFault({
|
|
436
|
+
phase: "checkpoint",
|
|
437
|
+
reason: "unsupported-composition",
|
|
438
|
+
message: `The checkpoint store for ${key.id} does not share the transaction owner`
|
|
439
|
+
});
|
|
440
|
+
/** Run the body for the owner, and fail when it writes to a stream on another owner. */
|
|
441
|
+
const withOwner = (tx, phase) => (body) => body.pipe(Effect.provideService(CurrentOwner, tx.owner), Effect.catchDefect((defect) => isForeignOwner(defect) ? Effect.fail(new ProjectionFault({
|
|
442
|
+
phase,
|
|
443
|
+
reason: "unsupported-composition",
|
|
444
|
+
message: defect.message
|
|
445
|
+
})) : Effect.die(defect)));
|
|
446
|
+
const checkpointState = (loaded) => loaded.pipe(Effect.map(({ record }) => Option.isSome(record) ? "present" : "absent"), Effect.catchIf((fault) => fault.reason === "retired-generation", () => Effect.succeed("retired")), Effect.catchIf((fault) => fault.reason === "invalid-record", () => Effect.succeed("present")), Effect.mapError(({ reason, message, cause }) => new ProjectionFault({
|
|
447
|
+
phase: "checkpoint",
|
|
448
|
+
reason,
|
|
449
|
+
message,
|
|
450
|
+
cause
|
|
451
|
+
})));
|
|
452
|
+
const removeWithRoutes = (store, key, checkpoint) => Effect.gen(function* () {
|
|
451
453
|
const records = yield* requireRouteRecords(store, "checkpoint");
|
|
452
|
-
if (
|
|
453
|
-
|
|
454
|
+
if (checkpoint === "absent") yield* records.forget(key, { ignoreUnsupportedList: true });
|
|
455
|
+
else if (checkpoint === "present") {
|
|
456
|
+
yield* records.forget(key);
|
|
454
457
|
yield* store.remove(key);
|
|
455
458
|
}
|
|
456
459
|
return yield* Effect.void;
|
|
457
460
|
});
|
|
458
|
-
const retireInTransaction = (request) => Effect.gen(function* () {
|
|
459
|
-
const tx = yield* Transaction;
|
|
461
|
+
const retireInTransaction = (tx, request) => Effect.gen(function* () {
|
|
460
462
|
const store = yield* CheckpointStore;
|
|
461
|
-
if (ownerOf(store.load) !== tx.owner) return yield*
|
|
463
|
+
if (ownerOf(store.load) !== tx.owner) return yield* checkpointOwnerFault(request.key);
|
|
464
|
+
return yield* tx.withTransaction(retire$1(request).pipe(withOwner(tx, "checkpoint")));
|
|
465
|
+
});
|
|
466
|
+
const TransactionCommitTypeId = Symbol("@streamsy/projection/TransactionCommit");
|
|
467
|
+
const removeInTransaction = (tx, key, cleanup) => tx.withTransaction(Effect.gen(function* () {
|
|
468
|
+
const store = yield* CheckpointStore;
|
|
469
|
+
if (ownerOf(store.load) !== tx.owner) return yield* checkpointOwnerFault(key);
|
|
470
|
+
const checkpoint = yield* checkpointState(store.load(key));
|
|
471
|
+
if (RoutedOutputs in key) yield* removeWithRoutes(store, key, checkpoint);
|
|
472
|
+
else yield* store.remove(key);
|
|
473
|
+
yield* cleanup.pipe(withOwner(tx, "checkpoint"));
|
|
474
|
+
if ((yield* checkpointState(checkpoint === "absent" ? loadLatest(store, key) : store.load(key))) !== (checkpoint === "retired" ? "retired" : "absent")) return yield* new ProjectionFault({
|
|
462
475
|
phase: "checkpoint",
|
|
463
|
-
reason: "
|
|
464
|
-
message: `
|
|
476
|
+
reason: "token-conflict",
|
|
477
|
+
message: `Record of ${key.id} changed while it was being forgotten. A second writer on the same key is the likely cause. Stop its runners, then forget again.`
|
|
465
478
|
});
|
|
466
|
-
return yield*
|
|
467
|
-
});
|
|
468
|
-
|
|
479
|
+
return yield* Effect.void;
|
|
480
|
+
}));
|
|
481
|
+
/**
|
|
482
|
+
* Commit the unit and checkpoint together on a memory or SQL storage host.
|
|
483
|
+
* A SqlClient used by the handler must be the storage owner's client for its
|
|
484
|
+
* statements to join the transaction.
|
|
485
|
+
*
|
|
486
|
+
* On the same client the unit joins the outer transaction without a savepoint.
|
|
487
|
+
* With two boundaries over one client, only the outer boundary's hooks and
|
|
488
|
+
* `committed` subscribers see the commit.
|
|
489
|
+
*
|
|
490
|
+
* On another SQL client the unit opens its own transaction. It commits first and
|
|
491
|
+
* survives an outer rollback. Use one client per database for nested work. With
|
|
492
|
+
* two clients on one database, SQLite can wait until it reports a busy lock.
|
|
493
|
+
* Postgres or MySQL can wait for a row lock that the outer transaction holds.
|
|
494
|
+
*
|
|
495
|
+
* Durable Object clients share the driver's transaction, so a second client
|
|
496
|
+
* joins the first. Using a second object's storage on one fiber is unsupported.
|
|
497
|
+
*/
|
|
498
|
+
const transaction = Object.assign(withPrepare(make$1({
|
|
469
499
|
name: "transaction",
|
|
470
500
|
guarantee: "exactly-once",
|
|
471
501
|
load,
|
|
472
|
-
remove: (key) =>
|
|
473
|
-
retire: retireInTransaction,
|
|
502
|
+
remove: (key) => Transaction.use((tx) => removeInTransaction(tx, key, Effect.void)),
|
|
503
|
+
retire: (request) => Transaction.use((tx) => retireInTransaction(tx, request)),
|
|
474
504
|
apply: (pending) => Effect.gen(function* () {
|
|
475
505
|
const tx = yield* Transaction;
|
|
476
506
|
const store = yield* CheckpointStore;
|
|
477
507
|
const { outputs, prepare } = detailsOf(pending);
|
|
478
508
|
const hasRoutes = RoutedOutputs in pending.key;
|
|
479
|
-
if (hasRoutes && ownerOf(store.load) !== tx.owner) return yield* new ProjectionFault({
|
|
480
|
-
phase: "checkpoint",
|
|
481
|
-
reason: "unsupported-composition",
|
|
482
|
-
message: `The checkpoint store for ${pending.key.id} does not share the transaction owner`
|
|
483
|
-
});
|
|
484
509
|
for (const { address, service } of Record.values(outputs)) if (service.owner !== tx.owner) return yield* new ProjectionFault({
|
|
485
510
|
phase: "load",
|
|
486
511
|
reason: "unsupported-composition",
|
|
487
512
|
message: `Output ${address.id} does not share the transaction owner`
|
|
488
513
|
});
|
|
514
|
+
if (ownerOf(store.load) !== tx.owner) return yield* checkpointOwnerFault(pending.key);
|
|
489
515
|
if (prepare !== void 0) yield* prepare;
|
|
490
516
|
yield* tx.withTransaction(Effect.gen(function* () {
|
|
491
517
|
const again = yield* store.load(pending.key);
|
|
@@ -503,11 +529,7 @@ const transaction = withPrepare(make$1({
|
|
|
503
529
|
message: `Generation-bound output ${address.id} must start empty and open`
|
|
504
530
|
});
|
|
505
531
|
}
|
|
506
|
-
const result = yield* pending.run.pipe(
|
|
507
|
-
phase: "process",
|
|
508
|
-
reason: "unsupported-composition",
|
|
509
|
-
message: defect.message
|
|
510
|
-
})) : Effect.die(defect)));
|
|
532
|
+
const result = yield* pending.run.pipe(withOwner(tx, "process"), Effect.provideService(TransactionGenerationOutputs, pending.generationOutputs), Effect.provideService(TransactionRoutedOutputs, hasRoutes ? routedUnit(pending.key, store) : void 0));
|
|
511
533
|
if (result !== void 0 && "outputs" in result) for (const [name, output] of Record.toEntries(result.outputs)) {
|
|
512
534
|
if (!Array.isReadonlyArrayNonEmpty(output.items)) continue;
|
|
513
535
|
const resolved = outputs[name];
|
|
@@ -528,24 +550,28 @@ const transaction = withPrepare(make$1({
|
|
|
528
550
|
}));
|
|
529
551
|
return yield* Effect.void;
|
|
530
552
|
})
|
|
531
|
-
})).pipe(withAccepted);
|
|
532
|
-
/**
|
|
553
|
+
})).pipe(withAccepted), { [TransactionCommitTypeId]: true });
|
|
554
|
+
/**
|
|
555
|
+
* Clean up binding state in the owner's transaction on forget and retire.
|
|
556
|
+
* The boundary follows the same nesting rules as {@link transaction}.
|
|
557
|
+
*/
|
|
533
558
|
const transactionWithCleanup = (cleanup) => inherit(transaction, {
|
|
534
559
|
...transaction,
|
|
535
|
-
remove: (key) => Transaction.use((tx) => tx
|
|
536
|
-
retire: (request) => Transaction.use((tx) => retireInTransaction({
|
|
560
|
+
remove: (key) => Transaction.use((tx) => removeInTransaction(tx, key, Effect.suspend(() => cleanup(key)))),
|
|
561
|
+
retire: (request) => Transaction.use((tx) => retireInTransaction(tx, {
|
|
537
562
|
...request,
|
|
538
|
-
cleanup: request.cleanup.pipe(Effect.andThen(() => cleanup(request.key
|
|
563
|
+
cleanup: request.cleanup.pipe(Effect.andThen(() => cleanup(request.key)))
|
|
539
564
|
}))
|
|
540
565
|
});
|
|
541
566
|
// src/commit/producer.ts
|
|
567
|
+
const PRODUCER_ID_SUFFIX = "/v1";
|
|
542
568
|
/**
|
|
543
569
|
* The pinned tuple identity: the params join the id so two parameterisations never share a sequence.
|
|
544
570
|
* An id within the storage limit is used as it is, because outputs already hold producer state under it.
|
|
545
|
-
* A longer id becomes `<id>/
|
|
571
|
+
* A longer id becomes `<id>/v1/#<sha256 of the full id>`. A long projection id is cut by
|
|
546
572
|
* code point so the result is at most the limit.
|
|
547
573
|
*/
|
|
548
|
-
const producerId = (id,
|
|
574
|
+
const producerId = (id, params) => fitKey(`${id}${PRODUCER_ID_SUFFIX}${Record.keys(params).length === 0 ? "" : `/${canonicalParams(params)}`}`, MAX_PRODUCER_ID_LENGTH, `${id}${PRODUCER_ID_SUFFIX}/`);
|
|
549
575
|
/** The head of one output. A missing output on the checkpoint's owner is created here. */
|
|
550
576
|
const prepareOutput = (output, store, beforeCreate) => {
|
|
551
577
|
const { wire, owner: outputOwner } = output.service;
|
|
@@ -655,7 +681,7 @@ const producer = pin$1(make$1({
|
|
|
655
681
|
if (before.record.state !== void 0) Object.assign(pinned, { state: before.record.state });
|
|
656
682
|
token = yield* owner.compareAndSet(projection, before.token, pinned).pipe(Effect.mapError(asPin));
|
|
657
683
|
}
|
|
658
|
-
const appendAs = yield* producerId(projection.id, projection.
|
|
684
|
+
const appendAs = yield* producerId(projection.id, projection.params);
|
|
659
685
|
for (const [name, output] of Record.toEntries(streams)) {
|
|
660
686
|
const seq = seqs[name];
|
|
661
687
|
const items = processed.items[name] ?? [];
|
|
@@ -1004,11 +1030,9 @@ const addressesOf = (inputs, params) => {
|
|
|
1004
1030
|
return Record.fromEntries(Array.map(Record.toEntries(inputs), ([name, input]) => [name, isDeclaration(input) ? input.at(params) : input]));
|
|
1005
1031
|
};
|
|
1006
1032
|
const declare = (definition, bind) => {
|
|
1007
|
-
const version = definition.version ?? 1;
|
|
1008
1033
|
const generation = definition.generation ?? 1;
|
|
1009
1034
|
const atGeneration = (params, selected) => bind({
|
|
1010
1035
|
id: definition.id,
|
|
1011
|
-
version,
|
|
1012
1036
|
generation: selected,
|
|
1013
1037
|
params: encodeParams(definition.id, definition.params, params),
|
|
1014
1038
|
decoded: params
|
|
@@ -1016,7 +1040,6 @@ const declare = (definition, bind) => {
|
|
|
1016
1040
|
const declaration = {
|
|
1017
1041
|
retry: definition.retry,
|
|
1018
1042
|
id: definition.id,
|
|
1019
|
-
version,
|
|
1020
1043
|
generation,
|
|
1021
1044
|
params: definition.params,
|
|
1022
1045
|
inputs: definition.inputs,
|
|
@@ -1032,7 +1055,14 @@ const declare = (definition, bind) => {
|
|
|
1032
1055
|
};
|
|
1033
1056
|
return Object.defineProperty(declaration, BindGeneration, { value: atGeneration });
|
|
1034
1057
|
};
|
|
1058
|
+
/** @internal Reject legacy definitions passed from JavaScript or unchecked TypeScript. */
|
|
1059
|
+
const rejectProjectionVersion = (id, definition) => {
|
|
1060
|
+
if (!("version" in definition) || definition.version === void 0) return;
|
|
1061
|
+
const advice = definition.version === 1 ? "Remove version: 1 from the declaration." : "Raise generation above every generation the projection has used.";
|
|
1062
|
+
throw new RangeError(`Projection ${id}: version was removed. ${advice}`);
|
|
1063
|
+
};
|
|
1035
1064
|
const build = (kind, definition) => {
|
|
1065
|
+
rejectProjectionVersion(definition.id, definition);
|
|
1036
1066
|
const commit = definition.commit ?? checkpoint;
|
|
1037
1067
|
const outputs = definition.outputs ?? {};
|
|
1038
1068
|
for (const output of Record.values(outputs)) {
|
|
@@ -1084,11 +1114,15 @@ function mapAccumBatch(definition) {
|
|
|
1084
1114
|
}
|
|
1085
1115
|
// src/forget.ts
|
|
1086
1116
|
/**
|
|
1087
|
-
* Deletes a live
|
|
1117
|
+
* Deletes a live checkpoint and marks its routed outputs as forgotten so retirement
|
|
1118
|
+
* can still find them. Preserves retirement tombstones. Stop the follower first,
|
|
1088
1119
|
* or use a new generation.
|
|
1120
|
+
* Pass the binding from `declaration.at(params)` for a routed projection. A bare
|
|
1121
|
+
* key deletes the checkpoint and leaves the route records live.
|
|
1089
1122
|
* Delete stream outputs first, because forgetting restarts their producer sequences.
|
|
1090
1123
|
*/
|
|
1091
1124
|
const forget = (projection) => Effect.gen(function* () {
|
|
1125
|
+
assertCheckpointKeyVersion(projection);
|
|
1092
1126
|
if (projection.commit.remove === void 0) return yield* new ProjectionFault({
|
|
1093
1127
|
phase: "checkpoint",
|
|
1094
1128
|
reason: "unsupported-composition",
|
|
@@ -1184,7 +1218,7 @@ const step = Effect.fn("Projection.pass")(function* (projection, context, allowE
|
|
|
1184
1218
|
}
|
|
1185
1219
|
const gone = Array.some(Record.values(batch), (slice) => slice.gone === true);
|
|
1186
1220
|
const ranges = pending?.ranges ?? (dueOnly ? {} : rangesOf(batch));
|
|
1187
|
-
const unit = unitOf(projection.id, projection.
|
|
1221
|
+
const unit = unitOf(projection.id, projection.generation, projection.params, ranges, due);
|
|
1188
1222
|
context?.unit(unit);
|
|
1189
1223
|
const { pending: _pending, wakeAt: _wakeAt, ...record } = before.record;
|
|
1190
1224
|
const closed = { ...record.closed };
|
|
@@ -1454,10 +1488,10 @@ var Registry = class Registry extends Context.Service()("@streamsy/projection/Re
|
|
|
1454
1488
|
const reached = (value) => value !== void 0 && compare(value, offset) >= 0;
|
|
1455
1489
|
return yield* Effect.scoped(Effect.gen(function* () {
|
|
1456
1490
|
const runner = yield* find(binding);
|
|
1457
|
-
if (runner.
|
|
1491
|
+
if (runner.generation !== binding.generation) return yield* new ProjectionFault({
|
|
1458
1492
|
phase: "load",
|
|
1459
1493
|
reason: "identity-mismatch",
|
|
1460
|
-
message: `Attached
|
|
1494
|
+
message: `Attached generation of ${binding.id} is ${runner.generation}, but the binding uses generation ${binding.generation}`
|
|
1461
1495
|
});
|
|
1462
1496
|
const pull = yield* Stream.toPull(observations);
|
|
1463
1497
|
const checkpoint = yield* read(binding);
|
|
@@ -2014,9 +2048,8 @@ const sourceGone = (inputs, cause) => Effect.gen(function* () {
|
|
|
2014
2048
|
* that transaction. Dies when the storage layer has no marks, for example
|
|
2015
2049
|
* over remote storage.
|
|
2016
2050
|
*/
|
|
2017
|
-
const durableMarks = (projection, match) => Effect.gen(function* () {
|
|
2051
|
+
const durableMarks = (projection, match, marks) => Effect.gen(function* () {
|
|
2018
2052
|
const hooks = yield* Effect.serviceOption(CommitHooks);
|
|
2019
|
-
const marks = yield* Effect.serviceOption(ProjectionMarks);
|
|
2020
2053
|
if (Option.isNone(hooks) || Option.isNone(marks)) return yield* Effect.die(/* @__PURE__ */ new Error(`Projection ${projection} cannot attach durably: the storage layer keeps no projection marks. Use a local memory or SQL storage layer, or pass durable: false.`));
|
|
2021
2054
|
yield* hooks.value.beforeCommit((ids) => {
|
|
2022
2055
|
const keys = /* @__PURE__ */ new Set();
|
|
@@ -2053,6 +2086,13 @@ const runnerOf = (declaration, options = {}, defaultDurable = false, host = "pro
|
|
|
2053
2086
|
const match = matcher(declaration);
|
|
2054
2087
|
return Effect.gen(function* () {
|
|
2055
2088
|
const storage = yield* Storage;
|
|
2089
|
+
const storedMarks = durable ? yield* Effect.serviceOption(ProjectionMarks) : Option.none();
|
|
2090
|
+
const maxIdLength = Option.getOrUndefined(storedMarks)?.maxProjectionIdLength;
|
|
2091
|
+
if (maxIdLength !== void 0 && Array.fromIterable(declaration.id).length > maxIdLength) return yield* Effect.die(new StorageFault({
|
|
2092
|
+
operation: "projection.attach",
|
|
2093
|
+
message: `MySQL projection id must be at most ${maxIdLength} characters`,
|
|
2094
|
+
retryable: false
|
|
2095
|
+
}));
|
|
2056
2096
|
const registry = yield* Effect.serviceOption(Registry);
|
|
2057
2097
|
const publish = Option.isSome(registry) ? eventPublisher(registry.value) : void 0;
|
|
2058
2098
|
const committed = yield* storage.committed;
|
|
@@ -2078,7 +2118,7 @@ const runnerOf = (declaration, options = {}, defaultDurable = false, host = "pro
|
|
|
2078
2118
|
}, () => Effect.sync(() => {
|
|
2079
2119
|
active -= 1;
|
|
2080
2120
|
}));
|
|
2081
|
-
const marks = durable ? yield* durableMarks(declaration.id, match) : memoryProjectionMarks();
|
|
2121
|
+
const marks = durable ? yield* durableMarks(declaration.id, match, storedMarks) : memoryProjectionMarks();
|
|
2082
2122
|
let failure = Option.none();
|
|
2083
2123
|
let started = false;
|
|
2084
2124
|
const startPermit = yield* Semaphore.make(1);
|
|
@@ -2380,7 +2420,6 @@ const attach$1 = (projection, options = {}) => {
|
|
|
2380
2420
|
const registry = yield* Effect.serviceOption(Registry);
|
|
2381
2421
|
const changed = Option.isNone(registry) ? Effect.void : yield* registerRunner(registry.value, {
|
|
2382
2422
|
id: projection.id,
|
|
2383
|
-
version: projection.version,
|
|
2384
2423
|
generation: projection.generation,
|
|
2385
2424
|
schedule,
|
|
2386
2425
|
rescan,
|
|
@@ -2445,7 +2484,7 @@ const current = Effect.fn("Projection.current")(function* (projection) {
|
|
|
2445
2484
|
offsets: offsetsOf$1({ inputs: offsets })
|
|
2446
2485
|
};
|
|
2447
2486
|
const ranges = rangesOf(page.slices);
|
|
2448
|
-
const unit = unitOf(projection.id, projection.
|
|
2487
|
+
const unit = unitOf(projection.id, projection.generation, projection.params, ranges);
|
|
2449
2488
|
const result = yield* projection.process(page.slices, unit, encoded);
|
|
2450
2489
|
state = result.value;
|
|
2451
2490
|
encoded = result.encoded;
|
|
@@ -2455,12 +2494,13 @@ const current = Effect.fn("Projection.current")(function* (projection) {
|
|
|
2455
2494
|
// src/bindings.ts
|
|
2456
2495
|
/**
|
|
2457
2496
|
* Lists the decoded params of bindings with live checkpoints in the selected store.
|
|
2458
|
-
* By default it includes every generation of the declaration's id
|
|
2459
|
-
*
|
|
2497
|
+
* By default it includes every generation of the declaration's id. The inventory
|
|
2498
|
+
* reads every stored version slot, even with a generation filter.
|
|
2460
2499
|
* Each binding appears once, in unspecified order.
|
|
2461
2500
|
*
|
|
2462
2501
|
* Fails with `InvalidBinding` if live params cannot round-trip through the current
|
|
2463
|
-
* codecs. MySQL
|
|
2502
|
+
* codecs. On MySQL, listing fails while the range contains a legacy hashed key
|
|
2503
|
+
* that has yet to be rewritten.
|
|
2464
2504
|
*/
|
|
2465
2505
|
const bindings = Effect.fn("Projection.bindings")(function* (declaration, options) {
|
|
2466
2506
|
const store = yield* CheckpointStore;
|
|
@@ -2533,6 +2573,7 @@ const retryLostRace = Effect.fnUntraced(function* (attempt, schedule) {
|
|
|
2533
2573
|
});
|
|
2534
2574
|
const isOffsetMismatch = (error) => Predicate.isTagged(error, "OffsetMismatch");
|
|
2535
2575
|
// src/retire.ts
|
|
2576
|
+
const streamOrder = Order.make((left, right) => Order.String(left.output, right.output) || Order.String(left.id, right.id));
|
|
2536
2577
|
const cleanStream = (address, wire, mode) => {
|
|
2537
2578
|
const { id, contentType } = address;
|
|
2538
2579
|
return (mode === "remove" ? wire.remove(id).pipe(Effect.as("removed")) : wire.append(id, {
|
|
@@ -2559,76 +2600,77 @@ const retire = (declaration, params, options) => {
|
|
|
2559
2600
|
reason: "unsupported-composition",
|
|
2560
2601
|
message: "Retirement requires a projection declaration with a Commit that supports it"
|
|
2561
2602
|
});
|
|
2562
|
-
const
|
|
2603
|
+
const shared = [];
|
|
2563
2604
|
const owned = [];
|
|
2564
2605
|
const bindings = bound[GenerationOutputs];
|
|
2565
2606
|
const routes = bound[RoutedOutputs];
|
|
2566
|
-
const routed = /* @__PURE__ */ new Map();
|
|
2567
2607
|
const context = yield* Effect.context();
|
|
2568
|
-
|
|
2569
|
-
|
|
2570
|
-
const binding = bindings?.get(name);
|
|
2608
|
+
for (const [output, address] of Record.toEntries(boundOutputs(bound))) {
|
|
2609
|
+
const binding = bindings?.get(output);
|
|
2571
2610
|
if (binding === void 0) continue;
|
|
2572
|
-
if (binding.shared)
|
|
2611
|
+
if (binding.shared) shared.push({
|
|
2612
|
+
output,
|
|
2573
2613
|
id: address.id,
|
|
2574
2614
|
result: "shared"
|
|
2575
|
-
}
|
|
2615
|
+
});
|
|
2576
2616
|
else owned.push({
|
|
2577
|
-
|
|
2617
|
+
output,
|
|
2578
2618
|
address,
|
|
2579
2619
|
service: yield* address.stream
|
|
2580
2620
|
});
|
|
2581
2621
|
}
|
|
2582
|
-
|
|
2622
|
+
let streams = shared;
|
|
2623
|
+
return {
|
|
2583
2624
|
record: yield* bound.commit.retire({
|
|
2584
2625
|
key: {
|
|
2585
2626
|
id: bound.id,
|
|
2586
|
-
version: bound.version,
|
|
2587
2627
|
generation: bound.generation,
|
|
2588
2628
|
params: bound.params
|
|
2589
2629
|
},
|
|
2590
2630
|
cleanup: Effect.gen(function* () {
|
|
2591
2631
|
const owner = yield* CurrentOwner;
|
|
2592
|
-
const
|
|
2632
|
+
const results = [...shared];
|
|
2633
|
+
const clean = (output, address, service) => {
|
|
2593
2634
|
if (owner !== void 0 && service.owner !== owner) return Effect.fail(new ProjectionFault({
|
|
2594
2635
|
phase: "checkpoint",
|
|
2595
2636
|
reason: "unsupported-composition",
|
|
2596
2637
|
message: `Output ${address.id} does not share the transaction owner`
|
|
2597
2638
|
}));
|
|
2598
2639
|
return cleanStream(address, service.wire, mode).pipe(Effect.map((result) => ({
|
|
2640
|
+
output,
|
|
2599
2641
|
id: address.id,
|
|
2600
2642
|
result
|
|
2601
2643
|
})));
|
|
2602
2644
|
};
|
|
2603
|
-
for (const {
|
|
2645
|
+
for (const { output, address, service } of owned) results.push(yield* clean(output, address, service));
|
|
2604
2646
|
if (routes !== void 0) {
|
|
2605
2647
|
const records = yield* currentRouteRecords;
|
|
2606
|
-
|
|
2648
|
+
const listed = Array.map(yield* records.list(bound), ({ output, streamId }) => ({
|
|
2649
|
+
output,
|
|
2650
|
+
id: streamId
|
|
2651
|
+
}));
|
|
2652
|
+
for (const { output, id } of Array.sort(listed, streamOrder)) {
|
|
2607
2653
|
const definition = routes.get(output);
|
|
2608
|
-
const results = routed.get(output);
|
|
2609
2654
|
const invalid = (cause) => new ProjectionFault({
|
|
2610
2655
|
phase: "checkpoint",
|
|
2611
2656
|
reason: "invalid-record",
|
|
2612
|
-
message: `Cannot restore routed output ${output} at ${
|
|
2657
|
+
message: `Cannot restore routed output ${output} at ${id}`,
|
|
2613
2658
|
cause
|
|
2614
2659
|
});
|
|
2615
2660
|
const address = yield* Effect.try({
|
|
2616
|
-
try: () => definition?.restore(
|
|
2661
|
+
try: () => definition?.restore(id),
|
|
2617
2662
|
catch: invalid
|
|
2618
2663
|
});
|
|
2619
|
-
if (address === void 0
|
|
2620
|
-
results.push(yield* clean(address, Context.get(context, address.stream)));
|
|
2664
|
+
if (address === void 0) return yield* invalid();
|
|
2665
|
+
results.push(yield* clean(output, address, Context.get(context, address.stream)));
|
|
2621
2666
|
}
|
|
2622
2667
|
if (mode === "remove") yield* records.clear(bound);
|
|
2623
2668
|
}
|
|
2669
|
+
streams = results;
|
|
2624
2670
|
return yield* Effect.void;
|
|
2625
2671
|
})
|
|
2626
2672
|
}),
|
|
2627
|
-
|
|
2628
|
-
};
|
|
2629
|
-
return routes === void 0 ? retired : {
|
|
2630
|
-
...retired,
|
|
2631
|
-
routed: Record.fromEntries(routed)
|
|
2673
|
+
streams: Array.sort(streams, streamOrder)
|
|
2632
2674
|
};
|
|
2633
2675
|
});
|
|
2634
2676
|
};
|
|
@@ -2660,10 +2702,10 @@ const row = (stream, offset, closed) => DurableState.upsert("stream", closed ? {
|
|
|
2660
2702
|
* ```
|
|
2661
2703
|
*/
|
|
2662
2704
|
function wake(definition) {
|
|
2705
|
+
rejectProjectionVersion(definition.id, definition);
|
|
2663
2706
|
const input = definition.input;
|
|
2664
2707
|
return build("mapAccumBatch", {
|
|
2665
2708
|
id: definition.id,
|
|
2666
|
-
version: definition.version,
|
|
2667
2709
|
generation: definition.generation,
|
|
2668
2710
|
params: definition.params,
|
|
2669
2711
|
commit: producer,
|
|
@@ -2716,7 +2758,6 @@ var projection_exports = /* @__PURE__ */ __exportAll({
|
|
|
2716
2758
|
});
|
|
2717
2759
|
const key = (projection) => ({
|
|
2718
2760
|
id: projection.id,
|
|
2719
|
-
version: projection.version,
|
|
2720
2761
|
generation: projection.generation,
|
|
2721
2762
|
params: projection.params
|
|
2722
2763
|
});
|
|
@@ -2812,6 +2853,16 @@ const sqlFault = (id) => (cause) => new ProjectionFault({
|
|
|
2812
2853
|
message: `Cannot update the rows of ${id}`,
|
|
2813
2854
|
cause
|
|
2814
2855
|
});
|
|
2856
|
+
/** Statements of a client without an open transaction would outlive a rolled-back unit. */
|
|
2857
|
+
const transactionClient = (id) => Effect.gen(function* () {
|
|
2858
|
+
const sql = yield* SqlClient.SqlClient;
|
|
2859
|
+
if (Option.isNone(yield* Effect.serviceOption(sql.transactionService))) return yield* new ProjectionFault({
|
|
2860
|
+
phase: "process",
|
|
2861
|
+
reason: "unsupported-composition",
|
|
2862
|
+
message: `The SqlClient for the rows of ${id} has no open transaction. Use the storage owner's client inside its transaction.`
|
|
2863
|
+
});
|
|
2864
|
+
return sql;
|
|
2865
|
+
});
|
|
2815
2866
|
const apply = Effect.fn("StateTable.apply")(function* (sql, id, before, items) {
|
|
2816
2867
|
let epoch = before.epoch;
|
|
2817
2868
|
let open = before.snapshot_open;
|
|
@@ -2872,7 +2923,7 @@ const migrate = Effect.fn("StateTable.migrate")(function* (sql) {
|
|
|
2872
2923
|
) WITHOUT ROWID`;
|
|
2873
2924
|
});
|
|
2874
2925
|
const synchronize = (address) => Effect.gen(function* () {
|
|
2875
|
-
const
|
|
2926
|
+
const sql = yield* transactionClient(address.id);
|
|
2876
2927
|
const { wire } = yield* address.stream;
|
|
2877
2928
|
const unavailable = () => new ProjectionFault({
|
|
2878
2929
|
phase: "read",
|
|
@@ -2919,7 +2970,7 @@ const synchronize = (address) => Effect.gen(function* () {
|
|
|
2919
2970
|
return yield* save(sql, address.id, state);
|
|
2920
2971
|
}).pipe(Effect.catchTag("SqlError", sqlFault(address.id)), Effect.asVoid, Effect.withSpan("StateTable.sync"));
|
|
2921
2972
|
const applySlice = (address, slice) => Effect.gen(function* () {
|
|
2922
|
-
const
|
|
2973
|
+
const sql = yield* transactionClient(address.id);
|
|
2923
2974
|
const stored = yield* head(sql, address.id);
|
|
2924
2975
|
if (Option.isSome(stored) && stored.value.applied_offset === slice.from || Option.isNone(stored) && slice.from === ZERO_OFFSET) {
|
|
2925
2976
|
if (Option.isNone(stored)) yield* clearRows(sql, address.id);
|
|
@@ -2985,9 +3036,11 @@ const reads = (id, collections) => {
|
|
|
2985
3036
|
};
|
|
2986
3037
|
/** A State stream's declaration, kept current by the ordinary projection runners. */
|
|
2987
3038
|
function define(state, options = {}) {
|
|
3039
|
+
const id = `state-table/${state.key}`;
|
|
3040
|
+
rejectProjectionVersion(id, options);
|
|
2988
3041
|
validateAccepted(options.accepted, state.paramCodecs, [state.template], transaction);
|
|
2989
3042
|
const declaration = declare({
|
|
2990
|
-
id
|
|
3043
|
+
id,
|
|
2991
3044
|
params: state.paramCodecs,
|
|
2992
3045
|
inputs: {}
|
|
2993
3046
|
}, ({ decoded, ...key }) => {
|
|
@@ -3013,17 +3066,18 @@ function define(state, options = {}) {
|
|
|
3013
3066
|
parse: state.parse
|
|
3014
3067
|
});
|
|
3015
3068
|
}
|
|
3016
|
-
const clearIndex = (
|
|
3069
|
+
const clearIndex = (id) => Effect.gen(function* () {
|
|
3070
|
+
const sql = yield* transactionClient(id);
|
|
3071
|
+
yield* clearRows(sql, id);
|
|
3072
|
+
yield* sql`DELETE FROM streamsy_state_heads WHERE stream_id = ${id}`;
|
|
3073
|
+
}).pipe(Effect.catchTag("SqlError", sqlFault(id)));
|
|
3017
3074
|
/** Delete cached rows and their applied offset, even if the State stream has been removed. */
|
|
3018
3075
|
const clear = Effect.fn("StateTable.clear")(function* (table) {
|
|
3019
|
-
|
|
3020
|
-
yield* tx.withTransaction(clearIndex(tx.sql, table.inputs.state.id));
|
|
3076
|
+
yield* (yield* Transaction).withTransaction(clearIndex(table.inputs.state.id));
|
|
3021
3077
|
});
|
|
3022
3078
|
/** Discard the index and replay the State stream atomically, retaining the binding checkpoint. */
|
|
3023
3079
|
const rebuild = Effect.fn("StateTable.rebuild")(function* (table) {
|
|
3024
|
-
|
|
3025
|
-
const id = table.inputs.state.id;
|
|
3026
|
-
yield* tx.withTransaction(clearIndex(tx.sql, id).pipe(Effect.andThen(table.sync())));
|
|
3080
|
+
yield* (yield* Transaction).withTransaction(clearIndex(table.inputs.state.id).pipe(Effect.andThen(table.sync())));
|
|
3027
3081
|
});
|
|
3028
3082
|
/** Append a reset to the State stream. The next table run applies it. */
|
|
3029
3083
|
const reset = Effect.fn("StateTable.reset")(function* (table) {
|
|
@@ -3129,7 +3183,6 @@ const attach = (projection, options = {}) => {
|
|
|
3129
3183
|
const registry = yield* Effect.serviceOption(Registry);
|
|
3130
3184
|
const changed = Option.isNone(registry) ? Effect.void : yield* registerRunner(registry.value, {
|
|
3131
3185
|
id: projection.id,
|
|
3132
|
-
version: projection.version,
|
|
3133
3186
|
generation: projection.generation,
|
|
3134
3187
|
status: Effect.gen(function* () {
|
|
3135
3188
|
const state = host === void 0 ? {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@streamsy/projection",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.14.0",
|
|
4
4
|
"description": "Checkpointed Effect projections over one or more retained streams.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -37,7 +37,7 @@
|
|
|
37
37
|
"test:dialects": "bun --bun vitest run test/sql-dialects.test.ts"
|
|
38
38
|
},
|
|
39
39
|
"dependencies": {
|
|
40
|
-
"@streamsy/core": "^0.
|
|
40
|
+
"@streamsy/core": "^0.14.0"
|
|
41
41
|
},
|
|
42
42
|
"devDependencies": {
|
|
43
43
|
"@cloudflare/workers-types": "4.20251228.0",
|
|
@@ -47,8 +47,8 @@
|
|
|
47
47
|
"@effect/sql-sqlite-bun": "4.1.2",
|
|
48
48
|
"@effect/sql-sqlite-do": "4.1.2",
|
|
49
49
|
"@effect/vitest": "4.1.2",
|
|
50
|
-
"@streamsy/serve": "0.
|
|
51
|
-
"@streamsy/storage": "0.
|
|
50
|
+
"@streamsy/serve": "0.14.0",
|
|
51
|
+
"@streamsy/storage": "0.14.0",
|
|
52
52
|
"@types/bun": "^1.4.3",
|
|
53
53
|
"effect": "4.1.2",
|
|
54
54
|
"miniflare": "4.20260730.0",
|