@lokative/messaging 1.1.5 → 1.1.7

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.
@@ -1,14 +1,13 @@
1
1
  import type { Type } from '@nestjs/common';
2
- import type { MessagingTransport } from './transport.interface';
3
- export interface MessagingConfig {
2
+ import type { MessagingTransport, SubscribeOptions } from './transport.interface';
3
+ export interface MessagingConfig<TOptions extends Record<string, any> = Record<string, any>> {
4
4
  transport: Type<MessagingTransport>;
5
- transportOptions?: Record<string, any>;
5
+ transportOptions?: TOptions;
6
6
  streams?: {
7
7
  name: string;
8
8
  subjects: string[];
9
9
  }[];
10
- consumers?: {
11
- group?: string;
10
+ consumers?: SubscribeOptions & {
12
11
  retry?: number;
13
12
  dlq?: boolean;
14
13
  };
@@ -1,5 +1,5 @@
1
1
  import { DynamicModule } from "@nestjs/common";
2
2
  import { MessagingConfig } from "./messaging.config";
3
3
  export declare class MessagingModule {
4
- static register(config: MessagingConfig): DynamicModule;
4
+ static register<TOptions extends Record<string, any> = Record<string, any>>(config: MessagingConfig<TOptions>): DynamicModule;
5
5
  }
@@ -45,8 +45,8 @@ let MessagingConsumerRegistry = class MessagingConsumerRegistry {
45
45
  }
46
46
  }
47
47
  async startConsumer(instance, handler, meta) {
48
- const group = this.config.consumers?.group;
49
- const sub = await this.transport.subscribe(meta.subject, { group });
48
+ const { retry, dlq, ...subscribeOpts } = this.config.consumers ?? {};
49
+ const sub = await this.transport.subscribe(meta.subject, subscribeOpts);
50
50
  for await (const msg of sub) {
51
51
  try {
52
52
  await handler.call(instance, msg.data);
@@ -13,5 +13,7 @@ export interface MessagingTransport {
13
13
  }
14
14
  export interface SubscribeOptions {
15
15
  group?: string;
16
+ durable?: boolean;
17
+ startFrom?: 'first' | 'new';
16
18
  }
17
19
  export declare const MESSAGING_TRANSPORT = "MESSAGING_TRANSPORT";
@@ -8,7 +8,7 @@ export declare class KafkaTransport implements MessagingTransport {
8
8
  private config;
9
9
  private kafka;
10
10
  private producer;
11
- constructor(config: MessagingConfig);
11
+ constructor(config: MessagingConfig<KafkaTransportOptions>);
12
12
  connect(): Promise<void>;
13
13
  publish(subject: string, payload: any): Promise<void>;
14
14
  subscribe(subject: string, options?: SubscribeOptions): Promise<Subscription>;
@@ -42,7 +42,10 @@ let KafkaTransport = class KafkaTransport {
42
42
  groupId: options?.group ?? 'nest-consumer',
43
43
  });
44
44
  await consumer.connect();
45
- await consumer.subscribe({ topic: subject, fromBeginning: false });
45
+ await consumer.subscribe({
46
+ topic: subject,
47
+ fromBeginning: options?.startFrom === 'first',
48
+ });
46
49
  const buffer = [];
47
50
  let waiting = null;
48
51
  await consumer.run({
@@ -12,7 +12,7 @@ export declare class NatsTransport implements MessagingTransport {
12
12
  private nc;
13
13
  private js;
14
14
  private jsm;
15
- constructor(config: MessagingConfig);
15
+ constructor(config: MessagingConfig<NatsTransportOptions>);
16
16
  connect(): Promise<void>;
17
17
  publish(subject: string, payload: any): Promise<void>;
18
18
  subscribe(subject: string, options?: SubscribeOptions): Promise<Subscription>;
@@ -34,11 +34,26 @@ let NatsTransport = class NatsTransport {
34
34
  await this.js.publish(subject, Buffer.from(JSON.stringify(payload)));
35
35
  }
36
36
  async subscribe(subject, options) {
37
- const durable = options?.group ?? 'nest-consumer';
37
+ const group = options?.group ?? 'nest-consumer';
38
+ const isDurable = options?.durable !== false; // default: durable
39
+ const startFrom = options?.startFrom ?? 'new'; // default: new messages only
40
+ const safeSuffix = subject.replace(/[.*>]/g, '-');
38
41
  const opts = (0, nats_1.consumerOpts)();
39
- opts.durable(durable);
42
+ if (isDurable) {
43
+ const durableName = `${group}-${safeSuffix}`;
44
+ opts.durable(durableName);
45
+ opts.deliverTo(`${durableName}-inbox`);
46
+ }
47
+ else {
48
+ opts.deliverTo(`_INBOX.${group}.${safeSuffix}`);
49
+ }
40
50
  opts.ackExplicit();
41
- opts.deliverTo(durable);
51
+ if (startFrom === 'first') {
52
+ opts.deliverAll();
53
+ }
54
+ else {
55
+ opts.deliverNew();
56
+ }
42
57
  const sub = await this.js.subscribe(subject, opts);
43
58
  const iter = sub[Symbol.asyncIterator]();
44
59
  const iterator = {
@@ -9,7 +9,7 @@ export declare class RedisTransport implements MessagingTransport {
9
9
  private config;
10
10
  private pub;
11
11
  private sub;
12
- constructor(config: MessagingConfig);
12
+ constructor(config: MessagingConfig<RedisTransportOptions>);
13
13
  connect(): Promise<void>;
14
14
  publish(subject: string, payload: any): Promise<void>;
15
15
  subscribe(subject: string, _options?: SubscribeOptions): Promise<Subscription>;
@@ -1,14 +1,13 @@
1
1
  import type { Type } from '@nestjs/common';
2
- import type { MessagingTransport } from './transport.interface';
3
- export interface MessagingConfig {
2
+ import type { MessagingTransport, SubscribeOptions } from './transport.interface';
3
+ export interface MessagingConfig<TOptions extends Record<string, any> = Record<string, any>> {
4
4
  transport: Type<MessagingTransport>;
5
- transportOptions?: Record<string, any>;
5
+ transportOptions?: TOptions;
6
6
  streams?: {
7
7
  name: string;
8
8
  subjects: string[];
9
9
  }[];
10
- consumers?: {
11
- group?: string;
10
+ consumers?: SubscribeOptions & {
12
11
  retry?: number;
13
12
  dlq?: boolean;
14
13
  };
@@ -1,5 +1,5 @@
1
1
  import { DynamicModule } from "@nestjs/common";
2
2
  import { MessagingConfig } from "./messaging.config";
3
3
  export declare class MessagingModule {
4
- static register(config: MessagingConfig): DynamicModule;
4
+ static register<TOptions extends Record<string, any> = Record<string, any>>(config: MessagingConfig<TOptions>): DynamicModule;
5
5
  }
@@ -42,8 +42,8 @@ let MessagingConsumerRegistry = class MessagingConsumerRegistry {
42
42
  }
43
43
  }
44
44
  async startConsumer(instance, handler, meta) {
45
- const group = this.config.consumers?.group;
46
- const sub = await this.transport.subscribe(meta.subject, { group });
45
+ const { retry, dlq, ...subscribeOpts } = this.config.consumers ?? {};
46
+ const sub = await this.transport.subscribe(meta.subject, subscribeOpts);
47
47
  for await (const msg of sub) {
48
48
  try {
49
49
  await handler.call(instance, msg.data);
@@ -13,5 +13,7 @@ export interface MessagingTransport {
13
13
  }
14
14
  export interface SubscribeOptions {
15
15
  group?: string;
16
+ durable?: boolean;
17
+ startFrom?: 'first' | 'new';
16
18
  }
17
19
  export declare const MESSAGING_TRANSPORT = "MESSAGING_TRANSPORT";
@@ -8,7 +8,7 @@ export declare class KafkaTransport implements MessagingTransport {
8
8
  private config;
9
9
  private kafka;
10
10
  private producer;
11
- constructor(config: MessagingConfig);
11
+ constructor(config: MessagingConfig<KafkaTransportOptions>);
12
12
  connect(): Promise<void>;
13
13
  publish(subject: string, payload: any): Promise<void>;
14
14
  subscribe(subject: string, options?: SubscribeOptions): Promise<Subscription>;
@@ -39,7 +39,10 @@ let KafkaTransport = class KafkaTransport {
39
39
  groupId: options?.group ?? 'nest-consumer',
40
40
  });
41
41
  await consumer.connect();
42
- await consumer.subscribe({ topic: subject, fromBeginning: false });
42
+ await consumer.subscribe({
43
+ topic: subject,
44
+ fromBeginning: options?.startFrom === 'first',
45
+ });
43
46
  const buffer = [];
44
47
  let waiting = null;
45
48
  await consumer.run({
@@ -12,7 +12,7 @@ export declare class NatsTransport implements MessagingTransport {
12
12
  private nc;
13
13
  private js;
14
14
  private jsm;
15
- constructor(config: MessagingConfig);
15
+ constructor(config: MessagingConfig<NatsTransportOptions>);
16
16
  connect(): Promise<void>;
17
17
  publish(subject: string, payload: any): Promise<void>;
18
18
  subscribe(subject: string, options?: SubscribeOptions): Promise<Subscription>;
@@ -31,11 +31,26 @@ let NatsTransport = class NatsTransport {
31
31
  await this.js.publish(subject, Buffer.from(JSON.stringify(payload)));
32
32
  }
33
33
  async subscribe(subject, options) {
34
- const durable = options?.group ?? 'nest-consumer';
34
+ const group = options?.group ?? 'nest-consumer';
35
+ const isDurable = options?.durable !== false; // default: durable
36
+ const startFrom = options?.startFrom ?? 'new'; // default: new messages only
37
+ const safeSuffix = subject.replace(/[.*>]/g, '-');
35
38
  const opts = consumerOpts();
36
- opts.durable(durable);
39
+ if (isDurable) {
40
+ const durableName = `${group}-${safeSuffix}`;
41
+ opts.durable(durableName);
42
+ opts.deliverTo(`${durableName}-inbox`);
43
+ }
44
+ else {
45
+ opts.deliverTo(`_INBOX.${group}.${safeSuffix}`);
46
+ }
37
47
  opts.ackExplicit();
38
- opts.deliverTo(durable);
48
+ if (startFrom === 'first') {
49
+ opts.deliverAll();
50
+ }
51
+ else {
52
+ opts.deliverNew();
53
+ }
39
54
  const sub = await this.js.subscribe(subject, opts);
40
55
  const iter = sub[Symbol.asyncIterator]();
41
56
  const iterator = {
@@ -9,7 +9,7 @@ export declare class RedisTransport implements MessagingTransport {
9
9
  private config;
10
10
  private pub;
11
11
  private sub;
12
- constructor(config: MessagingConfig);
12
+ constructor(config: MessagingConfig<RedisTransportOptions>);
13
13
  connect(): Promise<void>;
14
14
  publish(subject: string, payload: any): Promise<void>;
15
15
  subscribe(subject: string, _options?: SubscribeOptions): Promise<Subscription>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lokative/messaging",
3
- "version": "1.1.5",
3
+ "version": "1.1.7",
4
4
  "description": "",
5
5
  "main": "./dist/cjs/index.js",
6
6
  "module": "./dist/esm/index.js",