@sincpro/client-framework 1.6.0 → 1.7.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 +21 -6
- package/README.md +46 -28
- package/dist/client/adapters/json-rpc.js +3 -2
- package/dist/client/adapters/memory.js +3 -2
- package/dist/client/adapters/rest-operations.js +3 -2
- package/dist/client/adapters/rest.js +3 -2
- package/dist/client/index.d.ts +1 -1
- package/dist/client/index.js +1 -1
- package/dist/client/services/guarded.js +4 -3
- package/dist/client/services/propagation.d.ts +21 -9
- package/dist/client/services/propagation.js +60 -3
- package/dist/core/entrypoint/screens.js +2 -1
- package/dist/core/services/binding.d.ts +5 -2
- package/dist/core/services/binding.js +5 -8
- 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 +0 -4
- package/dist/observability/adapters/console.js +12 -4
- package/package.json +1 -1
- package/dist/event_driven/adapters/memory-event-store.d.ts +0 -10
|
@@ -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.
|
|
@@ -5,8 +5,4 @@ export type ConsoleLoggerOptions = {
|
|
|
5
5
|
level?: LogLevel;
|
|
6
6
|
layers?: Partial<Record<LogLayer, boolean>>;
|
|
7
7
|
};
|
|
8
|
-
/**
|
|
9
|
-
* Built-in console engine: silent in production, configurable
|
|
10
|
-
* development layers, and WARN/ERROR preserved. Other sinks are filtered independently.
|
|
11
|
-
*/
|
|
12
8
|
export declare function consoleLogger(options: ConsoleLoggerOptions): Logger;
|
|
@@ -3,6 +3,13 @@ const RANK = { debug: 0, info: 1, warn: 2, error: 3 };
|
|
|
3
3
|
* Built-in console engine: silent in production, configurable
|
|
4
4
|
* development layers, and WARN/ERROR preserved. Other sinks are filtered independently.
|
|
5
5
|
*/
|
|
6
|
+
/** The failure as an Error to print: its type as its name, its message, its stack. */
|
|
7
|
+
function errorOf(stack, failure) {
|
|
8
|
+
const shown = new Error(failure.message ?? failure.type ?? "");
|
|
9
|
+
shown.name = failure.type ?? "Error";
|
|
10
|
+
shown.stack = stack;
|
|
11
|
+
return shown;
|
|
12
|
+
}
|
|
6
13
|
export function consoleLogger(options) {
|
|
7
14
|
const mode = options.mode;
|
|
8
15
|
if (mode !== "development" && mode !== "production") {
|
|
@@ -29,15 +36,16 @@ export function consoleLogger(options) {
|
|
|
29
36
|
: record.level === "error"
|
|
30
37
|
? console.error
|
|
31
38
|
: console.log;
|
|
32
|
-
// An unexpected failure's stack prints as a stack, readable, after its line — not escaped
|
|
33
|
-
// inside the record.
|
|
34
39
|
const stack = record.failure?.stack;
|
|
35
40
|
if (!stack) {
|
|
36
41
|
method.call(console, prefix, record.message, record);
|
|
37
42
|
return;
|
|
38
43
|
}
|
|
39
|
-
|
|
40
|
-
|
|
44
|
+
// An unexpected failure goes as an Error — its name, its message, its stack —: what React
|
|
45
|
+
// Native's LogBox, Expo's terminal and a browser print as a failure, with its code frame.
|
|
46
|
+
// A bare stack string read as one printed `ERROR null null`.
|
|
47
|
+
const { stack: _shownBelow, ...failure } = record.failure ?? {};
|
|
48
|
+
method.call(console, `${prefix} ${record.message}`, { ...record, failure }, errorOf(stack, failure));
|
|
41
49
|
},
|
|
42
50
|
};
|
|
43
51
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sincpro/client-framework",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.7.0",
|
|
4
4
|
"description": "The client side of sincpro_framework, for every platform: Feature and ApplicationService traced and reported by default, applications made of domains, and the typed client generated from a service's contract. No React, no DOM.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
import type { EventStore, StoredEvent } from "../domain/store.js";
|
|
2
|
-
/** A memory store, and what it holds — in the order added — for whoever inspects it. */
|
|
3
|
-
export type MemoryEventStore = EventStore & {
|
|
4
|
-
readonly events: readonly StoredEvent[];
|
|
5
|
-
};
|
|
6
|
-
/**
|
|
7
|
-
* The events of a stored queue in this process's memory: what a browser tab or a test keeps.
|
|
8
|
-
* Gone with the process — a phone's store is a table.
|
|
9
|
-
*/
|
|
10
|
-
export declare function memoryEventStore(): MemoryEventStore;
|