@asenajs/asena 0.7.1 → 0.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -2
- package/dist/lib/adapter/AsenaAdapter.d.ts +2 -2
- package/dist/lib/adapter/AsenaAdapter.js.map +1 -1
- package/dist/lib/adapter/types/ServeOptions.d.ts +16 -0
- package/dist/lib/ioc/Container.d.ts +25 -0
- package/dist/lib/ioc/Container.js +47 -0
- package/dist/lib/ioc/Container.js.map +1 -1
- package/dist/lib/ioc/CoreContainer.d.ts +5 -3
- package/dist/lib/ioc/CoreContainer.js +9 -3
- package/dist/lib/ioc/CoreContainer.js.map +1 -1
- package/dist/lib/ioc/IocEngine.js +3 -1
- package/dist/lib/ioc/IocEngine.js.map +1 -1
- package/dist/lib/ioc/component/decorators/Inject.js +16 -0
- package/dist/lib/ioc/component/decorators/Inject.js.map +1 -1
- package/dist/lib/ioc/constants/ComponentConstants.d.ts +4 -0
- package/dist/lib/ioc/constants/ComponentConstants.js +5 -0
- package/dist/lib/ioc/constants/ComponentConstants.js.map +1 -1
- package/dist/lib/ioc/types/ICoreServiceNamesType.d.ts +1 -0
- package/dist/lib/ioc/types/ICoreServiceNamesType.js +1 -0
- package/dist/lib/ioc/types/ICoreServiceNamesType.js.map +1 -1
- package/dist/lib/ioc/types/InjectableComponent.d.ts +10 -0
- package/dist/lib/ioc/types/decorators/ComponentType.d.ts +2 -1
- package/dist/lib/ioc/types/decorators/ComponentType.js +1 -0
- package/dist/lib/ioc/types/decorators/ComponentType.js.map +1 -1
- package/dist/lib/server/AsenaServer.d.ts +46 -3
- package/dist/lib/server/AsenaServer.js +158 -28
- package/dist/lib/server/AsenaServer.js.map +1 -1
- package/dist/lib/server/AsenaServerFactory.d.ts +38 -3
- package/dist/lib/server/AsenaServerFactory.js +21 -2
- package/dist/lib/server/AsenaServerFactory.js.map +1 -1
- package/dist/lib/server/config/AsenaConfig.d.ts +82 -4
- package/dist/lib/server/config/AsenaConfig.js +39 -1
- package/dist/lib/server/config/AsenaConfig.js.map +1 -1
- package/dist/lib/server/decorators/components/MessageController.d.ts +49 -0
- package/dist/lib/server/decorators/components/MessageController.js +62 -0
- package/dist/lib/server/decorators/components/MessageController.js.map +1 -0
- package/dist/lib/server/decorators/components/index.d.ts +1 -0
- package/dist/lib/server/decorators/components/index.js +1 -0
- package/dist/lib/server/decorators/components/index.js.map +1 -1
- package/dist/lib/server/event/EventDispatchService.d.ts +3 -7
- package/dist/lib/server/event/EventDispatchService.js +9 -40
- package/dist/lib/server/event/EventDispatchService.js.map +1 -1
- package/dist/lib/server/event/PatternHandlerIndex.d.ts +53 -0
- package/dist/lib/server/event/PatternHandlerIndex.js +100 -0
- package/dist/lib/server/event/PatternHandlerIndex.js.map +1 -0
- package/dist/lib/server/event/decorators/On.d.ts +4 -0
- package/dist/lib/server/event/decorators/On.js +6 -0
- package/dist/lib/server/event/decorators/On.js.map +1 -1
- package/dist/lib/server/event/index.d.ts +1 -0
- package/dist/lib/server/event/index.js +2 -0
- package/dist/lib/server/event/index.js.map +1 -1
- package/dist/lib/server/event/types.d.ts +12 -0
- package/dist/lib/server/health/HealthServer.d.ts +26 -0
- package/dist/lib/server/health/HealthServer.js +63 -0
- package/dist/lib/server/health/HealthServer.js.map +1 -0
- package/dist/lib/server/health/index.d.ts +1 -0
- package/dist/lib/server/health/index.js +2 -0
- package/dist/lib/server/health/index.js.map +1 -0
- package/dist/lib/server/messaging/Ulak.d.ts +156 -0
- package/dist/lib/server/messaging/Ulak.js +174 -0
- package/dist/lib/server/messaging/Ulak.js.map +1 -1
- package/dist/lib/server/messaging/types.d.ts +5 -1
- package/dist/lib/server/messaging/types.js +5 -0
- package/dist/lib/server/messaging/types.js.map +1 -1
- package/dist/lib/server/microservice/InMemoryTransport.d.ts +45 -0
- package/dist/lib/server/microservice/InMemoryTransport.js +124 -0
- package/dist/lib/server/microservice/InMemoryTransport.js.map +1 -0
- package/dist/lib/server/microservice/MicroserviceTransport.d.ts +132 -0
- package/dist/lib/server/microservice/MicroserviceTransport.js +2 -0
- package/dist/lib/server/microservice/MicroserviceTransport.js.map +1 -0
- package/dist/lib/server/microservice/decorators/EventPattern.d.ts +38 -0
- package/dist/lib/server/microservice/decorators/EventPattern.js +59 -0
- package/dist/lib/server/microservice/decorators/EventPattern.js.map +1 -0
- package/dist/lib/server/microservice/decorators/MessagePattern.d.ts +31 -0
- package/dist/lib/server/microservice/decorators/MessagePattern.js +55 -0
- package/dist/lib/server/microservice/decorators/MessagePattern.js.map +1 -0
- package/dist/lib/server/microservice/decorators/index.d.ts +2 -0
- package/dist/lib/server/microservice/decorators/index.js +3 -0
- package/dist/lib/server/microservice/decorators/index.js.map +1 -0
- package/dist/lib/server/microservice/index.d.ts +5 -0
- package/dist/lib/server/microservice/index.js +11 -0
- package/dist/lib/server/microservice/index.js.map +1 -0
- package/dist/lib/server/microservice/interceptorChain.d.ts +20 -0
- package/dist/lib/server/microservice/interceptorChain.js +39 -0
- package/dist/lib/server/microservice/interceptorChain.js.map +1 -0
- package/dist/lib/server/microservice/types.d.ts +142 -0
- package/dist/lib/server/microservice/types.js +5 -0
- package/dist/lib/server/microservice/types.js.map +1 -0
- package/dist/lib/server/src/services/PrepareEventService.d.ts +4 -1
- package/dist/lib/server/src/services/PrepareEventService.js +10 -3
- package/dist/lib/server/src/services/PrepareEventService.js.map +1 -1
- package/dist/lib/server/src/services/PrepareMicroserviceService.d.ts +66 -0
- package/dist/lib/server/src/services/PrepareMicroserviceService.js +193 -0
- package/dist/lib/server/src/services/PrepareMicroserviceService.js.map +1 -0
- package/dist/lib/test/factory/deepMock.d.ts +25 -0
- package/dist/lib/test/factory/deepMock.js +105 -0
- package/dist/lib/test/factory/deepMock.js.map +1 -0
- package/dist/lib/test/factory/ulakStub.d.ts +44 -0
- package/dist/lib/test/factory/ulakStub.js +40 -0
- package/dist/lib/test/factory/ulakStub.js.map +1 -0
- package/dist/lib/test/harness/createTestApp.d.ts +33 -0
- package/dist/lib/test/harness/createTestApp.js +105 -0
- package/dist/lib/test/harness/createTestApp.js.map +1 -0
- package/dist/lib/test/harness/createWebTest.d.ts +28 -0
- package/dist/lib/test/harness/createWebTest.js +127 -0
- package/dist/lib/test/harness/createWebTest.js.map +1 -0
- package/dist/lib/test/harness/silentLogger.d.ts +27 -0
- package/dist/lib/test/harness/silentLogger.js +54 -0
- package/dist/lib/test/harness/silentLogger.js.map +1 -0
- package/dist/lib/test/harness/types.d.ts +147 -0
- package/dist/lib/test/harness/types.js +2 -0
- package/dist/lib/test/harness/types.js.map +1 -0
- package/dist/lib/test/http/TestHttpCall.d.ts +78 -0
- package/dist/lib/test/http/TestHttpCall.js +149 -0
- package/dist/lib/test/http/TestHttpCall.js.map +1 -0
- package/dist/lib/test/http/TestHttpResponse.d.ts +46 -0
- package/dist/lib/test/http/TestHttpResponse.js +70 -0
- package/dist/lib/test/http/TestHttpResponse.js.map +1 -0
- package/dist/lib/test/index.d.ts +9 -0
- package/dist/lib/test/index.js +8 -0
- package/dist/lib/test/index.js.map +1 -1
- package/dist/lib/test/metadata/discovery.d.ts +21 -0
- package/dist/lib/test/metadata/discovery.js +37 -8
- package/dist/lib/test/metadata/discovery.js.map +1 -1
- package/dist/lib/test/mockComponent.d.ts +25 -0
- package/dist/lib/test/mockComponent.js +39 -6
- package/dist/lib/test/mockComponent.js.map +1 -1
- package/dist/lib/test/types.d.ts +13 -0
- package/package.json +11 -7
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"InMemoryTransport.js","sourceRoot":"","sources":["../../../../lib/server/microservice/InMemoryTransport.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC;AAC9D,OAAO,EAAE,mBAAmB,EAAE,MAAM,8BAA8B,CAAC;AAUnE;;;;;;;;;;;;;;;;;;GAkBG;AACH,MAAM,OAAO,iBAAiB;IACZ,IAAI,GAAG,WAAW,CAAC;IAEnC;;OAEG;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+1,132 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @description Context passed to every microservice message/event handler.
|
|
3
|
+
* Carries transport-level metadata about the incoming message.
|
|
4
|
+
*/
|
|
5
|
+
export interface MessageContext {
|
|
6
|
+
/**
|
|
7
|
+
* Concrete pattern of the incoming message (e.g. 'order.create')
|
|
8
|
+
*/
|
|
9
|
+
pattern: string;
|
|
10
|
+
/**
|
|
11
|
+
* Transport-assigned unique id of the message (e.g. Redis stream entry id).
|
|
12
|
+
* Useful for idempotency/deduplication in at-least-once transports.
|
|
13
|
+
*/
|
|
14
|
+
messageId: string;
|
|
15
|
+
/**
|
|
16
|
+
* Correlation id, present only for request/response messages
|
|
17
|
+
*/
|
|
18
|
+
correlationId?: string;
|
|
19
|
+
/**
|
|
20
|
+
* Message headers (trace context, user metadata, ...)
|
|
21
|
+
*/
|
|
22
|
+
headers: Record<string, string>;
|
|
23
|
+
/**
|
|
24
|
+
* Producer-side timestamp (epoch millis)
|
|
25
|
+
*/
|
|
26
|
+
timestamp: number;
|
|
27
|
+
/**
|
|
28
|
+
* Delivery attempt number, starting at 1.
|
|
29
|
+
* Greater than 1 means the message was redelivered (at-least-once transports).
|
|
30
|
+
*/
|
|
31
|
+
attempt: number;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* @description Handler for request/response messages (@MessagePattern).
|
|
35
|
+
* The return value is serialized and sent back to the caller as the reply.
|
|
36
|
+
*/
|
|
37
|
+
export type MessageHandler = (data: any, context: MessageContext) => any | Promise<any>;
|
|
38
|
+
/**
|
|
39
|
+
* @description Handler for fire-and-forget events (@EventPattern)
|
|
40
|
+
*/
|
|
41
|
+
export type EventPatternHandler = (data: any, context: MessageContext) => void | Promise<void>;
|
|
42
|
+
/**
|
|
43
|
+
* @description Options for request/response sends
|
|
44
|
+
*/
|
|
45
|
+
export interface SendOptions {
|
|
46
|
+
/**
|
|
47
|
+
* Reply timeout in milliseconds (transport default applies when omitted)
|
|
48
|
+
*/
|
|
49
|
+
timeout?: number;
|
|
50
|
+
/**
|
|
51
|
+
* Extra headers attached to the outgoing message
|
|
52
|
+
*/
|
|
53
|
+
headers?: Record<string, string>;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* @description Options for fire-and-forget emits
|
|
57
|
+
*/
|
|
58
|
+
export interface EmitOptions {
|
|
59
|
+
/**
|
|
60
|
+
* Extra headers attached to the outgoing event
|
|
61
|
+
*/
|
|
62
|
+
headers?: Record<string, string>;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* @description Options for transport shutdown
|
|
66
|
+
*/
|
|
67
|
+
export interface DestroyOptions {
|
|
68
|
+
/**
|
|
69
|
+
* Max time in milliseconds to wait for in-flight handlers to finish
|
|
70
|
+
* before closing connections (graceful drain)
|
|
71
|
+
*/
|
|
72
|
+
drainTimeout?: number;
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* @description Broker-agnostic microservice transport SPI.
|
|
76
|
+
*
|
|
77
|
+
* A transport carries two message semantics between Asena services:
|
|
78
|
+
* - request/response (@MessagePattern) — exact patterns, a reply is produced
|
|
79
|
+
* - fire-and-forget events (@EventPattern) — wildcard patterns allowed
|
|
80
|
+
*
|
|
81
|
+
* Lifecycle contract (driven by PrepareMicroserviceService):
|
|
82
|
+
* 1. registerMessageHandler / registerEventHandler — push all handler registrations
|
|
83
|
+
* 2. init() — connect broker resources; after init, send/emit must work (client-only mode)
|
|
84
|
+
* 3. listen() — start consuming. MUST only consume sources that have at least one
|
|
85
|
+
* registered handler; with zero handlers no consumer may be started (client-only mode)
|
|
86
|
+
* 4. destroy() — graceful drain, then release all resources
|
|
87
|
+
*/
|
|
88
|
+
export interface MicroserviceTransport {
|
|
89
|
+
/**
|
|
90
|
+
* Human-readable transport name (used in logs and health reporting)
|
|
91
|
+
*/
|
|
92
|
+
readonly name: string;
|
|
93
|
+
/**
|
|
94
|
+
* Whether the transport currently holds a live broker connection.
|
|
95
|
+
* Read by the health endpoint.
|
|
96
|
+
*/
|
|
97
|
+
readonly isConnected: boolean;
|
|
98
|
+
/**
|
|
99
|
+
* Connect broker resources. Called once before listen().
|
|
100
|
+
* After init() resolves, send()/emit() must be usable (client-only mode).
|
|
101
|
+
*/
|
|
102
|
+
init(): Promise<void>;
|
|
103
|
+
/**
|
|
104
|
+
* Register a request/response handler. Patterns must be exact (no wildcards).
|
|
105
|
+
*/
|
|
106
|
+
registerMessageHandler(pattern: string, handler: MessageHandler): void;
|
|
107
|
+
/**
|
|
108
|
+
* Register a fire-and-forget event handler. Patterns arrive with the
|
|
109
|
+
* @MessageController prefix already resolved and may contain wildcards
|
|
110
|
+
* (e.g. 'payment.*').
|
|
111
|
+
*/
|
|
112
|
+
registerEventHandler(pattern: string, handler: EventPatternHandler): void;
|
|
113
|
+
/**
|
|
114
|
+
* Start consuming registered patterns.
|
|
115
|
+
* Contract: only sources with at least one registered handler are consumed;
|
|
116
|
+
* with zero handlers no consumer loop is started.
|
|
117
|
+
*/
|
|
118
|
+
listen(): Promise<void>;
|
|
119
|
+
/**
|
|
120
|
+
* Send a request and await its reply (request/response semantics)
|
|
121
|
+
*/
|
|
122
|
+
send<T = unknown>(pattern: string, data?: unknown, options?: SendOptions): Promise<T>;
|
|
123
|
+
/**
|
|
124
|
+
* Emit a fire-and-forget event
|
|
125
|
+
*/
|
|
126
|
+
emit(pattern: string, data?: unknown, options?: EmitOptions): Promise<void>;
|
|
127
|
+
/**
|
|
128
|
+
* Gracefully shut down: stop consuming, drain in-flight handlers,
|
|
129
|
+
* reject pending sends, release connections.
|
|
130
|
+
*/
|
|
131
|
+
destroy(options?: DestroyOptions): Promise<void>;
|
|
132
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"MicroserviceTransport.js","sourceRoot":"","sources":["../../../../lib/server/microservice/MicroserviceTransport.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import type { EventPatternParams } from '../types';
|
|
2
|
+
/**
|
|
3
|
+
* @description EventPattern decorator - Marks a method as a fire-and-forget event handler
|
|
4
|
+
*
|
|
5
|
+
* Methods decorated with @EventPattern will be automatically registered into the
|
|
6
|
+
* configured microservice transport when the server boots (via PrepareMicroserviceService).
|
|
7
|
+
* No reply is produced; delivery is at-least-once on durable transports, so handlers
|
|
8
|
+
* should be idempotent (use context.messageId for deduplication).
|
|
9
|
+
*
|
|
10
|
+
* The controller prefix is joined onto the pattern ('order' + 'created' →
|
|
11
|
+
* 'order.created'), matching what ulak.messages('order').emit('created') publishes.
|
|
12
|
+
* Pass `prefix: false` when the pattern is absolute — another service's event
|
|
13
|
+
* vocabulary, a Kafka external topic name, or a global catch-all.
|
|
14
|
+
* Wildcards are supported: 'payment.*', '*.completed', 'user.*.created'.
|
|
15
|
+
* Note that under a prefix, '*' becomes 'order.*' and is no longer a global catch-all.
|
|
16
|
+
*
|
|
17
|
+
* @param params - Event pattern or configuration object
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* @MessageController('order')
|
|
21
|
+
* export class OrderHandler {
|
|
22
|
+
* @EventPattern('created') // Handles 'order.created'
|
|
23
|
+
* async onCreated(event: OrderEvent, context: MessageContext) { }
|
|
24
|
+
*
|
|
25
|
+
* // Absolute - another service's event vocabulary
|
|
26
|
+
* @EventPattern({ pattern: 'payment.completed', prefix: false })
|
|
27
|
+
* async onPaymentCompleted(event: PaymentEvent, context: MessageContext) {
|
|
28
|
+
* await this.orderService.markPaid(event.orderId);
|
|
29
|
+
* }
|
|
30
|
+
*
|
|
31
|
+
* @EventPattern('*') // Wildcards allowed - handles 'order.*'
|
|
32
|
+
* async onAnyOrderEvent(event: any, context: MessageContext) { }
|
|
33
|
+
*
|
|
34
|
+
* @EventPattern({ pattern: 'stock.depleted', skip: true }) // Temporarily disabled
|
|
35
|
+
* async onStockDepleted(event: any, context: MessageContext) { }
|
|
36
|
+
* }
|
|
37
|
+
*/
|
|
38
|
+
export declare function EventPattern(params: EventPatternParams | string): MethodDecorator;
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { ComponentConstants } from '../../../ioc';
|
|
2
|
+
import { defineTypedMetadata, getOwnTypedMetadata } from '../../../utils';
|
|
3
|
+
/**
|
|
4
|
+
* @description EventPattern decorator - Marks a method as a fire-and-forget event handler
|
|
5
|
+
*
|
|
6
|
+
* Methods decorated with @EventPattern will be automatically registered into the
|
|
7
|
+
* configured microservice transport when the server boots (via PrepareMicroserviceService).
|
|
8
|
+
* No reply is produced; delivery is at-least-once on durable transports, so handlers
|
|
9
|
+
* should be idempotent (use context.messageId for deduplication).
|
|
10
|
+
*
|
|
11
|
+
* The controller prefix is joined onto the pattern ('order' + 'created' →
|
|
12
|
+
* 'order.created'), matching what ulak.messages('order').emit('created') publishes.
|
|
13
|
+
* Pass `prefix: false` when the pattern is absolute — another service's event
|
|
14
|
+
* vocabulary, a Kafka external topic name, or a global catch-all.
|
|
15
|
+
* Wildcards are supported: 'payment.*', '*.completed', 'user.*.created'.
|
|
16
|
+
* Note that under a prefix, '*' becomes 'order.*' and is no longer a global catch-all.
|
|
17
|
+
*
|
|
18
|
+
* @param params - Event pattern or configuration object
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* @MessageController('order')
|
|
22
|
+
* export class OrderHandler {
|
|
23
|
+
* @EventPattern('created') // Handles 'order.created'
|
|
24
|
+
* async onCreated(event: OrderEvent, context: MessageContext) { }
|
|
25
|
+
*
|
|
26
|
+
* // Absolute - another service's event vocabulary
|
|
27
|
+
* @EventPattern({ pattern: 'payment.completed', prefix: false })
|
|
28
|
+
* async onPaymentCompleted(event: PaymentEvent, context: MessageContext) {
|
|
29
|
+
* await this.orderService.markPaid(event.orderId);
|
|
30
|
+
* }
|
|
31
|
+
*
|
|
32
|
+
* @EventPattern('*') // Wildcards allowed - handles 'order.*'
|
|
33
|
+
* async onAnyOrderEvent(event: any, context: MessageContext) { }
|
|
34
|
+
*
|
|
35
|
+
* @EventPattern({ pattern: 'stock.depleted', skip: true }) // Temporarily disabled
|
|
36
|
+
* async onStockDepleted(event: any, context: MessageContext) { }
|
|
37
|
+
* }
|
|
38
|
+
*/
|
|
39
|
+
export function EventPattern(params) {
|
|
40
|
+
// Normalize params - support both object and string
|
|
41
|
+
const _params = typeof params === 'string' ? { pattern: params, skip: false } : params;
|
|
42
|
+
return function (target, propertyKey, descriptor) {
|
|
43
|
+
// Get existing handlers metadata (other @MessagePattern/@EventPattern methods)
|
|
44
|
+
const handlers = getOwnTypedMetadata(ComponentConstants.MessageHandlersKey, target.constructor) || {};
|
|
45
|
+
// Add this handler's metadata
|
|
46
|
+
handlers[propertyKey.toString()] = {
|
|
47
|
+
pattern: _params.pattern,
|
|
48
|
+
type: 'event',
|
|
49
|
+
methodName: propertyKey.toString(),
|
|
50
|
+
// `!== false` covers the string shorthand and objects that omit the flag
|
|
51
|
+
prefix: _params.prefix !== false,
|
|
52
|
+
skip: _params.skip || false,
|
|
53
|
+
};
|
|
54
|
+
// Store updated metadata
|
|
55
|
+
defineTypedMetadata(ComponentConstants.MessageHandlersKey, handlers, target.constructor);
|
|
56
|
+
return descriptor;
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
//# sourceMappingURL=EventPattern.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"EventPattern.js","sourceRoot":"","sources":["../../../../../lib/server/microservice/decorators/EventPattern.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAE,MAAM,cAAc,CAAC;AAClD,OAAO,EAAE,mBAAmB,EAAE,mBAAmB,EAAE,MAAM,gBAAgB,CAAC;AAG1E;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAmCG;AACH,MAAM,UAAU,YAAY,CAAC,MAAmC;IAC9D,oDAAoD;IACpD,MAAM,OAAO,GAAG,OAAO,MAAM,KAAK,QAAQ,CAAC,CAAC,CAAC,EAAE,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,KAAK,EAAE,CAAC,CAAC,CAAC,MAAM,CAAC;IAEvF,OAAO,UAAU,MAAc,EAAE,WAA4B,EAAE,UAA8B;QAC3F,+EAA+E;QAC/E,MAAM,QAAQ,GACZ,mBAAmB,CACjB,kBAAkB,CAAC,kBAAkB,EACrC,MAAM,CAAC,WAAW,CACnB,IAAI,EAAE,CAAC;QAEV,8BAA8B;QAC9B,QAAQ,CAAC,WAAW,CAAC,QAAQ,EAAE,CAAC,GAAG;YACjC,OAAO,EAAE,OAAO,CAAC,OAAO;YACxB,IAAI,EAAE,OAAO;YACb,UAAU,EAAE,WAAW,CAAC,QAAQ,EAAE;YAClC,yEAAyE;YACzE,MAAM,EAAE,OAAO,CAAC,MAAM,KAAK,KAAK;YAChC,IAAI,EAAE,OAAO,CAAC,IAAI,IAAI,KAAK;SAC5B,CAAC;QAEF,yBAAyB;QACzB,mBAAmB,CAAC,kBAAkB,CAAC,kBAAkB,EAAE,QAAQ,EAAE,MAAM,CAAC,WAAW,CAAC,CAAC;QAEzF,OAAO,UAAU,CAAC;IACpB,CAAC,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import type { MessagePatternParams } from '../types';
|
|
2
|
+
/**
|
|
3
|
+
* @description MessagePattern decorator - Marks a method as a request/response handler
|
|
4
|
+
*
|
|
5
|
+
* Methods decorated with @MessagePattern will be automatically registered into the
|
|
6
|
+
* configured microservice transport when the server boots (via PrepareMicroserviceService).
|
|
7
|
+
* The method's return value is sent back to the caller as the reply.
|
|
8
|
+
*
|
|
9
|
+
* The controller prefix is joined onto the pattern ('order' + 'create' → 'order.create').
|
|
10
|
+
* Pass `prefix: false` to register the pattern verbatim.
|
|
11
|
+
* Wildcards are NOT allowed — request/response requires exact routing — and
|
|
12
|
+
* `prefix: false` does not relax that.
|
|
13
|
+
*
|
|
14
|
+
* @param params - Request pattern or configuration object
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* @MessageController('order')
|
|
18
|
+
* export class OrderHandler {
|
|
19
|
+
* @MessagePattern('create') // Handles 'order.create'
|
|
20
|
+
* async create(data: CreateOrderDto, context: MessageContext) {
|
|
21
|
+
* return this.orderService.create(data); // reply sent to caller
|
|
22
|
+
* }
|
|
23
|
+
*
|
|
24
|
+
* @MessagePattern({ pattern: 'ping', prefix: false }) // Handles 'ping'
|
|
25
|
+
* async ping(data: any, context: MessageContext) { }
|
|
26
|
+
*
|
|
27
|
+
* @MessagePattern({ pattern: 'delete', skip: true }) // Temporarily disabled
|
|
28
|
+
* async delete(data: any, context: MessageContext) { }
|
|
29
|
+
* }
|
|
30
|
+
*/
|
|
31
|
+
export declare function MessagePattern(params: MessagePatternParams | string): MethodDecorator;
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { ComponentConstants } from '../../../ioc';
|
|
2
|
+
import { defineTypedMetadata, getOwnTypedMetadata } from '../../../utils';
|
|
3
|
+
/**
|
|
4
|
+
* @description MessagePattern decorator - Marks a method as a request/response handler
|
|
5
|
+
*
|
|
6
|
+
* Methods decorated with @MessagePattern will be automatically registered into the
|
|
7
|
+
* configured microservice transport when the server boots (via PrepareMicroserviceService).
|
|
8
|
+
* The method's return value is sent back to the caller as the reply.
|
|
9
|
+
*
|
|
10
|
+
* The controller prefix is joined onto the pattern ('order' + 'create' → 'order.create').
|
|
11
|
+
* Pass `prefix: false` to register the pattern verbatim.
|
|
12
|
+
* Wildcards are NOT allowed — request/response requires exact routing — and
|
|
13
|
+
* `prefix: false` does not relax that.
|
|
14
|
+
*
|
|
15
|
+
* @param params - Request pattern or configuration object
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* @MessageController('order')
|
|
19
|
+
* export class OrderHandler {
|
|
20
|
+
* @MessagePattern('create') // Handles 'order.create'
|
|
21
|
+
* async create(data: CreateOrderDto, context: MessageContext) {
|
|
22
|
+
* return this.orderService.create(data); // reply sent to caller
|
|
23
|
+
* }
|
|
24
|
+
*
|
|
25
|
+
* @MessagePattern({ pattern: 'ping', prefix: false }) // Handles 'ping'
|
|
26
|
+
* async ping(data: any, context: MessageContext) { }
|
|
27
|
+
*
|
|
28
|
+
* @MessagePattern({ pattern: 'delete', skip: true }) // Temporarily disabled
|
|
29
|
+
* async delete(data: any, context: MessageContext) { }
|
|
30
|
+
* }
|
|
31
|
+
*/
|
|
32
|
+
export function MessagePattern(params) {
|
|
33
|
+
// Normalize params - support both object and string
|
|
34
|
+
const _params = typeof params === 'string' ? { pattern: params, skip: false } : params;
|
|
35
|
+
if (_params.pattern.includes('*')) {
|
|
36
|
+
throw new Error(`@MessagePattern('${_params.pattern}') cannot contain wildcards - request/response requires exact patterns. Use @EventPattern for wildcard subscriptions.`);
|
|
37
|
+
}
|
|
38
|
+
return function (target, propertyKey, descriptor) {
|
|
39
|
+
// Get existing handlers metadata (other @MessagePattern/@EventPattern methods)
|
|
40
|
+
const handlers = getOwnTypedMetadata(ComponentConstants.MessageHandlersKey, target.constructor) || {};
|
|
41
|
+
// Add this handler's metadata
|
|
42
|
+
handlers[propertyKey.toString()] = {
|
|
43
|
+
pattern: _params.pattern,
|
|
44
|
+
type: 'message',
|
|
45
|
+
methodName: propertyKey.toString(),
|
|
46
|
+
// `!== false` covers the string shorthand and objects that omit the flag
|
|
47
|
+
prefix: _params.prefix !== false,
|
|
48
|
+
skip: _params.skip || false,
|
|
49
|
+
};
|
|
50
|
+
// Store updated metadata
|
|
51
|
+
defineTypedMetadata(ComponentConstants.MessageHandlersKey, handlers, target.constructor);
|
|
52
|
+
return descriptor;
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
//# sourceMappingURL=MessagePattern.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"MessagePattern.js","sourceRoot":"","sources":["../../../../../lib/server/microservice/decorators/MessagePattern.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAE,MAAM,cAAc,CAAC;AAClD,OAAO,EAAE,mBAAmB,EAAE,mBAAmB,EAAE,MAAM,gBAAgB,CAAC;AAG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,11 @@
|
|
|
1
|
+
// Transport SPI
|
|
2
|
+
export * from './MicroserviceTransport';
|
|
3
|
+
// Types (params, metadata, interceptor SPI)
|
|
4
|
+
export * from './types';
|
|
5
|
+
// In-process reference transport
|
|
6
|
+
export * from './InMemoryTransport';
|
|
7
|
+
// Interceptor composition (used by Ulak and PrepareMicroserviceService)
|
|
8
|
+
export * from './interceptorChain';
|
|
9
|
+
// Decorators
|
|
10
|
+
export * from './decorators';
|
|
11
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../lib/server/microservice/index.ts"],"names":[],"mappings":"AAAA,gBAAgB;AAChB,cAAc,yBAAyB,CAAC;AAExC,4CAA4C;AAC5C,cAAc,SAAS,CAAC;AAExB,iCAAiC;AACjC,cAAc,qBAAqB,CAAC;AAEpC,wEAAwE;AACxE,cAAc,oBAAoB,CAAC;AAEnC,aAAa;AACb,cAAc,cAAc,CAAC"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { MessageContext } from './MicroserviceTransport';
|
|
2
|
+
import type { MessagingInterceptor, OutboundContext } from './types';
|
|
3
|
+
/**
|
|
4
|
+
* @description Composes interceptor onSend hooks around an outgoing operation.
|
|
5
|
+
* Interceptors run in registration order (first interceptor is outermost).
|
|
6
|
+
*
|
|
7
|
+
* @param interceptors - Registered messaging interceptors
|
|
8
|
+
* @param ctx - Outbound context (interceptors may mutate ctx.headers)
|
|
9
|
+
* @param operation - The actual transport send/emit call
|
|
10
|
+
*/
|
|
11
|
+
export declare function composeOnSend(interceptors: MessagingInterceptor[], ctx: OutboundContext, operation: () => Promise<any>): Promise<any>;
|
|
12
|
+
/**
|
|
13
|
+
* @description Composes interceptor onReceive hooks around a handler invocation.
|
|
14
|
+
* Interceptors run in registration order (first interceptor is outermost).
|
|
15
|
+
*
|
|
16
|
+
* @param interceptors - Registered messaging interceptors
|
|
17
|
+
* @param ctx - Incoming message context
|
|
18
|
+
* @param operation - The actual handler invocation
|
|
19
|
+
*/
|
|
20
|
+
export declare function composeOnReceive(interceptors: MessagingInterceptor[], ctx: MessageContext, operation: () => Promise<any>): Promise<any>;
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @description Composes interceptor onSend hooks around an outgoing operation.
|
|
3
|
+
* Interceptors run in registration order (first interceptor is outermost).
|
|
4
|
+
*
|
|
5
|
+
* @param interceptors - Registered messaging interceptors
|
|
6
|
+
* @param ctx - Outbound context (interceptors may mutate ctx.headers)
|
|
7
|
+
* @param operation - The actual transport send/emit call
|
|
8
|
+
*/
|
|
9
|
+
export function composeOnSend(interceptors, ctx, operation) {
|
|
10
|
+
let next = operation;
|
|
11
|
+
for (let i = interceptors.length - 1; i >= 0; i--) {
|
|
12
|
+
const interceptor = interceptors[i];
|
|
13
|
+
if (interceptor.onSend) {
|
|
14
|
+
const inner = next;
|
|
15
|
+
next = () => interceptor.onSend(ctx, inner);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
return next();
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* @description Composes interceptor onReceive hooks around a handler invocation.
|
|
22
|
+
* Interceptors run in registration order (first interceptor is outermost).
|
|
23
|
+
*
|
|
24
|
+
* @param interceptors - Registered messaging interceptors
|
|
25
|
+
* @param ctx - Incoming message context
|
|
26
|
+
* @param operation - The actual handler invocation
|
|
27
|
+
*/
|
|
28
|
+
export function composeOnReceive(interceptors, ctx, operation) {
|
|
29
|
+
let next = operation;
|
|
30
|
+
for (let i = interceptors.length - 1; i >= 0; i--) {
|
|
31
|
+
const interceptor = interceptors[i];
|
|
32
|
+
if (interceptor.onReceive) {
|
|
33
|
+
const inner = next;
|
|
34
|
+
next = () => interceptor.onReceive(ctx, inner);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
return next();
|
|
38
|
+
}
|
|
39
|
+
//# sourceMappingURL=interceptorChain.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"interceptorChain.js","sourceRoot":"","sources":["../../../../lib/server/microservice/interceptorChain.ts"],"names":[],"mappings":"AAGA;;;;;;;GAOG;AACH,MAAM,UAAU,aAAa,CAC3B,YAAoC,EACpC,GAAoB,EACpB,SAA6B;IAE7B,IAAI,IAAI,GAAG,SAAS,CAAC;IAErB,KAAK,IAAI,CAAC,GAAG,YAAY,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC,IAAI,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC;QAClD,MAAM,WAAW,GAAG,YAAY,CAAC,CAAC,CAAC,CAAC;QAEpC,IAAI,WAAW,CAAC,MAAM,EAAE,CAAC;YACvB,MAAM,KAAK,GAAG,IAAI,CAAC;YAEnB,IAAI,GAAG,GAAG,EAAE,CAAC,WAAW,CAAC,MAAM,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC;QAC9C,CAAC;IACH,CAAC;IAED,OAAO,IAAI,EAAE,CAAC;AAChB,CAAC;AAED;;;;;;;GAOG;AACH,MAAM,UAAU,gBAAgB,CAC9B,YAAoC,EACpC,GAAmB,EACnB,SAA6B;IAE7B,IAAI,IAAI,GAAG,SAAS,CAAC;IAErB,KAAK,IAAI,CAAC,GAAG,YAAY,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC,IAAI,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC;QAClD,MAAM,WAAW,GAAG,YAAY,CAAC,CAAC,CAAC,CAAC;QAEpC,IAAI,WAAW,CAAC,SAAS,EAAE,CAAC;YAC1B,MAAM,KAAK,GAAG,IAAI,CAAC;YAEnB,IAAI,GAAG,GAAG,EAAE,CAAC,WAAW,CAAC,SAAS,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC;QACjD,CAAC;IACH,CAAC;IAED,OAAO,IAAI,EAAE,CAAC;AAChB,CAAC"}
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
import type { ComponentParams } from '../../ioc';
|
|
2
|
+
import type { MessageContext } from './MicroserviceTransport';
|
|
3
|
+
/**
|
|
4
|
+
* @description Name used when a single (unnamed) microservice transport is configured
|
|
5
|
+
*/
|
|
6
|
+
export declare const DEFAULT_TRANSPORT_NAME = "default";
|
|
7
|
+
/**
|
|
8
|
+
* @description Parameters for @MessageController decorator
|
|
9
|
+
*/
|
|
10
|
+
export interface MessageControllerParams extends ComponentParams {
|
|
11
|
+
/**
|
|
12
|
+
* Prefix joined (with a dot) onto every @MessagePattern AND @EventPattern in
|
|
13
|
+
* this controller. A single handler opts out with `prefix: false`.
|
|
14
|
+
* Must be a literal path segment — a wildcard prefix is rejected at boot.
|
|
15
|
+
* @example 'order' turns @MessagePattern('create') into 'order.create'
|
|
16
|
+
* and @EventPattern('created') into 'order.created'
|
|
17
|
+
*/
|
|
18
|
+
prefix?: string;
|
|
19
|
+
/**
|
|
20
|
+
* Name of the microservice transport this controller binds to.
|
|
21
|
+
* Only needed when multiple named transports are configured in transport()
|
|
22
|
+
* (e.g. part of the project on Redis, part on NATS).
|
|
23
|
+
* @default 'default'
|
|
24
|
+
*/
|
|
25
|
+
transport?: string;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* @description Parameters for @MessagePattern method decorator
|
|
29
|
+
*/
|
|
30
|
+
export interface MessagePatternParams {
|
|
31
|
+
/**
|
|
32
|
+
* Request pattern to handle. Must be exact — wildcards are not allowed
|
|
33
|
+
* (request/response requires exact routing).
|
|
34
|
+
* @example 'create' (joined with the controller prefix → 'order.create')
|
|
35
|
+
*/
|
|
36
|
+
pattern: string;
|
|
37
|
+
/**
|
|
38
|
+
* Join the @MessageController prefix onto `pattern`.
|
|
39
|
+
* Set false to register the pattern verbatim — for patterns that belong to
|
|
40
|
+
* another service's vocabulary rather than this controller's.
|
|
41
|
+
* @default true
|
|
42
|
+
*/
|
|
43
|
+
prefix?: boolean;
|
|
44
|
+
/**
|
|
45
|
+
* Skip this handler (useful for temporarily disabling handlers)
|
|
46
|
+
* @default false
|
|
47
|
+
*/
|
|
48
|
+
skip?: boolean;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* @description Parameters for @EventPattern method decorator
|
|
52
|
+
*/
|
|
53
|
+
export interface EventPatternParams {
|
|
54
|
+
/**
|
|
55
|
+
* Event pattern to handle. Wildcards are supported.
|
|
56
|
+
* The controller prefix is joined on unless `prefix: false`.
|
|
57
|
+
* @example 'created' (joined with the controller prefix → 'order.created')
|
|
58
|
+
*/
|
|
59
|
+
pattern: string;
|
|
60
|
+
/**
|
|
61
|
+
* Join the @MessageController prefix onto `pattern`.
|
|
62
|
+
* Set false for absolute names: another service's event vocabulary, a Kafka
|
|
63
|
+
* external topic name, or a global catch-all ('*').
|
|
64
|
+
* @default true
|
|
65
|
+
*/
|
|
66
|
+
prefix?: boolean;
|
|
67
|
+
/**
|
|
68
|
+
* Skip this handler (useful for temporarily disabling handlers)
|
|
69
|
+
* @default false
|
|
70
|
+
*/
|
|
71
|
+
skip?: boolean;
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* @description Metadata stored for each @MessagePattern/@EventPattern method
|
|
75
|
+
* Used by PrepareMicroserviceService to register handlers into the transport
|
|
76
|
+
*/
|
|
77
|
+
export interface MessageHandlerMetadata {
|
|
78
|
+
/**
|
|
79
|
+
* Pattern as written on the decorator. The @MessageController prefix is
|
|
80
|
+
* joined on later by PrepareMicroserviceService unless `prefix` is false.
|
|
81
|
+
*/
|
|
82
|
+
pattern: string;
|
|
83
|
+
/**
|
|
84
|
+
* Handler semantics: request/response ('message') or fire-and-forget ('event')
|
|
85
|
+
*/
|
|
86
|
+
type: 'message' | 'event';
|
|
87
|
+
/**
|
|
88
|
+
* Method name to call when a message arrives
|
|
89
|
+
*/
|
|
90
|
+
methodName: string;
|
|
91
|
+
/**
|
|
92
|
+
* Whether the controller prefix is joined onto `pattern`.
|
|
93
|
+
* Normalized at decoration time (`prefix: false` opt-out → false).
|
|
94
|
+
*/
|
|
95
|
+
prefix: boolean;
|
|
96
|
+
/**
|
|
97
|
+
* Whether this handler should be skipped
|
|
98
|
+
*/
|
|
99
|
+
skip: boolean;
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* @description Context passed to MessagingInterceptor.onSend for outgoing messages
|
|
103
|
+
*/
|
|
104
|
+
export interface OutboundContext {
|
|
105
|
+
/**
|
|
106
|
+
* Full outgoing pattern (prefix already applied)
|
|
107
|
+
*/
|
|
108
|
+
pattern: string;
|
|
109
|
+
/**
|
|
110
|
+
* Whether this is a request ('send') or a fire-and-forget event ('emit')
|
|
111
|
+
*/
|
|
112
|
+
kind: 'send' | 'emit';
|
|
113
|
+
/**
|
|
114
|
+
* Mutable outgoing headers — interceptors inject metadata here (e.g. trace context)
|
|
115
|
+
*/
|
|
116
|
+
headers: Record<string, string>;
|
|
117
|
+
/**
|
|
118
|
+
* Outgoing payload (read-only by convention)
|
|
119
|
+
*/
|
|
120
|
+
data: unknown;
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* @description Zero-dependency messaging interceptor SPI.
|
|
124
|
+
*
|
|
125
|
+
* Both hooks are wrappers around the actual operation, so an interceptor can
|
|
126
|
+
* measure duration and observe errors/replies (e.g. OpenTelemetry spans).
|
|
127
|
+
* Interceptors are registered via the transport() config hook:
|
|
128
|
+
* `transport() { return { microservice, interceptors: [myInterceptor] } }`
|
|
129
|
+
*/
|
|
130
|
+
export interface MessagingInterceptor {
|
|
131
|
+
/**
|
|
132
|
+
* Wraps outgoing send/emit. Mutate ctx.headers to inject metadata,
|
|
133
|
+
* call next() to perform the operation (its promise resolves with the
|
|
134
|
+
* reply for 'send', undefined for 'emit').
|
|
135
|
+
*/
|
|
136
|
+
onSend?(ctx: OutboundContext, next: () => Promise<any>): Promise<any>;
|
|
137
|
+
/**
|
|
138
|
+
* Wraps an incoming handler invocation. Call next() to run the handler
|
|
139
|
+
* (and any inner interceptors); its promise carries the handler result.
|
|
140
|
+
*/
|
|
141
|
+
onReceive?(ctx: MessageContext, next: () => Promise<any>): Promise<any>;
|
|
142
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../../../lib/server/microservice/types.ts"],"names":[],"mappings":"AAGA;;GAEG;AACH,MAAM,CAAC,MAAM,sBAAsB,GAAG,SAAS,CAAC"}
|
|
@@ -15,6 +15,7 @@ import type { ICoreService } from '../../../ioc';
|
|
|
15
15
|
* - Prefix='user', event='created' → 'user.created'
|
|
16
16
|
* - Prefix='user', event='*.updated' → 'user.*.updated'
|
|
17
17
|
* - Prefix='user', event='' → 'user'
|
|
18
|
+
* - Handler opted out (prefix: false) → event, verbatim
|
|
18
19
|
*/
|
|
19
20
|
export declare class PrepareEventService implements ICoreService {
|
|
20
21
|
readonly serviceName = "PrepareEventService";
|
|
@@ -31,7 +32,9 @@ export declare class PrepareEventService implements ICoreService {
|
|
|
31
32
|
/**
|
|
32
33
|
* Build final event pattern from prefix and event
|
|
33
34
|
*
|
|
34
|
-
* Rules
|
|
35
|
+
* Rules (identical to PrepareMicroserviceService.buildPattern and
|
|
36
|
+
* UlakMessages.buildPattern):
|
|
37
|
+
* - Handler opted out (prefix: false) → event
|
|
35
38
|
* - No prefix → event
|
|
36
39
|
* - No event → prefix
|
|
37
40
|
* - Both → prefix.event
|
|
@@ -26,6 +26,7 @@ import { getTypedMetadata } from '../../../utils';
|
|
|
26
26
|
* - Prefix='user', event='created' → 'user.created'
|
|
27
27
|
* - Prefix='user', event='*.updated' → 'user.*.updated'
|
|
28
28
|
* - Prefix='user', event='' → 'user'
|
|
29
|
+
* - Handler opted out (prefix: false) → event, verbatim
|
|
29
30
|
*/
|
|
30
31
|
let PrepareEventService = class PrepareEventService {
|
|
31
32
|
serviceName = 'PrepareEventService';
|
|
@@ -65,7 +66,9 @@ let PrepareEventService = class PrepareEventService {
|
|
|
65
66
|
continue;
|
|
66
67
|
}
|
|
67
68
|
// Build final event pattern
|
|
68
|
-
|
|
69
|
+
// `!== false`: metadata written by a pre-0.8 decorator build carries no
|
|
70
|
+
// flag and must follow the current default (apply the prefix)
|
|
71
|
+
const finalPattern = this.buildEventPattern(prefix, metadata.pattern, metadata.prefix !== false);
|
|
69
72
|
// Bind method to service instance
|
|
70
73
|
const boundHandler = service[methodName].bind(service);
|
|
71
74
|
// Register with EventEmitter
|
|
@@ -75,12 +78,16 @@ let PrepareEventService = class PrepareEventService {
|
|
|
75
78
|
/**
|
|
76
79
|
* Build final event pattern from prefix and event
|
|
77
80
|
*
|
|
78
|
-
* Rules
|
|
81
|
+
* Rules (identical to PrepareMicroserviceService.buildPattern and
|
|
82
|
+
* UlakMessages.buildPattern):
|
|
83
|
+
* - Handler opted out (prefix: false) → event
|
|
79
84
|
* - No prefix → event
|
|
80
85
|
* - No event → prefix
|
|
81
86
|
* - Both → prefix.event
|
|
82
87
|
*/
|
|
83
|
-
buildEventPattern(prefix, event) {
|
|
88
|
+
buildEventPattern(prefix, event, applyPrefix) {
|
|
89
|
+
if (!applyPrefix)
|
|
90
|
+
return event;
|
|
84
91
|
if (!prefix)
|
|
85
92
|
return event;
|
|
86
93
|
if (!event)
|