@opra/kafka 1.0.2 → 1.0.3

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.
@@ -71,9 +71,14 @@ class KafkaAdapter extends core_1.PlatformAdapter {
71
71
  }
72
72
  /** Subscribe to channels */
73
73
  for (const args of this._handlerArgs) {
74
- const { consumer, operation, operationOptions } = args;
74
+ const { consumer, operation, operationConfig } = args;
75
75
  args.topics = Array.isArray(operation.channel) ? operation.channel : [operation.channel];
76
- await consumer.subscribe({ topics: args.topics, fromBeginning: operationOptions.fromBeginning }).catch(e => {
76
+ await consumer
77
+ .subscribe({
78
+ ...operationConfig.subscribe,
79
+ topics: args.topics,
80
+ })
81
+ .catch(e => {
77
82
  this._emitError(e);
78
83
  throw e;
79
84
  });
@@ -137,17 +142,51 @@ class KafkaAdapter extends core_1.PlatformAdapter {
137
142
  * @param operation
138
143
  * @protected
139
144
  */
140
- async _getOperationOptions(controller, instance, operation) {
145
+ async _getOperationConfig(controller, instance, operation) {
141
146
  if (typeof instance[operation.name] !== 'function')
142
147
  return;
143
148
  const proto = controller.ctor?.prototype || Object.getPrototypeOf(instance);
144
- let operationOptions = Reflect.getMetadata(constants_js_1.KAFKA_OPERATION_METADATA, proto, operation.name);
145
- const configResolver = Reflect.getMetadata(constants_js_1.KAFKA_OPERATION_METADATA_RESOLVER, proto, operation.name);
146
- if (configResolver) {
147
- const cfg = await configResolver();
148
- operationOptions = { ...operationOptions, ...cfg };
149
+ if (Reflect.hasMetadata(common_1.RPC_CONTROLLER_METADATA, proto, operation.name))
150
+ return;
151
+ const operationConfig = {
152
+ consumer: {
153
+ groupId: constants_js_1.KAFKA_DEFAULT_GROUP,
154
+ },
155
+ subscribe: {},
156
+ };
157
+ if (this._config.defaults) {
158
+ if (this._config.defaults.subscribe) {
159
+ Object.assign(operationConfig.subscribe, this._config.defaults.subscribe);
160
+ }
161
+ if (this._config.defaults.consumer) {
162
+ Object.assign(operationConfig.consumer, this._config.defaults.consumer);
163
+ }
164
+ }
165
+ let kafkaMetadata = Reflect.getMetadata(constants_js_1.KAFKA_OPERATION_METADATA, proto, operation.name);
166
+ if (!kafkaMetadata) {
167
+ const configResolver = Reflect.getMetadata(constants_js_1.KAFKA_OPERATION_METADATA_RESOLVER, proto, operation.name);
168
+ if (configResolver) {
169
+ kafkaMetadata = await configResolver();
170
+ }
149
171
  }
150
- return operationOptions;
172
+ if (kafkaMetadata) {
173
+ if (kafkaMetadata.subscribe) {
174
+ Object.assign(operationConfig.subscribe, kafkaMetadata.subscribe);
175
+ }
176
+ if (kafkaMetadata.consumer) {
177
+ if (typeof kafkaMetadata.consumer === 'object') {
178
+ Object.assign(operationConfig.consumer, kafkaMetadata.consumer);
179
+ operationConfig.selfConsumer = true;
180
+ }
181
+ else {
182
+ const x = this._config.consumers?.[kafkaMetadata.consumer];
183
+ if (x) {
184
+ Object.assign(operationConfig.consumer, x);
185
+ }
186
+ }
187
+ }
188
+ }
189
+ return operationConfig;
151
190
  }
152
191
  /**
153
192
  *
@@ -163,16 +202,15 @@ class KafkaAdapter extends core_1.PlatformAdapter {
163
202
  this._controllerInstances.set(controller, instance);
164
203
  /** Build HandlerData array */
165
204
  for (const operation of controller.operations.values()) {
166
- const operationOptions = await this._getOperationOptions(controller, instance, operation);
167
- if (!operationOptions)
205
+ const operationConfig = await this._getOperationConfig(controller, instance, operation);
206
+ if (!operationConfig)
168
207
  continue;
169
- // const consumerConfig = this._getConsumerConfig(operationOptions);
170
208
  const args = {
171
209
  consumer: null,
172
210
  controller,
173
211
  instance,
174
212
  operation,
175
- operationOptions,
213
+ operationConfig,
176
214
  handler: null,
177
215
  topics: null,
178
216
  };
@@ -191,28 +229,16 @@ class KafkaAdapter extends core_1.PlatformAdapter {
191
229
  * @protected
192
230
  */
193
231
  async _createConsumer(args) {
194
- const { operationOptions } = args;
195
- const consumerConfig = {
196
- groupId: constants_js_1.KAFKA_DEFAULT_GROUP,
197
- };
198
- let consumer;
199
- if (typeof operationOptions.consumer === 'object') {
200
- consumer = this._consumers.get(operationOptions.consumer.groupId);
201
- if (consumer) {
202
- throw new Error(`Operation consumer for groupId (${operationOptions.consumer.groupId}) already exists`);
203
- }
204
- Object.assign(consumerConfig, operationOptions.consumer);
205
- }
206
- else if (operationOptions.consumer) {
207
- const x = this._config.consumers?.[operationOptions.consumer];
208
- Object.assign(consumerConfig, { ...x, groupId: operationOptions.consumer });
232
+ const { operationConfig } = args;
233
+ let consumer = this._consumers.get(operationConfig.consumer.groupId);
234
+ if (consumer && operationConfig.selfConsumer) {
235
+ throw new Error(`Operation consumer for groupId (${operationConfig.consumer.groupId}) already exists`);
209
236
  }
210
- consumer = this._consumers.get(consumerConfig.groupId);
211
237
  /** Create consumers */
212
238
  if (!consumer) {
213
- consumer = this.kafka.consumer(consumerConfig);
214
- consumer[kGroupId] = consumerConfig.groupId;
215
- this._consumers.set(consumerConfig.groupId, consumer);
239
+ consumer = this.kafka.consumer(operationConfig.consumer);
240
+ consumer[kGroupId] = operationConfig.consumer.groupId;
241
+ this._consumers.set(operationConfig.consumer.groupId, consumer);
216
242
  }
217
243
  args.consumer = consumer;
218
244
  }
@@ -1,4 +1,4 @@
1
- import { RpcApi } from '@opra/common';
1
+ import { RPC_CONTROLLER_METADATA, RpcApi } from '@opra/common';
2
2
  import { kAssetCache, PlatformAdapter } from '@opra/core';
3
3
  import { Kafka, logLevel } from 'kafkajs';
4
4
  import { vg } from 'valgen';
@@ -68,9 +68,14 @@ export class KafkaAdapter extends PlatformAdapter {
68
68
  }
69
69
  /** Subscribe to channels */
70
70
  for (const args of this._handlerArgs) {
71
- const { consumer, operation, operationOptions } = args;
71
+ const { consumer, operation, operationConfig } = args;
72
72
  args.topics = Array.isArray(operation.channel) ? operation.channel : [operation.channel];
73
- await consumer.subscribe({ topics: args.topics, fromBeginning: operationOptions.fromBeginning }).catch(e => {
73
+ await consumer
74
+ .subscribe({
75
+ ...operationConfig.subscribe,
76
+ topics: args.topics,
77
+ })
78
+ .catch(e => {
74
79
  this._emitError(e);
75
80
  throw e;
76
81
  });
@@ -134,17 +139,51 @@ export class KafkaAdapter extends PlatformAdapter {
134
139
  * @param operation
135
140
  * @protected
136
141
  */
137
- async _getOperationOptions(controller, instance, operation) {
142
+ async _getOperationConfig(controller, instance, operation) {
138
143
  if (typeof instance[operation.name] !== 'function')
139
144
  return;
140
145
  const proto = controller.ctor?.prototype || Object.getPrototypeOf(instance);
141
- let operationOptions = Reflect.getMetadata(KAFKA_OPERATION_METADATA, proto, operation.name);
142
- const configResolver = Reflect.getMetadata(KAFKA_OPERATION_METADATA_RESOLVER, proto, operation.name);
143
- if (configResolver) {
144
- const cfg = await configResolver();
145
- operationOptions = { ...operationOptions, ...cfg };
146
+ if (Reflect.hasMetadata(RPC_CONTROLLER_METADATA, proto, operation.name))
147
+ return;
148
+ const operationConfig = {
149
+ consumer: {
150
+ groupId: KAFKA_DEFAULT_GROUP,
151
+ },
152
+ subscribe: {},
153
+ };
154
+ if (this._config.defaults) {
155
+ if (this._config.defaults.subscribe) {
156
+ Object.assign(operationConfig.subscribe, this._config.defaults.subscribe);
157
+ }
158
+ if (this._config.defaults.consumer) {
159
+ Object.assign(operationConfig.consumer, this._config.defaults.consumer);
160
+ }
161
+ }
162
+ let kafkaMetadata = Reflect.getMetadata(KAFKA_OPERATION_METADATA, proto, operation.name);
163
+ if (!kafkaMetadata) {
164
+ const configResolver = Reflect.getMetadata(KAFKA_OPERATION_METADATA_RESOLVER, proto, operation.name);
165
+ if (configResolver) {
166
+ kafkaMetadata = await configResolver();
167
+ }
146
168
  }
147
- return operationOptions;
169
+ if (kafkaMetadata) {
170
+ if (kafkaMetadata.subscribe) {
171
+ Object.assign(operationConfig.subscribe, kafkaMetadata.subscribe);
172
+ }
173
+ if (kafkaMetadata.consumer) {
174
+ if (typeof kafkaMetadata.consumer === 'object') {
175
+ Object.assign(operationConfig.consumer, kafkaMetadata.consumer);
176
+ operationConfig.selfConsumer = true;
177
+ }
178
+ else {
179
+ const x = this._config.consumers?.[kafkaMetadata.consumer];
180
+ if (x) {
181
+ Object.assign(operationConfig.consumer, x);
182
+ }
183
+ }
184
+ }
185
+ }
186
+ return operationConfig;
148
187
  }
149
188
  /**
150
189
  *
@@ -160,16 +199,15 @@ export class KafkaAdapter extends PlatformAdapter {
160
199
  this._controllerInstances.set(controller, instance);
161
200
  /** Build HandlerData array */
162
201
  for (const operation of controller.operations.values()) {
163
- const operationOptions = await this._getOperationOptions(controller, instance, operation);
164
- if (!operationOptions)
202
+ const operationConfig = await this._getOperationConfig(controller, instance, operation);
203
+ if (!operationConfig)
165
204
  continue;
166
- // const consumerConfig = this._getConsumerConfig(operationOptions);
167
205
  const args = {
168
206
  consumer: null,
169
207
  controller,
170
208
  instance,
171
209
  operation,
172
- operationOptions,
210
+ operationConfig,
173
211
  handler: null,
174
212
  topics: null,
175
213
  };
@@ -188,28 +226,16 @@ export class KafkaAdapter extends PlatformAdapter {
188
226
  * @protected
189
227
  */
190
228
  async _createConsumer(args) {
191
- const { operationOptions } = args;
192
- const consumerConfig = {
193
- groupId: KAFKA_DEFAULT_GROUP,
194
- };
195
- let consumer;
196
- if (typeof operationOptions.consumer === 'object') {
197
- consumer = this._consumers.get(operationOptions.consumer.groupId);
198
- if (consumer) {
199
- throw new Error(`Operation consumer for groupId (${operationOptions.consumer.groupId}) already exists`);
200
- }
201
- Object.assign(consumerConfig, operationOptions.consumer);
202
- }
203
- else if (operationOptions.consumer) {
204
- const x = this._config.consumers?.[operationOptions.consumer];
205
- Object.assign(consumerConfig, { ...x, groupId: operationOptions.consumer });
229
+ const { operationConfig } = args;
230
+ let consumer = this._consumers.get(operationConfig.consumer.groupId);
231
+ if (consumer && operationConfig.selfConsumer) {
232
+ throw new Error(`Operation consumer for groupId (${operationConfig.consumer.groupId}) already exists`);
206
233
  }
207
- consumer = this._consumers.get(consumerConfig.groupId);
208
234
  /** Create consumers */
209
235
  if (!consumer) {
210
- consumer = this.kafka.consumer(consumerConfig);
211
- consumer[kGroupId] = consumerConfig.groupId;
212
- this._consumers.set(consumerConfig.groupId, consumer);
236
+ consumer = this.kafka.consumer(operationConfig.consumer);
237
+ consumer[kGroupId] = operationConfig.consumer.groupId;
238
+ this._consumers.set(operationConfig.consumer.groupId, consumer);
213
239
  }
214
240
  args.consumer = consumer;
215
241
  }
package/package.json CHANGED
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "@opra/kafka",
3
- "version": "1.0.2",
3
+ "version": "1.0.3",
4
4
  "description": "Opra Kafka package",
5
5
  "author": "Panates",
6
6
  "license": "MIT",
7
7
  "dependencies": {
8
- "@opra/common": "^1.0.2",
9
- "@opra/core": "^1.0.2",
8
+ "@opra/common": "^1.0.3",
9
+ "@opra/core": "^1.0.3",
10
10
  "node-events-async": "^1.0.0",
11
11
  "tslib": "^2.7.0",
12
12
  "valgen": "^5.10.0"
@@ -11,6 +11,12 @@ export declare namespace KafkaAdapter {
11
11
  interface Config extends PlatformAdapter.Options {
12
12
  client: StrictOmit<KafkaConfig, 'logCreator' | 'logLevel'>;
13
13
  consumers?: Record<string, StrictOmit<ConsumerConfig, 'groupId'>>;
14
+ defaults?: {
15
+ consumer?: ConsumerConfig;
16
+ subscribe?: {
17
+ fromBeginning?: boolean;
18
+ };
19
+ };
14
20
  document: ApiDocument;
15
21
  interceptors?: (InterceptorFunction | IKafkaInterceptor)[];
16
22
  logger?: ILogger;
@@ -21,7 +27,9 @@ export declare namespace KafkaAdapter {
21
27
  * groupId or ConsumerConfig
22
28
  */
23
29
  consumer?: string | ConsumerConfig;
24
- fromBeginning?: boolean;
30
+ subscribe?: {
31
+ fromBeginning?: boolean;
32
+ };
25
33
  }
26
34
  /**
27
35
  * @type InterceptorFunction
@@ -34,12 +42,17 @@ export declare namespace KafkaAdapter {
34
42
  intercept(context: KafkaContext, next: NextCallback): Promise<any>;
35
43
  };
36
44
  }
45
+ export interface OperationConfig {
46
+ consumer: ConsumerConfig;
47
+ selfConsumer?: boolean;
48
+ subscribe?: KafkaAdapter.OperationOptions['subscribe'];
49
+ }
37
50
  interface HandlerArguments {
38
51
  consumer: Consumer;
39
52
  controller: RpcController;
40
53
  instance: any;
41
54
  operation: RpcOperation;
42
- operationOptions: KafkaAdapter.OperationOptions;
55
+ operationConfig: OperationConfig;
43
56
  handler: EachMessageHandler;
44
57
  topics: (string | RegExp)[];
45
58
  }
@@ -81,7 +94,7 @@ export declare class KafkaAdapter extends PlatformAdapter {
81
94
  * @param operation
82
95
  * @protected
83
96
  */
84
- protected _getOperationOptions(controller: RpcController, instance: any, operation: RpcOperation): Promise<KafkaAdapter.OperationOptions | undefined>;
97
+ protected _getOperationConfig(controller: RpcController, instance: any, operation: RpcOperation): Promise<OperationConfig | undefined>;
85
98
  /**
86
99
  *
87
100
  * @protected