-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiris.server.ts
175 lines (153 loc) · 5.32 KB
/
iris.server.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import * as iris from '@iris-events/iris'
import { Logger, UseFilters } from '@nestjs/common'
import {
BaseRpcContext,
type CustomTransportStrategy,
MessagePattern,
Server,
} from '@nestjs/microservices'
import type { ConsumeMessage } from 'amqplib'
import type {
DiscoveredProcessMessageHandlerMetadataI,
Fn,
IrisOptionsI,
} from './interfaces'
import { AmqpMessageParam, MDCParam, MessageParam } from './iris.decorators'
import { IrisDiscovery } from './iris.discovery'
import { PropagadeRpcExceptionFilterInstance } from './nest-rpc.error'
import { describeHandler } from './util'
export class IrisContext extends BaseRpcContext<Record<string, any>> {}
export class IrisServer extends Server implements CustomTransportStrategy {
protected handlers: DiscoveredProcessMessageHandlerMetadataI[]
protected readonly TAG = IrisServer.name
constructor(
protected readonly discovery: IrisDiscovery,
protected readonly logger: Logger,
protected readonly options?: IrisOptionsI,
) {
super()
this.handlers = this.discovery.findIrisHandlers()
this.decorateHandlers()
}
async listen(
callback: (err?: unknown, ...optionalParams: unknown[]) => void,
): Promise<void> {
this.logger.debug(this.TAG, 'listen()')
this.start()
.then(() => callback())
.catch((err) => callback(err))
}
async close(): Promise<void> {
if (!iris.connection.isDisconnected()) {
this.logger.log(this.TAG, 'Closing AMQP connection.')
await iris.connection.disconnect()
}
}
async start(): Promise<void> {
await iris.connection.connect(
this.options ?? {
urlOrOpts: 'amqp://localhost',
},
)
await iris.registerProcessed.register(
iris.collectProcessedMessages(),
this.handlers.map(({ meta }) => meta),
)
}
protected decorateHandlers(): void {
this.autoDecorateKnownHandlerArguments(this.handlers)
this.decorateMessageHandlers(this.handlers)
}
/**
* Decorate all arguments found in the handlers that relate
* to classes Message/AmqpMessage classes with
* @Message/@AmqpMessage param decoratros
*/
private autoDecorateKnownHandlerArguments(
messageHandlers: DiscoveredProcessMessageHandlerMetadataI[],
): void {
for (const handler of messageHandlers) {
const { meta } = handler
const handlerClass = <Fn>handler.instanceWrapper.token
const target = handlerClass.prototype
const propertyKey = meta.methodName
const methodArgs = <Fn[]>(
Reflect.getMetadata('design:paramtypes', target, propertyKey)
)
for (let pos = 0; pos < methodArgs.length; pos++) {
const arg = methodArgs[pos]
if (iris.isMessageDecoratedClass(arg)) {
const msgMeta = iris.getMessageDecoratedClass(arg)
MessageParam(msgMeta)(target, propertyKey, pos)
} else if (iris.paramDecorators.isAmqpMessageClass(arg)) {
AmqpMessageParam()(target, propertyKey, pos)
} else if (iris.mdc.isMDCClass(arg)) {
MDCParam()(target, propertyKey, pos)
}
}
}
}
/**
* Decorate all message handlers with
* - `MessagePattern`
* so that they are registered and available via `this.getHandlers()`
* - `UseFilters(PropagatedExceptionFilter)`
* so that errors thrown by handlers are propagated to the iris module
* and not handled by Nest's exception filter
*/
private decorateMessageHandlers(
messageHandlers: DiscoveredProcessMessageHandlerMetadataI[],
): void {
for (const handler of messageHandlers) {
const { meta } = handler
const instance = handler.instanceWrapper.instance
if (meta.isStaticMethod) {
this.logger.error(
this.TAG,
`Static method can not be a message handler: ${describeHandler(
meta,
)}`,
)
throw new Error('IRIS_STATIC_METHOD_MESSAGE_HANDLER_FOUND')
}
const pattern = this.getHandlerPatternForExchange(meta)
this.logger.debug(
this.TAG,
`Decorating ${describeHandler(meta)} handler (pattern = ${pattern})`,
)
MessagePattern(pattern)(instance, meta.methodName, meta.descriptor)
UseFilters(PropagadeRpcExceptionFilterInstance)(
instance,
meta.methodName,
meta.descriptor,
)
// intercept messages and consume them via decorated handlers
// in order to support Nest's pipeline
meta.callback = async (ctx: unknown) =>
await this.handleIrisEvent(<ConsumeMessage>ctx)
}
}
private async handleIrisEvent(ctx: ConsumeMessage): Promise<unknown> {
const pattern = ctx.fields.consumerTag
const handler = this.getHandlerByPattern(pattern)
if (handler == null) {
this.logger.error(this.TAG, `No handler found for ${pattern}`)
throw new Error('IRIS_NO_HANDLER_FOUND')
}
this.logger.debug(this.TAG, `Handling event for ${pattern}`)
return new Promise((resolve, reject) => {
this.transformToObservable(handler(ctx, new IrisContext(ctx))).subscribe({
next: (resp) => resolve(resp),
error: (err) => reject(err),
})
})
}
protected getHandlerPatternForExchange(
handlerMeta: iris.ProcessedMessageHandlerMetadataI,
): string {
return iris.consume.getConsumerTag(
iris.getProcessedMessageDecoratedClass(handlerMeta.messageClass),
handlerMeta,
)
}
}