amqpx 0.0.0 → 1.0.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 (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 +63 -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 +51 -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 +63 -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 +51 -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
@@ -0,0 +1,25 @@
1
+ import { Queue } from '../../index';
2
+ /**
3
+ * Mock implementation of Queue using jest mocks.
4
+ *
5
+ * All `create*` methods return test instance of classes from this package.
6
+ * `assert` and `bindExchange` methods are jest mocks returning current instance.
7
+ *
8
+ * @example
9
+ * ```ts
10
+ * import { TestQueue } from 'amqpx/jest';
11
+ *
12
+ * const queue = new TestQueue();
13
+ * // or create from TestChannel, TestConnection, or TestExchange
14
+ *
15
+ * expect(queue.bindExchange).toBeCalledTimes(0);
16
+ * ```
17
+ */
18
+ export declare class TestQueue implements Queue {
19
+ assert: jest.Mock<any, any, any>;
20
+ name: jest.Mock<any, any, any>;
21
+ bindExchange: jest.Mock<any, any, any>;
22
+ createConsumer: jest.Mock<any, any, any>;
23
+ createBatchConsumer: jest.Mock<any, any, any>;
24
+ createProducer: jest.Mock<any, any, any>;
25
+ }
@@ -0,0 +1,13 @@
1
+ import { TestConsumer } from './test-consumer';
2
+ import { TestBatchConsumer } from './test-batch-consumer';
3
+ import { TestProducer } from './test-producer';
4
+ export class TestQueue {
5
+ constructor() {
6
+ this.assert = jest.fn().mockImplementation(() => Promise.resolve(this));
7
+ this.name = jest.fn().mockImplementation(() => Promise.resolve('test-queue'));
8
+ this.bindExchange = jest.fn().mockImplementation(() => Promise.resolve(this));
9
+ this.createConsumer = jest.fn().mockImplementation(() => Promise.resolve(new TestConsumer()));
10
+ this.createBatchConsumer = jest.fn().mockImplementation(() => Promise.resolve(new TestBatchConsumer()));
11
+ this.createProducer = jest.fn().mockImplementation(() => Promise.resolve(new TestProducer()));
12
+ }
13
+ }
@@ -0,0 +1,7 @@
1
+ export * from './test-connection';
2
+ export * from './test-channel';
3
+ export * from './test-exchange';
4
+ export * from './test-queue';
5
+ export * from './test-consumer';
6
+ export * from './test-batch-consumer';
7
+ export * from './test-producer';
@@ -0,0 +1,7 @@
1
+ export * from './test-connection';
2
+ export * from './test-channel';
3
+ export * from './test-exchange';
4
+ export * from './test-queue';
5
+ export * from './test-consumer';
6
+ export * from './test-batch-consumer';
7
+ export * from './test-producer';
@@ -0,0 +1,48 @@
1
+ import { EventEmitter } from 'events';
2
+ import type * as amqp from 'amqplib';
3
+ import { BatchConsumer } from '../../index';
4
+ import { TestQueue } from './test-queue';
5
+ import { TestChannel } from './test-channel';
6
+ /**
7
+ * Test implementation of BatchConsumer using vitest mocks.
8
+ *
9
+ * `listen` method instantly returns current instance wrapped in Promise.
10
+ * You can assert the parameters with which was this method called to assert
11
+ * `listen` method was called.
12
+ *
13
+ * `queue` and `channel` properties return new instances of TestQueue and
14
+ * TestChannel respectively. `close` method is a vitest mock returning void.
15
+ *
16
+ * Use `deliverMessages` to simulate a batch of messages arriving and invoke
17
+ * the registered listener.
18
+ *
19
+ * @example
20
+ * ```ts
21
+ * import { TestBatchConsumer } from 'amqpx/vitest';
22
+ *
23
+ * const consumer = new TestBatchConsumer<{ id: number }>();
24
+ *
25
+ * const handler = vitest.fn();
26
+ * await consumer.listen(handler);
27
+ *
28
+ * await consumer.deliverMessages([{ id: 1 }, { id: 2 }]);
29
+ *
30
+ * expect(handler).toHaveBeenCalledTimes(1);
31
+ * ```
32
+ */
33
+ export declare class TestBatchConsumer<T> extends EventEmitter implements BatchConsumer<T> {
34
+ constructor();
35
+ close: import("vitest").Mock<import("@vitest/spy").Procedure>;
36
+ listen: import("vitest").Mock<import("@vitest/spy").Procedure>;
37
+ queue: TestQueue;
38
+ channel: TestChannel;
39
+ /**
40
+ * Simulates a batch of messages arriving from RabbitMQ by invoking the most recently registered listener.
41
+ * Throws if `listen` has not been called yet.
42
+ * @param messages - Array of parsed message payloads to deliver as a single batch.
43
+ * @param options - Optional serialization override and raw amqplib message field overrides applied to each message.
44
+ */
45
+ deliverMessages(messages: T[], options?: {
46
+ serialize?(msg: T): Buffer;
47
+ } & Omit<Partial<amqp.ConsumeMessage>, 'content'>): Promise<void>;
48
+ }
@@ -0,0 +1,52 @@
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
10
+ var __rest = (this && this.__rest) || function (s, e) {
11
+ var t = {};
12
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
13
+ t[p] = s[p];
14
+ if (s != null && typeof Object.getOwnPropertySymbols === "function")
15
+ for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
16
+ if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
17
+ t[p[i]] = s[p[i]];
18
+ }
19
+ return t;
20
+ };
21
+ import { EventEmitter } from 'events';
22
+ import { TestQueue } from './test-queue';
23
+ import { TestChannel } from './test-channel';
24
+ export class TestBatchConsumer extends EventEmitter {
25
+ constructor() {
26
+ super();
27
+ this.close = vitest.fn().mockImplementation(() => Promise.resolve());
28
+ this.listen = vitest.fn().mockImplementation(() => Promise.resolve(this));
29
+ this.queue = new TestQueue();
30
+ this.channel = new TestChannel();
31
+ this.setMaxListeners(0);
32
+ }
33
+ deliverMessages(messages_1) {
34
+ return __awaiter(this, arguments, void 0, function* (messages, options = {}) {
35
+ const { calls } = this.listen.mock;
36
+ const lastCall = calls.length > 0 ? calls[calls.length - 1] : undefined;
37
+ const callback = lastCall !== undefined ? lastCall[0] : undefined;
38
+ if (!callback)
39
+ throw new Error('No listener registered. Call listen() before deliverMessages().');
40
+ const { serialize = (msg) => Buffer.from(JSON.stringify(msg)) } = options, rest = __rest(options, ["serialize"]);
41
+ const parsedMessages = messages.map(message => {
42
+ const raw = {
43
+ content: serialize(message),
44
+ fields: Object.assign({ deliveryTag: 1, redelivered: false, exchange: '', routingKey: '', consumerTag: '' }, rest.fields),
45
+ properties: Object.assign({ headers: {} }, rest.properties),
46
+ };
47
+ return { message, rabbitMessage: raw };
48
+ });
49
+ yield callback({ channel: this.channel, messages: parsedMessages });
50
+ });
51
+ }
52
+ }
@@ -0,0 +1,71 @@
1
+ import { EventEmitter } from 'events';
2
+ import { Channel, Connection } from '../../index';
3
+ /**
4
+ * Mock implementation of Channel using vitest mocks.
5
+ *
6
+ * The channel is instantly connected and all `create*` methods return
7
+ * test classes from this package. Non-creation methods return default
8
+ * mock implementations returning undefined (or the same instance if the semantic
9
+ * of the method requires so).
10
+ *
11
+ * @example
12
+ * ```ts
13
+ * import { TestConnection, TestChannel } from 'amqpx/vitest';
14
+ *
15
+ * const connection = new TestConnection();
16
+ *
17
+ * const channel = connection.createChannel();
18
+ * // or new TestChannel();
19
+ *
20
+ * const exchange = channel.createExchange();
21
+ *
22
+ * expect(exchange).toBeInstanceOf(TestExchange);
23
+ * ```
24
+ */
25
+ export declare class TestChannel extends EventEmitter implements Channel {
26
+ readonly connection: Connection;
27
+ constructor(connection?: Connection);
28
+ connect: import("vitest").Mock<import("@vitest/spy").Procedure>;
29
+ close: import("vitest").Mock<import("@vitest/spy").Procedure>;
30
+ createExchange: import("vitest").Mock<import("@vitest/spy").Procedure>;
31
+ createQueue: import("vitest").Mock<import("@vitest/spy").Procedure>;
32
+ createProducerForQueue: import("vitest").Mock<import("@vitest/spy").Procedure>;
33
+ createProducerForExchange: import("vitest").Mock<import("@vitest/spy").Procedure>;
34
+ createConsumerForQueue: import("vitest").Mock<import("@vitest/spy").Procedure>;
35
+ createConsumerForExchange: import("vitest").Mock<import("@vitest/spy").Procedure>;
36
+ createBatchConsumerForQueue: import("vitest").Mock<import("@vitest/spy").Procedure>;
37
+ createBatchConsumerForExchange: import("vitest").Mock<import("@vitest/spy").Procedure>;
38
+ native: import("vitest").Mock<import("@vitest/spy").Procedure>;
39
+ private _drainPending;
40
+ private _drainResolvers;
41
+ publish: import("vitest").Mock<import("@vitest/spy").Procedure>;
42
+ checkQueue: import("vitest").Mock<import("@vitest/spy").Procedure>;
43
+ /**
44
+ * Simulates a channel close by emitting the `close` event.
45
+ */
46
+ simulateClose(): void;
47
+ /**
48
+ * Simulates write-buffer backpressure. Subsequent `publish` calls will not resolve until {@link releaseDrain} is called.
49
+ */
50
+ simulateDrainBackpressure(): void;
51
+ /**
52
+ * Releases simulated backpressure, resolves all pending `publish` calls, and emits the `drain` event.
53
+ */
54
+ releaseDrain(): void;
55
+ consumeResponse: {
56
+ consumerTag: `${string}-${string}-${string}-${string}-${string}`;
57
+ };
58
+ nativeChannel: {
59
+ cancel: import("vitest").Mock<import("@vitest/spy").Procedure>;
60
+ prefetch: import("vitest").Mock<import("@vitest/spy").Procedure>;
61
+ consume: import("vitest").Mock<import("@vitest/spy").Procedure>;
62
+ ack: import("vitest").Mock<import("@vitest/spy").Procedure>;
63
+ nack: import("vitest").Mock<import("@vitest/spy").Procedure>;
64
+ assertQueue: import("vitest").Mock<import("@vitest/spy").Procedure>;
65
+ checkQueue: import("vitest").Mock<import("@vitest/spy").Procedure>;
66
+ assertExchange: import("vitest").Mock<import("@vitest/spy").Procedure>;
67
+ checkExchange: import("vitest").Mock<import("@vitest/spy").Procedure>;
68
+ bindQueue: import("vitest").Mock<import("@vitest/spy").Procedure>;
69
+ bindExchange: import("vitest").Mock<import("@vitest/spy").Procedure>;
70
+ };
71
+ }
@@ -0,0 +1,73 @@
1
+ import { EventEmitter } from 'events';
2
+ import { TestConsumer, TestBatchConsumer, TestExchange, TestProducer, TestQueue, TestConnection } from '.';
3
+ export class TestChannel extends EventEmitter {
4
+ constructor(connection = new TestConnection()) {
5
+ super();
6
+ this.connection = connection;
7
+ this.connect = vitest.fn().mockImplementation(() => Promise.resolve(this));
8
+ this.close = vitest.fn().mockImplementation(() => Promise.resolve());
9
+ this.createExchange = vitest.fn().mockImplementation(() => Promise.resolve(new TestExchange()));
10
+ this.createQueue = vitest.fn().mockImplementation(() => Promise.resolve(new TestQueue()));
11
+ this.createProducerForQueue = vitest.fn().mockImplementation(() => Promise.resolve(new TestProducer()));
12
+ this.createProducerForExchange = vitest.fn().mockImplementation(() => Promise.resolve(new TestProducer()));
13
+ this.createConsumerForQueue = vitest.fn().mockImplementation(() => Promise.resolve(new TestConsumer()));
14
+ this.createConsumerForExchange = vitest.fn().mockImplementation(() => Promise.resolve(new TestConsumer()));
15
+ this.createBatchConsumerForQueue = vitest.fn().mockImplementation(() => Promise.resolve(new TestBatchConsumer()));
16
+ this.createBatchConsumerForExchange = vitest.fn().mockImplementation(() => Promise.resolve(new TestBatchConsumer()));
17
+ this.native = vitest.fn().mockImplementation(() => Promise.resolve(this.nativeChannel));
18
+ this._drainPending = false;
19
+ this._drainResolvers = [];
20
+ this.publish = vitest.fn().mockImplementation(() => {
21
+ if (this._drainPending) {
22
+ return new Promise(resolve => {
23
+ this._drainResolvers.push(() => resolve(false));
24
+ });
25
+ }
26
+ return Promise.resolve(false);
27
+ });
28
+ this.checkQueue = vitest.fn().mockImplementation((queue) => Promise.resolve({
29
+ queue,
30
+ messageCount: 0,
31
+ consumerCount: 0,
32
+ }));
33
+ this.consumeResponse = {
34
+ consumerTag: crypto.randomUUID(),
35
+ };
36
+ this.nativeChannel = {
37
+ cancel: vitest.fn().mockImplementation(() => Promise.resolve()),
38
+ prefetch: vitest.fn().mockImplementation(() => Promise.resolve()),
39
+ consume: vitest.fn().mockImplementation(() => Promise.resolve(this.consumeResponse)),
40
+ ack: vitest.fn(),
41
+ nack: vitest.fn(),
42
+ assertQueue: vitest.fn().mockImplementation((queue) => Promise.resolve({
43
+ queue,
44
+ messageCount: 0,
45
+ consumerCount: 0,
46
+ })),
47
+ checkQueue: vitest.fn().mockImplementation((queue) => Promise.resolve({
48
+ queue,
49
+ messageCount: 0,
50
+ consumerCount: 0,
51
+ })),
52
+ assertExchange: vitest.fn().mockImplementation((exchange) => Promise.resolve({
53
+ exchange,
54
+ })),
55
+ checkExchange: vitest.fn().mockResolvedValue(undefined),
56
+ bindQueue: vitest.fn(),
57
+ bindExchange: vitest.fn(),
58
+ };
59
+ }
60
+ simulateClose() {
61
+ this.emit('close');
62
+ }
63
+ simulateDrainBackpressure() {
64
+ this._drainPending = true;
65
+ }
66
+ releaseDrain() {
67
+ this._drainPending = false;
68
+ const resolvers = this._drainResolvers.splice(0);
69
+ this.emit('drain');
70
+ for (const resolve of resolvers)
71
+ resolve();
72
+ }
73
+ }
@@ -0,0 +1,38 @@
1
+ import { EventEmitter } from 'events';
2
+ import { Connection } from '../../index';
3
+ /**
4
+ * Mock implementation of Connection using vitest mocks.
5
+ *
6
+ * The connection is instantly connected and all `create*` methods return
7
+ * test classes from this package. Non-creation methods return default
8
+ * mock implementations returning undefined (or the same instance if the semantic
9
+ * of the method requires so).
10
+ *
11
+ * @example
12
+ * ```ts
13
+ * import { TestConnection } from 'amqpx/vitest';
14
+ *
15
+ * const connection = new TestConnection();
16
+ *
17
+ * const channel = connection.createChannel();
18
+ *
19
+ * expect(channel).toBeInstanceOf(TestChannel);
20
+ * ```
21
+ */
22
+ export declare class TestConnection extends EventEmitter implements Connection {
23
+ close: import("vitest").Mock<import("@vitest/spy").Procedure>;
24
+ connect: import("vitest").Mock<import("@vitest/spy").Procedure>;
25
+ createChannel: import("vitest").Mock<import("@vitest/spy").Procedure>;
26
+ createConsumerForExchange: import("vitest").Mock<import("@vitest/spy").Procedure>;
27
+ createConsumerForQueue: import("vitest").Mock<import("@vitest/spy").Procedure>;
28
+ createBatchConsumerForQueue: import("vitest").Mock<import("@vitest/spy").Procedure>;
29
+ createBatchConsumerForExchange: import("vitest").Mock<import("@vitest/spy").Procedure>;
30
+ createProducerForExchange: import("vitest").Mock<import("@vitest/spy").Procedure>;
31
+ createProducerForQueue: import("vitest").Mock<import("@vitest/spy").Procedure>;
32
+ native: import("vitest").Mock<import("@vitest/spy").Procedure>;
33
+ state: import("vitest").Mock<import("@vitest/spy").Procedure>;
34
+ /**
35
+ * Simulates a server-side disconnect by emitting the `reconnecting` event.
36
+ */
37
+ simulateDisconnect(): void;
38
+ }
@@ -0,0 +1,25 @@
1
+ import { EventEmitter } from 'events';
2
+ import { ConnectionState } from '../../index';
3
+ import { TestProducer } from './test-producer';
4
+ import { TestChannel } from './test-channel';
5
+ import { TestConsumer } from './test-consumer';
6
+ import { TestBatchConsumer } from './test-batch-consumer';
7
+ export class TestConnection extends EventEmitter {
8
+ constructor() {
9
+ super(...arguments);
10
+ this.close = vitest.fn().mockImplementation(() => Promise.resolve());
11
+ this.connect = vitest.fn().mockImplementation(() => Promise.resolve(this));
12
+ this.createChannel = vitest.fn().mockImplementation(() => new TestChannel(this));
13
+ this.createConsumerForExchange = vitest.fn().mockImplementation(() => Promise.resolve(new TestConsumer()));
14
+ this.createConsumerForQueue = vitest.fn().mockImplementation(() => Promise.resolve(new TestConsumer()));
15
+ this.createBatchConsumerForQueue = vitest.fn().mockImplementation(() => Promise.resolve(new TestBatchConsumer()));
16
+ this.createBatchConsumerForExchange = vitest.fn().mockImplementation(() => Promise.resolve(new TestBatchConsumer()));
17
+ this.createProducerForExchange = vitest.fn().mockImplementation(() => Promise.resolve(new TestProducer()));
18
+ this.createProducerForQueue = vitest.fn().mockImplementation(() => Promise.resolve(new TestProducer()));
19
+ this.native = vitest.fn().mockResolvedValue(undefined);
20
+ this.state = vitest.fn().mockReturnValue(ConnectionState.connected);
21
+ }
22
+ simulateDisconnect() {
23
+ this.emit('reconnecting');
24
+ }
25
+ }
@@ -0,0 +1,49 @@
1
+ import { EventEmitter } from 'events';
2
+ import type * as amqp from 'amqplib';
3
+ import { Consumer } from '../../index';
4
+ import { TestQueue } from './test-queue';
5
+ import { TestChannel } from './test-channel';
6
+ /**
7
+ * Test implementation of Consumer using vitest mocks.
8
+ *
9
+ * `listen` method instantly returns current instance wrapped in Promise.
10
+ * You can assert the parameters with which was this method called to assert
11
+ * `listen` method was called.
12
+ *
13
+ * `queue` and `channel` properties are new instances of TestQueue and
14
+ * TestChannel respectively. All other methods are vitest mocks returning
15
+ * void or the current instance, depending on the method semantic.
16
+ *
17
+ * @example
18
+ * ```ts
19
+ * import { TestConsumer } from 'amqpx/vitest';
20
+ *
21
+ * const consumer = new TestConsumer();
22
+ *
23
+ * const channel = consumer.getChannel();
24
+ *
25
+ * expect(channel).toBeInstanceOf(TestChannel);
26
+ *
27
+ * const listener = () => { / * empty * / };
28
+ * await consumer.listen(listener);
29
+ *
30
+ * expect(consumer.listen).toBeCalledWith(listener);
31
+ * ```
32
+ */
33
+ export declare class TestConsumer<T> extends EventEmitter implements Consumer<T> {
34
+ constructor();
35
+ close: import("vitest").Mock<import("@vitest/spy").Procedure>;
36
+ listen: import("vitest").Mock<import("@vitest/spy").Procedure>;
37
+ setPrefetch: import("vitest").Mock<import("@vitest/spy").Procedure>;
38
+ queue: TestQueue;
39
+ channel: TestChannel;
40
+ /**
41
+ * Simulates a message arriving from RabbitMQ by invoking the most recently registered listener.
42
+ * Throws if `listen` has not been called yet.
43
+ * @param message - The parsed message payload to deliver.
44
+ * @param options - Optional serialization override and raw amqplib message field overrides.
45
+ */
46
+ deliverMessage(message: T, options?: {
47
+ serialize?(msg: T): Buffer;
48
+ } & Omit<Partial<amqp.ConsumeMessage>, 'content'>): Promise<void>;
49
+ }
@@ -0,0 +1,50 @@
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
10
+ var __rest = (this && this.__rest) || function (s, e) {
11
+ var t = {};
12
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
13
+ t[p] = s[p];
14
+ if (s != null && typeof Object.getOwnPropertySymbols === "function")
15
+ for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
16
+ if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
17
+ t[p[i]] = s[p[i]];
18
+ }
19
+ return t;
20
+ };
21
+ import { EventEmitter } from 'events';
22
+ import { TestQueue } from './test-queue';
23
+ import { TestChannel } from './test-channel';
24
+ export class TestConsumer extends EventEmitter {
25
+ constructor() {
26
+ super();
27
+ this.close = vitest.fn().mockImplementation(() => Promise.resolve());
28
+ this.listen = vitest.fn().mockImplementation(() => Promise.resolve(this));
29
+ this.setPrefetch = vitest.fn().mockImplementation(() => Promise.resolve());
30
+ this.queue = new TestQueue();
31
+ this.channel = new TestChannel();
32
+ this.setMaxListeners(0);
33
+ }
34
+ deliverMessage(message_1) {
35
+ return __awaiter(this, arguments, void 0, function* (message, options = {}) {
36
+ const { calls } = this.listen.mock;
37
+ const lastCall = calls.length > 0 ? calls[calls.length - 1] : undefined;
38
+ const callback = lastCall !== undefined ? lastCall[0] : undefined;
39
+ if (!callback)
40
+ throw new Error('No listener registered. Call listen() before deliverMessage().');
41
+ const { serialize = (msg) => Buffer.from(JSON.stringify(msg)) } = options, rest = __rest(options, ["serialize"]);
42
+ const raw = {
43
+ content: serialize(message),
44
+ fields: Object.assign({ deliveryTag: 1, redelivered: false, exchange: '', routingKey: '', consumerTag: '' }, rest.fields),
45
+ properties: Object.assign({ headers: {} }, rest.properties),
46
+ };
47
+ yield callback({ channel: this.channel, message, rabbitMessage: raw });
48
+ });
49
+ }
50
+ }
@@ -0,0 +1,26 @@
1
+ import { Exchange } from '../../index';
2
+ /**
3
+ * Mock implementation of Exchange using vitest mocks.
4
+ *
5
+ * All `create*` methods return test classes from this package.
6
+ * `assert`, `bindQueue`, and `bindExchange` methods are vitest mocks returning current instance.
7
+ *
8
+ * @example
9
+ * ```ts
10
+ * import { TestExchange } from 'amqpx/vitest';
11
+ *
12
+ * const exchange = new TestExchange();
13
+ * // or create from TestChannel or TestConnection
14
+ *
15
+ * expect(exchange.bindQueue).toBeCalledTimes(0);
16
+ * ```
17
+ */
18
+ export declare class TestExchange implements Exchange {
19
+ assert: import("vitest").Mock<import("@vitest/spy").Procedure>;
20
+ name: import("vitest").Mock<import("@vitest/spy").Procedure>;
21
+ bindQueue: import("vitest").Mock<import("@vitest/spy").Procedure>;
22
+ bindExchange: import("vitest").Mock<import("@vitest/spy").Procedure>;
23
+ createConsumer: import("vitest").Mock<import("@vitest/spy").Procedure>;
24
+ createBatchConsumer: import("vitest").Mock<import("@vitest/spy").Procedure>;
25
+ createProducer: import("vitest").Mock<import("@vitest/spy").Procedure>;
26
+ }
@@ -0,0 +1,12 @@
1
+ import { TestConsumer, TestBatchConsumer, TestProducer } from '.';
2
+ export class TestExchange {
3
+ constructor() {
4
+ this.assert = vitest.fn().mockImplementation(() => Promise.resolve(this));
5
+ this.name = vitest.fn().mockImplementation(() => Promise.resolve('test-exchange'));
6
+ this.bindQueue = vitest.fn().mockImplementation(() => Promise.resolve(this));
7
+ this.bindExchange = vitest.fn().mockImplementation(() => Promise.resolve(this));
8
+ this.createConsumer = vitest.fn().mockImplementation(() => Promise.resolve(new TestConsumer()));
9
+ this.createBatchConsumer = vitest.fn().mockImplementation(() => Promise.resolve(new TestBatchConsumer()));
10
+ this.createProducer = vitest.fn().mockImplementation(() => Promise.resolve(new TestProducer()));
11
+ }
12
+ }
@@ -0,0 +1,34 @@
1
+ import { EventEmitter } from 'events';
2
+ import { Producer } from '../../index';
3
+ import { TestChannel } from './test-channel';
4
+ /**
5
+ * Test implementation of Producer using vitest mocks.
6
+ *
7
+ * `publish` method instantly returns the message argument wrapped in a Promise.
8
+ * `channel` property is a new TestChannel instance.
9
+ *
10
+ * @example
11
+ * ```ts
12
+ * import { TestProducer } from 'amqpx/vitest';
13
+ *
14
+ * const producer = new TestProducer();
15
+ *
16
+ * const channel = producer.getChannel();
17
+ *
18
+ * expect(channel).toBeInstanceOf(TestChannel);
19
+ *
20
+ * const msg = await producer.publish('test');
21
+ *
22
+ * expect(msg).toBe('test');
23
+ * ```
24
+ */
25
+ export declare class TestProducer<T> extends EventEmitter implements Producer<T> {
26
+ constructor();
27
+ close: import("vitest").Mock<import("@vitest/spy").Procedure>;
28
+ publish: import("vitest").Mock<import("@vitest/spy").Procedure>;
29
+ channel: TestChannel;
30
+ /**
31
+ * Returns the first argument (the message payload) from every `publish` call recorded so far.
32
+ */
33
+ getPublishedMessages(): T[];
34
+ }
@@ -0,0 +1,14 @@
1
+ import { EventEmitter } from 'events';
2
+ import { TestChannel } from './test-channel';
3
+ export class TestProducer extends EventEmitter {
4
+ constructor() {
5
+ super();
6
+ this.close = vitest.fn().mockResolvedValue(undefined);
7
+ this.publish = vitest.fn().mockImplementation(msg => Promise.resolve(msg));
8
+ this.channel = new TestChannel();
9
+ this.setMaxListeners(0);
10
+ }
11
+ getPublishedMessages() {
12
+ return this.publish.mock.calls.map(([msg]) => msg);
13
+ }
14
+ }
@@ -0,0 +1,25 @@
1
+ import { Queue } from '../../index';
2
+ /**
3
+ * Mock implementation of Queue using vitest mocks.
4
+ *
5
+ * All `create*` methods return test instance of classes from this package.
6
+ * `assert` and `bindExchange` methods are vitest mocks returning current instance.
7
+ *
8
+ * @example
9
+ * ```ts
10
+ * import { TestQueue } from 'amqpx/vitest';
11
+ *
12
+ * const queue = new TestQueue();
13
+ * // or create from TestChannel, TestConnection, or TestExchange
14
+ *
15
+ * expect(queue.bindExchange).toBeCalledTimes(0);
16
+ * ```
17
+ */
18
+ export declare class TestQueue implements Queue {
19
+ assert: import("vitest").Mock<import("@vitest/spy").Procedure>;
20
+ name: import("vitest").Mock<import("@vitest/spy").Procedure>;
21
+ bindExchange: import("vitest").Mock<import("@vitest/spy").Procedure>;
22
+ createConsumer: import("vitest").Mock<import("@vitest/spy").Procedure>;
23
+ createBatchConsumer: import("vitest").Mock<import("@vitest/spy").Procedure>;
24
+ createProducer: import("vitest").Mock<import("@vitest/spy").Procedure>;
25
+ }
@@ -0,0 +1,13 @@
1
+ import { TestConsumer } from './test-consumer';
2
+ import { TestBatchConsumer } from './test-batch-consumer';
3
+ import { TestProducer } from './test-producer';
4
+ export class TestQueue {
5
+ constructor() {
6
+ this.assert = vitest.fn().mockImplementation(() => Promise.resolve(this));
7
+ this.name = vitest.fn().mockImplementation(() => Promise.resolve('test-queue'));
8
+ this.bindExchange = vitest.fn().mockImplementation(() => Promise.resolve(this));
9
+ this.createConsumer = vitest.fn().mockImplementation(() => Promise.resolve(new TestConsumer()));
10
+ this.createBatchConsumer = vitest.fn().mockImplementation(() => Promise.resolve(new TestBatchConsumer()));
11
+ this.createProducer = vitest.fn().mockImplementation(() => Promise.resolve(new TestProducer()));
12
+ }
13
+ }
@@ -0,0 +1,2 @@
1
+ export * from './zod-validated-consumer';
2
+ export * from './zod-validated-batch-consumer';
@@ -0,0 +1,2 @@
1
+ export * from './zod-validated-consumer';
2
+ export * from './zod-validated-batch-consumer';