@riaskov/nevo-messaging 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.
Files changed (43) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +831 -0
  3. package/dist/common/base.client.d.ts +12 -0
  4. package/dist/common/base.client.js +50 -0
  5. package/dist/common/base.controller.d.ts +28 -0
  6. package/dist/common/base.controller.js +159 -0
  7. package/dist/common/bigint.utils.d.ts +7 -0
  8. package/dist/common/bigint.utils.js +50 -0
  9. package/dist/common/error-code.d.ts +3 -0
  10. package/dist/common/error-code.js +7 -0
  11. package/dist/common/error-messages.d.ts +1 -0
  12. package/dist/common/error-messages.js +7 -0
  13. package/dist/common/errors.d.ts +9 -0
  14. package/dist/common/errors.js +41 -0
  15. package/dist/common/index.d.ts +8 -0
  16. package/dist/common/index.js +24 -0
  17. package/dist/common/service-utils.d.ts +21 -0
  18. package/dist/common/service-utils.js +54 -0
  19. package/dist/common/signal-router.decorator.d.ts +9 -0
  20. package/dist/common/signal-router.decorator.js +67 -0
  21. package/dist/common/types.d.ts +72 -0
  22. package/dist/common/types.js +2 -0
  23. package/dist/index.d.ts +4 -0
  24. package/dist/index.js +20 -0
  25. package/dist/signal-router.utils.d.ts +28 -0
  26. package/dist/signal-router.utils.js +131 -0
  27. package/dist/signal.decorator.d.ts +15 -0
  28. package/dist/signal.decorator.js +67 -0
  29. package/dist/transports/index.d.ts +1 -0
  30. package/dist/transports/index.js +17 -0
  31. package/dist/transports/kafka/index.d.ts +5 -0
  32. package/dist/transports/kafka/index.js +21 -0
  33. package/dist/transports/kafka/kafka.client-base.d.ts +8 -0
  34. package/dist/transports/kafka/kafka.client-base.js +18 -0
  35. package/dist/transports/kafka/kafka.config.d.ts +16 -0
  36. package/dist/transports/kafka/kafka.config.js +210 -0
  37. package/dist/transports/kafka/kafka.signal-router.decorator.d.ts +3 -0
  38. package/dist/transports/kafka/kafka.signal-router.decorator.js +78 -0
  39. package/dist/transports/kafka/microservice.config.d.ts +10 -0
  40. package/dist/transports/kafka/microservice.config.js +46 -0
  41. package/dist/transports/kafka/nevo-kafka.client.d.ts +16 -0
  42. package/dist/transports/kafka/nevo-kafka.client.js +87 -0
  43. package/package.json +64 -0
