@sincpro/client-framework 1.6.1 → 1.8.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 +13 -6
- package/README.md +53 -25
- package/dist/event_driven/adapters/memory-message-store.d.ts +10 -0
- package/dist/event_driven/adapters/{memory-event-store.js → memory-message-store.js} +3 -3
- package/dist/event_driven/adapters/stored-queue.d.ts +51 -42
- package/dist/event_driven/adapters/stored-queue.js +137 -89
- package/dist/event_driven/adapters/sync-queue.d.ts +4 -3
- package/dist/event_driven/adapters/sync-queue.js +11 -5
- package/dist/event_driven/domain/queue.d.ts +19 -10
- package/dist/event_driven/domain/store.d.ts +26 -21
- package/dist/event_driven/domain/verdict.d.ts +86 -0
- package/dist/event_driven/domain/verdict.js +80 -0
- package/dist/event_driven/entrypoint/publisher.d.ts +14 -8
- package/dist/event_driven/entrypoint/publisher.js +16 -5
- package/dist/event_driven/entrypoint/subscriber.d.ts +9 -1
- package/dist/event_driven/entrypoint/subscriber.js +16 -5
- package/dist/event_driven/index.d.ts +13 -12
- package/dist/event_driven/index.js +11 -10
- package/dist/event_driven/services/messages.d.ts +7 -0
- package/dist/event_driven/services/messages.js +22 -0
- package/dist/observability/adapters/console.d.ts +4 -6
- package/dist/observability/adapters/console.js +8 -9
- package/dist/observability/domain/logging.d.ts +27 -0
- package/dist/observability/domain/logging.js +21 -1
- package/dist/observability/index.d.ts +2 -2
- package/dist/observability/index.js +2 -1
- package/dist/observability/services/logging.d.ts +8 -1
- package/dist/observability/services/logging.js +16 -0
- package/package.json +1 -1
- package/dist/event_driven/adapters/memory-event-store.d.ts +0 -10
|
@@ -1,11 +1,12 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { MessageQueue, Subscribes } from "../domain/queue.js";
|
|
2
2
|
/**
|
|
3
3
|
* The queue that delivers in the call, in memory — the backend's `SyncQueue`: `publish` returns
|
|
4
|
-
* once every bounded context that hears the event executed it
|
|
4
|
+
* once every bounded context that hears the event executed it, `send` once its bus ran the
|
|
5
|
+
* Command. What a web application starts
|
|
5
6
|
* with, and what a test proves a flow with.
|
|
6
7
|
*
|
|
7
8
|
* Context: nothing is stored, so nothing outlives the process — a phone's queue is durable and
|
|
8
9
|
* the platform's. Every subscriber runs; then the first failure, if any, reaches the publisher,
|
|
9
10
|
* as an in-process call would.
|
|
10
11
|
*/
|
|
11
|
-
export declare function syncQueue(subscriber: Subscribes):
|
|
12
|
+
export declare function syncQueue(subscriber: Subscribes): MessageQueue;
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* The queue that delivers in the call, in memory — the backend's `SyncQueue`: `publish` returns
|
|
3
|
-
* once every bounded context that hears the event executed it
|
|
3
|
+
* once every bounded context that hears the event executed it, `send` once its bus ran the
|
|
4
|
+
* Command. What a web application starts
|
|
4
5
|
* with, and what a test proves a flow with.
|
|
5
6
|
*
|
|
6
7
|
* Context: nothing is stored, so nothing outlives the process — a phone's queue is durable and
|
|
@@ -8,12 +9,17 @@
|
|
|
8
9
|
* as an in-process call would.
|
|
9
10
|
*/
|
|
10
11
|
export function syncQueue(subscriber) {
|
|
12
|
+
const raise = (handled) => {
|
|
13
|
+
const failed = handled.find((one) => "error" in one);
|
|
14
|
+
if (failed && "error" in failed)
|
|
15
|
+
throw failed.error;
|
|
16
|
+
};
|
|
11
17
|
return {
|
|
12
18
|
async put(event, envelope) {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
19
|
+
raise(await subscriber.handle(event, envelope));
|
|
20
|
+
},
|
|
21
|
+
async send(command, envelope) {
|
|
22
|
+
raise(await subscriber.execute(command, envelope));
|
|
17
23
|
},
|
|
18
24
|
};
|
|
19
25
|
}
|
|
@@ -1,27 +1,29 @@
|
|
|
1
|
-
import type { AnyCommand, CommandClass, CommandDescriptor, DomainEvent, EventClass, EventDescriptor, RemoteEvent } from "../../core/domain/dto.js";
|
|
1
|
+
import type { AnyCommand, CommandClass, CommandDescriptor, DomainEvent, EventClass, EventDescriptor, RemoteCommand, RemoteEvent } from "../../core/domain/dto.js";
|
|
2
2
|
import type { Envelope } from "./envelope.js";
|
|
3
3
|
/**
|
|
4
|
-
* What carries a
|
|
5
|
-
*
|
|
4
|
+
* What carries a message to whoever consumes it — the port each platform implements with its own
|
|
5
|
+
* technology, as the backend's `Queue`:
|
|
6
6
|
*
|
|
7
7
|
* - `syncQueue(subscriber)` — this package's: in the call, in memory (the web's default, tests).
|
|
8
|
-
* -
|
|
9
|
-
*
|
|
10
|
-
* - a browser: IndexedDB to work offline, `BroadcastChannel` to tell the other tabs.
|
|
8
|
+
* - `storedQueue(...)` — stored first, then delivered with the rules of a `Verdict`: a phone's
|
|
9
|
+
* SQLite, a browser's IndexedDB.
|
|
11
10
|
* - a server's broker, through the backend.
|
|
12
11
|
*
|
|
13
12
|
* Context: delivery is the platform's — when, how often, how durably —; what it delivers and to
|
|
14
|
-
* whom is the same everywhere: an event
|
|
15
|
-
*
|
|
13
|
+
* whom is the same everywhere: an event for every bounded context that hears it, a Command for
|
|
14
|
+
* the one bus that consumes it, each with its envelope.
|
|
16
15
|
*/
|
|
17
|
-
export interface
|
|
16
|
+
export interface MessageQueue {
|
|
17
|
+
/** An event, for every bounded context that hears it. */
|
|
18
18
|
put(event: DomainEvent | RemoteEvent, envelope: Envelope): void | Promise<void>;
|
|
19
|
+
/** A Command, for the one bus that consumes it: an intention, answered when it runs. */
|
|
20
|
+
send(command: AnyCommand | RemoteCommand, envelope: Envelope): void | Promise<void>;
|
|
19
21
|
}
|
|
20
22
|
/** What an event of a wire name is rebuilt as: its class, or the generated entry it was registered with. */
|
|
21
23
|
export type EventType = EventClass<DomainEvent> | EventDescriptor;
|
|
22
24
|
/** What a Command of an identity is rebuilt as: its class, or the generated entry it was registered with. */
|
|
23
25
|
export type CommandType = CommandClass<AnyCommand> | CommandDescriptor;
|
|
24
|
-
/** What one bounded context answered, or failed with, for a delivered
|
|
26
|
+
/** What one bounded context answered, or failed with, for a delivered message. */
|
|
25
27
|
export type Handled = {
|
|
26
28
|
readonly boundedContext: string;
|
|
27
29
|
readonly answer: unknown;
|
|
@@ -41,4 +43,11 @@ export interface Subscribes {
|
|
|
41
43
|
* already have it — and what each answered.
|
|
42
44
|
*/
|
|
43
45
|
handle(event: DomainEvent | RemoteEvent, envelope?: Envelope, skip?: readonly string[]): Promise<Handled[]>;
|
|
46
|
+
/** What a consumed Command of that identity is rebuilt as, or `undefined` when none is. */
|
|
47
|
+
commandTypeOf(identity: string): CommandType | undefined;
|
|
48
|
+
/**
|
|
49
|
+
* The Command executed by the one bus that consumes it — nothing when that bus is in `skip` —,
|
|
50
|
+
* and what it answered or failed with.
|
|
51
|
+
*/
|
|
52
|
+
execute(command: AnyCommand | RemoteCommand, envelope?: Envelope, skip?: readonly string[]): Promise<Handled[]>;
|
|
44
53
|
}
|
|
@@ -2,11 +2,11 @@ import type { JsonValue } from "../../common/serialization.js";
|
|
|
2
2
|
import type { Envelope } from "./envelope.js";
|
|
3
3
|
import type { FailureKind } from "@sincpro/criteria";
|
|
4
4
|
/**
|
|
5
|
-
* Where a stored queue keeps what it was handed
|
|
6
|
-
*
|
|
5
|
+
* Where a stored queue keeps what it was handed — events and Commands — until it is done with
|
|
6
|
+
* them: the port each platform implements with its own technology:
|
|
7
7
|
*
|
|
8
|
-
* - `
|
|
9
|
-
* - a phone: a SQLite table, so
|
|
8
|
+
* - `memoryMessageStore()` — this package's: the process's memory (a browser tab, a test).
|
|
9
|
+
* - a phone: a SQLite table, so a message outlives a restart; the set-aside ones are its
|
|
10
10
|
* dead-letter queue.
|
|
11
11
|
* - a browser: IndexedDB, to work offline.
|
|
12
12
|
*
|
|
@@ -14,25 +14,26 @@ import type { FailureKind } from "@sincpro/criteria";
|
|
|
14
14
|
* try again, what to set aside, which contexts already have it (`storedQueue`). So the port only
|
|
15
15
|
* keeps records, in the order they came, and finds the next one waiting.
|
|
16
16
|
*/
|
|
17
|
-
export interface
|
|
18
|
-
/** Keeps a
|
|
19
|
-
add(stored:
|
|
17
|
+
export interface MessageStore {
|
|
18
|
+
/** Keeps a message, waiting. */
|
|
19
|
+
add(stored: StoredMessage): void | Promise<void>;
|
|
20
20
|
/**
|
|
21
|
-
* The oldest
|
|
22
|
-
*
|
|
21
|
+
* The oldest message still waiting (`pending`), in the order added — but those `skip` ids:
|
|
22
|
+
* not ready, or waiting their time, they wait while the rest go on.
|
|
23
23
|
*/
|
|
24
|
-
next(skip: ReadonlySet<string>):
|
|
25
|
-
/** What changed of a stored
|
|
26
|
-
save(stored:
|
|
24
|
+
next(skip: ReadonlySet<string>): StoredMessage | undefined | Promise<StoredMessage | undefined>;
|
|
25
|
+
/** What changed of a stored message — found by its envelope's id. */
|
|
26
|
+
save(stored: StoredMessage): void | Promise<void>;
|
|
27
27
|
}
|
|
28
28
|
/**
|
|
29
|
-
* - `pending`: waiting to be delivered —
|
|
30
|
-
*
|
|
31
|
-
* - `
|
|
32
|
-
*
|
|
33
|
-
* dead-letter queue; it never holds back what comes after.
|
|
29
|
+
* - `pending`: waiting to be delivered — its first time, or again after `retryAt`.
|
|
30
|
+
* - `delivered`: every bounded context that hears the event executed it; the Command answered.
|
|
31
|
+
* - `set_aside`: it failed and is not tried again on its own — the dead-letter queue, for a
|
|
32
|
+
* person to retry, or to drop.
|
|
34
33
|
*/
|
|
35
34
|
export type StoredState = "pending" | "delivered" | "set_aside";
|
|
35
|
+
/** What a message is: a fact for whoever hears it, or an intention for the bus that consumes it. */
|
|
36
|
+
export type MessageKind = "event" | "command";
|
|
36
37
|
/** Why a bounded context did not execute it. */
|
|
37
38
|
export type DeliveryFailure = {
|
|
38
39
|
readonly boundedContext: string;
|
|
@@ -40,17 +41,21 @@ export type DeliveryFailure = {
|
|
|
40
41
|
readonly reason: string;
|
|
41
42
|
readonly message: string;
|
|
42
43
|
};
|
|
43
|
-
/** A
|
|
44
|
-
export type
|
|
44
|
+
/** A message as a store keeps it: its envelope, its values and how its delivery goes. */
|
|
45
|
+
export type StoredMessage = {
|
|
46
|
+
readonly kind: MessageKind;
|
|
45
47
|
readonly envelope: Envelope;
|
|
46
|
-
/**
|
|
48
|
+
/** Its values (`valuesOf`): rebuilt as its class on delivery. */
|
|
47
49
|
readonly payload: JsonValue;
|
|
48
50
|
readonly state: StoredState;
|
|
51
|
+
/** Every time it was handed to its consumers. */
|
|
49
52
|
readonly attempts: number;
|
|
50
|
-
/** When it may be tried again, ISO; `null` when it need not wait. */
|
|
53
|
+
/** When it may be tried again, ISO; `null` when it need not wait a time. */
|
|
51
54
|
readonly retryAt: string | null;
|
|
52
55
|
/** The bounded contexts done with it — executed, or set aside —: a retry never runs them again. */
|
|
53
56
|
readonly reached: readonly string[];
|
|
54
57
|
/** Why the last attempt did not reach every context. */
|
|
55
58
|
readonly failures: readonly DeliveryFailure[];
|
|
59
|
+
/** What a Command answered (`valuesOf`), once delivered; `null` for an event. */
|
|
60
|
+
readonly answer: JsonValue | null;
|
|
56
61
|
};
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import type { StoredMessage } from "./store.js";
|
|
2
|
+
import type { FailureKind } from "@sincpro/criteria";
|
|
3
|
+
/**
|
|
4
|
+
* How one message is delivered — what its class declares, what the application sets for it by
|
|
5
|
+
* name, over the queue's:
|
|
6
|
+
*
|
|
7
|
+
* { retries: 0, maxAttempts: Infinity, requiresNetwork: false } // the defaults
|
|
8
|
+
*/
|
|
9
|
+
export type DeliveryPolicy = {
|
|
10
|
+
/**
|
|
11
|
+
* How many times it is sent again after a failure (0): sent once, and a failure is set aside —
|
|
12
|
+
* the dead-letter queue — for a person to retry.
|
|
13
|
+
*/
|
|
14
|
+
readonly retries?: number;
|
|
15
|
+
/**
|
|
16
|
+
* Every send, waits included, before it is set aside although it could still wait (Infinity):
|
|
17
|
+
* what bounds a message waiting for a server that never comes back.
|
|
18
|
+
*/
|
|
19
|
+
readonly maxAttempts?: number;
|
|
20
|
+
/**
|
|
21
|
+
* Its work goes over the network: a failure that says the other side is not there now
|
|
22
|
+
* (`WAITING_KINDS`) waits instead of failing, as long as it takes; a platform's `ready` holds it
|
|
23
|
+
* while there is no connection.
|
|
24
|
+
*/
|
|
25
|
+
readonly requiresNetwork?: boolean;
|
|
26
|
+
};
|
|
27
|
+
/** A policy with every rule said. */
|
|
28
|
+
export type FullDeliveryPolicy = Required<DeliveryPolicy>;
|
|
29
|
+
/** The queue's policy when nothing says otherwise. */
|
|
30
|
+
export declare const DEFAULT_DELIVERY_POLICY: FullDeliveryPolicy;
|
|
31
|
+
/**
|
|
32
|
+
* The kinds that mean the other side is not there now — down, an open circuit, out of quota, or
|
|
33
|
+
* the call went out and no answer came back —: a message whose work goes over the network waits
|
|
34
|
+
* them out instead of failing. Sending it again is safe because it carries its key (its id),
|
|
35
|
+
* which the backend runs once (`Idempotency-Key`); a server without keys is verified by the use
|
|
36
|
+
* case before it writes again.
|
|
37
|
+
*/
|
|
38
|
+
export declare const WAITING_KINDS: ReadonlySet<FailureKind>;
|
|
39
|
+
/** One delivery, as the verdict reads it. */
|
|
40
|
+
export type Delivery = {
|
|
41
|
+
readonly stored: StoredMessage;
|
|
42
|
+
/** Sends so far, this one included. */
|
|
43
|
+
readonly attempts: number;
|
|
44
|
+
/** What each consumer that failed this time raised. */
|
|
45
|
+
readonly errors: readonly unknown[];
|
|
46
|
+
readonly policy: FullDeliveryPolicy;
|
|
47
|
+
/** The wait before the next send, in ms, as the queue's backoff counts it. */
|
|
48
|
+
readonly backoff: number;
|
|
49
|
+
};
|
|
50
|
+
/**
|
|
51
|
+
* - `delivered`: every consumer has it.
|
|
52
|
+
* - `wait`: not a failure to judge — it waits, `after` ms or, without it, for `ready`.
|
|
53
|
+
* - `retry`: it failed, and is sent again `after` ms.
|
|
54
|
+
* - `set_aside`: it failed, and waits for a person.
|
|
55
|
+
*/
|
|
56
|
+
export type Verdict = {
|
|
57
|
+
readonly settlement: "delivered";
|
|
58
|
+
} | {
|
|
59
|
+
readonly settlement: "wait";
|
|
60
|
+
readonly after?: number;
|
|
61
|
+
} | {
|
|
62
|
+
readonly settlement: "retry";
|
|
63
|
+
readonly after: number;
|
|
64
|
+
} | {
|
|
65
|
+
readonly settlement: "set_aside";
|
|
66
|
+
};
|
|
67
|
+
/**
|
|
68
|
+
* The verdict on one delivery — the queue column of sincpro_framework's failure table
|
|
69
|
+
* (`entrypoints/adapters/queue/verdicts.py`), for a client:
|
|
70
|
+
*
|
|
71
|
+
* 1. Nothing failed: delivered.
|
|
72
|
+
* 2. It used its `maxAttempts`: set aside.
|
|
73
|
+
* 3. A failure every attempt would repeat (`PERMANENT_KINDS`): set aside.
|
|
74
|
+
* 4. Its work goes over the network and the other side is not there now (`WAITING_KINDS`): it
|
|
75
|
+
* waits — what the failure asks for (`retryAfter`, an open circuit's time left) or the backoff.
|
|
76
|
+
* 5. Any other failure — a bug, a device that did not answer, a conflict —: sent again while it
|
|
77
|
+
* has `retries`, after the backoff; else set aside.
|
|
78
|
+
* Final: several consumers failed — the one that asks the most: wait, then retry, then set aside.
|
|
79
|
+
*
|
|
80
|
+
* Context: pure, and the queue's default — a platform extends it by wrapping it
|
|
81
|
+
* (`storedQueue({ verdict: (delivery) => mine(delivery) ?? verdictOf(delivery) })`), never by
|
|
82
|
+
* changing the queue.
|
|
83
|
+
*/
|
|
84
|
+
export declare function verdictOf(delivery: Delivery): Verdict;
|
|
85
|
+
/** The one verdict of several consumers: the one that asks the most, its longest wait. */
|
|
86
|
+
export declare function combined(verdicts: readonly Verdict[]): Verdict;
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { failureOf } from "../../common/failures.js";
|
|
2
|
+
/** The queue's policy when nothing says otherwise. */
|
|
3
|
+
export const DEFAULT_DELIVERY_POLICY = {
|
|
4
|
+
retries: 0,
|
|
5
|
+
maxAttempts: Number.POSITIVE_INFINITY,
|
|
6
|
+
requiresNetwork: false,
|
|
7
|
+
};
|
|
8
|
+
/**
|
|
9
|
+
* The kinds that mean the other side is not there now — down, an open circuit, out of quota, or
|
|
10
|
+
* the call went out and no answer came back —: a message whose work goes over the network waits
|
|
11
|
+
* them out instead of failing. Sending it again is safe because it carries its key (its id),
|
|
12
|
+
* which the backend runs once (`Idempotency-Key`); a server without keys is verified by the use
|
|
13
|
+
* case before it writes again.
|
|
14
|
+
*/
|
|
15
|
+
export const WAITING_KINDS = new Set([
|
|
16
|
+
"unavailable",
|
|
17
|
+
"exhausted",
|
|
18
|
+
"in_progress",
|
|
19
|
+
"unknown_outcome",
|
|
20
|
+
]);
|
|
21
|
+
/**
|
|
22
|
+
* The verdict on one delivery — the queue column of sincpro_framework's failure table
|
|
23
|
+
* (`entrypoints/adapters/queue/verdicts.py`), for a client:
|
|
24
|
+
*
|
|
25
|
+
* 1. Nothing failed: delivered.
|
|
26
|
+
* 2. It used its `maxAttempts`: set aside.
|
|
27
|
+
* 3. A failure every attempt would repeat (`PERMANENT_KINDS`): set aside.
|
|
28
|
+
* 4. Its work goes over the network and the other side is not there now (`WAITING_KINDS`): it
|
|
29
|
+
* waits — what the failure asks for (`retryAfter`, an open circuit's time left) or the backoff.
|
|
30
|
+
* 5. Any other failure — a bug, a device that did not answer, a conflict —: sent again while it
|
|
31
|
+
* has `retries`, after the backoff; else set aside.
|
|
32
|
+
* Final: several consumers failed — the one that asks the most: wait, then retry, then set aside.
|
|
33
|
+
*
|
|
34
|
+
* Context: pure, and the queue's default — a platform extends it by wrapping it
|
|
35
|
+
* (`storedQueue({ verdict: (delivery) => mine(delivery) ?? verdictOf(delivery) })`), never by
|
|
36
|
+
* changing the queue.
|
|
37
|
+
*/
|
|
38
|
+
export function verdictOf(delivery) {
|
|
39
|
+
const { errors, attempts, policy, backoff } = delivery;
|
|
40
|
+
if (errors.length === 0)
|
|
41
|
+
return { settlement: "delivered" };
|
|
42
|
+
if (attempts >= policy.maxAttempts)
|
|
43
|
+
return { settlement: "set_aside" };
|
|
44
|
+
const verdicts = errors.map((error) => {
|
|
45
|
+
const failure = failureOf(error);
|
|
46
|
+
if (failure.permanent)
|
|
47
|
+
return { settlement: "set_aside" };
|
|
48
|
+
const asked = (failure.retryAfter ?? 0) * 1000;
|
|
49
|
+
if (policy.requiresNetwork && WAITING_KINDS.has(failure.kind)) {
|
|
50
|
+
return { settlement: "wait", after: Math.max(backoff, asked) };
|
|
51
|
+
}
|
|
52
|
+
if (attempts <= policy.retries) {
|
|
53
|
+
return { settlement: "retry", after: Math.max(backoff, asked) };
|
|
54
|
+
}
|
|
55
|
+
return { settlement: "set_aside" };
|
|
56
|
+
});
|
|
57
|
+
return combined(verdicts);
|
|
58
|
+
}
|
|
59
|
+
const WORST_FIRST = [
|
|
60
|
+
"wait",
|
|
61
|
+
"retry",
|
|
62
|
+
"set_aside",
|
|
63
|
+
"delivered",
|
|
64
|
+
];
|
|
65
|
+
/** The one verdict of several consumers: the one that asks the most, its longest wait. */
|
|
66
|
+
export function combined(verdicts) {
|
|
67
|
+
for (const settlement of WORST_FIRST) {
|
|
68
|
+
const same = verdicts.filter((one) => one.settlement === settlement);
|
|
69
|
+
if (same.length === 0)
|
|
70
|
+
continue;
|
|
71
|
+
const afters = same.flatMap((one) => "after" in one && one.after !== undefined ? [one.after] : []);
|
|
72
|
+
const after = afters.length > 0 ? Math.max(...afters) : undefined;
|
|
73
|
+
if (settlement === "wait")
|
|
74
|
+
return after === undefined ? { settlement } : { settlement, after };
|
|
75
|
+
if (settlement === "retry")
|
|
76
|
+
return { settlement, after: after ?? 0 };
|
|
77
|
+
return { settlement };
|
|
78
|
+
}
|
|
79
|
+
return { settlement: "delivered" };
|
|
80
|
+
}
|
|
@@ -1,15 +1,15 @@
|
|
|
1
1
|
import { BIND, type Execution } from "../../context/domain/execution.js";
|
|
2
|
-
import type { DomainEvent, RemoteEvent } from "../../core/domain/dto.js";
|
|
2
|
+
import type { AnyCommand, DomainEvent, RemoteCommand, 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
|
-
import type {
|
|
5
|
+
import type { MessageQueue } from "../domain/queue.js";
|
|
6
6
|
/**
|
|
7
|
-
* Puts
|
|
8
|
-
* `Publisher`:
|
|
7
|
+
* Puts messages into a queue — what a Feature or a screen holds to tell whoever listens, or to
|
|
8
|
+
* ask for something that runs when it can, as the backend's `Publisher`:
|
|
9
9
|
*
|
|
10
10
|
* shopBoundedContext.addDependency("publisher", new Publisher(queue));
|
|
11
|
-
* //
|
|
12
|
-
* await this.publisher.
|
|
11
|
+
* await this.publisher.publish(new OrderPaid(order.id)); // a fact: every hearer
|
|
12
|
+
* await this.publisher.send(new CommandSendMessage(chat, text)); // an intention: its bus
|
|
13
13
|
*
|
|
14
14
|
* Context: a dependency of a bounded context is bound to each run (`BIND`), so what a Feature
|
|
15
15
|
* publishes joins that run's flow — the same `correlationId`, caused by the run — with nothing
|
|
@@ -18,9 +18,15 @@ import type { EventQueue } from "../domain/queue.js";
|
|
|
18
18
|
export declare class Publisher {
|
|
19
19
|
private readonly queue;
|
|
20
20
|
private readonly cause?;
|
|
21
|
-
constructor(queue:
|
|
21
|
+
constructor(queue: MessageQueue, cause?: Cause | undefined);
|
|
22
22
|
/** `event` into the queue, with its envelope; answers the envelope once the queue took it. */
|
|
23
23
|
publish(event: DomainEvent | RemoteEvent): Promise<Envelope>;
|
|
24
|
-
/**
|
|
24
|
+
/**
|
|
25
|
+
* `command` into the queue, with its envelope: an intention, stored and run by the bus that
|
|
26
|
+
* consumes it when it can — its answer arrives later, kept with it. Answers the envelope once
|
|
27
|
+
* the queue took it: its id is the Command's key.
|
|
28
|
+
*/
|
|
29
|
+
send(command: AnyCommand | RemoteCommand): Promise<Envelope>;
|
|
30
|
+
/** The same publisher, publishing as `execution`: what it publishes and sends joins that run's flow. */
|
|
25
31
|
[BIND](execution: Execution): Publisher;
|
|
26
32
|
}
|
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
import { BIND } from "../../context/domain/execution.js";
|
|
2
2
|
import { envelopeOf, } from "../domain/envelope.js";
|
|
3
|
+
import { commandEnvelopeOf } from "../services/messages.js";
|
|
3
4
|
import { traceparentOf } from "../../observability/services/trace-context.js";
|
|
4
5
|
/**
|
|
5
|
-
* Puts
|
|
6
|
-
* `Publisher`:
|
|
6
|
+
* Puts messages into a queue — what a Feature or a screen holds to tell whoever listens, or to
|
|
7
|
+
* ask for something that runs when it can, as the backend's `Publisher`:
|
|
7
8
|
*
|
|
8
9
|
* shopBoundedContext.addDependency("publisher", new Publisher(queue));
|
|
9
|
-
* //
|
|
10
|
-
* await this.publisher.
|
|
10
|
+
* await this.publisher.publish(new OrderPaid(order.id)); // a fact: every hearer
|
|
11
|
+
* await this.publisher.send(new CommandSendMessage(chat, text)); // an intention: its bus
|
|
11
12
|
*
|
|
12
13
|
* Context: a dependency of a bounded context is bound to each run (`BIND`), so what a Feature
|
|
13
14
|
* publishes joins that run's flow — the same `correlationId`, caused by the run — with nothing
|
|
@@ -26,7 +27,17 @@ export class Publisher {
|
|
|
26
27
|
await this.queue.put(event, envelope);
|
|
27
28
|
return envelope;
|
|
28
29
|
}
|
|
29
|
-
/**
|
|
30
|
+
/**
|
|
31
|
+
* `command` into the queue, with its envelope: an intention, stored and run by the bus that
|
|
32
|
+
* consumes it when it can — its answer arrives later, kept with it. Answers the envelope once
|
|
33
|
+
* the queue took it: its id is the Command's key.
|
|
34
|
+
*/
|
|
35
|
+
async send(command) {
|
|
36
|
+
const envelope = commandEnvelopeOf(command, this.cause);
|
|
37
|
+
await this.queue.send(command, envelope);
|
|
38
|
+
return envelope;
|
|
39
|
+
}
|
|
40
|
+
/** The same publisher, publishing as `execution`: what it publishes and sends joins that run's flow. */
|
|
30
41
|
[BIND](execution) {
|
|
31
42
|
const traceparent = traceparentOf(execution.span);
|
|
32
43
|
return new Publisher(this.queue, {
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { EventConsumer, EventKey } from "../../core/domain/consumers.js";
|
|
2
|
-
import type { AnyCommand, CommandClass, CommandDescriptor, DomainEvent, DtoKey, EventClass, EventDescriptor, Executable, ExecutableOf, RemoteEvent } from "../../core/domain/dto.js";
|
|
2
|
+
import type { AnyCommand, CommandClass, CommandDescriptor, DomainEvent, DtoKey, EventClass, EventDescriptor, Executable, ExecutableOf, RemoteCommand, RemoteEvent } from "../../core/domain/dto.js";
|
|
3
3
|
import type { Cause, ExecuteOptions } from "../../core/domain/pipeline.js";
|
|
4
4
|
import { type HandledEvent } from "../../core/domain/services.js";
|
|
5
5
|
import { type HandlerRegistrar } from "../../core/services/event-handlers.js";
|
|
@@ -128,6 +128,14 @@ export declare class Subscriber<D extends object = Record<never, never>> impleme
|
|
|
128
128
|
* Final: what each answered, or failed with; nothing when nobody here knows it.
|
|
129
129
|
*/
|
|
130
130
|
receive(arrived: Message | BinaryMessage, skip?: readonly string[]): Promise<Handled[]>;
|
|
131
|
+
/**
|
|
132
|
+
* A Command this subscriber consumes, executed by the one bus that registered it — continuing
|
|
133
|
+
* the envelope's flow and trace when there is one —; nothing when that bus is in `skip`.
|
|
134
|
+
* One it does not consume is `CommandNotConsumed`: a bus never runs a Command it did not ask
|
|
135
|
+
* for.
|
|
136
|
+
* Final: what the bus answered, or failed with.
|
|
137
|
+
*/
|
|
138
|
+
execute(command: AnyCommand | RemoteCommand, envelope?: Envelope, skip?: readonly string[]): Promise<Handled[]>;
|
|
131
139
|
/** Every consumer of `event`, each under a key no other one has. */
|
|
132
140
|
private consumersOf;
|
|
133
141
|
/** One of its own handlers, as a consumer: its run, with this subscriber's dependencies. */
|
|
@@ -4,7 +4,7 @@ import { processHearing } from "../../core/infrastructure/listeners.js";
|
|
|
4
4
|
import { identityOf, keyIdentity } from "../../core/services/dto-keys.js";
|
|
5
5
|
import { eventKeysOf, handlerRegistrar, handles, runHandler, } from "../../core/services/event-handlers.js";
|
|
6
6
|
import { announceTo } from "../../core/services/listening.js";
|
|
7
|
-
import {
|
|
7
|
+
import { commandFrom, envelopeFrom, eventFrom, readMessage, } from "../services/messages.js";
|
|
8
8
|
/** A Command the queue carried that this subscriber was not told to consume. */
|
|
9
9
|
export class CommandNotConsumed extends Error {
|
|
10
10
|
type;
|
|
@@ -238,13 +238,24 @@ export class Subscriber {
|
|
|
238
238
|
throw new CommandNotConsumed(message.type);
|
|
239
239
|
return [];
|
|
240
240
|
}
|
|
241
|
-
|
|
241
|
+
return this.execute(commandFrom(commandType, message.data), envelopeFrom(message), skip);
|
|
242
|
+
}
|
|
243
|
+
/**
|
|
244
|
+
* A Command this subscriber consumes, executed by the one bus that registered it — continuing
|
|
245
|
+
* the envelope's flow and trace when there is one —; nothing when that bus is in `skip`.
|
|
246
|
+
* One it does not consume is `CommandNotConsumed`: a bus never runs a Command it did not ask
|
|
247
|
+
* for.
|
|
248
|
+
* Final: what the bus answered, or failed with.
|
|
249
|
+
*/
|
|
250
|
+
async execute(command, envelope, skip = []) {
|
|
251
|
+
const identity = identityOf(command)?.wire ?? "";
|
|
252
|
+
if (this.commandTypeOf(identity) === undefined)
|
|
253
|
+
throw new CommandNotConsumed(identity);
|
|
254
|
+
const bus = this.buses.find((one) => one.context.commandTypeOf(identity) !== undefined);
|
|
242
255
|
if (bus === undefined || skip.includes(bus.key))
|
|
243
256
|
return [];
|
|
244
257
|
try {
|
|
245
|
-
const answer = await bus.context.execute(
|
|
246
|
-
cause: causeOf(message),
|
|
247
|
-
});
|
|
258
|
+
const answer = await bus.context.execute(command, envelope ? { cause: envelope } : {});
|
|
248
259
|
return [{ boundedContext: bus.key, answer }];
|
|
249
260
|
}
|
|
250
261
|
catch (error) {
|
|
@@ -1,23 +1,24 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Event-driven: the ports and the in-memory pieces that let every platform publish
|
|
3
|
-
* and hand what a queue carries to the bus the same way — as sincpro_framework's
|
|
4
|
-
* Delivering them durably (SQLite on a phone, IndexedDB in a browser, a broker)
|
|
5
|
-
* platform's, behind `
|
|
2
|
+
* Event-driven: the ports and the in-memory pieces that let every platform publish events, send
|
|
3
|
+
* Commands, and hand what a queue carries to the bus the same way — as sincpro_framework's
|
|
4
|
+
* `event_driven`. Delivering them durably (SQLite on a phone, IndexedDB in a browser, a broker)
|
|
5
|
+
* is each platform's, behind `MessageQueue`.
|
|
6
6
|
*
|
|
7
|
-
* domain/ the envelope, the messages (an event, a Command)
|
|
8
|
-
* Subscribes,
|
|
7
|
+
* domain/ the envelope, the messages (an event, a Command), the ports (MessageQueue,
|
|
8
|
+
* Subscribes, MessageStore) and the delivery's rules (DeliveryPolicy, verdictOf)
|
|
9
9
|
* services/ writing and reading messages: a Command by its identity, an event by its name
|
|
10
|
-
* adapters/ syncQueue (in the call), storedQueue (store, then deliver
|
|
11
|
-
*
|
|
10
|
+
* adapters/ syncQueue (in the call), storedQueue (store, then deliver by the verdict),
|
|
11
|
+
* memoryMessageStore
|
|
12
12
|
* entrypoint/ Publisher (what a Feature holds), Subscriber (the queue's only entrypoint)
|
|
13
13
|
*/
|
|
14
|
-
export { type
|
|
14
|
+
export { type MemoryMessageStore, memoryMessageStore, } from "./adapters/memory-message-store.js";
|
|
15
15
|
export { type StoredQueue, storedQueue, type StoredQueueOptions, } from "./adapters/stored-queue.js";
|
|
16
16
|
export { syncQueue } from "./adapters/sync-queue.js";
|
|
17
17
|
export { type Envelope, envelopeOf, } from "./domain/envelope.js";
|
|
18
18
|
export type { BinaryMessage, Message, } from "./domain/messages.js";
|
|
19
|
-
export type { CommandType,
|
|
20
|
-
export type { DeliveryFailure,
|
|
19
|
+
export type { CommandType, EventType, Handled, MessageQueue, Subscribes, } from "./domain/queue.js";
|
|
20
|
+
export type { DeliveryFailure, MessageKind, MessageStore, StoredMessage, StoredState, } from "./domain/store.js";
|
|
21
|
+
export { combined, DEFAULT_DELIVERY_POLICY, type Delivery, type DeliveryPolicy, type FullDeliveryPolicy, type Verdict, verdictOf, WAITING_KINDS, } from "./domain/verdict.js";
|
|
21
22
|
export { Publisher } from "./entrypoint/publisher.js";
|
|
22
23
|
export { CommandNotConsumed, type HearingContext, type NamedSubscriberOptions, Subscriber, type SubscriberOptions, } from "./entrypoint/subscriber.js";
|
|
23
|
-
export { causeOf, commandFrom, commandMessageOf, envelopeFrom, eventFrom, eventMessageOf, type MessageOptions, readMessage, } from "./services/messages.js";
|
|
24
|
+
export { causeOf, commandEnvelopeOf, commandFrom, commandMessageOf, envelopeFrom, eventFrom, eventMessageOf, type MessageOptions, readMessage, } from "./services/messages.js";
|
|
@@ -1,20 +1,21 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Event-driven: the ports and the in-memory pieces that let every platform publish
|
|
3
|
-
* and hand what a queue carries to the bus the same way — as sincpro_framework's
|
|
4
|
-
* Delivering them durably (SQLite on a phone, IndexedDB in a browser, a broker)
|
|
5
|
-
* platform's, behind `
|
|
2
|
+
* Event-driven: the ports and the in-memory pieces that let every platform publish events, send
|
|
3
|
+
* Commands, and hand what a queue carries to the bus the same way — as sincpro_framework's
|
|
4
|
+
* `event_driven`. Delivering them durably (SQLite on a phone, IndexedDB in a browser, a broker)
|
|
5
|
+
* is each platform's, behind `MessageQueue`.
|
|
6
6
|
*
|
|
7
|
-
* domain/ the envelope, the messages (an event, a Command)
|
|
8
|
-
* Subscribes,
|
|
7
|
+
* domain/ the envelope, the messages (an event, a Command), the ports (MessageQueue,
|
|
8
|
+
* Subscribes, MessageStore) and the delivery's rules (DeliveryPolicy, verdictOf)
|
|
9
9
|
* services/ writing and reading messages: a Command by its identity, an event by its name
|
|
10
|
-
* adapters/ syncQueue (in the call), storedQueue (store, then deliver
|
|
11
|
-
*
|
|
10
|
+
* adapters/ syncQueue (in the call), storedQueue (store, then deliver by the verdict),
|
|
11
|
+
* memoryMessageStore
|
|
12
12
|
* entrypoint/ Publisher (what a Feature holds), Subscriber (the queue's only entrypoint)
|
|
13
13
|
*/
|
|
14
|
-
export {
|
|
14
|
+
export { memoryMessageStore, } from "./adapters/memory-message-store.js";
|
|
15
15
|
export { storedQueue, } from "./adapters/stored-queue.js";
|
|
16
16
|
export { syncQueue } from "./adapters/sync-queue.js";
|
|
17
17
|
export { envelopeOf, } from "./domain/envelope.js";
|
|
18
|
+
export { combined, DEFAULT_DELIVERY_POLICY, verdictOf, WAITING_KINDS, } from "./domain/verdict.js";
|
|
18
19
|
export { Publisher } from "./entrypoint/publisher.js";
|
|
19
20
|
export { CommandNotConsumed, Subscriber, } from "./entrypoint/subscriber.js";
|
|
20
|
-
export { causeOf, commandFrom, commandMessageOf, envelopeFrom, eventFrom, eventMessageOf, readMessage, } from "./services/messages.js";
|
|
21
|
+
export { causeOf, commandEnvelopeOf, commandFrom, commandMessageOf, envelopeFrom, eventFrom, eventMessageOf, readMessage, } from "./services/messages.js";
|
|
@@ -14,6 +14,13 @@ export type MessageOptions = {
|
|
|
14
14
|
* `as_json`, so a body read alone says which event it is.
|
|
15
15
|
*/
|
|
16
16
|
export declare function eventMessageOf(event: DomainEvent | RemoteEvent, envelope: Envelope, options?: MessageOptions): Message;
|
|
17
|
+
/**
|
|
18
|
+
* The envelope of a Command sent through a queue — its identity is its name —, sent by the run
|
|
19
|
+
* `cause` names, or the head of its own flow. Its id is the Command's key: what its use case
|
|
20
|
+
* sends as the `Idempotency-Key` (`this.execution.causationId`), so sending it again never runs
|
|
21
|
+
* it twice on a server that keeps keys.
|
|
22
|
+
*/
|
|
23
|
+
export declare function commandEnvelopeOf(command: AnyCommand | RemoteCommand, cause?: Cause): Envelope;
|
|
17
24
|
/**
|
|
18
25
|
* `command` as it travels — sent by the run `cause` names, or the head of its own flow; its
|
|
19
26
|
* `type` is its identity, which a bounded context gave it when it registered it.
|
|
@@ -32,6 +32,28 @@ export function eventMessageOf(event, envelope, options = {}) {
|
|
|
32
32
|
data: { name: envelope.name, ...dataOf(event) },
|
|
33
33
|
};
|
|
34
34
|
}
|
|
35
|
+
/**
|
|
36
|
+
* The envelope of a Command sent through a queue — its identity is its name —, sent by the run
|
|
37
|
+
* `cause` names, or the head of its own flow. Its id is the Command's key: what its use case
|
|
38
|
+
* sends as the `Idempotency-Key` (`this.execution.causationId`), so sending it again never runs
|
|
39
|
+
* it twice on a server that keeps keys.
|
|
40
|
+
*/
|
|
41
|
+
export function commandEnvelopeOf(command, cause) {
|
|
42
|
+
const known = identityOf(command);
|
|
43
|
+
if (known?.kind !== "command" || known.wire === undefined || known.wire.trim() === "") {
|
|
44
|
+
throw new TypeError(`${known?.type?.name ?? "This Command"} has no identity: a bounded context gives it one when it registers it`);
|
|
45
|
+
}
|
|
46
|
+
const id = newEntityId();
|
|
47
|
+
return {
|
|
48
|
+
name: known.wire,
|
|
49
|
+
id,
|
|
50
|
+
correlationId: cause?.correlationId ?? id,
|
|
51
|
+
causationId: cause?.id ?? null,
|
|
52
|
+
publishedAt: new Date().toISOString(),
|
|
53
|
+
...(cause?.traceparent ? { traceparent: cause.traceparent } : {}),
|
|
54
|
+
...(cause?.tracestate ? { tracestate: cause.tracestate } : {}),
|
|
55
|
+
};
|
|
56
|
+
}
|
|
35
57
|
/**
|
|
36
58
|
* `command` as it travels — sent by the run `cause` names, or the head of its own flow; its
|
|
37
59
|
* `type` is its identity, which a bounded context gave it when it registered it.
|
|
@@ -1,8 +1,6 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
level?: LogLevel;
|
|
6
|
-
layers?: Partial<Record<LogLayer, boolean>>;
|
|
1
|
+
import { type LogFilter, type Logger } from "../domain/logging.js";
|
|
2
|
+
/** The console's mode, and which INFO and DEBUG lines it prints (`LogFilter`). */
|
|
3
|
+
export type ConsoleLoggerOptions = LogFilter & {
|
|
4
|
+
readonly mode: "development" | "production";
|
|
7
5
|
};
|
|
8
6
|
export declare function consoleLogger(options: ConsoleLoggerOptions): Logger;
|