@shirudo/ddd-kit 3.0.0-rc.6 → 3.0.0-rc.8

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.
@@ -1668,6 +1668,130 @@ function mergeMetadata(...metadataObjects) {
1668
1668
  return merged;
1669
1669
  }
1670
1670
 
1671
+ //#endregion
1672
+ //#region src/internal/async/abort.ts
1673
+ /**
1674
+ * The value to reject with when an `AbortSignal` has fired.
1675
+ *
1676
+ * Returns the signal's `reason` (a `DOMException` `AbortError` for
1677
+ * `controller.abort()`, `TimeoutError` for `AbortSignal.timeout`), falling
1678
+ * back to a plain `Error` with `fallbackMessage` when `reason` is nullish.
1679
+ * A spec-compliant signal always populates `reason` when aborted, so the
1680
+ * fallback only fires for a non-spec polyfill; without it, a bare
1681
+ * `throw undefined` would surface, breaking `instanceof Error` handling.
1682
+ *
1683
+ * Centralizes the `signal.reason ?? new Error(...)` idiom used at every
1684
+ * abort site (event bus, `withCommit`, `UnitOfWork.run`, the retrying
1685
+ * scope) so a single fix covers all of them.
1686
+ */
1687
+ function abortReason(signal, fallbackMessage) {
1688
+ return signal.reason ?? new Error(fallbackMessage);
1689
+ }
1690
+
1691
+ //#endregion
1692
+ //#region src/internal/validate.ts
1693
+ /**
1694
+ * Shared construction-time guards for numeric options. `context` names
1695
+ * the throwing component so the error reads like the component's own
1696
+ * validation ("OutboxDispatcher: pollIntervalMs must be...").
1697
+ */
1698
+ /** Guard for numeric options that must be a non-negative finite number. */
1699
+ function assertNonNegativeFinite(context, field, value) {
1700
+ if (!Number.isFinite(value) || value < 0) throw new Error(`${context}: ${field} must be a non-negative finite number, got ${value}`);
1701
+ }
1702
+ /** Guard for count options that must be a whole number of at least 1. */
1703
+ function assertPositiveInteger(context, field, value) {
1704
+ if (!Number.isInteger(value) || value < 1) throw new Error(`${context}: ${field} must be an integer >= 1, got ${value}`);
1705
+ }
1706
+ /** Guard for retained-record capacities that must fit exact JS integers. */
1707
+ function assertPositiveSafeInteger(context, field, value) {
1708
+ if (!Number.isSafeInteger(value) || value < 1) throw new RangeError(`${context}: ${field} must be a positive safe integer, got ${value}`);
1709
+ }
1710
+
1711
+ //#endregion
1712
+ //#region src/internal/async/execution.ts
1713
+ /** Default bound for delivery and post-commit operations. */
1714
+ const DEFAULT_EXECUTION_TIMEOUT_MS = 3e4;
1715
+ /**
1716
+ * Owner signal of each child signal that {@link runBoundedExecution} minted.
1717
+ *
1718
+ * One bounded operation often wraps another, and every hop derives a fresh
1719
+ * signal. A consumer that follows a call chain by signal identity alone loses
1720
+ * the link at the first hop. Key and value are both weak: a long chain of
1721
+ * nested operations must not hold its whole ancestry alive.
1722
+ */
1723
+ const executionOwners = /* @__PURE__ */ new WeakMap();
1724
+ /**
1725
+ * The signal that a bounded execution derived this one from, or `undefined`
1726
+ * when the signal did not come from {@link runBoundedExecution} or had no
1727
+ * owner. Walk it to follow a chain across nested bounded executions.
1728
+ */
1729
+ function ownerSignalOf(signal) {
1730
+ return executionOwners.get(signal)?.deref();
1731
+ }
1732
+ /**
1733
+ * Runs one operation with a child signal that combines owner cancellation and a
1734
+ * shell-owned timeout. The returned promise settles on abort even when an
1735
+ * adapter ignores the signal; the adapter promise remains observed so a later
1736
+ * rejection cannot become an unhandled rejection.
1737
+ *
1738
+ * This bounds how long the shell waits; JavaScript cannot forcibly terminate
1739
+ * an arbitrary promise. An I/O adapter that must prevent zombie work and
1740
+ * overlapping retries has to pass `context.signal` to its native operation or
1741
+ * enforce a native timeout no later than `context.deadlineAt`.
1742
+ */
1743
+ function runBoundedExecution(label, options, operation) {
1744
+ if (options.deadlineAt === void 0) assertNonNegativeFinite(label, "timeoutMs", options.timeoutMs);
1745
+ else assertNonNegativeFinite(label, "deadlineAt", options.deadlineAt);
1746
+ const startedAt = Date.now();
1747
+ const deadlineAt = options.deadlineAt ?? startedAt + options.timeoutMs;
1748
+ const timeoutMs = Math.max(0, deadlineAt - startedAt);
1749
+ const timeoutError = () => new DOMException(`${label} timed out after ${timeoutMs}ms`, "TimeoutError");
1750
+ const controller = new AbortController();
1751
+ const context = Object.freeze({
1752
+ signal: controller.signal,
1753
+ deadlineAt
1754
+ });
1755
+ const ownerSignal = options.signal;
1756
+ if (ownerSignal !== void 0) executionOwners.set(controller.signal, new WeakRef(ownerSignal));
1757
+ const abortFromOwner = () => {
1758
+ controller.abort(ownerSignal === void 0 ? /* @__PURE__ */ new Error(`${label} aborted`) : abortReason(ownerSignal, `${label} aborted`));
1759
+ };
1760
+ if (ownerSignal?.aborted) abortFromOwner();
1761
+ else ownerSignal?.addEventListener("abort", abortFromOwner, { once: true });
1762
+ if (!controller.signal.aborted && options.deadlineAt !== void 0 && deadlineAt <= startedAt) controller.abort(timeoutError());
1763
+ const timer = setTimeout(() => {
1764
+ controller.abort(timeoutError());
1765
+ }, timeoutMs);
1766
+ return new Promise((resolve, reject) => {
1767
+ let settled = false;
1768
+ const finish = (complete) => {
1769
+ if (settled) return;
1770
+ settled = true;
1771
+ clearTimeout(timer);
1772
+ ownerSignal?.removeEventListener("abort", abortFromOwner);
1773
+ controller.signal.removeEventListener("abort", onAbort);
1774
+ complete();
1775
+ };
1776
+ const onAbort = () => {
1777
+ queueMicrotask(() => finish(() => reject(abortReason(controller.signal, `${label} aborted`))));
1778
+ };
1779
+ if (controller.signal.aborted) {
1780
+ onAbort();
1781
+ return;
1782
+ }
1783
+ controller.signal.addEventListener("abort", onAbort, { once: true });
1784
+ let outcome;
1785
+ try {
1786
+ outcome = Promise.resolve(operation(context));
1787
+ } catch (error) {
1788
+ finish(() => reject(error));
1789
+ return;
1790
+ }
1791
+ outcome.then((value) => finish(() => resolve(value)), (error) => finish(() => reject(error)));
1792
+ });
1793
+ }
1794
+
1671
1795
  //#endregion
