@lad-tech/nsc-toolkit 1.24.0 → 1.26.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.24.0](https://github.com/lad-tech/nsc-toolkit/compare/v1.23.3...v1.24.0) (2024-11-08)
1
+ # [1.26.0](https://github.com/lad-tech/nsc-toolkit/compare/v1.25.0...v1.26.0) (2025-01-14)
2
2
 
3
3
 
4
4
  ### Features
5
5
 
6
- * Uniq key for event ([#133](https://github.com/lad-tech/nsc-toolkit/issues/133)) ([ff69a67](https://github.com/lad-tech/nsc-toolkit/commit/ff69a67b464f64e9eebb44e2c65bb626c30b68ea))
6
+ * add rollup support ([#138](https://github.com/lad-tech/nsc-toolkit/issues/138)) ([423d1a8](https://github.com/lad-tech/nsc-toolkit/commit/423d1a86b4fb37b97a9cb9af8e4871b1e62caa76))
package/dist/Service.js CHANGED
@@ -30,12 +30,20 @@ class Service extends Root_1.Root {
30
30
  * de-duplication within the configured Duplicate Window
31
31
  */
32
32
  this.UNIQ_ID_HEADER = 'Nats-Msg-Id';
33
+ /**
34
+ * Nats-Rollup header indicating all prior messages should be purged
35
+ */
36
+ this.ROLLUP_HEADER = 'Nats-Rollup';
37
+ /**
38
+ * Roll-up only same subject message in the stream
39
+ */
40
+ this.ROLLUP_STRATEGY = 'sub';
33
41
  this.serviceName = options.name;
34
42
  this.logger.setLocation(this.serviceName);
35
43
  if (options.events) {
36
44
  const events = Object.keys(options.events.list);
37
45
  this.emitter = events.reduce((result, eventName) => {
38
- result[eventName] = ((params, uniqId) => {
46
+ result[eventName] = ((params, uniqId, rollupId) => {
39
47
  var _a, _b, _c;
40
48
  const subject = [options.name];
41
49
  const eventOptions = (_a = options.events) === null || _a === void 0 ? void 0 : _a.list[eventName];
@@ -52,6 +60,11 @@ class Service extends Root_1.Root {
52
60
  settings = { headers: (0, nats_1.headers)() };
53
61
  settings.headers.append(this.UNIQ_ID_HEADER, uniqId);
54
62
  }
63
+ if (rollupId) {
64
+ settings = settings !== null && settings !== void 0 ? settings : { headers: (0, nats_1.headers)() };
65
+ settings.headers.append(this.ROLLUP_HEADER, this.ROLLUP_STRATEGY);
66
+ subject.push(rollupId);
67
+ }
55
68
  this.broker.publish(subject.join('.'), this.buildMessage(params), settings);
56
69
  });
57
70
  return result;
@@ -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);
@@ -64,7 +65,7 @@ class StreamManager extends Root_1.Root {
64
65
  async createConsumer(serviceNameFrom, eventName, setting) {
65
66
  const consumerName = this.capitalizeFirstLetter(serviceNameFrom) + this.capitalizeFirstLetter(eventName);
66
67
  const prefix = this.param.options.prefix;
67
- const subject = `${this.param.serviceName}.${prefix}.${eventName}`;
68
+ const subject = `${this.param.serviceName}.${prefix}.${eventName}.*`;
68
69
  if (!this.jsm) {
69
70
  this.jsm = await this.param.broker.jetstreamManager();
70
71
  }
@@ -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, filter_subject: subject });
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
@@ -16,6 +16,14 @@ export declare class Service<E extends Emitter = Emitter> extends Root {
16
16
  * de-duplication within the configured Duplicate Window
17
17
  */
18
18
  private readonly UNIQ_ID_HEADER;
19
+ /**
20
+ * Nats-Rollup header indicating all prior messages should be purged
21
+ */
22
+ private readonly ROLLUP_HEADER;
23
+ /**
24
+ * Roll-up only same subject message in the stream
25
+ */
26
+ private readonly ROLLUP_STRATEGY;
19
27
  constructor(options: ServiceOptions<E>);
20
28
  /**
21
29
  * 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.24.0",
3
+ "version": "1.26.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 {};