@sincpro/client-framework 1.2.0 → 1.3.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/AGENTS.md +38 -21
- package/MANIFESTO.md +4 -4
- package/README.md +183 -158
- package/dist/client/services/propagation.js +4 -3
- package/dist/codegen/services/scaffold.d.ts +12 -10
- package/dist/codegen/services/scaffold.js +23 -40
- package/dist/core/domain/bus.d.ts +5 -3
- package/dist/core/domain/bus.js +8 -1
- package/dist/core/domain/dto.d.ts +68 -6
- package/dist/core/domain/dto.js +22 -2
- package/dist/core/domain/pipeline.d.ts +5 -2
- package/dist/core/domain/services.d.ts +39 -13
- package/dist/core/domain/services.js +30 -6
- package/dist/core/entrypoint/bounded-context.d.ts +54 -35
- package/dist/core/entrypoint/bounded-context.js +120 -52
- package/dist/core/entrypoint/screens.js +0 -5
- package/dist/core/index.d.ts +4 -3
- package/dist/core/index.js +4 -3
- package/dist/core/infrastructure/listeners.d.ts +5 -4
- package/dist/core/infrastructure/listeners.js +1 -1
- package/dist/core/services/dto-keys.d.ts +44 -0
- package/dist/core/services/dto-keys.js +97 -0
- package/dist/core/services/execution.d.ts +10 -21
- package/dist/core/services/execution.js +29 -32
- package/dist/core/services/interceptors.d.ts +2 -2
- package/dist/core/services/interceptors.js +5 -5
- package/dist/core/services/listening.d.ts +6 -8
- package/dist/core/services/listening.js +3 -2
- package/dist/event_driven/adapters/stored-queue.d.ts +5 -5
- package/dist/event_driven/adapters/stored-queue.js +5 -10
- package/dist/event_driven/domain/envelope.d.ts +2 -2
- package/dist/event_driven/domain/envelope.js +7 -1
- package/dist/event_driven/domain/messages.d.ts +34 -0
- package/dist/event_driven/domain/messages.js +1 -0
- package/dist/event_driven/domain/queue.d.ts +9 -5
- package/dist/event_driven/entrypoint/publisher.d.ts +2 -2
- package/dist/event_driven/entrypoint/publisher.js +4 -0
- package/dist/event_driven/entrypoint/subscriber.d.ts +66 -27
- package/dist/event_driven/entrypoint/subscriber.js +117 -21
- package/dist/event_driven/index.d.ts +11 -10
- package/dist/event_driven/index.js +9 -9
- package/dist/event_driven/services/messages.d.ts +41 -0
- package/dist/event_driven/services/messages.js +147 -0
- package/dist/observability/domain/logging.d.ts +4 -8
- package/dist/observability/services/logging.d.ts +3 -3
- package/dist/observability/services/logging.js +1 -1
- package/dist/observability/services/trace-context.d.ts +10 -0
- package/dist/observability/services/trace-context.js +29 -0
- package/package.json +2 -2
- package/dist/event_driven/entrypoint/subscriptions.d.ts +0 -110
- package/dist/event_driven/entrypoint/subscriptions.js +0 -184
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import { Command, COMMAND_IDENTITY, DomainEvent, } from "../domain/dto.js";
|
|
2
|
+
export function isEventClass(key) {
|
|
3
|
+
return typeof key === "function" && key.prototype instanceof DomainEvent;
|
|
4
|
+
}
|
|
5
|
+
export function isCommandClass(key) {
|
|
6
|
+
return typeof key === "function" && key.prototype instanceof Command;
|
|
7
|
+
}
|
|
8
|
+
export function isCommandDescriptor(key) {
|
|
9
|
+
return (key !== null &&
|
|
10
|
+
typeof key === "object" &&
|
|
11
|
+
typeof key.identity === "string");
|
|
12
|
+
}
|
|
13
|
+
export function isEventDescriptor(key) {
|
|
14
|
+
return (key !== null &&
|
|
15
|
+
typeof key === "object" &&
|
|
16
|
+
!isCommandDescriptor(key) &&
|
|
17
|
+
typeof key.name === "string");
|
|
18
|
+
}
|
|
19
|
+
/** Whether `key` is something a service can be registered for. */
|
|
20
|
+
export function isDtoKey(key) {
|
|
21
|
+
return (isCommandClass(key) ||
|
|
22
|
+
isEventClass(key) ||
|
|
23
|
+
isCommandDescriptor(key) ||
|
|
24
|
+
isEventDescriptor(key));
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* What a registration key names:
|
|
28
|
+
*
|
|
29
|
+
* keyIdentity(CommandIssueInvoice) -> { kind: "command", type: CommandIssueInvoice, wire: "billing.CommandIssueInvoice" }
|
|
30
|
+
* keyIdentity(OrderPaid) -> { kind: "event", type: OrderPaid, wire: "shop.order.v1.paid" }
|
|
31
|
+
* keyIdentity(operations.CommandSendIssue) -> { kind: "command", type: undefined, wire: "project.CommandSendIssue" }
|
|
32
|
+
* keyIdentity(events.IssueClosed) -> { kind: "event", type: undefined, wire: "project.issue.v1.closed" }
|
|
33
|
+
*/
|
|
34
|
+
export function keyIdentity(key) {
|
|
35
|
+
if (isEventClass(key))
|
|
36
|
+
return { kind: "event", type: key, wire: key.eventName };
|
|
37
|
+
if (isCommandClass(key))
|
|
38
|
+
return { kind: "command", type: key, wire: key.commandName };
|
|
39
|
+
if (isCommandDescriptor(key))
|
|
40
|
+
return { kind: "command", type: undefined, wire: key.identity };
|
|
41
|
+
return { kind: "event", type: undefined, wire: key.name };
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* What a value being executed is:
|
|
45
|
+
*
|
|
46
|
+
* 1. An instance of a Command or a domain event: its class, and the identity the class declares.
|
|
47
|
+
* 2. A generated Command (`commandOf`): the identity it carries.
|
|
48
|
+
* 3. A generated event: the wire name in its data.
|
|
49
|
+
* Final: `undefined` for anything else — nothing a bus executes.
|
|
50
|
+
*/
|
|
51
|
+
export function identityOf(executable) {
|
|
52
|
+
if (executable instanceof DomainEvent) {
|
|
53
|
+
const type = Object.getPrototypeOf(executable)?.constructor;
|
|
54
|
+
return { kind: "event", type, wire: executable.name || undefined };
|
|
55
|
+
}
|
|
56
|
+
if (executable instanceof Command) {
|
|
57
|
+
const type = Object.getPrototypeOf(executable)?.constructor;
|
|
58
|
+
return { kind: "command", type, wire: type.commandName };
|
|
59
|
+
}
|
|
60
|
+
if (executable === null || typeof executable !== "object")
|
|
61
|
+
return undefined;
|
|
62
|
+
const carried = executable[COMMAND_IDENTITY];
|
|
63
|
+
if (typeof carried === "string")
|
|
64
|
+
return { kind: "command", type: undefined, wire: carried };
|
|
65
|
+
const name = executable.name;
|
|
66
|
+
if (typeof name === "string" && name !== "")
|
|
67
|
+
return { kind: "event", type: undefined, wire: name };
|
|
68
|
+
return undefined;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Whether `key` is what `executable` was registered under: the same class — exactly, a subclass
|
|
72
|
+
* is another DTO — or, for a generated DTO, the same kind and wire identity.
|
|
73
|
+
*/
|
|
74
|
+
export function matches(key, executable) {
|
|
75
|
+
const known = identityOf(executable);
|
|
76
|
+
if (known === undefined)
|
|
77
|
+
return false;
|
|
78
|
+
const wanted = keyIdentity(key);
|
|
79
|
+
if (wanted.type !== undefined)
|
|
80
|
+
return known.type === wanted.type;
|
|
81
|
+
return known.type === undefined && known.kind === wanted.kind && known.wire === wanted.wire;
|
|
82
|
+
}
|
|
83
|
+
/** Whether two values being executed are the same DTO: an interceptor passes on only that. */
|
|
84
|
+
export function sameDto(left, right) {
|
|
85
|
+
const one = identityOf(left);
|
|
86
|
+
const other = identityOf(right);
|
|
87
|
+
if (one === undefined || other === undefined)
|
|
88
|
+
return false;
|
|
89
|
+
if (one.type !== undefined || other.type !== undefined)
|
|
90
|
+
return one.type === other.type;
|
|
91
|
+
return one.kind === other.kind && one.wire === other.wire;
|
|
92
|
+
}
|
|
93
|
+
/** A DTO's name for a message: its wire identity, or its class's name. */
|
|
94
|
+
export function describeDto(executable) {
|
|
95
|
+
const known = identityOf(executable);
|
|
96
|
+
return known?.wire ?? known?.type?.name ?? typeof executable;
|
|
97
|
+
}
|
|
@@ -4,12 +4,7 @@ import type { Executable } from "../domain/dto.js";
|
|
|
4
4
|
import type { ErrorHandler } from "../domain/errors.js";
|
|
5
5
|
import type { Cause, ExecutionCompleted, ExecutionStarted, Interceptor, PipelineLayer } from "../domain/pipeline.js";
|
|
6
6
|
import { type Attributes, type Outcome } from "../../observability/domain/tracing.js";
|
|
7
|
-
|
|
8
|
-
* Where a run sits: a use case's layer (`PipelineLayer`), or an entrypoint that calls use cases —
|
|
9
|
-
* an `EventSubscriber`. Interceptors wrap and listeners hear use cases only.
|
|
10
|
-
*/
|
|
11
|
-
export type RunLayer = PipelineLayer | "entrypoint";
|
|
12
|
-
type Layer = RunLayer;
|
|
7
|
+
type Layer = PipelineLayer;
|
|
13
8
|
/** Where a failure first happened: the run, its executable, its layer and how it ended. */
|
|
14
9
|
export type FailedAt = {
|
|
15
10
|
readonly execution: Execution;
|
|
@@ -25,20 +20,17 @@ export interface Announcer {
|
|
|
25
20
|
completed(heard: ExecutionCompleted): void;
|
|
26
21
|
}
|
|
27
22
|
export interface ExecutionOptions {
|
|
28
|
-
/** Its bounded context — or, for an entrypoint's run, the registry it belongs to. */
|
|
29
23
|
readonly boundedContext: string;
|
|
30
24
|
readonly name: string;
|
|
31
25
|
readonly layer: Layer;
|
|
32
|
-
/** The method that runs it: a use case's `execute`, an entrypoint's `handle`. */
|
|
33
|
-
readonly method?: "execute" | "handle";
|
|
34
26
|
/** What else its span says: the event it executes, the service it replaced. */
|
|
35
27
|
readonly attributes?: Attributes;
|
|
36
28
|
readonly dependencies: ReadonlyMap<string, unknown>;
|
|
37
29
|
readonly context: () => ContextValues;
|
|
38
30
|
readonly parent?: Execution;
|
|
39
31
|
/**
|
|
40
|
-
* The run that called this root from outside its bounded context —
|
|
41
|
-
*
|
|
32
|
+
* The run that called this root from outside its bounded context — a Feature of another
|
|
33
|
+
* bounded context — through a bounded context bound to it (`BIND`): its span
|
|
42
34
|
* is the parent, its flow continues, it is the cause and its signal cancels this one; the
|
|
43
35
|
* context, dependencies and handlers stay this bounded context's.
|
|
44
36
|
*/
|
|
@@ -47,28 +39,25 @@ export interface ExecutionOptions {
|
|
|
47
39
|
readonly cause?: Cause;
|
|
48
40
|
/** What cancels a root run; a child inherits its parent's. */
|
|
49
41
|
readonly signal?: AbortSignal;
|
|
50
|
-
|
|
51
|
-
readonly featureBus?: (execution: Execution) => object;
|
|
42
|
+
readonly featureBus: (execution: Execution) => object;
|
|
52
43
|
/** What runs around the service, outermost first. */
|
|
53
44
|
readonly interceptors: readonly Interceptor[];
|
|
54
|
-
/** Who hears
|
|
55
|
-
readonly announce
|
|
45
|
+
/** Who hears it start and answer. */
|
|
46
|
+
readonly announce: Announcer;
|
|
56
47
|
/** The bounded context's expected errors: they end `refused`, never reported. */
|
|
57
48
|
readonly expected: (error: unknown) => boolean;
|
|
58
49
|
/** The bounded context's handlers for this layer: what answers a failure instead of raising it. */
|
|
59
50
|
readonly handlers: readonly ErrorHandler[];
|
|
60
51
|
}
|
|
61
52
|
/**
|
|
62
|
-
* One pipeline for every registered Feature and ApplicationService
|
|
63
|
-
* run that calls them (an `EventSubscriber`).
|
|
53
|
+
* One pipeline for every registered Feature and ApplicationService.
|
|
64
54
|
*
|
|
65
|
-
* 1. A span `<bounded context>/<
|
|
55
|
+
* 1. A span `<bounded context>/<Service>`, a child of the run above it when there is one (its
|
|
66
56
|
* parent, or the caller from outside), and the execution every dependency, `this.logger` and
|
|
67
57
|
* child Feature are bound to. Its chain: the run above's correlation (or the cause's, or its
|
|
68
58
|
* own id for a root), caused by the run above or the delivered event.
|
|
69
|
-
* 2.
|
|
70
|
-
* execution through its own view of the instance.
|
|
71
|
-
* nor intercepted.
|
|
59
|
+
* 2. Its start is announced; the interceptors run around the service, which sees the
|
|
60
|
+
* execution through its own view of the instance.
|
|
72
61
|
* 3. A failure is observed where it happened: its outcome (expected by the bounded context →
|
|
73
62
|
* `refused`; else the outcome it already had where it first failed — another bounded
|
|
74
63
|
* context's rules —; else the process's `outcomeOf`), the span closed with it, its log line,
|
|
@@ -6,6 +6,7 @@ import { NO_TRACER, } from "../../observability/domain/tracing.js";
|
|
|
6
6
|
import { observabilityNow } from "../../observability/infrastructure/registry.js";
|
|
7
7
|
import { executionLogger, logExecution, logHandled, } from "../../observability/services/logging.js";
|
|
8
8
|
import { firstReport } from "../../observability/services/reporting.js";
|
|
9
|
+
import { remoteSpan } from "../../observability/services/trace-context.js";
|
|
9
10
|
const FAILED_AT = new WeakMap();
|
|
10
11
|
/** Where `error` first failed, when it is an object a run threw; `undefined` otherwise. */
|
|
11
12
|
export function failedAt(error) {
|
|
@@ -64,9 +65,7 @@ function serviceView(target, options, execution) {
|
|
|
64
65
|
return execution.signal;
|
|
65
66
|
if (property === "execution")
|
|
66
67
|
return execution;
|
|
67
|
-
if (property === "featureBus" &&
|
|
68
|
-
options.layer === "application_service" &&
|
|
69
|
-
options.featureBus) {
|
|
68
|
+
if (property === "featureBus" && options.layer === "application_service") {
|
|
70
69
|
return (features ??= options.featureBus(execution));
|
|
71
70
|
}
|
|
72
71
|
if (typeof property === "string" && options.dependencies.has(property)) {
|
|
@@ -97,16 +96,14 @@ function serviceView(target, options, execution) {
|
|
|
97
96
|
});
|
|
98
97
|
}
|
|
99
98
|
/**
|
|
100
|
-
* One pipeline for every registered Feature and ApplicationService
|
|
101
|
-
* run that calls them (an `EventSubscriber`).
|
|
99
|
+
* One pipeline for every registered Feature and ApplicationService.
|
|
102
100
|
*
|
|
103
|
-
* 1. A span `<bounded context>/<
|
|
101
|
+
* 1. A span `<bounded context>/<Service>`, a child of the run above it when there is one (its
|
|
104
102
|
* parent, or the caller from outside), and the execution every dependency, `this.logger` and
|
|
105
103
|
* child Feature are bound to. Its chain: the run above's correlation (or the cause's, or its
|
|
106
104
|
* own id for a root), caused by the run above or the delivered event.
|
|
107
|
-
* 2.
|
|
108
|
-
* execution through its own view of the instance.
|
|
109
|
-
* nor intercepted.
|
|
105
|
+
* 2. Its start is announced; the interceptors run around the service, which sees the
|
|
106
|
+
* execution through its own view of the instance.
|
|
110
107
|
* 3. A failure is observed where it happened: its outcome (expected by the bounded context →
|
|
111
108
|
* `refused`; else the outcome it already had where it first failed — another bounded
|
|
112
109
|
* context's rules —; else the process's `outcomeOf`), the span closed with it, its log line,
|
|
@@ -127,7 +124,6 @@ export async function executeService(target, command, options) {
|
|
|
127
124
|
const correlationId = above?.correlationId ?? options.cause?.correlationId ?? id;
|
|
128
125
|
const causationId = above?.id ?? options.cause?.id;
|
|
129
126
|
const layer = options.layer;
|
|
130
|
-
const useCaseLayer = layer === "entrypoint" ? undefined : layer;
|
|
131
127
|
const attributes = {
|
|
132
128
|
"sincpro.use_case": useCase,
|
|
133
129
|
"sincpro.layer": options.layer,
|
|
@@ -137,8 +133,13 @@ export async function executeService(target, command, options) {
|
|
|
137
133
|
...(causationId ? { "sincpro.causation_id": causationId } : {}),
|
|
138
134
|
...options.attributes,
|
|
139
135
|
};
|
|
136
|
+
// A delivered message from another process continues its trace: its span is the parent.
|
|
137
|
+
const parentSpan = above?.span ??
|
|
138
|
+
(options.cause?.traceparent
|
|
139
|
+
? remoteSpan(options.cause.traceparent, options.cause.tracestate)
|
|
140
|
+
: undefined);
|
|
140
141
|
const span = observe(() => ports.tracer.start(`${options.boundedContext}/${options.name}`, {
|
|
141
|
-
...(
|
|
142
|
+
...(parentSpan ? { parent: parentSpan } : {}),
|
|
142
143
|
attributes,
|
|
143
144
|
}), NO_TRACER.start(useCase, {}));
|
|
144
145
|
const execution = {
|
|
@@ -157,8 +158,7 @@ export async function executeService(target, command, options) {
|
|
|
157
158
|
: new Set();
|
|
158
159
|
REPORTED_BY_EXECUTION.set(execution, reported);
|
|
159
160
|
logExecution(layer, execution);
|
|
160
|
-
|
|
161
|
-
observe(() => options.announce?.started({ executable: command, execution, layer: useCaseLayer }), undefined);
|
|
161
|
+
observe(() => options.announce.started({ executable: command, execution, layer }), undefined);
|
|
162
162
|
const finish = (error) => {
|
|
163
163
|
const outcome = error
|
|
164
164
|
? observe(() => options.expected(error.value)
|
|
@@ -193,22 +193,20 @@ export async function executeService(target, command, options) {
|
|
|
193
193
|
return outcome;
|
|
194
194
|
};
|
|
195
195
|
const view = serviceView(target, options, execution);
|
|
196
|
-
const
|
|
197
|
-
const service = (current) => Promise.resolve(Reflect.apply(
|
|
196
|
+
const execute = Reflect.get(target, "execute");
|
|
197
|
+
const service = (current) => Promise.resolve(Reflect.apply(execute, view, [current]));
|
|
198
198
|
let answer;
|
|
199
199
|
try {
|
|
200
200
|
const signal = execution.signal;
|
|
201
201
|
if (signal?.aborted)
|
|
202
202
|
throw abortedBy(signal);
|
|
203
|
-
const running =
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
})
|
|
211
|
-
: service(command);
|
|
203
|
+
const running = runAround(options.interceptors, service, command, {
|
|
204
|
+
boundedContext: options.boundedContext,
|
|
205
|
+
useCase,
|
|
206
|
+
layer,
|
|
207
|
+
execution,
|
|
208
|
+
logger: executionLogger(layer, execution),
|
|
209
|
+
});
|
|
212
210
|
answer = signal === undefined ? await running : await untilAborted(running, signal);
|
|
213
211
|
}
|
|
214
212
|
catch (error) {
|
|
@@ -220,13 +218,12 @@ export async function executeService(target, command, options) {
|
|
|
220
218
|
return recovered;
|
|
221
219
|
}
|
|
222
220
|
finish();
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
}), undefined);
|
|
221
|
+
observe(() => options.announce.completed({
|
|
222
|
+
executable: command,
|
|
223
|
+
execution,
|
|
224
|
+
layer,
|
|
225
|
+
answer,
|
|
226
|
+
durationMs: Math.max(0, Date.now() - startedAt),
|
|
227
|
+
}), undefined);
|
|
231
228
|
return answer;
|
|
232
229
|
}
|
|
@@ -8,8 +8,8 @@ import type { InterceptedRun, Interceptor } from "../domain/pipeline.js";
|
|
|
8
8
|
* runAround([<passes another class on>], …) -> TypeError, naming both classes
|
|
9
9
|
*
|
|
10
10
|
* 1. Each interceptor gets a `next` bound to the position after it.
|
|
11
|
-
* 2. What reaches `next` must be
|
|
12
|
-
* so another
|
|
11
|
+
* 2. What reaches `next` must be the DTO the call started with — its class, or its generated
|
|
12
|
+
* identity: the bus routes by it, so another would be another use case under this one's name.
|
|
13
13
|
* Final: the outermost interceptor's answer — the service's, when none answers by itself.
|
|
14
14
|
*/
|
|
15
15
|
export declare function runAround(interceptors: readonly Interceptor[], handler: (executable: Executable) => Promise<unknown>, executable: Executable, run: InterceptedRun): Promise<unknown>;
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { describeDto, sameDto } from "./dto-keys.js";
|
|
1
2
|
/**
|
|
2
3
|
* `handler` run on `executable`, wrapped by `interceptors` — the first the outermost.
|
|
3
4
|
*
|
|
@@ -6,15 +7,14 @@
|
|
|
6
7
|
* runAround([<passes another class on>], …) -> TypeError, naming both classes
|
|
7
8
|
*
|
|
8
9
|
* 1. Each interceptor gets a `next` bound to the position after it.
|
|
9
|
-
* 2. What reaches `next` must be
|
|
10
|
-
* so another
|
|
10
|
+
* 2. What reaches `next` must be the DTO the call started with — its class, or its generated
|
|
11
|
+
* identity: the bus routes by it, so another would be another use case under this one's name.
|
|
11
12
|
* Final: the outermost interceptor's answer — the service's, when none answers by itself.
|
|
12
13
|
*/
|
|
13
14
|
export function runAround(interceptors, handler, executable, run) {
|
|
14
|
-
const started = Object.getPrototypeOf(executable)?.constructor;
|
|
15
15
|
const at = async (position, current) => {
|
|
16
|
-
if (
|
|
17
|
-
throw new TypeError(`An interceptor of ${run.useCase} passed on a ${current
|
|
16
|
+
if (current !== executable && !sameDto(current, executable)) {
|
|
17
|
+
throw new TypeError(`An interceptor of ${run.useCase} passed on a ${describeDto(current)} where it received a ${describeDto(executable)}`);
|
|
18
18
|
}
|
|
19
19
|
const interceptor = interceptors[position];
|
|
20
20
|
if (interceptor === undefined)
|
|
@@ -1,17 +1,15 @@
|
|
|
1
|
-
import type { Executable } from "../domain/dto.js";
|
|
1
|
+
import type { DtoKey, Executable } from "../domain/dto.js";
|
|
2
2
|
import type { Listener } from "../domain/pipeline.js";
|
|
3
|
-
/**
|
|
4
|
-
export type ExecutableClass = abstract new (...args: never[]) => Executable;
|
|
5
|
-
/** An interceptor or a listener, and the classes it is for — every class when it names none. */
|
|
3
|
+
/** An interceptor or a listener, and the DTOs it is for — every one when it names none. */
|
|
6
4
|
export type Hearing<F> = {
|
|
7
5
|
readonly what: F;
|
|
8
|
-
readonly dtos: readonly
|
|
6
|
+
readonly dtos: readonly DtoKey[];
|
|
9
7
|
};
|
|
10
|
-
/** Whether something registered for `dtos` concerns `executable`:
|
|
11
|
-
export declare function concerns(dtos: readonly
|
|
8
|
+
/** Whether something registered for `dtos` concerns `executable`: its exact class, or its generated identity. */
|
|
9
|
+
export declare function concerns(dtos: readonly DtoKey[], executable: Executable): boolean;
|
|
12
10
|
/** Each listener that hears it, in turn; one that fails is ignored. */
|
|
13
11
|
export declare function announceTo<E extends {
|
|
14
12
|
executable: Executable;
|
|
15
13
|
}>(hearing: ReadonlySet<Hearing<Listener<E>>>, heard: E): void;
|
|
16
14
|
/** `listener` added to `hearing` for `dtos`; answers how to stop. */
|
|
17
|
-
export declare function listenIn<E>(hearing: Set<Hearing<Listener<E>>>, listener: Listener<E>, dtos: readonly
|
|
15
|
+
export declare function listenIn<E>(hearing: Set<Hearing<Listener<E>>>, listener: Listener<E>, dtos: readonly DtoKey[]): () => void;
|
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
|
|
1
|
+
import { matches } from "./dto-keys.js";
|
|
2
|
+
/** Whether something registered for `dtos` concerns `executable`: its exact class, or its generated identity. */
|
|
2
3
|
export function concerns(dtos, executable) {
|
|
3
|
-
return dtos.length === 0 || dtos.
|
|
4
|
+
return dtos.length === 0 || dtos.some((dto) => matches(dto, executable));
|
|
4
5
|
}
|
|
5
6
|
/** Each listener that hears it, in turn; one that fails is ignored. */
|
|
6
7
|
export function announceTo(hearing, heard) {
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { type JsonValue } from "../../common/serialization.js";
|
|
2
|
-
import type { DomainEvent,
|
|
3
|
-
import type { EventQueue, Subscribes } from "../domain/queue.js";
|
|
2
|
+
import type { DomainEvent, RemoteEvent } from "../../core/domain/dto.js";
|
|
3
|
+
import type { EventQueue, EventType, Subscribes } from "../domain/queue.js";
|
|
4
4
|
import type { EventStore, StoredEvent } from "../domain/store.js";
|
|
5
5
|
export type StoredQueueOptions = {
|
|
6
6
|
/** Where the events wait: memory, a phone's SQLite, a browser's IndexedDB. */
|
|
@@ -12,7 +12,7 @@ export type StoredQueueOptions = {
|
|
|
12
12
|
* has one, else its values set on an instance of it — as JSON gave them: an event that carries
|
|
13
13
|
* a `Money`, a `Decimal` or a date declares `fromJSON` to read them back as such.
|
|
14
14
|
*/
|
|
15
|
-
readonly rebuild?: (type:
|
|
15
|
+
readonly rebuild?: (type: EventType, payload: JsonValue) => DomainEvent | RemoteEvent;
|
|
16
16
|
/**
|
|
17
17
|
* Whether an event may be delivered now — a phone asks for a connection, or a session. One
|
|
18
18
|
* that is not waits while the rest go on; `drain()` again when what it waits for comes back.
|
|
@@ -61,8 +61,8 @@ export type StoredQueue = EventQueue & {
|
|
|
61
61
|
*
|
|
62
62
|
* Delivery is at least once: a process that stops after a context executed an event and before
|
|
63
63
|
* the store saved it executes it there again on the next drain. What must not act twice keys what
|
|
64
|
-
* it does by the envelope's id — `this.execution.causationId` in
|
|
65
|
-
*
|
|
64
|
+
* it does by the envelope's id — `this.execution.causationId` in the use case of the event — or
|
|
65
|
+
* by the entity the event names.
|
|
66
66
|
* And one process drains a store: two tabs over one IndexedDB claim it first (the Web Locks API).
|
|
67
67
|
*/
|
|
68
68
|
export declare function storedQueue(options: StoredQueueOptions): StoredQueue;
|
|
@@ -1,12 +1,7 @@
|
|
|
1
1
|
import { failureOf } from "../../common/failures.js";
|
|
2
|
-
import {
|
|
2
|
+
import { valuesOf } from "../../common/serialization.js";
|
|
3
|
+
import { eventFrom } from "../services/messages.js";
|
|
3
4
|
import { loggerAdapter } from "../../observability/services/logging.js";
|
|
4
|
-
function rebuildEvent(type, payload) {
|
|
5
|
-
const values = readValues(payload);
|
|
6
|
-
if (typeof type.fromJSON === "function")
|
|
7
|
-
return rebuilt(values, type);
|
|
8
|
-
return Object.assign(Object.create(type.prototype), values);
|
|
9
|
-
}
|
|
10
5
|
function described(boundedContext, error) {
|
|
11
6
|
const failure = failureOf(error);
|
|
12
7
|
return {
|
|
@@ -42,8 +37,8 @@ function described(boundedContext, error) {
|
|
|
42
37
|
*
|
|
43
38
|
* Delivery is at least once: a process that stops after a context executed an event and before
|
|
44
39
|
* the store saved it executes it there again on the next drain. What must not act twice keys what
|
|
45
|
-
* it does by the envelope's id — `this.execution.causationId` in
|
|
46
|
-
*
|
|
40
|
+
* it does by the envelope's id — `this.execution.causationId` in the use case of the event — or
|
|
41
|
+
* by the entity the event names.
|
|
47
42
|
* And one process drains a store: two tabs over one IndexedDB claim it first (the Web Locks API).
|
|
48
43
|
*/
|
|
49
44
|
export function storedQueue(options) {
|
|
@@ -53,7 +48,7 @@ export function storedQueue(options) {
|
|
|
53
48
|
const maxAttempts = options.maxAttempts ?? 10;
|
|
54
49
|
if (!(maxAttempts >= 1))
|
|
55
50
|
throw new TypeError("A stored queue needs one attempt at least");
|
|
56
|
-
const rebuild = options.rebuild ??
|
|
51
|
+
const rebuild = options.rebuild ?? eventFrom;
|
|
57
52
|
let draining;
|
|
58
53
|
let again = false;
|
|
59
54
|
let timer;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type
|
|
1
|
+
import { type DomainEvent, type RemoteEvent } from "../../core/domain/dto.js";
|
|
2
2
|
import type { Cause } from "../../core/domain/pipeline.js";
|
|
3
3
|
/**
|
|
4
4
|
* What travels beside a published event: who it is, which flow it belongs to and what caused it
|
|
@@ -25,4 +25,4 @@ export type Envelope = Cause & {
|
|
|
25
25
|
* The envelope of `event`, published by the run `cause` names — or the head of its own flow when
|
|
26
26
|
* nothing caused it (its `correlationId` is its own `id`).
|
|
27
27
|
*/
|
|
28
|
-
export declare function envelopeOf(event: DomainEvent, cause?: Cause): Envelope;
|
|
28
|
+
export declare function envelopeOf(event: DomainEvent | RemoteEvent, cause?: Cause): Envelope;
|
|
@@ -1,13 +1,17 @@
|
|
|
1
1
|
import { newEntityId } from "../../common/ids.js";
|
|
2
|
+
import { EVENT_NAME, } from "../../core/domain/dto.js";
|
|
2
3
|
/**
|
|
3
4
|
* The envelope of `event`, published by the run `cause` names — or the head of its own flow when
|
|
4
5
|
* nothing caused it (its `correlationId` is its own `id`).
|
|
5
6
|
*/
|
|
6
7
|
export function envelopeOf(event, cause) {
|
|
7
8
|
const name = event.name;
|
|
8
|
-
if (name.trim() === "") {
|
|
9
|
+
if (typeof name !== "string" || name.trim() === "") {
|
|
9
10
|
throw new Error(`${event.constructor.name} has no eventName: an event is published by its wire name`);
|
|
10
11
|
}
|
|
12
|
+
if (!EVENT_NAME.test(name)) {
|
|
13
|
+
throw new Error(`${name} is not an event's name: <context>.<aggregate>.vN.<fact>`);
|
|
14
|
+
}
|
|
11
15
|
const id = newEntityId();
|
|
12
16
|
return {
|
|
13
17
|
name,
|
|
@@ -15,5 +19,7 @@ export function envelopeOf(event, cause) {
|
|
|
15
19
|
correlationId: cause?.correlationId ?? id,
|
|
16
20
|
causationId: cause?.id ?? null,
|
|
17
21
|
publishedAt: new Date().toISOString(),
|
|
22
|
+
...(cause?.traceparent ? { traceparent: cause.traceparent } : {}),
|
|
23
|
+
...(cause?.tracestate ? { tracestate: cause.tracestate } : {}),
|
|
18
24
|
};
|
|
19
25
|
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import type { JsonValue } from "../../common/serialization.js";
|
|
2
|
+
/**
|
|
3
|
+
* A Command or an event as it travels — CloudEvents 1.0, as sincpro_framework writes it on every
|
|
4
|
+
* queue (`Envelope`): `type` says what it is — an event's `name`, a Command's identity
|
|
5
|
+
* (`<context>.<Command>`) —, `data` is the DTO as JSON, the chain and the trace ride beside it.
|
|
6
|
+
*
|
|
7
|
+
* { specversion: "1.0", id: "0193…", source: "urn:svc:billing", type: "billing.invoice.v1.issued",
|
|
8
|
+
* time: "2026-11-11T03:00:00Z", correlationid: "0192…", causationid: "0191…",
|
|
9
|
+
* traceparent: "00-…-01", datacontenttype: "application/json", data: { name: …, invoice_id: … } }
|
|
10
|
+
*
|
|
11
|
+
* Context: one shape for both; what differs is the route — an event to every bus that registered
|
|
12
|
+
* its name, a Command to the one bus that registered its identity, and only when declared. A
|
|
13
|
+
* broker's binary mode carries the same attributes as `ce_` headers (`readMessage`).
|
|
14
|
+
*/
|
|
15
|
+
export type Message = {
|
|
16
|
+
readonly specversion: "1.0";
|
|
17
|
+
readonly id: string;
|
|
18
|
+
readonly source: string;
|
|
19
|
+
readonly type: string;
|
|
20
|
+
readonly subject?: string;
|
|
21
|
+
/** When it was produced, ISO. */
|
|
22
|
+
readonly time: string;
|
|
23
|
+
readonly correlationid: string;
|
|
24
|
+
readonly causationid?: string;
|
|
25
|
+
readonly traceparent?: string;
|
|
26
|
+
readonly tracestate?: string;
|
|
27
|
+
readonly datacontenttype: "application/json";
|
|
28
|
+
readonly data: JsonValue;
|
|
29
|
+
};
|
|
30
|
+
/** A message in a broker's binary mode: its attributes as headers, its data as the body. */
|
|
31
|
+
export type BinaryMessage = {
|
|
32
|
+
readonly headers: Readonly<Record<string, string | Uint8Array | null | undefined>>;
|
|
33
|
+
readonly body: string | JsonValue;
|
|
34
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { DomainEvent, EventClass } from "../../core/domain/dto.js";
|
|
1
|
+
import type { AnyCommand, CommandClass, CommandDescriptor, DomainEvent, EventClass, EventDescriptor, RemoteEvent } from "../../core/domain/dto.js";
|
|
2
2
|
import type { Envelope } from "./envelope.js";
|
|
3
3
|
/**
|
|
4
4
|
* What carries a published event to whoever hears it — the port each platform implements with
|
|
@@ -15,8 +15,12 @@ import type { Envelope } from "./envelope.js";
|
|
|
15
15
|
* every bounded context that hears it.
|
|
16
16
|
*/
|
|
17
17
|
export interface EventQueue {
|
|
18
|
-
put(event: DomainEvent, envelope: Envelope): void | Promise<void>;
|
|
18
|
+
put(event: DomainEvent | RemoteEvent, envelope: Envelope): void | Promise<void>;
|
|
19
19
|
}
|
|
20
|
+
/** What an event of a wire name is rebuilt as: its class, or the generated entry it was registered with. */
|
|
21
|
+
export type EventType = EventClass<DomainEvent> | EventDescriptor;
|
|
22
|
+
/** What a Command of an identity is rebuilt as: its class, or the generated entry it was registered with. */
|
|
23
|
+
export type CommandType = CommandClass<AnyCommand> | CommandDescriptor;
|
|
20
24
|
/** What one bounded context answered, or failed with, for a delivered event. */
|
|
21
25
|
export type Handled = {
|
|
22
26
|
readonly boundedContext: string;
|
|
@@ -30,11 +34,11 @@ export type Handled = {
|
|
|
30
34
|
* rebuilt as, and the event executed by every subscriber.
|
|
31
35
|
*/
|
|
32
36
|
export interface Subscribes {
|
|
33
|
-
/**
|
|
34
|
-
eventTypeOf(name: string):
|
|
37
|
+
/** What an event of that wire name is rebuilt as, or `undefined` when nobody hears it. */
|
|
38
|
+
eventTypeOf(name: string): EventType | undefined;
|
|
35
39
|
/**
|
|
36
40
|
* The event executed in every bounded context that hears it — but those `skip` names, which
|
|
37
41
|
* already have it — and what each answered.
|
|
38
42
|
*/
|
|
39
|
-
handle(event: DomainEvent, envelope?: Envelope, skip?: readonly string[]): Promise<Handled[]>;
|
|
43
|
+
handle(event: DomainEvent | RemoteEvent, envelope?: Envelope, skip?: readonly string[]): Promise<Handled[]>;
|
|
40
44
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { BIND, type Execution } from "../../context/domain/execution.js";
|
|
2
|
-
import type { DomainEvent } from "../../core/domain/dto.js";
|
|
2
|
+
import type { DomainEvent, RemoteEvent } from "../../core/domain/dto.js";
|
|
3
3
|
import type { Cause } from "../../core/domain/pipeline.js";
|
|
4
4
|
import { type Envelope } from "../domain/envelope.js";
|
|
5
5
|
import type { EventQueue } from "../domain/queue.js";
|
|
@@ -20,7 +20,7 @@ export declare class Publisher {
|
|
|
20
20
|
private readonly cause?;
|
|
21
21
|
constructor(queue: EventQueue, cause?: Cause | undefined);
|
|
22
22
|
/** `event` into the queue, with its envelope; answers the envelope once the queue took it. */
|
|
23
|
-
publish(event: DomainEvent): Promise<Envelope>;
|
|
23
|
+
publish(event: DomainEvent | RemoteEvent): Promise<Envelope>;
|
|
24
24
|
/** The same publisher, publishing as `execution`: its events join that run's flow. */
|
|
25
25
|
[BIND](execution: Execution): Publisher;
|
|
26
26
|
}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { BIND } from "../../context/domain/execution.js";
|
|
2
2
|
import { envelopeOf, } from "../domain/envelope.js";
|
|
3
|
+
import { traceparentOf } from "../../observability/services/trace-context.js";
|
|
3
4
|
/**
|
|
4
5
|
* Puts events into a queue — what a Feature holds to tell whoever listens, as the backend's
|
|
5
6
|
* `Publisher`:
|
|
@@ -27,9 +28,12 @@ export class Publisher {
|
|
|
27
28
|
}
|
|
28
29
|
/** The same publisher, publishing as `execution`: its events join that run's flow. */
|
|
29
30
|
[BIND](execution) {
|
|
31
|
+
const traceparent = traceparentOf(execution.span);
|
|
30
32
|
return new Publisher(this.queue, {
|
|
31
33
|
id: execution.id,
|
|
32
34
|
correlationId: execution.correlationId,
|
|
35
|
+
...(traceparent ? { traceparent } : {}),
|
|
36
|
+
...(execution.span.state ? { tracestate: execution.span.state } : {}),
|
|
33
37
|
});
|
|
34
38
|
}
|
|
35
39
|
}
|