@sincpro/client-framework 1.2.0 → 1.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (55) hide show
  1. package/AGENTS.md +57 -27
  2. package/MANIFESTO.md +5 -4
  3. package/README.md +225 -157
  4. package/dist/client/services/propagation.js +4 -3
  5. package/dist/codegen/services/scaffold.d.ts +12 -10
  6. package/dist/codegen/services/scaffold.js +23 -40
  7. package/dist/core/domain/bus.d.ts +5 -3
  8. package/dist/core/domain/bus.js +8 -1
  9. package/dist/core/domain/consumers.d.ts +19 -0
  10. package/dist/core/domain/consumers.js +1 -0
  11. package/dist/core/domain/dto.d.ts +68 -6
  12. package/dist/core/domain/dto.js +22 -2
  13. package/dist/core/domain/pipeline.d.ts +7 -3
  14. package/dist/core/domain/services.d.ts +97 -14
  15. package/dist/core/domain/services.js +65 -6
  16. package/dist/core/entrypoint/bounded-context.d.ts +86 -35
  17. package/dist/core/entrypoint/bounded-context.js +224 -71
  18. package/dist/core/entrypoint/screens.js +0 -5
  19. package/dist/core/index.d.ts +6 -3
  20. package/dist/core/index.js +4 -3
  21. package/dist/core/infrastructure/listeners.d.ts +5 -4
  22. package/dist/core/infrastructure/listeners.js +1 -1
  23. package/dist/core/services/dto-keys.d.ts +44 -0
  24. package/dist/core/services/dto-keys.js +97 -0
  25. package/dist/core/services/event-handlers.d.ts +57 -0
  26. package/dist/core/services/event-handlers.js +114 -0
  27. package/dist/core/services/execution.d.ts +15 -21
  28. package/dist/core/services/execution.js +38 -32
  29. package/dist/core/services/interceptors.d.ts +2 -2
  30. package/dist/core/services/interceptors.js +5 -5
  31. package/dist/core/services/listening.d.ts +6 -8
  32. package/dist/core/services/listening.js +3 -2
  33. package/dist/event_driven/adapters/stored-queue.d.ts +5 -5
  34. package/dist/event_driven/adapters/stored-queue.js +5 -10
  35. package/dist/event_driven/domain/envelope.d.ts +2 -2
  36. package/dist/event_driven/domain/envelope.js +7 -1
  37. package/dist/event_driven/domain/messages.d.ts +34 -0
  38. package/dist/event_driven/domain/messages.js +1 -0
  39. package/dist/event_driven/domain/queue.d.ts +9 -5
  40. package/dist/event_driven/entrypoint/publisher.d.ts +2 -2
  41. package/dist/event_driven/entrypoint/publisher.js +4 -0
  42. package/dist/event_driven/entrypoint/subscriber.d.ts +122 -29
  43. package/dist/event_driven/entrypoint/subscriber.js +277 -33
  44. package/dist/event_driven/index.d.ts +11 -10
  45. package/dist/event_driven/index.js +9 -9
  46. package/dist/event_driven/services/messages.d.ts +41 -0
  47. package/dist/event_driven/services/messages.js +147 -0
  48. package/dist/observability/domain/logging.d.ts +4 -8
  49. package/dist/observability/services/logging.d.ts +3 -3
  50. package/dist/observability/services/logging.js +1 -1
  51. package/dist/observability/services/trace-context.d.ts +10 -0
  52. package/dist/observability/services/trace-context.js +29 -0
  53. package/package.json +2 -2
  54. package/dist/event_driven/entrypoint/subscriptions.d.ts +0 -110
  55. package/dist/event_driven/entrypoint/subscriptions.js +0 -184
