@photon-grove/evt 0.1.2 → 0.2.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.
Files changed (66) hide show
  1. package/README.md +15 -3
  2. package/dist/dynamodb/head.d.ts +6 -0
  3. package/dist/dynamodb/head.d.ts.map +1 -1
  4. package/dist/dynamodb/head.js +11 -0
  5. package/dist/dynamodb/head.js.map +1 -1
  6. package/dist/dynamodb/index.d.ts +1 -1
  7. package/dist/dynamodb/index.d.ts.map +1 -1
  8. package/dist/dynamodb/index.js +1 -1
  9. package/dist/dynamodb/index.js.map +1 -1
  10. package/dist/dynamodb/repository.d.ts +9 -2
  11. package/dist/dynamodb/repository.d.ts.map +1 -1
  12. package/dist/dynamodb/repository.js +114 -35
  13. package/dist/dynamodb/repository.js.map +1 -1
  14. package/dist/dynamodb/requests.d.ts +36 -5
  15. package/dist/dynamodb/requests.d.ts.map +1 -1
  16. package/dist/dynamodb/requests.js +56 -13
  17. package/dist/dynamodb/requests.js.map +1 -1
  18. package/dist/errors.d.ts +38 -4
  19. package/dist/errors.d.ts.map +1 -1
  20. package/dist/errors.js +45 -4
  21. package/dist/errors.js.map +1 -1
  22. package/dist/index.d.ts +3 -2
  23. package/dist/index.d.ts.map +1 -1
  24. package/dist/index.js +3 -1
  25. package/dist/index.js.map +1 -1
  26. package/dist/memory/repository.d.ts +3 -2
  27. package/dist/memory/repository.d.ts.map +1 -1
  28. package/dist/memory/repository.js +78 -29
  29. package/dist/memory/repository.js.map +1 -1
  30. package/dist/postgres/repository.d.ts +9 -2
  31. package/dist/postgres/repository.d.ts.map +1 -1
  32. package/dist/postgres/repository.js +107 -36
  33. package/dist/postgres/repository.js.map +1 -1
  34. package/dist/postgres/schema.d.ts.map +1 -1
  35. package/dist/postgres/schema.js +16 -0
  36. package/dist/postgres/schema.js.map +1 -1
  37. package/dist/repository.d.ts +75 -0
  38. package/dist/repository.d.ts.map +1 -1
  39. package/dist/repository.js +89 -3
  40. package/dist/repository.js.map +1 -1
  41. package/dist/staging.d.ts +28 -0
  42. package/dist/staging.d.ts.map +1 -0
  43. package/dist/staging.js +89 -0
  44. package/dist/staging.js.map +1 -0
  45. package/dist/store.d.ts +11 -1
  46. package/dist/store.d.ts.map +1 -1
  47. package/dist/store.js +30 -71
  48. package/dist/store.js.map +1 -1
  49. package/dist/transaction.d.ts +113 -0
  50. package/dist/transaction.d.ts.map +1 -0
  51. package/dist/transaction.js +207 -0
  52. package/dist/transaction.js.map +1 -0
  53. package/package.json +1 -1
  54. package/src/dynamodb/head.ts +15 -0
  55. package/src/dynamodb/index.ts +1 -0
  56. package/src/dynamodb/repository.ts +162 -41
  57. package/src/dynamodb/requests.ts +98 -14
  58. package/src/errors.ts +63 -4
  59. package/src/index.ts +16 -7
  60. package/src/memory/repository.ts +93 -32
  61. package/src/postgres/repository.ts +133 -48
  62. package/src/postgres/schema.ts +16 -0
  63. package/src/repository.ts +169 -2
  64. package/src/staging.ts +138 -0
  65. package/src/store.ts +47 -96
  66. package/src/transaction.ts +322 -0
package/src/store.ts CHANGED
@@ -5,16 +5,20 @@ import {
5
5
  type Aggregate,
6
6
  type EventDefinitions,
7
7
  type EventOf,
8
- type Projection,
9
8
  type Rejection,
10
- encodeEventData,
11
9
  replayEvent,
12
10
  } from './aggregate.js'
13
11
  import {commitIds} from './commit-id.js'
14
12
  import {EvtError, SequenceGapError, SnapshotError, isConflict, isRetryable} from './errors.js'
15
- import {type SerializedEvent, eventId} from './event.js'
13
+ import type {SerializedEvent} from './event.js'
16
14
  import {type Metadata, emptyMetadata} from './metadata.js'
