@ddd-ts-kit/nats 0.0.0-stage → 0.1.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.
Files changed (158) hide show
  1. package/README.md +344 -2
  2. package/dist/codec/decode.d.ts +4 -0
  3. package/dist/codec/decode.d.ts.map +1 -0
  4. package/dist/codec/decode.js +39 -0
  5. package/dist/codec/decode.js.map +1 -0
  6. package/dist/codec/encode.d.ts +8 -0
  7. package/dist/codec/encode.d.ts.map +1 -0
  8. package/dist/codec/encode.js +19 -0
  9. package/dist/codec/encode.js.map +1 -0
  10. package/dist/codec/headers.d.ts +14 -0
  11. package/dist/codec/headers.d.ts.map +1 -0
  12. package/dist/codec/headers.js +14 -0
  13. package/dist/codec/headers.js.map +1 -0
  14. package/dist/codec/publication.d.ts +7 -0
  15. package/dist/codec/publication.d.ts.map +1 -0
  16. package/dist/codec/publication.js +2 -0
  17. package/dist/codec/publication.js.map +1 -0
  18. package/dist/codec/republication.d.ts +11 -0
  19. package/dist/codec/republication.d.ts.map +1 -0
  20. package/dist/codec/republication.js +16 -0
  21. package/dist/codec/republication.js.map +1 -0
  22. package/dist/connection/broker-link.d.ts +49 -0
  23. package/dist/connection/broker-link.d.ts.map +1 -0
  24. package/dist/connection/broker-link.js +160 -0
  25. package/dist/connection/broker-link.js.map +1 -0
  26. package/dist/consumption/consumption.d.ts +56 -0
  27. package/dist/consumption/consumption.d.ts.map +1 -0
  28. package/dist/consumption/consumption.js +223 -0
  29. package/dist/consumption/consumption.js.map +1 -0
  30. package/dist/consumption/failure-of.d.ts +7 -0
  31. package/dist/consumption/failure-of.d.ts.map +1 -0
  32. package/dist/consumption/failure-of.js +18 -0
  33. package/dist/consumption/failure-of.js.map +1 -0
  34. package/dist/consumption/poison-message.d.ts +3 -0
  35. package/dist/consumption/poison-message.d.ts.map +1 -0
  36. package/dist/consumption/poison-message.js +3 -0
  37. package/dist/consumption/poison-message.js.map +1 -0
  38. package/dist/consumption/track.d.ts +3 -0
  39. package/dist/consumption/track.d.ts.map +1 -0
  40. package/dist/consumption/track.js +9 -0
  41. package/dist/consumption/track.js.map +1 -0
  42. package/dist/errors/broker-unavailable-error.d.ts +8 -0
  43. package/dist/errors/broker-unavailable-error.d.ts.map +1 -0
  44. package/dist/errors/broker-unavailable-error.js +12 -0
  45. package/dist/errors/broker-unavailable-error.js.map +1 -0
  46. package/dist/errors/consumer-name-required-error.d.ts +5 -0
  47. package/dist/errors/consumer-name-required-error.d.ts.map +1 -0
  48. package/dist/errors/consumer-name-required-error.js +8 -0
  49. package/dist/errors/consumer-name-required-error.js.map +1 -0
  50. package/dist/errors/event-bus-closed-error.d.ts +5 -0
  51. package/dist/errors/event-bus-closed-error.d.ts.map +1 -0
  52. package/dist/errors/event-bus-closed-error.js +8 -0
  53. package/dist/errors/event-bus-closed-error.js.map +1 -0
  54. package/dist/errors/failure-code.d.ts +8 -0
  55. package/dist/errors/failure-code.d.ts.map +1 -0
  56. package/dist/errors/failure-code.js +19 -0
  57. package/dist/errors/failure-code.js.map +1 -0
  58. package/dist/errors/publish-not-acknowledged-error.d.ts +11 -0
  59. package/dist/errors/publish-not-acknowledged-error.d.ts.map +1 -0
  60. package/dist/errors/publish-not-acknowledged-error.js +16 -0
  61. package/dist/errors/publish-not-acknowledged-error.js.map +1 -0
  62. package/dist/ignore.d.ts +3 -0
  63. package/dist/ignore.d.ts.map +1 -0
  64. package/dist/ignore.js +5 -0
  65. package/dist/ignore.js.map +1 -0
  66. package/dist/index.d.ts +22 -0
  67. package/dist/index.d.ts.map +1 -0
  68. package/dist/index.js +11 -0
  69. package/dist/index.js.map +1 -0
  70. package/dist/messaging-system.d.ts +3 -0
  71. package/dist/messaging-system.d.ts.map +1 -0
  72. package/dist/messaging-system.js +3 -0
  73. package/dist/messaging-system.js.map +1 -0
  74. package/dist/nats-event-bus.d.ts +57 -0
  75. package/dist/nats-event-bus.d.ts.map +1 -0
  76. package/dist/nats-event-bus.js +168 -0
  77. package/dist/nats-event-bus.js.map +1 -0
  78. package/dist/options/nats-delivery.d.ts +14 -0
  79. package/dist/options/nats-delivery.d.ts.map +1 -0
  80. package/dist/options/nats-delivery.js +2 -0
  81. package/dist/options/nats-delivery.js.map +1 -0
  82. package/dist/options/nats-event-bus-options.d.ts +40 -0
  83. package/dist/options/nats-event-bus-options.d.ts.map +1 -0
  84. package/dist/options/nats-event-bus-options.js +3 -0
  85. package/dist/options/nats-event-bus-options.js.map +1 -0
  86. package/dist/options/nats-failure.d.ts +17 -0
  87. package/dist/options/nats-failure.d.ts.map +1 -0
  88. package/dist/options/nats-failure.js +2 -0
  89. package/dist/options/nats-failure.js.map +1 -0
  90. package/dist/options/reconnect-options.d.ts +9 -0
  91. package/dist/options/reconnect-options.d.ts.map +1 -0
  92. package/dist/options/reconnect-options.js +2 -0
  93. package/dist/options/reconnect-options.js.map +1 -0
  94. package/dist/options/retry-options.d.ts +7 -0
  95. package/dist/options/retry-options.d.ts.map +1 -0
  96. package/dist/options/retry-options.js +2 -0
  97. package/dist/options/retry-options.js.map +1 -0
  98. package/dist/options/settings-of.d.ts +5 -0
  99. package/dist/options/settings-of.d.ts.map +1 -0
  100. package/dist/options/settings-of.js +55 -0
  101. package/dist/options/settings-of.js.map +1 -0
  102. package/dist/options/settings.d.ts +23 -0
  103. package/dist/options/settings.d.ts.map +1 -0
  104. package/dist/options/settings.js +2 -0
  105. package/dist/options/settings.js.map +1 -0
  106. package/dist/options/stream-storage.d.ts +2 -0
  107. package/dist/options/stream-storage.d.ts.map +1 -0
  108. package/dist/options/stream-storage.js +2 -0
  109. package/dist/options/stream-storage.js.map +1 -0
  110. package/dist/publication/publications.d.ts +25 -0
  111. package/dist/publication/publications.d.ts.map +1 -0
  112. package/dist/publication/publications.js +47 -0
  113. package/dist/publication/publications.js.map +1 -0
  114. package/dist/secrecy/conceal.d.ts +8 -0
  115. package/dist/secrecy/conceal.d.ts.map +1 -0
  116. package/dist/secrecy/conceal.js +12 -0
  117. package/dist/secrecy/conceal.js.map +1 -0
  118. package/dist/topology/binding-names-of.d.ts +5 -0
  119. package/dist/topology/binding-names-of.d.ts.map +1 -0
  120. package/dist/topology/binding-names-of.js +13 -0
  121. package/dist/topology/binding-names-of.js.map +1 -0
  122. package/dist/topology/binding-names.d.ts +10 -0
  123. package/dist/topology/binding-names.d.ts.map +1 -0
  124. package/dist/topology/binding-names.js +2 -0
  125. package/dist/topology/binding-names.js.map +1 -0
  126. package/dist/topology/consumer-binding.d.ts +6 -0
  127. package/dist/topology/consumer-binding.d.ts.map +1 -0
  128. package/dist/topology/consumer-binding.js +2 -0
  129. package/dist/topology/consumer-binding.js.map +1 -0
  130. package/dist/topology/consumer-description.d.ts +9 -0
  131. package/dist/topology/consumer-description.d.ts.map +1 -0
  132. package/dist/topology/consumer-description.js +2 -0
  133. package/dist/topology/consumer-description.js.map +1 -0
  134. package/dist/topology/dead-stream-of.d.ts +2 -0
  135. package/dist/topology/dead-stream-of.d.ts.map +1 -0
  136. package/dist/topology/dead-stream-of.js +4 -0
  137. package/dist/topology/dead-stream-of.js.map +1 -0
  138. package/dist/topology/declare-topology.d.ts +8 -0
  139. package/dist/topology/declare-topology.d.ts.map +1 -0
  140. package/dist/topology/declare-topology.js +58 -0
  141. package/dist/topology/declare-topology.js.map +1 -0
  142. package/dist/topology/nats-topology.d.ts +8 -0
  143. package/dist/topology/nats-topology.d.ts.map +1 -0
  144. package/dist/topology/nats-topology.js +2 -0
  145. package/dist/topology/nats-topology.js.map +1 -0
  146. package/dist/topology/stream-description.d.ts +11 -0
  147. package/dist/topology/stream-description.d.ts.map +1 -0
  148. package/dist/topology/stream-description.js +2 -0
  149. package/dist/topology/stream-description.js.map +1 -0
  150. package/dist/topology/topology-of.d.ts +5 -0
  151. package/dist/topology/topology-of.d.ts.map +1 -0
  152. package/dist/topology/topology-of.js +29 -0
  153. package/dist/topology/topology-of.js.map +1 -0
  154. package/dist/topology/topology-settings.d.ts +11 -0
  155. package/dist/topology/topology-settings.d.ts.map +1 -0
  156. package/dist/topology/topology-settings.js +2 -0
  157. package/dist/topology/topology-settings.js.map +1 -0
  158. package/package.json +39 -4
