@streamsy/projection 0.4.0 → 0.5.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/dist/{checkpoint-store-yQb6UDAb.d.ts → checkpoint-store-C8KxieX5.d.ts} +0 -8
- package/dist/{checkpoint-store-DacqeoMQ.js → checkpoint-store-CoDWYNbd.js} +13 -16
- package/dist/checkpoint.d.ts +1 -1
- package/dist/checkpoint.js +1 -1
- package/dist/index.d.ts +4 -55
- package/dist/index.js +68 -99
- package/dist/{rolldown-runtime-D7D4PA-g.js → rolldown-runtime-D8jQpDG7.js} +1 -2
- package/package.json +4 -4
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { Context, Effect, Layer, Option, Schema } from "effect";
|
|
2
2
|
import { CommitBoundary, StreamAddress, Streamsy } from "@streamsy/core";
|
|
3
3
|
import { OwnerRecords } from "@streamsy/core/internal";
|
|
4
|
-
//#region src/batch.d.ts
|
|
5
4
|
/** Declaration order of the map is the read and item order of every pass. */
|
|
6
5
|
type InputMap = Record<string, Streamsy.Address<unknown, unknown>>;
|
|
7
6
|
type ItemOf<Address> = Address extends StreamAddress.StreamAddress<infer A> ? A : never;
|
|
@@ -21,8 +20,6 @@ type Entry<Inputs extends InputMap> = { readonly [K in keyof Inputs]: {
|
|
|
21
20
|
readonly index: number;
|
|
22
21
|
}; }[keyof Inputs];
|
|
23
22
|
type InputServices<Inputs extends InputMap> = Inputs[keyof Inputs]["stream"]["Identifier"];
|
|
24
|
-
//#endregion
|
|
25
|
-
//#region src/fault.d.ts
|
|
26
23
|
declare const ProjectionFault_base: Schema.Class<ProjectionFault, Schema.TaggedStruct<"ProjectionFault", {
|
|
27
24
|
readonly phase: Schema.Literals<readonly ["load", "read", "pin", "process", "checkpoint"]>;
|
|
28
25
|
readonly reason: Schema.Literals<readonly ["history-unavailable", "identity-mismatch", "invalid-record", "invalid-source", "invalid-options", "invalid-output", "token-conflict", "range-unreproducible", "stale-epoch", "storage-failure", "unsupported-composition"]>;
|
|
@@ -34,8 +31,6 @@ declare const ProjectionFault_base: Schema.Class<ProjectionFault, Schema.TaggedS
|
|
|
34
31
|
declare class ProjectionFault extends ProjectionFault_base {
|
|
35
32
|
get retryable(): boolean;
|
|
36
33
|
}
|
|
37
|
-
//#endregion
|
|
38
|
-
//#region src/unit.d.ts
|
|
39
34
|
declare const Range: Schema.Struct<{
|
|
40
35
|
readonly from: Schema.String;
|
|
41
36
|
readonly nextOffset: Schema.String;
|
|
@@ -52,8 +47,6 @@ interface Unit<P = Record<string, string>> {
|
|
|
52
47
|
readonly ranges: Record<string, Range>;
|
|
53
48
|
readonly key: string;
|
|
54
49
|
}
|
|
55
|
-
//#endregion
|
|
56
|
-
//#region src/checkpoint-store.d.ts
|
|
57
50
|
declare const CheckpointRecord: Schema.Struct<{
|
|
58
51
|
readonly identity: Schema.Struct<{
|
|
59
52
|
readonly inputs: Schema.$Record<Schema.String, Schema.String>;
|
|
@@ -122,5 +115,4 @@ interface Identity {
|
|
|
122
115
|
readonly params: Record<string, string>;
|
|
123
116
|
readonly inputs: InputMap;
|
|
124
117
|
}
|
|
125
|
-
//#endregion
|
|
126
118
|
export { Slice as _, Identity as a, fromStore as c, Unit as d, ProjectionFault as f, ItemOf as g, InputServices as h, EncodedStore as i, recordKey as l, InputMap as m, CheckpointStore as n, Loaded as o, Entry as p, CheckpointStoreApi as r, ProjectionKey as s, CheckpointRecord as t, Range as u, Slices as v };
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { Context, Effect, Layer, Option, Schema, Semaphore } from "effect";
|
|
1
|
+
import { Array, Context, Effect, Layer, Option, Order, Record, Schema, Semaphore } from "effect";
|
|
2
2
|
import { CommitBoundary, ZERO_OFFSET, isStreamsFault } from "@streamsy/core";
|
|
3
3
|
import { OwnerRecords, attachOwner, ownerOf } from "@streamsy/core/internal";
|
|
4
|
-
|
|
4
|
+
// src/fault.ts
|
|
5
5
|
/** Phase says where the run stopped; a `checkpoint` failure means processing already completed. */
|
|
6
6
|
var ProjectionFault = class extends Schema.TaggedError()("ProjectionFault", {
|
|
7
7
|
phase: Schema.Literals([
|
|
@@ -32,8 +32,7 @@ var ProjectionFault = class extends Schema.TaggedError()("ProjectionFault", {
|
|
|
32
32
|
return this.reason === "storage-failure" && (!isStreamsFault(this.cause) || this.cause.retryable);
|
|
33
33
|
}
|
|
34
34
|
};
|
|
35
|
-
|
|
36
|
-
//#region src/unit.ts
|
|
35
|
+
// src/unit.ts
|
|
37
36
|
/** Length-safe encoding of inert string components; no persistence decoding here. */
|
|
38
37
|
const encodeKey = Schema.encodeSync(Schema.fromJsonString(Schema.Array(Schema.String)));
|
|
39
38
|
const Counter = Schema.Finite.check(Schema.isInt(), Schema.isGreaterThanOrEqualTo(0));
|
|
@@ -53,8 +52,8 @@ const PendingUnit = Schema.Struct({
|
|
|
53
52
|
seqs: Schema.optionalKey(Schema.Record(Schema.String, Counter))
|
|
54
53
|
});
|
|
55
54
|
/** Deterministic JSON: keys sorted, so equal maps always encode identically. */
|
|
56
|
-
const canonicalParams = (params) => JSON.stringify(
|
|
57
|
-
const canonicalRanges = (ranges) => JSON.stringify(
|
|
55
|
+
const canonicalParams = (params) => JSON.stringify(Record.fromEntries(Array.map(Array.sort(Record.keys(params), Order.String), (k) => [k, params[k]])));
|
|
56
|
+
const canonicalRanges = (ranges) => JSON.stringify(Record.fromEntries(Array.map(Array.sort(Record.keys(ranges), Order.String), (k) => [k, {
|
|
58
57
|
from: ranges[k]?.from,
|
|
59
58
|
nextOffset: ranges[k]?.nextOffset
|
|
60
59
|
}])));
|
|
@@ -77,8 +76,7 @@ const withParams = (unit, params) => ({
|
|
|
77
76
|
...unit,
|
|
78
77
|
params
|
|
79
78
|
});
|
|
80
|
-
|
|
81
|
-
//#region src/checkpoint-store.ts
|
|
79
|
+
// src/checkpoint-store.ts
|
|
82
80
|
const CheckpointRecord = Schema.Struct({
|
|
83
81
|
identity: Schema.Struct({ inputs: Schema.Record(Schema.String, Schema.String) }),
|
|
84
82
|
/** Input key to accepted offset; the next pass reads after these. */
|
|
@@ -203,10 +201,10 @@ const fromStore = (store) => {
|
|
|
203
201
|
remove: (key) => store.remove(recordKey(key))
|
|
204
202
|
};
|
|
205
203
|
};
|
|
206
|
-
const identityOf = (inputs) =>
|
|
204
|
+
const identityOf = (inputs) => Record.fromEntries(Array.map(Record.toEntries(inputs), ([name, address]) => [name, encodeKey([address.id, address.contentType])]));
|
|
207
205
|
const sameIdentity = (a, b) => {
|
|
208
|
-
const keys =
|
|
209
|
-
return keys.length ===
|
|
206
|
+
const keys = Record.keys(a);
|
|
207
|
+
return keys.length === Record.keys(b).length && Array.every(keys, (k) => a[k] === b[k]);
|
|
210
208
|
};
|
|
211
209
|
/** The stored record, or the unstored initial record for a fresh key. */
|
|
212
210
|
const restore = Effect.fn("Projection.restore")(function* (projection, owner) {
|
|
@@ -215,7 +213,7 @@ const restore = Effect.fn("Projection.restore")(function* (projection, owner) {
|
|
|
215
213
|
if (Option.isNone(loaded.record)) return {
|
|
216
214
|
record: {
|
|
217
215
|
identity,
|
|
218
|
-
inputs:
|
|
216
|
+
inputs: Record.fromEntries(Array.map(Record.keys(projection.inputs), (name) => [name, ZERO_OFFSET])),
|
|
219
217
|
adapters: {}
|
|
220
218
|
},
|
|
221
219
|
token: loaded.token,
|
|
@@ -226,7 +224,7 @@ const restore = Effect.fn("Projection.restore")(function* (projection, owner) {
|
|
|
226
224
|
reason: "identity-mismatch",
|
|
227
225
|
message: `Stored inputs differ from the declaration of ${projection.id}`
|
|
228
226
|
});
|
|
229
|
-
for (const name of
|
|
227
|
+
for (const name of Record.keys(projection.inputs)) if (loaded.record.value.inputs[name] === void 0) return yield* new ProjectionFault({
|
|
230
228
|
phase: "load",
|
|
231
229
|
reason: "invalid-record",
|
|
232
230
|
input: name,
|
|
@@ -239,11 +237,10 @@ const restore = Effect.fn("Projection.restore")(function* (projection, owner) {
|
|
|
239
237
|
};
|
|
240
238
|
});
|
|
241
239
|
/** Accepted offsets after a unit: every read input moves to its `nextOffset`. */
|
|
242
|
-
const advance = (inputs, ranges) =>
|
|
240
|
+
const advance = (inputs, ranges) => Record.fromEntries(Array.map(Record.toEntries(inputs), ([name, offset]) => [name, ranges[name]?.nextOffset ?? offset]));
|
|
243
241
|
/** Only the inputs that contributed items; the unit key and any pin come from these. */
|
|
244
|
-
const rangesOf = (slices) =>
|
|
242
|
+
const rangesOf = (slices) => Record.fromEntries(Array.map(Array.filter(Record.toEntries(slices), ([, slice]) => slice.items.length > 0), ([name, slice]) => [name, {
|
|
245
243
|
from: slice.from,
|
|
246
244
|
nextOffset: slice.nextOffset
|
|
247
245
|
}]));
|
|
248
|
-
//#endregion
|
|
249
246
|
export { rangesOf as a, canonicalParams as c, ProjectionFault as d, fromStore as i, unitOf as l, CheckpointStore as n, recordKey as o, advance as r, restore as s, CheckpointRecord as t, withParams as u };
|
package/dist/checkpoint.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { c as fromStore, i as EncodedStore, l as recordKey, n as CheckpointStore, o as Loaded, r as CheckpointStoreApi, s as ProjectionKey, t as CheckpointRecord } from "./checkpoint-store-
|
|
1
|
+
import { c as fromStore, i as EncodedStore, l as recordKey, n as CheckpointStore, o as Loaded, r as CheckpointStoreApi, s as ProjectionKey, t as CheckpointRecord } from "./checkpoint-store-C8KxieX5.js";
|
|
2
2
|
export { CheckpointRecord, CheckpointStore, type CheckpointStoreApi, type EncodedStore, type Loaded, type ProjectionKey, fromStore, recordKey };
|
package/dist/checkpoint.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export { t as CheckpointRecord, n as CheckpointStore, i as fromStore, o as recordKey } from "./checkpoint-store-
|
|
1
|
+
export { t as CheckpointRecord, n as CheckpointStore, i as fromStore, o as recordKey } from "./checkpoint-store-CoDWYNbd.js";
|
package/dist/index.d.ts
CHANGED
|
@@ -1,25 +1,21 @@
|
|
|
1
|
-
import { _ as Slice, a as Identity, d as Unit, f as ProjectionFault, g as ItemOf, h as InputServices, m as InputMap, n as CheckpointStore, o as Loaded, p as Entry, r as CheckpointStoreApi, s as ProjectionKey, t as CheckpointRecord, u as Range, v as Slices } from "./checkpoint-store-
|
|
2
|
-
import { Cause, Context, Data, Effect, Fiber, Layer, Option, Schedule, Schema, Scope } from "effect";
|
|
1
|
+
import { _ as Slice, a as Identity, d as Unit, f as ProjectionFault, g as ItemOf, h as InputServices, m as InputMap, n as CheckpointStore, o as Loaded, p as Entry, r as CheckpointStoreApi, s as ProjectionKey, t as CheckpointRecord, u as Range, v as Slices } from "./checkpoint-store-C8KxieX5.js";
|
|
2
|
+
import { Cause, Context, Data, Effect, Fiber, Layer, Option, Pipeable, Schedule, Schema, Scope } from "effect";
|
|
3
3
|
import { CommitBoundary, Decided, Decision, DurableState, OffsetMismatch, StreamAddress, Streamsy } from "@streamsy/core";
|
|
4
4
|
import { StreamRoute } from "@streamsy/core/protocol";
|
|
5
|
-
import { Pipeable } from "effect/Pipeable";
|
|
6
5
|
import { SqlClient } from "effect/sql";
|
|
7
|
-
//#region src/read.d.ts
|
|
8
6
|
interface RunOptions {
|
|
9
7
|
/** Maximum checkpoint transactions per call; absent means run to completion. */
|
|
10
8
|
readonly limit?: number;
|
|
11
9
|
}
|
|
12
10
|
/** Tagged items in declaration order, then stream order within each input. */
|
|
13
11
|
declare const items: <Inputs extends InputMap>(batch: Slices<Inputs>) => ReadonlyArray<Entry<Inputs>>;
|
|
14
|
-
//#endregion
|
|
15
|
-
//#region src/commit/model.d.ts
|
|
16
12
|
declare const TypeId = "~@streamsy/projection/Commit";
|
|
17
13
|
interface Variance<in Result, in Provides, out Requires> {
|
|
18
14
|
readonly result: (_: Result) => void;
|
|
19
15
|
readonly provides: (_: Provides) => void;
|
|
20
16
|
readonly requires: () => Requires;
|
|
21
17
|
}
|
|
22
|
-
interface Commit<in Result, in Provides = never, out Requires = never> extends Pipeable {
|
|
18
|
+
interface Commit<in Result, in Provides = never, out Requires = never> extends Pipeable.Pipeable {
|
|
23
19
|
readonly [TypeId]: Variance<Result, Provides, Requires>;
|
|
24
20
|
readonly name: string;
|
|
25
21
|
readonly guarantee: "exactly-once" | "effectively-once" | "at-least-once";
|
|
@@ -68,11 +64,7 @@ declare const Folded: Data.TaggedEnum.ConstructorFrom<Folded<unknown>, "_tag">,
|
|
|
68
64
|
* load must return the stored state. If run and the write are not atomic,
|
|
69
65
|
* declare at-least-once. The guarantee must describe the actual write. */
|
|
70
66
|
declare const make: <Result, Provides = never, Requires = never>(options: Omit<Commit<Result, Provides, Requires>, typeof TypeId | "pipe">) => Commit<Result, Provides, Requires>;
|
|
71
|
-
//#endregion
|
|
72
|
-
//#region src/commit/checkpoint.d.ts
|
|
73
67
|
declare const checkpoint: Commit<Folded<unknown>, never, CheckpointStore>;
|
|
74
|
-
//#endregion
|
|
75
|
-
//#region src/transaction.d.ts
|
|
76
68
|
declare const Transaction_base: Context.ServiceClass<Transaction, "@streamsy/projection/Transaction", {
|
|
77
69
|
readonly owner: object;
|
|
78
70
|
readonly sql: SqlClient.SqlClient;
|
|
@@ -86,20 +78,10 @@ export declare class Transaction extends Transaction_base {
|
|
|
86
78
|
*/
|
|
87
79
|
static readonly layer: Layer.Layer<Transaction, never, CommitBoundary | SqlClient.SqlClient>;
|
|
88
80
|
}
|
|
89
|
-
//#endregion
|
|
90
|
-
//#region src/commit/internal.d.ts
|
|
91
81
|
type Result$1 = void | Folded<unknown> | Emitted<OutputNames> | Accumulated<unknown, OutputNames>;
|
|
92
|
-
//#endregion
|
|
93
|
-
//#region src/commit/transaction.d.ts
|
|
94
82
|
declare const transaction: Commit<Result$1, SqlClient.SqlClient, CheckpointStore | Transaction>;
|
|
95
|
-
//#endregion
|
|
96
|
-
//#region src/commit/producer.d.ts
|
|
97
83
|
declare const producer: Commit<Accumulated<unknown, Readonly<Record<string, never>>> | Emitted<Readonly<Record<string, never>>>, never, CheckpointStore>;
|
|
98
|
-
//#endregion
|
|
99
|
-
//#region src/commit/at-least-once.d.ts
|
|
100
84
|
declare const atLeastOnce: Commit<void, never, CheckpointStore>;
|
|
101
|
-
//#endregion
|
|
102
|
-
//#region src/commit/compensation.d.ts
|
|
103
85
|
declare const withCompensation: {
|
|
104
86
|
<Result, R>(f: (result: Result, cause: Cause.Cause<ProjectionFault | HandlerFailed>, unit: Unit) => Effect.Effect<void, never, R>): <P, Q>(self: Commit<Result, P, Q>) => Commit<Result, P, Q | R>;
|
|
105
87
|
<Result, P, Q, R>(self: Commit<Result, P, Q>, f: (result: Result, cause: Cause.Cause<ProjectionFault | HandlerFailed>, unit: Unit) => Effect.Effect<void, never, R>): Commit<Result, P, Q | R>;
|
|
@@ -107,17 +89,13 @@ declare const withCompensation: {
|
|
|
107
89
|
declare namespace commit_d_exports {
|
|
108
90
|
export { Accumulated, Commit, Emitted, Folded, HandlerFailed, OutputBatch, Pending, TypeId, Variance, atLeastOnce, checkpoint, make, producer, transaction, withCompensation };
|
|
109
91
|
}
|
|
110
|
-
//#endregion
|
|
111
|
-
//#region src/outputs.d.ts
|
|
112
92
|
type RoutedMap = Inputs;
|
|
113
93
|
type Item<T> = AddressOf<T> extends {
|
|
114
94
|
readonly collections: infer C extends StreamAddress.Collections;
|
|
115
|
-
} ? DurableState.
|
|
95
|
+
} ? DurableState.Entry<C> : AddressOf<T> extends {
|
|
116
96
|
readonly codec: Schema.Codec<infer A, any, any, any>;
|
|
117
97
|
} ? A : never;
|
|
118
98
|
type Services<O extends RoutedMap> = { [K in keyof O]: AddressOf<O[K]> extends Streamsy.Address<any, infer R> ? R : never; }[keyof O];
|
|
119
|
-
//#endregion
|
|
120
|
-
//#region src/state.d.ts
|
|
121
99
|
type StateSchema<A, I = unknown> = Schema.Codec<A, I>;
|
|
122
100
|
/** A value, or a function of the decoded params the projection was bound with. */
|
|
123
101
|
type Initial<A, P = Record<string, string>> = A | ((params: P) => A);
|
|
@@ -131,8 +109,6 @@ interface BoundState<A> {
|
|
|
131
109
|
readonly schema: StateSchema<A>;
|
|
132
110
|
readonly initial: () => A;
|
|
133
111
|
}
|
|
134
|
-
//#endregion
|
|
135
|
-
//#region src/executable.d.ts
|
|
136
112
|
/** Common bound declaration consumed by the kernel. */
|
|
137
113
|
interface Executable<I extends InputMap, E, R, Q, Result, P = never, S = unknown, OR = never> extends Identity {
|
|
138
114
|
readonly inputs: I;
|
|
@@ -142,8 +118,6 @@ interface Executable<I extends InputMap, E, R, Q, Result, P = never, S = unknown
|
|
|
142
118
|
readonly prepare?: (batch: Slices<I>, unit: Unit) => Effect.Effect<unknown, E, Q>;
|
|
143
119
|
readonly process: (batch: Slices<I>, unit: Unit, encoded?: string, prepared?: unknown) => Effect.Effect<Result, E, R | P>;
|
|
144
120
|
}
|
|
145
|
-
//#endregion
|
|
146
|
-
//#region src/declaration.d.ts
|
|
147
121
|
/** A stream declaration. `.at(params)` resolves it with the projection params. */
|
|
148
122
|
type StreamDeclaration = StreamRoute.StreamRoute<any, any> & Streamsy.Any & {
|
|
149
123
|
readonly at: (params: any) => Streamsy.Address<any, any>;
|
|
@@ -191,28 +165,20 @@ type OutputDeclaration<C extends StreamRoute.ParamCodecs, I extends Inputs, O ex
|
|
|
191
165
|
};
|
|
192
166
|
};
|
|
193
167
|
type AccumulatingDeclaration<C extends StreamRoute.ParamCodecs, I extends Inputs, O extends RoutedMap, A, E, R, P, Q> = OutputDeclaration<C, I, O, E, R, Accumulated<A, O>, P, Q, A>;
|
|
194
|
-
//#endregion
|
|
195
|
-
//#region src/step.d.ts
|
|
196
168
|
type Step<X, E = never, R = never> = X | Effect.Effect<X, E, R>;
|
|
197
169
|
/** Each output keeps its item type here; missing keys emit nothing. */
|
|
198
170
|
type Emits<O extends RoutedMap> = { readonly [K in keyof O]?: ReadonlyArray<Item<O[K]>>; };
|
|
199
|
-
//#endregion
|
|
200
|
-
//#region src/fold.d.ts
|
|
201
171
|
/** Fold one entry at a time. The default Commit saves state and offsets in one write. */
|
|
202
172
|
declare function fold<C extends StreamRoute.ParamCodecs, I extends Inputs, A, SI, E = never, R = never, P = never, Q = CheckpointStore>(definition: Common<C, I> & {
|
|
203
173
|
readonly state: StateDefinition<A, SI, StreamRoute.Params<C>>;
|
|
204
174
|
readonly commit?: Committing<Folded<NoInfer<A>>, P, Q>["commit"];
|
|
205
175
|
readonly fold: (state: NoInfer<A>, entry: Entry<Addresses<I>>, unit: Unit<StreamRoute.Params<C>>) => Step<A, E, R>;
|
|
206
176
|
}): StatefulDeclaration<C, I, A, E, R, Folded<A>, P, Q>;
|
|
207
|
-
//#endregion
|
|
208
|
-
//#region src/process.d.ts
|
|
209
177
|
/** Run side effects once per unit. prepare runs at least once, outside the transaction. */
|
|
210
178
|
declare function process<C extends StreamRoute.ParamCodecs, I extends Inputs, E = never, R = never, P = never, Q = never, Prepared = void, PE = never, PR = never>(definition: Common<C, I> & Committing<void, P, Q> & {
|
|
211
179
|
readonly prepare?: (batch: Slices<Addresses<I>>, unit: Unit<StreamRoute.Params<C>>) => Effect.Effect<Prepared, PE, PR>;
|
|
212
180
|
readonly process: (batch: Slices<Addresses<I>>, unit: Unit<StreamRoute.Params<C>>, prepared: Prepared) => Effect.Effect<void, E, R>;
|
|
213
181
|
}): RunningDeclaration<C, I, E | PE, R, void, P, Q | PR>;
|
|
214
|
-
//#endregion
|
|
215
|
-
//#region src/emit.d.ts
|
|
216
182
|
/** Emit zero or more output items for each input entry, in entry order. */
|
|
217
183
|
declare function emit<C extends StreamRoute.ParamCodecs, I extends Inputs, const O extends RoutedMap, X extends Emits<O>, E = never, R = never, P = never, Q = never>(definition: Common<C, I> & Committing<Emitted<NoInfer<O>>, P, Q> & {
|
|
218
184
|
readonly outputs: O & OutputsWithin<C, O>;
|
|
@@ -220,8 +186,6 @@ declare function emit<C extends StreamRoute.ParamCodecs, I extends Inputs, const
|
|
|
220
186
|
readonly undeclaredOutput: Exclude<keyof X, keyof O>;
|
|
221
187
|
});
|
|
222
188
|
}): OutputDeclaration<C, I, O, E, R, Emitted<O>, P, Q>;
|
|
223
|
-
//#endregion
|
|
224
|
-
//#region src/map-accum.d.ts
|
|
225
189
|
type Definition<C extends StreamRoute.ParamCodecs, I extends Inputs, O extends RoutedMap, A, SI, P, Q> = Common<C, I> & Committing<Accumulated<NoInfer<A>, NoInfer<O>>, P, Q> & {
|
|
226
190
|
readonly outputs: O & OutputsWithin<C, O>;
|
|
227
191
|
readonly state: StateDefinition<A, SI, StreamRoute.Params<C>>;
|
|
@@ -238,8 +202,6 @@ declare function mapAccumBatch<C extends StreamRoute.ParamCodecs, I extends Inpu
|
|
|
238
202
|
readonly undeclaredOutput: Exclude<keyof X, keyof O>;
|
|
239
203
|
});
|
|
240
204
|
}): AccumulatingDeclaration<C, I, O, A, E, R, P, Q>;
|
|
241
|
-
//#endregion
|
|
242
|
-
//#region src/snapshot.d.ts
|
|
243
205
|
/** The accepted offset of each input, keyed by input name. The next pass reads after these. */
|
|
244
206
|
type Offsets<Inputs extends InputMap> = { readonly [K in keyof Inputs]: string; };
|
|
245
207
|
/** The state and the offsets it is at. `state` is `undefined` when the declaration has no `state:`. */
|
|
@@ -258,8 +220,6 @@ declare const read: <Inputs extends InputMap, S = never, Q = never>(projection:
|
|
|
258
220
|
readonly state?: BoundState<S>;
|
|
259
221
|
readonly commit: Pick<Commit<never, never, Q>, "load">;
|
|
260
222
|
}) => Effect.Effect<Snapshot<StateValue<S>, Inputs>, ProjectionFault, Q>;
|
|
261
|
-
//#endregion
|
|
262
|
-
//#region src/run.d.ts
|
|
263
223
|
interface Progress<Inputs extends InputMap = InputMap> {
|
|
264
224
|
readonly status: "progress" | "caught-up" | "source-closed" | "limit-reached";
|
|
265
225
|
readonly units: number;
|
|
@@ -270,8 +230,6 @@ interface Progress<Inputs extends InputMap = InputMap> {
|
|
|
270
230
|
declare const pass: <Inputs extends InputMap, E, R, Q, Result, P, OR = never>(projection: Executable<Inputs, E, R, Q, Result, P, unknown, OR>) => Effect.Effect<Progress<Inputs>, E | ProjectionFault, OR | Q | InputServices<Inputs> | Exclude<R, P>>;
|
|
271
231
|
/** Repeats pass until caught up, closed, or limited. Use one writer per key. */
|
|
272
232
|
declare const run: <Inputs extends InputMap, E, R, Q, Result, P, OR = never>(projection: Executable<Inputs, E, R, Q, Result, P, unknown, OR>, options?: RunOptions | undefined) => Effect.Effect<Progress<Inputs>, E | ProjectionFault, OR | Q | InputServices<Inputs> | Exclude<R, P>>;
|
|
273
|
-
//#endregion
|
|
274
|
-
//#region src/follow.d.ts
|
|
275
233
|
interface FollowOptions extends RunOptions {
|
|
276
234
|
/** Upper bound on one wake wait; default 30000. */
|
|
277
235
|
readonly repairIntervalMs?: number;
|
|
@@ -285,8 +243,6 @@ interface FollowOptions extends RunOptions {
|
|
|
285
243
|
* and replaced once per `repairIntervalMs` until an input changes.
|
|
286
244
|
*/
|
|
287
245
|
declare const follow: <Inputs extends InputMap, E, R, Q, Result, P, OR = never>(projection: Executable<Inputs, E, R, Q, Result, P, unknown, OR>, options?: FollowOptions | undefined) => Effect.Effect<Fiber.Fiber<Progress<Inputs>, E | ProjectionFault>, ProjectionFault, OR | Q | Scope.Scope | InputServices<Inputs> | Exclude<R, P>>;
|
|
288
|
-
//#endregion
|
|
289
|
-
//#region src/current.d.ts
|
|
290
246
|
/** A bound value projection. Output items from its adapter are discarded. */
|
|
291
247
|
interface CurrentProjection<Inputs extends InputMap, S, E, R, Q> extends Identity {
|
|
292
248
|
readonly inputs: Inputs;
|
|
@@ -303,8 +259,6 @@ interface CurrentProjection<Inputs extends InputMap, S, E, R, Q> extends Identit
|
|
|
303
259
|
* Fold steps must be replayable. Pending ranges are read from committed offsets.
|
|
304
260
|
*/
|
|
305
261
|
declare const current: <Inputs extends InputMap, S, E, R, Q>(projection: CurrentProjection<Inputs, S, E, R, Q>) => Effect.Effect<Snapshot<S, Inputs>, E | ProjectionFault, Q | R | InputServices<Inputs>>;
|
|
306
|
-
//#endregion
|
|
307
|
-
//#region src/decide.d.ts
|
|
308
262
|
interface DecideSpec<Inputs extends InputMap, K extends keyof Inputs, S, B, E, R> {
|
|
309
263
|
/** The input to append to. Its offset in the snapshot is the CAS token. */
|
|
310
264
|
readonly input: K;
|
|
@@ -335,8 +289,6 @@ declare class LostRace {
|
|
|
335
289
|
readonly mismatch: OffsetMismatch;
|
|
336
290
|
constructor(mismatch: OffsetMismatch);
|
|
337
291
|
}
|
|
338
|
-
//#endregion
|
|
339
|
-
//#region src/forget.d.ts
|
|
340
292
|
/**
|
|
341
293
|
* Deletes the record. Stop the follower first, or use a new generation.
|
|
342
294
|
* Delete stream outputs first, because forgetting restarts their producer sequences.
|
|
@@ -350,9 +302,6 @@ declare namespace projection_d_exports {
|
|
|
350
302
|
declare const key: (projection: Identity) => ProjectionKey;
|
|
351
303
|
/** Runs each tagged entry in order and discards its success value. */
|
|
352
304
|
declare const each: <Inputs extends InputMap, E, R, P = Record<string, string>>(handle: (entry: Entry<Inputs>, unit: Unit<P>) => Effect.Effect<unknown, E, R>) => ((batch: Slices<Inputs>, unit: Unit<P>) => Effect.Effect<void, E, R>);
|
|
353
|
-
//#endregion
|
|
354
|
-
//#region src/retry.d.ts
|
|
355
305
|
/** Default storage-failure retry for follow; an explicit options.retry takes precedence. */
|
|
356
306
|
export declare const ProjectionRetry: Context.Reference<Schedule.Schedule<unknown, unknown, never, never>>;
|
|
357
|
-
//#endregion
|
|
358
307
|
export { type Accumulated, CheckpointRecord, CheckpointStore, type CheckpointStoreApi, commit_d_exports as Commit, type CurrentProjection, type DecideSpec, type Declaration, type Emits, type Emitted, type Entry, type Folded, type FollowOptions, type Identity, type Initial, type InputMap, type ItemOf, type Loaded, type Offsets, type OutputBatch, type Progress, projection_d_exports as Projection, ProjectionFault, type ProjectionKey, type Range, type RunOptions, type Slice, type Slices, type Snapshot, type StateDefinition, type StateSchema, type Step, type Unit };
|
package/dist/index.js
CHANGED
|
@@ -1,14 +1,11 @@
|
|
|
1
|
-
import { t as __exportAll } from "./rolldown-runtime-
|
|
2
|
-
import { a as rangesOf, c as canonicalParams, d as ProjectionFault, l as unitOf, n as CheckpointStore, r as advance, s as restore, t as CheckpointRecord, u as withParams } from "./checkpoint-store-
|
|
3
|
-
import { Array
|
|
1
|
+
import { t as __exportAll } from "./rolldown-runtime-D8jQpDG7.js";
|
|
2
|
+
import { a as rangesOf, c as canonicalParams, d as ProjectionFault, l as unitOf, n as CheckpointStore, r as advance, s as restore, t as CheckpointRecord, u as withParams } from "./checkpoint-store-CoDWYNbd.js";
|
|
3
|
+
import { Array, Context, Data, Duration, Effect, Function, Layer, Option, Pipeable, Predicate, Record, Ref, Result, Schedule, Schema } from "effect";
|
|
4
4
|
import { CommitBoundary, DurableState, Offset, ZERO_OFFSET, compare, isStreamsFault } from "@streamsy/core";
|
|
5
5
|
import { Streams, StreamsWriter } from "@streamsy/core/protocol";
|
|
6
|
-
import { pipeArguments } from "effect/Pipeable";
|
|
7
6
|
import { CurrentOwner, isForeignOwner, ownerOf } from "@streamsy/core/internal";
|
|
8
7
|
import { SqlClient } from "effect/sql";
|
|
9
|
-
|
|
10
|
-
import { isReadonlyArrayNonEmpty } from "effect/Array";
|
|
11
|
-
//#region src/batch.ts
|
|
8
|
+
// src/batch.ts
|
|
12
9
|
/** An input that was skipped this pass: nothing read, nothing known beyond its cursor. */
|
|
13
10
|
const emptySlice = (from) => ({
|
|
14
11
|
from,
|
|
@@ -18,9 +15,8 @@ const emptySlice = (from) => ({
|
|
|
18
15
|
closed: false
|
|
19
16
|
});
|
|
20
17
|
/** Iteration preserves the union of the declared input stream requirements. */
|
|
21
|
-
const inputEntries = (inputs) =>
|
|
22
|
-
|
|
23
|
-
//#region src/read.ts
|
|
18
|
+
const inputEntries = (inputs) => Record.toEntries(inputs);
|
|
19
|
+
// src/read.ts
|
|
24
20
|
const validateOptions = (options) => Effect.suspend(() => options.limit === void 0 || Number.isSafeInteger(options.limit) && options.limit > 0 ? Effect.void : Effect.fail(new ProjectionFault({
|
|
25
21
|
phase: "load",
|
|
26
22
|
reason: "invalid-options",
|
|
@@ -159,29 +155,28 @@ const readInputs = Effect.fn("Projection.readInputs")(function* (inputs, offsets
|
|
|
159
155
|
/** Tagged items in declaration order, then stream order within each input. */
|
|
160
156
|
const items = (batch) => {
|
|
161
157
|
const out = [];
|
|
162
|
-
for (const [input, slice] of
|
|
158
|
+
for (const [input, slice] of Record.toEntries(batch)) for (const item of slice.items) out.push({
|
|
163
159
|
input,
|
|
164
160
|
item,
|
|
165
161
|
index: out.length
|
|
166
162
|
});
|
|
167
163
|
return out;
|
|
168
164
|
};
|
|
169
|
-
|
|
170
|
-
//#region src/outputs.ts
|
|
165
|
+
// src/outputs.ts
|
|
171
166
|
/** Resolve routes just like inputs, retaining the route codec and collection names. */
|
|
172
167
|
const resolveOutputs = (outputs, params, inputs) => {
|
|
173
168
|
const streams = addressesOf(outputs, params);
|
|
174
|
-
const ids =
|
|
169
|
+
const ids = Array.map(Record.values(streams), (address) => address.id);
|
|
175
170
|
if (new Set(ids).size !== ids.length) throw new RangeError("Two outputs may not name the same stream");
|
|
176
|
-
const inputIds = new Set(
|
|
177
|
-
if (
|
|
178
|
-
if (
|
|
171
|
+
const inputIds = new Set(Array.map(Record.values(inputs), (address) => address.id));
|
|
172
|
+
if (Array.some(ids, (id) => inputIds.has(id))) throw new RangeError("An output may not name an input stream");
|
|
173
|
+
if (Array.some(Record.values(streams), (address) => !Predicate.isTagged(address, "Json"))) throw new RangeError("Projection outputs require JSON or state routes");
|
|
179
174
|
return streams;
|
|
180
175
|
};
|
|
181
176
|
const collect = (outputs, buffers, emitted) => Effect.gen(function* () {
|
|
182
|
-
for (const [name, entries] of
|
|
177
|
+
for (const [name, entries] of Record.toEntries(emitted)) {
|
|
183
178
|
const output = outputs[name];
|
|
184
|
-
if (!
|
|
179
|
+
if (!Record.has(outputs, name) || output === void 0 || !Array.isArray(entries)) return yield* new ProjectionFault({
|
|
185
180
|
phase: "process",
|
|
186
181
|
reason: "invalid-output",
|
|
187
182
|
message: `Invalid output array ${name}`
|
|
@@ -225,8 +220,7 @@ const appendPinned = Effect.fn("Projection.appendPinned")(function* (output, wir
|
|
|
225
220
|
StreamBusy: () => pin$1("invalid-output", `Output ${output.id} is busy`)
|
|
226
221
|
}));
|
|
227
222
|
});
|
|
228
|
-
|
|
229
|
-
//#region src/commit/range.ts
|
|
223
|
+
// src/commit/range.ts
|
|
230
224
|
const RangeKey = Symbol.for("@streamsy/projection/internal/Range");
|
|
231
225
|
const extend = (commit) => Object.defineProperty(commit, RangeKey, {
|
|
232
226
|
value: "extend",
|
|
@@ -244,8 +238,7 @@ const inherit = (source, target) => {
|
|
|
244
238
|
const PrepareKey = Symbol.for("@streamsy/projection/internal/Prepare");
|
|
245
239
|
const withPrepare = (commit) => Object.defineProperty(commit, PrepareKey, { value: true });
|
|
246
240
|
const supportsPrepare = (commit) => PrepareKey in commit && commit[PrepareKey] === true;
|
|
247
|
-
|
|
248
|
-
//#region src/commit/model.ts
|
|
241
|
+
// src/commit/model.ts
|
|
249
242
|
const TypeId = "~@streamsy/projection/Commit";
|
|
250
243
|
/** Opaque to Commit authors; the kernel restores the original typed handler error. */
|
|
251
244
|
var HandlerFailed = class extends Schema.TaggedError()("HandlerFailed", { error: Schema.Unknown }) {};
|
|
@@ -265,11 +258,10 @@ const make = (options) => extend({
|
|
|
265
258
|
}
|
|
266
259
|
},
|
|
267
260
|
pipe() {
|
|
268
|
-
return pipeArguments(this, arguments);
|
|
261
|
+
return Pipeable.pipeArguments(this, arguments);
|
|
269
262
|
}
|
|
270
263
|
});
|
|
271
|
-
|
|
272
|
-
//#region src/commit/internal.ts
|
|
264
|
+
// src/commit/internal.ts
|
|
273
265
|
const DetailsKey = Symbol.for("@streamsy/projection/internal/Pending");
|
|
274
266
|
const setDetails = (pending, value) => Object.defineProperty(pending, DetailsKey, { value });
|
|
275
267
|
const detailsOf = (pending) => {
|
|
@@ -286,11 +278,10 @@ const withState = (next, result) => result !== void 0 && "encoded" in result ? {
|
|
|
286
278
|
} : next;
|
|
287
279
|
const beforeRecord = (pending) => Option.getOrElse(pending.loaded.record, () => ({
|
|
288
280
|
...pending.next,
|
|
289
|
-
inputs:
|
|
281
|
+
inputs: Record.fromEntries(Array.map(Record.toEntries(pending.next.inputs), ([name, offset]) => [name, pending.unit.ranges[name]?.from ?? offset]))
|
|
290
282
|
}));
|
|
291
283
|
const settle = (pending, result) => CheckpointStore.use((store) => store.compareAndSet(pending.key, pending.loaded.token, withState(pending.next, result))).pipe(Effect.asVoid);
|
|
292
|
-
|
|
293
|
-
//#region src/commit/checkpoint.ts
|
|
284
|
+
// src/commit/checkpoint.ts
|
|
294
285
|
const checkpoint = make({
|
|
295
286
|
name: "checkpoint",
|
|
296
287
|
guarantee: "exactly-once",
|
|
@@ -298,8 +289,7 @@ const checkpoint = make({
|
|
|
298
289
|
remove,
|
|
299
290
|
apply: (pending) => Effect.flatMap(pending.run, (result) => settle(pending, result))
|
|
300
291
|
});
|
|
301
|
-
|
|
302
|
-
//#region src/transaction.ts
|
|
292
|
+
// src/transaction.ts
|
|
303
293
|
/** The SQL transaction and stream owner used by Commit.transaction. */
|
|
304
294
|
var Transaction = class extends Context.Service()("@streamsy/projection/Transaction") {
|
|
305
295
|
/**
|
|
@@ -321,8 +311,7 @@ var Transaction = class extends Context.Service()("@streamsy/projection/Transact
|
|
|
321
311
|
};
|
|
322
312
|
}));
|
|
323
313
|
};
|
|
324
|
-
|
|
325
|
-
//#region src/commit/transaction.ts
|
|
314
|
+
// src/commit/transaction.ts
|
|
326
315
|
const transaction = withPrepare(make({
|
|
327
316
|
name: "transaction",
|
|
328
317
|
guarantee: "exactly-once",
|
|
@@ -332,7 +321,7 @@ const transaction = withPrepare(make({
|
|
|
332
321
|
const tx = yield* Transaction;
|
|
333
322
|
const store = yield* CheckpointStore;
|
|
334
323
|
const outputs = detailsOf(pending).outputs;
|
|
335
|
-
for (const { address, service } of
|
|
324
|
+
for (const { address, service } of Record.values(outputs)) if (service.owner !== tx.owner) return yield* new ProjectionFault({
|
|
336
325
|
phase: "load",
|
|
337
326
|
reason: "unsupported-composition",
|
|
338
327
|
message: `Output ${address.id} does not share the transaction owner`
|
|
@@ -350,8 +339,8 @@ const transaction = withPrepare(make({
|
|
|
350
339
|
reason: "unsupported-composition",
|
|
351
340
|
message: defect.message
|
|
352
341
|
})) : Effect.die(defect)));
|
|
353
|
-
if (result !== void 0 && "outputs" in result) for (const [name, output] of
|
|
354
|
-
if (!Array
|
|
342
|
+
if (result !== void 0 && "outputs" in result) for (const [name, output] of Record.toEntries(result.outputs)) {
|
|
343
|
+
if (!Array.isReadonlyArrayNonEmpty(output.items)) continue;
|
|
355
344
|
const resolved = outputs[name];
|
|
356
345
|
if (resolved === void 0) return yield* new ProjectionFault({
|
|
357
346
|
phase: "process",
|
|
@@ -371,10 +360,9 @@ const transaction = withPrepare(make({
|
|
|
371
360
|
return yield* Effect.void;
|
|
372
361
|
})
|
|
373
362
|
}));
|
|
374
|
-
|
|
375
|
-
//#region src/commit/producer.ts
|
|
363
|
+
// src/commit/producer.ts
|
|
376
364
|
/** The pinned tuple identity: the params join the id so two parameterisations never share a sequence. */
|
|
377
|
-
const producerId = (id, version, params) => `${id}/v${version}${
|
|
365
|
+
const producerId = (id, version, params) => `${id}/v${version}${Record.keys(params).length === 0 ? "" : `/${canonicalParams(params)}`}`;
|
|
378
366
|
/** The pin write is the last save before the append, so its conflict is a pin failure. */
|
|
379
367
|
const asPin = (fault) => fault.phase === "checkpoint" && fault.reason === "token-conflict" ? new ProjectionFault({
|
|
380
368
|
phase: "pin",
|
|
@@ -393,7 +381,7 @@ const producer = pin(make({
|
|
|
393
381
|
reason: "invalid-record",
|
|
394
382
|
message: "A producer pin must include output sequences"
|
|
395
383
|
});
|
|
396
|
-
for (const [name, position] of
|
|
384
|
+
for (const [name, position] of Record.toEntries(loaded.record.value.adapters.outputs ?? {})) if (position.epoch !== key.generation) return yield* new ProjectionFault({
|
|
397
385
|
phase: "load",
|
|
398
386
|
reason: "stale-epoch",
|
|
399
387
|
message: `Stored epoch ${position.epoch} of ${key.id}/${name} is not generation ${key.generation}`
|
|
@@ -412,30 +400,30 @@ const producer = pin(make({
|
|
|
412
400
|
const streams = detailsOf(request).outputs;
|
|
413
401
|
const ranges = request.unit.ranges;
|
|
414
402
|
const positions = { ...adapters.outputs };
|
|
415
|
-
for (const [name, position] of
|
|
403
|
+
for (const [name, position] of Record.toEntries(positions)) if (position.epoch !== projection.generation) return yield* new ProjectionFault({
|
|
416
404
|
phase: "load",
|
|
417
405
|
reason: "stale-epoch",
|
|
418
406
|
message: `Stored epoch ${position.epoch} of ${projection.id}/${name} is not generation ${projection.generation}`
|
|
419
407
|
});
|
|
420
408
|
const pending = before.record.pending;
|
|
421
409
|
const result = yield* request.run;
|
|
422
|
-
const processed = { items:
|
|
423
|
-
const seqs = pending === void 0 ?
|
|
424
|
-
for (const name of
|
|
410
|
+
const processed = { items: Record.fromEntries(Array.map(Record.toEntries(result.outputs), ([name, output]) => [name, output.items])) };
|
|
411
|
+
const seqs = pending === void 0 ? Record.fromEntries(Array.map(Array.filter(Record.keys(streams), (name) => (processed.items[name]?.length ?? 0) > 0), (name) => [name, positions[name]?.nextSeq ?? 0])) : pending.seqs ?? {};
|
|
412
|
+
for (const name of Record.keys(seqs)) if (streams[name] === void 0 || (processed.items[name]?.length ?? 0) === 0) return yield* new ProjectionFault({
|
|
425
413
|
phase: "process",
|
|
426
414
|
reason: "invalid-output",
|
|
427
415
|
message: `Cannot reproduce pinned output ${name}`
|
|
428
416
|
});
|
|
429
417
|
if (pending !== void 0) {
|
|
430
|
-
for (const name of
|
|
418
|
+
for (const name of Record.keys(streams)) if ((processed.items[name]?.length ?? 0) > 0 && seqs[name] === void 0) return yield* new ProjectionFault({
|
|
431
419
|
phase: "process",
|
|
432
420
|
reason: "invalid-output",
|
|
433
421
|
message: `Cannot reproduce pinned unit: output ${name} has items that were not pinned`
|
|
434
422
|
});
|
|
435
423
|
}
|
|
436
424
|
let token = before.token;
|
|
437
|
-
if (pending === void 0 &&
|
|
438
|
-
for (const [name, output] of
|
|
425
|
+
if (pending === void 0 && Record.keys(seqs).length > 0) {
|
|
426
|
+
for (const [name, output] of Record.toEntries(streams)) {
|
|
439
427
|
if (seqs[name] === void 0) continue;
|
|
440
428
|
const { wire, owner: outputOwner } = output.service;
|
|
441
429
|
if ((yield* wire.head(output.address.id).pipe(Effect.catchTag("StreamNotFound", (cause) => outputOwner !== ownerOf(owner.load) ? Effect.fail(cause) : Streams.create(output.address).pipe(Effect.provideService(StreamsWriter, wire), Effect.andThen(wire.head(output.address.id)))), Effect.mapError((cause) => new ProjectionFault({
|
|
@@ -459,7 +447,7 @@ const producer = pin(make({
|
|
|
459
447
|
adapters: { outputs: { ...positions } },
|
|
460
448
|
pending: {
|
|
461
449
|
seqs,
|
|
462
|
-
ranges:
|
|
450
|
+
ranges: Record.fromEntries(Array.map(Record.toEntries(ranges), ([name, range]) => [name, {
|
|
463
451
|
from: range.from,
|
|
464
452
|
nextOffset: range.nextOffset,
|
|
465
453
|
count: detailsOf(request).ranges[name]?.count ?? 0
|
|
@@ -469,10 +457,10 @@ const producer = pin(make({
|
|
|
469
457
|
if (before.record.state !== void 0) Object.assign(pinned, { state: before.record.state });
|
|
470
458
|
token = yield* owner.compareAndSet(projection, before.token, pinned).pipe(Effect.mapError(asPin));
|
|
471
459
|
}
|
|
472
|
-
for (const [name, output] of
|
|
460
|
+
for (const [name, output] of Record.toEntries(streams)) {
|
|
473
461
|
const seq = seqs[name];
|
|
474
462
|
const items = processed.items[name] ?? [];
|
|
475
|
-
if (seq === void 0 || !Array
|
|
463
|
+
if (seq === void 0 || !Array.isReadonlyArrayNonEmpty(items)) continue;
|
|
476
464
|
yield* appendPinned(output.address, output.service.wire, items, {
|
|
477
465
|
producerId: producerId(projection.id, projection.version, projection.params),
|
|
478
466
|
producerEpoch: projection.generation,
|
|
@@ -486,7 +474,7 @@ const producer = pin(make({
|
|
|
486
474
|
const record = {
|
|
487
475
|
identity,
|
|
488
476
|
inputs: request.next.inputs,
|
|
489
|
-
adapters:
|
|
477
|
+
adapters: Record.keys(positions).length === 0 ? adapters : { outputs: positions }
|
|
490
478
|
};
|
|
491
479
|
yield* owner.compareAndSet(projection, token, withState({
|
|
492
480
|
...request.next,
|
|
@@ -495,8 +483,7 @@ const producer = pin(make({
|
|
|
495
483
|
return yield* Effect.void;
|
|
496
484
|
})
|
|
497
485
|
}));
|
|
498
|
-
|
|
499
|
-
//#region src/commit/at-least-once.ts
|
|
486
|
+
// src/commit/at-least-once.ts
|
|
500
487
|
const atLeastOnce = pin(make({
|
|
501
488
|
name: "atLeastOnce",
|
|
502
489
|
guarantee: "at-least-once",
|
|
@@ -513,9 +500,8 @@ const atLeastOnce = pin(make({
|
|
|
513
500
|
yield* store.compareAndSet(pending.key, token, pending.next);
|
|
514
501
|
})
|
|
515
502
|
}));
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
const withCompensation = dual(2, (self, f) => inherit(self, make({
|
|
503
|
+
// src/commit/compensation.ts
|
|
504
|
+
const withCompensation = Function.dual(2, (self, f) => inherit(self, make({
|
|
519
505
|
...self,
|
|
520
506
|
apply: (pending) => Effect.suspend(() => {
|
|
521
507
|
let completed = Option.none();
|
|
@@ -528,8 +514,7 @@ const withCompensation = dual(2, (self, f) => inherit(self, make({
|
|
|
528
514
|
return self.apply(observed).pipe(Effect.catchCause((cause) => (Option.isSome(completed) ? f(completed.value, cause, pending.unit) : Effect.void).pipe(Effect.andThen(Effect.failCause(cause)))));
|
|
529
515
|
})
|
|
530
516
|
})));
|
|
531
|
-
|
|
532
|
-
//#region src/commit.ts
|
|
517
|
+
// src/commit.ts
|
|
533
518
|
var commit_exports = /* @__PURE__ */ __exportAll({
|
|
534
519
|
Accumulated: () => Accumulated,
|
|
535
520
|
Emitted: () => Emitted,
|
|
@@ -543,8 +528,7 @@ var commit_exports = /* @__PURE__ */ __exportAll({
|
|
|
543
528
|
transaction: () => transaction,
|
|
544
529
|
withCompensation: () => withCompensation
|
|
545
530
|
});
|
|
546
|
-
|
|
547
|
-
//#region src/state.ts
|
|
531
|
+
// src/state.ts
|
|
548
532
|
const bindState = (definition, params) => {
|
|
549
533
|
const initial = definition.initial;
|
|
550
534
|
return {
|
|
@@ -570,13 +554,12 @@ const encodeState = (key, schema, value) => Schema.encodeEffect(Schema.fromJsonS
|
|
|
570
554
|
message: `Cannot encode state for ${key.id}`,
|
|
571
555
|
cause
|
|
572
556
|
})));
|
|
573
|
-
|
|
574
|
-
//#region src/step.ts
|
|
557
|
+
// src/step.ts
|
|
575
558
|
/** One adapter invocation is one unit. Per-entry variants thread state and preserve order. */
|
|
576
559
|
const adapt = (kind, key, state, streams, handler, batch, unit, encoded, prepared) => Effect.gen(function* () {
|
|
577
560
|
if (kind === "process") return yield* Effect.asVoid(handler(batch, unit, prepared));
|
|
578
561
|
let value = state === void 0 ? void 0 : yield* currentState(key, state, encoded);
|
|
579
|
-
const buffers =
|
|
562
|
+
const buffers = Record.fromEntries(Array.map(Record.keys(streams), (name) => [name, []]));
|
|
580
563
|
const steps = kind === "mapAccumBatch" ? [batch] : items(batch);
|
|
581
564
|
for (const entry of steps) {
|
|
582
565
|
const step = kind === "emit" ? handler(entry, unit) : handler(value, entry, unit);
|
|
@@ -588,7 +571,7 @@ const adapt = (kind, key, state, streams, handler, batch, unit, encoded, prepare
|
|
|
588
571
|
yield* collect(streams, buffers, emitted);
|
|
589
572
|
}
|
|
590
573
|
}
|
|
591
|
-
const batchOutputs =
|
|
574
|
+
const batchOutputs = Record.fromEntries(Array.map(Record.toEntries(streams), ([name, address]) => [name, {
|
|
592
575
|
address,
|
|
593
576
|
items: buffers[name] ?? []
|
|
594
577
|
}]));
|
|
@@ -603,19 +586,18 @@ const adapt = (kind, key, state, streams, handler, batch, unit, encoded, prepare
|
|
|
603
586
|
outputs: batchOutputs
|
|
604
587
|
});
|
|
605
588
|
});
|
|
606
|
-
|
|
607
|
-
//#region src/declaration.ts
|
|
589
|
+
// src/declaration.ts
|
|
608
590
|
const isDeclaration = (input) => "at" in input;
|
|
609
591
|
const encodeParams = (id, codecs, params) => {
|
|
610
592
|
const values = params;
|
|
611
|
-
return
|
|
593
|
+
return Record.fromEntries(Array.map(Record.toEntries(codecs), ([name, codec]) => {
|
|
612
594
|
const encoded = Schema.encodeOption(codec)(values[name]);
|
|
613
595
|
if (Option.isNone(encoded)) throw new RangeError(`Cannot encode projection ${id} parameter ${name}`);
|
|
614
596
|
return [name, encoded.value];
|
|
615
597
|
}));
|
|
616
598
|
};
|
|
617
599
|
const addressesOf = (inputs, params) => {
|
|
618
|
-
return
|
|
600
|
+
return Record.fromEntries(Array.map(Record.toEntries(inputs), ([name, input]) => [name, isDeclaration(input) ? input.at(params) : input]));
|
|
619
601
|
};
|
|
620
602
|
const declare = (definition, bind) => {
|
|
621
603
|
const version = definition.version ?? 1;
|
|
@@ -633,7 +615,7 @@ const declare = (definition, bind) => {
|
|
|
633
615
|
decoded: params
|
|
634
616
|
}),
|
|
635
617
|
parse: (id) => {
|
|
636
|
-
for (const input of
|
|
618
|
+
for (const input of Record.values(definition.inputs)) {
|
|
637
619
|
if (!isDeclaration(input)) continue;
|
|
638
620
|
const parsed = input.parse(id);
|
|
639
621
|
if (Option.isSome(parsed)) return Option.some(parsed.value);
|
|
@@ -660,31 +642,26 @@ const build = (kind, definition) => {
|
|
|
660
642
|
};
|
|
661
643
|
});
|
|
662
644
|
};
|
|
663
|
-
|
|
664
|
-
//#region src/fold.ts
|
|
645
|
+
// src/fold.ts
|
|
665
646
|
function fold(definition) {
|
|
666
647
|
return build("fold", definition);
|
|
667
648
|
}
|
|
668
|
-
|
|
669
|
-
//#region src/process.ts
|
|
649
|
+
// src/process.ts
|
|
670
650
|
function process(definition) {
|
|
671
651
|
return build("process", definition);
|
|
672
652
|
}
|
|
673
|
-
|
|
674
|
-
//#region src/emit.ts
|
|
653
|
+
// src/emit.ts
|
|
675
654
|
function emit(definition) {
|
|
676
655
|
return build("emit", definition);
|
|
677
656
|
}
|
|
678
|
-
|
|
679
|
-
//#region src/map-accum.ts
|
|
657
|
+
// src/map-accum.ts
|
|
680
658
|
function mapAccum(definition) {
|
|
681
659
|
return build("mapAccum", definition);
|
|
682
660
|
}
|
|
683
661
|
function mapAccumBatch(definition) {
|
|
684
662
|
return build("mapAccumBatch", definition);
|
|
685
663
|
}
|
|
686
|
-
|
|
687
|
-
//#region src/snapshot.ts
|
|
664
|
+
// src/snapshot.ts
|
|
688
665
|
/** Internal: the offsets of a record, typed by the declared inputs. */
|
|
689
666
|
const offsetsOf = (record) => record.inputs;
|
|
690
667
|
/**
|
|
@@ -702,8 +679,7 @@ const read = Effect.fn("Projection.read")(function* (projection) {
|
|
|
702
679
|
offsets: offsetsOf(restored.record)
|
|
703
680
|
};
|
|
704
681
|
});
|
|
705
|
-
|
|
706
|
-
//#region src/run.ts
|
|
682
|
+
// src/run.ts
|
|
707
683
|
/** Restore and adapt one input unit; the Commit owns every write. */
|
|
708
684
|
const pass = Effect.fn("Projection.pass")(function* (projection) {
|
|
709
685
|
const before = yield* restore(projection, projection.commit);
|
|
@@ -732,7 +708,7 @@ const pass = Effect.fn("Projection.pass")(function* (projection) {
|
|
|
732
708
|
batch = read.slices;
|
|
733
709
|
count = read.items;
|
|
734
710
|
if (count === 0) return {
|
|
735
|
-
status:
|
|
711
|
+
status: Array.every(Record.values(batch), (slice) => slice.closed) ? "source-closed" : "caught-up",
|
|
736
712
|
units: 0,
|
|
737
713
|
items: 0,
|
|
738
714
|
offsets: offsetsOf(before.record)
|
|
@@ -746,7 +722,7 @@ const pass = Effect.fn("Projection.pass")(function* (projection) {
|
|
|
746
722
|
inputs: advance(record.inputs, ranges)
|
|
747
723
|
};
|
|
748
724
|
const outputs = {};
|
|
749
|
-
for (const [name, address] of
|
|
725
|
+
for (const [name, address] of Record.toEntries(projection.streams ?? {})) outputs[name] = {
|
|
750
726
|
address,
|
|
751
727
|
service: yield* address.stream
|
|
752
728
|
};
|
|
@@ -764,7 +740,7 @@ const pass = Effect.fn("Projection.pass")(function* (projection) {
|
|
|
764
740
|
}, {
|
|
765
741
|
outputs,
|
|
766
742
|
prepare,
|
|
767
|
-
ranges:
|
|
743
|
+
ranges: Record.fromEntries(Array.map(Record.toEntries(ranges), ([name, range]) => [name, {
|
|
768
744
|
from: range.from,
|
|
769
745
|
nextOffset: range.nextOffset,
|
|
770
746
|
count: batch[name]?.items.length ?? 0
|
|
@@ -802,13 +778,11 @@ const run = Effect.fn("Projection.run")(function* (projection, options = {}) {
|
|
|
802
778
|
items
|
|
803
779
|
};
|
|
804
780
|
});
|
|
805
|
-
|
|
806
|
-
//#region src/retry.ts
|
|
781
|
+
// src/retry.ts
|
|
807
782
|
const defaultRetry = Schedule.exponential("200 millis").pipe(Schedule.modifyDelay(({ duration }) => Effect.succeed(Duration.millis(Math.min(3e4, Duration.toMillis(duration))))), Schedule.jittered, Schedule.modifyDelay(({ duration }) => Effect.succeed(Duration.millis(Math.min(3e4, Duration.toMillis(duration))))));
|
|
808
783
|
/** Default storage-failure retry for follow; an explicit options.retry takes precedence. */
|
|
809
784
|
const ProjectionRetry = Context.Reference("@streamsy/projection/ProjectionRetry", { defaultValue: () => defaultRetry });
|
|
810
|
-
|
|
811
|
-
//#region src/follow.ts
|
|
785
|
+
// src/follow.ts
|
|
812
786
|
/**
|
|
813
787
|
* One wake hint per input; the messages are discarded because the next run is
|
|
814
788
|
* authoritative. Only the response cursor is kept, so the next hint on that
|
|
@@ -873,14 +847,13 @@ const follow = Effect.fn("Projection.follow")(function* (projection, options = {
|
|
|
873
847
|
message: "repairIntervalMs must be a positive safe integer"
|
|
874
848
|
});
|
|
875
849
|
const cursors = yield* Ref.make({});
|
|
876
|
-
const wake = (result) => Effect.raceAllFirst(inputEntries(projection.inputs)
|
|
850
|
+
const wake = (result) => Effect.raceAllFirst(Array.map(inputEntries(projection.inputs), ([name, address]) => hint(name, address, result.offsets[name] ?? ZERO_OFFSET, interval, cursors)));
|
|
877
851
|
return yield* run(projection, options).pipe(Effect.tap((result) => result.status === "caught-up" ? wake(result) : Effect.yieldNow)).pipe(Effect.retry({
|
|
878
852
|
while: (fault) => Schema.is(ProjectionFault)(fault) && fault.retryable,
|
|
879
853
|
schedule: (options.retry ?? (yield* ProjectionRetry)).pipe(Schedule.tap(({ input }) => Schema.is(ProjectionFault)(input) && input.retryable ? Effect.logWarning("Projection.follow retry", input) : Effect.void))
|
|
880
854
|
}), Effect.repeat({ while: (result) => result.status !== "source-closed" }), Effect.forkScoped);
|
|
881
855
|
});
|
|
882
|
-
|
|
883
|
-
//#region src/current.ts
|
|
856
|
+
// src/current.ts
|
|
884
857
|
/**
|
|
885
858
|
* Folds the checkpoint and input tail in memory, without locks or writes.
|
|
886
859
|
* Includes entries appended before the call. A growing input can delay completion.
|
|
@@ -905,8 +878,7 @@ const current = Effect.fn("Projection.current")(function* (projection) {
|
|
|
905
878
|
offsets = advance(offsets, ranges);
|
|
906
879
|
}
|
|
907
880
|
});
|
|
908
|
-
|
|
909
|
-
//#region src/decide.ts
|
|
881
|
+
// src/decide.ts
|
|
910
882
|
/** Up to three retries after a lost race, so at most four appends per call. */
|
|
911
883
|
const defaultDecideRetry = Schedule.recurs(3);
|
|
912
884
|
/**
|
|
@@ -919,7 +891,7 @@ const decide = Effect.fn("Projection.decide")(function* (projection, spec) {
|
|
|
919
891
|
const snapshot = yield* current(projection);
|
|
920
892
|
const offset = snapshot.offsets[spec.input];
|
|
921
893
|
const { value, items } = yield* spec.decide(snapshot);
|
|
922
|
-
if (!isReadonlyArrayNonEmpty(items)) return {
|
|
894
|
+
if (!Array.isReadonlyArrayNonEmpty(items)) return {
|
|
923
895
|
value,
|
|
924
896
|
offset,
|
|
925
897
|
appended: false
|
|
@@ -957,8 +929,7 @@ const retryLostRace = Effect.fnUntraced(function* (attempt, schedule) {
|
|
|
957
929
|
return yield* Effect.fromResult(outcome);
|
|
958
930
|
});
|
|
959
931
|
const isOffsetMismatch = (error) => Predicate.isTagged(error, "OffsetMismatch");
|
|
960
|
-
|
|
961
|
-
//#region src/forget.ts
|
|
932
|
+
// src/forget.ts
|
|
962
933
|
/**
|
|
963
934
|
* Deletes the record. Stop the follower first, or use a new generation.
|
|
964
935
|
* Delete stream outputs first, because forgetting restarts their producer sequences.
|
|
@@ -972,8 +943,7 @@ const forget = (projection) => Effect.gen(function* () {
|
|
|
972
943
|
yield* projection.commit.remove(projection);
|
|
973
944
|
return yield* Effect.void;
|
|
974
945
|
});
|
|
975
|
-
|
|
976
|
-
//#region src/projection.ts
|
|
946
|
+
// src/projection.ts
|
|
977
947
|
var projection_exports = /* @__PURE__ */ __exportAll({
|
|
978
948
|
current: () => current,
|
|
979
949
|
decide: () => decide,
|
|
@@ -999,5 +969,4 @@ const key = (projection) => ({
|
|
|
999
969
|
});
|
|
1000
970
|
/** Runs each tagged entry in order and discards its success value. */
|
|
1001
971
|
const each = (handle) => (batch, unit) => Effect.forEach(items(batch), (entry) => handle(entry, unit), { discard: true });
|
|
1002
|
-
//#endregion
|
|
1003
972
|
export { CheckpointRecord, CheckpointStore, commit_exports as Commit, HandlerFailed, projection_exports as Projection, ProjectionFault, ProjectionRetry, Transaction };
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
|
|
1
|
+
// \0rolldown/runtime.js
|
|
2
2
|
var __defProp = Object.defineProperty;
|
|
3
3
|
var __exportAll = (all, no_symbols) => {
|
|
4
4
|
let target = {};
|
|
@@ -9,5 +9,4 @@ var __exportAll = (all, no_symbols) => {
|
|
|
9
9
|
if (!no_symbols) __defProp(target, Symbol.toStringTag, { value: "Module" });
|
|
10
10
|
return target;
|
|
11
11
|
};
|
|
12
|
-
//#endregion
|
|
13
12
|
export { __exportAll as t };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@streamsy/projection",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.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.5.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.0.5",
|
|
48
48
|
"@effect/sql-sqlite-do": "4.0.5",
|
|
49
49
|
"@effect/vitest": "4.0.5",
|
|
50
|
-
"@streamsy/serve": "0.
|
|
51
|
-
"@streamsy/storage": "0.
|
|
50
|
+
"@streamsy/serve": "0.5.0",
|
|
51
|
+
"@streamsy/storage": "0.5.0",
|
|
52
52
|
"@types/bun": "^1.4.3",
|
|
53
53
|
"effect": "4.0.5",
|
|
54
54
|
"miniflare": "4.20260730.0",
|