@rsdk/kafka.producer 5.4.0-next.0 → 5.4.0-next.2
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/CHANGELOG.md +10 -0
- package/dist/events-factory.abstract.d.ts +4 -1
- package/dist/events-factory.abstract.js +14 -7
- package/dist/events-factory.abstract.js.map +1 -1
- package/dist/interfaces.d.ts +3 -1
- package/dist/producer.config.d.ts +2 -3
- package/dist/producer.config.js +3 -3
- package/dist/producer.config.js.map +1 -1
- package/package.json +3 -2
- package/src/events-factory.abstract.ts +11 -2
- package/src/interfaces.ts +3 -1
- package/src/producer.config.ts +2 -4
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,16 @@
|
|
|
3
3
|
All notable changes to this project will be documented in this file.
|
|
4
4
|
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
5
5
|
|
|
6
|
+
## [5.4.0-next.2](https://github.com/R-Vision/rsdk/compare/v5.4.0-next.1...v5.4.0-next.2) (2024-11-25)
|
|
7
|
+
|
|
8
|
+
### Features
|
|
9
|
+
|
|
10
|
+
* **nats:** nats transport/kv/os + examples + docs ([#306](https://github.com/R-Vision/rsdk/issues/306)) ([de67eed](https://github.com/R-Vision/rsdk/commit/de67eed8b4cb17ec9d359067eae1e0c35d6cc736))
|
|
11
|
+
|
|
12
|
+
## [5.4.0-next.1](https://github.com/R-Vision/rsdk/compare/v5.4.0-next.0...v5.4.0-next.1) (2024-11-08)
|
|
13
|
+
|
|
14
|
+
**Note:** Version bump only for package @rsdk/kafka.producer
|
|
15
|
+
|
|
6
16
|
## [5.4.0-next.0](https://github.com/R-Vision/rsdk/compare/v5.3.0...v5.4.0-next.0) (2024-11-08)
|
|
7
17
|
|
|
8
18
|
**Note:** Version bump only for package @rsdk/kafka.producer
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import type { EventPayload, EventType } from '@rsdk/
|
|
1
|
+
import type { EventPayload, EventType } from '@rsdk/events.common';
|
|
2
|
+
import type { Message } from 'kafkajs';
|
|
2
3
|
import type { EventMetadata, ProducerOptions, PublishOptions } from './interfaces';
|
|
3
4
|
/**
|
|
4
5
|
* Абстрактный класс, содержащий полезные методы для подготовки
|
|
@@ -7,8 +8,10 @@ import type { EventMetadata, ProducerOptions, PublishOptions } from './interface
|
|
|
7
8
|
*/
|
|
8
9
|
export declare abstract class EventsFactory<T extends EventType, K extends object> {
|
|
9
10
|
protected readonly eventType: T;
|
|
11
|
+
protected readonly middleware: ((msg: Message) => Message)[];
|
|
10
12
|
private readonly payloadFormat;
|
|
11
13
|
constructor(eventType: T, config: ProducerOptions);
|
|
14
|
+
protected useMiddleware(event: Message): Message;
|
|
12
15
|
protected getMetadata(metadata: EventMetadata | undefined | null): Record<string, string>;
|
|
13
16
|
protected serializePayload<P extends EventPayload<T>>(payload: P): Buffer;
|
|
14
17
|
protected getPartitionKey<P extends Record<string, any>>(payload: P, options?: PublishOptions): string;
|
|
@@ -4,7 +4,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.EventsFactory = void 0;
|
|
7
|
-
const
|
|
7
|
+
const events_common_1 = require("@rsdk/events.common");
|
|
8
8
|
const camelCase_1 = __importDefault(require("lodash/camelCase"));
|
|
9
9
|
const get_1 = __importDefault(require("lodash/get"));
|
|
10
10
|
const node_crypto_1 = require("node:crypto");
|
|
@@ -15,24 +15,31 @@ const node_crypto_1 = require("node:crypto");
|
|
|
15
15
|
*/
|
|
16
16
|
class EventsFactory {
|
|
17
17
|
eventType;
|
|
18
|
+
middleware;
|
|
18
19
|
payloadFormat;
|
|
19
20
|
constructor(eventType, config) {
|
|
20
21
|
this.eventType = eventType;
|
|
21
|
-
this.payloadFormat = config.payloadFormat ??
|
|
22
|
+
this.payloadFormat = config.payloadFormat ?? events_common_1.DEFAULT_PAYLOAD_FORMAT;
|
|
23
|
+
this.middleware = config.middleware || [];
|
|
24
|
+
}
|
|
25
|
+
useMiddleware(event) {
|
|
26
|
+
return this.middleware.reduce((accum, middleware) => {
|
|
27
|
+
return middleware(accum);
|
|
28
|
+
}, event);
|
|
22
29
|
}
|
|
23
30
|
getMetadata(metadata) {
|
|
24
31
|
return {
|
|
25
32
|
...metadata,
|
|
26
33
|
...(this.payloadFormat &&
|
|
27
|
-
this.payloadFormat !==
|
|
28
|
-
[
|
|
34
|
+
this.payloadFormat !== events_common_1.DEFAULT_PAYLOAD_FORMAT && {
|
|
35
|
+
[events_common_1.X_FORMAT_HEADER]: this.payloadFormat,
|
|
29
36
|
}),
|
|
30
37
|
};
|
|
31
38
|
}
|
|
32
39
|
serializePayload(payload) {
|
|
33
|
-
const codec = this.payloadFormat ===
|
|
34
|
-
? new
|
|
35
|
-
: new
|
|
40
|
+
const codec = this.payloadFormat === events_common_1.PayloadFormat.JSON
|
|
41
|
+
? new events_common_1.JSONEventCodec()
|
|
42
|
+
: new events_common_1.ProtoEventCodec(this.eventType);
|
|
36
43
|
return codec.encode(payload);
|
|
37
44
|
}
|
|
38
45
|
getPartitionKey(payload, options) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"events-factory.abstract.js","sourceRoot":"","sources":["../src/events-factory.abstract.ts"],"names":[],"mappings":";;;;;;AACA,
|
|
1
|
+
{"version":3,"file":"events-factory.abstract.js","sourceRoot":"","sources":["../src/events-factory.abstract.ts"],"names":[],"mappings":";;;;;;AACA,uDAM6B;AAE7B,iEAAyC;AACzC,qDAA6B;AAC7B,6CAAyC;AAQzC;;;;GAIG;AACH,MAAsB,aAAa;IAKZ;IAJF,UAAU,CAAgC;IAC5C,aAAa,CAAgB;IAE9C,YACqB,SAAY,EAC/B,MAAuB;QADJ,cAAS,GAAT,SAAS,CAAG;QAG/B,IAAI,CAAC,aAAa,GAAG,MAAM,CAAC,aAAa,IAAI,sCAAsB,CAAC;QACpE,IAAI,CAAC,UAAU,GAAG,MAAM,CAAC,U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;;;;WAIG;QACH,OAAO,CACL,IAAA,aAAG,EAAC,OAAO,EAAE,IAAI,CAAC;YAClB,IAAA,aAAG,EAAC,OAAO,EAAE,MAAM,CAAC;YACpB,IAAA,aAAG,EAAC,OAAO,EAAE,MAAM,CAAC;YACpB,IAAA,wBAAU,GAAE,CACb,CAAC;IACJ,CAAC;CAGF;AA/ED,sCA+EC"}
|
package/dist/interfaces.d.ts
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
|
+
import type { Message } from '@nestjs/microservices/external/kafka.interface';
|
|
1
2
|
import type { MaybeReadonlyArray } from '@rsdk/common.node';
|
|
2
|
-
import type { EventPayload, EventType, PayloadFormat } from '@rsdk/
|
|
3
|
+
import type { EventPayload, EventType, PayloadFormat } from '@rsdk/events.common';
|
|
3
4
|
import type { TupleToUnion } from 'type-fest';
|
|
4
5
|
export interface ProducerOptions {
|
|
5
6
|
payloadFormat?: PayloadFormat;
|
|
7
|
+
middleware?: ((msg: Message) => Message)[];
|
|
6
8
|
}
|
|
7
9
|
export type EventMetadata = Record<string, unknown>;
|
|
8
10
|
export interface PublishOptions {
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { Config } from '@rsdk/core';
|
|
2
|
-
import { PayloadFormat } from '@rsdk/
|
|
3
|
-
|
|
4
|
-
export declare class ProducerConfig extends Config implements ProducerOptions {
|
|
2
|
+
import { PayloadFormat } from '@rsdk/events.common';
|
|
3
|
+
export declare class ProducerConfig extends Config {
|
|
5
4
|
payloadFormat: PayloadFormat;
|
|
6
5
|
}
|
package/dist/producer.config.js
CHANGED
|
@@ -11,15 +11,15 @@ var __metadata = (this && this.__metadata) || function (k, v) {
|
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
12
|
exports.ProducerConfig = void 0;
|
|
13
13
|
const core_1 = require("@rsdk/core");
|
|
14
|
-
const
|
|
14
|
+
const events_common_1 = require("@rsdk/events.common");
|
|
15
15
|
let ProducerConfig = class ProducerConfig extends core_1.Config {
|
|
16
16
|
payloadFormat;
|
|
17
17
|
};
|
|
18
18
|
exports.ProducerConfig = ProducerConfig;
|
|
19
19
|
__decorate([
|
|
20
|
-
(0, core_1.Property)('KAFKA_PAYLOAD_FORMAT', new core_1.EnumParser(
|
|
20
|
+
(0, core_1.Property)('KAFKA_PAYLOAD_FORMAT', new core_1.EnumParser(events_common_1.PayloadFormat), {
|
|
21
21
|
description: 'Option for producer payload serializer',
|
|
22
|
-
defaultValue:
|
|
22
|
+
defaultValue: events_common_1.DEFAULT_PAYLOAD_FORMAT,
|
|
23
23
|
}),
|
|
24
24
|
__metadata("design:type", String)
|
|
25
25
|
], ProducerConfig.prototype, "payloadFormat", void 0);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"producer.config.js","sourceRoot":"","sources":["../src/producer.config.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,qCAMoB;AACpB,
|
|
1
|
+
{"version":3,"file":"producer.config.js","sourceRoot":"","sources":["../src/producer.config.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,qCAMoB;AACpB,uDAA4E;AAMrE,IAAM,cAAc,GAApB,MAAM,cAAe,SAAQ,aAAM;IAKxC,aAAa,CAAiB;CAC/B,CAAA;AANY,wCAAc;AAKzB;IAJC,IAAA,eAAQ,EAAC,sBAAsB,EAAE,IAAI,iBAAU,CAAC,6BAAa,CAAC,EAAE;QAC/D,WAAW,EAAE,wCAAwC;QACrD,YAAY,EAAE,sCAAsB;KACrC,CAAC;;qDAC4B;yBALnB,cAAc;IAJ1B,IAAA,oBAAa,EAAC;QACb,IAAI,EAAE,uBAAuB;QAC7B,IAAI,EAAE,CAAC,gBAAS,CAAC,cAAc,EAAE,gBAAS,CAAC,OAAO,CAAC;KACpD,CAAC;GACW,cAAc,CAM1B"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rsdk/kafka.producer",
|
|
3
|
-
"version": "5.4.0-next.
|
|
3
|
+
"version": "5.4.0-next.2",
|
|
4
4
|
"description": "Publishers for kafka (different direct and outbox strategies)",
|
|
5
5
|
"license": "Apache License 2.0",
|
|
6
6
|
"publishConfig": {
|
|
@@ -16,11 +16,12 @@
|
|
|
16
16
|
"@nestjs/microservices": "^10.0.0",
|
|
17
17
|
"@rsdk/common": "*",
|
|
18
18
|
"@rsdk/core": "*",
|
|
19
|
+
"@rsdk/events.common": "*",
|
|
19
20
|
"@rsdk/kafka.common": "*",
|
|
20
21
|
"@rsdk/logging": "*",
|
|
21
22
|
"lodash": "^4.17.21",
|
|
22
23
|
"reflect-metadata": "^0.1.12 || ^0.2.0",
|
|
23
24
|
"rxjs": "^7.8.1"
|
|
24
25
|
},
|
|
25
|
-
"gitHead": "
|
|
26
|
+
"gitHead": "be4deac03b38b3f923a08116e1dc2b55838905b0"
|
|
26
27
|
}
|
|
@@ -1,11 +1,12 @@
|
|
|
1
|
-
import type { EventPayload, EventType } from '@rsdk/
|
|
1
|
+
import type { EventPayload, EventType } from '@rsdk/events.common';
|
|
2
2
|
import {
|
|
3
3
|
DEFAULT_PAYLOAD_FORMAT,
|
|
4
4
|
JSONEventCodec,
|
|
5
5
|
PayloadFormat,
|
|
6
6
|
ProtoEventCodec,
|
|
7
7
|
X_FORMAT_HEADER,
|
|
8
|
-
} from '@rsdk/
|
|
8
|
+
} from '@rsdk/events.common';
|
|
9
|
+
import type { Message } from 'kafkajs';
|
|
9
10
|
import camelCase from 'lodash/camelCase';
|
|
10
11
|
import get from 'lodash/get';
|
|
11
12
|
import { randomUUID } from 'node:crypto';
|
|
@@ -22,6 +23,7 @@ import type {
|
|
|
22
23
|
* kafkajs, так и для сохранения в outbox
|
|
23
24
|
*/
|
|
24
25
|
export abstract class EventsFactory<T extends EventType, K extends object> {
|
|
26
|
+
protected readonly middleware: ((msg: Message) => Message)[];
|
|
25
27
|
private readonly payloadFormat: PayloadFormat;
|
|
26
28
|
|
|
27
29
|
constructor(
|
|
@@ -29,6 +31,13 @@ export abstract class EventsFactory<T extends EventType, K extends object> {
|
|
|
29
31
|
config: ProducerOptions,
|
|
30
32
|
) {
|
|
31
33
|
this.payloadFormat = config.payloadFormat ?? DEFAULT_PAYLOAD_FORMAT;
|
|
34
|
+
this.middleware = config.middleware || [];
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
protected useMiddleware(event: Message): Message {
|
|
38
|
+
return this.middleware.reduce((accum, middleware): Message => {
|
|
39
|
+
return middleware(accum);
|
|
40
|
+
}, event);
|
|
32
41
|
}
|
|
33
42
|
|
|
34
43
|
protected getMetadata(
|
package/src/interfaces.ts
CHANGED
|
@@ -1,13 +1,15 @@
|
|
|
1
|
+
import type { Message } from '@nestjs/microservices/external/kafka.interface';
|
|
1
2
|
import type { MaybeReadonlyArray } from '@rsdk/common.node';
|
|
2
3
|
import type {
|
|
3
4
|
EventPayload,
|
|
4
5
|
EventType,
|
|
5
6
|
PayloadFormat,
|
|
6
|
-
} from '@rsdk/
|
|
7
|
+
} from '@rsdk/events.common';
|
|
7
8
|
import type { TupleToUnion } from 'type-fest';
|
|
8
9
|
|
|
9
10
|
export interface ProducerOptions {
|
|
10
11
|
payloadFormat?: PayloadFormat;
|
|
12
|
+
middleware?: ((msg: Message) => Message)[];
|
|
11
13
|
}
|
|
12
14
|
|
|
13
15
|
export type EventMetadata = Record<string, unknown>;
|
package/src/producer.config.ts
CHANGED
|
@@ -5,15 +5,13 @@ import {
|
|
|
5
5
|
EnumParser,
|
|
6
6
|
Property,
|
|
7
7
|
} from '@rsdk/core';
|
|
8
|
-
import { DEFAULT_PAYLOAD_FORMAT, PayloadFormat } from '@rsdk/
|
|
9
|
-
|
|
10
|
-
import type { ProducerOptions } from './interfaces';
|
|
8
|
+
import { DEFAULT_PAYLOAD_FORMAT, PayloadFormat } from '@rsdk/events.common';
|
|
11
9
|
|
|
12
10
|
@ConfigSection({
|
|
13
11
|
name: 'kafka producer config',
|
|
14
12
|
tags: [ConfigTag.infrastructure, ConfigTag.storage],
|
|
15
13
|
})
|
|
16
|
-
export class ProducerConfig extends Config
|
|
14
|
+
export class ProducerConfig extends Config {
|
|
17
15
|
@Property('KAFKA_PAYLOAD_FORMAT', new EnumParser(PayloadFormat), {
|
|
18
16
|
description: 'Option for producer payload serializer',
|
|
19
17
|
defaultValue: DEFAULT_PAYLOAD_FORMAT,
|