@riaskov/nevo-messaging 1.1.4 → 1.1.5

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.
@@ -35,9 +35,9 @@ var __importStar = (this && this.__importStar) || (function () {
35
35
  Object.defineProperty(exports, "__esModule", { value: true });
36
36
  exports.createNevoKafkaClient = void 0;
37
37
  const microservices_1 = require("@nestjs/microservices");
38
- const kafkajs_1 = require("kafkajs");
39
38
  const nevo_kafka_client_1 = require("./nevo-kafka.client");
40
39
  const common_1 = require("../../common");
40
+ const optional_deps_1 = require("../optional-deps");
41
41
  async function createKafkaTopics(serviceNames, options) {
42
42
  const host = process.env[options.kafkaHost || "KAFKA_HOST"] || "localhost";
43
43
  const port = parseInt(process.env[options.kafkaPort || "KAFKA_PORT"] || "9092");
@@ -210,6 +210,7 @@ const createNevoKafkaClient = (serviceNames, options) => {
210
210
  await new Promise((resolve) => setTimeout(resolve, 2000 * topicCreationAttempts));
211
211
  }
212
212
  }
213
+ const { Partitioners } = (0, optional_deps_1.getKafkaModule)();
213
214
  const kafkaClientConfig = {
214
215
  transport: microservices_1.Transport.KAFKA,
215
216
  options: {
@@ -232,7 +233,7 @@ const createNevoKafkaClient = (serviceNames, options) => {
232
233
  heartbeatInterval: 3000
233
234
  },
234
235
  producer: {
235
- createPartitioner: kafkajs_1.Partitioners.DefaultPartitioner,
236
+ createPartitioner: Partitioners.DefaultPartitioner,
236
237
  allowAutoTopicCreation: mergedOptions.allowAutoTopicCreation,
237
238
  idempotent: true,
238
239
  maxInFlightRequests: 1
@@ -4,7 +4,7 @@ exports.KafkaSignalRouter = KafkaSignalRouter;
4
4
  const microservices_1 = require("@nestjs/microservices");
5
5
  const signal_router_utils_1 = require("../../signal-router.utils");
6
6
  const common_1 = require("../../common");
7
- const kafkajs_1 = require("kafkajs");
7
+ const optional_deps_1 = require("../optional-deps");
8
8
  function KafkaSignalRouter(serviceType, options) {
9
9
  return (0, signal_router_utils_1.createSignalRouterDecorator)(serviceType, options, (data) => {
10
10
  let messageData = data;
@@ -30,7 +30,8 @@ function KafkaSignalRouter(serviceType, options) {
30
30
  await originalOnModuleInit.call(this);
31
31
  const kafkaHost = process.env["KAFKA_HOST"] || "localhost";
32
32
  const kafkaPort = process.env["KAFKA_PORT"] || "9092";
33
- const kafka = new kafkajs_1.Kafka({
33
+ const { Kafka } = (0, optional_deps_1.getKafkaModule)();
34
+ const kafka = new Kafka({
34
35
  clientId: `${eventPattern}-producer`,
35
36
  brokers: [`${kafkaHost}:${kafkaPort}`]
36
37
  });
@@ -4,7 +4,7 @@ exports.NevoKafkaClient = void 0;
4
4
  const rxjs_1 = require("rxjs");
5
5
  const common_1 = require("../../common");
6
6
  const node_crypto_1 = require("node:crypto");
7
- const kafkajs_1 = require("kafkajs");
7
+ const optional_deps_1 = require("../optional-deps");
8
8
  class NevoKafkaClient {
9
9
  constructor(kafkaClient, serviceNames, options) {
10
10
  this.inFlight = new Set();
@@ -180,7 +180,8 @@ class NevoKafkaClient {
180
180
  availableServices: this.serviceNames
181
181
  });
182
182
  }
183
- const kafka = new kafkajs_1.Kafka({
183
+ const { Kafka } = (0, optional_deps_1.getKafkaModule)();
184
+ const kafka = new Kafka({
184
185
  clientId: `${this.serviceName || "nevo"}-sub-${(0, node_crypto_1.randomUUID)()}`,
185
186
  brokers: this.brokers
186
187
  });
@@ -244,7 +245,8 @@ class NevoKafkaClient {
244
245
  }
245
246
  async initDiscovery() {
246
247
  try {
247
- const kafka = new kafkajs_1.Kafka({
248
+ const { Kafka } = (0, optional_deps_1.getKafkaModule)();
249
+ const kafka = new Kafka({
248
250
  clientId: `${this.serviceName || "nevo"}-discovery`,
249
251
  brokers: this.brokers
250
252
  });
@@ -1,9 +1,9 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.NatsSignalRouter = NatsSignalRouter;
4
- const nats_1 = require("nats");
5
4
  const signal_router_utils_1 = require("../../signal-router.utils");
6
5
  const common_1 = require("../../common");
6
+ const optional_deps_1 = require("../optional-deps");
7
7
  function NatsSignalRouter(serviceType, options) {
8
8
  return (0, signal_router_utils_1.createSignalRouterDecorator)(serviceType, options, (data) => {
9
9
  const messageData = typeof data === "string" ? (0, common_1.parseWithBigInt)(data) : data;
@@ -14,14 +14,15 @@ function NatsSignalRouter(serviceType, options) {
14
14
  meta: messageData.meta
15
15
  };
16
16
  }, (target, eventPattern, handlerName) => {
17
- const codec = (0, nats_1.StringCodec)();
17
+ const { connect, StringCodec } = (0, optional_deps_1.getNatsModule)();
18
+ const codec = StringCodec();
18
19
  target.prototype.natsConnection = null;
19
20
  target.prototype.natsSubscription = null;
20
21
  const originalOnModuleInit = target.prototype.onModuleInit || function () { };
21
22
  target.prototype.onModuleInit = async function () {
22
23
  await originalOnModuleInit.call(this);
23
24
  const servers = options?.servers && options.servers.length > 0 ? options.servers : ["nats://127.0.0.1:4222"];
24
- const nc = await (0, nats_1.connect)({ servers });
25
+ const nc = await connect({ servers });
25
26
  this.natsConnection = nc;
26
27
  const sub = nc.subscribe(eventPattern);
27
28
  this.natsSubscription = sub;
@@ -1,4 +1,4 @@
1
- import { NatsConnection } from "nats";
1
+ import type { NatsConnection } from "nats";
2
2
  import { Subscription, SubscriptionContext, SubscriptionOptions } from "../../common";
3
3
  export interface NevoNatsClientOptions {
4
4
  servers?: string[];
@@ -1,20 +1,21 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.NevoNatsClient = void 0;
4
- const nats_1 = require("nats");
5
4
  const common_1 = require("../../common");
6
5
  const node_crypto_1 = require("node:crypto");
6
+ const optional_deps_1 = require("../optional-deps");
7
7
  class NevoNatsClient {
8
8
  constructor(nc, serviceNames, options) {
9
- this.codec = (0, nats_1.StringCodec)();
10
9
  this.inFlight = new Set();
11
10
  this.discoveryRegistry = new common_1.DiscoveryRegistry();
11
+ const { StringCodec } = (0, optional_deps_1.getNatsModule)();
12
12
  this.nc = nc;
13
13
  this.serviceNames = serviceNames.map((name) => name.toLowerCase());
14
14
  this.timeoutMs = options?.timeoutMs || 20000;
15
15
  this.debug = options?.debug || false;
16
16
  this.serviceName = options?.serviceName;
17
17
  this.authToken = options?.authToken;
18
+ this.codec = StringCodec();
18
19
  this.backoffEnabled = options?.backoff?.enabled !== false;
19
20
  this.backoffBaseMs = options?.backoff?.baseMs || 100;
20
21
  this.backoffMaxMs = options?.backoff?.maxMs || 2000;
@@ -28,7 +29,8 @@ class NevoNatsClient {
28
29
  }
29
30
  }
30
31
  static async create(serviceNames, options) {
31
- const nc = await (0, nats_1.connect)({
32
+ const { connect } = (0, optional_deps_1.getNatsModule)();
33
+ const nc = await connect({
32
34
  servers: options?.servers && options.servers.length > 0 ? options.servers : ["nats://127.0.0.1:4222"]
33
35
  });
34
36
  return new NevoNatsClient(nc, serviceNames, options);
@@ -0,0 +1,9 @@
1
+ type KafkaModule = typeof import("kafkajs");
2
+ type NatsModule = typeof import("nats");
3
+ type SocketIoModule = typeof import("socket.io");
4
+ type SocketIoClientModule = typeof import("socket.io-client");
5
+ export declare function getKafkaModule(): KafkaModule;
6
+ export declare function getNatsModule(): NatsModule;
7
+ export declare function getSocketIoModule(): SocketIoModule;
8
+ export declare function getSocketIoClientModule(): SocketIoClientModule;
9
+ export {};
@@ -0,0 +1,57 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getKafkaModule = getKafkaModule;
4
+ exports.getNatsModule = getNatsModule;
5
+ exports.getSocketIoModule = getSocketIoModule;
6
+ exports.getSocketIoClientModule = getSocketIoClientModule;
7
+ let kafkaModule = null;
8
+ let natsModule = null;
9
+ let socketIoModule = null;
10
+ let socketIoClientModule = null;
11
+ function isModuleNotFound(error, name) {
12
+ if (!error) {
13
+ return false;
14
+ }
15
+ if (error.code === "MODULE_NOT_FOUND") {
16
+ return error.message?.includes(`'${name}'`) || error.message?.includes(`\"${name}\"`) || error.message?.includes(name);
17
+ }
18
+ return false;
19
+ }
20
+ function requireOptional(name) {
21
+ try {
22
+ // eslint-disable-next-line @typescript-eslint/no-var-requires
23
+ return require(name);
24
+ }
25
+ catch (error) {
26
+ if (isModuleNotFound(error, name)) {
27
+ const err = new Error(`Missing optional dependency "${name}". Install it to use this transport.`);
28
+ err.cause = error;
29
+ throw err;
30
+ }
31
+ throw error;
32
+ }
33
+ }
34
+ function getKafkaModule() {
35
+ if (!kafkaModule) {
36
+ kafkaModule = requireOptional("kafkajs");
37
+ }
38
+ return kafkaModule;
39
+ }
40
+ function getNatsModule() {
41
+ if (!natsModule) {
42
+ natsModule = requireOptional("nats");
43
+ }
44
+ return natsModule;
45
+ }
46
+ function getSocketIoModule() {
47
+ if (!socketIoModule) {
48
+ socketIoModule = requireOptional("socket.io");
49
+ }
50
+ return socketIoModule;
51
+ }
52
+ function getSocketIoClientModule() {
53
+ if (!socketIoClientModule) {
54
+ socketIoClientModule = requireOptional("socket.io-client");
55
+ }
56
+ return socketIoClientModule;
57
+ }
@@ -1,9 +1,9 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.NevoSocketClient = void 0;
4
- const socket_io_client_1 = require("socket.io-client");
5
4
  const common_1 = require("../../common");
6
5
  const node_crypto_1 = require("node:crypto");
6
+ const optional_deps_1 = require("../optional-deps");
7
7
  class NevoSocketClient {
8
8
  constructor(serviceUrls, options) {
9
9
  this.inFlight = new Set();
@@ -62,7 +62,8 @@ class NevoSocketClient {
62
62
  }
63
63
  let socket = this.sockets.get(normalized);
64
64
  if (!socket) {
65
- socket = (0, socket_io_client_1.io)(url, { transports: ["websocket"] });
65
+ const { io } = (0, optional_deps_1.getSocketIoClientModule)();
66
+ socket = io(url, { transports: ["websocket"] });
66
67
  this.sockets.set(normalized, socket);
67
68
  if (this.discoveryEnabled) {
68
69
  socket.on(common_1.DEFAULT_DISCOVERY_TOPIC, (raw) => {
@@ -2,9 +2,9 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.SocketSignalRouter = SocketSignalRouter;
4
4
  const node_http_1 = require("node:http");
5
- const socket_io_1 = require("socket.io");
6
5
  const signal_router_utils_1 = require("../../signal-router.utils");
7
6
  const common_1 = require("../../common");
7
+ const optional_deps_1 = require("../optional-deps");
8
8
  function SocketSignalRouter(serviceType, options) {
9
9
  return (0, signal_router_utils_1.createSignalRouterDecorator)(serviceType, options, (data) => {
10
10
  const messageData = data || {};
@@ -24,7 +24,8 @@ function SocketSignalRouter(serviceType, options) {
24
24
  const port = options?.port || 3100;
25
25
  const path = options?.path || "/socket.io";
26
26
  const httpServer = (0, node_http_1.createServer)();
27
- const io = new socket_io_1.Server(httpServer, {
27
+ const { Server: SocketServer } = (0, optional_deps_1.getSocketIoModule)();
28
+ const io = new SocketServer(httpServer, {
28
29
  path,
29
30
  cors: options?.cors || { origin: "*" }
30
31
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@riaskov/nevo-messaging",
3
- "version": "1.1.4",
3
+ "version": "1.1.5",
4
4
  "description": "Microservices messaging framework for NestJS with NATS/Kafka/SocketIO transport",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",