@rsdk/nats.producer 5.15.0-next.8 → 5.15.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
@@ -3,6 +3,10 @@
3
3
  All notable changes to this project will be documented in this file.
4
4
  See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
5
 
6
+ ## [5.15.0](https://github.com/R-Vision/rsdk/compare/v5.15.0-next.14...v5.15.0) (2026-10-30)
7
+
8
+ **Note:** Version bump only for package @rsdk/nats.producer
9
+
6
10
  ## [5.14.1](https://github.com/R-Vision/rsdk/compare/v5.14.1-next.0...v5.14.1) (2026-09-16)
7
11
 
8
12
  **Note:** Version bump only for package @rsdk/nats.producer
package/dist/index.d.ts CHANGED
@@ -1,3 +1,4 @@
1
1
  export { EventsFactory } from './event-factory.abstract';
2
2
  export { NatsProducer, PublishOptions, ProducerOptions, EventMetadata, } from './interfaces';
3
3
  export { NatsProducerConfig } from './producer.config';
4
+ export { NatsPublishRetryPolicy, NatsPublishRetryOptions, } from './publish-retry.policy';
package/dist/index.js CHANGED
@@ -1,8 +1,10 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.NatsProducerConfig = exports.EventsFactory = void 0;
3
+ exports.NatsPublishRetryPolicy = exports.NatsProducerConfig = exports.EventsFactory = void 0;
4
4
  var event_factory_abstract_1 = require("./event-factory.abstract");
5
5
  Object.defineProperty(exports, "EventsFactory", { enumerable: true, get: function () { return event_factory_abstract_1.EventsFactory; } });
6
6
  var producer_config_1 = require("./producer.config");
7
7
  Object.defineProperty(exports, "NatsProducerConfig", { enumerable: true, get: function () { return producer_config_1.NatsProducerConfig; } });
