@streamsy/core 0.12.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/dist/boundary-f1biibJ1.js +371 -0
- package/dist/http/index.d.ts +1 -1
- package/dist/{index-ooCOjS7N.d.ts → index-BtVxu6VJ.d.ts} +2 -16
- package/dist/index.d.ts +2 -2
- package/dist/index.js +6 -5
- package/dist/internal/index.d.ts +21 -3
- package/dist/internal/index.js +2 -2
- package/dist/{layer-Bf40J6so.js → layer-J6sV3UXD.js} +3 -150
- package/dist/{projection-marks-CzlDS-kj.d.ts → projection-marks-BEZv8qDJ.d.ts} +38 -3
- package/dist/protocol/index.d.ts +2 -2
- package/dist/remote/index.d.ts +2 -2
- package/dist/testing/index.d.ts +2 -2
- package/dist/testing/index.js +5 -5
- package/package.json +1 -1
- package/dist/marks-B-fXqEYG.js +0 -195
|
@@ -0,0 +1,371 @@
|
|
|
1
|
+
import { a as attachOwner } from "./route-internal-DNL0oGTN.js";
|
|
2
|
+
import { Array, Cause, Context, Effect, Exit, Option, Order, Schema, Semaphore } from "effect";
|
|
3
|
+
// src/storage/commit-boundary.ts
|
|
4
|
+
/** The transaction of the storage owner. Every Streamsy storage host provides it. */
|
|
5
|
+
var CommitBoundary = class extends Context.Service()("@streamsy/core/CommitBoundary") {};
|
|
6
|
+
// src/storage/memory/state.ts
|
|
7
|
+
const copyRecord = (record) => ({
|
|
8
|
+
id: record.id,
|
|
9
|
+
currentOffset: record.currentOffset,
|
|
10
|
+
config: { ...record.config },
|
|
11
|
+
lifecycle: { ...record.lifecycle }
|
|
12
|
+
});
|
|
13
|
+
const copyMessage = (message) => ({
|
|
14
|
+
offset: message.offset,
|
|
15
|
+
timestamp: message.timestamp,
|
|
16
|
+
data: new Uint8Array(message.data)
|
|
17
|
+
});
|
|
18
|
+
function patchRecord(record, patch) {
|
|
19
|
+
const config = {
|
|
20
|
+
...record.config,
|
|
21
|
+
...patch.config
|
|
22
|
+
};
|
|
23
|
+
const lifecycle = {
|
|
24
|
+
...record.lifecycle,
|
|
25
|
+
...patch.lifecycle
|
|
26
|
+
};
|
|
27
|
+
for (const key of patch.clear ?? []) switch (key) {
|
|
28
|
+
case "ttlSeconds":
|
|
29
|
+
delete config.ttlSeconds;
|
|
30
|
+
break;
|
|
31
|
+
case "expiresAt":
|
|
32
|
+
delete config.expiresAt;
|
|
33
|
+
break;
|
|
34
|
+
case "expiresAtMs":
|
|
35
|
+
delete lifecycle.expiresAtMs;
|
|
36
|
+
break;
|
|
37
|
+
case "lastSeq": delete lifecycle.lastSeq;
|
|
38
|
+
}
|
|
39
|
+
return {
|
|
40
|
+
id: record.id,
|
|
41
|
+
currentOffset: patch.currentOffset ?? record.currentOffset,
|
|
42
|
+
config,
|
|
43
|
+
lifecycle
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
function indexDeadlines(state) {
|
|
47
|
+
state.deadlines = Array.sort(Array.flatMap([...state.entries.values()], ({ record }) => record.lifecycle.softDeleted || record.lifecycle.expiresAtMs === void 0 ? [] : [{
|
|
48
|
+
at: record.lifecycle.expiresAtMs,
|
|
49
|
+
streamId: record.id
|
|
50
|
+
}]), Order.make((a, b) => {
|
|
51
|
+
const compared = a.at - b.at || a.streamId.localeCompare(b.streamId);
|
|
52
|
+
return compared < 0 ? -1 : compared > 0 ? 1 : 0;
|
|
53
|
+
}));
|
|
54
|
+
}
|
|
55
|
+
/** A private transaction draft; no mutable stream data aliases the committed state. */
|
|
56
|
+
const copyState = (state) => ({
|
|
57
|
+
entries: new Map(Array.map([...state.entries], ([id, entry]) => [id, {
|
|
58
|
+
record: copyRecord(entry.record),
|
|
59
|
+
messages: Array.map(entry.messages, copyMessage),
|
|
60
|
+
producers: new Map(Array.map([...entry.producers], ([producerId, producer]) => [producerId, {
|
|
61
|
+
epoch: producer.epoch,
|
|
62
|
+
lastSeq: producer.lastSeq
|
|
63
|
+
}]))
|
|
64
|
+
}])),
|
|
65
|
+
children: new Map(Array.map([...state.children], ([id, children]) => [id, new Set(children)])),
|
|
66
|
+
deadlines: Array.map(state.deadlines, (deadline) => ({ ...deadline }))
|
|
67
|
+
});
|
|
68
|
+
// src/internal/owner-records.ts
|
|
69
|
+
/** The `StorageFault.operation` of an owner store that cannot list its records. */
|
|
70
|
+
const OWNER_RECORDS_LIST_UNSUPPORTED = "records.list.unsupported";
|
|
71
|
+
var OwnerRecords = class extends Context.Service()("@streamsy/core/internal/OwnerRecords") {};
|
|
72
|
+
// src/internal/commit-hooks.ts
|
|
73
|
+
var CommitHooks = class extends Context.Service()("@streamsy/core/internal/CommitHooks") {};
|
|
74
|
+
/** Shared registry for storage boundaries: register in a scope, run in order. */
|
|
75
|
+
const commitHookRegistry = () => {
|
|
76
|
+
const hooks = /* @__PURE__ */ new Set();
|
|
77
|
+
const beforeCommit = (hook) => Effect.acquireRelease(Effect.sync(() => {
|
|
78
|
+
const entry = { hook };
|
|
79
|
+
hooks.add(entry);
|
|
80
|
+
return entry;
|
|
81
|
+
}), (entry) => Effect.sync(() => {
|
|
82
|
+
hooks.delete(entry);
|
|
83
|
+
})).pipe(Effect.asVoid);
|
|
84
|
+
const run = (ids) => ids.size === 0 || hooks.size === 0 ? Effect.void : Effect.forEach([...hooks], (entry) => entry.hook(ids), { discard: true });
|
|
85
|
+
return {
|
|
86
|
+
beforeCommit,
|
|
87
|
+
run
|
|
88
|
+
};
|
|
89
|
+
};
|
|
90
|
+
// src/internal/projection-marks.ts
|
|
91
|
+
var ProjectionMarks = class extends Context.Service()("@streamsy/core/internal/ProjectionMarks") {};
|
|
92
|
+
// src/storage/memory/marks.ts
|
|
93
|
+
/** A private transaction draft of the marks. */
|
|
94
|
+
const copyMarks = (marks) => new Map(Array.map([...marks], ([projection, rows]) => [projection, new Map(rows)]));
|
|
95
|
+
const rowsOf = (marks, projection) => {
|
|
96
|
+
const found = marks.get(projection);
|
|
97
|
+
if (found !== void 0) return found;
|
|
98
|
+
const rows = /* @__PURE__ */ new Map();
|
|
99
|
+
marks.set(projection, rows);
|
|
100
|
+
return rows;
|
|
101
|
+
};
|
|
102
|
+
const byDue = Order.mapInput(Order.Tuple([Order.Number, Order.String]), (mark) => [mark.dueAt, mark.params]);
|
|
103
|
+
const mark = (marks, projection, params) => {
|
|
104
|
+
const rows = rowsOf(marks, projection);
|
|
105
|
+
for (const value of params) {
|
|
106
|
+
const found = rows.get(value);
|
|
107
|
+
rows.set(value, found === void 0 ? {
|
|
108
|
+
params: value,
|
|
109
|
+
seq: 1,
|
|
110
|
+
attempts: 0,
|
|
111
|
+
dueAt: 0,
|
|
112
|
+
stopped: false,
|
|
113
|
+
wakeAt: Option.none(),
|
|
114
|
+
failure: Option.none()
|
|
115
|
+
} : {
|
|
116
|
+
...found,
|
|
117
|
+
seq: found.seq + 1,
|
|
118
|
+
dueAt: Option.isSome(found.wakeAt) ? 0 : found.dueAt,
|
|
119
|
+
wakeAt: Option.none()
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
};
|
|
123
|
+
const nextDue = (marks, projection) => {
|
|
124
|
+
let earliest;
|
|
125
|
+
for (const row of marks.get(projection)?.values() ?? []) if (!row.stopped) earliest = earliest === void 0 ? row.dueAt : Math.min(earliest, row.dueAt);
|
|
126
|
+
return Option.fromUndefinedOr(earliest);
|
|
127
|
+
};
|
|
128
|
+
const due = (marks, projection, now, limit) => Array.sort(Array.filter([...marks.get(projection)?.values() ?? []], (row) => !row.stopped && row.dueAt <= now), byDue).slice(0, limit);
|
|
129
|
+
const read = (marks, projection, params) => {
|
|
130
|
+
const rows = marks.get(projection);
|
|
131
|
+
const found = /* @__PURE__ */ new Map();
|
|
132
|
+
for (const value of params) {
|
|
133
|
+
const row = rows?.get(value);
|
|
134
|
+
if (row !== void 0) found.set(value, row);
|
|
135
|
+
}
|
|
136
|
+
return found;
|
|
137
|
+
};
|
|
138
|
+
const clear = (marks, projection, params, seq) => {
|
|
139
|
+
const rows = marks.get(projection);
|
|
140
|
+
const row = rows?.get(params);
|
|
141
|
+
if (rows === void 0 || row === void 0) return;
|
|
142
|
+
if (row.seq === seq) rows.delete(params);
|
|
143
|
+
else rows.set(params, {
|
|
144
|
+
...row,
|
|
145
|
+
attempts: 0,
|
|
146
|
+
dueAt: 0,
|
|
147
|
+
stopped: false,
|
|
148
|
+
wakeAt: Option.none(),
|
|
149
|
+
failure: Option.none()
|
|
150
|
+
});
|
|
151
|
+
};
|
|
152
|
+
/** Sleep only the seq that ran; a concurrent write must run before it can sleep. */
|
|
153
|
+
const sleep = (marks, projection, params, seq, wakeAt) => {
|
|
154
|
+
const rows = rowsOf(marks, projection);
|
|
155
|
+
const row = rows.get(params);
|
|
156
|
+
const matching = row === void 0 || row.seq === seq;
|
|
157
|
+
rows.set(params, {
|
|
158
|
+
params,
|
|
159
|
+
seq: row?.seq ?? 1,
|
|
160
|
+
attempts: 0,
|
|
161
|
+
dueAt: matching ? wakeAt : 0,
|
|
162
|
+
wakeAt: matching ? Option.some(wakeAt) : Option.none(),
|
|
163
|
+
stopped: false,
|
|
164
|
+
failure: Option.none()
|
|
165
|
+
});
|
|
166
|
+
};
|
|
167
|
+
const remove = (marks, projection, params) => {
|
|
168
|
+
marks.get(projection)?.delete(params);
|
|
169
|
+
};
|
|
170
|
+
const fail = (marks, projection, params, options) => {
|
|
171
|
+
const rows = rowsOf(marks, projection);
|
|
172
|
+
const found = rows.get(params);
|
|
173
|
+
rows.set(params, {
|
|
174
|
+
params,
|
|
175
|
+
seq: found?.seq ?? 1,
|
|
176
|
+
attempts: options.attempts,
|
|
177
|
+
dueAt: Option.getOrElse(options.dueAt, () => 0),
|
|
178
|
+
stopped: Option.isNone(options.dueAt),
|
|
179
|
+
wakeAt: Option.none(),
|
|
180
|
+
failure: Option.some({
|
|
181
|
+
...options.failure,
|
|
182
|
+
message: options.failure.message.slice(0, 1e3)
|
|
183
|
+
})
|
|
184
|
+
});
|
|
185
|
+
};
|
|
186
|
+
const failed = (marks, projection, limit) => Array.sort(Array.filter([...marks.get(projection)?.values() ?? []], (row) => row.attempts > 0), Order.combine(Order.mapInput(Order.Boolean, (row) => !row.stopped), byDue)).slice(0, limit);
|
|
187
|
+
const resume = (marks, projection) => {
|
|
188
|
+
let changed = 0;
|
|
189
|
+
const rows = marks.get(projection);
|
|
190
|
+
if (rows === void 0) return 0;
|
|
191
|
+
for (const [key, row] of rows) {
|
|
192
|
+
if (!row.stopped) continue;
|
|
193
|
+
rows.set(key, {
|
|
194
|
+
...row,
|
|
195
|
+
dueAt: 0,
|
|
196
|
+
stopped: false
|
|
197
|
+
});
|
|
198
|
+
changed += 1;
|
|
199
|
+
}
|
|
200
|
+
return changed;
|
|
201
|
+
};
|
|
202
|
+
const retry = (marks, projection, params) => {
|
|
203
|
+
let changed = 0;
|
|
204
|
+
const rows = marks.get(projection);
|
|
205
|
+
if (rows === void 0) return 0;
|
|
206
|
+
for (const [key, row] of rows) {
|
|
207
|
+
if (!row.stopped && (row.attempts === 0 || row.dueAt === 0) || params !== void 0 && key !== params) continue;
|
|
208
|
+
rows.set(key, {
|
|
209
|
+
...row,
|
|
210
|
+
dueAt: 0,
|
|
211
|
+
stopped: false
|
|
212
|
+
});
|
|
213
|
+
changed += 1;
|
|
214
|
+
}
|
|
215
|
+
return changed;
|
|
216
|
+
};
|
|
217
|
+
/** Classify each row once; a failed row at `now` is due, not backing off. */
|
|
218
|
+
const summary = (marks, projection, now) => {
|
|
219
|
+
let fresh = 0;
|
|
220
|
+
let sleeping = 0;
|
|
221
|
+
let retryDue = 0;
|
|
222
|
+
let backingOff = 0;
|
|
223
|
+
let stopped = 0;
|
|
224
|
+
let earliest;
|
|
225
|
+
for (const row of marks.get(projection)?.values() ?? []) {
|
|
226
|
+
if (row.stopped) {
|
|
227
|
+
stopped += 1;
|
|
228
|
+
continue;
|
|
229
|
+
}
|
|
230
|
+
if (Option.isSome(row.wakeAt)) sleeping += 1;
|
|
231
|
+
else if (row.attempts === 0) fresh += 1;
|
|
232
|
+
else if (row.dueAt <= now) retryDue += 1;
|
|
233
|
+
else backingOff += 1;
|
|
234
|
+
earliest = earliest === void 0 ? row.dueAt : Math.min(earliest, row.dueAt);
|
|
235
|
+
}
|
|
236
|
+
return {
|
|
237
|
+
fresh,
|
|
238
|
+
sleeping,
|
|
239
|
+
retryDue,
|
|
240
|
+
backingOff,
|
|
241
|
+
stopped,
|
|
242
|
+
nextDueAt: Option.fromUndefinedOr(earliest)
|
|
243
|
+
};
|
|
244
|
+
};
|
|
245
|
+
/** A new private marks store. Each operation runs synchronously when its Effect executes. */
|
|
246
|
+
const memoryProjectionMarks = () => {
|
|
247
|
+
const table = /* @__PURE__ */ new Map();
|
|
248
|
+
return {
|
|
249
|
+
mark: (projection, params) => Effect.sync(() => mark(table, projection, params)),
|
|
250
|
+
nextDue: (projection) => Effect.sync(() => nextDue(table, projection)),
|
|
251
|
+
summary: (projection, now) => Effect.sync(() => summary(table, projection, now)),
|
|
252
|
+
due: (projection, now, limit) => Effect.sync(() => due(table, projection, now, limit)),
|
|
253
|
+
read: (projection, params) => Effect.sync(() => read(table, projection, params)),
|
|
254
|
+
clear: (projection, params, seq) => Effect.sync(() => clear(table, projection, params, seq)),
|
|
255
|
+
sleep: (projection, params, seq, wakeAt) => Effect.sync(() => sleep(table, projection, params, seq, wakeAt)),
|
|
256
|
+
remove: (projection, params) => Effect.sync(() => remove(table, projection, params)),
|
|
257
|
+
fail: (projection, params, options) => Effect.sync(() => fail(table, projection, params, options)),
|
|
258
|
+
retry: (projection, params) => Effect.sync(() => retry(table, projection, params)),
|
|
259
|
+
resume: (projection) => Effect.sync(() => resume(table, projection)),
|
|
260
|
+
failed: (projection, limit) => Effect.sync(() => failed(table, projection, limit))
|
|
261
|
+
};
|
|
262
|
+
};
|
|
263
|
+
// src/storage/memory/boundary.ts
|
|
264
|
+
var ActiveDraft = class extends Context.Service()("@streamsy/core/internal/MemoryActiveDraft") {};
|
|
265
|
+
const activeDraft = (owner, reject) => Effect.flatMap(Effect.serviceOption(ActiveDraft), (draft) => Effect.withFiber((fiber) => {
|
|
266
|
+
if (Option.isSome(draft) && (draft.value.owner !== owner || !draft.value.active || draft.value.fiberId !== fiber.id)) return reject();
|
|
267
|
+
return Effect.succeed(draft);
|
|
268
|
+
}));
|
|
269
|
+
var MemoryEnlistFault = class extends Schema.TaggedError()("MemoryEnlistFault", { message: Schema.String }) {};
|
|
270
|
+
const rejectEnlist = () => Effect.fail(new MemoryEnlistFault({ message: "Enlisting requires the active memory owner fiber" }));
|
|
271
|
+
/** Join the memory owner transaction open on this fiber. */
|
|
272
|
+
function enlist(owner, participant) {
|
|
273
|
+
return Effect.flatMap(activeDraft(owner, rejectEnlist), (draft) => {
|
|
274
|
+
if (Option.isNone(draft)) return rejectEnlist();
|
|
275
|
+
draft.value.participants.add(participant);
|
|
276
|
+
return Effect.void;
|
|
277
|
+
});
|
|
278
|
+
}
|
|
279
|
+
const notifyParticipants = (participants, callback) => {
|
|
280
|
+
const defects = [];
|
|
281
|
+
for (const participant of participants) try {
|
|
282
|
+
participant[callback]();
|
|
283
|
+
} catch (defect) {
|
|
284
|
+
defects.push(defect);
|
|
285
|
+
}
|
|
286
|
+
if (defects.length > 0) throw new AggregateError(defects, `Memory participant ${callback} failed`);
|
|
287
|
+
};
|
|
288
|
+
const makeBoundary = (committed, publish) => Effect.gen(function* () {
|
|
289
|
+
const owner = {};
|
|
290
|
+
const lock = yield* Semaphore.make(1);
|
|
291
|
+
let committedRecords = /* @__PURE__ */ new Map();
|
|
292
|
+
let committedMarks = /* @__PURE__ */ new Map();
|
|
293
|
+
const hooks = commitHookRegistry();
|
|
294
|
+
const current = activeDraft(owner, () => Effect.die(/* @__PURE__ */ new Error("Memory boundary requires the active owner fiber")));
|
|
295
|
+
const withTransaction = (body) => Effect.flatMap(current, (ambient) => {
|
|
296
|
+
if (Option.isSome(ambient)) return body;
|
|
297
|
+
return Effect.withFiber((fiber) => Effect.suspend(() => {
|
|
298
|
+
const draft = {
|
|
299
|
+
owner,
|
|
300
|
+
fiberId: fiber.id,
|
|
301
|
+
state: copyState(committed),
|
|
302
|
+
records: new Map(committedRecords),
|
|
303
|
+
marks: copyMarks(committedMarks),
|
|
304
|
+
ids: /* @__PURE__ */ new Set(),
|
|
305
|
+
participants: /* @__PURE__ */ new Set(),
|
|
306
|
+
active: true,
|
|
307
|
+
changed: false
|
|
308
|
+
};
|
|
309
|
+
const withHooks = body.pipe(Effect.tap(() => Effect.orDie(hooks.run(draft.ids))), Effect.provideService(ActiveDraft, draft));
|
|
310
|
+
return Effect.uninterruptibleMask((restore) => Effect.exit(restore(withHooks)).pipe(Effect.flatMap((exit) => {
|
|
311
|
+
if (Exit.isFailure(exit)) return Effect.sync(() => notifyParticipants(Array.reverse(Array.fromIterable(draft.participants)), "onRollback")).pipe(Effect.catchCause((rollback) => Effect.failCause(Cause.combine(exit.cause, rollback))), Effect.andThen(exit));
|
|
312
|
+
return Effect.sync(() => {
|
|
313
|
+
Object.assign(committed, draft.state);
|
|
314
|
+
committedRecords = draft.records;
|
|
315
|
+
committedMarks = draft.marks;
|
|
316
|
+
notifyParticipants(draft.participants, "onCommit");
|
|
317
|
+
}).pipe(Effect.ensuring(Effect.suspend(() => draft.changed ? publish(draft.ids) : Effect.void)), Effect.as(exit.value));
|
|
318
|
+
}), Effect.ensuring(Effect.sync(() => {
|
|
319
|
+
draft.active = false;
|
|
320
|
+
}))));
|
|
321
|
+
}).pipe(Semaphore.withPermit(lock)));
|
|
322
|
+
});
|
|
323
|
+
const access = (f) => Effect.map(current, (draft) => f(Option.isSome(draft) ? draft.value.state : committed));
|
|
324
|
+
const changed = (ids) => Effect.map(current, (draft) => {
|
|
325
|
+
if (Option.isNone(draft)) throw new Error("Memory mutation requires an owner");
|
|
326
|
+
draft.value.changed = true;
|
|
327
|
+
for (const id of ids) draft.value.ids.add(id);
|
|
328
|
+
});
|
|
329
|
+
const boundary = CommitBoundary.of({ withTransaction });
|
|
330
|
+
const records = OwnerRecords.of({
|
|
331
|
+
read: (key) => Effect.map(current, (draft) => Option.fromUndefinedOr((Option.isSome(draft) ? draft.value.records : committedRecords).get(key))),
|
|
332
|
+
list: (prefix) => Effect.map(current, (draft) => Array.filter(Array.fromIterable(Option.isSome(draft) ? draft.value.records : committedRecords), ([key]) => key.startsWith(prefix))),
|
|
333
|
+
remove: (key) => withTransaction(Effect.map(current, (draft) => {
|
|
334
|
+
if (Option.isNone(draft)) throw new Error("Memory record delete requires an owner");
|
|
335
|
+
draft.value.records.delete(key);
|
|
336
|
+
})),
|
|
337
|
+
write: (key, encoded) => withTransaction(Effect.map(current, (draft) => {
|
|
338
|
+
if (Option.isNone(draft)) throw new Error("Memory record write requires an owner");
|
|
339
|
+
draft.value.records.set(key, encoded);
|
|
340
|
+
}))
|
|
341
|
+
});
|
|
342
|
+
const readMarks = (f) => Effect.map(current, (draft) => f(Option.isSome(draft) ? draft.value.marks : committedMarks));
|
|
343
|
+
const writeMarks = (f) => withTransaction(Effect.map(current, (draft) => {
|
|
344
|
+
if (Option.isNone(draft)) throw new Error("Memory mark write requires an owner");
|
|
345
|
+
return f(draft.value.marks);
|
|
346
|
+
}));
|
|
347
|
+
const marks = ProjectionMarks.of({
|
|
348
|
+
mark: (projection, params) => writeMarks((table) => mark(table, projection, params)),
|
|
349
|
+
nextDue: (projection) => readMarks((table) => nextDue(table, projection)),
|
|
350
|
+
summary: (projection, now) => readMarks((table) => summary(table, projection, now)),
|
|
351
|
+
due: (projection, now, limit) => readMarks((table) => due(table, projection, now, limit)),
|
|
352
|
+
read: (projection, params) => readMarks((table) => read(table, projection, params)),
|
|
353
|
+
clear: (projection, params, seq) => writeMarks((table) => clear(table, projection, params, seq)),
|
|
354
|
+
sleep: (projection, params, seq, wakeAt) => writeMarks((table) => sleep(table, projection, params, seq, wakeAt)),
|
|
355
|
+
remove: (projection, params) => writeMarks((table) => remove(table, projection, params)),
|
|
356
|
+
fail: (projection, params, options) => writeMarks((table) => fail(table, projection, params, options)),
|
|
357
|
+
resume: (projection) => writeMarks((table) => resume(table, projection)),
|
|
358
|
+
failed: (projection, limit) => readMarks((table) => failed(table, projection, limit)),
|
|
359
|
+
retry: (projection, params) => writeMarks((table) => retry(table, projection, params))
|
|
360
|
+
});
|
|
361
|
+
return {
|
|
362
|
+
boundary: attachOwner(boundary, owner),
|
|
363
|
+
marks: attachOwner(marks, owner),
|
|
364
|
+
records: attachOwner(records, owner),
|
|
365
|
+
hooks: attachOwner(CommitHooks.of({ beforeCommit: hooks.beforeCommit }), owner),
|
|
366
|
+
withTransaction,
|
|
367
|
+
access,
|
|
368
|
+
changed
|
|
369
|
+
};
|
|
370
|
+
});
|
|
371
|
+
export { CommitHooks as a, OwnerRecords as c, indexDeadlines as d, patchRecord as f, ProjectionMarks as i, copyMessage as l, makeBoundary as n, commitHookRegistry as o, CommitBoundary as p, memoryProjectionMarks as r, OWNER_RECORDS_LIST_UNSUPPORTED as s, enlist as t, copyRecord as u };
|
package/dist/http/index.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { C as Access, S as read, T as routes, _ as ShutdownSignalService, a as LIVE_SOCKET_PROTOCOL, b as ReadOptions, c as LiveControlFrame, d as controlFrame, f as isLiveUpgrade, g as ShutdownSignal, h as upgradeRequired, i as LIVE_PONG, l as LiveOpenFrame, m as openFrame, n as HttpLiveMode,
|
|
1
|
+
import { $ as streamPath, C as Access, S as read, T as routes, _ as ShutdownSignalService, a as LIVE_SOCKET_PROTOCOL, b as ReadOptions, c as LiveControlFrame, d as controlFrame, et as HttpOptions, f as isLiveUpgrade, g as ShutdownSignal, h as upgradeRequired, i as LIVE_PONG, l as LiveOpenFrame, m as openFrame, n as HttpLiveMode, o as LiveBatch, p as nextCursor, r as LIVE_PING, s as LiveCloseCode, tt as app, u as closeCodeForStatus, v as layerShutdownSignal, w as RoutesOptions, x as checkReadOptions, y as securityHeaders } from "../index-BtVxu6VJ.js";
|
|
2
2
|
export { type Access, type HttpLiveMode, type HttpOptions, LIVE_PING, LIVE_PONG, LIVE_SOCKET_PROTOCOL, type LiveBatch, LiveCloseCode, type LiveControlFrame, type LiveOpenFrame, type ReadOptions, type RoutesOptions, ShutdownSignal, type ShutdownSignalService, app, checkReadOptions, closeCodeForStatus, controlFrame, isLiveUpgrade, layerShutdownSignal, nextCursor, openFrame, read, routes, securityHeaders, streamPath, upgradeRequired };
|
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { A as Writer, At as StreamsFault, B as AppendErrorFor, D as Reader, Dt as DecodeFault, F as ReadNextResult, G as HeadError, M as CreateResult, N as HeadResult, O as StreamsReader, Ot as EncodeFault, Q as OffsetMismatch, R as AppendConflict, U as CreateError, X as InvalidReadRequest, Z as NotSupported, at as StreamBusy, ct as StreamNotFound, d as OwnerRecords, ft as ProducerOptions, h as Storage, ht as CreateOptions, i as ProjectionMarks, j as AppendResult, jt as TransportFault, k as StreamsWriter, kt as StorageFault, mt as ReadOptions$1, nt as ReadNextError, ot as StreamClosed, p as CommitBoundary, pt as ReadNextOptions, rt as RemoveError, s as CommitHooks, st as StreamGone, ut as AppendCallOptions, wt as StreamId } from "./projection-marks-BEZv8qDJ.js";
|
|
2
2
|
import { Array, Context, Data, Duration, Effect, Function, Layer, Option, Schedule, Schema, Stream } from "effect";
|
|
3
3
|
import { HttpRouter, HttpServerRequest, HttpServerResponse } from "effect/http";
|
|
4
|
-
import { SqlError } from "effect/sql";
|
|
5
4
|
interface HttpOptions {
|
|
6
5
|
readonly pathPrefix?: string;
|
|
7
6
|
/** Lifetime of an SSE response. Default 60 seconds. */
|
|
@@ -219,19 +218,6 @@ declare function custom$1<RouteParams, A, RD, RE>(options: {
|
|
|
219
218
|
}): StreamRoute<RouteParams, A, RD, RE>;
|
|
220
219
|
declare const ZERO_OFFSET: string & import("effect/Brand").Brand<"Offset">;
|
|
221
220
|
declare const compare: (a: string, b: string) => number;
|
|
222
|
-
interface CommitBoundaryApi {
|
|
223
|
-
/**
|
|
224
|
-
* Run `body` in the storage owner's transaction. A nested call joins the outer
|
|
225
|
-
* transaction (no savepoints), so a failure must escape the outer body to roll back.
|
|
226
|
-
* Stream writes, owner records and, on SQL hosts, application SQL in `body` commit
|
|
227
|
-
* together. Keep the body on the calling fiber, and do not wait in it for wakes that
|
|
228
|
-
* only its own commit publishes. Only SQL hosts fail with `SqlError`.
|
|
229
|
-
*/
|
|
230
|
-
readonly withTransaction: <A, E, R>(body: Effect.Effect<A, E, R>) => Effect.Effect<A, E | SqlError.SqlError, R>;
|
|
231
|
-
}
|
|
232
|
-
declare const CommitBoundary_base: Context.ServiceClass<CommitBoundary, "@streamsy/core/CommitBoundary", CommitBoundaryApi>;
|
|
233
|
-
/** The transaction of the storage owner. Every Streamsy storage host provides it. */
|
|
234
|
-
declare class CommitBoundary extends CommitBoundary_base {}
|
|
235
221
|
/** One host-triggered sweep; the host owns scheduling and cancellation. */
|
|
236
222
|
declare const expireDue: () => Effect.Effect<void, StorageFault, Storage>;
|
|
237
223
|
declare namespace layer_d_exports {
|
|
@@ -785,4 +771,4 @@ type HttpLiveMode = "long-poll" | "sse" | "websocket";
|
|
|
785
771
|
declare namespace index_d_exports {
|
|
786
772
|
export { Access, HttpLiveMode, HttpOptions, LIVE_PING, LIVE_PONG, LIVE_SOCKET_PROTOCOL, LiveBatch, LiveCloseCode, LiveControlFrame, LiveOpenFrame, ReadOptions, RoutesOptions, ShutdownSignal, ShutdownSignalService, app, checkReadOptions, closeCodeForStatus, controlFrame, isLiveUpgrade, layerShutdownSignal, nextCursor, openFrame, read, routes, securityHeaders, streamPath, upgradeRequired };
|
|
787
773
|
}
|
|
788
|
-
export {
|
|
774
|
+
export { streamPath as $, Batch as A, RemoveFailure as B, Access as C, Group as D, Any as E, Decision as F, streams_d_exports$1 as G, streamsy_d_exports as H, Folded as I, layer_d_exports as J, MemoryOptions as K, FollowFailure as L, CreateFailure as M, DecideSpec as N, Identifiers as O, Decided as P, address_d_exports as Q, HeadFailure as R, read as S, routes as T, producer_d_exports as U, streams_d_exports as V, durable_state_d_exports as W, compare as X, ZERO_OFFSET as Y, route_d_exports as Z, ShutdownSignalService as _, LIVE_SOCKET_PROTOCOL as a, ReadOptions as b, LiveControlFrame as c, controlFrame as d, HttpOptions as et, isLiveUpgrade as f, ShutdownSignal as g, upgradeRequired as h, LIVE_PONG as i, Collected as j, AppendFailure as k, LiveOpenFrame as l, openFrame as m, HttpLiveMode as n, LiveBatch as o, nextCursor as p, ProtocolOptions as q, LIVE_PING as r, LiveCloseCode as s, index_d_exports as t, app as tt, closeCodeForStatus as u, layerShutdownSignal as v, RoutesOptions as w, checkReadOptions as x, securityHeaders as y, ReadFailure as z };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import { $ as
|
|
2
|
-
import { A as Batch, B as RemoveFailure, F as Decision, G as streams_d_exports, H as streamsy_d_exports, I as Folded, L as FollowFailure, M as CreateFailure, N as DecideSpec, P as Decided, Q as
|
|
1
|
+
import { $ as ProducerGap, At as StreamsFault, B as AppendErrorFor, C as OperationResults, Ct as StreamConfig, Dt as DecodeFault, E as StorageCapabilities, Et as StreamRecord, F as ReadNextResult, G as HeadError, H as CreateConflictReason, I as ReadResult, J as InvalidEpochSeq, K as InvalidAppendRequest, L as AppendBaseError, M as CreateResult, Mt as isRetryableFault, N as HeadResult, Nt as isStreamsFault, Ot as EncodeFault, P as ReadMessage, Q as OffsetMismatch, R as AppendConflict, S as OperationResult, St as StoredMessage, T as ProducerPrecondition, Tt as StreamLifecycle, U as CreateError, V as CreateConflict, W as ForkSourceNotFound, X as InvalidReadRequest, Y as InvalidForkRequest, Z as NotSupported, _ as Mutation, _t as MessageWindow, at as StreamBusy, b as Mutations, bt as ProducerState, ct as StreamNotFound, dt as AppendOptions, et as ProtocolError, ft as ProducerOptions, g as StorageShape, gt as ChangeSnapshot, h as Storage, ht as CreateOptions, it as StaleEpoch, j as AppendResult, jt as TransportFault, kt as StorageFault, lt as isProtocolError, m as CommitBoundaryApi, mt as ReadOptions, nt as ReadNextError, ot as StreamClosed, p as CommitBoundary, pt as ReadNextOptions, q as InvalidCreateRequest, rt as RemoveError, st as StreamGone, tt as ReadError, ut as AppendCallOptions, v as MutationApplied, vt as Offset, w as Operations, wt as StreamId, x as Operation, xt as RecordPatch, y as MutationRejected, yt as ProducerId, z as AppendError } from "./projection-marks-BEZv8qDJ.js";
|
|
2
|
+
import { A as Batch, B as RemoveFailure, F as Decision, G as streams_d_exports, H as streamsy_d_exports, I as Folded, L as FollowFailure, M as CreateFailure, N as DecideSpec, P as Decided, Q as address_d_exports, R as HeadFailure, U as producer_d_exports, W as durable_state_d_exports, X as compare, Y as ZERO_OFFSET, j as Collected, k as AppendFailure, t as index_d_exports, z as ReadFailure } from "./index-BtVxu6VJ.js";
|
|
3
3
|
export { AppendBaseError, type AppendCallOptions, AppendConflict, AppendError, AppendErrorFor, type AppendFailure, type AppendOptions, AppendResult, type Batch, ChangeSnapshot, type Collected, CommitBoundary, type CommitBoundaryApi, CreateConflict, CreateConflictReason, CreateError, type CreateFailure, type CreateOptions, CreateResult, type DecideSpec, type Decided, type Decision, DecodeFault, durable_state_d_exports as DurableState, EncodeFault, type Folded, type FollowFailure, ForkSourceNotFound, HeadError, type HeadFailure, type HeadResult, index_d_exports as Http, InvalidAppendRequest, InvalidCreateRequest, InvalidEpochSeq, InvalidForkRequest, InvalidReadRequest, streams_d_exports as MemoryStorage, MessageWindow, Mutation, MutationApplied, MutationRejected, Mutations, NotSupported, Offset, OffsetMismatch, Operation, OperationResult, OperationResults, Operations, producer_d_exports as Producer, ProducerGap, ProducerId, type ProducerOptions, ProducerPrecondition, ProducerState, ProtocolError, ReadError, type ReadFailure, type ReadMessage, ReadNextError, type ReadNextOptions, type ReadNextResult, type ReadOptions, type ReadResult, RecordPatch, RemoveError, type RemoveFailure, StaleEpoch, Storage, StorageCapabilities, StorageFault, type StorageShape, StoredMessage, address_d_exports as StreamAddress, StreamBusy, StreamClosed, StreamConfig, StreamGone, StreamId, StreamLifecycle, StreamNotFound, StreamRecord, StreamsFault, streamsy_d_exports as Streamsy, TransportFault, ZERO_OFFSET, compare, isProtocolError, isRetryableFault, isStreamsFault };
|
package/dist/index.js
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { t as __exportAll } from "./rolldown-runtime-D8jQpDG7.js";
|
|
2
2
|
import { $ as ProducerState, A as StreamBusy, C as InvalidEpochSeq, D as OffsetMismatch, E as NotSupported, G as StreamsFault, H as DecodeFault, J as isStreamsFault, K as TransportFault, L as Storage, M as StreamGone, N as StreamNotFound, O as ProducerGap, P as isProtocolError, Q as ProducerId, R as ZERO_OFFSET, S as InvalidCreateRequest, T as InvalidReadRequest, U as EncodeFault, W as StorageFault, X as MessageWindow, Y as ChangeSnapshot, Z as Offset, _ as CreateResult, a as custom, at as StreamRecord, b as ForkSourceNotFound, c as state, d as address_exports, et as RecordPatch, f as isControl, g as AppendResult, h as stateControl, it as StreamLifecycle, j as StreamClosed, k as StaleEpoch, m as stateChange, n as bind, nt as StreamConfig, o as group, p as isUpsert, q as isRetryableFault, r as bytes, rt as StreamId, s as json, t as Wildcard, tt as StoredMessage, v as AppendConflict, w as InvalidForkRequest, x as InvalidAppendRequest, y as CreateConflict, z as compare } from "./streamsy-HKC10a_V.js";
|
|
3
|
-
import {
|
|
4
|
-
import { a as MutationRejected, c as OperationResult, d as ProducerPrecondition, i as MutationApplied, l as OperationResults, o as Mutations, r as Mutation, s as Operation, t as layer$
|
|
3
|
+
import { p as CommitBoundary } from "./boundary-f1biibJ1.js";
|
|
4
|
+
import { a as MutationRejected, c as OperationResult, d as ProducerPrecondition, i as MutationApplied, l as OperationResults, o as Mutations, r as Mutation, s as Operation, t as layer$1, u as Operations } from "./layer-DjKTdIY5.js";
|
|
5
|
+
import { t as layer$2 } from "./layer-J6sV3UXD.js";
|
|
5
6
|
import { t as http_exports } from "./http-YoA8cUTN.js";
|
|
6
7
|
import { Array, Data, Effect, Layer, Option, Predicate, Record, Schema, Stream } from "effect";
|
|
7
8
|
// src/storage/capabilities.ts
|
|
@@ -21,10 +22,10 @@ var streams_exports = /* @__PURE__ */ __exportAll({
|
|
|
21
22
|
layerProtocol: () => layerProtocol
|
|
22
23
|
});
|
|
23
24
|
/** Generic reader and writer over memory storage, for protocol tools. */
|
|
24
|
-
const layerProtocol = (options = {}) => layer$
|
|
25
|
+
const layerProtocol = (options = {}) => layer$1(options).pipe(Layer.provideMerge(layer$2(options)));
|
|
25
26
|
function layer(group, options = {}) {
|
|
26
|
-
const storage = layer$
|
|
27
|
-
const protocol = layer$
|
|
27
|
+
const storage = layer$2(options);
|
|
28
|
+
const protocol = layer$1(options).pipe(Layer.provideMerge(storage));
|
|
28
29
|
return bind(protocol, group).pipe(Layer.provideMerge(storage));
|
|
29
30
|
}
|
|
30
31
|
// src/toolkit/durable-state.ts
|
package/dist/internal/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { Context, Effect, Option } from "effect";
|
|
1
|
+
import { I as ReadResult, a as ProjectionMarksApi, c as CommitHooksApi, d as OwnerRecords, f as OwnerRecordsApi, i as ProjectionMarks, l as commitHookRegistry, n as PendingMark, o as CommitHook, r as ProjectionMarkSummary, s as CommitHooks, t as MarkFailure, u as OWNER_RECORDS_LIST_UNSUPPORTED } from "../projection-marks-BEZv8qDJ.js";
|
|
2
|
+
import { Cause, Context, Effect, Option, Schema } from "effect";
|
|
3
3
|
/** Private bridge: a stream guard dies with this sentinel; the atomic runner translates it. */
|
|
4
4
|
declare const ForeignOwnerType: unique symbol;
|
|
5
5
|
declare const ForeignOwner_base: new <A extends Record<string, any> = {}>(args: import("effect/Types").VoidIfEmpty<{ readonly [P in keyof A as P extends "_tag" ? never : P]: A[P]; }>) => import("effect/Cause").YieldableError & {
|
|
@@ -55,6 +55,24 @@ interface TemplateGrammar<Template extends string = string> {
|
|
|
55
55
|
export declare function compileSegments<const Template extends string>(template: Template): TemplateGrammar<Template>;
|
|
56
56
|
/** A new private marks store. Each operation runs synchronously when its Effect executes. */
|
|
57
57
|
export declare const memoryProjectionMarks: () => ProjectionMarksApi;
|
|
58
|
+
interface TransactionParticipant {
|
|
59
|
+
/** Runs synchronously after the draft becomes committed, before wakes. Must not throw. */
|
|
60
|
+
readonly onCommit: () => void;
|
|
61
|
+
/**
|
|
62
|
+
* Runs synchronously when the outer transaction rolls back. Must not throw.
|
|
63
|
+
* A throw adds a defect to the transaction's cause. Effect's typed recovery
|
|
64
|
+
* gives defects precedence even when the exit still contains a typed failure.
|
|
65
|
+
*/
|
|
66
|
+
readonly onRollback: () => void;
|
|
67
|
+
}
|
|
68
|
+
declare const MemoryEnlistFault_base: Schema.Class<MemoryEnlistFault, Schema.TaggedStruct<"MemoryEnlistFault", {
|
|
69
|
+
readonly message: Schema.String;
|
|
70
|
+
}>, Cause.YieldableError & {
|
|
71
|
+
readonly brand: unique symbol;
|
|
72
|
+
}>;
|
|
73
|
+
declare class MemoryEnlistFault extends MemoryEnlistFault_base {}
|
|
74
|
+
/** Join the memory owner transaction open on this fiber. */
|
|
75
|
+
export declare function enlist(owner: object, participant: TransactionParticipant): Effect.Effect<void, MemoryEnlistFault>;
|
|
58
76
|
export declare const attachOffsets: <A extends object>(result: A, offsets: ReadonlyArray<string>) => A;
|
|
59
77
|
export declare const offsetsOf: (result: Pick<ReadResult, "messages">) => ReadonlyArray<string> | undefined;
|
|
60
|
-
export { type CommitHook, CommitHooks, type CommitHooksApi, type MarkFailure, OwnerRecords, type OwnerRecordsApi, type PendingMark, type ProjectionMarkSummary, ProjectionMarks, type ProjectionMarksApi, commitHookRegistry };
|
|
78
|
+
export { type CommitHook, CommitHooks, type CommitHooksApi, type MarkFailure, OWNER_RECORDS_LIST_UNSUPPORTED, OwnerRecords, type OwnerRecordsApi, type PendingMark, type ProjectionMarkSummary, ProjectionMarks, type ProjectionMarksApi, type TransactionParticipant, commitHookRegistry };
|
package/dist/internal/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
+
import { a as CommitHooks, c as OwnerRecords, i as ProjectionMarks, o as commitHookRegistry, r as memoryProjectionMarks, s as OWNER_RECORDS_LIST_UNSUPPORTED, t as enlist } from "../boundary-f1biibJ1.js";
|
|
1
2
|
import { a as attachOwner, c as isForeignOwner, i as CurrentOwner, l as ownerOf, t as compileSegments } from "../route-internal-DNL0oGTN.js";
|
|
2
3
|
import { n as offsetsOf, t as attachOffsets } from "../read-offsets-CQ4VkAs8.js";
|
|
3
|
-
import { _ as commitHookRegistry, g as CommitHooks, h as ProjectionMarks, s as memoryProjectionMarks, v as OwnerRecords } from "../marks-B-fXqEYG.js";
|
|
4
4
|
import { Array, Effect } from "effect";
|
|
5
5
|
// src/internal/limits.ts
|
|
6
6
|
/**
|
|
@@ -26,4 +26,4 @@ const fitKey = (key, limit, readable = key) => {
|
|
|
26
26
|
const prefix = Array.join(Array.take(codePoints(readable), limit - DIGEST_LENGTH), "");
|
|
27
27
|
return Effect.map(sha256Hex(key), (digest) => `${prefix}#${digest}`);
|
|
28
28
|
};
|
|
29
|
-
export { CommitHooks, CurrentOwner, MAX_PRODUCER_ID_LENGTH, OwnerRecords, ProjectionMarks, attachOffsets, attachOwner, commitHookRegistry, compileSegments, fitKey, isForeignOwner, memoryProjectionMarks, offsetsOf, ownerOf };
|
|
29
|
+
export { CommitHooks, CurrentOwner, MAX_PRODUCER_ID_LENGTH, OWNER_RECORDS_LIST_UNSUPPORTED, OwnerRecords, ProjectionMarks, attachOffsets, attachOwner, commitHookRegistry, compileSegments, enlist, fitKey, isForeignOwner, memoryProjectionMarks, offsetsOf, ownerOf };
|
|
@@ -1,73 +1,8 @@
|
|
|
1
1
|
import { L as Storage, R as ZERO_OFFSET } from "./streamsy-HKC10a_V.js";
|
|
2
|
+
import { a as CommitHooks, c as OwnerRecords, d as indexDeadlines, f as patchRecord, i as ProjectionMarks, l as copyMessage, n as makeBoundary, p as CommitBoundary, u as copyRecord } from "./boundary-f1biibJ1.js";
|
|
2
3
|
import { a as MutationRejected, l as OperationResults, o as Mutations, u as Operations } from "./layer-DjKTdIY5.js";
|
|
3
4
|
import { a as attachOwner, l as ownerOf } from "./route-internal-DNL0oGTN.js";
|
|
4
|
-
import {
|
|
5
|
-
import { Array, Context, Effect, Layer, Option, Order, Predicate, PubSub, Queue, Schedule, Semaphore, Stream } from "effect";
|
|
6
|
-
// src/storage/commit-boundary.ts
|
|
7
|
-
/** The transaction of the storage owner. Every Streamsy storage host provides it. */
|
|
8
|
-
var CommitBoundary = class extends Context.Service()("@streamsy/core/CommitBoundary") {};
|
|
9
|
-
// src/storage/memory/state.ts
|
|
10
|
-
const copyRecord = (record) => ({
|
|
11
|
-
id: record.id,
|
|
12
|
-
currentOffset: record.currentOffset,
|
|
13
|
-
config: { ...record.config },
|
|
14
|
-
lifecycle: { ...record.lifecycle }
|
|
15
|
-
});
|
|
16
|
-
const copyMessage = (message) => ({
|
|
17
|
-
offset: message.offset,
|
|
18
|
-
timestamp: message.timestamp,
|
|
19
|
-
data: new Uint8Array(message.data)
|
|
20
|
-
});
|
|
21
|
-
function patchRecord(record, patch) {
|
|
22
|
-
const config = {
|
|
23
|
-
...record.config,
|
|
24
|
-
...patch.config
|
|
25
|
-
};
|
|
26
|
-
const lifecycle = {
|
|
27
|
-
...record.lifecycle,
|
|
28
|
-
...patch.lifecycle
|
|
29
|
-
};
|
|
30
|
-
for (const key of patch.clear ?? []) switch (key) {
|
|
31
|
-
case "ttlSeconds":
|
|
32
|
-
delete config.ttlSeconds;
|
|
33
|
-
break;
|
|
34
|
-
case "expiresAt":
|
|
35
|
-
delete config.expiresAt;
|
|
36
|
-
break;
|
|
37
|
-
case "expiresAtMs":
|
|
38
|
-
delete lifecycle.expiresAtMs;
|
|
39
|
-
break;
|
|
40
|
-
case "lastSeq": delete lifecycle.lastSeq;
|
|
41
|
-
}
|
|
42
|
-
return {
|
|
43
|
-
id: record.id,
|
|
44
|
-
currentOffset: patch.currentOffset ?? record.currentOffset,
|
|
45
|
-
config,
|
|
46
|
-
lifecycle
|
|
47
|
-
};
|
|
48
|
-
}
|
|
49
|
-
function indexDeadlines(state) {
|
|
50
|
-
state.deadlines = Array.sort(Array.flatMap([...state.entries.values()], ({ record }) => record.lifecycle.softDeleted || record.lifecycle.expiresAtMs === void 0 ? [] : [{
|
|
51
|
-
at: record.lifecycle.expiresAtMs,
|
|
52
|
-
streamId: record.id
|
|
53
|
-
}]), Order.make((a, b) => {
|
|
54
|
-
const compared = a.at - b.at || a.streamId.localeCompare(b.streamId);
|
|
55
|
-
return compared < 0 ? -1 : compared > 0 ? 1 : 0;
|
|
56
|
-
}));
|
|
57
|
-
}
|
|
58
|
-
/** A private transaction draft; no mutable stream data aliases the committed state. */
|
|
59
|
-
const copyState = (state) => ({
|
|
60
|
-
entries: new Map(Array.map([...state.entries], ([id, entry]) => [id, {
|
|
61
|
-
record: copyRecord(entry.record),
|
|
62
|
-
messages: Array.map(entry.messages, copyMessage),
|
|
63
|
-
producers: new Map(Array.map([...entry.producers], ([producerId, producer]) => [producerId, {
|
|
64
|
-
epoch: producer.epoch,
|
|
65
|
-
lastSeq: producer.lastSeq
|
|
66
|
-
}]))
|
|
67
|
-
}])),
|
|
68
|
-
children: new Map(Array.map([...state.children], ([id, children]) => [id, new Set(children)])),
|
|
69
|
-
deadlines: Array.map(state.deadlines, (deadline) => ({ ...deadline }))
|
|
70
|
-
});
|
|
5
|
+
import { Array, Context, Effect, Layer, Option, Order, Predicate, PubSub, Queue, Schedule, Stream } from "effect";
|
|
71
6
|
// src/storage/memory/lineage.ts
|
|
72
7
|
/** Synchronous port of cascade-reclaim and chained message composition.
|
|
73
8
|
* Called only inside the memory mutation lock or synchronous read snapshot.
|
|
@@ -142,88 +77,6 @@ const createNotifier = Effect.gen(function* () {
|
|
|
142
77
|
})
|
|
143
78
|
};
|
|
144
79
|
});
|
|
145
|
-
// src/storage/memory/boundary.ts
|
|
146
|
-
var ActiveDraft = class extends Context.Service()("@streamsy/core/internal/MemoryActiveDraft") {};
|
|
147
|
-
const makeBoundary = (committed, publish) => Effect.gen(function* () {
|
|
148
|
-
const owner = {};
|
|
149
|
-
const lock = yield* Semaphore.make(1);
|
|
150
|
-
let committedRecords = /* @__PURE__ */ new Map();
|
|
151
|
-
let committedMarks = /* @__PURE__ */ new Map();
|
|
152
|
-
const hooks = commitHookRegistry();
|
|
153
|
-
const current = Effect.flatMap(Effect.serviceOption(ActiveDraft), (draft) => Effect.withFiber((fiber) => {
|
|
154
|
-
if (Option.isSome(draft) && (draft.value.owner !== owner || !draft.value.active || draft.value.fiberId !== fiber.id)) return Effect.die(/* @__PURE__ */ new Error("Memory boundary requires the active owner fiber"));
|
|
155
|
-
return Effect.succeed(draft);
|
|
156
|
-
}));
|
|
157
|
-
const withTransaction = (body) => Effect.flatMap(current, (ambient) => {
|
|
158
|
-
if (Option.isSome(ambient)) return body;
|
|
159
|
-
return Effect.withFiber((fiber) => Effect.suspend(() => {
|
|
160
|
-
const draft = {
|
|
161
|
-
owner,
|
|
162
|
-
fiberId: fiber.id,
|
|
163
|
-
state: copyState(committed),
|
|
164
|
-
records: new Map(committedRecords),
|
|
165
|
-
marks: copyMarks(committedMarks),
|
|
166
|
-
ids: /* @__PURE__ */ new Set(),
|
|
167
|
-
active: true,
|
|
168
|
-
changed: false
|
|
169
|
-
};
|
|
170
|
-
const withHooks = body.pipe(Effect.tap(() => Effect.orDie(hooks.run(draft.ids))), Effect.provideService(ActiveDraft, draft));
|
|
171
|
-
return Effect.uninterruptibleMask((restore) => restore(withHooks).pipe(Effect.tap(() => Effect.sync(() => {
|
|
172
|
-
Object.assign(committed, draft.state);
|
|
173
|
-
committedRecords = draft.records;
|
|
174
|
-
committedMarks = draft.marks;
|
|
175
|
-
}).pipe(Effect.andThen(Effect.suspend(() => draft.changed ? publish(draft.ids) : Effect.void)))), Effect.ensuring(Effect.sync(() => {
|
|
176
|
-
draft.active = false;
|
|
177
|
-
}))));
|
|
178
|
-
}).pipe(Semaphore.withPermit(lock)));
|
|
179
|
-
});
|
|
180
|
-
const access = (f) => Effect.map(current, (draft) => f(Option.isSome(draft) ? draft.value.state : committed));
|
|
181
|
-
const changed = (ids) => Effect.map(current, (draft) => {
|
|
182
|
-
if (Option.isNone(draft)) throw new Error("Memory mutation requires an owner");
|
|
183
|
-
draft.value.changed = true;
|
|
184
|
-
for (const id of ids) draft.value.ids.add(id);
|
|
185
|
-
});
|
|
186
|
-
const boundary = CommitBoundary.of({ withTransaction });
|
|
187
|
-
const records = OwnerRecords.of({
|
|
188
|
-
read: (key) => Effect.map(current, (draft) => Option.fromUndefinedOr((Option.isSome(draft) ? draft.value.records : committedRecords).get(key))),
|
|
189
|
-
remove: (key) => withTransaction(Effect.map(current, (draft) => {
|
|
190
|
-
if (Option.isNone(draft)) throw new Error("Memory record delete requires an owner");
|
|
191
|
-
draft.value.records.delete(key);
|
|
192
|
-
})),
|
|
193
|
-
write: (key, encoded) => withTransaction(Effect.map(current, (draft) => {
|
|
194
|
-
if (Option.isNone(draft)) throw new Error("Memory record write requires an owner");
|
|
195
|
-
draft.value.records.set(key, encoded);
|
|
196
|
-
}))
|
|
197
|
-
});
|
|
198
|
-
const readMarks = (f) => Effect.map(current, (draft) => f(Option.isSome(draft) ? draft.value.marks : committedMarks));
|
|
199
|
-
const writeMarks = (f) => withTransaction(Effect.map(current, (draft) => {
|
|
200
|
-
if (Option.isNone(draft)) throw new Error("Memory mark write requires an owner");
|
|
201
|
-
return f(draft.value.marks);
|
|
202
|
-
}));
|
|
203
|
-
const marks = ProjectionMarks.of({
|
|
204
|
-
mark: (projection, params) => writeMarks((table) => mark(table, projection, params)),
|
|
205
|
-
nextDue: (projection) => readMarks((table) => nextDue(table, projection)),
|
|
206
|
-
summary: (projection, now) => readMarks((table) => summary(table, projection, now)),
|
|
207
|
-
due: (projection, now, limit) => readMarks((table) => due(table, projection, now, limit)),
|
|
208
|
-
read: (projection, params) => readMarks((table) => read(table, projection, params)),
|
|
209
|
-
clear: (projection, params, seq) => writeMarks((table) => clear(table, projection, params, seq)),
|
|
210
|
-
sleep: (projection, params, seq, wakeAt) => writeMarks((table) => sleep(table, projection, params, seq, wakeAt)),
|
|
211
|
-
remove: (projection, params) => writeMarks((table) => remove(table, projection, params)),
|
|
212
|
-
fail: (projection, params, options) => writeMarks((table) => fail(table, projection, params, options)),
|
|
213
|
-
resume: (projection) => writeMarks((table) => resume(table, projection)),
|
|
214
|
-
failed: (projection, limit) => readMarks((table) => failed(table, projection, limit)),
|
|
215
|
-
retry: (projection, params) => writeMarks((table) => retry(table, projection, params))
|
|
216
|
-
});
|
|
217
|
-
return {
|
|
218
|
-
boundary: attachOwner(boundary, owner),
|
|
219
|
-
marks: attachOwner(marks, owner),
|
|
220
|
-
records: attachOwner(records, owner),
|
|
221
|
-
hooks: attachOwner(CommitHooks.of({ beforeCommit: hooks.beforeCommit }), owner),
|
|
222
|
-
withTransaction,
|
|
223
|
-
access,
|
|
224
|
-
changed
|
|
225
|
-
};
|
|
226
|
-
});
|
|
227
80
|
// src/storage/memory/changes.ts
|
|
228
81
|
function snapshot(state, id) {
|
|
229
82
|
const record = state.entries.get(id)?.record;
|
|
@@ -367,4 +220,4 @@ const layer = (options = {}) => Layer.effectContext(Effect.gen(function* () {
|
|
|
367
220
|
});
|
|
368
221
|
return Context.make(Storage, attachOwner(storage, ownerOf(owner.boundary))).pipe(Context.add(CommitBoundary, owner.boundary), Context.add(OwnerRecords, owner.records), Context.add(CommitHooks, owner.hooks), Context.add(ProjectionMarks, owner.marks));
|
|
369
222
|
}));
|
|
370
|
-
export {
|
|
223
|
+
export { layer as t };
|
|
@@ -1102,15 +1102,48 @@ interface StorageShape {
|
|
|
1102
1102
|
}
|
|
1103
1103
|
declare const Storage_base: Context.ServiceClass<Storage, "@streamsy/core/Storage", StorageShape>;
|
|
1104
1104
|
declare class Storage extends Storage_base {}
|
|
1105
|
+
interface CommitBoundaryApi {
|
|
1106
|
+
/**
|
|
1107
|
+
* Run `body` in the storage owner's transaction. Nested calls on the same owner
|
|
1108
|
+
* join without savepoints, so a failure must escape the outer body to roll back.
|
|
1109
|
+
* Same-owner stream writes and records commit with application SQL on that client.
|
|
1110
|
+
* With two boundaries over one SQL client, only the outer boundary's hooks and
|
|
1111
|
+
* `committed` subscribers see the commit.
|
|
1112
|
+
*
|
|
1113
|
+
* A nested call on another SQL client opens its own transaction, as does a lone
|
|
1114
|
+
* `Storage.mutate` on that client. Its commit comes first and survives an outer
|
|
1115
|
+
* rollback. Use one client per database for nested work. With two clients on
|
|
1116
|
+
* one database, SQLite can wait until it reports a busy lock. Postgres or MySQL
|
|
1117
|
+
* can wait for a row lock that the outer transaction holds.
|
|
1118
|
+
*
|
|
1119
|
+
* Durable Object clients share the driver's transaction, so a second client
|
|
1120
|
+
* joins the first. Using a second object's storage on one fiber is unsupported.
|
|
1121
|
+
* `MemoryStorage` dies when work enters another `MemoryStorage` owner.
|
|
1122
|
+
*
|
|
1123
|
+
* Keep the body on the calling fiber, and do not wait in it for wakes that only
|
|
1124
|
+
* its own commit publishes. Only SQL hosts fail with `SqlError`.
|
|
1125
|
+
*/
|
|
1126
|
+
readonly withTransaction: <A, E, R>(body: Effect.Effect<A, E, R>) => Effect.Effect<A, E | SqlError.SqlError, R>;
|
|
1127
|
+
}
|
|
1128
|
+
declare const CommitBoundary_base: Context.ServiceClass<CommitBoundary, "@streamsy/core/CommitBoundary", CommitBoundaryApi>;
|
|
1129
|
+
/** The transaction of the storage owner. Every Streamsy storage host provides it. */
|
|
1130
|
+
declare class CommitBoundary extends CommitBoundary_base {}
|
|
1105
1131
|
/**
|
|
1106
1132
|
* Encoded records on the storage owner, for the projection checkpoint store.
|
|
1107
1133
|
* Calls inside `CommitBoundary.withTransaction` join that transaction.
|
|
1108
1134
|
*/
|
|
1109
1135
|
interface OwnerRecordsApi {
|
|
1110
1136
|
readonly read: (key: string) => Effect.Effect<Option.Option<string>, StorageFault>;
|
|
1137
|
+
/**
|
|
1138
|
+
* A snapshot of the owner's encoded entries whose keys start with this exact prefix.
|
|
1139
|
+
* A store that cannot list fails with operation `OWNER_RECORDS_LIST_UNSUPPORTED`.
|
|
1140
|
+
*/
|
|
1141
|
+
readonly list: (prefix: string) => Effect.Effect<ReadonlyArray<readonly [key: string, encoded: string]>, StorageFault>;
|
|
1111
1142
|
readonly write: (key: string, encoded: string) => Effect.Effect<void, StorageFault>;
|
|
1112
1143
|
readonly remove: (key: string) => Effect.Effect<void, StorageFault>;
|
|
1113
1144
|
}
|
|
1145
|
+
/** The `StorageFault.operation` of an owner store that cannot list its records. */
|
|
1146
|
+
declare const OWNER_RECORDS_LIST_UNSUPPORTED = "records.list.unsupported";
|
|
1114
1147
|
declare const OwnerRecords_base: Context.ServiceClass<OwnerRecords, "@streamsy/core/internal/OwnerRecords", OwnerRecordsApi>;
|
|
1115
1148
|
declare class OwnerRecords extends OwnerRecords_base {}
|
|
1116
1149
|
/**
|
|
@@ -1174,10 +1207,12 @@ interface ProjectionMarkSummary {
|
|
|
1174
1207
|
/**
|
|
1175
1208
|
* Pending marks for attached projections, on a storage owner or in private memory.
|
|
1176
1209
|
*
|
|
1177
|
-
* A mark is keyed by projection id and canonical params.
|
|
1178
|
-
*
|
|
1210
|
+
* A mark is keyed by projection id and canonical params. A deploy that raises the
|
|
1211
|
+
* generation picks up the existing marks.
|
|
1179
1212
|
*/
|
|
1180
1213
|
interface ProjectionMarksApi {
|
|
1214
|
+
/** Maximum projection id length in Unicode code points, when bounded by storage. */
|
|
1215
|
+
readonly maxProjectionIdLength?: number;
|
|
1181
1216
|
/**
|
|
1182
1217
|
* Insert a mark for each binding, or raise its `seq`. A raise keeps
|
|
1183
1218
|
* `attempts`, `stopped` and `failure`, so a write never resets backoff or a stop.
|
|
@@ -1218,4 +1253,4 @@ interface ProjectionMarksApi {
|
|
|
1218
1253
|
}
|
|
1219
1254
|
declare const ProjectionMarks_base: Context.ServiceClass<ProjectionMarks, "@streamsy/core/internal/ProjectionMarks", ProjectionMarksApi>;
|
|
1220
1255
|
declare class ProjectionMarks extends ProjectionMarks_base {}
|
|
1221
|
-
export {
|
|
1256
|
+
export { ProducerGap as $, Writer as A, StreamsFault as At, AppendErrorFor as B, OperationResults as C, StreamConfig as Ct, Reader as D, DecodeFault as Dt, StorageCapabilities as E, StreamRecord as Et, ReadNextResult as F, HeadError as G, CreateConflictReason as H, ReadResult as I, InvalidEpochSeq as J, InvalidAppendRequest as K, AppendBaseError as L, CreateResult as M, isRetryableFault as Mt, HeadResult as N, isStreamsFault as Nt, StreamsReader as O, EncodeFault as Ot, ReadMessage as P, OffsetMismatch as Q, AppendConflict as R, OperationResult as S, StoredMessage as St, ProducerPrecondition as T, StreamLifecycle as Tt, CreateError as U, CreateConflict as V, ForkSourceNotFound as W, InvalidReadRequest as X, InvalidForkRequest as Y, NotSupported as Z, Mutation as _, MessageWindow as _t, ProjectionMarksApi as a, StreamBusy as at, Mutations as b, ProducerState as bt, CommitHooksApi as c, StreamNotFound as ct, OwnerRecords as d, AppendOptions as dt, ProtocolError as et, OwnerRecordsApi as f, ProducerOptions as ft, StorageShape as g, ChangeSnapshot as gt, Storage as h, CreateOptions as ht, ProjectionMarks as i, StaleEpoch as it, AppendResult as j, TransportFault as jt, StreamsWriter as k, StorageFault as kt, commitHookRegistry as l, isProtocolError as lt, CommitBoundaryApi as m, ReadOptions as mt, PendingMark as n, ReadNextError as nt, CommitHook as o, StreamClosed as ot, CommitBoundary as p, ReadNextOptions as pt, InvalidCreateRequest as q, ProjectionMarkSummary as r, RemoveError as rt, CommitHooks as s, StreamGone as st, MarkFailure as t, ReadError as tt, OWNER_RECORDS_LIST_UNSUPPORTED as u, AppendCallOptions as ut, MutationApplied as v, Offset as vt, Operations as w, StreamId as wt, Operation as x, RecordPatch as xt, MutationRejected as y, ProducerId as yt, AppendError as z };
|
package/dist/protocol/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { A as Writer, D as Reader, O as StreamsReader, k as StreamsWriter } from "../projection-marks-BEZv8qDJ.js";
|
|
2
|
+
import { J as layer_d_exports, V as streams_d_exports, Z as route_d_exports, w as RoutesOptions } from "../index-BtVxu6VJ.js";
|
|
3
3
|
import { HttpRouter } from "effect/http";
|
|
4
4
|
/** Raw protocol route for adapters and wire conformance tests. */
|
|
5
5
|
export declare const routes: (options?: Omit<RoutesOptions, "access">) => import("effect/Layer").Layer<never, never, HttpRouter.HttpRouter | HttpRouter.Request<"Requires", StreamsReader> | HttpRouter.Request<"Requires", StreamsWriter>>;
|
package/dist/remote/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { D as Group, E as Any, O as Identifiers } from "../index-
|
|
1
|
+
import { O as StreamsReader, jt as TransportFault, k as StreamsWriter } from "../projection-marks-BEZv8qDJ.js";
|
|
2
|
+
import { D as Group, E as Any, O as Identifiers } from "../index-BtVxu6VJ.js";
|
|
3
3
|
import { Layer } from "effect";
|
|
4
4
|
import { HttpClient, HttpClientRequest } from "effect/http";
|
|
5
5
|
interface Options {
|
package/dist/testing/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { K as MemoryOptions, q as ProtocolOptions } from "../index-
|
|
1
|
+
import { E as StorageCapabilities, O as StreamsReader, gt as ChangeSnapshot, h as Storage, k as StreamsWriter, wt as StreamId } from "../projection-marks-BEZv8qDJ.js";
|
|
2
|
+
import { K as MemoryOptions, q as ProtocolOptions } from "../index-BtVxu6VJ.js";
|
|
3
3
|
import { Context, Effect, Layer } from "effect";
|
|
4
4
|
interface StorageContractOptions {
|
|
5
5
|
readonly name: string;
|
package/dist/testing/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { L as Storage, Q as ProducerId, R as ZERO_OFFSET, V as next, W as StorageFault, rt as StreamId } from "../streamsy-HKC10a_V.js";
|
|
2
|
-
import {
|
|
3
|
-
import { a as MutationRejected, t as layer
|
|
4
|
-
import {
|
|
2
|
+
import { a as CommitHooks, c as OwnerRecords, i as ProjectionMarks, p as CommitBoundary } from "../boundary-f1biibJ1.js";
|
|
3
|
+
import { a as MutationRejected, t as layer } from "../layer-DjKTdIY5.js";
|
|
4
|
+
import { t as layer$1 } from "../layer-J6sV3UXD.js";
|
|
5
5
|
import { Array, Cause, Context, Deferred, Effect, Exit, Fiber, Layer, Match, Option, Order, Predicate, PubSub, Queue, Record, Ref, Scope, Stream } from "effect";
|
|
6
6
|
import { SqlError } from "effect/sql";
|
|
7
7
|
import { describe, expect, it } from "vitest";
|
|
@@ -1299,13 +1299,13 @@ const layerTest = (options = {}) => Layer.effectContext(Effect.gen(function* ()
|
|
|
1299
1299
|
})));
|
|
1300
1300
|
}))
|
|
1301
1301
|
});
|
|
1302
|
-
const context = yield* Layer.build(layer
|
|
1302
|
+
const context = yield* Layer.build(layer(options).pipe(Layer.provide(Layer.succeed(Storage, observed))));
|
|
1303
1303
|
return Context.add(context, StreamsTest, StreamsTest.of({
|
|
1304
1304
|
storage: observed,
|
|
1305
1305
|
subscribers: Ref.get(count),
|
|
1306
1306
|
snapshot: Queue.take(snapshots)
|
|
1307
1307
|
}));
|
|
1308
|
-
})).pipe(Layer.provide(layer(options)));
|
|
1308
|
+
})).pipe(Layer.provide(layer$1(options)));
|
|
1309
1309
|
// src/testing/expect-failure-tag.ts
|
|
1310
1310
|
/** An unexpected success stays a failure so the enclosing test cannot pass. */
|
|
1311
1311
|
const expectFailureTag = (effect, tag) => effect.pipe(Effect.flip, Effect.tap((error) => Effect.sync(() => expect(error._tag).toBe(tag))));
|
package/package.json
CHANGED
package/dist/marks-B-fXqEYG.js
DELETED
|
@@ -1,195 +0,0 @@
|
|
|
1
|
-
import { Array, Context, Effect, Option, Order } from "effect";
|
|
2
|
-
// src/internal/owner-records.ts
|
|
3
|
-
var OwnerRecords = class extends Context.Service()("@streamsy/core/internal/OwnerRecords") {};
|
|
4
|
-
// src/internal/commit-hooks.ts
|
|
5
|
-
var CommitHooks = class extends Context.Service()("@streamsy/core/internal/CommitHooks") {};
|
|
6
|
-
/** Shared registry for storage boundaries: register in a scope, run in order. */
|
|
7
|
-
const commitHookRegistry = () => {
|
|
8
|
-
const hooks = /* @__PURE__ */ new Set();
|
|
9
|
-
const beforeCommit = (hook) => Effect.acquireRelease(Effect.sync(() => {
|
|
10
|
-
const entry = { hook };
|
|
11
|
-
hooks.add(entry);
|
|
12
|
-
return entry;
|
|
13
|
-
}), (entry) => Effect.sync(() => {
|
|
14
|
-
hooks.delete(entry);
|
|
15
|
-
})).pipe(Effect.asVoid);
|
|
16
|
-
const run = (ids) => ids.size === 0 || hooks.size === 0 ? Effect.void : Effect.forEach([...hooks], (entry) => entry.hook(ids), { discard: true });
|
|
17
|
-
return {
|
|
18
|
-
beforeCommit,
|
|
19
|
-
run
|
|
20
|
-
};
|
|
21
|
-
};
|
|
22
|
-
// src/internal/projection-marks.ts
|
|
23
|
-
var ProjectionMarks = class extends Context.Service()("@streamsy/core/internal/ProjectionMarks") {};
|
|
24
|
-
// src/storage/memory/marks.ts
|
|
25
|
-
/** A private transaction draft of the marks. */
|
|
26
|
-
const copyMarks = (marks) => new Map(Array.map([...marks], ([projection, rows]) => [projection, new Map(rows)]));
|
|
27
|
-
const rowsOf = (marks, projection) => {
|
|
28
|
-
const found = marks.get(projection);
|
|
29
|
-
if (found !== void 0) return found;
|
|
30
|
-
const rows = /* @__PURE__ */ new Map();
|
|
31
|
-
marks.set(projection, rows);
|
|
32
|
-
return rows;
|
|
33
|
-
};
|
|
34
|
-
const byDue = Order.mapInput(Order.Tuple([Order.Number, Order.String]), (mark) => [mark.dueAt, mark.params]);
|
|
35
|
-
const mark = (marks, projection, params) => {
|
|
36
|
-
const rows = rowsOf(marks, projection);
|
|
37
|
-
for (const value of params) {
|
|
38
|
-
const found = rows.get(value);
|
|
39
|
-
rows.set(value, found === void 0 ? {
|
|
40
|
-
params: value,
|
|
41
|
-
seq: 1,
|
|
42
|
-
attempts: 0,
|
|
43
|
-
dueAt: 0,
|
|
44
|
-
stopped: false,
|
|
45
|
-
wakeAt: Option.none(),
|
|
46
|
-
failure: Option.none()
|
|
47
|
-
} : {
|
|
48
|
-
...found,
|
|
49
|
-
seq: found.seq + 1,
|
|
50
|
-
dueAt: Option.isSome(found.wakeAt) ? 0 : found.dueAt,
|
|
51
|
-
wakeAt: Option.none()
|
|
52
|
-
});
|
|
53
|
-
}
|
|
54
|
-
};
|
|
55
|
-
const nextDue = (marks, projection) => {
|
|
56
|
-
let earliest;
|
|
57
|
-
for (const row of marks.get(projection)?.values() ?? []) if (!row.stopped) earliest = earliest === void 0 ? row.dueAt : Math.min(earliest, row.dueAt);
|
|
58
|
-
return Option.fromUndefinedOr(earliest);
|
|
59
|
-
};
|
|
60
|
-
const due = (marks, projection, now, limit) => Array.sort(Array.filter([...marks.get(projection)?.values() ?? []], (row) => !row.stopped && row.dueAt <= now), byDue).slice(0, limit);
|
|
61
|
-
const read = (marks, projection, params) => {
|
|
62
|
-
const rows = marks.get(projection);
|
|
63
|
-
const found = /* @__PURE__ */ new Map();
|
|
64
|
-
for (const value of params) {
|
|
65
|
-
const row = rows?.get(value);
|
|
66
|
-
if (row !== void 0) found.set(value, row);
|
|
67
|
-
}
|
|
68
|
-
return found;
|
|
69
|
-
};
|
|
70
|
-
const clear = (marks, projection, params, seq) => {
|
|
71
|
-
const rows = marks.get(projection);
|
|
72
|
-
const row = rows?.get(params);
|
|
73
|
-
if (rows === void 0 || row === void 0) return;
|
|
74
|
-
if (row.seq === seq) rows.delete(params);
|
|
75
|
-
else rows.set(params, {
|
|
76
|
-
...row,
|
|
77
|
-
attempts: 0,
|
|
78
|
-
dueAt: 0,
|
|
79
|
-
stopped: false,
|
|
80
|
-
wakeAt: Option.none(),
|
|
81
|
-
failure: Option.none()
|
|
82
|
-
});
|
|
83
|
-
};
|
|
84
|
-
/** Sleep only the seq that ran; a concurrent write must run before it can sleep. */
|
|
85
|
-
const sleep = (marks, projection, params, seq, wakeAt) => {
|
|
86
|
-
const rows = rowsOf(marks, projection);
|
|
87
|
-
const row = rows.get(params);
|
|
88
|
-
const matching = row === void 0 || row.seq === seq;
|
|
89
|
-
rows.set(params, {
|
|
90
|
-
params,
|
|
91
|
-
seq: row?.seq ?? 1,
|
|
92
|
-
attempts: 0,
|
|
93
|
-
dueAt: matching ? wakeAt : 0,
|
|
94
|
-
wakeAt: matching ? Option.some(wakeAt) : Option.none(),
|
|
95
|
-
stopped: false,
|
|
96
|
-
failure: Option.none()
|
|
97
|
-
});
|
|
98
|
-
};
|
|
99
|
-
const remove = (marks, projection, params) => {
|
|
100
|
-
marks.get(projection)?.delete(params);
|
|
101
|
-
};
|
|
102
|
-
const fail = (marks, projection, params, options) => {
|
|
103
|
-
const rows = rowsOf(marks, projection);
|
|
104
|
-
const found = rows.get(params);
|
|
105
|
-
rows.set(params, {
|
|
106
|
-
params,
|
|
107
|
-
seq: found?.seq ?? 1,
|
|
108
|
-
attempts: options.attempts,
|
|
109
|
-
dueAt: Option.getOrElse(options.dueAt, () => 0),
|
|
110
|
-
stopped: Option.isNone(options.dueAt),
|
|
111
|
-
wakeAt: Option.none(),
|
|
112
|
-
failure: Option.some({
|
|
113
|
-
...options.failure,
|
|
114
|
-
message: options.failure.message.slice(0, 1e3)
|
|
115
|
-
})
|
|
116
|
-
});
|
|
117
|
-
};
|
|
118
|
-
const failed = (marks, projection, limit) => Array.sort(Array.filter([...marks.get(projection)?.values() ?? []], (row) => row.attempts > 0), Order.combine(Order.mapInput(Order.Boolean, (row) => !row.stopped), byDue)).slice(0, limit);
|
|
119
|
-
const resume = (marks, projection) => {
|
|
120
|
-
let changed = 0;
|
|
121
|
-
const rows = marks.get(projection);
|
|
122
|
-
if (rows === void 0) return 0;
|
|
123
|
-
for (const [key, row] of rows) {
|
|
124
|
-
if (!row.stopped) continue;
|
|
125
|
-
rows.set(key, {
|
|
126
|
-
...row,
|
|
127
|
-
dueAt: 0,
|
|
128
|
-
stopped: false
|
|
129
|
-
});
|
|
130
|
-
changed += 1;
|
|
131
|
-
}
|
|
132
|
-
return changed;
|
|
133
|
-
};
|
|
134
|
-
const retry = (marks, projection, params) => {
|
|
135
|
-
let changed = 0;
|
|
136
|
-
const rows = marks.get(projection);
|
|
137
|
-
if (rows === void 0) return 0;
|
|
138
|
-
for (const [key, row] of rows) {
|
|
139
|
-
if (!row.stopped && (row.attempts === 0 || row.dueAt === 0) || params !== void 0 && key !== params) continue;
|
|
140
|
-
rows.set(key, {
|
|
141
|
-
...row,
|
|
142
|
-
dueAt: 0,
|
|
143
|
-
stopped: false
|
|
144
|
-
});
|
|
145
|
-
changed += 1;
|
|
146
|
-
}
|
|
147
|
-
return changed;
|
|
148
|
-
};
|
|
149
|
-
/** Classify each row once; a failed row at `now` is due, not backing off. */
|
|
150
|
-
const summary = (marks, projection, now) => {
|
|
151
|
-
let fresh = 0;
|
|
152
|
-
let sleeping = 0;
|
|
153
|
-
let retryDue = 0;
|
|
154
|
-
let backingOff = 0;
|
|
155
|
-
let stopped = 0;
|
|
156
|
-
let earliest;
|
|
157
|
-
for (const row of marks.get(projection)?.values() ?? []) {
|
|
158
|
-
if (row.stopped) {
|
|
159
|
-
stopped += 1;
|
|
160
|
-
continue;
|
|
161
|
-
}
|
|
162
|
-
if (Option.isSome(row.wakeAt)) sleeping += 1;
|
|
163
|
-
else if (row.attempts === 0) fresh += 1;
|
|
164
|
-
else if (row.dueAt <= now) retryDue += 1;
|
|
165
|
-
else backingOff += 1;
|
|
166
|
-
earliest = earliest === void 0 ? row.dueAt : Math.min(earliest, row.dueAt);
|
|
167
|
-
}
|
|
168
|
-
return {
|
|
169
|
-
fresh,
|
|
170
|
-
sleeping,
|
|
171
|
-
retryDue,
|
|
172
|
-
backingOff,
|
|
173
|
-
stopped,
|
|
174
|
-
nextDueAt: Option.fromUndefinedOr(earliest)
|
|
175
|
-
};
|
|
176
|
-
};
|
|
177
|
-
/** A new private marks store. Each operation runs synchronously when its Effect executes. */
|
|
178
|
-
const memoryProjectionMarks = () => {
|
|
179
|
-
const table = /* @__PURE__ */ new Map();
|
|
180
|
-
return {
|
|
181
|
-
mark: (projection, params) => Effect.sync(() => mark(table, projection, params)),
|
|
182
|
-
nextDue: (projection) => Effect.sync(() => nextDue(table, projection)),
|
|
183
|
-
summary: (projection, now) => Effect.sync(() => summary(table, projection, now)),
|
|
184
|
-
due: (projection, now, limit) => Effect.sync(() => due(table, projection, now, limit)),
|
|
185
|
-
read: (projection, params) => Effect.sync(() => read(table, projection, params)),
|
|
186
|
-
clear: (projection, params, seq) => Effect.sync(() => clear(table, projection, params, seq)),
|
|
187
|
-
sleep: (projection, params, seq, wakeAt) => Effect.sync(() => sleep(table, projection, params, seq, wakeAt)),
|
|
188
|
-
remove: (projection, params) => Effect.sync(() => remove(table, projection, params)),
|
|
189
|
-
fail: (projection, params, options) => Effect.sync(() => fail(table, projection, params, options)),
|
|
190
|
-
retry: (projection, params) => Effect.sync(() => retry(table, projection, params)),
|
|
191
|
-
resume: (projection) => Effect.sync(() => resume(table, projection)),
|
|
192
|
-
failed: (projection, limit) => Effect.sync(() => failed(table, projection, limit))
|
|
193
|
-
};
|
|
194
|
-
};
|
|
195
|
-
export { commitHookRegistry as _, failed as a, nextDue as c, resume as d, retry as f, CommitHooks as g, ProjectionMarks as h, fail as i, read as l, summary as m, copyMarks as n, mark as o, sleep as p, due as r, memoryProjectionMarks as s, clear as t, remove as u, OwnerRecords as v };
|