@opra/kafka 1.4.2 → 1.4.4
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/cjs/kafka-adapter.js
CHANGED
|
@@ -33,7 +33,8 @@ class KafkaAdapter extends core_1.PlatformAdapter {
|
|
|
33
33
|
this.platform = KafkaAdapter.PlatformName;
|
|
34
34
|
this._document = document;
|
|
35
35
|
this._config = config;
|
|
36
|
-
if (!(this.document.api instanceof common_1.RpcApi &&
|
|
36
|
+
if (!(this.document.api instanceof common_1.RpcApi &&
|
|
37
|
+
this.document.api.platform === KafkaAdapter.PlatformName)) {
|
|
37
38
|
throw new TypeError(`The document doesn't expose a Kafka Api`);
|
|
38
39
|
}
|
|
39
40
|
// this._config = config;
|
|
@@ -62,7 +63,9 @@ class KafkaAdapter extends core_1.PlatformAdapter {
|
|
|
62
63
|
return;
|
|
63
64
|
this._kafka = new kafkajs_1.Kafka({
|
|
64
65
|
...this._config.client,
|
|
65
|
-
logCreator: this.logger
|
|
66
|
+
logCreator: this.logger
|
|
67
|
+
? () => this._createLogCreator(this.logger, this._config.logExtra)
|
|
68
|
+
: undefined,
|
|
66
69
|
});
|
|
67
70
|
await this._createAllConsumers();
|
|
68
71
|
}
|
|
@@ -85,7 +88,9 @@ class KafkaAdapter extends core_1.PlatformAdapter {
|
|
|
85
88
|
/** Subscribe to channels */
|
|
86
89
|
for (const args of this._handlerArgs) {
|
|
87
90
|
const { consumer, operation, operationConfig } = args;
|
|
88
|
-
args.topics = Array.isArray(operation.channel)
|
|
91
|
+
args.topics = Array.isArray(operation.channel)
|
|
92
|
+
? operation.channel
|
|
93
|
+
: [operation.channel];
|
|
89
94
|
await consumer
|
|
90
95
|
.subscribe({
|
|
91
96
|
...operationConfig.subscribe,
|
|
@@ -110,7 +115,7 @@ class KafkaAdapter extends core_1.PlatformAdapter {
|
|
|
110
115
|
let handlerArgsArray = topicMap.get(topicCacheKey);
|
|
111
116
|
if (!handlerArgsArray) {
|
|
112
117
|
handlerArgsArray = this._handlerArgs.filter(args => args.consumer === consumer &&
|
|
113
|
-
args.topics.find(t =>
|
|
118
|
+
args.topics.find(t => t instanceof RegExp ? t.test(topic) : t === topic));
|
|
114
119
|
/* istanbul ignore next */
|
|
115
120
|
if (!handlerArgsArray) {
|
|
116
121
|
this._emitError(new Error(`Unhandled topic (${topic})`));
|
|
@@ -274,12 +279,19 @@ class KafkaAdapter extends core_1.PlatformAdapter {
|
|
|
274
279
|
const parseKey = request_parser_js_1.RequestParser.STRING;
|
|
275
280
|
const parsePayload = request_parser_js_1.RequestParser.STRING;
|
|
276
281
|
/** Prepare decoders */
|
|
277
|
-
const decodeKey = operation.keyType?.generateCodec('decode', {
|
|
278
|
-
|
|
282
|
+
const decodeKey = operation.keyType?.generateCodec('decode', {
|
|
283
|
+
ignoreWriteonlyFields: true,
|
|
284
|
+
}) || valgen_1.vg.isAny();
|
|
285
|
+
const decodePayload = operation.payloadType?.generateCodec('decode', {
|
|
286
|
+
ignoreWriteonlyFields: true,
|
|
287
|
+
}) || valgen_1.vg.isAny();
|
|
279
288
|
operation.headers.forEach(header => {
|
|
280
289
|
let decode = this[core_1.kAssetCache].get(header, 'decode');
|
|
281
290
|
if (!decode) {
|
|
282
|
-
decode =
|
|
291
|
+
decode =
|
|
292
|
+
header.type?.generateCodec('decode', {
|
|
293
|
+
ignoreReadonlyFields: true,
|
|
294
|
+
}) || valgen_1.vg.isAny();
|
|
283
295
|
this[core_1.kAssetCache].set(header, 'decode', decode);
|
|
284
296
|
}
|
|
285
297
|
});
|
|
@@ -363,7 +375,7 @@ class KafkaAdapter extends core_1.PlatformAdapter {
|
|
|
363
375
|
.catch(noOp);
|
|
364
376
|
}
|
|
365
377
|
_wrapExceptions(exceptions) {
|
|
366
|
-
const wrappedErrors = exceptions.map(e =>
|
|
378
|
+
const wrappedErrors = exceptions.map(e => e instanceof common_1.OpraException ? e : new common_1.OpraException(e));
|
|
367
379
|
if (!wrappedErrors.length)
|
|
368
380
|
wrappedErrors.push(new common_1.OpraException('Internal Server Error'));
|
|
369
381
|
return wrappedErrors;
|
package/cjs/kafka-context.js
CHANGED
|
@@ -2,7 +2,15 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.KafkaContext = void 0;
|
|
4
4
|
const core_1 = require("@opra/core");
|
|
5
|
+
/**
|
|
6
|
+
* KafkaContext class provides the context for handling Kafka messages.
|
|
7
|
+
* It extends the ExecutionContext and implements the AsyncEventEmitter.
|
|
8
|
+
*/
|
|
5
9
|
class KafkaContext extends core_1.ExecutionContext {
|
|
10
|
+
/**
|
|
11
|
+
* Constructor
|
|
12
|
+
* @param init the context options
|
|
13
|
+
*/
|
|
6
14
|
constructor(init) {
|
|
7
15
|
super({
|
|
8
16
|
...init,
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import '@opra/core';
|
|
2
2
|
import { classes } from '@opra/common';
|
|
3
|
-
import { KAFKA_OPERATION_METADATA, KAFKA_OPERATION_METADATA_RESOLVER } from '../constants.js';
|
|
3
|
+
import { KAFKA_OPERATION_METADATA, KAFKA_OPERATION_METADATA_RESOLVER, } from '../constants.js';
|
|
4
4
|
/** Implementation **/
|
|
5
5
|
classes.RpcOperationDecoratorFactory.augment((decorator, decoratorChain) => {
|
|
6
6
|
decorator.Kafka = (config) => {
|
package/esm/kafka-adapter.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { OpraException, RPC_CONTROLLER_METADATA, RpcApi, } from '@opra/common';
|
|
2
2
|
import { kAssetCache, PlatformAdapter } from '@opra/core';
|
|
3
|
-
import { Kafka, logLevel } from 'kafkajs';
|
|
3
|
+
import { Kafka, logLevel, } from 'kafkajs';
|
|
4
4
|
import { vg } from 'valgen';
|
|
5
|
-
import { KAFKA_DEFAULT_GROUP, KAFKA_OPERATION_METADATA, KAFKA_OPERATION_METADATA_RESOLVER } from './constants.js';
|
|
5
|
+
import { KAFKA_DEFAULT_GROUP, KAFKA_OPERATION_METADATA, KAFKA_OPERATION_METADATA_RESOLVER, } from './constants.js';
|
|
6
6
|
import { KafkaContext } from './kafka-context.js';
|
|
7
7
|
import { RequestParser } from './request-parser.js';
|
|
8
8
|
const globalErrorTypes = ['unhandledRejection', 'uncaughtException'];
|
|
@@ -30,7 +30,8 @@ export class KafkaAdapter extends PlatformAdapter {
|
|
|
30
30
|
this.platform = KafkaAdapter.PlatformName;
|
|
31
31
|
this._document = document;
|
|
32
32
|
this._config = config;
|
|
33
|
-
if (!(this.document.api instanceof RpcApi &&
|
|
33
|
+
if (!(this.document.api instanceof RpcApi &&
|
|
34
|
+
this.document.api.platform === KafkaAdapter.PlatformName)) {
|
|
34
35
|
throw new TypeError(`The document doesn't expose a Kafka Api`);
|
|
35
36
|
}
|
|
36
37
|
// this._config = config;
|
|
@@ -59,7 +60,9 @@ export class KafkaAdapter extends PlatformAdapter {
|
|
|
59
60
|
return;
|
|
60
61
|
this._kafka = new Kafka({
|
|
61
62
|
...this._config.client,
|
|
62
|
-
logCreator: this.logger
|
|
63
|
+
logCreator: this.logger
|
|
64
|
+
? () => this._createLogCreator(this.logger, this._config.logExtra)
|
|
65
|
+
: undefined,
|
|
63
66
|
});
|
|
64
67
|
await this._createAllConsumers();
|
|
65
68
|
}
|
|
@@ -82,7 +85,9 @@ export class KafkaAdapter extends PlatformAdapter {
|
|
|
82
85
|
/** Subscribe to channels */
|
|
83
86
|
for (const args of this._handlerArgs) {
|
|
84
87
|
const { consumer, operation, operationConfig } = args;
|
|
85
|
-
args.topics = Array.isArray(operation.channel)
|
|
88
|
+
args.topics = Array.isArray(operation.channel)
|
|
89
|
+
? operation.channel
|
|
90
|
+
: [operation.channel];
|
|
86
91
|
await consumer
|
|
87
92
|
.subscribe({
|
|
88
93
|
...operationConfig.subscribe,
|
|
@@ -107,7 +112,7 @@ export class KafkaAdapter extends PlatformAdapter {
|
|
|
107
112
|
let handlerArgsArray = topicMap.get(topicCacheKey);
|
|
108
113
|
if (!handlerArgsArray) {
|
|
109
114
|
handlerArgsArray = this._handlerArgs.filter(args => args.consumer === consumer &&
|
|
110
|
-
args.topics.find(t =>
|
|
115
|
+
args.topics.find(t => t instanceof RegExp ? t.test(topic) : t === topic));
|
|
111
116
|
/* istanbul ignore next */
|
|
112
117
|
if (!handlerArgsArray) {
|
|
113
118
|
this._emitError(new Error(`Unhandled topic (${topic})`));
|
|
@@ -271,12 +276,19 @@ export class KafkaAdapter extends PlatformAdapter {
|
|
|
271
276
|
const parseKey = RequestParser.STRING;
|
|
272
277
|
const parsePayload = RequestParser.STRING;
|
|
273
278
|
/** Prepare decoders */
|
|
274
|
-
const decodeKey = operation.keyType?.generateCodec('decode', {
|
|
275
|
-
|
|
279
|
+
const decodeKey = operation.keyType?.generateCodec('decode', {
|
|
280
|
+
ignoreWriteonlyFields: true,
|
|
281
|
+
}) || vg.isAny();
|
|
282
|
+
const decodePayload = operation.payloadType?.generateCodec('decode', {
|
|
283
|
+
ignoreWriteonlyFields: true,
|
|
284
|
+
}) || vg.isAny();
|
|
276
285
|
operation.headers.forEach(header => {
|
|
277
286
|
let decode = this[kAssetCache].get(header, 'decode');
|
|
278
287
|
if (!decode) {
|
|
279
|
-
decode =
|
|
288
|
+
decode =
|
|
289
|
+
header.type?.generateCodec('decode', {
|
|
290
|
+
ignoreReadonlyFields: true,
|
|
291
|
+
}) || vg.isAny();
|
|
280
292
|
this[kAssetCache].set(header, 'decode', decode);
|
|
281
293
|
}
|
|
282
294
|
});
|
|
@@ -360,7 +372,7 @@ export class KafkaAdapter extends PlatformAdapter {
|
|
|
360
372
|
.catch(noOp);
|
|
361
373
|
}
|
|
362
374
|
_wrapExceptions(exceptions) {
|
|
363
|
-
const wrappedErrors = exceptions.map(e =>
|
|
375
|
+
const wrappedErrors = exceptions.map(e => e instanceof OpraException ? e : new OpraException(e));
|
|
364
376
|
if (!wrappedErrors.length)
|
|
365
377
|
wrappedErrors.push(new OpraException('Internal Server Error'));
|
|
366
378
|
return wrappedErrors;
|
package/esm/kafka-context.js
CHANGED
|
@@ -1,5 +1,13 @@
|
|
|
1
1
|
import { ExecutionContext } from '@opra/core';
|
|
2
|
+
/**
|
|
3
|
+
* KafkaContext class provides the context for handling Kafka messages.
|
|
4
|
+
* It extends the ExecutionContext and implements the AsyncEventEmitter.
|
|
5
|
+
*/
|
|
2
6
|
export class KafkaContext extends ExecutionContext {
|
|
7
|
+
/**
|
|
8
|
+
* Constructor
|
|
9
|
+
* @param init the context options
|
|
10
|
+
*/
|
|
3
11
|
constructor(init) {
|
|
4
12
|
super({
|
|
5
13
|
...init,
|
package/package.json
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@opra/kafka",
|
|
3
|
-
"version": "1.4.
|
|
3
|
+
"version": "1.4.4",
|
|
4
4
|
"description": "Opra Kafka package",
|
|
5
5
|
"author": "Panates",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"dependencies": {
|
|
8
|
-
"@opra/common": "^1.4.
|
|
9
|
-
"@opra/core": "^1.4.
|
|
8
|
+
"@opra/common": "^1.4.4",
|
|
9
|
+
"@opra/core": "^1.4.4",
|
|
10
10
|
"node-events-async": "^1.0.0",
|
|
11
11
|
"tslib": "^2.8.1",
|
|
12
12
|
"valgen": "^5.12.0"
|
package/types/kafka-context.d.ts
CHANGED
|
@@ -3,23 +3,10 @@ import { ExecutionContext } from '@opra/core';
|
|
|
3
3
|
import type { KafkaMessage } from 'kafkajs';
|
|
4
4
|
import type { AsyncEventEmitter } from 'node-events-async';
|
|
5
5
|
import type { KafkaAdapter } from './kafka-adapter.js';
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
controllerInstance?: any;
|
|
11
|
-
operation?: RpcOperation;
|
|
12
|
-
operationHandler?: Function;
|
|
13
|
-
topic: string;
|
|
14
|
-
partition: number;
|
|
15
|
-
key: any;
|
|
16
|
-
payload: any;
|
|
17
|
-
headers: Record<string, any>;
|
|
18
|
-
rawMessage: KafkaMessage;
|
|
19
|
-
heartbeat(): Promise<void>;
|
|
20
|
-
pause(): () => void;
|
|
21
|
-
}
|
|
22
|
-
}
|
|
6
|
+
/**
|
|
7
|
+
* KafkaContext class provides the context for handling Kafka messages.
|
|
8
|
+
* It extends the ExecutionContext and implements the AsyncEventEmitter.
|
|
9
|
+
*/
|
|
23
10
|
export declare class KafkaContext extends ExecutionContext implements AsyncEventEmitter {
|
|
24
11
|
readonly protocol: OpraSchema.Transport;
|
|
25
12
|
readonly platform: string;
|
|
@@ -36,5 +23,26 @@ export declare class KafkaContext extends ExecutionContext implements AsyncEvent
|
|
|
36
23
|
readonly rawMessage: KafkaMessage;
|
|
37
24
|
readonly heartbeat: () => Promise<void>;
|
|
38
25
|
readonly pause: () => void;
|
|
26
|
+
/**
|
|
27
|
+
* Constructor
|
|
28
|
+
* @param init the context options
|
|
29
|
+
*/
|
|
39
30
|
constructor(init: KafkaContext.Initiator);
|
|
40
31
|
}
|
|
32
|
+
export declare namespace KafkaContext {
|
|
33
|
+
interface Initiator extends Omit<ExecutionContext.Initiator, 'document' | 'protocol' | 'documentNode'> {
|
|
34
|
+
adapter: KafkaAdapter;
|
|
35
|
+
controller?: RpcController;
|
|
36
|
+
controllerInstance?: any;
|
|
37
|
+
operation?: RpcOperation;
|
|
38
|
+
operationHandler?: Function;
|
|
39
|
+
topic: string;
|
|
40
|
+
partition: number;
|
|
41
|
+
key: any;
|
|
42
|
+
payload: any;
|
|
43
|
+
headers: Record<string, any>;
|
|
44
|
+
rawMessage: KafkaMessage;
|
|
45
|
+
heartbeat(): Promise<void>;
|
|
46
|
+
pause(): void;
|
|
47
|
+
}
|
|
48
|
+
}
|