8
+ var publish_retry_policy_1 = require("./publish-retry.policy");
9
+ Object.defineProperty(exports, "NatsPublishRetryPolicy", { enumerable: true, get: function () { return publish_retry_policy_1.NatsPublishRetryPolicy; } });
8
10
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAAA,mEAAyD;AAAhD,uHAAA,aAAa,OAAA;AAQtB,qDAAuD;AAA9C,qHAAA,kBAAkB,OAAA"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAAA,mEAAyD;AAAhD,uHAAA,aAAa,OAAA;AAQtB,qDAAuD;AAA9C,qHAAA,kBAAkB,OAAA;AAC3B,+DAGgC;AAF9B,8HAAA,sBAAsB,OAAA"}
@@ -0,0 +1,17 @@
1
+ export type NatsPublishRetryOptions = {
2
+ /** Additional attempts after the first one. `0` disables retries */
3
+ maxRetries: number;
4
+ delayMs: number;
5
+ };
6
+ /**
7
+ * Retries a publish on `timeout` and `no_responders` errors. `onRetry`
8
+ * runs before every retry, e.g. to count it in
9
+ * `nats_client_publish_retries_total`.
10
+ */
11
+ export declare class NatsPublishRetryPolicy {
12
+ private readonly options;
13
+ private readonly onRetry;
14
+ constructor(options: NatsPublishRetryOptions, onRetry?: () => void);
15
+ execute<T>(publish: () => Promise<T>): Promise<T>;
16
+ private shouldRetry;
17
+ }
@@ -0,0 +1,46 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.NatsPublishRetryPolicy = void 0;
4
+ const nats_common_1 = require("@rsdk/nats.common");
5
+ const promises_1 = require("node:timers/promises");
6
+ /** Error reasons worth retrying */
7
+ const RETRY_ON = new Set([
8
+ 'timeout',
9
+ 'no_responders',
10
+ ]);
11
+ const errorClassifier = new nats_common_1.PublishErrorClassifier();
12
+ /**
13
+ * Retries a publish on `timeout` and `no_responders` errors. `onRetry`
14
+ * runs before every retry, e.g. to count it in
15
+ * `nats_client_publish_retries_total`.
16
+ */
17
+ class NatsPublishRetryPolicy {
18
+ options;
19
+ onRetry;
20
+ constructor(options, onRetry = () => { }) {
21
+ this.options = options;
22
+ this.onRetry = onRetry;
23
+ }
24
+ async execute(publish) {
25
+ for (let attempt = 0;; attempt++) {
26
+ try {
27
+ return await publish();
28
+ }
29
+ catch (error) {
30
+ if (!this.shouldRetry(error, attempt)) {
31
+ throw error;
32
+ }
33
+ this.onRetry();
34
+ if (this.options.delayMs > 0) {
35
+ await (0, promises_1.setTimeout)(this.options.delayMs);
36
+ }
37
+ }
38
+ }
39
+ }
40
+ shouldRetry(error, attempt) {
41
+ return (attempt < this.options.maxRetries &&
42
+ RETRY_ON.has(errorClassifier.classify(error)));
43
+ }
44
+ }
45
+ exports.NatsPublishRetryPolicy = NatsPublishRetryPolicy;
46
+ //# sourceMappingURL=publish-retry.policy.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"publish-retry.policy.js","sourceRoot":"","sources":["../src/publish-retry.policy.ts"],"names":[],"mappings":";;;AACA,mDAA2D;AAC3D,mDAA2D;AAQ3D,mCAAmC;AACnC,MAAM,QAAQ,GAAoC,IAAI,GAAG,CAAC;IACxD,SAAS;IACT,eAAe;CAChB,CAAC,CAAC;AAEH,MAAM,eAAe,GAAG,IAAI,oCAAsB,EAAE,CAAC;AAErD;;;;GAIG;AACH,MAAa,sBAAsB;IAEd;IACA;IAFnB,YACmB,OAAgC,EAChC,UAAsB,GAAG,EAAE,GAAE,CAAC;QAD9B,YAAO,GAAP,OAAO,CAAyB;QAChC,YAAO,GAAP,OAAO,CAAuB;IAC9C,CAAC;IAEJ,KAAK,CAAC,OAAO,CAAI,OAAyB;QACxC,KAAK,IAAI,OAAO,GAAG,CAAC,GAAI,OAAO,EAAE,EAAE,CAAC;YAClC,IAAI,CAAC;gBACH,OAAO,MAAM,OAAO,EAAE,CAAC;YACzB,CAAC;YAAC,OAAO,KAAK,EAAE,CAAC;gBACf,IAAI,CAAC,IAAI,CAAC,WAAW,CAAC,KAAK,EAAE,OAAO,CAAC,EAAE,CAAC;oBACtC,MAAM,KAAK,CAAC;gBACd,CAAC;gBAED,IAAI,CAAC,OAAO,EAAE,CAAC;gBACf,IAAI,IAAI,CAAC,OAAO,CAAC,OAAO,GAAG,CAAC,EAAE,CAAC;oBAC7B,MAAM,IAAA,qBAAK,EAAC,IAAI,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC;gBACpC,CAAC;YACH,CAAC;QACH,CAAC;IACH,CAAC;IAEO,WAAW,CAAC,KAAc,EAAE,OAAe;QACjD,OAAO,CACL,OAAO,GAAG,IAAI,CAAC,OAAO,CAAC,UAAU;YACjC,QAAQ,CAAC,GAAG,CAAC,eAAe,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC,CAC9C,CAAC;IACJ,CAAC;CACF;AA7BD,wDA6BC"}
@@ -0,0 +1 @@
1
+ module.exports = require('@rsdk/jest/jest.config.unit');
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rsdk/nats.producer",
3
- "version": "5.15.0-next.8",
3
+ "version": "5.15.0",
4
4
  "description": "Publishers for Nats",
5
5
  "license": "Apache License 2.0",
6
6
  "publishConfig": {
@@ -24,5 +24,5 @@
24
24
  "reflect-metadata": "^0.1.12 || ^0.2.0",
25
25
  "rxjs": "^7.1.0"
26
26
  },
27
- "gitHead": "3da629f7dbb0db6a2bfe04f31b510ab3a3d5fd0c"
27
+ "gitHead": "1b5584a7b75980e56ac9a2e0a92996aab80510fd"
28
28
  }
package/src/index.ts CHANGED
@@ -7,3 +7,7 @@ export {
7
7
  EventMetadata,
8
8
  } from './interfaces';