@@ -0,0 +1,10 @@
1
+ import type { Span } from "../domain/tracing.js";
2
+ /** A span as W3C trace context writes it (`traceparent`), or `undefined` when nothing traces. */
3
+ export declare function traceparentOf(span: Span): string | undefined;
4
+ /**
5
+ * The span another process opened, as its `traceparent` says — what a run continues when a
6
+ * message from it arrives. `undefined` for one that is not W3C's or names the all-zeros trace.
7
+ *
8
+ * remoteSpan("00-4bf9…4736-00f0…02b7-01") -> { traceId: "4bf9…4736", spanId: "00f0…02b7", sampled: true }
9
+ */
10
+ export declare function remoteSpan(traceparent: string, tracestate?: string): Span | undefined;
@@ -0,0 +1,29 @@
1
+ const TRACEPARENT = /^00-([0-9a-f]{32})-([0-9a-f]{16})-([0-9a-f]{2})$/;
2
+ /** A span as W3C trace context writes it (`traceparent`), or `undefined` when nothing traces. */
3
+ export function traceparentOf(span) {
4
+ if (span.traceId === "" || span.spanId === "")
5
+ return undefined;
6
+ return `00-${span.traceId}-${span.spanId}-${span.sampled ? "01" : "00"}`;
7
+ }
8
+ /**
9
+ * The span another process opened, as its `traceparent` says — what a run continues when a
10
+ * message from it arrives. `undefined` for one that is not W3C's or names the all-zeros trace.
11
+ *
12
+ * remoteSpan("00-4bf9…4736-00f0…02b7-01") -> { traceId: "4bf9…4736", spanId: "00f0…02b7", sampled: true }
13
+ */
14
+ export function remoteSpan(traceparent, tracestate) {
15
+ const found = TRACEPARENT.exec(traceparent.trim().toLowerCase());
16
+ if (found === null)
17
+ return undefined;
18
+ const [, traceId = "", spanId = "", flags = "00"] = found;
19
+ if (/^0+$/.test(traceId) || /^0+$/.test(spanId))
20
+ return undefined;
21
+ return {
22
+ traceId,
23
+ spanId,
24
+ sampled: (Number.parseInt(flags, 16) & 1) === 1,
25
+ ...(tracestate ? { state: tracestate } : {}),
26
+ set: () => undefined,
27
+ end: () => undefined,
28
+ };
29
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sincpro/client-framework",
3
- "version": "1.2.0",
3
+ "version": "1.4.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",
@@ -40,7 +40,7 @@
40
40
  }
41
41
  },
42
42
  "bin": {
43
- "sincpro-client": "./dist/codegen/entrypoint/cli.js"
43
+ "sincpro-client": "dist/codegen/entrypoint/cli.js"
44
44
  },
