@ddd-ts-kit/kernel 0.109.0 → 0.110.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 (28) hide show
  1. package/README.fr.md +10 -1
  2. package/README.md +9 -0
  3. package/dist/domain/errors/index.d.ts +1 -0
  4. package/dist/domain/errors/index.d.ts.map +1 -1
  5. package/dist/domain/errors/index.js +1 -0
  6. package/dist/domain/errors/index.js.map +1 -1
  7. package/dist/domain/errors/kit-codes.d.ts +32 -0
  8. package/dist/domain/errors/kit-codes.d.ts.map +1 -0
  9. package/dist/domain/errors/kit-codes.js +30 -0
  10. package/dist/domain/errors/kit-codes.js.map +1 -0
  11. package/dist/infrastructure/domain-events/announce-domain-events.d.ts +6 -1
  12. package/dist/infrastructure/domain-events/announce-domain-events.d.ts.map +1 -1
  13. package/dist/infrastructure/domain-events/announce-domain-events.js +11 -5
  14. package/dist/infrastructure/domain-events/announce-domain-events.js.map +1 -1
  15. package/dist/infrastructure/domain-events/dispatching-on-save.d.ts +8 -1
  16. package/dist/infrastructure/domain-events/dispatching-on-save.d.ts.map +1 -1
  17. package/dist/infrastructure/domain-events/dispatching-on-save.js +8 -4
  18. package/dist/infrastructure/domain-events/dispatching-on-save.js.map +1 -1
  19. package/dist/infrastructure/domain-events/domain-event-runs.js +5 -2
  20. package/dist/infrastructure/domain-events/domain-event-runs.js.map +1 -1
  21. package/dist/infrastructure/domain-events/in-context-of.d.ts +9 -0
  22. package/dist/infrastructure/domain-events/in-context-of.d.ts.map +1 -0
  23. package/dist/infrastructure/domain-events/in-context-of.js +19 -0
  24. package/dist/infrastructure/domain-events/in-context-of.js.map +1 -0
  25. package/dist/infrastructure/domain-events/unit-of-work-scope.d.ts +8 -2
  26. package/dist/infrastructure/domain-events/unit-of-work-scope.d.ts.map +1 -1
  27. package/dist/infrastructure/domain-events/unit-of-work-scope.js.map +1 -1
  28. package/package.json +1 -1
package/README.fr.md CHANGED
@@ -1,5 +1,5 @@
1
1
  [English](README.md) · **Français**
2
- <!-- translation-of: README.md sha256:7668980877181ed1 -->
2
+ <!-- translation-of: README.md sha256:97710c410f2344fb -->
3
3
 
4
4
  # @ddd-ts-kit/kernel
5
5
 
@@ -283,6 +283,15 @@ Le statut HTTP de chaque code se choisit dans la couche de présentation, à un
283
283
  journaux d'erreurs, les *spans* et le journal d'audit enregistrent le `code`, et le rapporteur
284
284
  d'erreurs laisse une `DomainError` tranquille (les deux exceptions sont sous « ErrorReporter »).
285
285
 
286
+ Les codes que lèvent les paquets du kit forment eux aussi une liste, `KitCodes`, avec son type `KitCode` :
287
+ `KitCodes.Forbidden` vaut `'FORBIDDEN'`, `KitCodes.ValidationFailed` vaut `'VALIDATION_FAILED'`, et de même pour
288
+ `NotFound`, `Conflict`, `InvariantViolation`, `Unauthorized`, `NotAcceptable`, `ConcurrencyConflict`,
289
+ `InvalidCriteria`, `InvalidPageRequest`, `Timeout`, `CircuitOpen`, `BulkheadFull`, `AttemptsExhausted`,
290
+ `ServiceUnavailable`, `PreconditionFailed`, `PreconditionRequired`, `FileTooLarge`, `UnsupportedMediaType`,
291
+ `FileNotFound`, `FileRejected` et `InvalidFileKey`. Une route les nomme dans `errors` et un test les attend
292
+ (`rejects.toMatchObject({ code: KitCodes.Forbidden })`) sans écrire la chaîne : l'éditeur complète la clé,
293
+ et une clé mal orthographiée ne compile pas.
294
+
286
295
  Les codes d'un contexte forment un vocabulaire fermé, `SchedulingCode` : `isRefusal(error, SchedulingCodes)`
287
296
  restreint ce qui a été attrapé à l'un d'eux, si bien qu'un `switch` sur son code est exhaustif, et
288
297
  la classe d'un refus connaît ses faits :
package/README.md CHANGED
@@ -278,6 +278,15 @@ The HTTP status of each code is chosen in the presentation layer, in one place;
278
278
  logs, the spans and the audit trail record the `code`, and the error reporter leaves a
279
279
  `DomainError` alone (the two exceptions are under "ErrorReporter").
280
280
 
281
+ The codes the packages of the kit raise are a list too, `KitCodes`, with its type `KitCode`:
282
+ `KitCodes.Forbidden` is `'FORBIDDEN'`, `KitCodes.ValidationFailed` is `'VALIDATION_FAILED'`, and so on for
283
+ `NotFound`, `Conflict`, `InvariantViolation`, `Unauthorized`, `NotAcceptable`, `ConcurrencyConflict`,
284
+ `InvalidCriteria`, `InvalidPageRequest`, `Timeout`, `CircuitOpen`, `BulkheadFull`, `AttemptsExhausted`,
285
+ `ServiceUnavailable`, `PreconditionFailed`, `PreconditionRequired`, `FileTooLarge`, `UnsupportedMediaType`,
286
+ `FileNotFound`, `FileRejected` and `InvalidFileKey`. A route names them in `errors` and a test expects them
287
+ (`rejects.toMatchObject({ code: KitCodes.Forbidden })`) without writing the string: the editor completes
288
+ the key, and a misspelled one does not compile.
289
+
281
290
  The codes of a context are a closed vocabulary, `SchedulingCode`: `isRefusal(error, SchedulingCodes)`