@@ -0,0 +1,46 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createKafkaMicroservice = createKafkaMicroservice;
4
+ const core_1 = require("@nestjs/core");
5
+ const platform_fastify_1 = require("@nestjs/platform-fastify");
6
+ const microservices_1 = require("@nestjs/microservices");
7
+ async function createKafkaMicroservice(options) {
8
+ // @ts-ignore
9
+ const { microserviceName, module, port = 3000, host = "0.0.0.0", debug = process.env["NODE_ENV"] !== "production", onInit } = options;
10
+ const app = await core_1.NestFactory.create(module, new platform_fastify_1.FastifyAdapter());
11
+ app.connectMicroservice({
12
+ transport: microservices_1.Transport.KAFKA,
13
+ options: {
14
+ client: {
15
+ clientId: `${microserviceName}-microservice`,
16
+ brokers: [process.env["KAFKA_HOST"] ? `${process.env["KAFKA_HOST"]}:${process.env["KAFKA_PORT"] || 9092}` : "127.0.0.1:9092"],
17
+ connectionTimeout: 3000,
18
+ requestTimeout: 15000,
19
+ retry: {
20
+ retries: 3
21
+ }
22
+ },
23
+ consumer: {
24
+ groupId: `${microserviceName}-events`,
25
+ allowAutoTopicCreation: true,
26
+ sessionTimeout: 15000,
27
+ maxWaitTimeInMs: 1000,
28
+ heartbeatInterval: 1000
29
+ },
30
+ subscribe: {
31
+ fromBeginning: true
32
+ },
33
+ producer: {
34
+ idempotent: true,
35
+ maxInFlightRequests: 1
36
+ }
37
+ }
38
+ });
39
+ await app.startAllMicroservices();
40
+ if (onInit) {
41
+ await onInit(app);
42
+ }
43
+ await app.listen(port, host);
44
+ console.log(`Service started on http://${host === "0.0.0.0" ? "localhost" : host}:${port}`);
45
+ return app;
46
+ }
@@ -0,0 +1,16 @@
1
+ import { ClientKafka } from "@nestjs/microservices";
2
+ export interface NevoKafkaClientOptions {
3
+ timeoutMs?: number;
4
+ debug?: boolean;
5
+ }
6
+ export declare class NevoKafkaClient {
7
+ private readonly kafkaClient;
8
+ private readonly serviceNames;
9
+ private readonly timeoutMs;
10
+ private readonly debug;
11
+ constructor(kafkaClient: ClientKafka, serviceNames: string[], options?: NevoKafkaClientOptions);
12
+ private createMessagePayload;
13
+ query<T = any>(serviceName: string, method: string, params: any): Promise<T>;
14
+ emit(serviceName: string, method: string, params: any): Promise<void>;
15
+ getAvailableServices(): string[];
16
+ }
@@ -0,0 +1,87 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.NevoKafkaClient = void 0;
4
+ const rxjs_1 = require("rxjs");
5
+ const common_1 = require("../../common");
6
+ const node_crypto_1 = require("node:crypto");
7
+ class NevoKafkaClient {
8
+ constructor(kafkaClient, serviceNames, options) {
9
+ this.kafkaClient = kafkaClient;
10
+ this.serviceNames = serviceNames.map((name) => name.toLowerCase());
11
+ this.timeoutMs = options?.timeoutMs || 20000;
12
+ this.debug = options?.debug || false;
13
+ this.serviceNames.forEach((serviceName) => {
14
+ const topicName = `${serviceName}-events`;
15
+ const replyTopicName = `${topicName}.reply`;
16
+ this.kafkaClient.subscribeToResponseOf(topicName);
17
+ this.kafkaClient.subscribeToResponseOf(replyTopicName);
18
+ });
19
+ }
20
+ createMessagePayload(method, params) {
21
+ const uuid = (0, node_crypto_1.randomUUID)();
22
+ return {
23
+ key: uuid,
24
+ value: (0, common_1.stringifyWithBigInt)({ uuid, method, params })
25
+ };
26
+ }
27
+ async query(serviceName, method, params) {
28
+ const normalizedServiceName = serviceName.toLowerCase();
29
+ if (!this.serviceNames.includes(normalizedServiceName)) {
30
+ throw new common_1.MessagingError(common_1.ErrorCode.UNKNOWN, {
31
+ message: `Service "${serviceName}" is not registered in nevo kafka client`,
32
+ availableServices: this.serviceNames
33
+ });
34
+ }
35
+ const topicName = `${normalizedServiceName}-events`;
36
+ const payload = this.createMessagePayload(method, params);
37
+ if (this.debug) {
38
+ console.log(`[NevoKafkaClient] Sending query to ${topicName}:`, { method, params });
39
+ }
40
+ try {
41
+ const response = await (0, rxjs_1.lastValueFrom)(this.kafkaClient.send(topicName, payload).pipe((0, rxjs_1.timeout)(this.timeoutMs)));
42
+ if (response?.params?.result === "error" && response?.params?.error) {
43
+ const errorData = response.params.error;
44
+ const error = new common_1.MessagingError(errorData.code, errorData.details, errorData.service || serviceName);
45
+ if (process.env["MODE"] !== "production" && errorData.stack) {
46
+ error.stack = errorData.stack;
47
+ }
48
+ throw error;
49
+ }
50
+ return response?.params?.result;
51
+ }
52
+ catch (error) {
53
+ if (error instanceof rxjs_1.TimeoutError) {
54
+ console.error(`Kafka request timed out after ${this.timeoutMs}ms`);
55
+ throw new common_1.MessagingError(common_1.ErrorCode.UNKNOWN, {
56
+ message: `Request to ${serviceName}.${method} timed out after ${this.timeoutMs}ms`
57
+ });
58
+ }
59
+ throw error;
60
+ }
61
+ }
62
+ async emit(serviceName, method, params) {
63
+ const normalizedServiceName = serviceName.toLowerCase();
64
+ if (!this.serviceNames.includes(normalizedServiceName)) {
65
+ throw new common_1.MessagingError(common_1.ErrorCode.UNKNOWN, {
66
+ message: `Service "${serviceName}" is not registered in nevo kafka client`,
67
+ availableServices: this.serviceNames
68
+ });
69
+ }
70
+ const topicName = `${normalizedServiceName}-events`;
71
+ const payload = this.createMessagePayload(method, params);
72
+ if (this.debug) {
73
+ console.log(`[NevoKafkaClient] Emitting to ${topicName}:`, { method, params });
74
+ }
75
+ try {
76
+ this.kafkaClient.emit(topicName, payload);
77
+ }
78
+ catch (error) {
79
+ console.error(`Failed to emit event to ${serviceName}.${method}:`, error);
80
+ throw error;
81
+ }
82
+ }
83
+ getAvailableServices() {
84
+ return [...this.serviceNames];
85
+ }
86
+ }
87
+ exports.NevoKafkaClient = NevoKafkaClient;
package/package.json ADDED
@@ -0,0 +1,64 @@
1
+ {
2
+ "name": "@riaskov/nevo-messaging",
3
+ "version": "1.0.0",
4
+ "description": "Microservices messaging framework for NestJS with Kafka transport",
5
+ "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
7
+ "scripts": {
8
+ "build": "tsc",
9
+ "dev": "tsc --watch",
10
+ "prepublishOnly": "npm run build",
11
+ "test": "jest",
12
+ "lint": "eslint src/**/*.ts",
13
+ "format": "prettier --write \"src/**/*.ts\" \"examples/**/*.ts\""
14
+ },
15
+ "keywords": [
16
+ "nestjs",
17
+ "microservices",
18
+ "kafka",
19
+ "messaging",
20
+ "signal-router",
21
+ "nevo"
22
+ ],
23
+ "author": "Andrei Riaskov <code@riaskov.com>",
24
+ "license": "MIT",
25
+ "peerDependencies": {
26
+ "@nestjs/common": "^11.1.0",
27
+ "@nestjs/core": "^11.1.0",
28
+ "@nestjs/microservices": "^11.1.0",
29
+ "@nestjs/config": "^4.0.0",
30
+ "@nestjs/platform-fastify": "^11.1.0",
31
+ "kafkajs": "^2.2.4",
32
+ "rxjs": "^7.8.2",
33
+ "reflect-metadata": "^0.2.2"
34
+ },
35
+ "devDependencies": {
36
+ "@nestjs/common": "11.1.0",
37
+ "@nestjs/core": "11.1.0",
38
+ "@nestjs/microservices": "11.1.0",
39
+ "@nestjs/config": "4.0.2",
40
+ "@nestjs/platform-fastify": "11.1.0",
41
+ "kafkajs": "2.2.4",
42
+ "rxjs": "7.8.2",
43
+ "reflect-metadata": "0.2.2",
44
+ "@types/node": "^22.15.30",
45
+ "@typescript-eslint/eslint-plugin": "6.21.0",
46
+ "@typescript-eslint/parser": "6.21.0",
47
+ "eslint": "8.57.1",
48
+ "jest": "29.7.0",
49
+ "prettier": "3.5.3",
50
+ "typescript": "5.9.2"
51
+ },
52
+ "files": [
53
+ "dist",
54
+ "README.md"
55
+ ],
56
+ "repository": {
57
+ "type": "git",
58
+ "url": "https://github.com/ARyaskov/nevo-messaging.git"
59
+ },
60
+ "bugs": {
61
+ "url": "https://github.com/ARyaskov/nevo-messaging/issues"
62
+ },
63
+ "homepage": "https://github.com/ARyaskov/nevo-messaging#readme"
64
+ }