@sincpro/client-framework 1.1.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 +51 -17
- package/MANIFESTO.md +9 -7
- package/README.md +261 -131
- package/dist/client/services/guarded.d.ts +1 -1
- package/dist/client/services/propagation.js +4 -3
- package/dist/codegen/services/scaffold.d.ts +12 -10
- package/dist/codegen/services/scaffold.js +29 -51
- package/dist/context/entrypoint/context.d.ts +5 -4
- package/dist/core/domain/bus.d.ts +5 -3
- package/dist/core/domain/bus.js +8 -1
- package/dist/core/domain/dto.d.ts +71 -9
- package/dist/core/domain/dto.js +25 -5
- package/dist/core/domain/errors.d.ts +2 -2
- package/dist/core/domain/pipeline.d.ts +8 -5
- package/dist/core/domain/services.d.ts +40 -13
- package/dist/core/domain/services.js +31 -7
- package/dist/core/entrypoint/bounded-context.d.ts +71 -41
- package/dist/core/entrypoint/bounded-context.js +155 -56
- 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 +6 -5
- package/dist/core/infrastructure/listeners.js +2 -2
- 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 +15 -10
- package/dist/core/services/execution.js +36 -23
- 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 +8 -7
- package/dist/event_driven/adapters/stored-queue.js +8 -12
- 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 +3 -3
- package/dist/event_driven/entrypoint/publisher.js +5 -1
- package/dist/event_driven/entrypoint/subscriber.d.ts +66 -19
- package/dist/event_driven/entrypoint/subscriber.js +122 -15
- package/dist/event_driven/index.d.ts +11 -7
- package/dist/event_driven/index.js +9 -6
- package/dist/event_driven/services/messages.d.ts +41 -0
- package/dist/event_driven/services/messages.js +147 -0
- package/dist/observability/adapters/memory.d.ts +1 -1
- package/dist/observability/domain/logging.d.ts +2 -2
- package/dist/observability/services/logging.d.ts +6 -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
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import { listenIn
|
|
1
|
+
import { listenIn } from "../services/listening.js";
|
|
2
2
|
/**
|
|
3
3
|
* Who hears every bounded context of this process — the backend's process listeners: an
|
|
4
4
|
* activity indicator, a toast, an audit that must not be wired into each context.
|
|
5
5
|
*
|
|
6
6
|
* const stop = processListeners.onFailure((failed) => toast(failed.execution?.useCase));
|
|
7
7
|
*
|
|
8
|
-
* Context: a bounded context's own listeners (`
|
|
8
|
+
* Context: a bounded context's own listeners (`projectBoundedContext.onStart`) hear it alone; these hear all of
|
|
9
9
|
* them, after its own. They live as long as the process — `configureProcess` does not clear
|
|
10
10
|
* them —, so whoever adds one keeps the function that stops it.
|
|
11
11
|
*/
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { type AnyCommand, type CommandClass, type CommandDescriptor, DomainEvent, type DtoKey, type EventClass, type EventDescriptor, type Executable } from "../domain/dto.js";
|
|
2
|
+
/** How a DTO travels: a Command to one bus, by its identity; an event to N, by its wire name. */
|
|
3
|
+
export type DtoKind = "command" | "event";
|
|
4
|
+
/** What a DTO is known by: its kind, its class when it has one, and its wire identity when it has one. */
|
|
5
|
+
export type DtoIdentity = {
|
|
6
|
+
readonly kind: DtoKind;
|
|
7
|
+
/** The class it is an instance of; `undefined` for a generated DTO, which is data. */
|
|
8
|
+
readonly type: (abstract new (...args: never[]) => object) | undefined;
|
|
9
|
+
/** A Command's identity (`<context>.<Command>`), an event's wire name; `undefined` when none is declared. */
|
|
10
|
+
readonly wire: string | undefined;
|
|
11
|
+
};
|
|
12
|
+
export declare function isEventClass(key: unknown): key is EventClass<DomainEvent>;
|
|
13
|
+
export declare function isCommandClass(key: unknown): key is CommandClass<AnyCommand>;
|
|
14
|
+
export declare function isCommandDescriptor(key: unknown): key is CommandDescriptor;
|
|
15
|
+
export declare function isEventDescriptor(key: unknown): key is EventDescriptor;
|
|
16
|
+
/** Whether `key` is something a service can be registered for. */
|
|
17
|
+
export declare function isDtoKey(key: unknown): key is DtoKey;
|
|
18
|
+
/**
|
|
19
|
+
* What a registration key names:
|
|
20
|
+
*
|
|
21
|
+
* keyIdentity(CommandIssueInvoice) -> { kind: "command", type: CommandIssueInvoice, wire: "billing.CommandIssueInvoice" }
|
|
22
|
+
* keyIdentity(OrderPaid) -> { kind: "event", type: OrderPaid, wire: "shop.order.v1.paid" }
|
|
23
|
+
* keyIdentity(operations.CommandSendIssue) -> { kind: "command", type: undefined, wire: "project.CommandSendIssue" }
|
|
24
|
+
* keyIdentity(events.IssueClosed) -> { kind: "event", type: undefined, wire: "project.issue.v1.closed" }
|
|
25
|
+
*/
|
|
26
|
+
export declare function keyIdentity(key: DtoKey): DtoIdentity;
|
|
27
|
+
/**
|
|
28
|
+
* What a value being executed is:
|
|
29
|
+
*
|
|
30
|
+
* 1. An instance of a Command or a domain event: its class, and the identity the class declares.
|
|
31
|
+
* 2. A generated Command (`commandOf`): the identity it carries.
|
|
32
|
+
* 3. A generated event: the wire name in its data.
|
|
33
|
+
* Final: `undefined` for anything else — nothing a bus executes.
|
|
34
|
+
*/
|
|
35
|
+
export declare function identityOf(executable: unknown): DtoIdentity | undefined;
|
|
36
|
+
/**
|
|
37
|
+
* Whether `key` is what `executable` was registered under: the same class — exactly, a subclass
|
|
38
|
+
* is another DTO — or, for a generated DTO, the same kind and wire identity.
|
|
39
|
+
*/
|
|
40
|
+
export declare function matches(key: DtoKey, executable: Executable): boolean;
|
|
41
|
+
/** Whether two values being executed are the same DTO: an interceptor passes on only that. */
|
|
42
|
+
export declare function sameDto(left: unknown, right: unknown): boolean;
|
|
43
|
+
/** A DTO's name for a message: its wire identity, or its class's name. */
|
|
44
|
+
export declare function describeDto(executable: unknown): string;
|
|
@@ -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,9 +4,6 @@ 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
|
-
export interface RuntimeService {
|
|
8
|
-
execute(command: object): unknown;
|
|
9
|
-
}
|
|
10
7
|
type Layer = PipelineLayer;
|
|
11
8
|
/** Where a failure first happened: the run, its executable, its layer and how it ended. */
|
|
12
9
|
export type FailedAt = {
|
|
@@ -31,6 +28,13 @@ export interface ExecutionOptions {
|
|
|
31
28
|
readonly dependencies: ReadonlyMap<string, unknown>;
|
|
32
29
|
readonly context: () => ContextValues;
|
|
33
30
|
readonly parent?: Execution;
|
|
31
|
+
/**
|
|
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
|
|
34
|
+
* is the parent, its flow continues, it is the cause and its signal cancels this one; the
|
|
35
|
+
* context, dependencies and handlers stay this bounded context's.
|
|
36
|
+
*/
|
|
37
|
+
readonly caller?: Execution;
|
|
34
38
|
/** What caused a root run from outside: a delivered event's envelope continues its chain. */
|
|
35
39
|
readonly cause?: Cause;
|
|
36
40
|
/** What cancels a root run; a child inherits its parent's. */
|
|
@@ -48,19 +52,20 @@ export interface ExecutionOptions {
|
|
|
48
52
|
/**
|
|
49
53
|
* One pipeline for every registered Feature and ApplicationService.
|
|
50
54
|
*
|
|
51
|
-
* 1. A span `<bounded context>/<
|
|
52
|
-
* the execution every dependency, `this.logger` and
|
|
53
|
-
* Its chain: the
|
|
54
|
-
* the
|
|
55
|
+
* 1. A span `<bounded context>/<Service>`, a child of the run above it when there is one (its
|
|
56
|
+
* parent, or the caller from outside), and the execution every dependency, `this.logger` and
|
|
57
|
+
* child Feature are bound to. Its chain: the run above's correlation (or the cause's, or its
|
|
58
|
+
* own id for a root), caused by the run above or the delivered event.
|
|
55
59
|
* 2. Its start is announced; the interceptors run around the service, which sees the
|
|
56
60
|
* execution through its own view of the instance.
|
|
57
61
|
* 3. A failure is observed where it happened: its outcome (expected by the bounded context →
|
|
58
|
-
* `refused
|
|
59
|
-
*
|
|
62
|
+
* `refused`; else the outcome it already had where it first failed — another bounded
|
|
63
|
+
* context's rules —; else the process's `outcomeOf`), the span closed with it, its log line,
|
|
64
|
+
* where it first failed and — when `failed`, and only once per error along the flow — its
|
|
60
65
|
* report.
|
|
61
66
|
* 4. Then the layer's error handlers answer it, in order; with none, or when the last throws,
|
|
62
67
|
* the error reaches the caller.
|
|
63
68
|
* Final: the service's answer — announced —, or its handlers'.
|
|
64
69
|
*/
|
|
65
|
-
export declare function executeService(target:
|
|
70
|
+
export declare function executeService(target: object, command: Executable, options: ExecutionOptions): Promise<unknown>;
|
|
66
71
|
export {};
|
|
@@ -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) {
|
|
@@ -97,15 +98,16 @@ function serviceView(target, options, execution) {
|
|
|
97
98
|
/**
|
|
98
99
|
* One pipeline for every registered Feature and ApplicationService.
|
|
99
100
|
*
|
|
100
|
-
* 1. A span `<bounded context>/<
|
|
101
|
-
* the execution every dependency, `this.logger` and
|
|
102
|
-
* Its chain: the
|
|
103
|
-
* the
|
|
101
|
+
* 1. A span `<bounded context>/<Service>`, a child of the run above it when there is one (its
|
|
102
|
+
* parent, or the caller from outside), and the execution every dependency, `this.logger` and
|
|
103
|
+
* child Feature are bound to. Its chain: the run above's correlation (or the cause's, or its
|
|
104
|
+
* own id for a root), caused by the run above or the delivered event.
|
|
104
105
|
* 2. Its start is announced; the interceptors run around the service, which sees the
|
|
105
106
|
* execution through its own view of the instance.
|
|
106
107
|
* 3. A failure is observed where it happened: its outcome (expected by the bounded context →
|
|
107
|
-
* `refused
|
|
108
|
-
*
|
|
108
|
+
* `refused`; else the outcome it already had where it first failed — another bounded
|
|
109
|
+
* context's rules —; else the process's `outcomeOf`), the span closed with it, its log line,
|
|
110
|
+
* where it first failed and — when `failed`, and only once per error along the flow — its
|
|
109
111
|
* report.
|
|
110
112
|
* 4. Then the layer's error handlers answer it, in order; with none, or when the last throws,
|
|
111
113
|
* the error reaches the caller.
|
|
@@ -117,8 +119,11 @@ export async function executeService(target, command, options) {
|
|
|
117
119
|
const useCase = `${options.boundedContext}.${options.name}`;
|
|
118
120
|
const context = options.parent?.context ?? options.context();
|
|
119
121
|
const id = newEntityId();
|
|
120
|
-
|
|
121
|
-
const
|
|
122
|
+
// The run above this one: its parent in the bounded context, or the caller from outside.
|
|
123
|
+
const above = options.parent ?? options.caller;
|
|
124
|
+
const correlationId = above?.correlationId ?? options.cause?.correlationId ?? id;
|
|
125
|
+
const causationId = above?.id ?? options.cause?.id;
|
|
126
|
+
const layer = options.layer;
|
|
122
127
|
const attributes = {
|
|
123
128
|
"sincpro.use_case": useCase,
|
|
124
129
|
"sincpro.layer": options.layer,
|
|
@@ -128,33 +133,40 @@ export async function executeService(target, command, options) {
|
|
|
128
133
|
...(causationId ? { "sincpro.causation_id": causationId } : {}),
|
|
129
134
|
...options.attributes,
|
|
130
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);
|
|
131
141
|
const span = observe(() => ports.tracer.start(`${options.boundedContext}/${options.name}`, {
|
|
132
|
-
...(
|
|
142
|
+
...(parentSpan ? { parent: parentSpan } : {}),
|
|
133
143
|
attributes,
|
|
134
144
|
}), NO_TRACER.start(useCase, {}));
|
|
135
145
|
const execution = {
|
|
136
146
|
id,
|
|
137
147
|
useCase,
|
|
138
148
|
boundedContext: options.boundedContext,
|
|
139
|
-
parent:
|
|
149
|
+
parent: above,
|
|
140
150
|
correlationId,
|
|
141
151
|
causationId,
|
|
142
|
-
signal: options.signal ??
|
|
152
|
+
signal: options.signal ?? above?.signal,
|
|
143
153
|
span,
|
|
144
154
|
context,
|
|
145
155
|
};
|
|
146
|
-
const reported =
|
|
147
|
-
? (REPORTED_BY_EXECUTION.get(
|
|
156
|
+
const reported = above
|
|
157
|
+
? (REPORTED_BY_EXECUTION.get(above) ?? new Set())
|
|
148
158
|
: new Set();
|
|
149
159
|
REPORTED_BY_EXECUTION.set(execution, reported);
|
|
150
|
-
logExecution(
|
|
151
|
-
observe(() => options.announce.started({ executable: command, execution, layer
|
|
160
|
+
logExecution(layer, execution);
|
|
161
|
+
observe(() => options.announce.started({ executable: command, execution, layer }), undefined);
|
|
152
162
|
const finish = (error) => {
|
|
153
163
|
const outcome = error
|
|
154
|
-
? observe(() =>
|
|
164
|
+
? observe(() => options.expected(error.value)
|
|
165
|
+
? "refused"
|
|
166
|
+
: (failedAt(error.value)?.outcome ?? ports.outcomeOf(error.value)), "failed")
|
|
155
167
|
: "ok";
|
|
156
168
|
observe(() => span.end(outcome, error?.value), undefined);
|
|
157
|
-
logExecution(
|
|
169
|
+
logExecution(layer, execution, {
|
|
158
170
|
outcome,
|
|
159
171
|
durationMs: Math.max(0, Date.now() - startedAt),
|
|
160
172
|
});
|
|
@@ -175,13 +187,14 @@ export async function executeService(target, command, options) {
|
|
|
175
187
|
FAILED_AT.set(error.value, {
|
|
176
188
|
execution,
|
|
177
189
|
executable: command,
|
|
178
|
-
layer
|
|
190
|
+
layer,
|
|
179
191
|
outcome,
|
|
180
192
|
});
|
|
181
193
|
return outcome;
|
|
182
194
|
};
|
|
183
195
|
const view = serviceView(target, options, execution);
|
|
184
|
-
const
|
|
196
|
+
const execute = Reflect.get(target, "execute");
|
|
197
|
+
const service = (current) => Promise.resolve(Reflect.apply(execute, view, [current]));
|
|
185
198
|
let answer;
|
|
186
199
|
try {
|
|
187
200
|
const signal = execution.signal;
|
|
@@ -190,9 +203,9 @@ export async function executeService(target, command, options) {
|
|
|
190
203
|
const running = runAround(options.interceptors, service, command, {
|
|
191
204
|
boundedContext: options.boundedContext,
|
|
192
205
|
useCase,
|
|
193
|
-
layer
|
|
206
|
+
layer,
|
|
194
207
|
execution,
|
|
195
|
-
logger: executionLogger(
|
|
208
|
+
logger: executionLogger(layer, execution),
|
|
196
209
|
});
|
|
197
210
|
answer = signal === undefined ? await running : await untilAborted(running, signal);
|
|
198
211
|
}
|
|
@@ -201,14 +214,14 @@ export async function executeService(target, command, options) {
|
|
|
201
214
|
if (options.handlers.length === 0)
|
|
202
215
|
throw error;
|
|
203
216
|
const recovered = await handled(options.handlers, error);
|
|
204
|
-
logHandled(
|
|
217
|
+
logHandled(layer, { execution });
|
|
205
218
|
return recovered;
|
|
206
219
|
}
|
|
207
220
|
finish();
|
|
208
221
|
observe(() => options.announce.completed({
|
|
209
222
|
executable: command,
|
|
210
223
|
execution,
|
|
211
|
-
layer
|
|
224
|
+
layer,
|
|
212
225
|
answer,
|
|
213
226
|
durationMs: Math.max(0, Date.now() - startedAt),
|
|
214
227
|
}), undefined);
|
|
@@ -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.
|
|
@@ -41,7 +41,7 @@ export type StoredQueue = EventQueue & {
|
|
|
41
41
|
* each platform plugs.
|
|
42
42
|
*
|
|
43
43
|
* const queue = storedQueue({ store: sqliteEventStore(db), subscriber, ready: online });
|
|
44
|
-
*
|
|
44
|
+
* shopBoundedContext.addDependency("publisher", new Publisher(queue));
|
|
45
45
|
* connectivity.onBack(() => queue.drain());
|
|
46
46
|
*
|
|
47
47
|
* Context: what a phone did by hand in its event bus, the same on every platform. `publish`
|
|
@@ -60,8 +60,9 @@ export type StoredQueue = EventQueue & {
|
|
|
60
60
|
* refused it for good or ran out of attempts; `onSettled` hears either.
|
|
61
61
|
*
|
|
62
62
|
* Delivery is at least once: a process that stops after a context executed an event and before
|
|
63
|
-
* the store saved it executes it there again on the next drain.
|
|
64
|
-
*
|
|
65
|
-
*
|
|
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 the use case of the event — or
|
|
65
|
+
* by the entity the event names.
|
|
66
|
+
* And one process drains a store: two tabs over one IndexedDB claim it first (the Web Locks API).
|
|
66
67
|
*/
|
|
67
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 {
|
|
@@ -22,7 +17,7 @@ function described(boundedContext, error) {
|
|
|
22
17
|
* each platform plugs.
|
|
23
18
|
*
|
|
24
19
|
* const queue = storedQueue({ store: sqliteEventStore(db), subscriber, ready: online });
|
|
25
|
-
*
|
|
20
|
+
* shopBoundedContext.addDependency("publisher", new Publisher(queue));
|
|
26
21
|
* connectivity.onBack(() => queue.drain());
|
|
27
22
|
*
|
|
28
23
|
* Context: what a phone did by hand in its event bus, the same on every platform. `publish`
|
|
@@ -41,9 +36,10 @@ function described(boundedContext, error) {
|
|
|
41
36
|
* refused it for good or ran out of attempts; `onSettled` hears either.
|
|
42
37
|
*
|
|
43
38
|
* Delivery is at least once: a process that stops after a context executed an event and before
|
|
44
|
-
* the store saved it executes it there again on the next drain.
|
|
45
|
-
*
|
|
46
|
-
*
|
|
39
|
+
* the store saved it executes it there again on the next drain. What must not act twice keys what
|
|
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.
|
|
42
|
+
* And one process drains a store: two tabs over one IndexedDB claim it first (the Web Locks API).
|
|
47
43
|
*/
|
|
48
44
|
export function storedQueue(options) {
|
|
49
45
|
const { store, subscriber } = options;
|
|
@@ -52,7 +48,7 @@ export function storedQueue(options) {
|
|
|
52
48
|
const maxAttempts = options.maxAttempts ?? 10;
|
|
53
49
|
if (!(maxAttempts >= 1))
|
|
54
50
|
throw new TypeError("A stored queue needs one attempt at least");
|
|
55
|
-
const rebuild = options.rebuild ??
|
|
51
|
+
const rebuild = options.rebuild ?? eventFrom;
|
|
56
52
|
let draining;
|
|
57
53
|
let again = false;
|
|
58
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
|
}
|