45
45
  "files": [
46
46
  "dist",
@@ -1,110 +0,0 @@
1
- import type { Execution } from "../../context/domain/execution.js";
2
- import type { ContextValues } from "../../context/domain/store.js";
3
- import { DomainEvent, type EventClass } from "../../core/domain/dto.js";
4
- import type { Cause, ExecuteOptions } from "../../core/domain/pipeline.js";
5
- import type { LayerLogger } from "../../observability/domain/logging.js";
6
- import type { Span } from "../../observability/domain/tracing.js";
7
- /**
8
- * An entrypoint that hears one domain event and calls the use cases it starts — as a cron calls
9
- * them on a tick, or a screen on a gesture:
10
- *
11
- * class LoggedSuccessfully extends EventSubscriber<OdooLoggedIn> {
12
- * static readonly subscriberName = "LoggedSuccessfully";
13
- * declare readonly ticketsBoundedContext: BoundedContext<TicketsDependencies>;
14
- *
15
- * async handle(event: OdooLoggedIn) {
16
- * await this.ticketsBoundedContext.execute(new CommandPrepareSession(event.uid));
17
- * }
18
- * }
19
- *
20
- * Context: a subscriber is not a use case. It reads the event, builds the Command — or the
21
- * Commands — and executes them on a bounded context it was given; what to decide, and work of
22
- * several steps, is a Feature or an ApplicationService it executes. While `handle` runs,
23
- * `this.span`, `this.logger`, `this.signal`, `this.execution` and every dependency are that
24
- * delivery's: a bounded context it was given executes as a child of it — the same flow, caused by
25
- * it (`BIND`). `subscriberName` is stable metadata, never `constructor.name`. Like a service, a
26
- * subscriber is one instance for every delivery: what belongs to one goes in its locals.
27
- */
28
- export declare abstract class EventSubscriber<E extends DomainEvent, Context extends object = ContextValues> {
29
- /** What this delivery runs with: the process's context. */
30
- readonly context: Readonly<Partial<Context>>;
31
- /** This delivery's span: the Commands it executes are its children. */
32
- readonly span: Span;
33
- /** This delivery's logger: every line carries its flow and trace. */
34
- readonly logger: LayerLogger;
35
- /** What cancels this delivery, when it was executed with one. */
36
- readonly signal: AbortSignal | undefined;
37
- /**
38
- * This delivery: its id, flow (`correlationId`) and cause — the envelope's id, the key that
39
- * makes hearing an event twice harmless.
40
- */
41
- readonly execution: Execution;
42
- abstract handle(event: E): unknown;
43
- }
44
- type SubscriberClass<E extends DomainEvent> = {
45
- new (): EventSubscriber<E>;
46
- readonly subscriberName: string;
47
- };
48
- /** What a subscription takes the place of: the subscriber registered now for the event. */
49
- export type SubscriptionOptions = {
50
- replaces?: abstract new (...args: never[]) => object;
51
- };
52
- /** What `subscribe(...)` answers: the function that registers the subscriber. */
53
- export type Subscribing<E extends DomainEvent> = <S extends SubscriberClass<NoInfer<E>>>(Subscriber: S, decorator?: ClassDecoratorContext<S>) => S;
54
- /**
55
- * The subscribers of one part of an application — which `EventSubscriber` hears which event —,
56
- * with the dependencies they call: what a queue's `Subscriber` hands an event to, beside the
57
- * bounded contexts:
58
- *
59
- * export const ticketsSubscriptions = new Subscriptions<TicketsEntrypoints>("tickets-subscriptions");
60
- * ticketsSubscriptions.addDependency("ticketsBoundedContext", ticketsBoundedContext);
61
- * ticketsSubscriptions.subscribe(OdooLoggedIn)(LoggedSuccessfully);
62
- *
63
- * storedQueue({ store, subscriber: new Subscriber(odooBoundedContext, ticketsSubscriptions) });
64
- *
65
- * Context: the shape of a bounded context, and of the backend's `Crons`: a name that is its
66
- * identity in spans, logs and the queue's record of who already heard an event — so it differs
67
- * from every bounded context's —, dependencies by name, and a function (or a decorator) that
68
- * registers. One subscriber per event here: several reactions to one event are one
69
- * ApplicationService it executes, or subscribers of other registries. Each delivery is a run of
70
- * its own (`<name>/<subscriberName>`, layer `entrypoint`), continuing the envelope's flow; it is
71
- * not a use case, so interceptors and listeners do not see it — they see the use cases it calls.
72
- */
73
- export declare class Subscriptions<D extends object = Record<never, never>> {
74
- readonly name: string;
75
- private readonly registrations;
76
- private readonly dependencyValues;
77
- constructor(name: string);
78
- /** A dependency its subscribers find on `this`, bound to each delivery; `replaces` as a bounded context's. */
79
- addDependency<K extends keyof D & string>(name: K, dependency: D[K], options?: {
80
- replaces: D[K];
81
- }): this;
82
- /**
83
- * Registers the subscriber that hears `event` here. With `replaces`, it takes the place of the
84
- * one registered now.
85
- */
86
- subscribe<E extends DomainEvent>(event: EventClass<E>, options?: SubscriptionOptions): Subscribing<E>;
87
- /** The subscriber registered now for an event class — what a `replaces` names. */
88
- handlerOf(event: abstract new (...args: never[]) => DomainEvent): (abstract new (...args: never[]) => object) | undefined;
89
- /** The event class registered here under a wire name, or `undefined` when nobody here hears it. */
90
- eventTypeOf(name: string): EventClass<DomainEvent> | undefined;
91
- /**
92
- * `event` heard by its subscriber here — continuing the envelope's flow when it is given
93
- * (`execute(event, envelope)`) — and what `handle` returned.
94
- *
95
- * 1. The subscriber registered for the event's exact class; none: an error.
96
- * 2. A run `<name>/<subscriberName>`, layer `entrypoint`: its span, log lines and — when it
97
- * fails with a fault nothing reported yet — its report; its dependencies bound to it.
98
- * Final: what `handle` returned; its failure, as it is, to the queue that delivered it.
99
- */
100
- execute(event: DomainEvent, options?: ExecuteOptions | Cause): Promise<unknown>;
101
- /**
102
- * 1. The subscriber is an `EventSubscriber`, for a domain event class with its `eventName`.
103
- * 2. The event has no subscriber here yet — or, with `replaces`, the one it has is that one.
104
- * 3. Its `subscriberName` is given and no other subscriber here has it; no other event class
105
- * here has the event's name.
106
- * Final: registered, said on its spans when it replaced another; the next delivery hears it.
107
- */
108
- private register;
109
- }
110
- export {};
@@ -1,184 +0,0 @@
1
- import { processContext } from "../../context/infrastructure/process.js";
2
- import { optionsOf } from "../../core/domain/bus.js";
3
- import { DomainEvent } from "../../core/domain/dto.js";
4
- import { RESERVED_MEMBERS } from "../../core/domain/services.js";
5
- import { executeService } from "../../core/services/execution.js";
6
- /**
7
- * An entrypoint that hears one domain event and calls the use cases it starts — as a cron calls
8
- * them on a tick, or a screen on a gesture:
9
- *
10
- * class LoggedSuccessfully extends EventSubscriber<OdooLoggedIn> {
11
- * static readonly subscriberName = "LoggedSuccessfully";
12
- * declare readonly ticketsBoundedContext: BoundedContext<TicketsDependencies>;
13
- *
14
- * async handle(event: OdooLoggedIn) {
15
- * await this.ticketsBoundedContext.execute(new CommandPrepareSession(event.uid));
16
- * }
17
- * }
18
- *
19
- * Context: a subscriber is not a use case. It reads the event, builds the Command — or the
20
- * Commands — and executes them on a bounded context it was given; what to decide, and work of
21
- * several steps, is a Feature or an ApplicationService it executes. While `handle` runs,
22
- * `this.span`, `this.logger`, `this.signal`, `this.execution` and every dependency are that
23
- * delivery's: a bounded context it was given executes as a child of it — the same flow, caused by
24
- * it (`BIND`). `subscriberName` is stable metadata, never `constructor.name`. Like a service, a
25
- * subscriber is one instance for every delivery: what belongs to one goes in its locals.
26
- */
27
- export class EventSubscriber {
28
- }
29
- function isEventClass(value) {
30
- return typeof value === "function" && value.prototype instanceof DomainEvent;
31
- }
32
- function nameOf(Subscriber) {
33
- const named = Subscriber;
34
- return String(named?.subscriberName ?? named?.name ?? Subscriber);
35
- }
36
- /**
37
- * The subscribers of one part of an application — which `EventSubscriber` hears which event —,
38
- * with the dependencies they call: what a queue's `Subscriber` hands an event to, beside the
39
- * bounded contexts:
40
- *
41
- * export const ticketsSubscriptions = new Subscriptions<TicketsEntrypoints>("tickets-subscriptions");
42
- * ticketsSubscriptions.addDependency("ticketsBoundedContext", ticketsBoundedContext);
43
- * ticketsSubscriptions.subscribe(OdooLoggedIn)(LoggedSuccessfully);
44
- *
45
- * storedQueue({ store, subscriber: new Subscriber(odooBoundedContext, ticketsSubscriptions) });
46
- *
47
- * Context: the shape of a bounded context, and of the backend's `Crons`: a name that is its
48
- * identity in spans, logs and the queue's record of who already heard an event — so it differs
49
- * from every bounded context's —, dependencies by name, and a function (or a decorator) that
50
- * registers. One subscriber per event here: several reactions to one event are one
51
- * ApplicationService it executes, or subscribers of other registries. Each delivery is a run of
52
- * its own (`<name>/<subscriberName>`, layer `entrypoint`), continuing the envelope's flow; it is
53
- * not a use case, so interceptors and listeners do not see it — they see the use cases it calls.
54
- */
55
- export class Subscriptions {
56
- name;
57
- registrations = new Map();
58
- dependencyValues = new Map();
59
- constructor(name) {
60
- this.name = name;
61
- if (name.trim() === "" || name.includes("/")) {
62
- throw new TypeError("Subscriptions need a nonempty name without '/'");
63
- }
64
- }
65
- /** A dependency its subscribers find on `this`, bound to each delivery; `replaces` as a bounded context's. */
66
- addDependency(name, dependency, options) {
67
- if (RESERVED_MEMBERS.has(name))
68
- throw new Error(`Reserved dependency name: ${name}`);
69
- const registered = this.dependencyValues.has(name);
70
- if (registered && options === undefined) {
71
- throw new Error(`Dependency already registered: ${name}`);
72
- }
73
- if (options && (!registered || this.dependencyValues.get(name) !== options.replaces)) {
74
- throw new Error(`Dependency replacement does not match the registration: ${name}`);
75
- }
76
- this.dependencyValues.set(name, dependency);
77
- return this;
78
- }
79
- /**
80
- * Registers the subscriber that hears `event` here. With `replaces`, it takes the place of the
81
- * one registered now.
82
- */
83
- subscribe(event, options = {}) {
84
- return (Subscriber, decorator) => {
85
- const register = () => this.register(event, Subscriber, options.replaces);
86
- // Standard decorators run before static fields have initialized.
87
- if (decorator)
88
- decorator.addInitializer(register);
89
- else
90
- register();
91
- return Subscriber;
92
- };
93
- }
94
- /** The subscriber registered now for an event class — what a `replaces` names. */
95
- handlerOf(event) {
96
- return this.registrations.get(event)?.Subscriber;
97
- }
98
- /** The event class registered here under a wire name, or `undefined` when nobody here hears it. */
99
- eventTypeOf(name) {
100
- for (const event of this.registrations.keys()) {
101
- if (event.eventName === name)
102
- return event;
103
- }
104
- return undefined;
105
- }
106
- /**
107
- * `event` heard by its subscriber here — continuing the envelope's flow when it is given
108
- * (`execute(event, envelope)`) — and what `handle` returned.
109
- *
110
- * 1. The subscriber registered for the event's exact class; none: an error.
111
- * 2. A run `<name>/<subscriberName>`, layer `entrypoint`: its span, log lines and — when it
112
- * fails with a fault nothing reported yet — its report; its dependencies bound to it.
113
- * Final: what `handle` returned; its failure, as it is, to the queue that delivered it.
114
- */
115
- execute(event, options) {
116
- const type = Object.getPrototypeOf(event)?.constructor;
117
- const registration = this.registrations.get(type);
118
- if (registration === undefined) {
119
- return Promise.reject(new Error(`${this.name}: no subscriber hears ${event.name}`));
120
- }
121
- registration.instance ??= new registration.Subscriber();
122
- const given = optionsOf(options);
123
- const attributes = {
124
- "sincpro.event": event.name,
125
- ...(registration.replaces ? { "sincpro.replaces": registration.replaces } : {}),
126
- };
127
- return executeService(registration.instance, event, {
128
- boundedContext: this.name,
129
- name: registration.name,
130
- layer: "entrypoint",
131
- method: "handle",
132
- attributes,
133
- dependencies: new Map(this.dependencyValues),
134
- context: () => ({ ...processContext.values }),
135
- ...(given.cause ? { cause: given.cause } : {}),
136
- ...(given.signal ? { signal: given.signal } : {}),
137
- interceptors: [],
138
- expected: () => false,
139
- handlers: [],
140
- });
141
- }
142
- /**
143
- * 1. The subscriber is an `EventSubscriber`, for a domain event class with its `eventName`.
144
- * 2. The event has no subscriber here yet — or, with `replaces`, the one it has is that one.
145
- * 3. Its `subscriberName` is given and no other subscriber here has it; no other event class
146
- * here has the event's name.
147
- * Final: registered, said on its spans when it replaced another; the next delivery hears it.
148
- */
149
- register(event, Subscriber, replaces) {
150
- if (!(Subscriber.prototype instanceof EventSubscriber)) {
151
- throw new TypeError("The subscriber is not an EventSubscriber");
152
- }
153
- if (!isEventClass(event)) {
154
- throw new TypeError("A subscriber is registered for a DomainEvent class");
155
- }
156
- if (typeof event.eventName !== "string" || event.eventName.trim() === "") {
157
- throw new Error("A domain event needs a nonempty eventName");
158
- }
159
- const current = this.registrations.get(event);
160
- if (replaces !== undefined && current?.Subscriber !== replaces) {
161
- throw new Error(`Replacement does not match the subscription: ${nameOf(replaces)}`);
162
- }
163
- if (replaces === undefined && current) {
164
- throw new Error(`Event already subscribed in ${this.name}: one subscriber per event — several reactions are one ApplicationService`);
165
- }
166
- const name = Subscriber.subscriberName;
167
- if (typeof name !== "string" || name.trim() === "" || name.includes("/")) {
168
- throw new Error("A subscriber needs a nonempty subscriberName without '/'");
169
- }
170
- for (const [other, registration] of this.registrations) {
171
- if (registration !== current && registration.name === name) {
172
- throw new Error(`Subscriber name already registered: ${name}`);
173
- }
174
- if (other !== event && other.eventName === event.eventName) {
175
- throw new Error(`Event name already registered: ${event.eventName}`);
176
- }
177
- }
178
- this.registrations.set(event, {
179
- name,
180
- Subscriber,
181
- ...(current ? { replaces: current.name } : {}),
182
- });
183
- }
184
- }