17
- import type {CallOptions, NewSnapshot, ProjectorGroup, Repository} from './repository.js'
15
+ import type {
16
+ CallOptions,
17
+ NewSnapshot,
18
+ ProjectorGroup,
19
+ Repository,
20
+ TransactionalRepository,
21
+ } from './repository.js'
18
22
  import {
19
23
  DEFAULT_RETRY_POLICY,
20
24
  type RetryPolicy,
@@ -22,7 +26,8 @@ import {
22
26
  retryDelay,
23
27
  sleep,
24
28
  } from './retry.js'
25
- import {MAX_COMMIT_ITEM_BYTES, snapshotCommitSize, snapshotEventsDue} from './snapshots.js'
29
+ import {commitSnapshot, projectCommit, serializeEvents} from './staging.js'
30
+ import {Transaction} from './transaction.js'
26
31
 
27
32
  /** The default number of events between snapshots, as in Go and Rust. */
28
33
  export const DEFAULT_SNAPSHOT_INTERVAL = 5
@@ -30,8 +35,6 @@ export const DEFAULT_SNAPSHOT_INTERVAL = 5
30
35
  // How many times a load reads the stream when it finds a sequence gap.
31
36
  const LOAD_ATTEMPTS = 3
32
37
 
33
- const INT64_MAX = 2n ** 63n - 1n
34
-
35
38
  /** How a {@link Store} fills in what commands leave out. */
36
39
  export interface StoreOptions {
37
40
  /** The region metadata gets when a command's metadata has none. */
@@ -220,6 +223,28 @@ export class Store {
220
223
  /* oxlint-enable no-await-in-loop */
221
224
  }
222
225
 
226
+ /**
227
+ * Begins a transaction that reads streams of any aggregate types, appends to some of them, and
228
+ * commits them all at once, with a read fence on every stream it reads but doesn't write. The
229
+ * store's region, clock, commit IDs and snapshot interval apply to it, and each stream's
230
+ * aggregate projectors run in its commit. The repository must be a
231
+ * {@link TransactionalRepository}, as every one this package ships is, or this throws
232
+ * `invalidArgument`. {@link execute} stays the shortcut for one stream.
233
+ */
234
+ transaction(): Transaction {
235
+ const repository = this.repository
236
+ if (!isTransactional(repository)) {
237
+ throw new EvtError('invalidArgument', 'the repository has no commitTransaction')
238
+ }
239
+
240
+ return new Transaction({
241
+ repository,
242
+ load: (aggregate, entityId, options) => this.load(aggregate, entityId, options),
243
+ snapshotInterval: (aggregate) => this.snapshotInterval(aggregate),
244
+ metadata: (partial) => this.#metadata(partial),
245
+ })
246
+ }
247
+
223
248
  async #loadOnce<S, C, D extends EventDefinitions>(
224
249
  aggregate: Aggregate<S, C, D>,
225
250
  entityId: string,
@@ -284,48 +309,16 @@ export class Store {
284
309
  projections: ProjectorGroup[]
285
310
  } {
286
311
  const entityId = loaded.entityId
287
- let sequence = loaded.sequence
288
- let state = loaded.state
289
- const events: SerializedEvent[] = []
290
-
291
- for (const event of decided) {
292
- if (sequence >= INT64_MAX) {
293
- throw new EvtError(
294
- 'invalidCommit',
295
- `invalid commit: entity ${entityId} is at the maximum event sequence`,
296
- )
297
- }
298
- sequence++
299
-
300
- const {version, payload} = encodeEventData(aggregate.events, event)
301
- events.push({
302
- id: eventId(entityId, sequence),
303
- sequence,
304
- type: event.type,
305
- version,
306
- entityId,
307
- entityType: aggregate.type,
308
- payload,
309
- metadata: copyMetadata(metadata),
310
- })
311
- state = aggregate.apply(state, event)
312
- }
313
-
312
+ const {events, state} = serializeEvents(
313
+ aggregate,
314
+ entityId,
315
+ loaded.sequence,
316
+ loaded.state,
317
+ decided,
318
+ metadata,
319
+ )
314
320
  const interval = this.snapshotInterval(aggregate)
315
- let snapshot: NewSnapshot | null = null
316
- if (snapshotEventsDue(loaded.sequence, decided.length, interval) > 0) {
317
- snapshot = {
318
- entityType: aggregate.type,
319
- sequence: loaded.snapshotSequence + 1n,
320
- payload: aggregate.state.encode(state),
321
- }
322
-
323
- // A snapshot over the per-item budget would fail every commit that carries it.
324
- const size = snapshotCommitSize({...snapshot, entityId, eventSequence: sequence})
325
- if (size > MAX_COMMIT_ITEM_BYTES) {
326
- snapshot = null
327
- }
328
- }
321
+ const snapshot = commitSnapshot(aggregate, entityId, interval, loaded, events.length, state)
329
322
 
330
323
  const seenCommandIds = new Set(loaded.seenCommandIds)
331
324
  if (metadata.commandId !== null && metadata.commandId !== '') {
@@ -336,52 +329,16 @@ export class Store {
336
329
  state: {
337
330
  entityId,
338
331
  state,
339
- sequence,
332
+ sequence: loaded.sequence + BigInt(events.length),
340
333
  snapshotSequence: snapshot ? snapshot.sequence : loaded.snapshotSequence,
341
334
  seenCommandIds,
342
335
  },
343
336
  events,
344
337
  snapshot,
345
- projections: this.#project(aggregate, entityId, state, events, snapshot),
338
+ projections: projectCommit(aggregate, entityId, state, events, snapshot),
346
339
  }
347
340
  }
348
341
 
349
- // The operations the aggregate's projectors add to the commit, in declaration order.
350
- #project<S, C, D extends EventDefinitions>(
351
- aggregate: Aggregate<S, C, D>,
352
- entityId: string,
353
- state: S,
354
- events: SerializedEvent[],
355
- snapshot: NewSnapshot | null,
356
- ): ProjectorGroup[] {
357
- const projectors = aggregate.projectors ?? []
358
- if (projectors.length === 0) {
359
- return []
360
- }
361
-
362
- // Each projector gets its own frozen copy of the events, their metadata and every byte array,
363
- // so none can change what another one sees, or what the commit writes.
364
- const encodedState = snapshot?.payload ?? aggregate.state.encode(state)
365
- const detached = (): Projection<S> =>
366
- Object.freeze({
367
- entityType: aggregate.type,
368
- entityId,
369
- state,
370
- encodedState: encodedState.slice(),
371
- events: Object.freeze(
372
- events.map((event) =>
373
- Object.freeze({
374
- ...event,
375
- payload: event.payload && event.payload.slice(),
376
- metadata: Object.freeze(copyMetadata(event.metadata)),
377
- }),
378
- ),
379
- ),
380
- })
381
-
382
- return projectors.flatMap((projector) => projector.project(detached()) ?? [])
383
- }
384
-
385
342
  // The command's metadata with the store's region and the clock's timestamp where it has none,
386
343
  // and the commit ID the engine stamps: the command ID, else the caller's, else a generated one.
387
344
  // Field by field, so a field an untyped caller set to undefined is empty too, and every key is
@@ -411,17 +368,11 @@ export class Store {
411
368
  }
412
369
  }
413
370
 
414
- // Each stored event gets its own metadata, so changing one changes no other.
415
- function copyMetadata(metadata: Metadata): Metadata {
416
- return {
417
- ...metadata,
418
- trace: metadata.trace && new Map(metadata.trace),
419
- origin: metadata.origin && {...metadata.origin},
420
- extensions: new Map(metadata.extensions),
421
- }
422
- }
423
-
424
371
  // RFC 3339 in UTC, to the second, as Go's time.RFC3339 formats a UTC time.
425
372
  function rfc3339Seconds(ms: number): string {
426
373
  return new Date(ms).toISOString().replace(/\.\d{3}Z$/, 'Z')
427
374
  }
375
+
376
+ function isTransactional(repository: Repository): repository is TransactionalRepository {
377
+ return typeof (repository as Partial<TransactionalRepository>).commitTransaction === 'function'
378
+ }
@@ -0,0 +1,322 @@
1
+ // The Store's multi-stream transaction (ADR 0004 C1, C2): a read set of any aggregate types, the
2
+ // events decided for some of them, and one atomic commit that fences every stream it read but
3
+ // didn't write.
4
+
5
+ import type {Aggregate, EventDefinitions, EventOf} from './aggregate.js'
6
+ import {EvtError} from './errors.js'
7
+ import type {SerializedEvent} from './event.js'
8
+ import {type Metadata, emptyMetadata} from './metadata.js'
9
+ import {
10
+ type CallOptions,
11
+ type Commit,
12
+ type ReadFence,
13
+ type TransactionalRepository,
14
+ checkNotReserved,
15
+ } from './repository.js'
16
+ import {commitSnapshot, copyMetadata, projectCommit, serializeEvents} from './staging.js'
17
+ import type {ExecuteOptions, Loaded} from './store.js'
18
+ import {compareUtf8} from './utf8.js'
19
+
20
+ const MAX_INT64 = 2n ** 63n - 1n
21
+
22
+ // The key of the property that types a handle by its aggregate's events. It exists only in types,
23
+ // and a symbol keeps it out of the way while the published declarations still check it.
24
+ declare const eventTypes: unique symbol
25
+
26
+ /** What a transaction needs from its store. */
27
+ export interface TransactionContext {
28
+ readonly repository: TransactionalRepository
29
+ load<S, C, D extends EventDefinitions>(
30
+ aggregate: Aggregate<S, C, D>,
31
+ entityId: string,
32
+ options?: CallOptions,
33
+ ): Promise<Loaded<S>>
34
+ snapshotInterval(aggregate: {readonly snapshotInterval?: number | undefined}): number
35
+ /** The command's metadata with what the store fills in, and the commit ID stamped. */
36
+ metadata(partial?: Partial<Metadata>): Metadata
37
+ }
38
+
39
+ /** A fence the caller stamps on a stream the transaction doesn't read. */
40
+ export interface CallerFence {
41
+ readonly entityId: string
42
+ /** The stream's entity type, which a fence on a retained type is refused for. */
43
+ readonly entityType: string
44
+ /** The head the caller observed: 0 for a stream with no events. */
45
+ readonly head: bigint
46
+ }
47
+
48
+ /**
49
+ * What a transaction's commit did:
50
+ *
51
+ * - `committed`: every stream's events (possibly none) are stored, and every fence held. `events`
52
+ * are the stored events in the order the commit wrote them: by entity ID bytes, then sequence.
53
+ * - `duplicate`: an event a read stream folded in since its latest snapshot already carries the
54
+ * command ID, so nothing was written.
55
+ */
56
+ export type TransactionResult =
57
+ {kind: 'committed'; events: SerializedEvent[]} | {kind: 'duplicate'; commandId: string}
58
+
59
+ // One stream of the read set: what the read loaded, and what the transaction appended since.
60
+ interface Stream {
61
+ // oxlint-disable-next-line typescript/no-explicit-any -- each stream has its own aggregate type.
62
+ readonly aggregate: Aggregate<any, any, any>
63
+ // The position of its read call, which orders the commit's projector operations.
64
+ readonly order: number
65
+ readonly loaded: Loaded<unknown>
66
+ state: unknown
67
+ events: SerializedEvent[]
68
+ }
69
+
70
+ /**
71
+ * A stream read into a {@link Transaction}, typed by its aggregate. Pass it to
72
+ * {@link Transaction.append} to stage events for it; a read never appended to is fenced at the
73
+ * head it was read at.
74
+ */
75
+ export class StreamRead<S, D extends EventDefinitions> {
76
+ readonly #stream: Stream
77
+ // Ties the handle to its aggregate's event types, which only append reads.
78
+ declare readonly [eventTypes]?: D
79
+
80
+ /** @internal Built by {@link Transaction.read}. */
81
+ constructor(stream: Stream) {
82
+ this.#stream = stream
83
+ }
84
+
85
+ get entityId(): string {
86
+ return this.#stream.loaded.entityId
87
+ }
88
+
89
+ /** The state, with the events appended through this handle. */
90
+ get state(): S {
91
+ return this.#stream.state as S
92
+ }
93
+
94
+ /** The sequence of the last event, with the events appended through this handle. */
95
+ get sequence(): bigint {
96
+ return this.#stream.loaded.sequence + BigInt(this.#stream.events.length)
97
+ }
98
+
99
+ /** The stream as it was read. */
100
+ get loaded(): Loaded<S> {
101
+ return this.#stream.loaded as Loaded<S>
102
+ }
103
+ }
104
+
105
+ /**
106
+ * One atomic commit across several streams. {@link read} loads streams of any aggregate types,
107
+ * {@link append} stages the events decided for them, {@link fence} adds a fence the caller stamped
108
+ * on a stream it didn't read, and {@link commit} writes everything in one repository transaction,
109
+ * or nothing. A stream read but never appended to adds a fence the engine filled at the head it
110
+ * was read at, so the commit fails with a retryable `FenceConflictError` once another writer
111
+ * moves it. Begin one with {@link Store.transaction}.
112
+ *
113
+ * Every staging call checks its input before it changes anything, so one that throws leaves the
114
+ * transaction as it was. A transaction commits once: after {@link commit}, whatever it returned
115
+ * or threw, every call throws `invalidCommit`. Begin a new transaction to try again.
116
+ */
117
+ export class Transaction {
118
+ readonly #context: TransactionContext
119
+ readonly #streams = new Map<StreamRead<unknown, EventDefinitions>, Stream>()
120
+ // Every stream read or being read, by ID, with the position of its read call.
121
+ readonly #ids = new Map<string, number>()
122
+ readonly #fences = new Map<string, CallerFence>()
123
+ #reads = 0
124
+ #pending = 0
125
+ #done = false
126
+
127
+ /** @internal Begun by {@link Store.transaction}. */
128
+ constructor(context: TransactionContext) {
129
+ this.#context = context
130
+ }
131
+
132
+ /**
133
+ * Loads a stream into the read set, as {@link Store.load} does, and returns its typed handle.
134
+ * Reading a stream the transaction already read, or is reading, throws `invalidCommit`.
135
+ */
136
+ async read<S, C, D extends EventDefinitions>(
137
+ aggregate: Aggregate<S, C, D>,
138
+ entityId: string,
139
+ options: CallOptions = {},
140
+ ): Promise<StreamRead<S, D>> {
141
+ this.#checkOpen()
142
+ if (this.#ids.has(entityId)) {
143
+ throw invalidCommit(`duplicate read of ${entityId}`)
144
+ }
145
+
146
+ // The ID is held while the load runs, so a concurrent read of the same stream is refused too,
147
+ // and the read keeps the place it was called in, whenever its load finishes.
148
+ const order = this.#reads++
149
+ this.#ids.set(entityId, order)
150
+ this.#pending++
151
+ let loaded: Loaded<S>
152
+ try {
153
+ loaded = await this.#context.load(aggregate, entityId, options)
154
+ } catch (error) {
155
+ this.#ids.delete(entityId)
156
+ throw error
157
+ } finally {
158
+ this.#pending--
159
+ }
160
+
161
+ const stream: Stream = {
162
+ aggregate,
163
+ order,
164
+ loaded,
165
+ state: loaded.state,
166
+ events: [],
167
+ }
168
+ const read = new StreamRead<S, D>(stream)
169
+ this.#streams.set(read as StreamRead<unknown, EventDefinitions>, stream)
170
+
171
+ return read
172
+ }
173
+
174
+ /**
175
+ * Stages events for a stream this transaction read. They're applied to the handle's state, and
176
+ * appending again continues the sequence. The commit stamps their metadata, and runs the
177
+ * aggregate's projectors over every event appended to the stream, with its final state. A
178
+ * handle from another transaction throws `invalidCommit`.
179
+ */
180
+ append<S, D extends EventDefinitions>(
181
+ read: StreamRead<S, D>,
182
+ events: readonly EventOf<D>[],
183
+ ): this {
184
+ this.#checkOpen()
185
+ const stream = this.#streams.get(read as StreamRead<unknown, EventDefinitions>)
186
+ if (stream === undefined) {
187
+ throw invalidCommit(`stream ${read.entityId} was not read by this transaction`)
188
+ }
189
+
190
+ // Serialized now, so an event the aggregate can't encode fails here, with nothing staged.
191
+ const after = stream.loaded.sequence + BigInt(stream.events.length)
192
+ const staged = serializeEvents(
193
+ stream.aggregate,
194
+ stream.loaded.entityId,
195
+ after,
196
+ stream.state,
197
+ events,
198
+ emptyMetadata(),
199
+ )
200
+ stream.events.push(...staged.events)
201
+ stream.state = staged.state
202
+
203
+ return this
204
+ }
205
+
206
+ /**
207
+ * Adds a fence the caller stamped, for a stream the transaction doesn't read: the commit fails
208
+ * with a `FenceConflictError` once another writer moves the stream past `head`, which isn't
209
+ * retryable, since only the caller knows what it read. A fence on a stream the transaction reads,
210
+ * before or after the read, is dropped at commit: the read's own fence, or its write, holds the
211
+ * stream instead. A fence in a reserved namespace, at a head outside [0, 2^63 - 1), or on a
212
+ * stream already fenced throws, with nothing added.
213
+ */
214
+ fence(fence: CallerFence): this {
215
+ this.#checkOpen()
216
+ checkNotReserved(fence.entityId)
217
+ if (fence.head < 0n || fence.head >= MAX_INT64 || this.#fences.has(fence.entityId)) {
218
+ throw invalidCommit(`invalid or duplicate fence ${fence.entityId}`)
219
+ }
220
+
221
+ this.#fences.set(fence.entityId, {...fence})
222
+
223
+ return this
224
+ }
225
+
226
+ /**
227
+ * Stamps one commit ID and the command's metadata on every appended event, and commits every
228
+ * stream with events, a fence on every stream read without any, and the caller's fences, in one
229
+ * atomic repository transaction. On failure nothing is written. A command ID that an event a
230
+ * read stream folded in since its latest snapshot already carries is a `duplicate`, and nothing
231
+ * is written. Projector operations follow the streams in the order they were read. A commit
232
+ * while a read is still loading throws `invalidCommit`, and leaves the transaction open.
233
+ */
234
+ async commit(options: ExecuteOptions = {}): Promise<TransactionResult> {
235
+ this.#checkOpen()
236
+ if (this.#pending > 0) {
237
+ throw invalidCommit('a read is still loading; await it before the commit')
238
+ }
239
+ this.#done = true
240
+
241
+ const metadata = this.#context.metadata(options.metadata)
242
+ const commandId = metadata.commandId
243
+ const streams = [...this.#streams.values()]
244
+ streams.sort((a, b) => a.order - b.order)
245
+ if (
246
+ commandId !== null &&
247
+ commandId !== '' &&
248
+ streams.some((stream) => stream.loaded.seenCommandIds.has(commandId))
249
+ ) {
250
+ return {kind: 'duplicate', commandId}
251
+ }
252
+
253
+ const commits: Commit[] = []
254
+ const fences: ReadFence[] = []
255
+ for (const stream of streams) {
256
+ const {aggregate, loaded} = stream
257
+ if (stream.events.length === 0) {
258
+ fences.push({
259
+ entityId: loaded.entityId,
260
+ entityType: aggregate.type,
261
+ head: loaded.sequence,
262
+ provenance: 'engine',
263
+ })
264
+ continue
265
+ }
266
+
267
+ for (const event of stream.events) {
268
+ event.metadata = copyMetadata(metadata)
269
+ }
270
+ const interval = this.#context.snapshotInterval(aggregate)
271
+ const appended = stream.events.length
272
+ const snapshot = commitSnapshot(
273
+ aggregate,
274
+ loaded.entityId,
275
+ interval,
276
+ loaded,
277
+ appended,
278
+ stream.state,
279
+ )
280
+ commits.push({
281
+ entityId: loaded.entityId,
282
+ events: stream.events,
283
+ snapshot,
284
+ projections: projectCommit(
285
+ aggregate,
286
+ loaded.entityId,
287
+ stream.state,
288
+ stream.events,
289
+ snapshot,
290
+ ),
291
+ })
292
+ }
293
+
294
+ // A caller's fence on a stream the transaction read is superseded by the read's own.
295
+ for (const fence of this.#fences.values()) {
296
+ if (!this.#ids.has(fence.entityId)) {
297
+ fences.push({...fence, provenance: 'caller'})
298
+ }
299
+ }
300
+
301
+ await this.#context.repository.commitTransaction({streams: commits, fences}, options)
302
+
303
+ // Sorted apart from what the repository was given, which stays in read order.
304
+ const written = [...commits]
305
+ written.sort((a, b) => compareUtf8(a.entityId, b.entityId))
306
+
307
+ return {
308
+ kind: 'committed',
309
+ events: written.flatMap((commit) => commit.events),
310
+ }
311
+ }
312
+
313
+ #checkOpen(): void {
314
+ if (this.#done) {
315
+ throw invalidCommit('the transaction has committed; begin a new one')
316
+ }
317
+ }
318
+ }
319
+
320
+ function invalidCommit(message: string): EvtError {
321
+ return new EvtError('invalidCommit', `invalid commit: ${message}`)
322
+ }