9
9
  export { NatsProducerConfig } from './producer.config';
10
+ export {
11
+ NatsPublishRetryPolicy,
12
+ NatsPublishRetryOptions,
13
+ } from './publish-retry.policy';
@@ -0,0 +1,44 @@
1
+ import { ErrorCode, NatsError } from 'nats';
2
+
3
+ import { NatsPublishRetryPolicy } from './publish-retry.policy';
4
+
5
+ describe('NatsPublishRetryPolicy', () => {
6
+ it('retries transient errors only', async () => {
7
+ const onRetry = jest.fn();
8
+ const publish = jest
9
+ .fn()
10
+ .mockRejectedValueOnce(
11
+ new NatsError(ErrorCode.NoResponders, ErrorCode.NoResponders),
12
+ )
13
+ .mockResolvedValueOnce('ok');
14
+
15
+ const policy = new NatsPublishRetryPolicy(
16
+ { maxRetries: 2, delayMs: 0 },
17
+ onRetry,
18
+ );
19
+
20
+ await expect(policy.execute(publish)).resolves.toBe('ok');
21
+ expect(onRetry).toHaveBeenCalledTimes(1);
22
+
23
+ const failing = jest.fn().mockRejectedValue(new Error('boom'));
24
+
25
+ await expect(policy.execute(failing)).rejects.toThrow('boom');
26
+ expect(failing).toHaveBeenCalledTimes(1);
27
+ expect(onRetry).toHaveBeenCalledTimes(1);
28
+ });
29
+
30
+ it('stops after maxRetries', async () => {
31
+ const onRetry = jest.fn();
32
+ const timeout = new NatsError(ErrorCode.Timeout, ErrorCode.Timeout);
33
+ const publish = jest.fn().mockRejectedValue(timeout);
34
+
35
+ const policy = new NatsPublishRetryPolicy(
36
+ { maxRetries: 2, delayMs: 0 },
37
+ onRetry,
38
+ );
39
+
40
+ await expect(policy.execute(publish)).rejects.toBe(timeout);
41
+ expect(publish).toHaveBeenCalledTimes(3);
42
+ expect(onRetry).toHaveBeenCalledTimes(2);
43
+ });
44
+ });
@@ -0,0 +1,53 @@
1
+ import type { PublishErrorReason } from '@rsdk/nats.common';
2
+ import { PublishErrorClassifier } from '@rsdk/nats.common';
3
+ import { setTimeout as sleep } from 'node:timers/promises';
4
+
5
+ export type NatsPublishRetryOptions = {
6
+ /** Additional attempts after the first one. `0` disables retries */
7
+ maxRetries: number;
8
+ delayMs: number;
9
+ };
10
+
11
+ /** Error reasons worth retrying */
12
+ const RETRY_ON: ReadonlySet<PublishErrorReason> = new Set([
13
+ 'timeout',
14
+ 'no_responders',
15
+ ]);
16
+
17
+ const errorClassifier = new PublishErrorClassifier();
18
+
19
+ /**
20
+ * Retries a publish on `timeout` and `no_responders` errors. `onRetry`
21
+ * runs before every retry, e.g. to count it in
22
+ * `nats_client_publish_retries_total`.
23
+ */
24
+ export class NatsPublishRetryPolicy {
25
+ constructor(
26
+ private readonly options: NatsPublishRetryOptions,
27
+ private readonly onRetry: () => void = () => {},
28
+ ) {}
29
+
30
+ async execute<T>(publish: () => Promise<T>): Promise<T> {
31
+ for (let attempt = 0; ; attempt++) {
32
+ try {
33
+ return await publish();
34
+ } catch (error) {
35
+ if (!this.shouldRetry(error, attempt)) {
36
+ throw error;
37
+ }
38
+
39
+ this.onRetry();
40
+ if (this.options.delayMs > 0) {
41
+ await sleep(this.options.delayMs);
42
+ }
43
+ }
44
+ }
45
+ }
46
+
47
+ private shouldRetry(error: unknown, attempt: number): boolean {
48
+ return (
49
+ attempt < this.options.maxRetries &&
50
+ RETRY_ON.has(errorClassifier.classify(error))
51
+ );
52
+ }
53
+ }