282
291
  narrows what was caught to one of them, so a `switch` on its code is exhaustive, and the class of
283
292
  one refusal knows its facts:
@@ -8,6 +8,7 @@ export * from './domain-refusal.js';
8
8
  export * from './factual-refusal.js';
9
9
  export * from './invariant-violation.js';
10
10
  export * from './is-refusal.js';
11
+ export * from './kit-codes.js';
11
12
  export * from './not-found-error.js';
12
13
  export * from './precondition-failed-error.js';
13
14
  export * from './refusal-facts.js';
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/domain/errors/index.ts"],"names":[],"mappings":"AAAA,cAAc,WAAW,CAAC;AAC1B,cAAc,iBAAiB,CAAC;AAChC,cAAc,qBAAqB,CAAC;AACpC,cAAc,kBAAkB,CAAC;AACjC,cAAc,qBAAqB,CAAC;AACpC,cAAc,gBAAgB,CAAC;AAC/B,cAAc,kBAAkB,CAAC;AACjC,cAAc,mBAAmB,CAAC;AAClC,cAAc,uBAAuB,CAAC;AACtC,cAAc,cAAc,CAAC;AAC7B,cAAc,mBAAmB,CAAC;AAClC,cAAc,6BAA6B,CAAC;AAC5C,cAAc,iBAAiB,CAAC;AAChC,cAAc,cAAc,CAAC;AAC7B,cAAc,WAAW,CAAC;AAC1B,cAAc,8BAA8B,CAAC;AAC7C,cAAc,0BAA0B,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/domain/errors/index.ts"],"names":[],"mappings":"AAAA,cAAc,WAAW,CAAC;AAC1B,cAAc,iBAAiB,CAAC;AAChC,cAAc,qBAAqB,CAAC;AACpC,cAAc,kBAAkB,CAAC;AACjC,cAAc,qBAAqB,CAAC;AACpC,cAAc,gBAAgB,CAAC;AAC/B,cAAc,kBAAkB,CAAC;AACjC,cAAc,mBAAmB,CAAC;AAClC,cAAc,uBAAuB,CAAC;AACtC,cAAc,cAAc,CAAC;AAC7B,cAAc,aAAa,CAAC;AAC5B,cAAc,mBAAmB,CAAC;AAClC,cAAc,6BAA6B,CAAC;AAC5C,cAAc,iBAAiB,CAAC;AAChC,cAAc,cAAc,CAAC;AAC7B,cAAc,WAAW,CAAC;AAC1B,cAAc,8BAA8B,CAAC;AAC7C,cAAc,0BAA0B,CAAC"}
@@ -8,6 +8,7 @@ export * from './domain-refusal.js';
8
8
  export * from './factual-refusal.js';
9
9
  export * from './invariant-violation.js';
10
10
  export * from './is-refusal.js';
11
+ export * from './kit-codes.js';
11
12
  export * from './not-found-error.js';
12
13
  export * from './precondition-failed-error.js';
