@midwayjs/kafka 1.0.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 +15 -0
- package/dist/configuration.d.ts +6 -0
- package/dist/configuration.js +35 -0
- package/dist/framework.d.ts +12 -0
- package/dist/framework.js +95 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +23 -0
- package/dist/interface.d.ts +19 -0
- package/dist/interface.js +3 -0
- package/dist/kafka.d.ts +15 -0
- package/dist/kafka.js +58 -0
- package/index.d.ts +8 -0
- package/package.json +48 -0
package/README.md
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# midway framework for kafka
|
|
2
|
+
|
|
3
|
+
[](http://packagequality.com/#?package=midway-web)
|
|
4
|
+
[](https://github.com/midwayjs/midway/pulls)
|
|
5
|
+
|
|
6
|
+
this is a sub package for midway.
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
this module use [kafkaJS](https://kafka.js.org)
|
|
10
|
+
|
|
11
|
+
Document: [https://midwayjs.org](https://midwayjs.org)
|
|
12
|
+
|
|
13
|
+
## License
|
|
14
|
+
|
|
15
|
+
[MIT]((http://github.com/midwayjs/midway/blob/master/LICENSE))
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.KafkaConfiguration = void 0;
|
|
13
|
+
const decorator_1 = require("@midwayjs/decorator");
|
|
14
|
+
const framework_1 = require("./framework");
|
|
15
|
+
let KafkaConfiguration = class KafkaConfiguration {
|
|
16
|
+
async onReady() { }
|
|
17
|
+
};
|
|
18
|
+
__decorate([
|
|
19
|
+
(0, decorator_1.Inject)(),
|
|
20
|
+
__metadata("design:type", framework_1.MidwayKafkaFramework)
|
|
21
|
+
], KafkaConfiguration.prototype, "framework", void 0);
|
|
22
|
+
KafkaConfiguration = __decorate([
|
|
23
|
+
(0, decorator_1.Configuration)({
|
|
24
|
+
namespace: 'kafka',
|
|
25
|
+
importConfigs: [
|
|
26
|
+
{
|
|
27
|
+
default: {
|
|
28
|
+
kafka: {},
|
|
29
|
+
},
|
|
30
|
+
},
|
|
31
|
+
],
|
|
32
|
+
})
|
|
33
|
+
], KafkaConfiguration);
|
|
34
|
+
exports.KafkaConfiguration = KafkaConfiguration;
|
|
35
|
+
//# sourceMappingURL=configuration.js.map
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { BaseFramework } from '@midwayjs/core';
|
|
2
|
+
import { MidwayFrameworkType } from '@midwayjs/decorator';
|
|
3
|
+
export declare class MidwayKafkaFramework extends BaseFramework<any, any, any> {
|
|
4
|
+
configure(): any;
|
|
5
|
+
applicationInitialize(options: any): Promise<void>;
|
|
6
|
+
run(): Promise<void>;
|
|
7
|
+
private loadSubscriber;
|
|
8
|
+
getFrameworkName(): string;
|
|
9
|
+
protected beforeStop(): Promise<void>;
|
|
10
|
+
getFrameworkType(): MidwayFrameworkType;
|
|
11
|
+
}
|
|
12
|
+
//# sourceMappingURL=framework.d.ts.map
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
+
exports.MidwayKafkaFramework = void 0;
|
|
10
|
+
const core_1 = require("@midwayjs/core");
|
|
11
|
+
const decorator_1 = require("@midwayjs/decorator");
|
|
12
|
+
const kafka_1 = require("./kafka");
|
|
13
|
+
let MidwayKafkaFramework = class MidwayKafkaFramework extends core_1.BaseFramework {
|
|
14
|
+
configure() {
|
|
15
|
+
return this.configService.getConfiguration('kafka');
|
|
16
|
+
}
|
|
17
|
+
async applicationInitialize(options) {
|
|
18
|
+
// Create a connection manager
|
|
19
|
+
this.app = new kafka_1.KafkaConsumerServer({
|
|
20
|
+
logger: this.logger,
|
|
21
|
+
...this.configurationOptions,
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
async run() {
|
|
25
|
+
await this.app.connect(this.configurationOptions.kafkaConfig, this.configurationOptions.consumerConfig);
|
|
26
|
+
await this.loadSubscriber();
|
|
27
|
+
this.logger.info('Kafka consumer server start success');
|
|
28
|
+
}
|
|
29
|
+
async loadSubscriber() {
|
|
30
|
+
const subscriberModules = (0, decorator_1.listModule)(decorator_1.MS_CONSUMER_KEY, module => {
|
|
31
|
+
const metadata = (0, decorator_1.getClassMetadata)(decorator_1.MS_CONSUMER_KEY, module);
|
|
32
|
+
return metadata.type === decorator_1.MSListenerType.KAFKA;
|
|
33
|
+
});
|
|
34
|
+
for (const module of subscriberModules) {
|
|
35
|
+
const data = (0, decorator_1.listPropertyDataFromClass)(decorator_1.MS_CONSUMER_KEY, module);
|
|
36
|
+
for (const methodBindListeners of data) {
|
|
37
|
+
for (const listenerOptions of methodBindListeners) {
|
|
38
|
+
await this.app.connection.subscribe({
|
|
39
|
+
topic: listenerOptions.topic,
|
|
40
|
+
// fromBeginning: true,
|
|
41
|
+
});
|
|
42
|
+
await this.app.connection.run({
|
|
43
|
+
autoCommit: false,
|
|
44
|
+
eachMessage: async ({ topic, partition, message }) => {
|
|
45
|
+
const ctx = {
|
|
46
|
+
topic: topic,
|
|
47
|
+
partition: partition,
|
|
48
|
+
message: message,
|
|
49
|
+
commitOffsets: (offset) => {
|
|
50
|
+
return this.app.connection.commitOffsets([
|
|
51
|
+
{
|
|
52
|
+
topic: topic,
|
|
53
|
+
partition: partition,
|
|
54
|
+
offset,
|
|
55
|
+
},
|
|
56
|
+
]);
|
|
57
|
+
},
|
|
58
|
+
};
|
|
59
|
+
this.app.createAnonymousContext(ctx);
|
|
60
|
+
const ins = await ctx.requestContext.getAsync(module);
|
|
61
|
+
const fn = await this.applyMiddleware(async (ctx) => {
|
|
62
|
+
return await ins[listenerOptions.propertyKey].call(ins, message);
|
|
63
|
+
});
|
|
64
|
+
try {
|
|
65
|
+
const result = await fn(ctx);
|
|
66
|
+
// 返回为undefined,下面永远不会执行
|
|
67
|
+
if (result) {
|
|
68
|
+
const res = await this.app.connection.commitOffsets(message.offset);
|
|
69
|
+
return res;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
catch (error) {
|
|
73
|
+
this.logger.error(error);
|
|
74
|
+
}
|
|
75
|
+
},
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
getFrameworkName() {
|
|
82
|
+
return 'midway:kafka';
|
|
83
|
+
}
|
|
84
|
+
async beforeStop() {
|
|
85
|
+
await this.app.close();
|
|
86
|
+
}
|
|
87
|
+
getFrameworkType() {
|
|
88
|
+
return decorator_1.MidwayFrameworkType.MS_KAFKA;
|
|
89
|
+
}
|
|
90
|
+
};
|
|
91
|
+
MidwayKafkaFramework = __decorate([
|
|
92
|
+
(0, decorator_1.Framework)()
|
|
93
|
+
], MidwayKafkaFramework);
|
|
94
|
+
exports.MidwayKafkaFramework = MidwayKafkaFramework;
|
|
95
|
+
//# sourceMappingURL=framework.js.map
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
exports.Configuration = exports.Framework = void 0;
|
|
18
|
+
var framework_1 = require("./framework");
|
|
19
|
+
Object.defineProperty(exports, "Framework", { enumerable: true, get: function () { return framework_1.MidwayKafkaFramework; } });
|
|
20
|
+
__exportStar(require("./interface"), exports);
|
|
21
|
+
var configuration_1 = require("./configuration");
|
|
22
|
+
Object.defineProperty(exports, "Configuration", { enumerable: true, get: function () { return configuration_1.KafkaConfiguration; } });
|
|
23
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { IConfigurationOptions, IMidwayApplication, IMidwayContext, NextFunction as BaseNextFunction } from '@midwayjs/core';
|
|
2
|
+
import { Kafka, KafkaConfig } from 'kafkajs';
|
|
3
|
+
export interface IKafkaApplication {
|
|
4
|
+
}
|
|
5
|
+
export declare type IMidwayKafkaApplication = IMidwayApplication<IMidwayKafkaContext> & IKafkaApplication;
|
|
6
|
+
export interface IMidwayKafkaConfigurationOptions extends IConfigurationOptions, KafkaConfig {
|
|
7
|
+
}
|
|
8
|
+
export declare type IMidwayKafkaContext = IMidwayContext<{
|
|
9
|
+
topic: any;
|
|
10
|
+
partition: any;
|
|
11
|
+
message: any;
|
|
12
|
+
commitOffsets(data: any): void;
|
|
13
|
+
}>;
|
|
14
|
+
export declare type Application = IMidwayKafkaApplication;
|
|
15
|
+
export interface Context extends IMidwayKafkaContext {
|
|
16
|
+
}
|
|
17
|
+
export declare type NextFunction = BaseNextFunction;
|
|
18
|
+
export declare type DefaultConfig = string | Kafka;
|
|
19
|
+
//# sourceMappingURL=interface.d.ts.map
|
package/dist/kafka.d.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import { Consumer, ConsumerConfig, KafkaConfig } from 'kafkajs';
|
|
3
|
+
import { ILogger } from '@midwayjs/logger';
|
|
4
|
+
import { IKafkaApplication } from './interface';
|
|
5
|
+
import { EventEmitter } from 'stream';
|
|
6
|
+
export declare class KafkaConsumerServer extends EventEmitter implements IKafkaApplication {
|
|
7
|
+
protected loggers: ILogger;
|
|
8
|
+
protected connection: Consumer;
|
|
9
|
+
constructor(options?: any);
|
|
10
|
+
bindError(): void;
|
|
11
|
+
connect(options: KafkaConfig, consumerOptions: ConsumerConfig): Promise<void>;
|
|
12
|
+
protected closeConnection(): Promise<void>;
|
|
13
|
+
close(): Promise<void>;
|
|
14
|
+
}
|
|
15
|
+
//# sourceMappingURL=kafka.d.ts.map
|
package/dist/kafka.js
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.KafkaConsumerServer = void 0;
|
|
4
|
+
const kafkajs_1 = require("kafkajs");
|
|
5
|
+
const stream_1 = require("stream");
|
|
6
|
+
class KafkaConsumerServer extends stream_1.EventEmitter {
|
|
7
|
+
constructor(options = {}) {
|
|
8
|
+
super();
|
|
9
|
+
this.connection = null;
|
|
10
|
+
this.loggers = options.logger;
|
|
11
|
+
this.bindError();
|
|
12
|
+
}
|
|
13
|
+
bindError() {
|
|
14
|
+
this.on('error', err => {
|
|
15
|
+
this.loggers.error(err);
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
async connect(options, consumerOptions) {
|
|
19
|
+
try {
|
|
20
|
+
this.connection = new kafkajs_1.Kafka(options).consumer(consumerOptions);
|
|
21
|
+
this.connection.on('consumer.connect', () => {
|
|
22
|
+
this.loggers.info('Kafka consumer connected!');
|
|
23
|
+
});
|
|
24
|
+
this.connection.on('consumer.disconnect', err => {
|
|
25
|
+
if (err) {
|
|
26
|
+
this.loggers.error('Kafka consumer disconnected', err);
|
|
27
|
+
}
|
|
28
|
+
else {
|
|
29
|
+
this.loggers.info('Kafka consumer disconnected!');
|
|
30
|
+
}
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
catch (error) {
|
|
34
|
+
this.loggers.error('Kafka consumer connect fail', error);
|
|
35
|
+
await this.closeConnection();
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
async closeConnection() {
|
|
39
|
+
try {
|
|
40
|
+
if (this.connection) {
|
|
41
|
+
await this.connection.disconnect();
|
|
42
|
+
}
|
|
43
|
+
this.loggers.debug('Kafka consumer connection close success');
|
|
44
|
+
}
|
|
45
|
+
catch (err) {
|
|
46
|
+
this.loggers.error('Kafka consumer connection close error', err);
|
|
47
|
+
}
|
|
48
|
+
finally {
|
|
49
|
+
this.connection = null;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
async close() {
|
|
53
|
+
this.loggers.debug('Kafka consumer will be close');
|
|
54
|
+
await this.closeConnection();
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
exports.KafkaConsumerServer = KafkaConsumerServer;
|
|
58
|
+
//# sourceMappingURL=kafka.js.map
|
package/index.d.ts
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@midwayjs/kafka",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Midway Framework for kafka",
|
|
5
|
+
"main": "dist/index",
|
|
6
|
+
"typings": "index.d.ts",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"build": "tsc",
|
|
9
|
+
"test": "node --require=ts-node/register ../../node_modules/.bin/jest --runInBand",
|
|
10
|
+
"cov": "node --require=ts-node/register ../../node_modules/.bin/jest --runInBand --coverage --forceExit",
|
|
11
|
+
"lint:fix": "../../node_modules/.bin/mwts fix"
|
|
12
|
+
},
|
|
13
|
+
"keywords": [
|
|
14
|
+
"midway",
|
|
15
|
+
"IoC",
|
|
16
|
+
"ws",
|
|
17
|
+
"scene",
|
|
18
|
+
"kafka"
|
|
19
|
+
],
|
|
20
|
+
"files": [
|
|
21
|
+
"dist/**/*.js",
|
|
22
|
+
"dist/**/*.d.ts",
|
|
23
|
+
"index.d.ts"
|
|
24
|
+
],
|
|
25
|
+
"license": "MIT",
|
|
26
|
+
"repository": {
|
|
27
|
+
"type": "git",
|
|
28
|
+
"url": "git+ssh://git@github.com/midwayjs/midway.git"
|
|
29
|
+
},
|
|
30
|
+
"author": "chenzhaozheng <389428742@qq.com>",
|
|
31
|
+
"bugs": {
|
|
32
|
+
"url": "https://github.com/midwayjs/midway/issues"
|
|
33
|
+
},
|
|
34
|
+
"homepage": "https://github.com/midwayjs/midway#readme",
|
|
35
|
+
"devDependencies": {
|
|
36
|
+
"@midwayjs/decorator": "^3.4.0-beta.1",
|
|
37
|
+
"@midwayjs/mock": "^3.4.0-beta.1",
|
|
38
|
+
"kafkajs": "^1.16.0"
|
|
39
|
+
},
|
|
40
|
+
"dependencies": {
|
|
41
|
+
"@midwayjs/core": "^3.4.0-beta.1",
|
|
42
|
+
"@midwayjs/logger": "^2.15.0",
|
|
43
|
+
"kafkajs": "^1.16.0"
|
|
44
|
+
},
|
|
45
|
+
"engines": {
|
|
46
|
+
"node": ">=12"
|
|
47
|
+
}
|
|
48
|
+
}
|