@@ -0,0 +1,19 @@
1
+ import { errorCode } from '@ddd-ts-kit/events';
2
+ /**
3
+ * What may be reported about a caught error. JetStream answers a refused request with a
4
+ * numeric error code (`10058` for a stream that exists with another configuration): it
5
+ * becomes `JETSTREAM_10058`. A connection error wraps the system error: its code
6
+ * (`ECONNREFUSED`) is the one reported. Never the message.
7
+ */
8
+ export function failureCode(error) {
9
+ if (typeof error !== 'object' || error === null) {
10
+ return errorCode(error);
11
+ }
12
+ if ('code' in error && typeof error.code === 'number' && Number.isInteger(error.code)) {
13
+ return `JETSTREAM_${String(error.code)}`;
14
+ }
15
+ const cause = 'cause' in error ? error.cause : undefined;
16
+ const causeCode = errorCode(cause);
17
+ return causeCode === 'UNKNOWN_ERROR' || causeCode === 'Error' ? errorCode(error) : causeCode;
18
+ }
19
+ //# sourceMappingURL=failure-code.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"failure-code.js","sourceRoot":"","sources":["../../src/errors/failure-code.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,oBAAoB,CAAC;AAE/C;;;;;GAKG;AACH,MAAM,UAAU,WAAW,CAAC,KAAc;IACxC,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,KAAK,IAAI,EAAE,CAAC;QAChD,OAAO,SAAS,CAAC,KAAK,CAAC,CAAC;IAC1B,CAAC;IAED,IAAI,MAAM,IAAI,KAAK,IAAI,OAAO,KAAK,CAAC,IAAI,KAAK,QAAQ,IAAI,MAAM,CAAC,SAAS,CAAC,KAAK,CAAC,IAAI,CAAC,EAAE,CAAC;QACtF,OAAO,aAAa,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,EAAE,CAAC;IAC3C,CAAC;IAED,MAAM,KAAK,GAAY,OAAO,IAAI,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC,CAAC,SAAS,CAAC;IAClE,MAAM,SAAS,GAAG,SAAS,CAAC,KAAK,CAAC,CAAC;IAEnC,OAAO,SAAS,KAAK,eAAe,IAAI,SAAS,KAAK,OAAO,CAAC,CAAC,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC;AAC/F,CAAC"}
@@ -0,0 +1,11 @@
1
+ /**
2
+ * The stream did not acknowledge the message in time: it may or may not hold it. Publish
3
+ * again; inside the duplicate window the stream drops the second copy.
4
+ */
5
+ export declare class PublishNotAcknowledgedError extends Error {
6
+ readonly eventId: string;
7
+ readonly reason: string;
8
+ readonly code = "PUBLISH_NOT_ACKNOWLEDGED";
9
+ constructor(eventId: string, reason: string);
10
+ }
11
+ //# sourceMappingURL=publish-not-acknowledged-error.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"publish-not-acknowledged-error.d.ts","sourceRoot":"","sources":["../../src/errors/publish-not-acknowledged-error.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,qBAAa,2BAA4B,SAAQ,KAAK;IAIlD,QAAQ,CAAC,OAAO,EAAE,MAAM;IACxB,QAAQ,CAAC,MAAM,EAAE,MAAM;IAJzB,QAAQ,CAAC,IAAI,8BAA8B;IAE3C,YACW,OAAO,EAAE,MAAM,EACf,MAAM,EAAE,MAAM,EAIxB;CACF"}
@@ -0,0 +1,16 @@
1
+ /**
2
+ * The stream did not acknowledge the message in time: it may or may not hold it. Publish
3
+ * again; inside the duplicate window the stream drops the second copy.
4
+ */
5
+ export class PublishNotAcknowledgedError extends Error {
6
+ eventId;
7
+ reason;
8
+ code = 'PUBLISH_NOT_ACKNOWLEDGED';
9
+ constructor(eventId, reason) {
10
+ super(`The stream did not acknowledge event ${eventId} (${reason})`);
11
+ this.eventId = eventId;
12
+ this.reason = reason;
13
+ this.name = 'PublishNotAcknowledgedError';
14
+ }
15
+ }
16
+ //# sourceMappingURL=publish-not-acknowledged-error.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"publish-not-acknowledged-error.js","sourceRoot":"","sources":["../../src/errors/publish-not-acknowledged-error.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,MAAM,OAAO,2BAA4B,SAAQ,KAAK;IAIzC,OAAO;IACP,MAAM;IAJR,IAAI,GAAG,0BAA0B,CAAC;IAE3C,YACW,OAAe,EACf,MAAc;QAEvB,KAAK,CAAC,wCAAwC,OAAO,KAAK,MAAM,GAAG,CAAC,CAAC;uBAH5D,OAAO;sBACP,MAAM;QAGf,IAAI,CAAC,IAAI,GAAG,6BAA6B,CAAC;IAC5C,CAAC;CACF"}
@@ -0,0 +1,3 @@
1
+ /** For a rejection or an event that needs no answer: closing what is already closed. */
2
+ export declare function ignore(): undefined;
3
+ //# sourceMappingURL=ignore.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ignore.d.ts","sourceRoot":"","sources":["../src/ignore.ts"],"names":[],"mappings":"AAAA,wFAAwF;AACxF,wBAAgB,MAAM,IAAI,SAAS,CAElC"}
package/dist/ignore.js ADDED
@@ -0,0 +1,5 @@
1
+ /** For a rejection or an event that needs no answer: closing what is already closed. */
2
+ export function ignore() {
3
+ return undefined;
4
+ }
5
+ //# sourceMappingURL=ignore.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ignore.js","sourceRoot":"","sources":["../src/ignore.ts"],"names":[],"mappings":"AAAA,wFAAwF;AACxF,MAAM,UAAU,MAAM;IACpB,OAAO,SAAS,CAAC;AACnB,CAAC"}
@@ -0,0 +1,22 @@
1
+ export { NatsEventBus } from './nats-event-bus.js';
2
+ export { DEFAULT_STREAM, DEFAULT_SUBJECT_PREFIX } from './options/nats-event-bus-options.js';
3
+ export type { NatsEventBusOptions } from './options/nats-event-bus-options.js';
4
+ export type { NatsDelivery } from './options/nats-delivery.js';
5
+ export type { NatsFailure } from './options/nats-failure.js';
6
+ export type { ReconnectOptions } from './options/reconnect-options.js';
7
+ export type { RetryOptions } from './options/retry-options.js';
8
+ export type { StreamStorage } from './options/stream-storage.js';
9
+ export { bindingNamesOf } from './topology/binding-names-of.js';
10
+ export { deadStreamOf } from './topology/dead-stream-of.js';
11
+ export type { BindingNames } from './topology/binding-names.js';
12
+ export type { ConsumerBinding } from './topology/consumer-binding.js';
13
+ export type { ConsumerDescription } from './topology/consumer-description.js';
14
+ export type { NatsTopology } from './topology/nats-topology.js';
15
+ export type { StreamDescription } from './topology/stream-description.js';
16
+ export { ATTEMPT_HEADER, CORRELATION_HEADER, DEAD_REASON_HEADER, ERROR_CODE_HEADER, EVENT_TYPE_HEADER, EVENT_VERSION_HEADER, MESSAGE_ID_HEADER, REPLAY_FOR_HEADER, SOURCE_HEADER, TRACEPARENT_HEADER, } from './codec/headers.js';
17
+ export { POISON_MESSAGE } from './consumption/poison-message.js';
18
+ export { BrokerUnavailableError } from './errors/broker-unavailable-error.js';
19
+ export { ConsumerNameRequiredError } from './errors/consumer-name-required-error.js';
20
+ export { EventBusClosedError } from './errors/event-bus-closed-error.js';
21
+ export { PublishNotAcknowledgedError } from './errors/publish-not-acknowledged-error.js';
22
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAC;AACnD,OAAO,EAAE,cAAc,EAAE,sBAAsB,EAAE,MAAM,qCAAqC,CAAC;AAC7F,YAAY,EAAE,mBAAmB,EAAE,MAAM,qCAAqC,CAAC;AAC/E,YAAY,EAAE,YAAY,EAAE,MAAM,4BAA4B,CAAC;AAC/D,YAAY,EAAE,WAAW,EAAE,MAAM,2BAA2B,CAAC;AAC7D,YAAY,EAAE,gBAAgB,EAAE,MAAM,gCAAgC,CAAC;AACvE,YAAY,EAAE,YAAY,EAAE,MAAM,4BAA4B,CAAC;AAC/D,YAAY,EAAE,aAAa,EAAE,MAAM,6BAA6B,CAAC;AACjE,OAAO,EAAE,cAAc,EAAE,MAAM,gCAAgC,CAAC;AAChE,OAAO,EAAE,YAAY,EAAE,MAAM,8BAA8B,CAAC;AAC5D,YAAY,EAAE,YAAY,EAAE,MAAM,6BAA6B,CAAC;AAChE,YAAY,EAAE,eAAe,EAAE,MAAM,gCAAgC,CAAC;AACtE,YAAY,EAAE,mBAAmB,EAAE,MAAM,oCAAoC,CAAC;AAC9E,YAAY,EAAE,YAAY,EAAE,MAAM,6BAA6B,CAAC;AAChE,YAAY,EAAE,iBAAiB,EAAE,MAAM,kCAAkC,CAAC;AAC1E,OAAO,EACL,cAAc,EACd,kBAAkB,EAClB,kBAAkB,EAClB,iBAAiB,EACjB,iBAAiB,EACjB,oBAAoB,EACpB,iBAAiB,EACjB,iBAAiB,EACjB,aAAa,EACb,kBAAkB,GACnB,MAAM,oBAAoB,CAAC;AAC5B,OAAO,EAAE,cAAc,EAAE,MAAM,iCAAiC,CAAC;AACjE,OAAO,EAAE,sBAAsB,EAAE,MAAM,sCAAsC,CAAC;AAC9E,OAAO,EAAE,yBAAyB,EAAE,MAAM,0CAA0C,CAAC;AACrF,OAAO,EAAE,mBAAmB,EAAE,MAAM,oCAAoC,CAAC;AACzE,OAAO,EAAE,2BAA2B,EAAE,MAAM,4CAA4C,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,11 @@
1
+ export { NatsEventBus } from './nats-event-bus.js';
2
+ export { DEFAULT_STREAM, DEFAULT_SUBJECT_PREFIX } from './options/nats-event-bus-options.js';
3
+ export { bindingNamesOf } from './topology/binding-names-of.js';
4
+ export { deadStreamOf } from './topology/dead-stream-of.js';
5
+ export { ATTEMPT_HEADER, CORRELATION_HEADER, DEAD_REASON_HEADER, ERROR_CODE_HEADER, EVENT_TYPE_HEADER, EVENT_VERSION_HEADER, MESSAGE_ID_HEADER, REPLAY_FOR_HEADER, SOURCE_HEADER, TRACEPARENT_HEADER, } from './codec/headers.js';
6
+ export { POISON_MESSAGE } from './consumption/poison-message.js';
7
+ export { BrokerUnavailableError } from './errors/broker-unavailable-error.js';
8
+ export { ConsumerNameRequiredError } from './errors/consumer-name-required-error.js';
9
+ export { EventBusClosedError } from './errors/event-bus-closed-error.js';
10
+ export { PublishNotAcknowledgedError } from './errors/publish-not-acknowledged-error.js';
11
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAC;AACnD,OAAO,EAAE,cAAc,EAAE,sBAAsB,EAAE,MAAM,qCAAqC,CAAC;AAO7F,OAAO,EAAE,cAAc,EAAE,MAAM,gCAAgC,CAAC;AAChE,OAAO,EAAE,YAAY,EAAE,MAAM,8BAA8B,CAAC;AAM5D,OAAO,EACL,cAAc,EACd,kBAAkB,EAClB,kBAAkB,EAClB,iBAAiB,EACjB,iBAAiB,EACjB,oBAAoB,EACpB,iBAAiB,EACjB,iBAAiB,EACjB,aAAa,EACb,kBAAkB,GACnB,MAAM,oBAAoB,CAAC;AAC5B,OAAO,EAAE,cAAc,EAAE,MAAM,iCAAiC,CAAC;AACjE,OAAO,EAAE,sBAAsB,EAAE,MAAM,sCAAsC,CAAC;AAC9E,OAAO,EAAE,yBAAyB,EAAE,MAAM,0CAA0C,CAAC;AACrF,OAAO,EAAE,mBAAmB,EAAE,MAAM,oCAAoC,CAAC;AACzE,OAAO,EAAE,2BAA2B,EAAE,MAAM,4CAA4C,CAAC"}
@@ -0,0 +1,3 @@
1
+ /** The name of the transport in what the bus says of itself and of each delivery. */
2
+ export declare const MESSAGING_SYSTEM = "nats";
3
+ //# sourceMappingURL=messaging-system.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"messaging-system.d.ts","sourceRoot":"","sources":["../src/messaging-system.ts"],"names":[],"mappings":"AAAA,qFAAqF;AACrF,eAAO,MAAM,gBAAgB,SAAS,CAAC"}
@@ -0,0 +1,3 @@
1
+ /** The name of the transport in what the bus says of itself and of each delivery. */
2
+ export const MESSAGING_SYSTEM = 'nats';
3
+ //# sourceMappingURL=messaging-system.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"messaging-system.js","sourceRoot":"","sources":["../src/messaging-system.ts"],"names":[],"mappings":"AAAA,qFAAqF;AACrF,MAAM,CAAC,MAAM,gBAAgB,GAAG,MAAM,CAAC"}
@@ -0,0 +1,57 @@
1
+ import { inspect } from 'node:util';
2
+ import type { EventBus, IntegrationEvent, IntegrationEventCallback, IntegrationEventSubscriber, Subscription } from '@ddd-ts-kit/kernel';
3
+ import type { NatsEventBusOptions } from './options/nats-event-bus-options.js';
4
+ import type { ConsumerBinding } from './topology/consumer-binding.js';
5
+ import type { NatsTopology } from './topology/nats-topology.js';
6
+ /**
7
+ * The provider for NATS JetStream.
8
+ *
9
+ * Publishing: one stream capturing `<prefix>.>`, subject = `<prefix>.<event type>`, the event
10
+ * id as message id, and `publish` resolves only when the stream acknowledged every message,
11
+ * so it is a safe target for the outbox relay. Consuming: a durable pull consumer per
12
+ * consumer and event type, acknowledged after the handlers resolved, a bounded number of
13
+ * delayed redeliveries, then a dead-letter stream. Delivery is at least once and unordered:
14
+ * consumers deduplicate.
15
+ */
16
+ export declare class NatsEventBus implements EventBus {
17
+ /** Read by what observes the bus from outside: the name of the transport. */
18
+ readonly messagingSystem = "nats";
19
+ private readonly settings;
20
+ private readonly link;
21
+ private readonly publications;
22
+ private readonly consumption;
23
+ private closed;
24
+ constructor(options: NatsEventBusOptions);
25
+ /** Messages delivered to this bus whose handlers have not finished yet. */
26
+ get deliveriesInFlight(): number;
27
+ /**
28
+ * Resolves when the stream acknowledged every event, rejects otherwise: nothing is
29
+ * buffered for later, so the caller (the outbox relay) knows what to retry.
30
+ */
31
+ publish(events: readonly IntegrationEvent[]): Promise<void>;
32
+ /** Subscribes the consumer named by the `consumer` option. */
33
+ subscribe(type: string, handler: IntegrationEventCallback): Subscription;
34
+ /** A subscriber for one consuming context, on the connection of this bus. */
35
+ forConsumer(consumer: string): IntegrationEventSubscriber;
36
+ /**
37
+ * Connects if needed and creates the streams and the durable consumers of the
38
+ * subscriptions made so far. Idempotent. Resolves once those subscriptions are consuming.
39
+ */
40
+ assertTopology(): Promise<void>;
41
+ describeTopology(): NatsTopology;
42
+ /**
43
+ * Moves up to `limit` messages of a consumer from the dead-letter stream back to the
44
+ * subject they came from, for that consumer only and with a fresh attempt budget. Returns
45
+ * how many were moved.
46
+ */
47
+ replayDead(binding: ConsumerBinding, limit?: number): Promise<number>;
48
+ /** Stops consuming, waits for the handlers in flight and their acknowledgements, disconnects. */
49
+ close(): Promise<void>;
50
+ /** What a serialisation shows of the bus: its name, never its connection. */
51
+ toJSON(): string;
52
+ /** What `console.log` and `util.inspect` show of the bus: the same. */
53
+ [inspect.custom](): string;
54
+ /** Adds a handler to the durable consumer of one consumer for one type, and connects if needed. */
55
+ private consume;
56
+ }
57
+ //# sourceMappingURL=nats-event-bus.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"nats-event-bus.d.ts","sourceRoot":"","sources":["../src/nats-event-bus.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AAGpC,OAAO,KAAK,EACV,QAAQ,EACR,gBAAgB,EAChB,wBAAwB,EACxB,0BAA0B,EAC1B,YAAY,EACb,MAAM,oBAAoB,CAAC;AAQ5B,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,qCAAqC,CAAC;AAM/E,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,gCAAgC,CAAC;AAGtE,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,6BAA6B,CAAC;AAMhE;;;;;;;;;GASG;AACH,qBAAa,YAAa,YAAW,QAAQ;IAC3C,6EAA6E;IAC7E,QAAQ,CAAC,eAAe,UAAoB;IAE5C,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAW;IAEpC,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAa;IAElC,OAAO,CAAC,QAAQ,CAAC,YAAY,CAAe;IAE5C,OAAO,CAAC,QAAQ,CAAC,WAAW,CAAc;IAE1C,OAAO,CAAC,MAAM,CAAS;IAEvB,YAAY,OAAO,EAAE,mBAAmB,EA2BvC;IAED,2EAA2E;IAC3E,IAAI,kBAAkB,IAAI,MAAM,CAE/B;IAED;;;OAGG;IACG,OAAO,CAAC,MAAM,EAAE,SAAS,gBAAgB,EAAE,GAAG,OAAO,CAAC,IAAI,CAAC,CAShE;IAED,8DAA8D;IAC9D,SAAS,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,wBAAwB,GAAG,YAAY,CAMvE;IAED,6EAA6E;IAC7E,WAAW,CAAC,QAAQ,EAAE,MAAM,GAAG,0BAA0B,CAMxD;IAED;;;OAGG;IACG,cAAc,IAAI,OAAO,CAAC,IAAI,CAAC,CAKpC;IAED,gBAAgB,IAAI,YAAY,CAE/B;IAED;;;;OAIG;IACG,UAAU,CAAC,OAAO,EAAE,eAAe,EAAE,KAAK,SAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAsBvE;IAED,iGAAiG;IAC3F,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC,CAS3B;IAED,6EAA6E;IAC7E,MAAM,IAAI,MAAM,CAEf;IAED,uEAAuE;IACvE,CAAC,OAAO,CAAC,MAAM,CAAC,IAAI,MAAM,CAEzB;IAED,mGAAmG;IACnG,OAAO,CAAC,OAAO;CAYhB"}
@@ -0,0 +1,168 @@
1
+ import { inspect } from 'node:util';
2
+ import { jetstreamManager } from '@nats-io/jetstream';
3
+ import { connect } from '@nats-io/transport-node';
4
+ import { REPLAY_FOR_HEADER } from './codec/headers.js';
5
+ import { republication } from './codec/republication.js';
6
+ import { BrokerLink } from './connection/broker-link.js';
7
+ import { Consumption } from './consumption/consumption.js';
8
+ import { ConsumerNameRequiredError } from './errors/consumer-name-required-error.js';
9
+ import { MESSAGING_SYSTEM } from './messaging-system.js';
10
+ import { EventBusClosedError } from './errors/event-bus-closed-error.js';
11
+ import { settingsOf } from './options/settings-of.js';
12
+ import { Publications } from './publication/publications.js';
13
+ import { conceal } from './secrecy/conceal.js';
14
+ import { bindingNamesOf } from './topology/binding-names-of.js';
15
+ import { deadStreamOf } from './topology/dead-stream-of.js';
16
+ import { declareTopology } from './topology/declare-topology.js';
17
+ import { topologyOf } from './topology/topology-of.js';
18
+ const DESCRIPTION = 'NatsEventBus [connection concealed]';
19
+ /**
20
+ * The provider for NATS JetStream.
21
+ *
22
+ * Publishing: one stream capturing `<prefix>.>`, subject = `<prefix>.<event type>`, the event
23
+ * id as message id, and `publish` resolves only when the stream acknowledged every message,
24
+ * so it is a safe target for the outbox relay. Consuming: a durable pull consumer per
25
+ * consumer and event type, acknowledged after the handlers resolved, a bounded number of
26
+ * delayed redeliveries, then a dead-letter stream. Delivery is at least once and unordered:
27
+ * consumers deduplicate.
28
+ */
29
+ export class NatsEventBus {
30
+ /** Read by what observes the bus from outside: the name of the transport. */
31
+ messagingSystem = MESSAGING_SYSTEM;
32
+ settings;
33
+ link;
34
+ publications;
35
+ consumption;
36
+ closed = false;
37
+ constructor(options) {
38
+ this.settings = settingsOf(options, connect);
39
+ this.publications = new Publications(this.settings);
40
+ this.consumption = new Consumption(this.publications, {
41
+ ...this.settings,
42
+ topology: topologySettingsOf(this.settings),
43
+ consumerLost: () => {
44
+ this.link.restart();
45
+ },
46
+ });
47
+ this.link = new BrokerLink(this.settings, {
48
+ opened: async (connection) => {
49
+ this.publications.attach(connection);
50
+ await this.consumption.attach(connection);
51
+ },
52
+ lost: () => {
53
+ this.publications.detach();
54
+ this.consumption.detach();
55
+ },
56
+ failed: (code) => {
57
+ this.settings.onFailure({ kind: 'connection', code });
58
+ },
59
+ });
60
+ conceal(this);
61
+ }
62
+ /** Messages delivered to this bus whose handlers have not finished yet. */
63
+ get deliveriesInFlight() {
64
+ return this.consumption.inFlight;
65
+ }
66
+ /**
67
+ * Resolves when the stream acknowledged every event, rejects otherwise: nothing is
68
+ * buffered for later, so the caller (the outbox relay) knows what to retry.
69
+ */
70
+ async publish(events) {
71
+ if (events.length === 0) {
72
+ return;
73
+ }
74
+ await this.link.opened();
75
+ // The consumers of this bus exist before it publishes: it never misses its own events.
76
+ await this.consumption.started();
77
+ await this.publications.publish(events);
78
+ }
79
+ /** Subscribes the consumer named by the `consumer` option. */
80
+ subscribe(type, handler) {
81
+ if (this.settings.consumer === undefined) {
82
+ throw new ConsumerNameRequiredError();
83
+ }
84
+ return this.consume({ consumer: this.settings.consumer, type }, handler);
85
+ }
86
+ /** A subscriber for one consuming context, on the connection of this bus. */
87
+ forConsumer(consumer) {
88
+ if (consumer.length === 0) {
89
+ throw new ConsumerNameRequiredError();
90
+ }
91
+ return { subscribe: (type, handler) => this.consume({ consumer, type }, handler) };
92
+ }
93
+ /**
94
+ * Connects if needed and creates the streams and the durable consumers of the
95
+ * subscriptions made so far. Idempotent. Resolves once those subscriptions are consuming.
96
+ */
97
+ async assertTopology() {
98
+ const manager = await jetstreamManager(await this.link.opened());
99
+ await declareTopology(manager, this.describeTopology());
100
+ await this.consumption.started();
101
+ }
102
+ describeTopology() {
103
+ return topologyOf(topologySettingsOf(this.settings), this.consumption.bindings());
104
+ }
105
+ /**
106
+ * Moves up to `limit` messages of a consumer from the dead-letter stream back to the
107
+ * subject they came from, for that consumer only and with a fresh attempt budget. Returns
108
+ * how many were moved.
109
+ */
110
+ async replayDead(binding, limit = 100) {
111
+ const { stream, subjectPrefix } = this.settings;
112
+ const { durable, subject, deadSubject } = bindingNamesOf(binding, subjectPrefix);
113
+ const { streams, direct } = await jetstreamManager(await this.link.opened());
114
+ let replayed = 0;
115
+ for (let seq = 1; replayed < limit; replayed += 1) {
116
+ const dead = await direct.getMessage(deadStreamOf(stream), { seq, next_by_subj: deadSubject });
117
+ if (dead === null) {
118
+ break;
119
+ }
120
+ // A message id of its own: inside the duplicate window the original id would drop it.
121
+ const copy = { data: dead.data, headers: dead.header, messageId: `${durable}.replay.${String(dead.seq)}` };
122
+ await this.publications.send(subject, republication(copy, { [REPLAY_FOR_HEADER]: durable }));
123
+ await streams.deleteMessage(deadStreamOf(stream), dead.seq);
124
+ seq = dead.seq + 1;
125
+ }
126
+ return replayed;
127
+ }
128
+ /** Stops consuming, waits for the handlers in flight and their acknowledgements, disconnects. */
129
+ async close() {
130
+ if (this.closed) {
131
+ return;
132
+ }
133
+ this.closed = true;
134
+ await this.consumption.drain();
135
+ await this.link.close();
136
+ this.publications.detach();
137
+ }
138
+ /** What a serialisation shows of the bus: its name, never its connection. */
139
+ toJSON() {
140
+ return DESCRIPTION;
141
+ }
142
+ /** What `console.log` and `util.inspect` show of the bus: the same. */
143
+ [inspect.custom]() {
144
+ return DESCRIPTION;
145
+ }
146
+ /** Adds a handler to the durable consumer of one consumer for one type, and connects if needed. */
147
+ consume(binding, handler) {
148
+ if (this.closed) {
149
+ throw new EventBusClosedError();
150
+ }
151
+ const subscription = this.consumption.add(binding, handler);
152
+ // Connects in the background; a failure is reported by the link and retried.
153
+ void this.link.opened().catch(() => undefined);
154
+ return subscription;
155
+ }
156
+ }
157
+ function topologySettingsOf(settings) {
158
+ return {
159
+ stream: settings.stream,
160
+ subjectPrefix: settings.subjectPrefix,
161
+ storage: settings.storage,
162
+ replicas: settings.replicas,
163
+ duplicateWindowMs: settings.duplicateWindowMs,
164
+ ackWaitMs: settings.ackWaitMs,
165
+ maxAttempts: settings.retry.maxAttempts,
166
+ };
167
+ }
168
+ //# sourceMappingURL=nats-event-bus.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"nats-event-bus.js","sourceRoot":"","sources":["../src/nats-event-bus.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AACpC,OAAO,EAAE,gBAAgB,EAAE,MAAM,oBAAoB,CAAC;AACtD,OAAO,EAAE,OAAO,EAAE,MAAM,yBAAyB,CAAC;AAQlD,OAAO,EAAE,iBAAiB,EAAE,MAAM,oBAAoB,CAAC;AACvD,OAAO,EAAE,a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CAAC,KAAK,EAAE,CAAC;QACxB,IAAI,CAAC,YAAY,CAAC,MAAM,EAAE,CAAC;IAC7B,CAAC;IAED,6EAA6E;IAC7E,MAAM;QACJ,OAAO,WAAW,CAAC;IACrB,CAAC;IAED,uEAAuE;IACvE,CAAC,OAAO,CAAC,MAAM,CAAC;QACd,OAAO,WAAW,CAAC;IACrB,CAAC;IAED,mGAAmG;IAC3F,OAAO,CAAC,OAAwB,EAAE,OAAiC;QACzE,IAAI,IAAI,CAAC,MAAM,EAAE,CAAC;YAChB,MAAM,IAAI,mBAAmB,EAAE,CAAC;QAClC,CAAC;QAED,MAAM,YAAY,GAAG,IAAI,CAAC,WAAW,CAAC,GAAG,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;QAE5D,6EAA6E;QAC7E,KAAK,IAAI,CAAC,IAAI,CAAC,MAAM,EAAE,CAAC,KAAK,CAAC,GAAG,EAAE,CAAC,SAAS,CAAC,CAAC;QAE/C,OAAO,YAAY,CAAC;IACtB,CAAC;CACF;AAED,SAAS,kBAAkB,CAAC,QAAkB;IAC5C,OAAO;QACL,MAAM,EAAE,QAAQ,CAAC,MAAM;QACvB,aAAa,EAAE,QAAQ,CAAC,aAAa;QACrC,OAAO,EAAE,QAAQ,CAAC,OAAO;QACzB,QAAQ,EAAE,QAAQ,CAAC,QAAQ;QAC3B,iBAAiB,EAAE,QAAQ,CAAC,iBAAiB;QAC7C,SAAS,EAAE,QAAQ,CAAC,SAAS;QAC7B,WAAW,EAAE,QAAQ,CAAC,KAAK,CAAC,WAAW;KACxC,CAAC;AACJ,CAAC"}
@@ -0,0 +1,14 @@
1
+ /** One message settled by a consumer of this bus. Counted by outcome, it is the metric to watch. */
2
+ export interface NatsDelivery {
3
+ /** The durable consumer the message was delivered to. */
4
+ readonly consumer: string;
5
+ /** Which delivery of the message this was, starting at 1: the delivery count of the server. */
6
+ readonly attempt: number;
7
+ /**
8
+ * `acknowledged`: every handler resolved. `retried`: refused with a delay, the server
9
+ * delivers it again. `dead`: attempts spent, copied to the dead-letter stream and
10
+ * terminated. `poison`: not an envelope, copied there too.
11
+ */
12
+ readonly outcome: 'acknowledged' | 'retried' | 'dead' | 'poison';
13
+ }
14
+ //# sourceMappingURL=nats-delivery.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"nats-delivery.d.ts","sourceRoot":"","sources":["../../src/options/nats-delivery.ts"],"names":[],"mappings":"AAAA,oGAAoG;AACpG,MAAM,WAAW,YAAY;IAC3B,yDAAyD;IACzD,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;IAC1B,+FAA+F;IAC/F,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IACzB;;;;OAIG;IACH,QAAQ,CAAC,OAAO,EAAE,cAAc,GAAG,SAAS,GAAG,MAAM,GAAG,QAAQ,CAAC;CAClE"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=nats-delivery.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"nats-delivery.js","sourceRoot":"","sources":["../../src/options/nats-delivery.ts"],"names":[],"mappings":""}
@@ -0,0 +1,40 @@
1
+ import type { NatsConnection, NodeConnectionOptions } from '@nats-io/transport-node';
2
+ import type { NatsDelivery } from './nats-delivery.js';
3
+ import type { NatsFailure } from './nats-failure.js';
4
+ import type { ReconnectOptions } from './reconnect-options.js';
5
+ import type { RetryOptions } from './retry-options.js';
6
+ import type { StreamStorage } from './stream-storage.js';
7
+ export declare const DEFAULT_STREAM = "integration-events";
8
+ export declare const DEFAULT_SUBJECT_PREFIX = "events";
9
+ export interface NatsEventBusOptions {
10
+ /**
11
+ * Server URLs, connection options of the client, or a factory opening a NEW connection each
12
+ * time it is called (credentials, TLS). The bus owns what it opens and closes it.
13
+ */
14
+ readonly connection: string | readonly string[] | NodeConnectionOptions | (() => Promise<NatsConnection>);
15
+ /** The stream that keeps the events. Default `integration-events`. */
16
+ readonly stream?: string;
17
+ /** Events are published to `<subjectPrefix>.<event type>`. Default `events`. */
18
+ readonly subjectPrefix?: string;
19
+ /** The consuming context `subscribe` creates its durable consumers for. See also `forConsumer`. */
20
+ readonly consumer?: string;
21
+ /** Messages of one durable consumer this bus handles at once. Default 10. */
22
+ readonly prefetch?: number;
23
+ /** Default `file`. */
24
+ readonly storage?: StreamStorage;
25
+ /** Replicas of the streams the bus creates. Default 1; use 3 on a cluster. */
26
+ readonly replicas?: number;
27
+ /** Time during which the stream drops a second message with the same event id. Default 120000. */
28
+ readonly duplicateWindowMs?: number;
29
+ /** Time a handler has before the server delivers its message to someone else. Default 30000. */
30
+ readonly ackWaitMs?: number;
31
+ /** Time the stream has to acknowledge a publication before `publish` rejects. Default 5000. */
32
+ readonly publishTimeoutMs?: number;
33
+ readonly retry?: RetryOptions;
34
+ readonly reconnect?: ReconnectOptions;
35
+ /** Observability hook. It receives codes only, so it is safe to log as is. */
36
+ readonly onFailure?: (failure: NatsFailure) => void;
37
+ /** Metrics hook, called once per message a consumer settles, before it is acknowledged. */
38
+ readonly onDelivery?: (delivery: NatsDelivery) => void;
39
+ }
40
+ //# sourceMappingURL=nats-event-bus-options.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"nats-event-bus-options.d.ts","sourceRoot":"","sources":["../../src/options/nats-event-bus-options.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,qBAAqB,EAAE,MAAM,yBAAyB,CAAC;AACrF,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AACvD,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AACrD,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,wBAAwB,CAAC;AAC/D,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AACvD,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AAEzD,eAAO,MAAM,cAAc,uBAAuB,CAAC;AACnD,eAAO,MAAM,sBAAsB,WAAW,CAAC;AAE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sourceRoot":"","sources":["../../src/options/nats-event-bus-options.ts"],"names":[],"mappings":"AAOA,MAAM,CAAC,MAAM,cAAc,GAAG,oBAAoB,CAAC;AACnD,MAAM,CAAC,MAAM,sBAAsB,GAAG,QAAQ,CAAC"}
@@ -0,0 +1,17 @@
1
+ export type NatsFailure = {
2
+ readonly kind: 'handler';
3
+ readonly consumer: string;
4
+ readonly eventId: string;
5
+ readonly eventType: string;
6
+ readonly code: string;
7
+ readonly attempt: number;
8
+ readonly dead: boolean;
9
+ } | {
10
+ readonly kind: 'poison';
11
+ readonly consumer: string;
12
+ readonly code: string;
13
+ } | {
14
+ readonly kind: 'connection';
15
+ readonly code: string;
16
+ };
17
+ //# sourceMappingURL=nats-failure.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"nats-failure.d.ts","sourceRoot":"","sources":["../../src/options/nats-failure.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,WAAW,GACnB;IACE,QAAQ,CAAC,IAAI,EAAE,SAAS,CAAC;IACzB,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;IAC1B,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IACzB,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC;IAC3B,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IACzB,QAAQ,CAAC,IAAI,EAAE,OAAO,CAAC;CACxB,GACD;IAAE,QAAQ,CAAC,IAAI,EAAE,QAAQ,CAAC;IAAC,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;IAAC,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAA;CAAE,GAC7E;IAAE,QAAQ,CAAC,IAAI,EAAE,YAAY,CAAC;IAAC,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAA;CAAE,CAAC"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=nats-failure.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"nats-failure.js","sourceRoot":"","sources":["../../src/options/nats-failure.ts"],"names":[],"mappings":""}
@@ -0,0 +1,9 @@
1
+ export interface ReconnectOptions {
2
+ /** Delay before the first reconnection attempt. Default 200. */
3
+ readonly initialMs?: number;
4
+ /** Multiplier applied at each further attempt to open a first connection. Default 2. */
5
+ readonly factor?: number;
6
+ /** Upper bound of the delay. Default 30000. */
7
+ readonly maxMs?: number;
8
+ }
9
+ //# sourceMappingURL=reconnect-options.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"reconnect-options.d.ts","sourceRoot":"","sources":["../../src/options/reconnect-options.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,gBAAgB;IAC/B,gEAAgE;IAChE,QAAQ,CAAC,SAAS,CAAC,EAAE,MAAM,CAAC;IAC5B,wFAAwF;IACxF,QAAQ,CAAC,MAAM,CAAC,EAAE,MAAM,CAAC;IACzB,+CAA+C;IAC/C,QAAQ,CAAC,KAAK,CAAC,EAAE,MAAM,CAAC;CACzB"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=reconnect-options.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"reconnect-options.js","sourceRoot":"","sources":["../../src/options/reconnect-options.ts"],"names":[],"mappings":""}
@@ -0,0 +1,7 @@
1
+ export interface RetryOptions {
2
+ /** Deliveries after which a message goes to the dead-letter stream. Default 5. */
3
+ readonly maxAttempts?: number;
4
+ /** Time the server waits before it delivers a failed message again. Default 5000. */
5
+ readonly delayMs?: number;
6
+ }
7
+ //# sourceMappingURL=retry-options.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"retry-options.d.ts","sourceRoot":"","sources":["../../src/options/retry-options.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,YAAY;IAC3B,kFAAkF;IAClF,QAAQ,CAAC,WAAW,CAAC,EAAE,MAAM,CAAC;IAC9B,qFAAqF;IACrF,QAAQ,CAAC,OAAO,CAAC,EAAE,MAAM,CAAC;CAC3B"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=retry-options.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"retry-options.js","sourceRoot":"","sources":["../../src/options/retry-options.ts"],"names":[],"mappings":""}
@@ -0,0 +1,5 @@
1
+ import type { NatsConnection, NodeConnectionOptions } from '@nats-io/transport-node';
2
+ import { type NatsEventBusOptions } from './nats-event-bus-options.js';
3
+ import type { Settings } from './settings.js';
4
+ export declare function settingsOf(options: NatsEventBusOptions, connect: (target: NodeConnectionOptions) => Promise<NatsConnection>): Settings;
5
+ //# sourceMappingURL=settings-of.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"settings-of.d.ts","sourceRoot":"","sources":["../../src/options/settings-of.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,qBAAqB,EAAE,MAAM,yBAAyB,CAAC;AACrF,OAAO,EAA0C,KAAK,mBAAmB,EAAE,MAAM,6BAA6B,CAAC;AAG/G,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AAE9C,wBAAgB,UAAU,CACxB,OAAO,EAAE,mBAAmB,EAC5B,OAAO,EAAE,CAAC,MAAM,EAAE,qBAAqB,KAAK,OAAO,CAAC,cAAc,CAAC,GAClE,QAAQ,CAoBV"}
@@ -0,0 +1,55 @@
1
+ import { DEFAULT_STREAM, DEFAULT_SUBJECT_PREFIX } from './nats-event-bus-options.js';
2
+ export function settingsOf(options, connect) {
3
+ const reconnect = reconnectOf(options.reconnect);
4
+ const { connection } = options;
5
+ return {
6
+ open: typeof connection === 'function' ? connection : () => connect(targetOf(connection, reconnect)),
7
+ stream: options.stream ?? DEFAULT_STREAM,
8
+ subjectPrefix: options.subjectPrefix ?? DEFAULT_SUBJECT_PREFIX,
9
+ consumer: options.consumer,
10
+ prefetch: options.prefetch ?? 10,
11
+ storage: options.storage ?? 'file',
12
+ replicas: options.replicas ?? 1,
13
+ duplicateWindowMs: options.duplicateWindowMs ?? 120_000,
14
+ ackWaitMs: options.ackWaitMs ?? 30_000,
15
+ publishTimeoutMs: options.publishTimeoutMs ?? 5_000,
16
+ retry: retryOf(options.retry),
17
+ reconnect,
18
+ onFailure: guarded(options.onFailure),
19
+ onDelivery: guarded(options.onDelivery),
20
+ };
21
+ }
22
+ // The client reconnects by itself, for ever unless told otherwise: the bus only has to open
23
+ // the first connection, and another one if the client gives up.
24
+ function targetOf(connection, reconnect) {
25
+ const given = typeof connection === 'string' || isList(connection) ? { servers: [...asList(connection)] } : connection;
26
+ return { maxReconnectAttempts: -1, reconnectTimeWait: reconnect.initialMs, ...given };
27
+ }
28
+ function isList(connection) {
29
+ return Array.isArray(connection);
30
+ }
31
+ function asList(connection) {
32
+ return typeof connection === 'string' ? [connection] : connection;
33
+ }
34
+ function retryOf(retry = {}) {
35
+ return { maxAttempts: retry.maxAttempts ?? 5, delayMs: retry.delayMs ?? 5_000 };
36
+ }
37
+ function reconnectOf(reconnect = {}) {
38
+ return {
39
+ initialMs: reconnect.initialMs ?? 200,
40
+ factor: reconnect.factor ?? 2,
41
+ maxMs: reconnect.maxMs ?? 30_000,
42
+ };
43
+ }
44
+ // A hook that throws must not leave a message unacknowledged or stop a reconnection.
45
+ function guarded(hook) {
46
+ return (value) => {
47
+ try {
48
+ hook?.(value);
49
+ }
50
+ catch {
51
+ return;
52
+ }
53
+ };
54
+ }
55
+ //# sourceMappingURL=settings-of.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"settings-of.js","sourceRoot":"","sources":["../../src/options/settings-of.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,cAAc,EAAE,sBAAsB,EAA4B,MAAM,6BAA6B,CAAC;AAK/G,MAAM,UAAU,UAAU,CACxB,OAA4B,EAC5B,OAAmE;IAEnE,MAAM,SAAS,GAAG,WAAW,CAAC,OAAO,CAAC,SAAS,CAAC,CAAC;IACjD,MAAM,EAAE,UAAU,EAAE,GAAG,OAAO,CAAC;IAE/B,OAAO;QACL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