13
14
  export * from './refusal-facts.js';
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/domain/errors/index.ts"],"names":[],"mappings":"AAAA,cAAc,WAAW,CAAC;AAC1B,cAAc,iBAAiB,CAAC;AAChC,cAAc,qBAAqB,CAAC;AACpC,cAAc,kBAAkB,CAAC;AACjC,cAAc,qBAAqB,CAAC;AACpC,cAAc,gBAAgB,CAAC;AAC/B,cAAc,kBAAkB,CAAC;AACjC,cAAc,mBAAmB,CAAC;AAClC,cAAc,uBAAuB,CAAC;AACtC,cAAc,cAAc,CAAC;AAC7B,cAAc,mBAAmB,CAAC;AAClC,cAAc,6BAA6B,CAAC;AAC5C,cAAc,iBAAiB,CAAC;AAChC,cAAc,cAAc,CAAC;AAC7B,cAAc,WAAW,CAAC;AAC1B,cAAc,8BAA8B,CAAC;AAC7C,cAAc,0BAA0B,CAAC"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/domain/errors/index.ts"],"names":[],"mappings":"AAAA,cAAc,WAAW,CAAC;AAC1B,cAAc,iBAAiB,CAAC;AAChC,cAAc,qBAAqB,CAAC;AACpC,cAAc,kBAAkB,CAAC;AACjC,cAAc,qBAAqB,CAAC;AACpC,cAAc,gBAAgB,CAAC;AAC/B,cAAc,kBAAkB,CAAC;AACjC,cAAc,mBAAmB,CAAC;AAClC,cAAc,uBAAuB,CAAC;AACtC,cAAc,cAAc,CAAC;AAC7B,cAAc,aAAa,CAAC;AAC5B,cAAc,mBAAmB,CAAC;AAClC,cAAc,6BAA6B,CAAC;AAC5C,cAAc,iBAAiB,CAAC;AAChC,cAAc,cAAc,CAAC;AAC7B,cAAc,WAAW,CAAC;AAC1B,cAAc,8BAA8B,CAAC;AAC7C,cAAc,0BAA0B,CAAC"}
@@ -0,0 +1,32 @@
1
+ /**
2
+ * The codes of the refusals the packages of the kit raise, whichever package raises them: what a
3
+ * route may declare in `errors`, and what a test may expect, without a class of the project.
4
+ * `KitCodes.Forbidden` is the code `'FORBIDDEN'`; a misspelled key does not compile.
5
+ */
6
+ export declare const KitCodes: {
7
+ readonly ValidationFailed: 'VALIDATION_FAILED';
8
+ readonly NotFound: 'NOT_FOUND';
9
+ readonly Conflict: 'CONFLICT';
10
+ readonly InvariantViolation: 'INVARIANT_VIOLATION';
11
+ readonly Unauthorized: 'UNAUTHORIZED';
12
+ readonly Forbidden: 'FORBIDDEN';
13
+ readonly NotAcceptable: 'NOT_ACCEPTABLE';
14
+ readonly ConcurrencyConflict: 'CONCURRENCY_CONFLICT';
15
+ readonly InvalidCriteria: 'INVALID_CRITERIA';
16
+ readonly InvalidPageRequest: 'INVALID_PAGE_REQUEST';
17
+ readonly Timeout: 'TIMEOUT';
18
+ readonly CircuitOpen: 'CIRCUIT_OPEN';
19
+ readonly BulkheadFull: 'BULKHEAD_FULL';
20
+ readonly AttemptsExhausted: 'ATTEMPTS_EXHAUSTED';
21
+ readonly ServiceUnavailable: 'SERVICE_UNAVAILABLE';
22
+ readonly PreconditionFailed: 'PRECONDITION_FAILED';
23
+ readonly PreconditionRequired: 'PRECONDITION_REQUIRED';
24
+ readonly FileTooLarge: 'FILE_TOO_LARGE';
25
+ readonly UnsupportedMediaType: 'UNSUPPORTED_MEDIA_TYPE';
26
+ readonly FileNotFound: 'FILE_NOT_FOUND';
27
+ readonly FileRejected: 'FILE_REJECTED';
28
+ readonly InvalidFileKey: 'INVALID_FILE_KEY';
29
+ };
30
+ /** One code of the kit: `'FORBIDDEN' | 'NOT_FOUND' | ...`. */
31
+ export type KitCode = (typeof KitCodes)[keyof typeof KitCodes];
32
+ //# sourceMappingURL=kit-codes.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"kit-codes.d.ts","sourceRoot":"","sources":["../../../src/domain/errors/kit-codes.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,eAAO,MAAM,QAAQ;aACnB,gBAAgB,EAAE,mBAAmB;aACrC,QAAQ,EAAE,WAAW;aACrB,QAAQ,EAAE,UAAU;aACpB,kBAAkB,EAAE,qBAAqB;aACzC,YAAY,EAAE,cAAc;aAC5B,SAAS,EAAE,WAAW;aACtB,aAAa,EAAE,gBAAgB;aAC/B,mBAAmB,EAAE,sBAAsB;aAC3C,eAAe,EAAE,kBAAkB;aACnC,kBAAkB,EAAE,sBAAsB;aAC1C,OAAO,EAAE,SAAS;aAClB,WAAW,EAAE,cAAc;aAC3B,YAAY,EAAE,eAAe;aAC7B,iBAAiB,EAAE,oBAAoB;aACvC,kBAAkB,EAAE,qBAAqB;aACzC,kBAAkB,EAAE,qBAAqB;aACzC,oBAAoB,EAAE,uBAAuB;aAC7C,YAAY,EAAE,gBAAgB;aAC9B,oBAAoB,EAAE,wBAAwB;aAC9C,YAAY,EAAE,gBAAgB;aAC9B,YAAY,EAAE,eAAe;aAC7B,cAAc,EAAE,kBAAkB;CAC1B,CAAC;AAEX,8DAA8D;AAC9D,MAAM,MAAM,OAAO,GAAG,CAAC,OAAO,QAAQ,CAAC,CAAC,MAAM,OAAO,QAAQ,CAAC,CAAC"}
@@ -0,0 +1,30 @@
1
+ /**
2
+ * The codes of the refusals the packages of the kit raise, whichever package raises them: what a
3
+ * route may declare in `errors`, and what a test may expect, without a class of the project.
4
+ * `KitCodes.Forbidden` is the code `'FORBIDDEN'`; a misspelled key does not compile.
5
+ */
6
+ export const KitCodes = {
7
+ ValidationFailed: 'VALIDATION_FAILED',
8
+ NotFound: 'NOT_FOUND',
9
+ Conflict: 'CONFLICT',
10
+ InvariantViolation: 'INVARIANT_VIOLATION',
11
+ Unauthorized: 'UNAUTHORIZED',
12
+ Forbidden: 'FORBIDDEN',
13
+ NotAcceptable: 'NOT_ACCEPTABLE',
14
+ ConcurrencyConflict: 'CONCURRENCY_CONFLICT',
15
+ InvalidCriteria: 'INVALID_CRITERIA',
16
+ InvalidPageRequest: 'INVALID_PAGE_REQUEST',
17
+ Timeout: 'TIMEOUT',
18
+ CircuitOpen: 'CIRCUIT_OPEN',
19
+ BulkheadFull: 'BULKHEAD_FULL',
20
+ AttemptsExhausted: 'ATTEMPTS_EXHAUSTED',
21
+ ServiceUnavailable: 'SERVICE_UNAVAILABLE',
22
+ PreconditionFailed: 'PRECONDITION_FAILED',
23
+ PreconditionRequired: 'PRECONDITION_REQUIRED',
24
+ FileTooLarge: 'FILE_TOO_LARGE',
25
+ UnsupportedMediaType: 'UNSUPPORTED_MEDIA_TYPE',
26
+ FileNotFound: 'FILE_NOT_FOUND',
27
+ FileRejected: 'FILE_REJECTED',
28
+ InvalidFileKey: 'INVALID_FILE_KEY',
29
+ };
30
+ //# sourceMappingURL=kit-codes.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"kit-codes.js","sourceRoot":"","sources":["../../../src/domain/errors/kit-codes.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,MAAM,CAAC,MAAM,QAAQ,GAAG;IACtB,gBAAgB,EAAE,mBAAmB;IACrC,QAAQ,EAAE,WAAW;IACrB,QAAQ,EAAE,UAAU;IACpB,kBAAkB,EAAE,qBAAqB;IACzC,YAAY,EAAE,cAAc;IAC5B,SAAS,EAAE,WAAW;IACtB,aAAa,EAAE,gBAAgB;IAC/B,mBAAmB,EAAE,sBAAsB;IAC3C,eAAe,EAAE,kBAAkB;IACnC,kBAAkB,EAAE,sBAAsB;IAC1C,OAAO,EAAE,SAAS;IAClB,WAAW,EAAE,cAAc;IAC3B,YAAY,EAAE,eAAe;IAC7B,iBAAiB,EAAE,oBAAoB;IACvC,kBAAkB,EAAE,qBAAqB;IACzC,kBAAkB,EAAE,qBAAqB;IACzC,oBAAoB,EAAE,uBAAuB;IAC7C,YAAY,EAAE,gBAAgB;IAC9B,oBAAoB,EAAE,wBAAwB;IAC9C,YAAY,EAAE,gBAAgB;IAC9B,YAAY,EAAE,eAAe;IAC7B,cAAc,EAAE,kBAAkB;CAC1B,CAAC"}
@@ -1,4 +1,5 @@
1
1
  import type { DomainEventDispatcher } from '../../application/ports/domain-event-dispatcher.js';
