amqpx 0.0.0 → 1.0.1

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 (245) hide show
  1. package/LICENSE +8 -0
  2. package/README.md +63 -0
  3. package/dist/cjs/channel/channel-implementation.d.ts +30 -0
  4. package/dist/cjs/channel/channel-implementation.js +212 -0
  5. package/dist/cjs/channel/channel.d.ts +149 -0
  6. package/dist/cjs/channel/channel.js +2 -0
  7. package/dist/cjs/channel/index.d.ts +2 -0
  8. package/dist/cjs/channel/index.js +5 -0
  9. package/dist/cjs/channel/types.d.ts +12 -0
  10. package/dist/cjs/channel/types.js +2 -0
  11. package/dist/cjs/connection/connection-implementation.d.ts +35 -0
  12. package/dist/cjs/connection/connection-implementation.js +195 -0
  13. package/dist/cjs/connection/connection.d.ts +143 -0
  14. package/dist/cjs/connection/connection.js +11 -0
  15. package/dist/cjs/connection/index.d.ts +2 -0
  16. package/dist/cjs/connection/index.js +8 -0
  17. package/dist/cjs/consumer/base-consumer.d.ts +33 -0
  18. package/dist/cjs/consumer/base-consumer.js +85 -0
  19. package/dist/cjs/consumer/batch-consumer-implementation.d.ts +25 -0
  20. package/dist/cjs/consumer/batch-consumer-implementation.js +313 -0
  21. package/dist/cjs/consumer/batch-consumer.d.ts +39 -0
  22. package/dist/cjs/consumer/batch-consumer.js +2 -0
  23. package/dist/cjs/consumer/consumer-implementation.d.ts +12 -0
  24. package/dist/cjs/consumer/consumer-implementation.js +105 -0
  25. package/dist/cjs/consumer/consumer.d.ts +40 -0
  26. package/dist/cjs/consumer/consumer.js +4 -0
  27. package/dist/cjs/consumer/index.d.ts +5 -0
  28. package/dist/cjs/consumer/index.js +10 -0
  29. package/dist/cjs/consumer/types.d.ts +194 -0
  30. package/dist/cjs/consumer/types.js +34 -0
  31. package/dist/cjs/errors.d.ts +22 -0
  32. package/dist/cjs/errors.js +13 -0
  33. package/dist/cjs/exchange/default-exchange.d.ts +10 -0
  34. package/dist/cjs/exchange/default-exchange.js +14 -0
  35. package/dist/cjs/exchange/exchange-implementation.d.ts +24 -0
  36. package/dist/cjs/exchange/exchange-implementation.js +159 -0
  37. package/dist/cjs/exchange/exchange.d.ts +67 -0
  38. package/dist/cjs/exchange/exchange.js +2 -0
  39. package/dist/cjs/exchange/index.d.ts +3 -0
  40. package/dist/cjs/exchange/index.js +5 -0
  41. package/dist/cjs/exchange/types.d.ts +58 -0
  42. package/dist/cjs/exchange/types.js +8 -0
  43. package/dist/cjs/extensions/jest/index.d.ts +7 -0
  44. package/dist/cjs/extensions/jest/index.js +23 -0
  45. package/dist/cjs/extensions/jest/test-batch-consumer.d.ts +48 -0
  46. package/dist/cjs/extensions/jest/test-batch-consumer.js +56 -0
  47. package/dist/cjs/extensions/jest/test-channel.d.ts +71 -0
  48. package/dist/cjs/extensions/jest/test-channel.js +75 -0
  49. package/dist/cjs/extensions/jest/test-connection.d.ts +38 -0
  50. package/dist/cjs/extensions/jest/test-connection.js +29 -0
  51. package/dist/cjs/extensions/jest/test-consumer.d.ts +49 -0
  52. package/dist/cjs/extensions/jest/test-consumer.js +54 -0
  53. package/dist/cjs/extensions/jest/test-exchange.d.ts +26 -0
  54. package/dist/cjs/extensions/jest/test-exchange.js +16 -0
  55. package/dist/cjs/extensions/jest/test-producer.d.ts +33 -0
  56. package/dist/cjs/extensions/jest/test-producer.js +17 -0
  57. package/dist/cjs/extensions/jest/test-queue.d.ts +25 -0
  58. package/dist/cjs/extensions/jest/test-queue.js +17 -0
  59. package/dist/cjs/extensions/vitest/index.d.ts +7 -0
  60. package/dist/cjs/extensions/vitest/index.js +23 -0
  61. package/dist/cjs/extensions/vitest/test-batch-consumer.d.ts +48 -0
  62. package/dist/cjs/extensions/vitest/test-batch-consumer.js +56 -0
  63. package/dist/cjs/extensions/vitest/test-channel.d.ts +71 -0
  64. package/dist/cjs/extensions/vitest/test-channel.js +77 -0
  65. package/dist/cjs/extensions/vitest/test-connection.d.ts +38 -0
  66. package/dist/cjs/extensions/vitest/test-connection.js +29 -0
  67. package/dist/cjs/extensions/vitest/test-consumer.d.ts +49 -0
  68. package/dist/cjs/extensions/vitest/test-consumer.js +54 -0
  69. package/dist/cjs/extensions/vitest/test-exchange.d.ts +26 -0
  70. package/dist/cjs/extensions/vitest/test-exchange.js +16 -0
  71. package/dist/cjs/extensions/vitest/test-producer.d.ts +34 -0
  72. package/dist/cjs/extensions/vitest/test-producer.js +18 -0
  73. package/dist/cjs/extensions/vitest/test-queue.d.ts +25 -0
  74. package/dist/cjs/extensions/vitest/test-queue.js +17 -0
  75. package/dist/cjs/extensions/zod/index.d.ts +2 -0
  76. package/dist/cjs/extensions/zod/index.js +18 -0
  77. package/dist/cjs/extensions/zod/zod-validated-batch-consumer.d.ts +57 -0
  78. package/dist/cjs/extensions/zod/zod-validated-batch-consumer.js +46 -0
  79. package/dist/cjs/extensions/zod/zod-validated-consumer.d.ts +55 -0
  80. package/dist/cjs/extensions/zod/zod-validated-consumer.js +42 -0
  81. package/dist/cjs/graceful-shutdown/index.d.ts +1 -0
  82. package/dist/cjs/graceful-shutdown/index.js +17 -0
  83. package/dist/cjs/graceful-shutdown/rabbit-closer.d.ts +29 -0
  84. package/dist/cjs/graceful-shutdown/rabbit-closer.js +42 -0
  85. package/dist/cjs/index.d.ts +11 -0
  86. package/dist/cjs/index.js +51 -0
  87. package/dist/cjs/package.json +1 -0
  88. package/dist/cjs/predefined.d.ts +62 -0
  89. package/dist/cjs/predefined.js +27 -0
  90. package/dist/cjs/producer/index.d.ts +3 -0
  91. package/dist/cjs/producer/index.js +5 -0
  92. package/dist/cjs/producer/producer-implementation.d.ts +18 -0
  93. package/dist/cjs/producer/producer-implementation.js +109 -0
  94. package/dist/cjs/producer/producer.d.ts +44 -0
  95. package/dist/cjs/producer/producer.js +2 -0
  96. package/dist/cjs/producer/types.d.ts +70 -0
  97. package/dist/cjs/producer/types.js +13 -0
  98. package/dist/cjs/queue/index.d.ts +3 -0
  99. package/dist/cjs/queue/index.js +5 -0
  100. package/dist/cjs/queue/queue-implementation.d.ts +21 -0
  101. package/dist/cjs/queue/queue-implementation.js +91 -0
  102. package/dist/cjs/queue/queue.d.ts +55 -0
  103. package/dist/cjs/queue/queue.js +2 -0
  104. package/dist/cjs/queue/types.d.ts +25 -0
  105. package/dist/cjs/queue/types.js +2 -0
  106. package/dist/cjs/retry/default-retry-strategy.d.ts +9 -0
  107. package/dist/cjs/retry/default-retry-strategy.js +8 -0
  108. package/dist/cjs/retry/index.d.ts +4 -0
  109. package/dist/cjs/retry/index.js +45 -0
  110. package/dist/cjs/retry/retry-implementation.d.ts +18 -0
  111. package/dist/cjs/retry/retry-implementation.js +44 -0
  112. package/dist/cjs/retry/retry-strategy.d.ts +28 -0
  113. package/dist/cjs/retry/retry-strategy.js +10 -0
  114. package/dist/cjs/retry/time-strategies.d.ts +45 -0
  115. package/dist/cjs/retry/time-strategies.js +32 -0
  116. package/dist/cjs/types.d.ts +30 -0
  117. package/dist/cjs/types.js +9 -0
  118. package/dist/cjs/utils.d.ts +11 -0
  119. package/dist/cjs/utils.js +83 -0
  120. package/dist/esm/channel/channel-implementation.d.ts +30 -0
  121. package/dist/esm/channel/channel-implementation.js +208 -0
  122. package/dist/esm/channel/channel.d.ts +149 -0
  123. package/dist/esm/channel/channel.js +1 -0
  124. package/dist/esm/channel/index.d.ts +2 -0
  125. package/dist/esm/channel/index.js +1 -0
  126. package/dist/esm/channel/types.d.ts +12 -0
  127. package/dist/esm/channel/types.js +1 -0
  128. package/dist/esm/connection/connection-implementation.d.ts +35 -0
  129. package/dist/esm/connection/connection-implementation.js +157 -0
  130. package/dist/esm/connection/connection.d.ts +143 -0
  131. package/dist/esm/connection/connection.js +8 -0
  132. package/dist/esm/connection/index.d.ts +2 -0
  133. package/dist/esm/connection/index.js +2 -0
  134. package/dist/esm/consumer/base-consumer.d.ts +33 -0
  135. package/dist/esm/consumer/base-consumer.js +81 -0
  136. package/dist/esm/consumer/batch-consumer-implementation.d.ts +25 -0
  137. package/dist/esm/consumer/batch-consumer-implementation.js +309 -0
  138. package/dist/esm/consumer/batch-consumer.d.ts +39 -0
  139. package/dist/esm/consumer/batch-consumer.js +1 -0
  140. package/dist/esm/consumer/consumer-implementation.d.ts +12 -0
  141. package/dist/esm/consumer/consumer-implementation.js +101 -0
  142. package/dist/esm/consumer/consumer.d.ts +40 -0
  143. package/dist/esm/consumer/consumer.js +1 -0
  144. package/dist/esm/consumer/index.d.ts +5 -0
  145. package/dist/esm/consumer/index.js +3 -0
  146. package/dist/esm/consumer/types.d.ts +194 -0
  147. package/dist/esm/consumer/types.js +31 -0
  148. package/dist/esm/errors.d.ts +22 -0
  149. package/dist/esm/errors.js +8 -0
  150. package/dist/esm/exchange/default-exchange.d.ts +10 -0
  151. package/dist/esm/exchange/default-exchange.js +10 -0
  152. package/dist/esm/exchange/exchange-implementation.d.ts +24 -0
  153. package/dist/esm/exchange/exchange-implementation.js +152 -0
  154. package/dist/esm/exchange/exchange.d.ts +67 -0
  155. package/dist/esm/exchange/exchange.js +1 -0
  156. package/dist/esm/exchange/index.d.ts +3 -0
  157. package/dist/esm/exchange/index.js +1 -0
  158. package/dist/esm/exchange/types.d.ts +58 -0
  159. package/dist/esm/exchange/types.js +5 -0
  160. package/dist/esm/extensions/jest/index.d.ts +7 -0
  161. package/dist/esm/extensions/jest/index.js +7 -0
  162. package/dist/esm/extensions/jest/test-batch-consumer.d.ts +48 -0
  163. package/dist/esm/extensions/jest/test-batch-consumer.js +52 -0
  164. package/dist/esm/extensions/jest/test-channel.d.ts +71 -0
  165. package/dist/esm/extensions/jest/test-channel.js +71 -0
  166. package/dist/esm/extensions/jest/test-connection.d.ts +38 -0
  167. package/dist/esm/extensions/jest/test-connection.js +25 -0
  168. package/dist/esm/extensions/jest/test-consumer.d.ts +49 -0
  169. package/dist/esm/extensions/jest/test-consumer.js +50 -0
  170. package/dist/esm/extensions/jest/test-exchange.d.ts +26 -0
  171. package/dist/esm/extensions/jest/test-exchange.js +12 -0
  172. package/dist/esm/extensions/jest/test-producer.d.ts +33 -0
  173. package/dist/esm/extensions/jest/test-producer.js +13 -0
  174. package/dist/esm/extensions/jest/test-queue.d.ts +25 -0
  175. package/dist/esm/extensions/jest/test-queue.js +13 -0
  176. package/dist/esm/extensions/vitest/index.d.ts +7 -0
  177. package/dist/esm/extensions/vitest/index.js +7 -0
  178. package/dist/esm/extensions/vitest/test-batch-consumer.d.ts +48 -0
  179. package/dist/esm/extensions/vitest/test-batch-consumer.js +52 -0
  180. package/dist/esm/extensions/vitest/test-channel.d.ts +71 -0
  181. package/dist/esm/extensions/vitest/test-channel.js +73 -0
  182. package/dist/esm/extensions/vitest/test-connection.d.ts +38 -0
  183. package/dist/esm/extensions/vitest/test-connection.js +25 -0
  184. package/dist/esm/extensions/vitest/test-consumer.d.ts +49 -0
  185. package/dist/esm/extensions/vitest/test-consumer.js +50 -0
  186. package/dist/esm/extensions/vitest/test-exchange.d.ts +26 -0
  187. package/dist/esm/extensions/vitest/test-exchange.js +12 -0
  188. package/dist/esm/extensions/vitest/test-producer.d.ts +34 -0
  189. package/dist/esm/extensions/vitest/test-producer.js +14 -0
  190. package/dist/esm/extensions/vitest/test-queue.d.ts +25 -0
  191. package/dist/esm/extensions/vitest/test-queue.js +13 -0
  192. package/dist/esm/extensions/zod/index.d.ts +2 -0
  193. package/dist/esm/extensions/zod/index.js +2 -0
  194. package/dist/esm/extensions/zod/zod-validated-batch-consumer.d.ts +57 -0
  195. package/dist/esm/extensions/zod/zod-validated-batch-consumer.js +42 -0
  196. package/dist/esm/extensions/zod/zod-validated-consumer.d.ts +55 -0
  197. package/dist/esm/extensions/zod/zod-validated-consumer.js +38 -0
  198. package/dist/esm/graceful-shutdown/index.d.ts +1 -0
  199. package/dist/esm/graceful-shutdown/index.js +1 -0
  200. package/dist/esm/graceful-shutdown/rabbit-closer.d.ts +29 -0
  201. package/dist/esm/graceful-shutdown/rabbit-closer.js +38 -0
  202. package/dist/esm/index.d.ts +11 -0
  203. package/dist/esm/index.js +11 -0
  204. package/dist/esm/package.json +1 -0
  205. package/dist/esm/predefined.d.ts +62 -0
  206. package/dist/esm/predefined.js +18 -0
  207. package/dist/esm/producer/index.d.ts +3 -0
  208. package/dist/esm/producer/index.js +1 -0
  209. package/dist/esm/producer/producer-implementation.d.ts +18 -0
  210. package/dist/esm/producer/producer-implementation.js +105 -0
  211. package/dist/esm/producer/producer.d.ts +44 -0
  212. package/dist/esm/producer/producer.js +1 -0
  213. package/dist/esm/producer/types.d.ts +70 -0
  214. package/dist/esm/producer/types.js +10 -0
  215. package/dist/esm/queue/index.d.ts +3 -0
  216. package/dist/esm/queue/index.js +1 -0
  217. package/dist/esm/queue/queue-implementation.d.ts +21 -0
  218. package/dist/esm/queue/queue-implementation.js +87 -0
  219. package/dist/esm/queue/queue.d.ts +55 -0
  220. package/dist/esm/queue/queue.js +1 -0
  221. package/dist/esm/queue/types.d.ts +25 -0
  222. package/dist/esm/queue/types.js +1 -0
  223. package/dist/esm/retry/default-retry-strategy.d.ts +9 -0
  224. package/dist/esm/retry/default-retry-strategy.js +5 -0
  225. package/dist/esm/retry/index.d.ts +4 -0
  226. package/dist/esm/retry/index.js +4 -0
  227. package/dist/esm/retry/retry-implementation.d.ts +18 -0
  228. package/dist/esm/retry/retry-implementation.js +40 -0
  229. package/dist/esm/retry/retry-strategy.d.ts +28 -0
  230. package/dist/esm/retry/retry-strategy.js +6 -0
  231. package/dist/esm/retry/time-strategies.d.ts +45 -0
  232. package/dist/esm/retry/time-strategies.js +24 -0
  233. package/dist/esm/types.d.ts +30 -0
  234. package/dist/esm/types.js +6 -0
  235. package/dist/esm/utils.d.ts +11 -0
  236. package/dist/esm/utils.js +72 -0
  237. package/package.json +101 -6
  238. package/.claude/settings.local.json +0 -13
  239. package/.idea/amqp-oop.iml +0 -15
  240. package/.idea/aws.xml +0 -11
  241. package/.idea/inspectionProfiles/Project_Default.xml +0 -6
  242. package/.idea/jsLibraryMappings.xml +0 -6
  243. package/.idea/jsLinters/eslint.xml +0 -6
  244. package/.idea/modules.xml +0 -8
  245. package/.idea/vcs.xml +0 -6
