@shirudo/ddd-kit 3.0.0-rc.13 → 3.0.0-rc.15

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.
@@ -276,6 +276,55 @@ function constructorName(prototype) {
276
276
  return typeof name === "string" && name !== "" ? name : void 0;
277
277
  }
278
278
 
279
+ //#endregion
280
+ //#region src/internal/uuid-v7.ts
281
+ const COUNTER_LIMIT = 2 ** 42;
282
+ let lastMilliseconds = Number.NEGATIVE_INFINITY;
283
+ let counter = 0;
284
+ /**
285
+ * RFC 9562 UUID version 7: the Unix time in milliseconds in the first 48 bits,
286
+ * then random bits from Web Crypto. A B-tree index on such ids stays
287
+ * clustered.
288
+ *
289
+ * The ids of one process sort in creation order. Ids from several processes
290
+ * interleave within one millisecond and depend on their clocks. Within one
291
+ * millisecond of one process, a 42-bit counter increases by one per id: the
292
+ * 12 bits of `rand_a` and the first 30 bits of `rand_b`. It starts at a random
293
+ * value with its top bit clear, so it does not run out in practice, also not
294
+ * on a runtime that stops the clock during a request. If it runs out, the time
295
+ * field moves one millisecond ahead. A clock that goes back does not move the
296
+ * time field back. This is method 1 of RFC 9562, section 6.2.
297
+ */
298
+ function uuidV7() {
299
+ const bytes = /* @__PURE__ */ new Uint8Array(16);
300
+ globalThis.crypto.getRandomValues(bytes);
301
+ const view = new DataView(bytes.buffer);
302
+ const seed = (view.getUint16(0) & 511) * 2 ** 32 + view.getUint32(2);
303
+ const now = Date.now();
304
+ if (now > lastMilliseconds) {
305
+ lastMilliseconds = now;
306
+ counter = seed;
307
+ } else if (counter + 1 < COUNTER_LIMIT) counter += 1;
308
+ else {
309
+ lastMilliseconds += 1;
310
+ counter = seed;
311
+ }
312
+ let time = lastMilliseconds;
313
+ for (let index = 5; index >= 0; index -= 1) {
314
+ bytes[index] = time % 256;
315
+ time = Math.floor(time / 256);
316
+ }
317
+ const counterHigh = Math.floor(counter / 2 ** 24);
318
+ const counterLow = counter % 2 ** 24;
319
+ view.setUint16(6, 28672 | counterHigh >>> 6);
320
+ view.setUint8(8, 128 | counterHigh & 63);
321
+ view.setUint8(9, counterLow >>> 16);
322
+ view.setUint16(10, counterLow & 65535);
323
+ let hex = "";
324
+ for (const byte of bytes) hex += byte.toString(16).padStart(2, "0");
325
+ return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20)}`;
326
+ }
327
+
279
328
  //#endregion
280
329
  //#region src/domain/event/clock.ts
281
330
  /** Immutable library default captured by the default domain-event factory. */
@@ -320,14 +369,14 @@ var SnapshotTimeValidationError = class extends TypeError {
320
369
 
321
370
  //#endregion
322
371
  //#region src/domain/event/domain-event.ts
323
- const defaultEventIdFactory = () => crypto.randomUUID();
372
+ const defaultEventIdFactory = uuidV7;
324
373
  /**
325
374
  * Creates an immutable, instance-bound domain-event factory.
326
375
  *
327
376
  * The supplied functions are read once and captured by value. The returned
328
377
  * object is frozen, so another request, test, or library cannot replace its
329
378
  * policy. Its {@link DomainEventFactory.createStamp} method is the
330
- * application-shell bridge that records an accepted aggregate decision.
379
+ * application-shell bridge that stamps an accepted aggregate decision.
331
380
  * Passing the factory through `AggregateConfig`
332
381
  * enables the explicitly named convenience methods, whose defaults read time
333
382
  * and randomness.
@@ -338,8 +387,7 @@ const defaultEventIdFactory = () => crypto.randomUUID();
338
387
  * eventIdFactory: () => uuidv7(),
339
388
  * clock: () => new Date(),
340
389
  * });
341
- * order.confirm();
342
- * stampPendingEvents(order, domainEvents);
390
+ * const uow = new UnitOfWork({ ...deps, eventStampFactory: domainEvents });
343
391
  * ```
344
392
  */
345
393
  function createDomainEventFactory(options = {}) {