2
+ import type { ExecutionScope } from '../../application/ports/execution-scope.js';
2
3
  import type { DomainEvent } from '../../domain/domain-event.js';
3
4
  /**
4
5
  * Hands over the events a save pulled from its aggregates.
@@ -11,6 +12,10 @@ import type { DomainEvent } from '../../domain/domain-event.js';
11
12
  * handlers then `after-commit` ones, in the asynchronous context of that save. Events recorded
12
13
  * meanwhile, by a handler that saves an aggregate, wait behind them: one chain, in recording
13
14
  * order, without recursion. The first `before-commit` handler that fails rejects the promise.
15
+ *
16
+ * With an execution scope, the handlers of both phases run in the execution context of the save,
17
+ * whenever the dispatch happens: a command run inside another, for another tenant or another actor,
18
+ * shares the unit of work of the first, and its events are dispatched at the end of that one.
14
19
  */
15
- export declare function announceDomainEvents(events: readonly DomainEvent[], dispatcher: Pick<DomainEventDispatcher, 'dispatch'>): Promise<void>;
20
+ export declare function announceDomainEvents(events: readonly DomainEvent[], dispatcher: Pick<DomainEventDispatcher, 'dispatch'>, scope?: ExecutionScope): Promise<void>;
16
21
  //# sourceMappingURL=announce-domain-events.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"announce-domain-events.d.ts","sourceRoot":"","sources":["../../../src/infrastructure/domain-events/announce-domain-events.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,iDAAiD,CAAC;AAC7F,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,2BAA2B,CAAC;AAO7D;;;;;;;;;;;GAWG;AACH,wBAAsB,oBAAoB,CACxC,MAAM,EAAE,SAAS,WAAW,EAAE,EAC9B,UAAU,EAAE,IAAI,CAAC,qBAAqB,EAAE,UAAU,CAAC,GAClD,OAAO,CAAC,IAAI,CAAC,CAkBf"}
