@lad-tech/nsc-toolkit 1.23.3 → 1.25.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/CHANGELOG.md CHANGED
@@ -1,6 +1,6 @@
1
- ## [1.23.3](https://github.com/lad-tech/nsc-toolkit/compare/v1.23.2...v1.23.3) (2024-11-08)
1
+ # [1.25.0](https://github.com/lad-tech/nsc-toolkit/compare/v1.24.0...v1.25.0) (2024-12-13)
2
2
 
3
3
 
4
- ### Bug Fixes
4
+ ### Features
5
5
 
6
- * Remove close call method ([#131](https://github.com/lad-tech/nsc-toolkit/issues/131)) ([61145d8](https://github.com/lad-tech/nsc-toolkit/commit/61145d895e3bb6776ebc036a001fbb99b041013e))
6
+ * auto update consumer setting \n\n BREAKING CHANGE: all push con… ([#136](https://github.com/lad-tech/nsc-toolkit/issues/136)) ([51d63a1](https://github.com/lad-tech/nsc-toolkit/commit/51d63a17f549cf8ebed37953f8ff0d6433943620))
package/dist/Service.js CHANGED
@@ -25,12 +25,17 @@ class Service extends Root_1.Root {
25
25
  this.subscriptions = [];
26
26
  this.httpMethods = new Map();
27
27
  this.rootSpans = new Map();
28
+ /**
29
+ * Unique identifier NATS header for a message that will be used by the server apply
30
+ * de-duplication within the configured Duplicate Window
31
+ */
32
+ this.UNIQ_ID_HEADER = 'Nats-Msg-Id';
28
33
  this.serviceName = options.name;
29
34
  this.logger.setLocation(this.serviceName);
30
35
  if (options.events) {
31
36
  const events = Object.keys(options.events.list);
32
37
  this.emitter = events.reduce((result, eventName) => {
33
- result[eventName] = ((params) => {
38
+ result[eventName] = ((params, uniqId) => {
34
39
  var _a, _b, _c;
35
40
  const subject = [options.name];
36
41
  const eventOptions = (_a = options.events) === null || _a === void 0 ? void 0 : _a.list[eventName];
@@ -42,7 +47,12 @@ class Service extends Root_1.Root {
42
47
  subject.push(prefix);
43
48
  }
44
49
  subject.push(String(eventName));
45
- this.broker.publish(subject.join('.'), this.buildMessage(params));
50
+ let settings;
51
+ if (uniqId) {
52
+ settings = { headers: (0, nats_1.headers)() };
53
+ settings.headers.append(this.UNIQ_ID_HEADER, uniqId);
54
+ }
55
+ this.broker.publish(subject.join('.'), this.buildMessage(params), settings);
46
56
  });
47
57
  return result;
48
58
  }, this.emitter);
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.StreamBatchMsgFetcher = void 0;
4
+ class StreamBatchMsgFetcher {
5
+ constructor(consumer, options) {
6
+ this.consumer = consumer;
7
+ this.options = options;
8
+ }
9
+ async fetch(size, expires) {
10
+ return await this.consumer.fetch({
11
+ max_messages: size !== null && size !== void 0 ? size : this.options.batchSize,
12
+ expires: expires !== null && expires !== void 0 ? expires : this.options.batchTimeout,
13
+ });
14
+ }
15
+ }
16
+ exports.StreamBatchMsgFetcher = StreamBatchMsgFetcher;
17
+ //# sourceMappingURL=StreamBatchMsgFetcher.js.map
@@ -3,8 +3,9 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.StreamManager = void 0;
4
4
  const nats_1 = require("nats");
5
5
  const Root_1 = require("./Root");
6
- const StreamFetcher_1 = require("./StreamFetcher");
6
+ const StreamBatchMsgFetcher_1 = require("./StreamBatchMsgFetcher");
7
7
  const types_1 = require("nats/lib/jetstream/types");
8
+ const StreamSingleMsgFetcher_1 = require("./StreamSingleMsgFetcher");
8
9
  class StreamManager extends Root_1.Root {
9
10
  constructor(param) {
10
11
  super(param.broker, param.outputFormatter);
@@ -76,9 +77,6 @@ class StreamManager extends Root_1.Root {
76
77
  .ackExplicit()
77
78
  .filterSubject(subject)
78
79
  .maxAckPending((setting === null || setting === void 0 ? void 0 : setting.maxPending) || 10);
79
- if (!isPullConsumer) {
80
- options.deliverTo((0, nats_1.createInbox)());
81
- }
82
80
  if (isPullConsumer) {
83
81
  if (setting.maxPullRequestExpires) {
84
82
  options.maxPullRequestExpires(setting.maxPullRequestExpires);
@@ -115,13 +113,17 @@ class StreamManager extends Root_1.Root {
115
113
  if (!isConsumerExist) {
116
114
  await this.jsm.consumers.add(streamName, { ...options.config, filter_subject: subject });
117
115
  }
116
+ else {
117
+ await this.jsm.consumers.update(streamName, consumerName, options.config);
118
+ }
118
119
  }
120
+ const consumer = await this.broker.jetstream().consumers.get(streamName, consumerName);
119
121
  return isPullConsumer
120
- ? new StreamFetcher_1.StreamFetcher(this.broker.jetstream(), streamName, consumerName, {
122
+ ? new StreamBatchMsgFetcher_1.StreamBatchMsgFetcher(consumer, {
121
123
  batchSize: setting.maxPullRequestBatch,
122
124
  batchTimeout: setting.maxPullRequestExpires,
123
125
  })
124
- : this.broker.jetstream().subscribe(subject, options);
126
+ : new StreamSingleMsgFetcher_1.StreamSingleMsgFetcher(consumer);
125
127
  }
126
128
  getStreamName(eventName) {
127
129
  const serviceName = this.capitalizeFirstLetter(this.param.serviceName);
@@ -29,6 +29,6 @@ class BufferToJsonTransform extends stream_1.Transform {
29
29
  }
30
30
  exports.BufferToJsonTransform = BufferToJsonTransform;
31
31
  BufferToJsonTransform.errors = {
32
- CONVERSION_ERROR: 'Не удалось преобразовать данные',
32
+ CONVERSION_ERROR: 'Failed to convert data',
33
33
  };
34
34
  //# sourceMappingURL=BufferToJsonTransform.js.map
@@ -0,0 +1,33 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.StreamSingleMsgFetcher = void 0;
4
+ class StreamSingleMsgFetcher {
5
+ constructor(consumer) {
6
+ this.consumer = consumer;
7
+ this.done = false;
8
+ }
9
+ [Symbol.asyncIterator]() {
10
+ const done = {
11
+ value: this.msg,
12
+ done: true,
13
+ };
14
+ return {
15
+ next: async () => {
16
+ const msg = await this.consumer.next();
17
+ if (msg) {
18
+ this.msg = msg;
19
+ return {
20
+ value: this.msg,
21
+ done: this.done,
22
+ };
23
+ }
24
+ return done;
25
+ },
26
+ };
27
+ }
28
+ unsubscribe() {
29
+ this.done = true;
30
+ }
31
+ }
32
+ exports.StreamSingleMsgFetcher = StreamSingleMsgFetcher;
33
+ //# sourceMappingURL=StreamSingleMsgFetcher.js.map
@@ -11,6 +11,11 @@ export declare class Service<E extends Emitter = Emitter> extends Root {
11
11
  private subscriptions;
12
12
  private httpMethods;
13
13
  private rootSpans;
14
+ /**
15
+ * Unique identifier NATS header for a message that will be used by the server apply
16
+ * de-duplication within the configured Duplicate Window
17
+ */
18
+ private readonly UNIQ_ID_HEADER;
14
19
  constructor(options: ServiceOptions<E>);
15
20
  /**
16
21
  * Create global Tracer
@@ -0,0 +1,13 @@
1
+ import { Consumer, ConsumerMessages } from 'nats';
2
+ interface BatcherOptions {
3
+ batchSize?: number;
4
+ batchTimeout?: number;
5
+ noWait?: boolean;
6
+ }
7
+ export declare class StreamBatchMsgFetcher {
8
+ private consumer;
9
+ private options;
10
+ constructor(consumer: Consumer, options: BatcherOptions);
11
+ fetch(size?: number, expires?: number): Promise<ConsumerMessages>;
12
+ }
13
+ export {};
@@ -1,7 +1,8 @@
1
1
  import { StreamManagerParam, GetListenerOptions, GetBatchListenerOptions } from '.';
2
- import { JetStreamSubscription, Subscription } from 'nats';
2
+ import { Subscription } from 'nats';
3
3
  import { Root } from './Root';
4
- import { StreamFetcher } from './StreamFetcher';
4
+ import { StreamBatchMsgFetcher } from './StreamBatchMsgFetcher';
5
+ import { StreamSingleMsgFetcher } from './StreamSingleMsgFetcher';
5
6
  export declare class StreamManager extends Root {
6
7
  private param;
7
8
  private readonly STAR_WILDCARD;
@@ -13,10 +14,10 @@ export declare class StreamManager extends Root {
13
14
  private jsm?;
14
15
  constructor(param: StreamManagerParam);
15
16
  static isPullConsumerOptions(setting?: GetListenerOptions | GetBatchListenerOptions): setting is GetBatchListenerOptions;
16
- static isStreamFetcher(consumer?: JetStreamSubscription | StreamFetcher | Subscription): consumer is StreamFetcher;
17
+ static isStreamFetcher(consumer?: StreamSingleMsgFetcher | StreamBatchMsgFetcher | Subscription): consumer is StreamBatchMsgFetcher;
17
18
  createStreams(): Promise<void>;
18
- createConsumer(serviceNameFrom: string, eventName: string, setting?: GetListenerOptions): Promise<JetStreamSubscription>;
19
- createConsumer(serviceNameFrom: string, eventName: string, setting?: GetBatchListenerOptions): Promise<StreamFetcher>;
19
+ createConsumer(serviceNameFrom: string, eventName: string, setting?: GetListenerOptions): Promise<StreamSingleMsgFetcher>;
20
+ createConsumer(serviceNameFrom: string, eventName: string, setting?: GetBatchListenerOptions): Promise<StreamBatchMsgFetcher>;
20
21
  private getStreamName;
21
22
  private isNotFoundStreamError;
22
23
  private buildPrefixForStreamName;
@@ -0,0 +1,14 @@
1
+ import { Consumer, JsMsg } from 'nats';
2
+ export declare class StreamSingleMsgFetcher {
3
+ private consumer;
4
+ private done;
5
+ private msg;
6
+ constructor(consumer: Consumer);
7
+ [Symbol.asyncIterator](): {
8
+ next: () => Promise<{
9
+ value: JsMsg;
10
+ done: boolean;
11
+ }>;
12
+ };
13
+ unsubscribe(): void;
14
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lad-tech/nsc-toolkit",
3
- "version": "1.23.3",
3
+ "version": "1.25.0",
4
4
  "description": "Toolkit for create microservices around NATS",
5
5
  "main": "dist/index.js",
6
6
  "types": "./dist/types/index.d.ts",
@@ -1,20 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.StreamFetcher = void 0;
4
- class StreamFetcher {
5
- constructor(jsClient, streamName, consumerName, options) {
6
- this.jsClient = jsClient;
7
- this.streamName = streamName;
8
- this.consumerName = consumerName;
9
- this.options = options;
10
- }
11
- async fetch(size, expires) {
12
- const consumer = await this.jsClient.consumers.get(this.streamName, this.consumerName);
13
- return await consumer.fetch({
14
- max_messages: size !== null && size !== void 0 ? size : this.options.batchSize,
15
- expires: expires !== null && expires !== void 0 ? expires : this.options.batchTimeout,
16
- });
17
- }
18
- }
19
- exports.StreamFetcher = StreamFetcher;
20
- //# sourceMappingURL=StreamFetcher.js.map
@@ -1,15 +0,0 @@
1
- import { ConsumerMessages, JetStreamClient } from 'nats';
2
- interface BatcherOptions {
3
- batchSize?: number;
4
- batchTimeout?: number;
5
- noWait?: boolean;
6
- }
7
- export declare class StreamFetcher {
8
- private jsClient;
9
- private streamName;
10
- private consumerName;
11
- private options;
12
- constructor(jsClient: JetStreamClient, streamName: string, consumerName: string, options: BatcherOptions);
13
- fetch(size?: number, expires?: number): Promise<ConsumerMessages>;
14
- }
15
- export {};