1672
1796
  //#region src/messaging/outbox/ports.ts
1673
1797
  /**
@@ -1687,5 +1811,5 @@ function isDispatchTrackingOutbox(outbox) {
1687
1811
  }
1688
1812
 
1689
1813
  //#endregion
1690
- export { isBuiltInObject as A, voWithValidation as C, builtInTagWithoutInvokingAccessors as D, deepEqual as E, isWeakMap as M, findPropertyDescriptor as O, voEqualsExcept as S, deepOmit as T, stampCooperativeBrand as _, createDomainEvent as a, vo as b, createUncommittedDomainEvent as c, isUncommittedDomainEvent as d, mergeMetadata as f, hasCooperativeBrand as g, SnapshotTimeValidationError as h, copyMetadata as i, isIntrinsicConstructorPrototype as j, hasIntrinsicPrototypeChain as k, defaultDomainEventFactory as l, DomainEventValidationError as m, adoptRecordedDomainEvent as n, createDomainEventFactory as o, recordDomainEvent as p, adoptUncommittedDomainEvent as r, createDomainEventFromFacts as s, isDispatchTrackingOutbox as t, isRecordedDomainEvent as u, ValueObject as v, deepEqualExcept as w, voEquals as x, deepFreeze as y };
1814
+ export { voWithValidation as A, hasCooperativeBrand as C, vo as D, deepFreeze as E, findPropertyDescriptor as F, hasIntrinsicPrototypeChain as I, isBuiltInObject as L, deepOmit as M, deepEqual as N, voEquals as O, builtInTagWithoutInvokingAccessors as P, isIntrinsicConstructorPrototype as R, SnapshotTimeValidationError as S, ValueObject as T, isRecordedDomainEvent as _, assertNonNegativeFinite as a, recordDomainEvent as b, abortReason as c, copyMetadata as d, createDomainEvent as f, defaultDomainEventFactory as g, createUncommittedDomainEvent as h, runBoundedExecution as i, deepEqualExcept as j, voEqualsExcept as k, adoptRecordedDomainEvent as l, createDomainEventFromFacts as m, DEFAULT_EXECUTION_TIMEOUT_MS as n, assertPositiveInteger as o, createDomainEventFactory as p, ownerSignalOf as r, assertPositiveSafeInteger as s, isDispatchTrackingOutbox as t, adoptUncommittedDomainEvent as u, isUncommittedDomainEvent as v, stampCooperativeBrand as w, DomainEventValidationError as x, mergeMetadata as y, isWeakMap as z };
1691
1815
  //# sourceMappingURL=ports.js.map