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.
- package/LICENSE +8 -0
- package/README.md +63 -0
- package/dist/cjs/channel/channel-implementation.d.ts +30 -0
- package/dist/cjs/channel/channel-implementation.js +212 -0
- package/dist/cjs/channel/channel.d.ts +149 -0
- package/dist/cjs/channel/channel.js +2 -0
- package/dist/cjs/channel/index.d.ts +2 -0
- package/dist/cjs/channel/index.js +5 -0
- package/dist/cjs/channel/types.d.ts +12 -0
- package/dist/cjs/channel/types.js +2 -0
- package/dist/cjs/connection/connection-implementation.d.ts +35 -0
- package/dist/cjs/connection/connection-implementation.js +195 -0
- package/dist/cjs/connection/connection.d.ts +143 -0
- package/dist/cjs/connection/connection.js +11 -0
- package/dist/cjs/connection/index.d.ts +2 -0
- package/dist/cjs/connection/index.js +8 -0
- package/dist/cjs/consumer/base-consumer.d.ts +33 -0
- package/dist/cjs/consumer/base-consumer.js +85 -0
- package/dist/cjs/consumer/batch-consumer-implementation.d.ts +25 -0
- package/dist/cjs/consumer/batch-consumer-implementation.js +313 -0
- package/dist/cjs/consumer/batch-consumer.d.ts +39 -0
- package/dist/cjs/consumer/batch-consumer.js +2 -0
- package/dist/cjs/consumer/consumer-implementation.d.ts +12 -0
- package/dist/cjs/consumer/consumer-implementation.js +105 -0
- package/dist/cjs/consumer/consumer.d.ts +40 -0
- package/dist/cjs/consumer/consumer.js +4 -0
- package/dist/cjs/consumer/index.d.ts +5 -0
- package/dist/cjs/consumer/index.js +10 -0
- package/dist/cjs/consumer/types.d.ts +194 -0
- package/dist/cjs/consumer/types.js +34 -0
- package/dist/cjs/errors.d.ts +22 -0
- package/dist/cjs/errors.js +13 -0
- package/dist/cjs/exchange/default-exchange.d.ts +10 -0
- package/dist/cjs/exchange/default-exchange.js +14 -0
- package/dist/cjs/exchange/exchange-implementation.d.ts +24 -0
- package/dist/cjs/exchange/exchange-implementation.js +159 -0
- package/dist/cjs/exchange/exchange.d.ts +63 -0
- package/dist/cjs/exchange/exchange.js +2 -0
- package/dist/cjs/exchange/index.d.ts +3 -0
- package/dist/cjs/exchange/index.js +5 -0
- package/dist/cjs/exchange/types.d.ts +58 -0
- package/dist/cjs/exchange/types.js +8 -0
- package/dist/cjs/extensions/jest/index.d.ts +7 -0
- package/dist/cjs/extensions/jest/index.js +23 -0
- package/dist/cjs/extensions/jest/test-batch-consumer.d.ts +48 -0
- package/dist/cjs/extensions/jest/test-batch-consumer.js +56 -0
- package/dist/cjs/extensions/jest/test-channel.d.ts +71 -0
- package/dist/cjs/extensions/jest/test-channel.js +75 -0
- package/dist/cjs/extensions/jest/test-connection.d.ts +38 -0
- package/dist/cjs/extensions/jest/test-connection.js +29 -0
- package/dist/cjs/extensions/jest/test-consumer.d.ts +49 -0
- package/dist/cjs/extensions/jest/test-consumer.js +54 -0
- package/dist/cjs/extensions/jest/test-exchange.d.ts +26 -0
- package/dist/cjs/extensions/jest/test-exchange.js +16 -0
- package/dist/cjs/extensions/jest/test-producer.d.ts +33 -0
- package/dist/cjs/extensions/jest/test-producer.js +17 -0
- package/dist/cjs/extensions/jest/test-queue.d.ts +25 -0
- package/dist/cjs/extensions/jest/test-queue.js +17 -0
- package/dist/cjs/extensions/vitest/index.d.ts +7 -0
- package/dist/cjs/extensions/vitest/index.js +23 -0
- package/dist/cjs/extensions/vitest/test-batch-consumer.d.ts +48 -0
- package/dist/cjs/extensions/vitest/test-batch-consumer.js +56 -0
- package/dist/cjs/extensions/vitest/test-channel.d.ts +71 -0
- package/dist/cjs/extensions/vitest/test-channel.js +77 -0
- package/dist/cjs/extensions/vitest/test-connection.d.ts +38 -0
- package/dist/cjs/extensions/vitest/test-connection.js +29 -0
- package/dist/cjs/extensions/vitest/test-consumer.d.ts +49 -0
- package/dist/cjs/extensions/vitest/test-consumer.js +54 -0
- package/dist/cjs/extensions/vitest/test-exchange.d.ts +26 -0
- package/dist/cjs/extensions/vitest/test-exchange.js +16 -0
- package/dist/cjs/extensions/vitest/test-producer.d.ts +34 -0
- package/dist/cjs/extensions/vitest/test-producer.js +18 -0
- package/dist/cjs/extensions/vitest/test-queue.d.ts +25 -0
- package/dist/cjs/extensions/vitest/test-queue.js +17 -0
- package/dist/cjs/extensions/zod/index.d.ts +2 -0
- package/dist/cjs/extensions/zod/index.js +18 -0
- package/dist/cjs/extensions/zod/zod-validated-batch-consumer.d.ts +57 -0
- package/dist/cjs/extensions/zod/zod-validated-batch-consumer.js +46 -0
- package/dist/cjs/extensions/zod/zod-validated-consumer.d.ts +55 -0
- package/dist/cjs/extensions/zod/zod-validated-consumer.js +42 -0
- package/dist/cjs/graceful-shutdown/index.d.ts +1 -0
- package/dist/cjs/graceful-shutdown/index.js +17 -0
- package/dist/cjs/graceful-shutdown/rabbit-closer.d.ts +29 -0
- package/dist/cjs/graceful-shutdown/rabbit-closer.js +42 -0
- package/dist/cjs/index.d.ts +11 -0
- package/dist/cjs/index.js +51 -0
- package/dist/cjs/package.json +1 -0
- package/dist/cjs/predefined.d.ts +62 -0
- package/dist/cjs/predefined.js +27 -0
- package/dist/cjs/producer/index.d.ts +3 -0
- package/dist/cjs/producer/index.js +5 -0
- package/dist/cjs/producer/producer-implementation.d.ts +18 -0
- package/dist/cjs/producer/producer-implementation.js +109 -0
- package/dist/cjs/producer/producer.d.ts +44 -0
- package/dist/cjs/producer/producer.js +2 -0
- package/dist/cjs/producer/types.d.ts +70 -0
- package/dist/cjs/producer/types.js +13 -0
- package/dist/cjs/queue/index.d.ts +3 -0
- package/dist/cjs/queue/index.js +5 -0
- package/dist/cjs/queue/queue-implementation.d.ts +21 -0
- package/dist/cjs/queue/queue-implementation.js +91 -0
- package/dist/cjs/queue/queue.d.ts +51 -0
- package/dist/cjs/queue/queue.js +2 -0
- package/dist/cjs/queue/types.d.ts +25 -0
- package/dist/cjs/queue/types.js +2 -0
- package/dist/cjs/retry/default-retry-strategy.d.ts +9 -0
- package/dist/cjs/retry/default-retry-strategy.js +8 -0
- package/dist/cjs/retry/index.d.ts +4 -0
- package/dist/cjs/retry/index.js +45 -0
- package/dist/cjs/retry/retry-implementation.d.ts +18 -0
- package/dist/cjs/retry/retry-implementation.js +44 -0
- package/dist/cjs/retry/retry-strategy.d.ts +28 -0
- package/dist/cjs/retry/retry-strategy.js +10 -0
- package/dist/cjs/retry/time-strategies.d.ts +45 -0
- package/dist/cjs/retry/time-strategies.js +32 -0
- package/dist/cjs/types.d.ts +30 -0
- package/dist/cjs/types.js +9 -0
- package/dist/cjs/utils.d.ts +11 -0
- package/dist/cjs/utils.js +83 -0
- package/dist/esm/channel/channel-implementation.d.ts +30 -0
- package/dist/esm/channel/channel-implementation.js +208 -0
- package/dist/esm/channel/channel.d.ts +149 -0
- package/dist/esm/channel/channel.js +1 -0
- package/dist/esm/channel/index.d.ts +2 -0
- package/dist/esm/channel/index.js +1 -0
- package/dist/esm/channel/types.d.ts +12 -0
- package/dist/esm/channel/types.js +1 -0
- package/dist/esm/connection/connection-implementation.d.ts +35 -0
- package/dist/esm/connection/connection-implementation.js +157 -0
- package/dist/esm/connection/connection.d.ts +143 -0
- package/dist/esm/connection/connection.js +8 -0
- package/dist/esm/connection/index.d.ts +2 -0
- package/dist/esm/connection/index.js +2 -0
- package/dist/esm/consumer/base-consumer.d.ts +33 -0
- package/dist/esm/consumer/base-consumer.js +81 -0
- package/dist/esm/consumer/batch-consumer-implementation.d.ts +25 -0
- package/dist/esm/consumer/batch-consumer-implementation.js +309 -0
- package/dist/esm/consumer/batch-consumer.d.ts +39 -0
- package/dist/esm/consumer/batch-consumer.js +1 -0
- package/dist/esm/consumer/consumer-implementation.d.ts +12 -0
- package/dist/esm/consumer/consumer-implementation.js +101 -0
- package/dist/esm/consumer/consumer.d.ts +40 -0
- package/dist/esm/consumer/consumer.js +1 -0
- package/dist/esm/consumer/index.d.ts +5 -0
- package/dist/esm/consumer/index.js +3 -0
- package/dist/esm/consumer/types.d.ts +194 -0
- package/dist/esm/consumer/types.js +31 -0
- package/dist/esm/errors.d.ts +22 -0
- package/dist/esm/errors.js +8 -0
- package/dist/esm/exchange/default-exchange.d.ts +10 -0
- package/dist/esm/exchange/default-exchange.js +10 -0
- package/dist/esm/exchange/exchange-implementation.d.ts +24 -0
- package/dist/esm/exchange/exchange-implementation.js +152 -0
- package/dist/esm/exchange/exchange.d.ts +63 -0
- package/dist/esm/exchange/exchange.js +1 -0
- package/dist/esm/exchange/index.d.ts +3 -0
- package/dist/esm/exchange/index.js +1 -0
- package/dist/esm/exchange/types.d.ts +58 -0
- package/dist/esm/exchange/types.js +5 -0
- package/dist/esm/extensions/jest/index.d.ts +7 -0
- package/dist/esm/extensions/jest/index.js +7 -0
- package/dist/esm/extensions/jest/test-batch-consumer.d.ts +48 -0
- package/dist/esm/extensions/jest/test-batch-consumer.js +52 -0
- package/dist/esm/extensions/jest/test-channel.d.ts +71 -0
- package/dist/esm/extensions/jest/test-channel.js +71 -0
- package/dist/esm/extensions/jest/test-connection.d.ts +38 -0
- package/dist/esm/extensions/jest/test-connection.js +25 -0
- package/dist/esm/extensions/jest/test-consumer.d.ts +49 -0
- package/dist/esm/extensions/jest/test-consumer.js +50 -0
- package/dist/esm/extensions/jest/test-exchange.d.ts +26 -0
- package/dist/esm/extensions/jest/test-exchange.js +12 -0
- package/dist/esm/extensions/jest/test-producer.d.ts +33 -0
- package/dist/esm/extensions/jest/test-producer.js +13 -0
- package/dist/esm/extensions/jest/test-queue.d.ts +25 -0
- package/dist/esm/extensions/jest/test-queue.js +13 -0
- package/dist/esm/extensions/vitest/index.d.ts +7 -0
- package/dist/esm/extensions/vitest/index.js +7 -0
- package/dist/esm/extensions/vitest/test-batch-consumer.d.ts +48 -0
- package/dist/esm/extensions/vitest/test-batch-consumer.js +52 -0
- package/dist/esm/extensions/vitest/test-channel.d.ts +71 -0
- package/dist/esm/extensions/vitest/test-channel.js +73 -0
- package/dist/esm/extensions/vitest/test-connection.d.ts +38 -0
- package/dist/esm/extensions/vitest/test-connection.js +25 -0
- package/dist/esm/extensions/vitest/test-consumer.d.ts +49 -0
- package/dist/esm/extensions/vitest/test-consumer.js +50 -0
- package/dist/esm/extensions/vitest/test-exchange.d.ts +26 -0
- package/dist/esm/extensions/vitest/test-exchange.js +12 -0
- package/dist/esm/extensions/vitest/test-producer.d.ts +34 -0
- package/dist/esm/extensions/vitest/test-producer.js +14 -0
- package/dist/esm/extensions/vitest/test-queue.d.ts +25 -0
- package/dist/esm/extensions/vitest/test-queue.js +13 -0
- package/dist/esm/extensions/zod/index.d.ts +2 -0
- package/dist/esm/extensions/zod/index.js +2 -0
- package/dist/esm/extensions/zod/zod-validated-batch-consumer.d.ts +57 -0
- package/dist/esm/extensions/zod/zod-validated-batch-consumer.js +42 -0
- package/dist/esm/extensions/zod/zod-validated-consumer.d.ts +55 -0
- package/dist/esm/extensions/zod/zod-validated-consumer.js +38 -0
- package/dist/esm/graceful-shutdown/index.d.ts +1 -0
- package/dist/esm/graceful-shutdown/index.js +1 -0
- package/dist/esm/graceful-shutdown/rabbit-closer.d.ts +29 -0
- package/dist/esm/graceful-shutdown/rabbit-closer.js +38 -0
- package/dist/esm/index.d.ts +11 -0
- package/dist/esm/index.js +11 -0
- package/dist/esm/package.json +1 -0
- package/dist/esm/predefined.d.ts +62 -0
- package/dist/esm/predefined.js +18 -0
- package/dist/esm/producer/index.d.ts +3 -0
- package/dist/esm/producer/index.js +1 -0
- package/dist/esm/producer/producer-implementation.d.ts +18 -0
- package/dist/esm/producer/producer-implementation.js +105 -0
- package/dist/esm/producer/producer.d.ts +44 -0
- package/dist/esm/producer/producer.js +1 -0
- package/dist/esm/producer/types.d.ts +70 -0
- package/dist/esm/producer/types.js +10 -0
- package/dist/esm/queue/index.d.ts +3 -0
- package/dist/esm/queue/index.js +1 -0
- package/dist/esm/queue/queue-implementation.d.ts +21 -0
- package/dist/esm/queue/queue-implementation.js +87 -0
- package/dist/esm/queue/queue.d.ts +51 -0
- package/dist/esm/queue/queue.js +1 -0
- package/dist/esm/queue/types.d.ts +25 -0
- package/dist/esm/queue/types.js +1 -0
- package/dist/esm/retry/default-retry-strategy.d.ts +9 -0
- package/dist/esm/retry/default-retry-strategy.js +5 -0
- package/dist/esm/retry/index.d.ts +4 -0
- package/dist/esm/retry/index.js +4 -0
- package/dist/esm/retry/retry-implementation.d.ts +18 -0
- package/dist/esm/retry/retry-implementation.js +40 -0
- package/dist/esm/retry/retry-strategy.d.ts +28 -0
- package/dist/esm/retry/retry-strategy.js +6 -0
- package/dist/esm/retry/time-strategies.d.ts +45 -0
- package/dist/esm/retry/time-strategies.js +24 -0
- package/dist/esm/types.d.ts +30 -0
- package/dist/esm/types.js +6 -0
- package/dist/esm/utils.d.ts +11 -0
- package/dist/esm/utils.js +72 -0
- package/package.json +101 -6
- package/.claude/settings.local.json +0 -13
- package/.idea/amqp-oop.iml +0 -15
- package/.idea/aws.xml +0 -11
- package/.idea/inspectionProfiles/Project_Default.xml +0 -6
- package/.idea/jsLibraryMappings.xml +0 -6
- package/.idea/jsLinters/eslint.xml +0 -6
- package/.idea/modules.xml +0 -8
- package/.idea/vcs.xml +0 -6
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { Queue } from '../queue';
|
|
2
|
+
import { Consumer, BatchConsumer } from '../consumer';
|
|
3
|
+
import { ProducerOptions, Producer } from '../producer';
|
|
4
|
+
import { ExchangeConsumerQueueOptions, ExchangeConsumerOptions, ExchangeBatchConsumerOptions, BindingArgs } from './types';
|
|
5
|
+
export interface Exchange {
|
|
6
|
+
/**
|
|
7
|
+
* Verify the exchange against the broker, according to the configured `assertionMode`.
|
|
8
|
+
*
|
|
9
|
+
* - `Assert` (default) — declare the exchange; creates it if absent, throws on config mismatch.
|
|
10
|
+
* - `Check` — verify the exchange exists without creating it; throws if absent.
|
|
11
|
+
* - `Passive` — no-op; no network call is made.
|
|
12
|
+
*
|
|
13
|
+
* Subsequent calls return immediately once the first call completes successfully.
|
|
14
|
+
* Returns `this` to allow chaining.
|
|
15
|
+
*/
|
|
16
|
+
assert(): Promise<Exchange>;
|
|
17
|
+
/**
|
|
18
|
+
* Returns the name of the exchange.
|
|
19
|
+
* Asserts the exchange first if it has not been asserted yet.
|
|
20
|
+
*/
|
|
21
|
+
name(): Promise<string>;
|
|
22
|
+
/**
|
|
23
|
+
* Bind a queue to this exchange.
|
|
24
|
+
* Asserts the exchange first if it has not been asserted yet.
|
|
25
|
+
* @param queue - Queue to bind to this exchange.
|
|
26
|
+
* @param pattern - Routing key pattern under which to bind.
|
|
27
|
+
* @param args - Optional AMQP headers or x-arguments for the binding.
|
|
28
|
+
*/
|
|
29
|
+
bindQueue(queue: Queue, pattern: string, args?: BindingArgs): Promise<Exchange>;
|
|
30
|
+
/**
|
|
31
|
+
* Bind another exchange to this exchange.
|
|
32
|
+
* Asserts the exchange first if it has not been asserted yet.
|
|
33
|
+
* @param exchange - Exchange to bind to this exchange.
|
|
34
|
+
* @param pattern - Routing key pattern under which to bind.
|
|
35
|
+
* @param args - Optional AMQP headers or x-arguments for the binding.
|
|
36
|
+
*/
|
|
37
|
+
bindExchange(exchange: Exchange, pattern: string, args?: BindingArgs): Promise<Exchange>;
|
|
38
|
+
/**
|
|
39
|
+
* Creates a consumer for the current exchange.
|
|
40
|
+
* Internally asserts an exclusive queue and binds it to the exchange.
|
|
41
|
+
* Unless specified otherwise, uses the same channel as was used to declare the exchange.
|
|
42
|
+
* This is generally not recommended, as each consumer should have its own dedicated channel.
|
|
43
|
+
* Consider using `connection.createConsumerForExchange` instead.
|
|
44
|
+
*/
|
|
45
|
+
createConsumer<T>(options: ExchangeConsumerOptions<T>, queueOptions?: ExchangeConsumerQueueOptions): Promise<Consumer<T>>;
|
|
46
|
+
/**
|
|
47
|
+
* Creates a batch consumer for the current exchange.
|
|
48
|
+
* Internally asserts an exclusive queue and binds it to the exchange.
|
|
49
|
+
* Unless specified otherwise, uses the same channel as was used to declare the exchange.
|
|
50
|
+
* This is generally not recommended, as each consumer should have its own dedicated channel.
|
|
51
|
+
* Consider using `connection.createBatchConsumerForExchange` instead.
|
|
52
|
+
*/
|
|
53
|
+
createBatchConsumer<T>(options: ExchangeBatchConsumerOptions<T>, queueOptions?: ExchangeConsumerQueueOptions): Promise<BatchConsumer<T>>;
|
|
54
|
+
/**
|
|
55
|
+
* Creates a producer for the current exchange.
|
|
56
|
+
* Unless specified otherwise, uses the same channel as was used to declare the exchange.
|
|
57
|
+
* This is generally not recommended, as each producer should have its own dedicated channel.
|
|
58
|
+
* Consider using `connection.createProducerForExchange` instead.
|
|
59
|
+
*/
|
|
60
|
+
createProducer<T>(options?: ProducerOptions<T> & {
|
|
61
|
+
isConfirmed?: boolean;
|
|
62
|
+
}): Promise<Producer<T>>;
|
|
63
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { ExchangeImplementation } from './exchange-implementation';
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import * as amqp from 'amqplib';
|
|
2
|
+
import { AssertionMode } from '../types';
|
|
3
|
+
import { ConsumerOptions, BatchConsumerOptions } from '../consumer';
|
|
4
|
+
import { Queue } from '../queue';
|
|
5
|
+
import { Exchange } from './exchange';
|
|
6
|
+
/**
|
|
7
|
+
* The type of exchange to create. Accepted values: `'direct'`, `'fanout'`, `'topic'`, `'headers'`, `'match'`.
|
|
8
|
+
*
|
|
9
|
+
* Passed as the second argument to `channel.assertExchange` in the amqplib API.
|
|
10
|
+
*
|
|
11
|
+
* @see https://amqp-node.github.io/amqplib/channel_api.html
|
|
12
|
+
*/
|
|
13
|
+
export type ExchangeTypes = Parameters<amqp.Channel['assertExchange']>[1];
|
|
14
|
+
/**
|
|
15
|
+
* Options for the auto-created exclusive queue when consuming from an exchange.
|
|
16
|
+
* The `durable` and `exclusive` fields are omitted because they are managed internally.
|
|
17
|
+
*/
|
|
18
|
+
export type ExchangeConsumerQueueOptions = Omit<amqp.Options.AssertQueue, 'durable' | 'exclusive'>;
|
|
19
|
+
export type ExchangeConsumerBindingOptions = {
|
|
20
|
+
pattern: string;
|
|
21
|
+
bindingArgs?: BindingArgs;
|
|
22
|
+
};
|
|
23
|
+
/**
|
|
24
|
+
* Options for consuming messages from an exchange.
|
|
25
|
+
* Combines binding options (`pattern`, optional `bindingArgs`) with standard {@link ConsumerOptions}.
|
|
26
|
+
*/
|
|
27
|
+
export type ExchangeConsumerOptions<T> = ExchangeConsumerBindingOptions & ConsumerOptions<T>;
|
|
28
|
+
/**
|
|
29
|
+
* Options for batch-consuming messages from an exchange.
|
|
30
|
+
* Combines binding options (`pattern`, optional `bindingArgs`) with standard {@link BatchConsumerOptions}.
|
|
31
|
+
*/
|
|
32
|
+
export type ExchangeBatchConsumerOptions<T> = ExchangeConsumerBindingOptions & BatchConsumerOptions<T>;
|
|
33
|
+
export type BindingArgs = Record<string, string | number | boolean>;
|
|
34
|
+
export declare enum BindingType {
|
|
35
|
+
queue = "queue",
|
|
36
|
+
exchange = "exchange"
|
|
37
|
+
}
|
|
38
|
+
type Pattern = string;
|
|
39
|
+
export type Binding = {
|
|
40
|
+
pattern: Pattern;
|
|
41
|
+
args?: BindingArgs;
|
|
42
|
+
} & ({
|
|
43
|
+
type: BindingType.queue;
|
|
44
|
+
queue: Queue;
|
|
45
|
+
} | {
|
|
46
|
+
type: BindingType.exchange;
|
|
47
|
+
exchange: Exchange;
|
|
48
|
+
});
|
|
49
|
+
export type ExchangeOptions = amqp.Options.AssertExchange & {
|
|
50
|
+
/**
|
|
51
|
+
* Controls how the exchange is verified against the broker when `assert()` is called.
|
|
52
|
+
* Defaults to `AssertionMode.Assert`.
|
|
53
|
+
*
|
|
54
|
+
* @see AssertionMode
|
|
55
|
+
*/
|
|
56
|
+
assertionMode?: AssertionMode;
|
|
57
|
+
};
|
|
58
|
+
export {};
|
|
@@ -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 jest 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 jest 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/jest';
|
|
22
|
+
*
|
|
23
|
+
* const consumer = new TestBatchConsumer<{ id: number }>();
|
|
24
|
+
*
|
|
25
|
+
* const handler = jest.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: jest.Mock<any, any, any>;
|
|
36
|
+
listen: jest.Mock<any, any, any>;
|
|
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 = jest.fn().mockImplementation(() => Promise.resolve());
|
|
28
|
+
this.listen = jest.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] : [];
|
|
37
|
+
const callback = lastCall[0];
|
|
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 jest 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/jest';
|
|
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
|
+
consumeResponse: {
|
|
29
|
+
consumerTag: `${string}-${string}-${string}-${string}-${string}`;
|
|
30
|
+
};
|
|
31
|
+
nativeChannel: {
|
|
32
|
+
cancel: jest.Mock<any, any, any>;
|
|
33
|
+
prefetch: jest.Mock<any, any, any>;
|
|
34
|
+
consume: jest.Mock<any, any, any>;
|
|
35
|
+
ack: jest.Mock<any, any, any>;
|
|
36
|
+
nack: jest.Mock<any, any, any>;
|
|
37
|
+
assertQueue: jest.Mock<any, any, any>;
|
|
38
|
+
checkQueue: jest.Mock<any, any, any>;
|
|
39
|
+
assertExchange: jest.Mock<any, any, any>;
|
|
40
|
+
checkExchange: jest.Mock<any, any, any>;
|
|
41
|
+
bindQueue: jest.Mock<any, any, any>;
|
|
42
|
+
bindExchange: jest.Mock<any, any, any>;
|
|
43
|
+
};
|
|
44
|
+
connect: jest.Mock<any, any, any>;
|
|
45
|
+
close: jest.Mock<any, any, any>;
|
|
46
|
+
createExchange: jest.Mock<any, any, any>;
|
|
47
|
+
createQueue: jest.Mock<any, any, any>;
|
|
48
|
+
createProducerForQueue: jest.Mock<any, any, any>;
|
|
49
|
+
createProducerForExchange: jest.Mock<any, any, any>;
|
|
50
|
+
createConsumerForQueue: jest.Mock<any, any, any>;
|
|
51
|
+
createConsumerForExchange: jest.Mock<any, any, any>;
|
|
52
|
+
createBatchConsumerForQueue: jest.Mock<any, any, any>;
|
|
53
|
+
createBatchConsumerForExchange: jest.Mock<any, any, any>;
|
|
54
|
+
native: jest.Mock<any, any, any>;
|
|
55
|
+
private _drainPending;
|
|
56
|
+
private _drainResolvers;
|
|
57
|
+
publish: jest.Mock<any, any, any>;
|
|
58
|
+
checkQueue: jest.Mock<any, any, any>;
|
|
59
|
+
/**
|
|
60
|
+
* Simulates a channel close by emitting the `close` event.
|
|
61
|
+
*/
|
|
62
|
+
simulateClose(): void;
|
|
63
|
+
/**
|
|
64
|
+
* Simulates write-buffer backpressure. Subsequent `publish` calls will not resolve until {@link releaseDrain} is called.
|
|
65
|
+
*/
|
|
66
|
+
simulateDrainBackpressure(): void;
|
|
67
|
+
/**
|
|
68
|
+
* Releases simulated backpressure, resolves all pending `publish` calls, and emits the `drain` event.
|
|
69
|
+
*/
|
|
70
|
+
releaseDrain(): void;
|
|
71
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
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.consumeResponse = {
|
|
8
|
+
consumerTag: crypto.randomUUID(),
|
|
9
|
+
};
|
|
10
|
+
this.nativeChannel = {
|
|
11
|
+
cancel: jest.fn().mockImplementation(() => Promise.resolve()),
|
|
12
|
+
prefetch: jest.fn().mockImplementation(() => Promise.resolve()),
|
|
13
|
+
consume: jest.fn().mockImplementation(() => Promise.resolve(this.consumeResponse)),
|
|
14
|
+
ack: jest.fn(),
|
|
15
|
+
nack: jest.fn(),
|
|
16
|
+
assertQueue: jest.fn().mockImplementation((queue) => Promise.resolve({
|
|
17
|
+
queue,
|
|
18
|
+
messageCount: 0,
|
|
19
|
+
consumerCount: 0,
|
|
20
|
+
})),
|
|
21
|
+
checkQueue: jest.fn().mockImplementation((queue) => Promise.resolve({
|
|
22
|
+
queue,
|
|
23
|
+
messageCount: 0,
|
|
24
|
+
consumerCount: 0,
|
|
25
|
+
})),
|
|
26
|
+
assertExchange: jest.fn(),
|
|
27
|
+
checkExchange: jest.fn(),
|
|
28
|
+
bindQueue: jest.fn(),
|
|
29
|
+
bindExchange: jest.fn(),
|
|
30
|
+
};
|
|
31
|
+
this.connect = jest.fn().mockImplementation(() => Promise.resolve(this));
|
|
32
|
+
this.close = jest.fn().mockImplementation(() => Promise.resolve());
|
|
33
|
+
this.createExchange = jest.fn().mockImplementation(() => Promise.resolve(new TestExchange()));
|
|
34
|
+
this.createQueue = jest.fn().mockImplementation(() => Promise.resolve(new TestQueue()));
|
|
35
|
+
this.createProducerForQueue = jest.fn().mockImplementation(() => Promise.resolve(new TestProducer()));
|
|
36
|
+
this.createProducerForExchange = jest.fn().mockImplementation(() => Promise.resolve(new TestProducer()));
|
|
37
|
+
this.createConsumerForQueue = jest.fn().mockImplementation(() => Promise.resolve(new TestConsumer()));
|
|
38
|
+
this.createConsumerForExchange = jest.fn().mockImplementation(() => Promise.resolve(new TestConsumer()));
|
|
39
|
+
this.createBatchConsumerForQueue = jest.fn().mockImplementation(() => Promise.resolve(new TestBatchConsumer()));
|
|
40
|
+
this.createBatchConsumerForExchange = jest.fn().mockImplementation(() => Promise.resolve(new TestBatchConsumer()));
|
|
41
|
+
this.native = jest.fn().mockImplementation(() => Promise.resolve(this.nativeChannel));
|
|
42
|
+
this._drainPending = false;
|
|
43
|
+
this._drainResolvers = [];
|
|
44
|
+
this.publish = jest.fn().mockImplementation(() => {
|
|
45
|
+
if (this._drainPending) {
|
|
46
|
+
return new Promise(resolve => {
|
|
47
|
+
this._drainResolvers.push(() => resolve(false));
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
return Promise.resolve(false);
|
|
51
|
+
});
|
|
52
|
+
this.checkQueue = jest.fn().mockImplementation((queue) => Promise.resolve({
|
|
53
|
+
queue,
|
|
54
|
+
messageCount: 0,
|
|
55
|
+
consumerCount: 0,
|
|
56
|
+
}));
|
|
57
|
+
}
|
|
58
|
+
simulateClose() {
|
|
59
|
+
this.emit('close');
|
|
60
|
+
}
|
|
61
|
+
simulateDrainBackpressure() {
|
|
62
|
+
this._drainPending = true;
|
|
63
|
+
}
|
|
64
|
+
releaseDrain() {
|
|
65
|
+
this._drainPending = false;
|
|
66
|
+
const resolvers = this._drainResolvers.splice(0);
|
|
67
|
+
this.emit('drain');
|
|
68
|
+
for (const resolve of resolvers)
|
|
69
|
+
resolve();
|
|
70
|
+
}
|
|
71
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { EventEmitter } from 'events';
|
|
2
|
+
import { Connection } from '../../index';
|
|
3
|
+
/**
|
|
4
|
+
* Mock implementation of Connection using jest 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/jest';
|
|
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: jest.Mock<any, any, any>;
|
|
24
|
+
connect: jest.Mock<any, any, any>;
|
|
25
|
+
createChannel: jest.Mock<any, any, any>;
|
|
26
|
+
createConsumerForExchange: jest.Mock<any, any, any>;
|
|
27
|
+
createConsumerForQueue: jest.Mock<any, any, any>;
|
|
28
|
+
createBatchConsumerForQueue: jest.Mock<any, any, any>;
|
|
29
|
+
createBatchConsumerForExchange: jest.Mock<any, any, any>;
|
|
30
|
+
createProducerForExchange: jest.Mock<any, any, any>;
|
|
31
|
+
createProducerForQueue: jest.Mock<any, any, any>;
|
|
32
|
+
native: jest.Mock<any, any, any>;
|
|
33
|
+
state: jest.Mock<any, any, any>;
|
|
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 = jest.fn().mockImplementation(() => Promise.resolve());
|
|
11
|
+
this.connect = jest.fn().mockImplementation(() => Promise.resolve(this));
|
|
12
|
+
this.createChannel = jest.fn().mockImplementation(() => new TestChannel(this));
|
|
13
|
+
this.createConsumerForExchange = jest.fn().mockImplementation(() => Promise.resolve(new TestConsumer()));
|
|
14
|
+
this.createConsumerForQueue = jest.fn().mockImplementation(() => Promise.resolve(new TestConsumer()));
|
|
15
|
+
this.createBatchConsumerForQueue = jest.fn().mockImplementation(() => Promise.resolve(new TestBatchConsumer()));
|
|
16
|
+
this.createBatchConsumerForExchange = jest.fn().mockImplementation(() => Promise.resolve(new TestBatchConsumer()));
|
|
17
|
+
this.createProducerForExchange = jest.fn().mockImplementation(() => Promise.resolve(new TestProducer()));
|
|
18
|
+
this.createProducerForQueue = jest.fn().mockImplementation(() => Promise.resolve(new TestProducer()));
|
|
19
|
+
this.native = jest.fn().mockResolvedValue(undefined);
|
|
20
|
+
this.state = jest.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 jest 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 jest mocks returning
|
|
15
|
+
* void or the current instance, depending on the method semantic.
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* ```ts
|
|
19
|
+
* import { TestConsumer } from 'amqpx/jest';
|
|
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: jest.Mock<any, any, any>;
|
|
36
|
+
listen: jest.Mock<any, any, any>;
|
|
37
|
+
setPrefetch: jest.Mock<any, any, any>;
|
|
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 = jest.fn().mockImplementation(() => Promise.resolve());
|
|
28
|
+
this.listen = jest.fn().mockImplementation(() => Promise.resolve(this));
|
|
29
|
+
this.setPrefetch = jest.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] : [];
|
|
38
|
+
const callback = lastCall[0];
|
|
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 jest mocks.
|
|
4
|
+
*
|
|
5
|
+
* All `create*` methods return test classes from this package.
|
|
6
|
+
* `assert`, `bindQueue`, and `bindExchange` methods are jest mocks returning current instance.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```ts
|
|
10
|
+
* import { TestExchange } from 'amqpx/jest';
|
|
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: jest.Mock<any, any, any>;
|
|
20
|
+
name: jest.Mock<any, any, any>;
|
|
21
|
+
bindQueue: jest.Mock<any, any, any>;
|
|
22
|
+
bindExchange: jest.Mock<any, any, any>;
|
|
23
|
+
createConsumer: jest.Mock<any, any, any>;
|
|
24
|
+
createBatchConsumer: jest.Mock<any, any, any>;
|
|
25
|
+
createProducer: jest.Mock<any, any, any>;
|
|
26
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { TestConsumer, TestBatchConsumer, TestProducer } from '.';
|
|
2
|
+
export class TestExchange {
|
|
3
|
+
constructor() {
|
|
4
|
+
this.assert = jest.fn().mockImplementation(() => Promise.resolve(this));
|
|
5
|
+
this.name = jest.fn().mockImplementation(() => Promise.resolve('test-exchange'));
|
|
6
|
+
this.bindQueue = jest.fn().mockImplementation(() => Promise.resolve(this));
|
|
7
|
+
this.bindExchange = jest.fn().mockImplementation(() => Promise.resolve(this));
|
|
8
|
+
this.createConsumer = jest.fn().mockImplementation(() => Promise.resolve(new TestConsumer()));
|
|
9
|
+
this.createBatchConsumer = jest.fn().mockImplementation(() => Promise.resolve(new TestBatchConsumer()));
|
|
10
|
+
this.createProducer = jest.fn().mockImplementation(() => Promise.resolve(new TestProducer()));
|
|
11
|
+
}
|
|
12
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { EventEmitter } from 'events';
|
|
2
|
+
import { Producer } from '../../index';
|
|
3
|
+
import { TestChannel } from './test-channel';
|
|
4
|
+
/**
|
|
5
|
+
* Test implementation of Producer using jest 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/jest';
|
|
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
|
+
close: jest.Mock<any, any, any>;
|
|
27
|
+
publish: jest.Mock<any, any, any>;
|
|
28
|
+
channel: TestChannel;
|
|
29
|
+
/**
|
|
30
|
+
* Returns the first argument (the message payload) from every `publish` call recorded so far.
|
|
31
|
+
*/
|
|
32
|
+
getPublishedMessages(): T[];
|
|
33
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { EventEmitter } from 'events';
|
|
2
|
+
import { TestChannel } from './test-channel';
|
|
3
|
+
export class TestProducer extends EventEmitter {
|
|
4
|
+
constructor() {
|
|
5
|
+
super(...arguments);
|
|
6
|
+
this.close = jest.fn().mockResolvedValue(undefined);
|
|
7
|
+
this.publish = jest.fn().mockImplementation(msg => Promise.resolve(msg));
|
|
8
|
+
this.channel = new TestChannel();
|
|
9
|
+
}
|
|
10
|
+
getPublishedMessages() {
|
|
11
|
+
return this.publish.mock.calls.map(([msg]) => msg);
|
|
12
|
+
}
|
|
13
|
+
}
|