@venizia/ignis-kernel 0.2.0-10 → 0.2.0-12
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/dist/cjs/base/auth/authorize/builders/policy.builder.d.ts +15 -0
- package/dist/cjs/base/auth/authorize/builders/policy.builder.d.ts.map +1 -1
- package/dist/cjs/base/auth/authorize/builders/policy.builder.js +22 -0
- package/dist/cjs/base/auth/authorize/builders/policy.builder.js.map +1 -1
- package/dist/cjs/base/events/common/constants.d.ts +32 -0
- package/dist/cjs/base/events/common/constants.d.ts.map +1 -1
- package/dist/cjs/base/events/common/constants.js +34 -1
- package/dist/cjs/base/events/common/constants.js.map +1 -1
- package/dist/cjs/base/events/common/types.d.ts +41 -3
- package/dist/cjs/base/events/common/types.d.ts.map +1 -1
- package/dist/cjs/base/events/event-bus.d.ts +45 -8
- package/dist/cjs/base/events/event-bus.d.ts.map +1 -1
- package/dist/cjs/base/events/event-bus.js +144 -24
- package/dist/cjs/base/events/event-bus.js.map +1 -1
- package/dist/cjs/base/models/common/types.d.ts +20 -0
- package/dist/cjs/base/models/common/types.d.ts.map +1 -1
- package/dist/cjs/base/models/common/types.js +9 -1
- package/dist/cjs/base/models/common/types.js.map +1 -1
- package/dist/esm/base/auth/authorize/builders/policy.builder.d.ts +15 -0
- package/dist/esm/base/auth/authorize/builders/policy.builder.d.ts.map +1 -1
- package/dist/esm/base/auth/authorize/builders/policy.builder.js +23 -1
- package/dist/esm/base/auth/authorize/builders/policy.builder.js.map +1 -1
- package/dist/esm/base/events/common/constants.d.ts +32 -0
- package/dist/esm/base/events/common/constants.d.ts.map +1 -1
- package/dist/esm/base/events/common/constants.js +32 -0
- package/dist/esm/base/events/common/constants.js.map +1 -1
- package/dist/esm/base/events/common/types.d.ts +41 -3
- package/dist/esm/base/events/common/types.d.ts.map +1 -1
- package/dist/esm/base/events/event-bus.d.ts +45 -8
- package/dist/esm/base/events/event-bus.d.ts.map +1 -1
- package/dist/esm/base/events/event-bus.js +145 -25
- package/dist/esm/base/events/event-bus.js.map +1 -1
- package/dist/esm/base/models/common/types.d.ts +20 -0
- package/dist/esm/base/models/common/types.d.ts.map +1 -1
- package/dist/esm/base/models/common/types.js +7 -0
- package/dist/esm/base/models/common/types.js.map +1 -1
- package/package.json +1 -1
|
@@ -23,6 +23,8 @@ type TBuilderGrantRow = {
|
|
|
23
23
|
action: string;
|
|
24
24
|
effect: TAuthorizationDecision;
|
|
25
25
|
domain: TNullable<string>;
|
|
26
|
+
domainType: TNullable<string>;
|
|
27
|
+
domainId: TNullable<IdType>;
|
|
26
28
|
};
|
|
27
29
|
type TBuilderCustomGrantRow = Omit<TBuilderGrantRow, 'action'> & {
|
|
28
30
|
action: typeof AuthorizationActions.CUSTOM;
|
|
@@ -34,6 +36,17 @@ export declare class AuthorizationPolicyBuilder {
|
|
|
34
36
|
static readonly ACTION_PRINCIPAL = "Action";
|
|
35
37
|
/** Serialize to the casbin token {@link resolveRequestDomain} emits: scope literals pass through; typed domains become `<type>_<id>` so `g3` cascades; null stays null (the adapter defaults grants to `ANY_MEMBER`). */
|
|
36
38
|
private static serializeDomain;
|
|
39
|
+
/**
|
|
40
|
+
* The same value as {@link serializeDomain}, in the two-column form that replaces it. Written
|
|
41
|
+
* alongside `domain` while `domain` is still the read source.
|
|
42
|
+
*
|
|
43
|
+
* `ANY_MEMBER` NORMALISES TO NULL, so `domain` and `domainType` deliberately disagree for a caller
|
|
44
|
+
* that passed the literal: `domain: 'ANY_MEMBER'` but `domainType: null`. A backfill check
|
|
45
|
+
* comparing the two columns by equality WILL report those rows and must not treat it as drift -
|
|
46
|
+
* null already meant `ANY_MEMBER` to the adapter, and admitting both spellings would make one
|
|
47
|
+
* state reachable two ways.
|
|
48
|
+
*/
|
|
49
|
+
private static splitDomain;
|
|
37
50
|
/** A grant (casbin `p`): role/user -> permission, carrying action + effect + domain. A null `domain` means `ANY_MEMBER` (adapter default); pass a scope literal or a typed `{ type, id }` domain. */
|
|
38
51
|
static grant(opts: {
|
|
39
52
|
subject: {
|
|
@@ -74,6 +87,8 @@ export declare class AuthorizationPolicyBuilder {
|
|
|
74
87
|
};
|
|
75
88
|
domain?: TNullable<TPolicyDomainInput>;
|
|
76
89
|
}): {
|
|
90
|
+
domainType: TNullable<string>;
|
|
91
|
+
domainId: TNullable<IdType>;
|
|
77
92
|
variant: "assign_role";
|
|
78
93
|
subjectType: string;
|
|
79
94
|
subjectId: IdType;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"policy.builder.d.ts","sourceRoot":"","sources":["../../../../../../src/base/auth/authorize/builders/policy.builder.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AACrC,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,+BAA+B,CAAC;AAC/D,OAAO,KAAK,EAAE,oBAAoB,EAAE,sBAAsB,EAAE,MAAM,qBAAqB,CAAC;AACxF,OAAO,
|
|
1
|
+
{"version":3,"file":"policy.builder.d.ts","sourceRoot":"","sources":["../../../../../../src/base/auth/authorize/builders/policy.builder.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AACrC,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,+BAA+B,CAAC;AAC/D,OAAO,KAAK,EAAE,oBAAoB,EAAE,sBAAsB,EAAE,MAAM,qBAAqB,CAAC;AACxF,OAAO,EACL,oBAAoB,EAEpB,2BAA2B,EAC5B,MAAM,qBAAqB,CAAC;AAE7B,wGAAwG;AACxG,MAAM,MAAM,kBAAkB,GAAG,MAAM,GAAG;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,EAAE,EAAE,MAAM,CAAA;CAAE,CAAC;AAEvE;;;;;;GAMG;AACH,KAAK,gBAAgB,GAAG;IACtB,OAAO,EAAE,OAAO,2BAA2B,CAAC,KAAK,CAAC,MAAM,CAAC;IACzD,WAAW,EAAE,MAAM,CAAC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M,CAAA;SAAE,CAAC;QACpC,MAAM,EAAE;YAAE,IAAI,EAAE,MAAM,CAAC;YAAC,EAAE,EAAE,MAAM,CAAA;SAAE,CAAC;KACtC;;;;;;;IAUD,+GAA+G;IAC/G,MAAM,CAAC,cAAc,CAAC,IAAI,EAAE;QAAE,KAAK,EAAE,oBAAoB,CAAC;QAAC,MAAM,EAAE,oBAAoB,CAAA;KAAE;;;;;;;IAUzF,iHAAiH;IACjH,MAAM,CAAC,aAAa;;;;;;;IAIpB,8HAA8H;IAC9H,MAAM,CAAC,cAAc,CAAC,IAAI,EAAE;QAC1B,KAAK,EAAE;YAAE,IAAI,EAAE,MAAM,CAAC;YAAC,EAAE,EAAE,MAAM,CAAA;SAAE,CAAC;QACpC,MAAM,EAAE;YAAE,IAAI,EAAE,MAAM,CAAC;YAAC,EAAE,EAAE,MAAM,CAAA;SAAE,CAAC;KACtC;;;;;;;IAUD,uMAAuM;IACvM,MAAM,CAAC,UAAU,CAAC,IAAI,EAAE;QACtB,IAAI,EAAE;YAAE,IAAI,EAAE,MAAM,CAAC;YAAC,EAAE,EAAE,MAAM,CAAA;SAAE,CAAC;QACnC,UAAU,EAAE;YACV,IAAI,EAAE,MAAM,CAAC;YACb,QAAQ,EAAE,WAAW,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;SACvC,CAAC;QAEF,MAAM,EAAE,aAAa,CAAC;YACpB,YAAY,EAAE,MAAM,CAAC;YACrB,MAAM,EAAE,MAAM,CAAC;YACf,MAAM,CAAC,EAAE,SAAS,CAAC,kBAAkB,CAAC,CAAC;YACvC,MAAM,EAAE,sBAAsB,CAAC;SAChC,CAAC,CAAC;KACJ;CAsBF"}
|
|
@@ -14,6 +14,25 @@ class AuthorizationPolicyBuilder {
|
|
|
14
14
|
}
|
|
15
15
|
return [domain.type, domain.id].join('_');
|
|
16
16
|
}
|
|
17
|
+
/**
|
|
18
|
+
* The same value as {@link serializeDomain}, in the two-column form that replaces it. Written
|
|
19
|
+
* alongside `domain` while `domain` is still the read source.
|
|
20
|
+
*
|
|
21
|
+
* `ANY_MEMBER` NORMALISES TO NULL, so `domain` and `domainType` deliberately disagree for a caller
|
|
22
|
+
* that passed the literal: `domain: 'ANY_MEMBER'` but `domainType: null`. A backfill check
|
|
23
|
+
* comparing the two columns by equality WILL report those rows and must not treat it as drift -
|
|
24
|
+
* null already meant `ANY_MEMBER` to the adapter, and admitting both spellings would make one
|
|
25
|
+
* state reachable two ways.
|
|
26
|
+
*/
|
|
27
|
+
static splitDomain(domain) {
|
|
28
|
+
if (domain == null || domain === constants_1.AuthorizationDomainScopes.ANY_MEMBER) {
|
|
29
|
+
return { domainType: null, domainId: null };
|
|
30
|
+
}
|
|
31
|
+
if (typeof domain === 'string') {
|
|
32
|
+
return { domainType: domain, domainId: null };
|
|
33
|
+
}
|
|
34
|
+
return { domainType: domain.type, domainId: domain.id };
|
|
35
|
+
}
|
|
17
36
|
/** A grant (casbin `p`): role/user -> permission, carrying action + effect + domain. A null `domain` means `ANY_MEMBER` (adapter default); pass a scope literal or a typed `{ type, id }` domain. */
|
|
18
37
|
static grant(opts) {
|
|
19
38
|
return {
|
|
@@ -25,6 +44,7 @@ class AuthorizationPolicyBuilder {
|
|
|
25
44
|
action: opts.action,
|
|
26
45
|
effect: opts.effect,
|
|
27
46
|
domain: AuthorizationPolicyBuilder.serializeDomain(opts.domain),
|
|
47
|
+
...AuthorizationPolicyBuilder.splitDomain(opts.domain),
|
|
28
48
|
};
|
|
29
49
|
}
|
|
30
50
|
/** A subset grant (casbin `p` x N): role/user -> resource node with the granted operations in metadata. `action` is fixed to the CUSTOM sentinel; the adapter expands `ops` at read time. */
|
|
@@ -38,6 +58,7 @@ class AuthorizationPolicyBuilder {
|
|
|
38
58
|
action: constants_1.AuthorizationActions.CUSTOM,
|
|
39
59
|
effect: opts.effect,
|
|
40
60
|
domain: AuthorizationPolicyBuilder.serializeDomain(opts.domain),
|
|
61
|
+
...AuthorizationPolicyBuilder.splitDomain(opts.domain),
|
|
41
62
|
metadata: { ops: opts.ops },
|
|
42
63
|
};
|
|
43
64
|
}
|
|
@@ -50,6 +71,7 @@ class AuthorizationPolicyBuilder {
|
|
|
50
71
|
targetType: opts.role.type,
|
|
51
72
|
targetId: opts.role.id,
|
|
52
73
|
domain: AuthorizationPolicyBuilder.serializeDomain(opts.domain),
|
|
74
|
+
...AuthorizationPolicyBuilder.splitDomain(opts.domain),
|
|
53
75
|
};
|
|
54
76
|
}
|
|
55
77
|
/** A user joins a domain (casbin `g2`) - backs the `ANY_MEMBER` grant scope. */
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"policy.builder.js","sourceRoot":"","sources":["../../../../../../src/base/auth/authorize/builders/policy.builder.ts"],"names":[],"mappings":";;;AAGA,
|
|
1
|
+
{"version":3,"file":"policy.builder.js","sourceRoot":"","sources":["../../../../../../src/base/auth/authorize/builders/policy.builder.ts"],"names":[],"mappings":";;;AAGA,mDAI6B;AA8B7B,MAAa,0BAA0B;aACrB,qBAAgB,GAAG,QAAQ,CAAC;IAE5C,yNAAyN;IACjN,MAAM,CAAC,eAAe,CAAC,MAAsC;QACnE,IAAI,MAAM,IAAI,IAAI,EAAE,CAAC;YACnB,OAAO,IAAI,CAAC;QACd,CAAC;QAED,IAAI,OAAO,MAAM,KAAK,QAAQ,EAAE,CAAC;YAC/B,OAAO,MAAM,CAAC;QAChB,CAAC;QAED,OAAO,CAAC,MAAM,CAAC,IAAI,EAAE,MAAM,CAAC,EAAE,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;IAC5C,CAAC;IAED;;;;;;;;;OASG;IACK,MAAM,CAAC,WAAW,CAAC,MAAsC;QAI/D,IAAI,MAAM,IAAI,IAAI,IAAI,MAAM,KAAK,qCAAyB,CAAC,UAAU,EAAE,CAAC;YACtE,OAAO,EAAE,UAAU,EAAE,IAAI,EAAE,QAAQ,EAAE,IAAI,EAAE,CAAC;QAC9C,CAAC;QAED,IAAI,OAAO,MAAM,KAAK,QAAQ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E,KAAK,MAAM,KAAK,IAAI,IAAI,CAAC,MAAM,EAAE,CAAC;YAChC,MAAM,YAAY,GAAG,IAAI,CAAC,UAAU,CAAC,QAAQ,CAAC,GAAG,CAAC,KAAK,CAAC,YAAY,CAAC,CAAC;YACtE,IAAI,CAAC,YAAY,EAAE,CAAC;gBAClB,SAAS;YACX,CAAC;YAED,MAAM,MAAM,GAAG,0BAA0B,CAAC,KAAK,CAAC;gBAC9C,OAAO,EAAE,EAAE,IAAI,EAAE,IAAI,CAAC,IAAI,CAAC,IAAI,EAAE,EAAE,EAAE,IAAI,CAAC,IAAI,CAAC,EAAE,EAAE;gBACnD,UAAU,EAAE,EAAE,IAAI,EAAE,IAAI,CAAC,UAAU,CAAC,IAAI,EAAE,EAAE,EAAE,YAAY,EAAE;gBAC5D,MAAM,EAAE,KAAK,CAAC,MAAM;gBACpB,MAAM,EAAE,KAAK,CAAC,MAAM;gBACpB,MAAM,EAAE,KAAK,CAAC,MAAM;aACrB,CAAC,CAAC;YAEH,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;QACpB,CAAC;QAED,OAAO,IAAI,CAAC;IACd,CAAC;;AAhNH,gEAiNC"}
|
|
@@ -1,5 +1,37 @@
|
|
|
1
|
+
import type { TConstValue } from '@venizia/ignis-helpers/common';
|
|
1
2
|
export declare class EventDispatchRetry {
|
|
2
3
|
static readonly MAX_ATTEMPTS = 3;
|
|
3
4
|
static readonly BASE_DELAY_MS = 100;
|
|
5
|
+
/**
|
|
6
|
+
* Ceiling on `register`'s `retry.maxAttempts`. Dispatch is fire-and-forget - nobody awaits it - so
|
|
7
|
+
* an attempt count high enough to matter is already high enough to hammer a struggling downstream
|
|
8
|
+
* resource attempt after attempt. Twice the highest count any known consumer has asked for (5)
|
|
9
|
+
* leaves room to tune without permitting an attempt storm.
|
|
10
|
+
*/
|
|
11
|
+
static readonly MAX_ATTEMPTS_CEILING = 10;
|
|
12
|
+
/**
|
|
13
|
+
* Ceiling, in ms, on the total backoff window `register`'s `retry` can produce (the sum of every
|
|
14
|
+
* inter-attempt delay, computed without jitter since jitter only shrinks it). Matches
|
|
15
|
+
* `RetryHelper`'s own default cap on a SINGLE delay: a dispatch holds a handler's resources - e.g.
|
|
16
|
+
* a database connection - for its entire retry window, so the sum across every attempt gets the
|
|
17
|
+
* same "unreasonable beyond this" line the framework already draws for one attempt.
|
|
18
|
+
*/
|
|
19
|
+
static readonly MAX_TOTAL_WINDOW_MS = 30000;
|
|
4
20
|
}
|
|
21
|
+
/**
|
|
22
|
+
* Tag for `EventBus.register`'s `handler` union (`TEventHandlerReference`). The tag makes the
|
|
23
|
+
* caller's choice explicit at the call site instead of implicit in whichever field is present, so a
|
|
24
|
+
* mismatched `type`/field pair (`BINDING_KEY` with `fn`, or `FUNCTION` with `key`) is a compile
|
|
25
|
+
* error there rather than a runtime surprise later, inside a `queueMicrotask` where nobody is
|
|
26
|
+
* watching.
|
|
27
|
+
*/
|
|
28
|
+
export declare class EventHandlerTypes {
|
|
29
|
+
/** `handler.key` is resolved from the container on every retry attempt - a rebind reaches an attempt already in flight. */
|
|
30
|
+
static readonly BINDING_KEY = "binding-key";
|
|
31
|
+
/** `handler.fn` is invoked directly as a captured closure - there is nothing to rebind, so every attempt in a retry window calls the exact function registered here. */
|
|
32
|
+
static readonly FUNCTION = "function";
|
|
33
|
+
static readonly SCHEME_SET: Set<string>;
|
|
34
|
+
static isValid(value: string): value is TEventHandlerType;
|
|
35
|
+
}
|
|
36
|
+
export type TEventHandlerType = TConstValue<typeof EventHandlerTypes>;
|
|
5
37
|
//# sourceMappingURL=constants.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../../../../../src/base/events/common/constants.ts"],"names":[],"mappings":"AAAA,qBAAa,kBAAkB;IAC7B,MAAM,CAAC,QAAQ,CAAC,YAAY,KAAK;IACjC,MAAM,CAAC,QAAQ,CAAC,aAAa,OAAO;
|
|
1
|
+
{"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../../../../../src/base/events/common/constants.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,+BAA+B,CAAC;AAEjE,qBAAa,kBAAkB;IAC7B,MAAM,CAAC,QAAQ,CAAC,YAAY,KAAK;IACjC,MAAM,CAAC,QAAQ,CAAC,aAAa,OAAO;IAEpC;;;;;OAKG;IACH,MAAM,CAAC,QAAQ,CAAC,oBAAoB,MAAM;IAE1C;;;;;;OAMG;IACH,MAAM,CAAC,QAAQ,CAAC,mBAAmB,SAAU;CAC9C;AAED;;;;;;GAMG;AACH,qBAAa,iBAAiB;IAC5B,2HAA2H;IAC3H,MAAM,CAAC,QAAQ,CAAC,WAAW,iBAAiB;IAE5C,wKAAwK;IACxK,MAAM,CAAC,QAAQ,CAAC,QAAQ,cAAc;IAEtC,MAAM,CAAC,QAAQ,CAAC,UAAU,cAAsD;IAEhF,MAAM,CAAC,OAAO,CAAC,KAAK,EAAE,MAAM,GAAG,KAAK,IAAI,iBAAiB;CAG1D;AAED,MAAM,MAAM,iBAAiB,GAAG,WAAW,CAAC,OAAO,iBAAiB,CAAC,CAAC"}
|
|
@@ -1,9 +1,42 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.EventDispatchRetry = void 0;
|
|
3
|
+
exports.EventHandlerTypes = exports.EventDispatchRetry = void 0;
|
|
4
4
|
class EventDispatchRetry {
|
|
5
5
|
static { this.MAX_ATTEMPTS = 3; }
|
|
6
6
|
static { this.BASE_DELAY_MS = 100; }
|
|
7
|
+
/**
|
|
8
|
+
* Ceiling on `register`'s `retry.maxAttempts`. Dispatch is fire-and-forget - nobody awaits it - so
|
|
9
|
+
* an attempt count high enough to matter is already high enough to hammer a struggling downstream
|
|
10
|
+
* resource attempt after attempt. Twice the highest count any known consumer has asked for (5)
|
|
11
|
+
* leaves room to tune without permitting an attempt storm.
|
|
12
|
+
*/
|
|
13
|
+
static { this.MAX_ATTEMPTS_CEILING = 10; }
|
|
14
|
+
/**
|
|
15
|
+
* Ceiling, in ms, on the total backoff window `register`'s `retry` can produce (the sum of every
|
|
16
|
+
* inter-attempt delay, computed without jitter since jitter only shrinks it). Matches
|
|
17
|
+
* `RetryHelper`'s own default cap on a SINGLE delay: a dispatch holds a handler's resources - e.g.
|
|
18
|
+
* a database connection - for its entire retry window, so the sum across every attempt gets the
|
|
19
|
+
* same "unreasonable beyond this" line the framework already draws for one attempt.
|
|
20
|
+
*/
|
|
21
|
+
static { this.MAX_TOTAL_WINDOW_MS = 30_000; }
|
|
7
22
|
}
|
|
8
23
|
exports.EventDispatchRetry = EventDispatchRetry;
|
|
24
|
+
/**
|
|
25
|
+
* Tag for `EventBus.register`'s `handler` union (`TEventHandlerReference`). The tag makes the
|
|
26
|
+
* caller's choice explicit at the call site instead of implicit in whichever field is present, so a
|
|
27
|
+
* mismatched `type`/field pair (`BINDING_KEY` with `fn`, or `FUNCTION` with `key`) is a compile
|
|
28
|
+
* error there rather than a runtime surprise later, inside a `queueMicrotask` where nobody is
|
|
29
|
+
* watching.
|
|
30
|
+
*/
|
|
31
|
+
class EventHandlerTypes {
|
|
32
|
+
/** `handler.key` is resolved from the container on every retry attempt - a rebind reaches an attempt already in flight. */
|
|
33
|
+
static { this.BINDING_KEY = 'binding-key'; }
|
|
34
|
+
/** `handler.fn` is invoked directly as a captured closure - there is nothing to rebind, so every attempt in a retry window calls the exact function registered here. */
|
|
35
|
+
static { this.FUNCTION = 'function'; }
|
|
36
|
+
static { this.SCHEME_SET = new Set([this.BINDING_KEY, this.FUNCTION]); }
|
|
37
|
+
static isValid(value) {
|
|
38
|
+
return this.SCHEME_SET.has(value);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
exports.EventHandlerTypes = EventHandlerTypes;
|
|
9
42
|
//# sourceMappingURL=constants.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"constants.js","sourceRoot":"","sources":["../../../../../src/base/events/common/constants.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"constants.js","sourceRoot":"","sources":["../../../../../src/base/events/common/constants.ts"],"names":[],"mappings":";;;AAEA,MAAa,kBAAkB;aACb,iBAAY,GAAG,CAAC,CAAC;aACjB,kBAAa,GAAG,GAAG,CAAC;IAEpC;;;;;OAKG;aACa,yBAAoB,GAAG,EAAE,CAAC;IAE1C;;;;;;OAMG;aACa,wBAAmB,GAAG,MAAM,CAAC;;AAnB/C,gDAoBC;AAED;;;;;;GAMG;AACH,MAAa,iBAAiB;IAC5B,2HAA2H;aAC3G,gBAAW,GAAG,aAAa,CAAC;IAE5C,wKAAwK;aACxJ,aAAQ,GAAG,UAAU,CAAC;aAEtB,eAAU,GAAG,IAAI,GAAG,CAAS,CAAC,IAAI,CAAC,WAAW,EAAE,IAAI,CAAC,QAAQ,CAAC,CAAC,CAAC;IAEhF,MAAM,CAAC,OAAO,CAAC,KAAa;QAC1B,OAAO,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC;IACpC,CAAC;;AAXH,8CAYC"}
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import type { ValueOrPromise } from '@venizia/ignis-helpers/common';
|
|
2
|
+
import { EventHandlerTypes } from './constants';
|
|
1
3
|
/** One occurrence of a named domain event, decoupled from whatever publishes or handles it. */
|
|
2
4
|
export interface IDomainEvent<TPayload = unknown> {
|
|
3
5
|
name: string;
|
|
@@ -6,7 +8,41 @@ export interface IDomainEvent<TPayload = unknown> {
|
|
|
6
8
|
traceId?: string;
|
|
7
9
|
}
|
|
8
10
|
/**
|
|
9
|
-
*
|
|
11
|
+
* Per-registration retry override for `EventBus.register`. Both fields must be positive integers,
|
|
12
|
+
* and are bounded by `EventDispatchRetry.MAX_ATTEMPTS_CEILING` / `MAX_TOTAL_WINDOW_MS` - a value
|
|
13
|
+
* outside either throws at `register()`, never clamps.
|
|
14
|
+
*/
|
|
15
|
+
export interface IEventHandlerRetryOptions {
|
|
16
|
+
/** Default `EventDispatchRetry.MAX_ATTEMPTS`. */
|
|
17
|
+
maxAttempts?: number;
|
|
18
|
+
/** First backoff delay in ms; exponential growth is computed from it. Default `EventDispatchRetry.BASE_DELAY_MS`. */
|
|
19
|
+
baseDelayMs?: number;
|
|
20
|
+
}
|
|
21
|
+
/** A handler callback - synchronous or asynchronous, `EventBus` awaits either the same way. */
|
|
22
|
+
export type TEventHandlerFunction<TPayload = unknown> = (opts: {
|
|
23
|
+
event: IDomainEvent<TPayload>;
|
|
24
|
+
}) => ValueOrPromise<void>;
|
|
25
|
+
/** `register`'s binding-key handler shape: resolved from the container by `key` on every retry attempt. */
|
|
26
|
+
export interface IEventHandlerBindingKeyReference {
|
|
27
|
+
type: typeof EventHandlerTypes.BINDING_KEY;
|
|
28
|
+
key: string;
|
|
29
|
+
}
|
|
30
|
+
/** `register`'s function handler shape: `fn` is invoked directly, captured as a closure - see `TEventHandlerReference`. */
|
|
31
|
+
export interface IEventHandlerFunctionReference<TPayload = unknown> {
|
|
32
|
+
type: typeof EventHandlerTypes.FUNCTION;
|
|
33
|
+
fn: TEventHandlerFunction<TPayload>;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* The two shapes `EventBus.register`'s `handler` accepts, tagged by `type` so the caller states
|
|
37
|
+
* which rather than the bus inferring it from whichever field is present. Only `BINDING_KEY` shares
|
|
38
|
+
* `IEventHandler`'s rebind property; see its doc.
|
|
39
|
+
*/
|
|
40
|
+
export type TEventHandlerReference<TPayload = unknown> = IEventHandlerBindingKeyReference | IEventHandlerFunctionReference<TPayload>;
|
|
41
|
+
/**
|
|
42
|
+
* Handles one `IDomainEvent`. Implemented by whatever is bound into the container under
|
|
43
|
+
* `register`'s `{ type: EventHandlerTypes.BINDING_KEY, key }` form - see `TEventHandlerReference`
|
|
44
|
+
* for the `FUNCTION` form, which does not implement this interface and does not share its rebind
|
|
45
|
+
* property.
|
|
10
46
|
*
|
|
11
47
|
* MUST be idempotent. The bus retries a failed handler, so delivery is at-least-once: one that
|
|
12
48
|
* half-succeeds then throws runs again from the top, re-applying whatever already landed. Three
|
|
@@ -16,11 +52,13 @@ export interface IDomainEvent<TPayload = unknown> {
|
|
|
16
52
|
* audit row, which simply gains a duplicate - corrupting the record used to diagnose the retry.
|
|
17
53
|
*
|
|
18
54
|
* Resolved from the container by binding key at dispatch time, never captured as a direct reference,
|
|
19
|
-
* so rebinding the key takes effect on the next attempt - including one already in flight.
|
|
55
|
+
* so rebinding the key takes effect on the next attempt - including one already in flight. A
|
|
56
|
+
* `FUNCTION`-form handler does NOT have this property: `fn` is a captured closure, so every attempt
|
|
57
|
+
* in a long retry window calls the exact function registered at startup.
|
|
20
58
|
*/
|
|
21
59
|
export interface IEventHandler<TEvent extends IDomainEvent = IDomainEvent> {
|
|
22
60
|
handle(opts: {
|
|
23
61
|
event: TEvent;
|
|
24
|
-
}):
|
|
62
|
+
}): ValueOrPromise<void>;
|
|
25
63
|
}
|
|
26
64
|
//# sourceMappingURL=types.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../../../src/base/events/common/types.ts"],"names":[],"mappings":"AAAA,+FAA+F;AAC/F,MAAM,WAAW,YAAY,CAAC,QAAQ,GAAG,OAAO;IAC9C,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,QAAQ,CAAC;IAClB,UAAU,EAAE,MAAM,CAAC;IACnB,OAAO,CAAC,EAAE,MAAM,CAAC;CAClB;AAED
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../../../src/base/events/common/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,+BAA+B,CAAC;AACpE,OAAO,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAC;AAEhD,+FAA+F;AAC/F,MAAM,WAAW,YAAY,CAAC,QAAQ,GAAG,OAAO;IAC9C,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,QAAQ,CAAC;IAClB,UAAU,EAAE,MAAM,CAAC;IACnB,OAAO,CAAC,EAAE,MAAM,CAAC;CAClB;AAED;;;;GAIG;AACH,MAAM,WAAW,yBAAyB;IACxC,iDAAiD;IACjD,WAAW,CAAC,EAAE,MAAM,CAAC;IAErB,qHAAqH;IACrH,WAAW,CAAC,EAAE,MAAM,CAAC;CACtB;AAED,+FAA+F;AAC/F,MAAM,MAAM,qBAAqB,CAAC,QAAQ,GAAG,OAAO,IAAI,CAAC,IAAI,EAAE;IAC7D,KAAK,EAAE,YAAY,CAAC,QAAQ,CAAC,CAAC;CAC/B,KAAK,cAAc,CAAC,IAAI,CAAC,CAAC;AAE3B,2GAA2G;AAC3G,MAAM,WAAW,gCAAgC;IAC/C,IAAI,EAAE,OAAO,iBAAiB,CAAC,WAAW,CAAC;IAC3C,GAAG,EAAE,MAAM,CAAC;CACb;AAED,2HAA2H;AAC3H,MAAM,WAAW,8BAA8B,CAAC,QAAQ,GAAG,OAAO;IAChE,IAAI,EAAE,OAAO,iBAAiB,CAAC,QAAQ,CAAC;IACxC,EAAE,EAAE,qBAAqB,CAAC,QAAQ,CAAC,CAAC;CACrC;AAED;;;;GAIG;AACH,MAAM,MAAM,sBAAsB,CAAC,QAAQ,GAAG,OAAO,IACnD,gCAAgC,GAAG,8BAA8B,CAAC,QAAQ,CAAC,CAAC;AAE9E;;;;;;;;;;;;;;;;;GAiBG;AACH,MAAM,WAAW,aAAa,CAAC,MAAM,SAAS,YAAY,GAAG,YAAY;IACvE,MAAM,CAAC,IAAI,EAAE;QAAE,KAAK,EAAE,MAAM,CAAA;KAAE,GAAG,cAAc,CAAC,IAAI,CAAC,CAAC;CACvD"}
|
|
@@ -1,21 +1,52 @@
|
|
|
1
1
|
import type { Container } from '../../helpers/inversion/container';
|
|
2
2
|
import { BaseHelper } from '@venizia/ignis-helpers/core';
|
|
3
|
+
import type { IEventHandlerRetryOptions, TEventHandlerReference } from './common/types';
|
|
3
4
|
/** In-process, fire-and-forget domain event bus. `TPayloadMap` keys the event names, so a wrong name
|
|
4
|
-
* or payload shape is a compile error. Delivery is always async
|
|
5
|
-
*
|
|
6
|
-
*
|
|
5
|
+
* or payload shape is a compile error. Delivery is always async. Retry timing is a property of the
|
|
6
|
+
* handler's own failure mode, not of the event, so `register` may tune it per registration. But
|
|
7
|
+
* nobody awaits a fire-and-forget dispatch, so an unbounded window would let one event hold a
|
|
8
|
+
* handler - and whatever it holds open, e.g. a database connection - for an unbounded time with no
|
|
9
|
+
* one to report it. What the bus guarantees instead: a bounded MAXIMUM time a single dispatch can
|
|
10
|
+
* occupy, enforced as a hard ceiling at `register()` - see `EventDispatchRetry`. Retry makes
|
|
11
|
+
* delivery at-least-once: handlers must be idempotent, see `IEventHandler`. */
|
|
7
12
|
export declare class EventBus<TPayloadMap extends object> extends BaseHelper {
|
|
8
13
|
private readonly container;
|
|
9
14
|
/** `register` appends, never replaces - two handlers may share an event name, and replacing would
|
|
10
15
|
* drop the first with no way for the caller to notice. */
|
|
11
|
-
private readonly
|
|
16
|
+
private readonly registrationsByName;
|
|
12
17
|
constructor(opts: {
|
|
13
18
|
scope?: string;
|
|
14
19
|
container: Container;
|
|
15
20
|
});
|
|
21
|
+
/** The pre-jitter total of every inter-attempt delay `maxAttempts`/`baseDelayMs` would produce -
|
|
22
|
+
* jitter (always on, see `dispatch`) only ever shrinks a delay, so this is a true upper bound. */
|
|
23
|
+
private static computeTotalWindowMs;
|
|
24
|
+
/** Loud rejection, never a silent clamp: a caller who believes a number the bus does not honour is
|
|
25
|
+
* worse off than one who sees the ceiling at boot. */
|
|
26
|
+
private static assertValidRetry;
|
|
27
|
+
/**
|
|
28
|
+
* Turns `handler` into a uniform invoker plus a description for the exhaustion log line.
|
|
29
|
+
* `BINDING_KEY` resolves through the container INSIDE the returned closure, so every call re-reads
|
|
30
|
+
* the current binding; `FUNCTION` calls `fn` directly, so the closure IS the resolution - there is
|
|
31
|
+
* nothing left to re-read on a later attempt. The tag (`handler.type`) is what this switches on,
|
|
32
|
+
* not which field happens to be present - see `EventHandlerTypes`' doc.
|
|
33
|
+
*/
|
|
34
|
+
private static buildHandlerInvoker;
|
|
35
|
+
/**
|
|
36
|
+
* `retry` is optional and per REGISTRATION, not per publish: the right window belongs to the
|
|
37
|
+
* handler's own failure mode, and two handlers on the same event name may each pick their own.
|
|
38
|
+
* Omitting it reproduces today's `EventDispatchRetry` defaults exactly. Both `maxAttempts` and the
|
|
39
|
+
* total backoff window it implies are bounded - see `EventDispatchRetry` - and exceeding either
|
|
40
|
+
* throws here, at boot where a human sees it, rather than being silently clamped at dispatch time.
|
|
41
|
+
*/
|
|
16
42
|
register<K extends keyof TPayloadMap & string>(opts: {
|
|
17
43
|
name: K;
|
|
18
|
-
|
|
44
|
+
/** A `BINDING_KEY` reference re-resolves through the container on every retry attempt, so a
|
|
45
|
+
* rebind reaches a dispatch already in flight. A `FUNCTION` reference is captured as-is: a
|
|
46
|
+
* rebind has nothing to reach, and every attempt in a long retry window calls the exact `fn`
|
|
47
|
+
* registered here. */
|
|
48
|
+
handler: TEventHandlerReference<TPayloadMap[K]>;
|
|
49
|
+
retry?: IEventHandlerRetryOptions;
|
|
19
50
|
}): void;
|
|
20
51
|
/**
|
|
21
52
|
* Never blocks and never throws back to the caller: dispatch runs outside this call's flow via
|
|
@@ -28,9 +59,15 @@ export declare class EventBus<TPayloadMap extends object> extends BaseHelper {
|
|
|
28
59
|
traceId?: string;
|
|
29
60
|
}): void;
|
|
30
61
|
/**
|
|
31
|
-
*
|
|
32
|
-
*
|
|
33
|
-
*
|
|
62
|
+
* Attempts/backoff come from the registration (`EventDispatchRetry`'s defaults when omitted),
|
|
63
|
+
* always with full jitter - `RetryJitterModes.FULL` decorrelates a shared-resource pile-up hardest,
|
|
64
|
+
* so many handlers retrying the same lock do not collide on the same instant a second and third
|
|
65
|
+
* time. `invokeHandler` already encodes whether that re-resolves (`BINDING_KEY`) or was captured
|
|
66
|
+
* once (`FUNCTION`) - see `buildHandlerInvoker` - so this method calls it uniformly. A handler that
|
|
67
|
+
* throws synchronously is caught exactly like one that returns a rejected promise: `execution`
|
|
68
|
+
* here is itself `async`, and JS converts a synchronous throw inside an async function body into a
|
|
69
|
+
* rejection of that function's own promise - `RetryHelper.executeWithRetry`'s attempt loop never
|
|
70
|
+
* sees the difference.
|
|
34
71
|
*
|
|
35
72
|
* `RetryHelper.executeWithRetry` is used without its own `logger` - its internal per-attempt/exhaustion
|
|
36
73
|
* logging knows nothing about the event name or `traceId`, so it cannot be the trace anyone
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"event-bus.d.ts","sourceRoot":"","sources":["../../../../src/base/events/event-bus.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,+BAA+B,CAAC;
|
|
1
|
+
{"version":3,"file":"event-bus.d.ts","sourceRoot":"","sources":["../../../../src/base/events/event-bus.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,+BAA+B,CAAC;AAE/D,OAAO,EACL,UAAU,EAKX,MAAM,6BAA6B,CAAC;AAGrC,OAAO,KAAK,EAGV,yBAAyB,EACzB,sBAAsB,EACvB,MAAM,gBAAgB,CAAC;AAexB;;;;;;;+EAO+E;AAC/E,qBAAa,QAAQ,CAAC,WAAW,SAAS,MAAM,CAAE,SAAQ,UAAU;IAClE,OAAO,CAAC,QAAQ,CAAC,SAAS,CAAY;IAEtC;8DAC0D;IAC1D,OAAO,CAAC,QAAQ,CAAC,mBAAmB,CAA4C;gBAEpE,IAAI,EAAE;QAAE,KAAK,CAAC,EAAE,MAAM,CAAC;QAAC,SAAS,EAAE,SAAS,CAAA;KAAE;IAM1D;sGACkG;IAClG,OAAO,CAAC,MAAM,CAAC,oBAAoB;IAkBnC;0DACsD;IACtD,OAAO,CAAC,MAAM,CAAC,gBAAgB;IAiC/B;;;;;;OAMG;IACH,OAAO,CAAC,MAAM,CAAC,mBAAmB;IAkDlC;;;;;;OAMG;IACH,QAAQ,CAAC,CAAC,SAAS,MAAM,WAAW,GAAG,MAAM,EAAE,IAAI,EAAE;QACnD,IAAI,EAAE,CAAC,CAAC;QACR;;;8BAGsB;QACtB,OAAO,EAAE,sBAAsB,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC,CAAC;QAChD,KAAK,CAAC,EAAE,yBAAyB,CAAC;KACnC,GAAG,IAAI;IAmCR;;;;OAIG;IACH,OAAO,CAAC,CAAC,SAAS,MAAM,WAAW,GAAG,MAAM,EAAE,IAAI,EAAE;QAClD,IAAI,EAAE,CAAC,CAAC;QACR,OAAO,EAAE,WAAW,CAAC,CAAC,CAAC,CAAC;QACxB,OAAO,CAAC,EAAE,MAAM,CAAC;KAClB,GAAG,IAAI;IAiCR;;;;;;;;;;;;;;OAcG;YACW,QAAQ;CAmCvB"}
|
|
@@ -8,28 +8,142 @@ const core_1 = require("@venizia/ignis-helpers/core");
|
|
|
8
8
|
const isEmpty_1 = __importDefault(require("lodash/isEmpty"));
|
|
9
9
|
const constants_1 = require("./common/constants");
|
|
10
10
|
/** In-process, fire-and-forget domain event bus. `TPayloadMap` keys the event names, so a wrong name
|
|
11
|
-
* or payload shape is a compile error. Delivery is always async
|
|
12
|
-
*
|
|
13
|
-
*
|
|
11
|
+
* or payload shape is a compile error. Delivery is always async. Retry timing is a property of the
|
|
12
|
+
* handler's own failure mode, not of the event, so `register` may tune it per registration. But
|
|
13
|
+
* nobody awaits a fire-and-forget dispatch, so an unbounded window would let one event hold a
|
|
14
|
+
* handler - and whatever it holds open, e.g. a database connection - for an unbounded time with no
|
|
15
|
+
* one to report it. What the bus guarantees instead: a bounded MAXIMUM time a single dispatch can
|
|
16
|
+
* occupy, enforced as a hard ceiling at `register()` - see `EventDispatchRetry`. Retry makes
|
|
17
|
+
* delivery at-least-once: handlers must be idempotent, see `IEventHandler`. */
|
|
14
18
|
class EventBus extends core_1.BaseHelper {
|
|
15
19
|
constructor(opts) {
|
|
16
20
|
super({ scope: opts.scope ?? EventBus.name });
|
|
17
21
|
this.container = opts.container;
|
|
18
|
-
this.
|
|
22
|
+
this.registrationsByName = new Map();
|
|
19
23
|
}
|
|
24
|
+
/** The pre-jitter total of every inter-attempt delay `maxAttempts`/`baseDelayMs` would produce -
|
|
25
|
+
* jitter (always on, see `dispatch`) only ever shrinks a delay, so this is a true upper bound. */
|
|
26
|
+
static computeTotalWindowMs(opts) {
|
|
27
|
+
const { maxAttempts, baseDelayMs } = opts;
|
|
28
|
+
let totalWindowMs = 0;
|
|
29
|
+
for (let attempt = 1; attempt < maxAttempts; attempt++) {
|
|
30
|
+
totalWindowMs += core_1.RetryHelper.computeBackoffDelayMs({
|
|
31
|
+
attempt,
|
|
32
|
+
backoff: {
|
|
33
|
+
strategy: core_1.RetryBackoffStrategies.EXPONENTIAL,
|
|
34
|
+
initialDelayMs: baseDelayMs,
|
|
35
|
+
jitter: core_1.RetryJitterModes.NONE,
|
|
36
|
+
},
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
return totalWindowMs;
|
|
40
|
+
}
|
|
41
|
+
/** Loud rejection, never a silent clamp: a caller who believes a number the bus does not honour is
|
|
42
|
+
* worse off than one who sees the ceiling at boot. */
|
|
43
|
+
static assertValidRetry(opts) {
|
|
44
|
+
const { name, maxAttempts, baseDelayMs } = opts;
|
|
45
|
+
if (!Number.isInteger(maxAttempts) || maxAttempts < 1) {
|
|
46
|
+
throw (0, core_1.getError)({
|
|
47
|
+
message: `[EventBus][register] retry.maxAttempts must be a positive integer | name: ${name} | Got: ${maxAttempts}`,
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
if (!Number.isInteger(baseDelayMs) || baseDelayMs < 1) {
|
|
51
|
+
throw (0, core_1.getError)({
|
|
52
|
+
message: `[EventBus][register] retry.baseDelayMs must be a positive integer | name: ${name} | Got: ${baseDelayMs}`,
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
if (maxAttempts > constants_1.EventDispatchRetry.MAX_ATTEMPTS_CEILING) {
|
|
56
|
+
throw (0, core_1.getError)({
|
|
57
|
+
message: `[EventBus][register] retry.maxAttempts exceeds the ceiling | name: ${name} | Got: ${maxAttempts} | Ceiling: ${constants_1.EventDispatchRetry.MAX_ATTEMPTS_CEILING} | Lower retry.maxAttempts`,
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
const totalWindowMs = EventBus.computeTotalWindowMs({ maxAttempts, baseDelayMs });
|
|
61
|
+
if (totalWindowMs > constants_1.EventDispatchRetry.MAX_TOTAL_WINDOW_MS) {
|
|
62
|
+
throw (0, core_1.getError)({
|
|
63
|
+
message: `[EventBus][register] retry window exceeds the ceiling | name: ${name} | Got: ${totalWindowMs}ms | Ceiling: ${constants_1.EventDispatchRetry.MAX_TOTAL_WINDOW_MS}ms | Lower retry.maxAttempts or retry.baseDelayMs`,
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Turns `handler` into a uniform invoker plus a description for the exhaustion log line.
|
|
69
|
+
* `BINDING_KEY` resolves through the container INSIDE the returned closure, so every call re-reads
|
|
70
|
+
* the current binding; `FUNCTION` calls `fn` directly, so the closure IS the resolution - there is
|
|
71
|
+
* nothing left to re-read on a later attempt. The tag (`handler.type`) is what this switches on,
|
|
72
|
+
* not which field happens to be present - see `EventHandlerTypes`' doc.
|
|
73
|
+
*/
|
|
74
|
+
static buildHandlerInvoker(opts) {
|
|
75
|
+
const { container, handler, name } = opts;
|
|
76
|
+
if ((0, isEmpty_1.default)(handler)) {
|
|
77
|
+
throw (0, core_1.getError)({
|
|
78
|
+
message: `[EventBus][register] Invalid handler | name: ${name} | Got: ${handler}`,
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
switch (handler.type) {
|
|
82
|
+
case constants_1.EventHandlerTypes.BINDING_KEY: {
|
|
83
|
+
const { key } = handler;
|
|
84
|
+
if ((0, isEmpty_1.default)(key)) {
|
|
85
|
+
throw (0, core_1.getError)({
|
|
86
|
+
message: `[EventBus][register] Invalid handler.key | name: ${name} | Got: ${key}`,
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
return {
|
|
90
|
+
invokeHandler: ({ event }) => container.get({ key }).handle({ event }),
|
|
91
|
+
description: `binding-key:${key}`,
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
case constants_1.EventHandlerTypes.FUNCTION: {
|
|
95
|
+
const { fn } = handler;
|
|
96
|
+
if (typeof fn !== 'function') {
|
|
97
|
+
throw (0, core_1.getError)({
|
|
98
|
+
message: `[EventBus][register] Invalid handler.fn | name: ${name} | Expected a function | Got: ${typeof fn}`,
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
return {
|
|
102
|
+
invokeHandler: ({ event }) => fn({ event }),
|
|
103
|
+
description: `function:${fn.name || 'anonymous'}`,
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
default: {
|
|
107
|
+
throw (0, core_1.getError)({
|
|
108
|
+
message: `[EventBus][register] Unknown handler.type | name: ${name} | Expected one of: ${[...constants_1.EventHandlerTypes.SCHEME_SET].join(', ')} | Got: ${handler.type}`,
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* `retry` is optional and per REGISTRATION, not per publish: the right window belongs to the
|
|
115
|
+
* handler's own failure mode, and two handlers on the same event name may each pick their own.
|
|
116
|
+
* Omitting it reproduces today's `EventDispatchRetry` defaults exactly. Both `maxAttempts` and the
|
|
117
|
+
* total backoff window it implies are bounded - see `EventDispatchRetry` - and exceeding either
|
|
118
|
+
* throws here, at boot where a human sees it, rather than being silently clamped at dispatch time.
|
|
119
|
+
*/
|
|
20
120
|
register(opts) {
|
|
21
|
-
const { name,
|
|
22
|
-
if ((0, isEmpty_1.default)(name)
|
|
121
|
+
const { name, handler, retry } = opts;
|
|
122
|
+
if ((0, isEmpty_1.default)(name)) {
|
|
23
123
|
throw (0, core_1.getError)({
|
|
24
|
-
message: `[EventBus][register] Invalid registration | name: ${String(name)}
|
|
124
|
+
message: `[EventBus][register] Invalid registration | name: ${String(name)}`,
|
|
25
125
|
});
|
|
26
126
|
}
|
|
27
|
-
const
|
|
127
|
+
const maxAttempts = retry?.maxAttempts ?? constants_1.EventDispatchRetry.MAX_ATTEMPTS;
|
|
128
|
+
const baseDelayMs = retry?.baseDelayMs ?? constants_1.EventDispatchRetry.BASE_DELAY_MS;
|
|
129
|
+
EventBus.assertValidRetry({ name, maxAttempts, baseDelayMs });
|
|
130
|
+
const { invokeHandler, description } = EventBus.buildHandlerInvoker({
|
|
131
|
+
container: this.container,
|
|
132
|
+
handler,
|
|
133
|
+
name,
|
|
134
|
+
});
|
|
135
|
+
const registration = {
|
|
136
|
+
invokeHandler,
|
|
137
|
+
description,
|
|
138
|
+
maxAttempts,
|
|
139
|
+
baseDelayMs,
|
|
140
|
+
};
|
|
141
|
+
const existing = this.registrationsByName.get(name);
|
|
28
142
|
if (existing) {
|
|
29
|
-
existing.push(
|
|
143
|
+
existing.push(registration);
|
|
30
144
|
return;
|
|
31
145
|
}
|
|
32
|
-
this.
|
|
146
|
+
this.registrationsByName.set(name, [registration]);
|
|
33
147
|
}
|
|
34
148
|
/**
|
|
35
149
|
* Never blocks and never throws back to the caller: dispatch runs outside this call's flow via
|
|
@@ -44,55 +158,61 @@ class EventBus extends core_1.BaseHelper {
|
|
|
44
158
|
occurredAt: new Date().toISOString(),
|
|
45
159
|
traceId,
|
|
46
160
|
};
|
|
47
|
-
const
|
|
48
|
-
if (!
|
|
161
|
+
const registrations = this.registrationsByName.get(name);
|
|
162
|
+
if (!registrations?.length) {
|
|
49
163
|
// A typo is already a compile error, so at runtime this only means nobody is listening yet -
|
|
50
164
|
// a legitimate state on a fire-and-forget bus, not an incident.
|
|
51
165
|
this.logger.debug('[publish] No handler registered | name: %s | traceId: %s', name, traceId);
|
|
52
166
|
return;
|
|
53
167
|
}
|
|
54
|
-
for (const
|
|
168
|
+
for (const registration of registrations) {
|
|
55
169
|
queueMicrotask(() => {
|
|
56
170
|
// `dispatch` catches every failure path itself and never rejects - this `.catch` is an
|
|
57
171
|
// unreachable backstop, kept (and logged) rather than left floating, for the day it isn't.
|
|
58
|
-
this.dispatch({ event,
|
|
172
|
+
this.dispatch({ event, registration }).catch((error) => {
|
|
59
173
|
this.logger.error('[publish] Unexpected dispatch failure | name: %s | Error: %s', event.name, error);
|
|
60
174
|
});
|
|
61
175
|
});
|
|
62
176
|
}
|
|
63
177
|
}
|
|
64
178
|
/**
|
|
65
|
-
*
|
|
66
|
-
*
|
|
67
|
-
*
|
|
179
|
+
* Attempts/backoff come from the registration (`EventDispatchRetry`'s defaults when omitted),
|
|
180
|
+
* always with full jitter - `RetryJitterModes.FULL` decorrelates a shared-resource pile-up hardest,
|
|
181
|
+
* so many handlers retrying the same lock do not collide on the same instant a second and third
|
|
182
|
+
* time. `invokeHandler` already encodes whether that re-resolves (`BINDING_KEY`) or was captured
|
|
183
|
+
* once (`FUNCTION`) - see `buildHandlerInvoker` - so this method calls it uniformly. A handler that
|
|
184
|
+
* throws synchronously is caught exactly like one that returns a rejected promise: `execution`
|
|
185
|
+
* here is itself `async`, and JS converts a synchronous throw inside an async function body into a
|
|
186
|
+
* rejection of that function's own promise - `RetryHelper.executeWithRetry`'s attempt loop never
|
|
187
|
+
* sees the difference.
|
|
68
188
|
*
|
|
69
189
|
* `RetryHelper.executeWithRetry` is used without its own `logger` - its internal per-attempt/exhaustion
|
|
70
190
|
* logging knows nothing about the event name or `traceId`, so it cannot be the trace anyone
|
|
71
191
|
* relies on. The one line below, logged only on final failure, is.
|
|
72
192
|
*/
|
|
73
193
|
async dispatch(opts) {
|
|
74
|
-
const { event,
|
|
194
|
+
const { event, registration } = opts;
|
|
195
|
+
const { invokeHandler, description, maxAttempts, baseDelayMs } = registration;
|
|
75
196
|
let lastAttempt = 0;
|
|
76
197
|
try {
|
|
77
198
|
await core_1.RetryHelper.executeWithRetry({
|
|
78
199
|
operation: `EventBus.dispatch:${event.name}`,
|
|
79
|
-
maxAttempts
|
|
200
|
+
maxAttempts,
|
|
80
201
|
backoff: {
|
|
81
202
|
strategy: core_1.RetryBackoffStrategies.EXPONENTIAL,
|
|
82
|
-
initialDelayMs:
|
|
83
|
-
jitter: core_1.RetryJitterModes.
|
|
203
|
+
initialDelayMs: baseDelayMs,
|
|
204
|
+
jitter: core_1.RetryJitterModes.FULL,
|
|
84
205
|
},
|
|
85
206
|
execution: async ({ attempt }) => {
|
|
86
207
|
lastAttempt = attempt;
|
|
87
|
-
|
|
88
|
-
await handler.handle({ event });
|
|
208
|
+
await invokeHandler({ event });
|
|
89
209
|
},
|
|
90
210
|
});
|
|
91
211
|
}
|
|
92
212
|
catch (error) {
|
|
93
213
|
// The only trace this failure leaves: it is caught here and never reaches the publisher, so
|
|
94
214
|
// the event name, traceId and attempt count all have to be on this one line.
|
|
95
|
-
this.logger.error('[dispatch] Handler exhausted retries | name: %s | traceId: %s | attempts: %d |
|
|
215
|
+
this.logger.error('[dispatch] Handler exhausted retries | name: %s | traceId: %s | attempts: %d | handler: %s | Error: %s', event.name, event.traceId, lastAttempt, description, error);
|
|
96
216
|
}
|
|
97
217
|
}
|
|
98
218
|
}
|