1
+ {"version":3,"file":"announce-domain-events.d.ts","sourceRoot":"","sources":["../../../src/infrastructure/domain-events/announce-domain-events.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,iDAAiD,CAAC;AAC7F,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,yCAAyC,CAAC;AAC9E,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,2BAA2B,CAAC;AAQ7D;;;;;;;;;;;;;;;GAeG;AACH,wBAAsB,oBAAoB,CACxC,MAAM,EAAE,SAAS,WAAW,EAAE,EAC9B,UAAU,EAAE,IAAI,CAAC,qBAAqB,EAAE,UAAU,CAAC,EACnD,KAAK,CAAC,EAAE,cAAc,GACrB,OAAO,CAAC,IAAI,CAAC,CAsBf"}
@@ -3,6 +3,7 @@ import { dispatchAfterCommit } from './dispatch-after-commit.js';
3
3
  import { dispatchBeforeCommit } from './dispatch-before-commit.js';
4
4
  import { here } from './here.js';
5
5
  import { SaveAfterCommitError } from './save-after-commit-error.js';
6
+ import { inContextOf } from './in-context-of.js';
6
7
  import { unitOfWorkScope } from './unit-of-work-scope.js';
7
8
  /**
8
9
  * Hands over the events a save pulled from its aggregates.
@@ -15,8 +16,12 @@ import { unitOfWorkScope } from './unit-of-work-scope.js';
15
16
  * handlers then `after-commit` ones, in the asynchronous context of that save. Events recorded
16
17
  * meanwhile, by a handler that saves an aggregate, wait behind them: one chain, in recording
17
18
  * order, without recursion. The first `before-commit` handler that fails rejects the promise.
19
+ *
20
+ * With an execution scope, the handlers of both phases run in the execution context of the save,
21
+ * whenever the dispatch happens: a command run inside another, for another tenant or another actor,
22
+ * shares the unit of work of the first, and its events are dispatched at the end of that one.
18
23
  */
19
- export async function announceDomainEvents(events, dispatcher) {
24
+ export async function announceDomainEvents(events, dispatcher, scope) {
20
25
  if (events.length === 0) {
21
26
  return;
22
27
  }
@@ -25,13 +30,14 @@ export async function announceDomainEvents(events, dispatcher) {
25
30
  throw new SaveAfterCommitError('announceDomainEvents');
26
31
  }
27
32
  if (current !== undefined && current.stage !== 'closed') {
28
- current.queue.push(...queued(events, dispatcher, current.kind === 'save' ? AsyncLocalStorage.snapshot() : here));
33
+ const within = current.kind === 'save' ? AsyncLocalStorage.snapshot() : here;
34
+ current.queue.push(...queued(events, dispatcher, { within, inContext: inContextOf(scope) }));
29
35
  return;
30
36
  }
31
- await dispatchedNow(queued(events, dispatcher, AsyncLocalStorage.snapshot()));
37
+ await dispatchedNow(queued(events, dispatcher, { within: AsyncLocalStorage.snapshot(), inContext: inContextOf(scope) }));
32
38
  }
33
- function queued(events, dispatcher, within) {
34
- return events.map((event) => ({ event, dispatcher, within }));
39
+ function queued(events, dispatcher, where) {
40
+ return events.map((event) => ({ event, dispatcher, ...where }));
35
41
  }
36
42
  async function dispatchedNow(events) {
37
43
  const scope = { kind: 'save', stage: 'open', queue: events, dispatched: [] };
@@ -1 +1 @@
1
- {"version":3,"file":"announce-domain-events.js","sourceRoot":"","sources":["../../../src/infrastructure/domain-events/announce-domain-events.ts"],"names":[],"m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UAAU,EAAE,MAAM,EAAE,CAAC,CAAC,CAAC;AAChE,CAAC;AAED,KAAK,UAAU,aAAa,CAAC,MAA2B;IACtD,MAAM,KAAK,GAAoB,EAAE,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,UAAU,EAAE,EAAE,EAAE,CAAC;IAE9F,IAAI,CAAC;QACH,MAAM,oBAAoB,CAAC,KAAK,CAAC,CAAC;IACpC,CAAC;YAAS,CAAC;QACT,KAAK,CAAC,KAAK,GAAG,QAAQ,CAAC;QACvB,KAAK,CAAC,KAAK,CAAC,MAAM,GAAG,CAAC,CAAC;IACzB,CAAC;IAED,MAAM,mBAAmB,CAAC,KAAK,CAAC,CAAC;AACnC,CAAC"}
1
+ {"version":3,"file":"announce-domain-events.js","sourceRoot":"","sources":["../../../src/infrastructure/domain-events/announce-domain-events.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,kBAAkB,CAAC;AAIrD,OAAO,EAAE,mBAAmB,EAAE,MAAM,yBAAyB,CAAC;AAC9D,OAAO,EAAE,oBAAoB,EAAE,MAAM,0BAA0B,CAAC;AAChE,OAAO,EAAE,IAAI,EAAE,MAAM,QAAQ,CAAC;AAC9B,OAAO,EAAE,oBAAoB,EAAE,MAAM,2BAA2B,CAAC;AACjE,OAAO,EAAE,WAAW,EAAE,MAAM,iBAAiB,CAAC;AAC9C,OAAO,EAAE,eAAe,EAA6D,MAAM,sBAAsB,CAAC;AAElH;;;;;;;;;;;;;;;GAeG;AACH,MAAM,CAAC,KAAK,UAAU,oBAAoB,CACxC,MAA8B,EAC9B,UAAmD,EACnD,KAAsB;IAEtB,IAAI,MAAM,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QACxB,OAAO;IACT,CAAC;IAED,MAAM,OAAO,GAAG,eAAe,CAAC,QAAQ,EAAE,CAAC;IAE3C,IAAI,OAAO,EAAE,KAAK,KAAK,cAAc,EAAE,CAAC;Q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B,CAAC,KAAK,CAAC,CAAC;AACnC,CAAC"}
@@ -1,5 +1,8 @@
1
1
  import type { DomainEventDispatcher } from '../../application/ports/domain-event-dispatcher.js';
2
+ import type { ExecutionScope } from '../../application/ports/execution-scope.js';
2
3
  type Dispatching = () => Pick<DomainEventDispatcher, 'dispatch'>;
4
+ /** The execution scope of the application, asked for at each save; none where the application binds none. */
5
+ type Scoping = () => ExecutionScope | undefined;
3
6
  /**
4
7
  * Makes domain events leave with the save. Every method of the repository that is handed an
5
8
  * aggregate (`save(order)`, `saveAll([a, b])`, `remove(order)`) and resolves has the events
@@ -14,7 +17,11 @@ type Dispatching = () => Pick<DomainEventDispatcher, 'dispatch'>;
14
17
  * answered and wrapping twice is a no-op; a sealed or frozen one is fronted by a proxy. The
15
18
  * dispatcher is asked for at each dispatch. An adapter contains no dispatch code: one that
16
19
  * pulls the events itself leaves nothing to dispatch here.
20
+ *
21
+ * Given the execution scope, the handlers run in the execution context of the save (its tenant, its
22
+ * actor, its correlation id), also when the unit of work that dispatches them was opened in another:
23
+ * a command run inside another one, for another tenant.
17
24
  */
18
- export declare function dispatchingOnSave<T extends object>(repository: T, dispatcher: Dispatching): T;
25
+ export declare function dispatchingOnSave<T extends object>(repository: T, dispatcher: Dispatching, scope?: Scoping): T;
19
26
  export {};
20
27
  //# sourceMappingURL=dispatching-on-save.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"dispatching-on-save.d.ts","sourceRoot":"","sources":["../../../src/infrastructure/domain-events/dispatching-on-save.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,iDAAiD,CAAC;AAQ7F,KAAK,WAAW,GAAG,MAAM,IAAI,CAAC,qBAAqB,EAAE,UAAU,CAAC,CAAC;AAIjE;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,iBAAiB,CAAC,CAAC,SAAS,MAAM,EAAE,UAAU,EAAE,CAAC,EAAE,UAAU,EAAE,WAAW,GAAG,CAAC,CAgB7F"}
1
+ {"version":3,"file":"dispatching-on-save.d.ts","sourceRoot":"","sources":["../../../src/infrastructure/domain-events/dispatching-on-save.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,iDAAiD,CAAC;AAC7F,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,yCAAyC,CAAC;AAQ9E,KAAK,WAAW,GAAG,MAAM,IAAI,CAAC,qBAAqB,EAAE,UAAU,CAAC,CAAC;AAEjE,6GAA6G;AAC7G,KAAK,OAAO,GAAG,MAAM,cAAc,GAAG,SAAS,CAAC;AAIhD;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,iBAAiB,CAAC,CAAC,SAAS,MAAM,EAAE,UAAU,EAAE,CAAC,EAAE,UAAU,EAAE,WAAW,EAAE,KAAK,CAAC,EAAE,OAAO,GAAG,CAAC,CAgB9G"}
@@ -17,14 +17,18 @@ const wrapped = new WeakSet();
17
17
  * answered and wrapping twice is a no-op; a sealed or frozen one is fronted by a proxy. The
18
18
  * dispatcher is asked for at each dispatch. An adapter contains no dispatch code: one that
19
19
  * pulls the events itself leaves nothing to dispatch here.
20
+ *
21
+ * Given the execution scope, the handlers run in the execution context of the save (its tenant, its
22
+ * actor, its correlation id), also when the unit of work that dispatches them was opened in another:
23
+ * a command run inside another one, for another tenant.
20
24
  */
21
- export function dispatchingOnSave(repository, dispatcher) {
25
+ export function dispatchingOnSave(repository, dispatcher, scope) {
22
26
  if (wrapped.has(repository)) {
23
27
  return repository;
24
28
  }
25
29
  const wrappers = new Map();
26
30
  for (const [name, method] of methodsOf(repository)) {
27
- wrappers.set(name, announcing(method, dispatcher));
31
+ wrappers.set(name, announcing(method, dispatcher, scope));
28
32
  }
29
33
  const result = Object.isExtensible(repository) ? shadowed(repository, wrappers) : proxied(repository, wrappers);
30
34
  wrapped.add(result);
@@ -51,10 +55,10 @@ function isMethod(value) {
51
55
  return typeof value === 'function' && !Function.prototype.toString.call(value).startsWith('class');
52
56
  }
53
57
  /** Async when the method is declared so, for what reads it later (tracing, an aspect) to see an asynchronous method. */
54
- function announcing(original, dispatcher) {
58
+ function announcing(original, dispatcher, scope) {
55
59
  const announced = async (args, result) => {
56
60
  const value = await result;
57
- await announceDomainEvents(recordedAggregates(args).flatMap((aggregate) => aggregate.pullEvents()), dispatcher());
61
+ await announceDomainEvents(recordedAggregates(args).flatMap((aggregate) => aggregate.pullEvents()), dispatcher(), scope?.());
58
62
  return value;
59
63
  };
60
64
  const wrapper = original.constructor.name === 'AsyncFunction'
@@ -1 +1 @@
1
- {"version":3,"file":"dispatching-on-save.js","sourceRoot":"","sources":["../../../src/infrastructure/domain-events/dispatching-on-save.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,oBAAoB,EAAE,MAAM,0BAA0B,CAAC;AAChE,OAAO,EAAE,kBAAkB,EAAE,MAAM,uBAAuB,CAAC;AAC3D,OAAO,EAAE,oBAAoB,EAAE,MAAM,2BAA2B,CAAC;AACjE,OAAO,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAC;AAMvD,MAAM,OAAO,GAAG,IAAI,OAAO,EAAU,CAAC;AAEtC;;;;;;;;;;;;;;GAcG;AACH,MAAM,UAAU,iBAAiB,CAAmB,UAAa,EAAE,UAAuB;IACxF,IAAI,OAAO,CAAC,GAAG,CAAC,UAAU,CAAC,EAAE,CAAC;QAC5B,OAAO,UAAU,CAAC;IACpB,CAAC;IAED,MAAM,QAAQ,GAAG,IAAI,GAAG,EAAkB,CAAC;IAE3C,KAAK,MAAM,CAAC,IAAI,EAAE,MAAM,CAAC,IAAI,SAAS,CAAC,UAAU,CAAC,EAAE,CAAC;QACnD,QAAQ,CAAC,GAAG,CAAC,IAAI,EAAE,UAAU,CAAC,MAAM,EAAE,UAAU,CAAC,CAAC,CAAC;IACrD,CAAC;IAED,MAAM,MAAM,GAAG,MAAM,CAAC,YAAY,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,UAAU,EAAE,QAAQ,CAA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AC;AAC5G,CAAC;AAED,SAAS,QAAQ,CAAmB,QAAW,EAAE,QAAqC;IACpF,KAAK,MAAM,CAAC,IAAI,EAAE,OAAO,CAAC,IAAI,QAAQ,EAAE,CAAC;QACvC,MAAM,UAAU,GAAG,MAAM,CAAC,wBAAwB,CAAC,QAAQ,EAAE,IAAI,CAAC,EAAE,UAAU,IAAI,KAAK,CAAC;QAExF,MAAM,CAAC,cAAc,CAAC,QAAQ,EAAE,IAAI,EAAE,EAAE,KAAK,EAAE,OAAO,EAAE,QAAQ,EAAE,IAAI,EAAE,YAAY,EAAE,IAAI,EAAE,UAAU,EAAE,CAAC,CAAC;IAC5G,CAAC;IAED,OAAO,QAAQ,CAAC;AAClB,CAAC;AAED,gHAAgH;AAChH,SAAS,OAAO,CAAmB,QAAW,EAAE,QAAqC;IACnF,OAAO,IAAI,KAAK,CAAC,QAAQ,EAAE;QACzB,GAAG,CAAC,MAAM,EAAE,QAAQ,EAAE,QAAiB;YACrC,MAAM,OAAO,GAAG,OAAO,QAAQ,KAAK,QAAQ,CAAC,CAAC,CAAC,QAAQ,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC;YAElF,OAAO,OAAO,IAAI,OAAO,CAAC,GAAG,CAAC,MAAM,EAAE,QAAQ,EAAE,QAAQ,CAAC,CAAC;QAC5D,CAAC;KACF,CAAC,CAAC;AACL,CAAC"}
@@ -3,7 +3,7 @@ export function domainEventRuns(events) {
3
3
  const groups = [];
4
4
  for (const queued of events) {
5
5
  const last = groups.at(-1);
6
- if (last !== undefined && last[0].dispatcher === queued.dispatcher && last[0].within === queued.within) {
6
+ if (last !== undefined && leavesWith(last[0], queued)) {
7
7
  last.push(queued);
8
8
  }
9
9
  else {
@@ -16,7 +16,10 @@ function runOf(group) {
16
16
  const [first] = group;
17
17
  const facts = group.map(({ event }) => event);
18
18
  return {
19
- dispatch: (scope, phase) => first.within(() => unitOfWorkScope.run(scope, async () => first.dispatcher.dispatch(facts, phase))),
19
+ dispatch: (scope, phase) => first.within(() => first.inContext(() => unitOfWorkScope.run(scope, async () => first.dispatcher.dispatch(facts, phase)))),
20
20
  };
21
21
  }
22
+ function leavesWith(one, other) {
23
+ return one.dispatcher === other.dispatcher && one.within === other.within && one.inContext === other.inContext;
24
+ }
22
25
  //# sourceMappingURL=domain-event-runs.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"domain-event-runs.js","sourceRoot":"","sources":["../../../src/infrastructure/domain-events/domain-event-runs.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,eAAe,EAAgD,MAAM,sBAAsB,CAAC;AAWrG,MAAM,UAAU,eAAe,CAAC,MAAoC;IAClE,MAAM,MAAM,GAAY,EAAE,CAAC;IAE3B,KAAK,MAAM,MAAM,IAAI,MAAM,EAAE,CAAC;QAC5B,MAAM,IAAI,GAAG,MAAM,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC;QAE3B,IAAI,IAAI,KAAK,SAAS,IAAI,IAAI,CAAC,CAAC,CAAC,CAAC,UAAU,KAAK,MAAM,CAAC,UAAU,IAAI,IAAI,CAAC,CAAC,CAAC,CAAC,MAAM,KAAK,MAAM,CAAC,MAAM,EAAE,CAAC;YACvG,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;QACpB,CAAC;aAAM,CAAC;YACN,MAAM,CAAC,IAAI,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC;QACxB,CAAC;IACH,CAAC;IAED,OAAO,MAAM,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC;AAC3B,CAAC;AAED,SAAS,KAAK,CAAC,KAAY;IACzB,MAAM,CAAC,KAAK,CAAC,GAAG,KAAK,CAAC;IACtB,MAAM,KAAK,GAAG,KAAK,CAAC,GAAG,CAAC,CAAC,EAAE,KAAK,EAAE,EAAE,EAAE,CAAC,KAAK,CAAC,CAAC;IAE9C,OAAO;QACL,QAAQ,EAAE,CAAC,KAAK,EAAE,KAAK,EAAE,EAAE,CACzB,KAA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K,EAAE,KAAK,IAAI,EAAE,CAAC,KAAK,CAAC,UAAU,CAAC,QAAQ,CAAC,KAAK,EAAE,KAAK,CAAC,CAAC,CAAC,CACvG;KACJ,CAAC;AACJ,CAAC;AAED,SAAS,UAAU,CAAC,GAAsB,EAAE,KAAwB;IAClE,OAAO,GAAG,CAAC,UAAU,KAAK,KAAK,CAAC,UAAU,IAAI,GAAG,CAAC,MAAM,KAAK,KAAK,CAAC,MAAM,IAAI,GAAG,CAAC,SAAS,KAAK,KAAK,CAAC,SAAS,CAAC;AACjH,CAAC"}
@@ -0,0 +1,9 @@
1
+ import type { ExecutionScope } from '../../application/ports/execution-scope.js';
2
+ import type { Within } from './unit-of-work-scope.js';
3
+ /**
4
+ * Runs a work in the execution context in progress now, whenever it runs. One function per context,
5
+ * so that the events of two saves of the same command still leave together; the caller as it is
6
+ * without a scope, or outside of any execution context.
7
+ */
8
+ export declare function inContextOf(scope: ExecutionScope | undefined): Within;
9
+ //# sourceMappingURL=in-context-of.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"in-context-of.d.ts","sourceRoot":"","sources":["../../../src/infrastructure/domain-events/in-context-of.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,yCAAyC,CAAC;AAE9E,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,sBAAsB,CAAC;AAInD;;;;GAIG;AACH,wBAAgB,WAAW,CAAC,KAAK,EAAE,cAAc,GAAG,SAAS,GAAG,MAAM,CAcrE"}
@@ -0,0 +1,19 @@
1
+ import { here } from './here.js';
2
+ const known = new WeakMap();
3
+ /**
4
+ * Runs a work in the execution context in progress now, whenever it runs. One function per context,
5
+ * so that the events of two saves of the same command still leave together; the caller as it is
6
+ * without a scope, or outside of any execution context.
7
+ */
8
+ export function inContextOf(scope) {
9
+ const context = scope?.current();
10
+ if (scope === undefined || context === undefined) {
11
+ return here;
12
+ }
13
+ const ofScope = known.get(scope) ?? new WeakMap();
14
+ const inContext = ofScope.get(context) ?? ((work) => scope.run(context, work));
15
+ known.set(scope, ofScope);
16
+ ofScope.set(context, inContext);
17
+ return inContext;
18
+ }
19
+ //# sourceMappingURL=in-context-of.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"in-context-of.js","sourceRoot":"","sources":["../../../src/infrastructure/domain-events/in-context-of.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,IAAI,EAAE,MAAM,QAAQ,CAAC;AAG9B,MAAM,KAAK,GAAG,IAAI,OAAO,EAAqD,CAAC;AAE/E;;;;GAIG;AACH,MAAM,UAAU,WAAW,CAAC,KAAiC;IAC3D,MAAM,OAAO,GAAG,KAAK,EAAE,OAAO,EAAE,CAAC;IAEjC,IAAI,KAAK,KAAK,SAAS,IAAI,OAAO,KAAK,SAAS,EAAE,CAAC;QACjD,OAAO,IAAI,CAAC;IACd,CAAC;IAED,MAAM,OAAO,GAAG,KAAK,CAAC,GAAG,CAAC,KAAK,CAAC,IAAI,IAAI,OAAO,EAA4B,CAAC;IAC5E,MAAM,SAAS,GAAG,OAAO,CAAC,GAAG,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,KAAK,CAAC,GAAG,CAAC,OAAO,EAAE,IAAI,CAAC,CAAC,CAAC;IAE/E,KAAK,CAAC,GAAG,CAAC,KAAK,EAAE,OAAO,CAAC,CAAC;IAC1B,OAAO,CAAC,GAAG,CAAC,OAAO,EAAE,SAAS,CAAC,CAAC;IAEhC,OAAO,SAAS,CAAC;AACnB,CAAC"}
@@ -2,11 +2,17 @@ import { AsyncLocalStorage } from 'node:async_hooks';
2
2
  import type { DomainEventDispatcher } from '../../application/ports/domain-event-dispatcher.js';
3
3
  import type { DomainEvent } from '../../domain/domain-event.js';
4
4
  import type { CommitStep } from './commit-step.js';
5
- /** One event waiting for the end of its scope: who dispatches it, and in which asynchronous context. */
5
+ /** Runs the work somewhere: in an asynchronous context, in an execution context. */
6
+ export type Within = <T>(work: () => T) => T;
7
+ /**
8
+ * One event waiting for the end of its scope: who dispatches it, in which asynchronous context, and in
9
+ * which execution context (`inContext`: that of the save that recorded it, kept in both phases).
10
+ */
6
11
  export interface QueuedDomainEvent {
7
12
  readonly event: DomainEvent;
8
13
  readonly dispatcher: Pick<DomainEventDispatcher, 'dispatch'>;
9
- readonly within: <T>(work: () => T) => T;
14
+ readonly within: Within;
15
+ readonly inContext: Within;
10
16
  }
11
17
  /**
12
18
  * What one unit of work has to dispatch. `unit-of-work` is opened by `withinUnitOfWork`; `save`
@@ -1 +1 @@
1
- {"version":3,"file":"unit-of-work-scope.d.ts","sourceRoot":"","sources":["../../../src/infrastructure/domain-events/unit-of-work-scope.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,kBAAkB,CAAC;AACrD,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,iDAAiD,CAAC;AAC7F,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,2BAA2B,CAAC;AAC7D,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AAEhD,wGAAwG;AACxG,MAAM,WAAW,iBAAiB;IAChC,QAAQ,CAAC,KAAK,EAAE,WAAW,CAAC;IAC5B,QAAQ,CAAC,UAAU,EAAE,IAAI,CAAC,qBAAqB,EAAE,UAAU,CAAC,CAAC;IAC7D,QAAQ,CAAC,MAAM,EAAE,CAAC,CAAC,EAAE,IAAI,EAAE,MAAM,CAAC,KAAK,CAAC,CAAC;CAC1C;AAED;;;GAGG;AACH,MAAM,WAAW,eAAe;IAC9B,QAAQ,CAAC,IAAI,EAAE,cAAc,GAAG,MAAM,CAAC;IACvC,KAAK,EAAE,MAAM,GAAG,eAAe,GAAG,cAAc,GAAG,QAAQ,CAAC;IAC5D,qDAAqD;IACrD,QAAQ,CAAC,KAAK,EAAE,iBAAiB,EAAE,CAAC;IACpC,2FAA2F;IAC3F,QAAQ,CAAC,UAAU,EAAE,iBAAiB,EAAE,CAAC;IACzC,mH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,SAAS,CAAC,EAAE,CAAC,MAAM,OAAO,CAAC,IAAI,CAAC,CAAC,EAAE,CAAC;IAC7C,0FAA0F;IAC1F,QAAQ,CAAC,QAAQ,CAAC,EAAE,UAAU,EAAE,CAAC;CAClC;AAGD,eAAO,MAAM,eAAe,oCAA2C,CAAC"}
@@ -1 +1 @@
1
- {"version":3,"file":"unit-of-work-scope.js","sourceRoot":"","sources":["../../../src/infrastructure/domain-events/unit-of-work-scope.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,kBAAkB,CAAC;AA6BrD,kGAAkG;AAClG,MAAM,CAAC,MAAM,eAAe,GAAG,IAAI,iBAAiB,EAAmB,CAAC"}
1
+ {"version":3,"file":"unit-of-work-scope.js","sourceRoot":"","sources":["../../../src/infrastructure/domain-events/unit-of-work-scope.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,kBAAkB,CAAC;AAoCrD,kGAAkG;AAClG,MAAM,CAAC,MAAM,eAAe,GAAG,IAAI,iBAAiB,EAAmB,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ddd-ts-kit/kernel",
3
- "version": "0.109.0",
3
+ "version": "0.110.0",
4
4
  "description": "Framework-agnostic Domain-Driven Design (DDD) building blocks for TypeScript: aggregates, entities, value objects, domain events, CQRS command and query buses, unit of work and application ports, with an in-memory event bus.",
5
5
  "keywords": [
6
6
  "ddd",