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,101 @@
|
|
|
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
|
+
import { debuglog } from 'util';
|
|
11
|
+
import { deepMerge, errToMessage, LIB_NAME } from '../utils';
|
|
12
|
+
import { ConsumptionFailureStrategy, DEFAULT_CONSUMER_OPTIONS, } from './types';
|
|
13
|
+
import { BaseConsumer } from './base-consumer';
|
|
14
|
+
const debug = debuglog(`${LIB_NAME}:consume:simple`);
|
|
15
|
+
export class ConsumerImplementation extends BaseConsumer {
|
|
16
|
+
constructor(channel, queue, options) {
|
|
17
|
+
super(channel, queue);
|
|
18
|
+
this.options = deepMerge({}, DEFAULT_CONSUMER_OPTIONS, options);
|
|
19
|
+
}
|
|
20
|
+
listen(callback) {
|
|
21
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
22
|
+
if (this.consumer)
|
|
23
|
+
throw new Error('Listener is already attached');
|
|
24
|
+
this.consumer = (() => __awaiter(this, void 0, void 0, function* () {
|
|
25
|
+
const [channel, queueName] = yield Promise.all([
|
|
26
|
+
this.channel.native(),
|
|
27
|
+
this.queue.name(),
|
|
28
|
+
]);
|
|
29
|
+
const shouldAck = this.shouldAcknowledge();
|
|
30
|
+
debug('start-listening queue=%s prefetch=%d ack=%s', queueName, this.options.prefetch, shouldAck);
|
|
31
|
+
yield channel.prefetch(this.options.prefetch);
|
|
32
|
+
const channelWrapper = {
|
|
33
|
+
isConnected: true,
|
|
34
|
+
callback,
|
|
35
|
+
messagesInFlight: 0,
|
|
36
|
+
};
|
|
37
|
+
this.channel.once('close', () => {
|
|
38
|
+
debug(`channel-closed queue=%s`, queueName);
|
|
39
|
+
channelWrapper.isConnected = false;
|
|
40
|
+
this.channelCloseCallback();
|
|
41
|
+
});
|
|
42
|
+
channelWrapper.amqpConsumer = yield channel.consume(queueName, this.messageReceiver.bind(this, callback, shouldAck, channel, channelWrapper, queueName), Object.assign(Object.assign({}, this.options.consumeOptions), { noAck: !shouldAck }));
|
|
43
|
+
return channelWrapper;
|
|
44
|
+
}))();
|
|
45
|
+
yield this.consumer;
|
|
46
|
+
return this;
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
messageReceiver(callback, shouldAck, originalChannel, consumerWrapper, queueName, msg) {
|
|
50
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
51
|
+
var _a;
|
|
52
|
+
if (!msg) {
|
|
53
|
+
debug('receive-empty-message queue=%s', queueName);
|
|
54
|
+
yield this.close();
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
try {
|
|
58
|
+
consumerWrapper.messagesInFlight++;
|
|
59
|
+
debug('receive-message queue=%s in-flight=%d', queueName, consumerWrapper.messagesInFlight);
|
|
60
|
+
const { content } = msg;
|
|
61
|
+
const parsed = yield this.options.parseMessageFn(content);
|
|
62
|
+
yield callback({
|
|
63
|
+
message: parsed,
|
|
64
|
+
rabbitMessage: msg,
|
|
65
|
+
channel: this.channel,
|
|
66
|
+
});
|
|
67
|
+
if (consumerWrapper.isConnected && shouldAck)
|
|
68
|
+
yield originalChannel.ack(msg);
|
|
69
|
+
}
|
|
70
|
+
catch (error) {
|
|
71
|
+
debug('message-handling-failed queue=%s error=%s strategy=%s', queueName, errToMessage(error), this.options.failureStrategy);
|
|
72
|
+
this.emit('handlingFailed', error);
|
|
73
|
+
if (!consumerWrapper.isConnected)
|
|
74
|
+
return;
|
|
75
|
+
switch (this.options.failureStrategy) {
|
|
76
|
+
case ConsumptionFailureStrategy.Drop:
|
|
77
|
+
if (shouldAck)
|
|
78
|
+
yield originalChannel.ack(msg);
|
|
79
|
+
return;
|
|
80
|
+
case ConsumptionFailureStrategy.Reject:
|
|
81
|
+
yield originalChannel.nack(msg, false, false);
|
|
82
|
+
return;
|
|
83
|
+
case ConsumptionFailureStrategy.Requeue:
|
|
84
|
+
yield originalChannel.nack(msg, false, true);
|
|
85
|
+
return;
|
|
86
|
+
default:
|
|
87
|
+
this.emit('error', new Error(`Not supported failure strategy: ${this.options.failureStrategy}`));
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
finally {
|
|
91
|
+
consumerWrapper.messagesInFlight--;
|
|
92
|
+
debug('message-processed queue=%s in-flight=%d', queueName, consumerWrapper.messagesInFlight);
|
|
93
|
+
(_a = this.notifyMessageProcessed) === null || _a === void 0 ? void 0 : _a.call(this);
|
|
94
|
+
}
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
shouldAcknowledge() {
|
|
98
|
+
return (this.options.prefetch > 0 ||
|
|
99
|
+
this.options.failureStrategy !== ConsumptionFailureStrategy.Drop);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { EventEmitter } from 'events';
|
|
2
|
+
import { Queue } from '../queue';
|
|
3
|
+
import { Channel } from '../channel';
|
|
4
|
+
import { ConsumerCallbackFn } from './types';
|
|
5
|
+
import { BaseConsumerEventMap } from './base-consumer';
|
|
6
|
+
export declare const RECONNECT_TIMEOUT = 100;
|
|
7
|
+
/**
|
|
8
|
+
* Events emitted by a {@link Consumer}.
|
|
9
|
+
*/
|
|
10
|
+
export type ConsumerEventMap = BaseConsumerEventMap & {
|
|
11
|
+
/**
|
|
12
|
+
* Emitted when the message handler throws an error.
|
|
13
|
+
*/
|
|
14
|
+
handlingFailed: [error: unknown];
|
|
15
|
+
};
|
|
16
|
+
/**
|
|
17
|
+
* Represents a service consuming messages from RabbitMQ.
|
|
18
|
+
* The generic type `Message` is the type of the parsed message payload,
|
|
19
|
+
* and `AdditionalProperties` are extra fields that a consumer implementation can add to the callback arguments.
|
|
20
|
+
*/
|
|
21
|
+
export interface Consumer<Message, AdditionalProperties = Record<string, unknown>> extends EventEmitter<ConsumerEventMap> {
|
|
22
|
+
/**
|
|
23
|
+
* Stops consuming new messages and waits for all in-flight message handlers to finish.
|
|
24
|
+
* @param timeout - Maximum time in milliseconds to wait for in-flight messages to complete. Defaults to 30000.
|
|
25
|
+
*/
|
|
26
|
+
close(timeout?: number): Promise<void>;
|
|
27
|
+
/**
|
|
28
|
+
* Registers the message handler and starts consuming from the queue.
|
|
29
|
+
* @param callback - Handler invoked for each received message.
|
|
30
|
+
*/
|
|
31
|
+
listen(callback: ConsumerCallbackFn<Message, AdditionalProperties>): Promise<Consumer<Message, AdditionalProperties>>;
|
|
32
|
+
/**
|
|
33
|
+
* The queue this consumer reads from.
|
|
34
|
+
*/
|
|
35
|
+
get queue(): Queue;
|
|
36
|
+
/**
|
|
37
|
+
* The channel used by this consumer.
|
|
38
|
+
*/
|
|
39
|
+
get channel(): Channel;
|
|
40
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const RECONNECT_TIMEOUT = 100;
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export { Consumer } from './consumer';
|
|
2
|
+
export { BatchConsumer } from './batch-consumer';
|
|
3
|
+
export { ConsumerImplementation } from './consumer-implementation';
|
|
4
|
+
export { BatchConsumerImplementation } from './batch-consumer-implementation';
|
|
5
|
+
export { ConsumptionFailureStrategy, ConsumerOptions, ConsumerCallbackFn, BatchConsumerCallbackFn, BatchConsumerOptions, BatchConsumerCallbackArgs, BatchFailureStrategy, } from './types';
|
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
import * as amqp from 'amqplib';
|
|
2
|
+
import { Channel } from '../channel';
|
|
3
|
+
import { MaybePromise } from '../types';
|
|
4
|
+
/**
|
|
5
|
+
* How to handle Rabbit message when the processing fails (throws error).
|
|
6
|
+
*/
|
|
7
|
+
export declare enum ConsumptionFailureStrategy {
|
|
8
|
+
/**
|
|
9
|
+
* Acknowledges the message as successfully processed even when the handler throws.
|
|
10
|
+
*/
|
|
11
|
+
Drop = "Drop",
|
|
12
|
+
/**
|
|
13
|
+
* Rejects the message and returns it to the queue, so a different consumer can process it again.
|
|
14
|
+
*/
|
|
15
|
+
Requeue = "Requeue",
|
|
16
|
+
/**
|
|
17
|
+
* Message is rejected and moved into dead letter queue if configured on the consuming queue.
|
|
18
|
+
*/
|
|
19
|
+
Reject = "Reject"
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* How to handle messages batch when processing fails (throw error).
|
|
23
|
+
*/
|
|
24
|
+
export declare enum BatchFailureStrategy {
|
|
25
|
+
/**
|
|
26
|
+
* Reject the whole batch; the consumer will not try to reprocess the messages.
|
|
27
|
+
* Each message in the batch is processed according to the `failureStrategy` property.
|
|
28
|
+
*/
|
|
29
|
+
Fail = "Fail",
|
|
30
|
+
/**
|
|
31
|
+
* Split the batch into individual messages and try to process each message separately.
|
|
32
|
+
* The listener will be called for each message, where the batch passed into the listener
|
|
33
|
+
* consists only of the single message.
|
|
34
|
+
*
|
|
35
|
+
* If the processing of the message fails again, it is handled according to the `failureStrategy` property.
|
|
36
|
+
*
|
|
37
|
+
* Split strategy is not supported when the batch size is equal to 1.
|
|
38
|
+
*/
|
|
39
|
+
Split = "Split"
|
|
40
|
+
}
|
|
41
|
+
export declare enum BatchState {
|
|
42
|
+
WaitingForData = "WaitingForData",
|
|
43
|
+
Processing = "Processing",
|
|
44
|
+
Failed = "Failed",
|
|
45
|
+
Processed = "Processed",
|
|
46
|
+
Acknowledging = "Acknowledging",
|
|
47
|
+
Acknowledged = "Acknowledged"
|
|
48
|
+
}
|
|
49
|
+
export type ConsumerCallbackArgs<Message, AdditionalProperties = Record<string, unknown>> = {
|
|
50
|
+
channel: Channel;
|
|
51
|
+
message: Message;
|
|
52
|
+
rabbitMessage: amqp.Message;
|
|
53
|
+
} & AdditionalProperties;
|
|
54
|
+
/**
|
|
55
|
+
* Arguments passed to a batch consumer callback.
|
|
56
|
+
* Contains the channel and an array of message entries, each holding the parsed payload,
|
|
57
|
+
* the raw amqplib message, and any additional properties added by the consumer implementation.
|
|
58
|
+
*/
|
|
59
|
+
export type BatchConsumerCallbackArgs<Message, AdditionalProperties = Record<string, unknown>> = {
|
|
60
|
+
channel: Channel;
|
|
61
|
+
messages: Array<{
|
|
62
|
+
message: Message;
|
|
63
|
+
rabbitMessage: amqp.Message;
|
|
64
|
+
} & AdditionalProperties>;
|
|
65
|
+
};
|
|
66
|
+
export type ConsumerCallbackFn<Message, AdditionalProperties = Record<string, unknown>> = (args: ConsumerCallbackArgs<Message, AdditionalProperties>) => MaybePromise<void>;
|
|
67
|
+
/**
|
|
68
|
+
* Callback function type for a batch consumer handler.
|
|
69
|
+
* Receives a batch of messages along with their raw amqplib payloads and the channel.
|
|
70
|
+
*/
|
|
71
|
+
export type BatchConsumerCallbackFn<Message, AdditionalProperties = Record<string, unknown>> = (args: BatchConsumerCallbackArgs<Message, AdditionalProperties>) => MaybePromise<void>;
|
|
72
|
+
export type ConsumerOptions<T> = {
|
|
73
|
+
/**
|
|
74
|
+
* How to handle a RabbitMQ message when the handler throws an error.
|
|
75
|
+
* Options are:
|
|
76
|
+
* - Drop: acknowledges the message as successfully processed even when the handler throws.
|
|
77
|
+
* - Requeue: rejects the message and returns it to the queue so a different consumer can process it.
|
|
78
|
+
* - Reject: rejects the message and moves it to the dead letter queue if one is configured.
|
|
79
|
+
*/
|
|
80
|
+
failureStrategy?: ConsumptionFailureStrategy;
|
|
81
|
+
/**
|
|
82
|
+
* Function that parses the message received by Rabbit.
|
|
83
|
+
* By default it calls JSON.parse, but you may provide your own function to parse message in XML or Proto format.
|
|
84
|
+
* @param payload content of the rabbit message.
|
|
85
|
+
*/
|
|
86
|
+
parseMessageFn?(payload: Buffer): MaybePromise<T>;
|
|
87
|
+
/**
|
|
88
|
+
* Options passed down into `amqplib.channel.consume` function.
|
|
89
|
+
* Auto acknowledgement is controlled by the `failureStrategy` property instead.
|
|
90
|
+
*/
|
|
91
|
+
consumeOptions?: Omit<amqp.Options.Consume, 'noAck'>;
|
|
92
|
+
/**
|
|
93
|
+
* How many messages the RabbitMQ broker can send to the consumer in parallel.
|
|
94
|
+
* This effectively controls the concurrency of the consumer.
|
|
95
|
+
* Defaults to 0, which means the broker can send as many messages as it wants.
|
|
96
|
+
*
|
|
97
|
+
* Warning: prefetch is configured on the channel. It is recommended to use a separate channel
|
|
98
|
+
* per consumer so that different prefetch values do not interfere with each other.
|
|
99
|
+
*/
|
|
100
|
+
prefetch?: number;
|
|
101
|
+
/**
|
|
102
|
+
* Channel to use for consuming. If `null` (default), a new dedicated channel is created.
|
|
103
|
+
*/
|
|
104
|
+
channel?: Channel | null;
|
|
105
|
+
};
|
|
106
|
+
export type BatchConsumerOptions<T> = ConsumerOptions<T> & {
|
|
107
|
+
/**
|
|
108
|
+
* How many messages are in single batch. Consumer can process multiple batches in parallel,
|
|
109
|
+
* when prefetch is higher than the batch size.
|
|
110
|
+
*
|
|
111
|
+
* The batch can be smaller, for example when not enough messages were received during `maxWaitTimeForBatch`,
|
|
112
|
+
* or when `batchFailureStrategy` is set to `Split`.
|
|
113
|
+
*
|
|
114
|
+
* If not specified, prefetch size will be used as a batch size.
|
|
115
|
+
* If either batch size nor prefetch is specified, the default value is 20.
|
|
116
|
+
*/
|
|
117
|
+
batchSize?: number;
|
|
118
|
+
/**
|
|
119
|
+
* Time in milliseconds to wait before process incomplete batch.
|
|
120
|
+
*
|
|
121
|
+
* When consumer doesn't have enough messages in buffer, it will wait
|
|
122
|
+
* specified number of milliseconds for new messages to arrive. If after
|
|
123
|
+
* the time there is still not enough messages, the consumer will invoke
|
|
124
|
+
* the listener with smaller batch.
|
|
125
|
+
*
|
|
126
|
+
* Effectively, this property controls for how long the message can be in buffer
|
|
127
|
+
* before the consumer invokes listener for it.
|
|
128
|
+
*
|
|
129
|
+
* By default is 100ms.
|
|
130
|
+
*/
|
|
131
|
+
maxWaitTimeForBatch?: number;
|
|
132
|
+
/**
|
|
133
|
+
* After batch is successfully processed, and `failureStrategy` is not set to `Reject`,
|
|
134
|
+
* consumer needs to confirm messages to the RabbitMQ broker. If there is different batch,
|
|
135
|
+
* that has older messages than the current one, consumer will wait some time before
|
|
136
|
+
* acknowledge the messages (so the batch with older messages have some time to finish).
|
|
137
|
+
*
|
|
138
|
+
* Reasoning behind is that Rabbit allows to either acknowledge each message separately, or
|
|
139
|
+
* acknowledge sequential set of messages using single call (faster). When batch, that is not
|
|
140
|
+
* the oldest one, finish processing, this timeout allows it to wait for the oldest batch
|
|
141
|
+
* to complete, allowing consumer to acknowledge all the messages (both from the older batch,
|
|
142
|
+
* as well for the current) using single acknowledgement call.
|
|
143
|
+
*
|
|
144
|
+
* When the processed job is the oldest, this property doesn't delay the acknowledgement and the
|
|
145
|
+
* consumer acknowledge the whole batch right away using single call.
|
|
146
|
+
*
|
|
147
|
+
* If the older batches are not processed until the specified time, consumer acknowledge
|
|
148
|
+
* each message separately.
|
|
149
|
+
*
|
|
150
|
+
* The property allows to wait for acknowledgements only, rejections are send to the RabbitMQ
|
|
151
|
+
* broker immediately and there is currently no way to control it.
|
|
152
|
+
*
|
|
153
|
+
* This property effectively controls how long the message can wait before is acknowledge,
|
|
154
|
+
* but after it is processed by listener.
|
|
155
|
+
*
|
|
156
|
+
* The value is specified in milliseconds. Default value is 0, meaning the consumer
|
|
157
|
+
* doesn't wait for older batches to finish processing.
|
|
158
|
+
*/
|
|
159
|
+
maxWaitTimeForAck?: number;
|
|
160
|
+
/**
|
|
161
|
+
* How to handle a batch of messages when processing fails (throws error).
|
|
162
|
+
* Options are:
|
|
163
|
+
* - Fail: reject the whole batch; the consumer will not try to reprocess the messages.
|
|
164
|
+
* - Split: split the batch into individual messages and try to process each one separately.
|
|
165
|
+
*/
|
|
166
|
+
batchFailureStrategy?: BatchFailureStrategy;
|
|
167
|
+
};
|
|
168
|
+
export declare const DEFAULT_CONSUMER_OPTIONS: {
|
|
169
|
+
readonly failureStrategy: ConsumptionFailureStrategy.Reject;
|
|
170
|
+
readonly parseMessageFn: (payload: Buffer) => any;
|
|
171
|
+
readonly consumeOptions: {};
|
|
172
|
+
readonly prefetch: 0;
|
|
173
|
+
readonly batchSize: -1;
|
|
174
|
+
readonly maxWaitTimeForBatch: 100;
|
|
175
|
+
readonly maxWaitTimeForAck: 0;
|
|
176
|
+
readonly batchFailureStrategy: BatchFailureStrategy.Fail;
|
|
177
|
+
readonly channel: null;
|
|
178
|
+
};
|
|
179
|
+
export type BatchRecord = {
|
|
180
|
+
messages: Array<{
|
|
181
|
+
rabbitMessage: amqp.Message;
|
|
182
|
+
}>;
|
|
183
|
+
state: BatchState;
|
|
184
|
+
confirmTimer?: NodeJS.Timeout;
|
|
185
|
+
};
|
|
186
|
+
export type ConsumerWrapper<Callback> = {
|
|
187
|
+
callback: Callback;
|
|
188
|
+
amqpConsumer: amqp.Replies.Consume;
|
|
189
|
+
messagesInFlight: number;
|
|
190
|
+
isConnected: boolean;
|
|
191
|
+
};
|
|
192
|
+
export type BatchConsumerWrapper<Callback> = ConsumerWrapper<Callback> & {
|
|
193
|
+
batches: BatchRecord[];
|
|
194
|
+
};
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
export var ConsumptionFailureStrategy;
|
|
2
|
+
(function (ConsumptionFailureStrategy) {
|
|
3
|
+
ConsumptionFailureStrategy["Drop"] = "Drop";
|
|
4
|
+
ConsumptionFailureStrategy["Requeue"] = "Requeue";
|
|
5
|
+
ConsumptionFailureStrategy["Reject"] = "Reject";
|
|
6
|
+
})(ConsumptionFailureStrategy || (ConsumptionFailureStrategy = {}));
|
|
7
|
+
export var BatchFailureStrategy;
|
|
8
|
+
(function (BatchFailureStrategy) {
|
|
9
|
+
BatchFailureStrategy["Fail"] = "Fail";
|
|
10
|
+
BatchFailureStrategy["Split"] = "Split";
|
|
11
|
+
})(BatchFailureStrategy || (BatchFailureStrategy = {}));
|
|
12
|
+
export var BatchState;
|
|
13
|
+
(function (BatchState) {
|
|
14
|
+
BatchState["WaitingForData"] = "WaitingForData";
|
|
15
|
+
BatchState["Processing"] = "Processing";
|
|
16
|
+
BatchState["Failed"] = "Failed";
|
|
17
|
+
BatchState["Processed"] = "Processed";
|
|
18
|
+
BatchState["Acknowledging"] = "Acknowledging";
|
|
19
|
+
BatchState["Acknowledged"] = "Acknowledged";
|
|
20
|
+
})(BatchState || (BatchState = {}));
|
|
21
|
+
export const DEFAULT_CONSUMER_OPTIONS = {
|
|
22
|
+
failureStrategy: ConsumptionFailureStrategy.Reject,
|
|
23
|
+
parseMessageFn: (payload) => JSON.parse(payload.toString()),
|
|
24
|
+
consumeOptions: {},
|
|
25
|
+
prefetch: 0,
|
|
26
|
+
batchSize: -1,
|
|
27
|
+
maxWaitTimeForBatch: 100,
|
|
28
|
+
maxWaitTimeForAck: 0,
|
|
29
|
+
batchFailureStrategy: BatchFailureStrategy.Fail,
|
|
30
|
+
channel: null,
|
|
31
|
+
};
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Thrown when a retryable operation exhausts all retry attempts
|
|
3
|
+
* as defined by the configured {@link RetryStrategy}.
|
|
4
|
+
*/
|
|
5
|
+
export declare class TooManyRetriesError extends Error {
|
|
6
|
+
/**
|
|
7
|
+
* The last error thrown by the retried operation before retries were exhausted.
|
|
8
|
+
*/
|
|
9
|
+
readonly cause: unknown;
|
|
10
|
+
constructor(message: string,
|
|
11
|
+
/**
|
|
12
|
+
* The last error thrown by the retried operation before retries were exhausted.
|
|
13
|
+
*/
|
|
14
|
+
cause: unknown);
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Thrown when the channel's internal write buffer does not drain
|
|
18
|
+
* within the configured drain timeout.
|
|
19
|
+
* The underlying amqplib channel is closed when this error is thrown.
|
|
20
|
+
*/
|
|
21
|
+
export declare class DrainError extends Error {
|
|
22
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { Channel } from '../channel';
|
|
2
|
+
import { ExchangeImplementation } from './exchange-implementation';
|
|
3
|
+
/**
|
|
4
|
+
* Class representing the default, direct exchange created by the RabbitMQ broker.
|
|
5
|
+
* Every queue is automatically bound to this exchange under a routing key equal to the queue name.
|
|
6
|
+
* The exchange cannot be declared or removed; broker interaction is skipped entirely.
|
|
7
|
+
*/
|
|
8
|
+
export declare class DefaultExchange extends ExchangeImplementation {
|
|
9
|
+
constructor(channel: Channel);
|
|
10
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { AssertionMode } from '../types';
|
|
2
|
+
import { ExchangeImplementation } from './exchange-implementation';
|
|
3
|
+
export class DefaultExchange extends ExchangeImplementation {
|
|
4
|
+
constructor(channel) {
|
|
5
|
+
super(channel, '', 'direct', {
|
|
6
|
+
durable: true,
|
|
7
|
+
assertionMode: AssertionMode.Passive,
|
|
8
|
+
});
|
|
9
|
+
}
|
|
10
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { Channel } from '../channel';
|
|
2
|
+
import { Queue } from '../queue';
|
|
3
|
+
import { ProducerOptions, Producer } from '../producer';
|
|
4
|
+
import { BindingArgs, ExchangeConsumerQueueOptions, ExchangeConsumerOptions, ExchangeBatchConsumerOptions, ExchangeTypes, ExchangeOptions } from './types';
|
|
5
|
+
import { Exchange } from './exchange';
|
|
6
|
+
export declare class ExchangeImplementation implements Exchange {
|
|
7
|
+
protected readonly channel: Channel;
|
|
8
|
+
private readonly exchangeName;
|
|
9
|
+
private readonly exchangeType;
|
|
10
|
+
private readonly options?;
|
|
11
|
+
private assertPromise;
|
|
12
|
+
private bindings;
|
|
13
|
+
constructor(channel: Channel, exchangeName: string, exchangeType: ExchangeTypes, options?: ExchangeOptions);
|
|
14
|
+
assert(): Promise<this>;
|
|
15
|
+
name(): Promise<string>;
|
|
16
|
+
bindQueue(queue: Queue, pattern: string, args?: BindingArgs): Promise<this>;
|
|
17
|
+
bindExchange(exchange: Exchange, pattern: string, args?: BindingArgs): Promise<this>;
|
|
18
|
+
private rebind;
|
|
19
|
+
createConsumer<T>(options: ExchangeConsumerOptions<T>, queueOptions?: ExchangeConsumerQueueOptions): Promise<import("..").Consumer<T, Record<string, unknown>>>;
|
|
20
|
+
createBatchConsumer<T>(options: ExchangeBatchConsumerOptions<T>, queueOptions?: ExchangeConsumerQueueOptions): Promise<import("..").BatchConsumer<T, Record<string, unknown>>>;
|
|
21
|
+
createProducer<T>(options?: ProducerOptions<T> & {
|
|
22
|
+
isConfirmed?: boolean;
|
|
23
|
+
}): Promise<Producer<T>>;
|
|
24
|
+
}
|
|
@@ -0,0 +1,152 @@
|
|
|
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
|
+
import { debuglog } from 'util';
|
|
11
|
+
import stableJsonStringify from 'safe-stable-stringify';
|
|
12
|
+
import { ProducerImplementation } from '../producer';
|
|
13
|
+
import { deepMerge, errToMessage, LIB_NAME } from '../utils';
|
|
14
|
+
import { AssertionMode } from '../types';
|
|
15
|
+
import { BindingType } from './types';
|
|
16
|
+
const debug = debuglog(`${LIB_NAME}:exchange`);
|
|
17
|
+
const DEFAULT_EXCHANGE_OPTIONS = {
|
|
18
|
+
assertionMode: AssertionMode.Assert,
|
|
19
|
+
};
|
|
20
|
+
export class ExchangeImplementation {
|
|
21
|
+
constructor(channel, exchangeName, exchangeType, options) {
|
|
22
|
+
this.channel = channel;
|
|
23
|
+
this.exchangeName = exchangeName;
|
|
24
|
+
this.exchangeType = exchangeType;
|
|
25
|
+
this.assertPromise = null;
|
|
26
|
+
this.bindings = [];
|
|
27
|
+
this.options = deepMerge({}, DEFAULT_EXCHANGE_OPTIONS, options !== null && options !== void 0 ? options : {});
|
|
28
|
+
this.channel.on('close', () => {
|
|
29
|
+
debug('native-close');
|
|
30
|
+
this.assertPromise = null;
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
assert() {
|
|
34
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
35
|
+
if (this.assertPromise) {
|
|
36
|
+
yield this.assertPromise;
|
|
37
|
+
return this;
|
|
38
|
+
}
|
|
39
|
+
this.assertPromise = (() => __awaiter(this, void 0, void 0, function* () {
|
|
40
|
+
var _a, _b, _c;
|
|
41
|
+
debug('assert name=%s type=%s mode=%s', this.exchangeName, this.exchangeType, (_a = this.options) === null || _a === void 0 ? void 0 : _a.assertionMode);
|
|
42
|
+
const channel = yield this.channel.native();
|
|
43
|
+
switch ((_b = this.options) === null || _b === void 0 ? void 0 : _b.assertionMode) {
|
|
44
|
+
case AssertionMode.Check:
|
|
45
|
+
yield channel.checkExchange(this.exchangeName);
|
|
46
|
+
debug('native-check-done name=%s', this.exchangeName);
|
|
47
|
+
break;
|
|
48
|
+
case AssertionMode.Assert:
|
|
49
|
+
yield channel.assertExchange(this.exchangeName, this.exchangeType, this.options);
|
|
50
|
+
debug('native-assert-done name=%s type=%s', this.exchangeName, this.exchangeType);
|
|
51
|
+
break;
|
|
52
|
+
case AssertionMode.Passive:
|
|
53
|
+
debug('no-assert name=%s mode=%s', this.exchangeName, this.options.assertionMode);
|
|
54
|
+
break;
|
|
55
|
+
default:
|
|
56
|
+
throw new Error(`Unknown assertion mode: ${(_c = this.options) === null || _c === void 0 ? void 0 : _c.assertionMode}`);
|
|
57
|
+
}
|
|
58
|
+
}))();
|
|
59
|
+
yield this.assertPromise;
|
|
60
|
+
yield this.rebind();
|
|
61
|
+
debug('assert-complete name=%s', this.exchangeName);
|
|
62
|
+
return this;
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
name() {
|
|
66
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
67
|
+
return this.assert().then(self => self.exchangeName);
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
bindQueue(queue, pattern, args) {
|
|
71
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
72
|
+
const [thisName, queueName] = yield Promise.all([
|
|
73
|
+
this.name(),
|
|
74
|
+
queue.name(),
|
|
75
|
+
]);
|
|
76
|
+
debug('bind-queue exchange=%s queue=%s pattern=%s', thisName, queueName, pattern);
|
|
77
|
+
const doesBindingMatch = (binding) => binding.type === BindingType.queue &&
|
|
78
|
+
binding.pattern === pattern &&
|
|
79
|
+
binding.queue === queue &&
|
|
80
|
+
stableJsonStringify(binding.args) === stableJsonStringify(args);
|
|
81
|
+
if (this.bindings.every(binding => !doesBindingMatch(binding))) {
|
|
82
|
+
this.bindings.push({
|
|
83
|
+
args,
|
|
84
|
+
pattern,
|
|
85
|
+
type: BindingType.queue,
|
|
86
|
+
queue,
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
const channel = yield this.channel.native();
|
|
90
|
+
yield channel.bindQueue(queueName, thisName, pattern, args);
|
|
91
|
+
debug('bind-queue-done exchange=%s queue=%s pattern=%s', thisName, queueName, pattern);
|
|
92
|
+
return this;
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
bindExchange(exchange, pattern, args) {
|
|
96
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
97
|
+
const [thisName, exchangeName] = yield Promise.all([
|
|
98
|
+
this.name(),
|
|
99
|
+
exchange.name(),
|
|
100
|
+
]);
|
|
101
|
+
debug('bind-exchange dest=%s source=%s pattern=%s', thisName, exchangeName, pattern);
|
|
102
|
+
const doesBindingMatch = (binding) => binding.type === BindingType.exchange &&
|
|
103
|
+
binding.pattern === pattern &&
|
|
104
|
+
binding.exchange === exchange &&
|
|
105
|
+
stableJsonStringify(binding.args) === stableJsonStringify(args);
|
|
106
|
+
if (this.bindings.every(binding => !doesBindingMatch(binding))) {
|
|
107
|
+
this.bindings.push({
|
|
108
|
+
args,
|
|
109
|
+
pattern,
|
|
110
|
+
type: BindingType.exchange,
|
|
111
|
+
exchange,
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
const channel = yield this.channel.native();
|
|
115
|
+
yield channel.bindExchange(exchangeName, thisName, pattern, args);
|
|
116
|
+
debug('bind-exchange-done dest=%s source=%s pattern=%s', thisName, exchangeName, pattern);
|
|
117
|
+
return this;
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
rebind() {
|
|
121
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
122
|
+
debug('rebind name=%s bindings=%d', this.exchangeName, this.bindings.length);
|
|
123
|
+
const promises = yield Promise.allSettled(this.bindings.map(binding => (binding.type === BindingType.queue
|
|
124
|
+
? this.bindQueue(binding.queue, binding.pattern, binding.args)
|
|
125
|
+
: this.bindExchange(binding.exchange, binding.pattern, binding.args))));
|
|
126
|
+
const failedPromise = promises.find(p => p.status === 'rejected');
|
|
127
|
+
if (failedPromise) {
|
|
128
|
+
debug('rebind-failed error=%s', errToMessage(failedPromise.reason));
|
|
129
|
+
throw failedPromise.reason;
|
|
130
|
+
}
|
|
131
|
+
debug('rebind-complete name=%s', this.exchangeName);
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
createConsumer(options_1) {
|
|
135
|
+
return __awaiter(this, arguments, void 0, function* (options, queueOptions = {}) {
|
|
136
|
+
const queue = yield this.channel.createQueue('', Object.assign(Object.assign({}, queueOptions), { durable: false, exclusive: true }));
|
|
137
|
+
yield this.bindQueue(queue, options.pattern, options.bindingArgs);
|
|
138
|
+
return queue.createConsumer(options);
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
createBatchConsumer(options_1) {
|
|
142
|
+
return __awaiter(this, arguments, void 0, function* (options, queueOptions = {}) {
|
|
143
|
+
const queue = yield this.channel.createQueue('', Object.assign(Object.assign({}, queueOptions), { durable: false, exclusive: true }));
|
|
144
|
+
yield this.bindQueue(queue, options.pattern, options.bindingArgs);
|
|
145
|
+
return queue.createBatchConsumer(options);
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
createProducer(options = {}) {
|
|
149
|
+
var _a;
|
|
150
|
+
return Promise.resolve(new ProducerImplementation((_a = options.channel) !== null && _a !== void 0 ? _a : this.channel.connection.createChannel(options.isConfirmed), this, options));
|
|
151
|
+
}
|
|
152
|
+
}
|