package/LICENSE ADDED
@@ -0,0 +1,8 @@
1
+ Copyright 2026 Patrik Valkovič
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
8
+
package/README.md ADDED
@@ -0,0 +1,63 @@
1
+ # amqpx
2
+
3
+ Type-safe RabbitMQ messaging for TypeScript, built on top of [amqplib](https://www.npmjs.com/package/amqplib).
4
+
5
+ - **Generic producers and consumers** — define message shapes once, TypeScript enforces them everywhere
6
+ - **Automatic reconnect** — pluggable retry strategies handle broker failures transparently
7
+ - **Composable topology** — Connection → Channel → Exchange / Queue → Producer / Consumer
8
+ - **Failure strategies** — drop, requeue, or reject on handler errors, per consumer
9
+ - **Batch consuming** — built-in batching with configurable size, timeouts, and ack coalescing
10
+ - **Extensions** — optional Zod validation and in-memory test doubles for Vitest/Jest
11
+
12
+ ## Installation
13
+
14
+ ```bash
15
+ npm install amqpx
16
+ ```
17
+
18
+ | Entry point | Contents |
19
+ |---|---|
20
+ | `amqpx` | Core entities |
21
+ | `amqpx/zod` | `ZodValidatedConsumer`, `ZodValidatedBatchConsumer` |
22
+ | `amqpx/jest` | Test doubles for Jest |
23
+ | `amqpx/vitest` | Test doubles for Vitest |
24
+
25
+ ## Quick example
26
+
27
+ ```typescript
28
+ import { connect, AssertionMode } from 'amqpx'
29
+
30
+ type Order = { orderId: string; total: number }
31
+
32
+ const connection = await connect({
33
+ hostname: 'localhost',
34
+ username: 'guest',
35
+ password: 'guest',
36
+ })
37
+
38
+ // Topology
39
+ const channel = await connection.createChannel()
40
+ const queue = await channel.createQueue('orders', { durable: true })
41
+
42
+ // Produce
43
+ const producer = await queue.createProducer<Order>()
44
+ await producer.publish({ orderId: 'abc-123', total: 49.99 })
45
+
46
+ // Consume
47
+ const consumer = await consumerQueue.createConsumer<Order>({ prefetch: 10 })
48
+ await consumer.listen(async ({ message }) => {
49
+ console.log('order', message.orderId, 'total', message.total)
50
+ })
51
+ ```
52
+
53
+ ## Documentation
54
+
55
+ Full documentation at **https://patrikvalkovic.github.io/amqpx/**
56
+
57
+ - [Guide](https://patrikvalkovic.github.io/amqpx/guide/what-is-amqpx)
58
+ - [Examples](https://patrikvalkovic.github.io/amqpx/examples/)
59
+ - [API Reference](https://patrikvalkovic.github.io/amqpx/api/)
60
+
61
+ ## License
62
+
63
+ MIT
@@ -0,0 +1,30 @@
1
+ import { EventEmitter } from 'events';
2
+ import * as amqp from 'amqplib';
3
+ import { Replies } from 'amqplib';
4
+ import { Connection } from '../connection';
5
+ import { Exchange, ExchangeTypes, ExchangeConsumerQueueOptions, ExchangeConsumerOptions, ExchangeBatchConsumerOptions, ExchangeOptions } from '../exchange';
6
+ import { Queue, QueueOptions } from '../queue';
7
+ import { ProducerOptions, Producer } from '../producer';
8
+ import { Consumer, ConsumerOptions, BatchConsumer, BatchConsumerOptions } from '../consumer';
9
+ import { Channel, ChannelEventMap } from './channel';
10
+ import { ChannelPublishOptions } from './types';
11
+ export declare class ChannelImplementation extends EventEmitter<ChannelEventMap> implements Channel {
12
+ readonly connection: Connection;
13
+ private readonly wrapper;
14
+ private closeHandler;
15
+ private drainPromise;
16
+ constructor(connection: Connection, publishConfirm: boolean);
17
+ connect(): Promise<ChannelImplementation>;
18
+ close(timeout?: number): Promise<void>;
19
+ createExchange(name: string, type: ExchangeTypes, options?: ExchangeOptions): Promise<Exchange>;
20
+ createQueue(name: string, options?: QueueOptions): Promise<Queue>;
21
+ checkQueue(name: string): Promise<Replies.AssertQueue>;
22
+ native(): Promise<amqp.Channel | amqp.ConfirmChannel>;
23
+ publish(exchange: string, routingKey: string, content: Buffer, optionsArgs: ChannelPublishOptions): Promise<boolean>;
24
+ createProducerForQueue<T>(queue: Queue, options?: ProducerOptions<T>): Promise<Producer<T>>;
25
+ createProducerForExchange<T>(exchange: Exchange, options?: ProducerOptions<T>): Promise<Producer<T>>;
26
+ createConsumerForQueue<T>(queue: Queue, options?: ConsumerOptions<T>): Promise<Consumer<T>>;
27
+ createConsumerForExchange<T>(exchange: Exchange, options: ExchangeConsumerOptions<T>, queueOptions?: ExchangeConsumerQueueOptions): Promise<Consumer<T>>;
28
+ createBatchConsumerForQueue<T>(queue: Queue, options?: BatchConsumerOptions<T>): Promise<BatchConsumer<T>>;
29
+ createBatchConsumerForExchange<T>(exchange: Exchange, options: ExchangeBatchConsumerOptions<T>, queueOptions?: ExchangeConsumerQueueOptions): Promise<BatchConsumer<T>>;
30
+ }
@@ -0,0 +1,212 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __rest = (this && this.__rest) || function (s, e) {
12
+ var t = {};
13
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
14
+ t[p] = s[p];
15
+ if (s != null && typeof Object.getOwnPropertySymbols === "function")
16
+ for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
17
+ if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
18
+ t[p[i]] = s[p[i]];
19
+ }
20
+ return t;
21
+ };
22
+ Object.defineProperty(exports, "__esModule", { value: true });
23
+ exports.ChannelImplementation = void 0;
24
+ const util_1 = require("util");
25
+ const events_1 = require("events");
26
+ const perf_hooks_1 = require("perf_hooks");
27
+ const exchange_1 = require("../exchange");
28
+ const queue_1 = require("../queue");
29
+ const retry_1 = require("../retry");
30
+ const errors_1 = require("../errors");
31
+ const utils_1 = require("../utils");
32
+ const debug = (0, util_1.debuglog)(`${utils_1.LIB_NAME}:channel`);
33
+ class ChannelImplementation extends events_1.EventEmitter {
34
+ constructor(connection, publishConfirm) {
35
+ super();
36
+ this.connection = connection;
37
+ this.closeHandler = null;
38
+ this.drainPromise = null;
39
+ this.wrapper = {
40
+ channel: null,
41
+ isConfirmed: publishConfirm,
42
+ };
43
+ }
44
+ connect() {
45
+ return __awaiter(this, void 0, void 0, function* () {
46
+ if (this.wrapper.channel) {
47
+ yield this.wrapper.channel;
48
+ return this;
49
+ }
50
+ this.wrapper.channel = (() => __awaiter(this, void 0, void 0, function* () {
51
+ try {
52
+ debug('connecting confirmed=%s', this.wrapper.isConfirmed);
53
+ const connection = yield this.connection.native();
54
+ const channel = yield (this.wrapper.isConfirmed ? connection.createConfirmChannel() : connection.createChannel());
55
+ debug('connected confirmed=%s', this.wrapper.isConfirmed);
56
+ channel.on('error', error => {
57
+ this.wrapper.channel = null;
58
+ debug('native-error error=%s', error === null || error === void 0 ? void 0 : error.message);
59
+ this.emit('error', error);
60
+ });
61
+ channel.on(`close`, () => __awaiter(this, void 0, void 0, function* () {
62
+ this.wrapper.channel = null;
63
+ debug('native-close');
64
+ this.emit('close');
65
+ }));
66
+ channel.on('drain', () => {
67
+ debug('native-drain');
68
+ this.emit('drain');
69
+ });
70
+ return channel;
71
+ }
72
+ catch (err) {
73
+ this.wrapper.channel = null;
74
+ throw err;
75
+ }
76
+ }))();
77
+ yield this.wrapper.channel;
78
+ return this;
79
+ });
80
+ }
81
+ close() {
82
+ return __awaiter(this, arguments, void 0, function* (timeout = 30000) {
83
+ var _a;
84
+ if (!this.wrapper.channel)
85
+ return;
86
+ (_a = this.closeHandler) !== null && _a !== void 0 ? _a : (this.closeHandler = (() => __awaiter(this, void 0, void 0, function* () {
87
+ try {
88
+ debug('closing');
89
+ const wrapper = {
90
+ isConfirmed: this.wrapper.isConfirmed,
91
+ channel: yield this.wrapper.channel,
92
+ };
93
+ if (!wrapper.channel)
94
+ throw new Error('Internal error: channel is empty but app attempted to close it');
95
+ const doClose = (arg) => __awaiter(this, void 0, void 0, function* () {
96
+ if (!arg.channel)
97
+ return;
98
+ if (wrapper.isConfirmed)
99
+ yield (0, utils_1.swallowError)(arg.channel.waitForConfirms());
100
+ yield (0, utils_1.swallowError)(arg.channel.close());
101
+ });
102
+ const abort = new AbortController();
103
+ yield Promise.race([
104
+ doClose(wrapper).then(() => abort.abort()),
105
+ (0, utils_1.sleepPromise)(timeout).then(() => {
106
+ if (!abort.signal.aborted)
107
+ throw new Error('Channel close timed out');
108
+ }),
109
+ ]);
110
+ debug('closed');
111
+ }
112
+ finally {
113
+ this.closeHandler = null;
114
+ this.wrapper.channel = null;
115
+ }
116
+ }))());
117
+ return this.closeHandler;
118
+ });
119
+ }
120
+ createExchange(name, type, options) {
121
+ return new exchange_1.ExchangeImplementation(this, name, type, options).assert();
122
+ }
123
+ createQueue(name, options) {
124
+ return new queue_1.QueueImplementation(this, name, options).assert();
125
+ }
126
+ checkQueue(name) {
127
+ return __awaiter(this, void 0, void 0, function* () {
128
+ const nativeChannel = yield this.native();
129
+ return nativeChannel.checkQueue(name);
130
+ });
131
+ }
132
+ native() {
133
+ return __awaiter(this, void 0, void 0, function* () {
134
+ yield this.connect();
135
+ if (this.wrapper.channel)
136
+ return this.wrapper.channel;
137
+ throw new Error(`Channel not created`);
138
+ });
139
+ }
140
+ publish(exchange, routingKey, content, optionsArgs) {
141
+ return __awaiter(this, void 0, void 0, function* () {
142
+ debug('publish exchange=%s routing-key=%s confirmed=%s', exchange, routingKey, this.wrapper.isConfirmed);
143
+ const { drainTimeout, retryStrategy = retry_1.DEFAULT_RETRY_STRATEGY } = optionsArgs, options = __rest(optionsArgs, ["drainTimeout", "retryStrategy"]);
144
+ while (true) {
145
+ const native = yield this.native();
146
+ if (this.drainPromise)
147
+ yield this.drainPromise;
148
+ let publishResult;
149
+ if (!this.wrapper.isConfirmed) {
150
+ const channel = native;
151
+ publishResult = channel.publish(exchange, routingKey, content, options);
152
+ }
153
+ else {
154
+ const channel = native;
155
+ publishResult = yield (0, retry_1.retryLoop)(retryStrategy, () => new Promise((resolve, reject) => {
156
+ const status = channel.publish(exchange, routingKey, content, options, err => {
157
+ err ? reject(err) : resolve(status);
158
+ });
159
+ }), err => {
160
+ debug('publish-nacked error=%s', (0, utils_1.errToMessage)(err));
161
+ return (0, utils_1.errToMessage)(err) === 'message nacked';
162
+ });
163
+ }
164
+ if (publishResult) {
165
+ debug('publish-success exchange=%s routing-key=%s', exchange, routingKey);
166
+ return this.wrapper.isConfirmed;
167
+ }
168
+ if (!this.drainPromise) {
169
+ const drainStart = perf_hooks_1.performance.now();
170
+ debug('backpressure-detected exchange=%s routing-key=%s', exchange, routingKey);
171
+ this.drainPromise = new Promise((resolve, reject) => {
172
+ let timeoutHandler;
173
+ const drainHandler = () => {
174
+ clearTimeout(timeoutHandler);
175
+ debug('drain-cleared after=%dms', perf_hooks_1.performance.now() - drainStart);
176
+ this.drainPromise = null;
177
+ resolve();
178
+ };
179
+ this.once('drain', drainHandler);
180
+ this.once('close', drainHandler);
181
+ timeoutHandler = setTimeout(() => __awaiter(this, void 0, void 0, function* () {
182
+ this.removeListener('drain', drainHandler);
183
+ this.drainPromise = null;
184
+ debug('drain-timeout exchange=%s routing-key=%s timeout=%dms', exchange, routingKey, drainTimeout);
185
+ reject(new errors_1.DrainError('Rabbit drain timeout'));
186
+ void native.close();
187
+ }), drainTimeout);
188
+ });
189
+ }
190
+ }
191
+ });
192
+ }
193
+ createProducerForQueue(queue, options = {}) {
194
+ return queue.createProducer(Object.assign({ channel: this }, options));
195
+ }
196
+ createProducerForExchange(exchange, options = {}) {
197
+ return exchange.createProducer(Object.assign({ channel: this }, options));
198
+ }
199
+ createConsumerForQueue(queue, options) {
200
+ return queue.createConsumer(Object.assign({ channel: this }, options));
201
+ }
202
+ createConsumerForExchange(exchange, options, queueOptions) {
203
+ return exchange.createConsumer(Object.assign({ channel: this }, options), queueOptions);
204
+ }
205
+ createBatchConsumerForQueue(queue, options) {
206
+ return queue.createBatchConsumer(Object.assign({ channel: this }, options));
207
+ }
208
+ createBatchConsumerForExchange(exchange, options, queueOptions) {
209
+ return exchange.createBatchConsumer(Object.assign({ channel: this }, options), queueOptions);
210
+ }
211
+ }
212
+ exports.ChannelImplementation = ChannelImplementation;
@@ -0,0 +1,149 @@
1
+ import { EventEmitter } from 'events';
2
+ import * as amqp from 'amqplib';
3
+ import { Replies } from 'amqplib';
4
+ import { Queue, QueueOptions } from '../queue';
5
+ import { ExchangeTypes, Exchange, ExchangeConsumerQueueOptions, ExchangeConsumerOptions, ExchangeBatchConsumerOptions, ExchangeOptions } from '../exchange';
6
+ import { Producer, ProducerOptions } from '../producer';
7
+ import { ConsumerOptions, Consumer, BatchConsumerOptions, BatchConsumer } from '../consumer';
8
+ import { Connection } from '../connection';
9
+ import { ChannelPublishOptions } from './types';
10
+ /**
11
+ * Events emitted by a {@link Channel}.
12
+ */
13
+ export interface ChannelEventMap {
14
+ /**
15
+ * Emitted when the underlying amqplib channel encounters an error.
16
+ */
17
+ error: [err: unknown];
18
+ /**
19
+ * Emitted when the underlying amqplib channel is closed.
20
+ */
21
+ close: [];
22
+ /**
23
+ * Emitted when the internal write buffer drains after backpressure.
24
+ */
25
+ drain: [];
26
+ }
27
+ export interface Channel extends EventEmitter<ChannelEventMap> {
28
+ /**
29
+ * The {@link Connection} that owns this channel.
30
+ */
31
+ get connection(): Connection;
32
+ /**
33
+ * Open the underlying amqplib channel.
34
+ * Must be called before any other operation.
35
+ * Subsequent calls are no-ops if the channel is already open.
36
+ */
37
+ connect(): Promise<Channel>;
38
+ /**
39
+ * Close the underlying amqplib channel.
40
+ * For confirm channels, waits for all pending confirmations before closing.
41
+ * @param timeout - Maximum milliseconds to wait for pending confirmations before closing.
42
+ * Throws `'Channel close timed out'` if exceeded. Waits 30s when omitted.
43
+ */
44
+ close(timeout?: number): Promise<void>;
45
+ /**
46
+ * Assert an exchange on the broker and return an {@link Exchange} handle.
47
+ *
48
+ * @param name - Exchange name.
49
+ * @param type - Exchange type (direct, fanout, topic, headers, …).
50
+ * @param options - Optional exchange assertion options.
51
+ */
52
+ createExchange(name: string, type: ExchangeTypes, options?: ExchangeOptions): Promise<Exchange>;
53
+ /**
54
+ * Assert a queue on the broker and return a {@link Queue} handle.
55
+ *
56
+ * @param name - Queue name.
57
+ * @param options - Optional queue assertion options.
58
+ */
59
+ createQueue(name: string, options?: QueueOptions): Promise<Queue>;
60
+ /**
61
+ * Check whether a queue exists on the broker without asserting it.
62
+ * Resolves with queue metadata; rejects if the queue does not exist.
63
+ *
64
+ * @param name - Queue name to inspect.
65
+ */
66
+ checkQueue(name: string): Promise<Replies.AssertQueue>;
67
+ /**
68
+ * Returns the underlying amqplib channel, connecting first if not already open.
69
+ * This is intended for internal use.
70
+ */
71
+ native(): Promise<amqp.Channel | amqp.ConfirmChannel>;
72
+ /**
73
+ * Publish message directly using amqplib publish function.
74
+ * Handles backpressure (drain) and confirm-channel retries automatically.
75
+ * This is intended for internal use.
76
+ * Use `createProducer*` functions instead.
77
+ *
78
+ * @param exchange - Exchange name to publish to.
79
+ * @param routingKey - Routing key for the message.
80
+ * @param content - Serialized message body.
81
+ * @param options - Publish options including drain timeout and retry strategy.
82
+ * @returns `true` if the broker confirmed receipt (confirm channel), `false` if fire-and-forget (non-confirm channel).
83
+ */
84
+ publish(exchange: string, routingKey: string, content: Buffer, options: ChannelPublishOptions): Promise<boolean>;
85
+ /**
86
+ * Create a producer that publishes directly to a queue using this channel.
87
+ * Each producer should have its own dedicated channel; this method uses the current
88
+ * channel. Consider using `connection.createProducerForQueue` or pass a channel
89
+ * in `options` param.
90
+ *
91
+ * @param queue - Target queue.
92
+ * @param options - Optional producer options (serialization, routing key, hooks, …).
93
+ */
94
+ createProducerForQueue<T>(queue: Queue, options?: ProducerOptions<T>): Promise<Producer<T>>;
95
+ /**
96
+ * Create a producer that publishes to an exchange using this channel.
97
+ * Each producer should have its own dedicated channel; this method uses the current
98
+ * channel. Consider using `connection.createProducerForExchange` or pass a channel
99
+ * in `options` param.
100
+ *
101
+ * @param exchange - Target exchange.
102
+ * @param options - Optional producer options (serialization, routing key, hooks, …).
103
+ */
104
+ createProducerForExchange<T>(exchange: Exchange, options?: ProducerOptions<T>): Promise<Producer<T>>;
105
+ /**
106
+ * Create a consumer that reads from a queue using this channel.
107
+ * Each consumer should have its own dedicated channel; this method uses the current
108
+ * channel. Consider using `connection.createConsumerForQueue` or pass a channel
109
+ * in `options` param.
110
+ *
111
+ * @param queue - Source queue.
112
+ * @param options - Optional consumer options (failure strategy, prefetch, …).
113
+ */
114
+ createConsumerForQueue<T>(queue: Queue, options?: ConsumerOptions<T>): Promise<Consumer<T>>;
115
+ /**
116
+ * Create a consumer that reads from an exchange using this channel.
117
+ * Internally asserts an exclusive queue and binds it to the exchange.
118
+ * Each consumer should have its own dedicated channel; this method uses the current
119
+ * channel. Consider using `connection.createConsumerForExchange` or pass a channel
120
+ * in `options` param.
121
+ *
122
+ * @param exchange - Source exchange.
123
+ * @param options - Consumer options including the binding pattern.
124
+ * @param queueOptions - Options for the auto-created exclusive queue binding.
125
+ */
126
+ createConsumerForExchange<T>(exchange: Exchange, options: ExchangeConsumerOptions<T>, queueOptions?: ExchangeConsumerQueueOptions): Promise<Consumer<T>>;
127
+ /**
128
+ * Create a batch consumer that reads from a queue using this channel.
129
+ * Each consumer should have its own dedicated channel; this method uses the current
130
+ * channel. Consider using `connection.createBatchConsumerForQueue` or pass a channel
131
+ * in `options` param.
132
+ *
133
+ * @param queue - Source queue.
134
+ * @param options - Optional batch consumer options (batch size, failure strategy, prefetch, …).
135
+ */
136
+ createBatchConsumerForQueue<T>(queue: Queue, options?: BatchConsumerOptions<T>): Promise<BatchConsumer<T>>;
137
+ /**
138
+ * Create a batch consumer that reads from an exchange using this channel.
139
+ * Internally asserts an exclusive queue and binds it to the exchange.
140
+ * Each consumer should have its own dedicated channel; this method uses the current
141
+ * channel. Consider using `connection.createBatchConsumerForExchange` or pass a channel
142
+ * in `options` param.
143
+ *
144
+ * @param exchange - Source exchange.
145
+ * @param options - Batch consumer options including the binding pattern.
146
+ * @param queueOptions - Options for the auto-created exclusive queue binding.
147
+ */
148
+ createBatchConsumerForExchange<T>(exchange: Exchange, options: ExchangeBatchConsumerOptions<T>, queueOptions?: ExchangeConsumerQueueOptions): Promise<BatchConsumer<T>>;
149
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,2 @@
1
+ export { Channel } from './channel';
2
+ export { ChannelImplementation } from './channel-implementation';
@@ -0,0 +1,5 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ChannelImplementation = void 0;
4
+ var channel_implementation_1 = require("./channel-implementation");
5
+ Object.defineProperty(exports, "ChannelImplementation", { enumerable: true, get: function () { return channel_implementation_1.ChannelImplementation; } });
@@ -0,0 +1,12 @@
1
+ import * as amqp from 'amqplib';
2
+ import { ProducerPublishOptions } from '../producer';
3
+ export type ChannelPublishOptions = ProducerPublishOptions & {
4
+ drainTimeout: number;
5
+ };
6
+ export type ChannelWrapper = ({
7
+ isConfirmed: true;
8
+ channel: Promise<amqp.ConfirmChannel> | null;
9
+ } | {
10
+ isConfirmed: false;
11
+ channel: Promise<amqp.Channel> | null;
12
+ });
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,35 @@
1
+ import { EventEmitter } from 'events';
2
+ import * as amqp from 'amqplib';
3
+ import { RetryStrategy } from '../retry';
4
+ import { Channel } from '../channel';
5
+ import { Exchange, ExchangeConsumerQueueOptions, ExchangeConsumerOptions, ExchangeBatchConsumerOptions } from '../exchange';
6
+ import { Consumer, ConsumerOptions, BatchConsumer, BatchConsumerOptions } from '../consumer';
7
+ import { Queue } from '../queue';
8
+ import { ProducerOptions, Producer } from '../producer';
9
+ import { Connection, ConnectionEventMap, ConnectionState } from './connection';
10
+ export declare class ConnectionImplementation extends EventEmitter<ConnectionEventMap> implements Connection {
11
+ private readonly options;
12
+ private readonly socketOptions?;
13
+ private readonly retryStrategy;
14
+ private connection;
15
+ private closingHandler;
16
+ private connectionState;
17
+ constructor(options: amqp.Options.Connect, retryStrategy?: RetryStrategy, socketOptions?: unknown | undefined);
18
+ connect(): Promise<this>;
19
+ close(timeout?: number): Promise<void>;
20
+ state(): ConnectionState;
21
+ native(): Promise<amqp.ChannelModel>;
22
+ createChannel(isConfirmed?: boolean): Channel;
23
+ createConsumerForExchange<T>(exchange: Exchange, options: ExchangeConsumerOptions<T>, queueOptions?: ExchangeConsumerQueueOptions): Promise<Consumer<T>>;
24
+ createConsumerForQueue<T>(queue: Queue, options?: ConsumerOptions<T>): Promise<Consumer<T>>;
25
+ createBatchConsumerForQueue<T>(queue: Queue, options?: BatchConsumerOptions<T>): Promise<BatchConsumer<T>>;
26
+ createBatchConsumerForExchange<T>(exchange: Exchange, options: ExchangeBatchConsumerOptions<T>, queueOptions?: ExchangeConsumerQueueOptions): Promise<BatchConsumer<T>>;
27
+ createProducerForExchange<T>(exchange: Exchange, options?: ProducerOptions<T>, isConfirmed?: boolean): Promise<Producer<T>>;
28
+ createProducerForQueue<T>(queue: Queue, options?: ProducerOptions<T>, isConfirmed?: boolean): Promise<Producer<T>>;
29
+ }
30
+ /**
31
+ * Creates a {@link ConnectionImplementation} and establishes the connection in one step.
32
+ * Equivalent to `new ConnectionImplementation(...args).connect()`.
33
+ * @param args - Same arguments as the {@link ConnectionImplementation} constructor.
34
+ */
35
+ export declare function connect(...args: ConstructorParameters<typeof ConnectionImplementation>): Promise<Connection>;