@outbox-event-bus/rabbitmq-publisher 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/README.md ADDED
@@ -0,0 +1,152 @@
1
+ # RabbitMQ Publisher
2
+
3
+ ![npm version](https://img.shields.io/npm/v/@outbox-event-bus/rabbitmq-publisher?style=flat-square&color=2563eb)
4
+ ![license](https://img.shields.io/npm/l/@outbox-event-bus/rabbitmq-publisher?style=flat-square&color=2563eb)
5
+
6
+ > **Enterprise-Grade Message Routing**
7
+
8
+ RabbitMQ (AMQP) publisher for [outbox-event-bus](https://github.com/dunika/outbox-event-bus#readme). Forwards events to RabbitMQ exchanges with intelligent routing and configurable retries.
9
+
10
+ ```typescript
11
+ import { RabbitMQPublisher } from '@outbox-event-bus/rabbitmq-publisher';
12
+
13
+ const publisher = new RabbitMQPublisher(bus, {
14
+ channel: amqpChannel,
15
+ exchange: 'events-exchange',
16
+ routingKey: 'my-app.events' // Optional
17
+ });
18
+
19
+ publisher.subscribe(['user.created']);
20
+ ```
21
+
22
+ ## When to Use
23
+
24
+ **Choose RabbitMQ Publisher when:**
25
+ - You need **complex routing** (fanout, topic, direct).
26
+ - You require **message prioritization**.
27
+ - You want to use **standard AMQP** protocols.
28
+ - You need to integrate with heterogeneous systems that support RabbitMQ.
29
+
30
+ **Consider alternatives when:**
31
+ - You want a **fully managed serverless service** (use AWS SNS/EventBridge).
32
+ - You need **distributed log streaming** (use Kafka).
33
+ - You want the **simplest possible setup** (use Redis Streams).
34
+
35
+ ## Installation
36
+
37
+ ```bash
38
+ npm install @outbox-event-bus/rabbitmq-publisher amqplib
39
+ ```
40
+
41
+ ## Configuration
42
+
43
+ ### RabbitMQPublisherConfig
44
+
45
+ ```typescript
46
+ interface RabbitMQPublisherConfig {
47
+ channel: Channel; // amqplib Channel instance
48
+ exchange: string; // Target exchange name
49
+ routingKey?: string; // Optional static routing key
50
+ processingConfig?: {
51
+ bufferSize?: number; // Default: 50
52
+ bufferTimeoutMs?: number; // Default: 100
53
+ concurrency?: number; // Default: 5
54
+ maxBatchSize?: number; // Optional downstream batch limit
55
+ };
56
+ retryConfig?: {
57
+ maxAttempts?: number; // Default: 3
58
+ initialDelayMs?: number; // Default: 1000
59
+ maxDelayMs?: number; // Default: 10000
60
+ };
61
+ }
62
+ ```
63
+
64
+ ## Batching & Buffering
65
+
66
+ This publisher has **buffering enabled by default** (50 items or 100ms). This safe default ensures efficient use of the AMQP channel while maintaining low latency.
67
+
68
+ To tune buffering, adjust the `processingConfig`:
69
+ ```typescript
70
+ const publisher = new RabbitMQPublisher(bus, {
71
+ // ...
72
+ processingConfig: {
73
+ bufferSize: 100,
74
+ bufferTimeoutMs: 50
75
+ }
76
+ });
77
+ ```
78
+
79
+ ## Usage
80
+
81
+ ### Basic Setup
82
+
83
+ ```typescript
84
+ import { RabbitMQPublisher } from '@outbox-event-bus/rabbitmq-publisher';
85
+
86
+ const publisher = new RabbitMQPublisher(bus, {
87
+ channel: myChannel,
88
+ exchange: 'app-events'
89
+ });
90
+
91
+ publisher.subscribe(['*']);
92
+ ```
93
+
94
+ ### Dynamic Routing
95
+ If `routingKey` is not provided, the **event type** is used as the routing key automatically.
96
+
97
+ ### Configuration Options
98
+
99
+ - `channel`: An instance of the `amqplib` `Channel`.
100
+ - `exchange`: The exchange to publish to.
101
+ - `routingKey`: (Optional) The routing key. Default: uses `event.type`.
102
+ - `processingConfig`: (Optional) Settings for accumulation and batching.
103
+ - `bufferSize`: Number of events to accumulate in memory before publishing. Default: `50`.
104
+ - `bufferTimeoutMs`: Maximum time to wait for a buffer to fill before flushing. Default: `100ms`.
105
+ - `concurrency`: Maximum number of concurrent processing tasks. Default: `5`.
106
+ - `maxBatchSize`: (Optional) If set, the accumulated buffer will be split into smaller downstream batches.
107
+ - `retryConfig`: (Optional) Custom retry settings for publishing failures.
108
+ - `maxAttempts`: Maximum number of publication attempts. Default: `3`.
109
+ - `initialDelayMs`: Initial backoff delay in milliseconds. Default: `1000ms`.
110
+ - `maxDelayMs`: Maximum backoff delay in milliseconds. Default: `10000ms`.
111
+
112
+ > [!NOTE]
113
+ > RabbitMQ publishers are often used with smaller buffers for lower latency, but larger buffers can significantly improve throughput for high-volume event streams.
114
+
115
+ ## Message Format
116
+
117
+ Messages are published as JSON buffers:
118
+
119
+ | AMQP Field | Value | Description |
120
+ |------------|-------|-------------|
121
+ | **Body** | `JSON.stringify(event)` | The full event object. |
122
+ | **Routing Key** | `config.routingKey` or `event.type` | Used for routing to queues. |
123
+ | **Headers** | `eventType`, `eventId` | Metadata headers. |
124
+
125
+ ## Error Handling
126
+
127
+ ### Application-Level Retries
128
+ The RabbitMQ publisher implements **exponential backoff retries** to handle channel buffer saturation or temporary connection drops.
129
+
130
+ ```typescript
131
+ const publisher = new RabbitMQPublisher(bus, {
132
+ // ...
133
+ retryConfig: {
134
+ maxAttempts: 5,
135
+ initialDelayMs: 500
136
+ }
137
+ });
138
+ ```
139
+
140
+ ## Troubleshooting
141
+
142
+ ### `channel buffer full`
143
+ - **Cause**: Publishing faster than RabbitMQ can accept (backpressure).
144
+ - **Solution**: Check your RabbitMQ server load and memory. The publisher will retry, but persistent failure indicates an infrastructure bottleneck.
145
+
146
+ ### Unrouteable Messages
147
+ - **Cause**: Misconfigured exchange or routing keys.
148
+ - **Solution**: Ensure the exchange exists and queues are bound with appropriate keys matching the event type.
149
+
150
+ ## License
151
+
152
+ MIT © [Dunika](https://github.com/dunika)
package/dist/index.cjs ADDED
@@ -0,0 +1,34 @@
1
+ let outbox_event_bus = require("outbox-event-bus");
2
+
3
+ //#region src/rabbitmq-publisher.ts
4
+ var RabbitMQPublisher = class {
5
+ channel;
6
+ exchange;
7
+ routingKey;
8
+ publisher;
9
+ constructor(bus, config) {
10
+ this.channel = config.channel;
11
+ this.exchange = config.exchange;
12
+ this.routingKey = config.routingKey ?? "";
13
+ this.publisher = new outbox_event_bus.EventPublisher(bus, config);
14
+ }
15
+ subscribe(eventTypes) {
16
+ this.publisher.subscribe(eventTypes, async (events) => {
17
+ for (const event of events) {
18
+ const message = JSON.stringify(event);
19
+ const routingKey = this.routingKey || event.type;
20
+ if (!this.channel.publish(this.exchange, routingKey, Buffer.from(message), {
21
+ contentType: "application/json",
22
+ headers: {
23
+ eventType: event.type,
24
+ eventId: event.id
25
+ }
26
+ })) throw new outbox_event_bus.BackpressureError("RabbitMQ channel");
27
+ }
28
+ });
29
+ }
30
+ };
31
+
32
+ //#endregion
33
+ exports.RabbitMQPublisher = RabbitMQPublisher;
34
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.cjs","names":["EventPublisher","BackpressureError"],"sources":["../src/rabbitmq-publisher.ts"],"sourcesContent":["import type { Channel } from \"amqplib\"\nimport type { BusEvent, IOutboxEventBus, IPublisher, PublisherConfig } from \"outbox-event-bus\"\nimport { BackpressureError, EventPublisher } from \"outbox-event-bus\"\n\nexport interface RabbitMQPublisherConfig extends PublisherConfig {\n channel: Channel\n exchange: string\n routingKey?: string\n}\n\nexport class RabbitMQPublisher<TTransaction = unknown> implements IPublisher {\n private readonly channel: Channel\n private readonly exchange: string\n private readonly routingKey: string\n private readonly publisher: EventPublisher<TTransaction>\n\n constructor(bus: IOutboxEventBus<TTransaction>, config: RabbitMQPublisherConfig) {\n this.channel = config.channel\n this.exchange = config.exchange\n this.routingKey = config.routingKey ?? \"\"\n this.publisher = new EventPublisher(bus, config)\n }\n\n subscribe(eventTypes: string[]): void {\n this.publisher.subscribe(eventTypes, async (events: BusEvent[]) => {\n for (const event of events) {\n const message = JSON.stringify(event)\n const routingKey = this.routingKey || event.type\n\n const published = this.channel.publish(this.exchange, routingKey, Buffer.from(message), {\n contentType: \"application/json\",\n headers: {\n eventType: event.type,\n eventId: event.id,\n },\n })\n\n if (!published) {\n throw new BackpressureError(\"RabbitMQ channel\")\n }\n }\n })\n }\n}\n"],"mappings":";;;AAUA,IAAa,oBAAb,MAA6E;CAC3E,AAAiB;CACjB,AAAiB;CACjB,AAAiB;CACjB,AAAiB;CAEjB,YAAY,KAAoC,QAAiC;AAC/E,OAAK,UAAU,OAAO;AACtB,OAAK,WAAW,OAAO;AACvB,OAAK,aAAa,OAAO,cAAc;AACvC,OAAK,YAAY,IAAIA,gCAAe,KAAK,OAAO;;CAGlD,UAAU,YAA4B;AACpC,OAAK,UAAU,UAAU,YAAY,OAAO,WAAuB;AACjE,QAAK,MAAM,SAAS,QAAQ;IAC1B,MAAM,UAAU,KAAK,UAAU,MAAM;IACrC,MAAM,aAAa,KAAK,cAAc,MAAM;AAU5C,QAAI,CARc,KAAK,QAAQ,QAAQ,KAAK,UAAU,YAAY,OAAO,KAAK,QAAQ,EAAE;KACtF,aAAa;KACb,SAAS;MACP,WAAW,MAAM;MACjB,SAAS,MAAM;MAChB;KACF,CAAC,CAGA,OAAM,IAAIC,mCAAkB,mBAAmB;;IAGnD"}
@@ -0,0 +1,76 @@
1
+ import { Channel } from "amqplib";
2
+
3
+ //#region ../../core/src/types/types.d.ts
4
+ type PartialBy<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>;
5
+ type BusEvent<T extends string = string, P = unknown> = {
6
+ id: string;
7
+ type: T;
8
+ payload: P;
9
+ occurredAt: Date;
10
+ metadata?: Record<string, unknown>;
11
+ };
12
+ type FailedBusEvent<T extends string = string, P = unknown> = BusEvent<T, P> & {
13
+ error?: string;
14
+ retryCount: number;
15
+ lastAttemptAt?: Date;
16
+ };
17
+ type BusEventInput<T extends string = string, P = unknown> = PartialBy<BusEvent<T, P>, "id" | "occurredAt">;
18
+ type AnyListener = (...args: unknown[]) => unknown;
19
+ type RetryOptions = {
20
+ maxAttempts?: number;
21
+ initialDelayMs?: number;
22
+ maxDelayMs?: number;
23
+ };
24
+ type ProcessingOptions = {
25
+ bufferSize?: number;
26
+ bufferTimeoutMs?: number;
27
+ concurrency?: number;
28
+ maxBatchSize?: number;
29
+ };
30
+ type PublisherConfig = {
31
+ retryConfig?: RetryOptions;
32
+ processingConfig?: ProcessingOptions;
33
+ };
34
+ //#endregion
35
+ //#region ../../core/src/types/interfaces.d.ts
36
+ interface IOutboxEventBus<TTransaction> {
37
+ emit: <T extends string, P>(event: BusEventInput<T, P>, transaction?: TTransaction) => Promise<void>;
38
+ emitMany: <T extends string, P>(events: BusEventInput<T, P>[], transaction?: TTransaction) => Promise<void>;
39
+ on: <T extends string, P = unknown>(eventType: T, handler: (event: BusEvent<T, P>) => Promise<void>) => this;
40
+ addListener: <T extends string, P = unknown>(eventType: T, handler: (event: BusEvent<T, P>) => Promise<void>) => this;
41
+ off: <T extends string, P = unknown>(eventType: T, handler: (event: BusEvent<T, P>) => Promise<void>) => this;
42
+ removeListener: <T extends string, P = unknown>(eventType: T, handler: (event: BusEvent<T, P>) => Promise<void>) => this;
43
+ once: <T extends string, P = unknown>(eventType: T, handler: (event: BusEvent<T, P>) => Promise<void>) => this;
44
+ removeAllListeners: <T extends string>(eventType?: T) => this;
45
+ getSubscriptionCount: () => number;
46
+ listenerCount: (eventType: string) => number;
47
+ getListener: (eventType: string) => AnyListener | undefined;
48
+ eventNames: () => string[];
49
+ start: () => void;
50
+ stop: () => Promise<void>;
51
+ subscribe: <T extends string, P = unknown>(eventTypes: T[], handler: (event: BusEvent<T, P>) => Promise<void>) => this;
52
+ waitFor: <T extends string, P = unknown>(eventType: T, timeoutMs?: number) => Promise<BusEvent<T, P>>;
53
+ getFailedEvents: () => Promise<FailedBusEvent[]>;
54
+ retryEvents: (eventIds: string[]) => Promise<void>;
55
+ }
56
+ interface IPublisher {
57
+ subscribe(eventTypes: string[]): void;
58
+ }
59
+ //#endregion
60
+ //#region src/rabbitmq-publisher.d.ts
61
+ interface RabbitMQPublisherConfig extends PublisherConfig {
62
+ channel: Channel;
63
+ exchange: string;
64
+ routingKey?: string;
65
+ }
66
+ declare class RabbitMQPublisher<TTransaction = unknown> implements IPublisher {
67
+ private readonly channel;
68
+ private readonly exchange;
69
+ private readonly routingKey;
70
+ private readonly publisher;
71
+ constructor(bus: IOutboxEventBus<TTransaction>, config: RabbitMQPublisherConfig);
72
+ subscribe(eventTypes: string[]): void;
73
+ }
74
+ //#endregion
75
+ export { RabbitMQPublisher, RabbitMQPublisherConfig };
76
+ //# sourceMappingURL=index.d.cts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.cts","names":[],"sources":["../../../core/src/types/types.ts","../../../core/src/types/interfaces.ts","../src/rabbitmq-publisher.ts"],"sourcesContent":[],"mappings":";;;KAEK,6BAA6B,KAAK,KAAK,GAAG,KAAK,QAAQ,KAAK,GAAG;KAExD;EAFP,EAAA,EAAA,MAAA;EAA6B,IAAA,EAI1B,CAJ0B;EAAU,OAAA,EAKjC,CALiC;EAAG,UAAA,EAMjC,IANiC;EAAR,QAAA,CAAA,EAO1B,MAP0B,CAAA,MAAA,EAAA,OAAA,CAAA;CAA0B;AAAG,KAUxD,cAVwD,CAAA,UAAA,MAAA,GAAA,MAAA,EAAA,IAAA,OAAA,CAAA,GAUC,QAVD,CAUU,CAVV,EAUa,CAVb,CAAA,GAAA;EAAR,KAAA,CAAA,EAAA,MAAA;EAAR,UAAA,EAAA,MAAA;EAAO,aAAA,CAAA,EAazC,IAbyC;AAE3D,CAAA;AAEQ,KAYI,aAZJ,CAAA,UAAA,MAAA,GAAA,MAAA,EAAA,IAAA,OAAA,CAAA,GAY4D,SAZ5D,CAaN,QAbM,CAaG,CAbH,EAaM,CAbN,CAAA,EAAA,IAAA,GAAA,YAAA,CAAA;AAEM,KAmBF,WAAA,GAnBE,CAAA,GAAA,IAAA,EAAA,OAAA,EAAA,EAAA,GAAA,OAAA;AACK,KAsBP,YAAA,GAtBO;EAGP,WAAA,CAAA,EAAA,MAAc;EAAoD,cAAA,CAAA,EAAA,MAAA;EAAG,UAAA,CAAA,EAAA,MAAA;CAAZ;AAGnD,KAsBN,iBAAA,GAtBM;EAAI,UAAA,CAAA,EAAA,MAAA;EAGV,eAAA,CAAa,EAAA,MAAA;EACd,WAAA,CAAA,EAAA,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DpCO,EAAA,SAAA,CAAA,EAAA,MAAA,EAAA,GCsCf,ODtCe,CCsCP,QDtCO,CCsCE,CDtCF,ECsCK,CDtCL,CAAA,CAAA;EAGV,eAAA,EAAa,GAAA,GCoCA,ODpCA,CCoCQ,cDpCR,EAAA,CAAA;EACd,WAAA,EAAA,CAAA,QAAA,EAAA,MAAA,EAAA,EAAA,GCoC4B,ODpC5B,CAAA,IAAA,CAAA;;AAAT,UCuCe,UAAA,CDvCf;EADkE,SAAA,CAAA,UAAA,EAAA,MAAA,EAAA,CAAA,EAAA,IAAA;;;;UEdnD,uBAAA,SAAgC;EFF5C,OAAA,EEGM,OFHG;EAAoB,QAAA,EAAA,MAAA;EAAU,UAAA,CAAA,EAAA,MAAA;;AAAL,cEQ1B,iBFR0B,CAAA,eAAA,OAAA,CAAA,YEQ2B,UFR3B,CAAA;EAA0B,iBAAA,OAAA;EAAG,iBAAA,QAAA;EAAR,iBAAA,UAAA;EAAR,iBAAA,SAAA;EAAO,WAAA,CAAA,GAAA,EEcxC,eFdwC,CEcxB,YFdwB,CAAA,EAAA,MAAA,EEcD,uBFdC;EAE/C,SAAA,CAAA,UAAQ,EAAA,MAAA,EAAA,CAAA,EAAA,IAAA"}
@@ -0,0 +1,76 @@
1
+ import { Channel } from "amqplib";
2
+
3
+ //#region ../../core/src/types/types.d.ts
4
+ type PartialBy<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>;
5
+ type BusEvent<T extends string = string, P = unknown> = {
6
+ id: string;
7
+ type: T;
8
+ payload: P;
9
+ occurredAt: Date;
10
+ metadata?: Record<string, unknown>;
11
+ };
12
+ type FailedBusEvent<T extends string = string, P = unknown> = BusEvent<T, P> & {
13
+ error?: string;
14
+ retryCount: number;
15
+ lastAttemptAt?: Date;
16
+ };
17
+ type BusEventInput<T extends string = string, P = unknown> = PartialBy<BusEvent<T, P>, "id" | "occurredAt">;
18
+ type AnyListener = (...args: unknown[]) => unknown;
19
+ type RetryOptions = {
20
+ maxAttempts?: number;
21
+ initialDelayMs?: number;
22
+ maxDelayMs?: number;
23
+ };
24
+ type ProcessingOptions = {
25
+ bufferSize?: number;
26
+ bufferTimeoutMs?: number;
27
+ concurrency?: number;
28
+ maxBatchSize?: number;
29
+ };
30
+ type PublisherConfig = {
31
+ retryConfig?: RetryOptions;
32
+ processingConfig?: ProcessingOptions;
33
+ };
34
+ //#endregion
35
+ //#region ../../core/src/types/interfaces.d.ts
36
+ interface IOutboxEventBus<TTransaction> {
37
+ emit: <T extends string, P>(event: BusEventInput<T, P>, transaction?: TTransaction) => Promise<void>;
38
+ emitMany: <T extends string, P>(events: BusEventInput<T, P>[], transaction?: TTransaction) => Promise<void>;
39
+ on: <T extends string, P = unknown>(eventType: T, handler: (event: BusEvent<T, P>) => Promise<void>) => this;
40
+ addListener: <T extends string, P = unknown>(eventType: T, handler: (event: BusEvent<T, P>) => Promise<void>) => this;
41
+ off: <T extends string, P = unknown>(eventType: T, handler: (event: BusEvent<T, P>) => Promise<void>) => this;
42
+ removeListener: <T extends string, P = unknown>(eventType: T, handler: (event: BusEvent<T, P>) => Promise<void>) => this;
43
+ once: <T extends string, P = unknown>(eventType: T, handler: (event: BusEvent<T, P>) => Promise<void>) => this;
44
+ removeAllListeners: <T extends string>(eventType?: T) => this;
45
+ getSubscriptionCount: () => number;
46
+ listenerCount: (eventType: string) => number;
47
+ getListener: (eventType: string) => AnyListener | undefined;
48
+ eventNames: () => string[];
49
+ start: () => void;
50
+ stop: () => Promise<void>;
51
+ subscribe: <T extends string, P = unknown>(eventTypes: T[], handler: (event: BusEvent<T, P>) => Promise<void>) => this;
52
+ waitFor: <T extends string, P = unknown>(eventType: T, timeoutMs?: number) => Promise<BusEvent<T, P>>;
53
+ getFailedEvents: () => Promise<FailedBusEvent[]>;
54
+ retryEvents: (eventIds: string[]) => Promise<void>;
55
+ }
56
+ interface IPublisher {
57
+ subscribe(eventTypes: string[]): void;
58
+ }
59
+ //#endregion
60
+ //#region src/rabbitmq-publisher.d.ts
61
+ interface RabbitMQPublisherConfig extends PublisherConfig {
62
+ channel: Channel;
63
+ exchange: string;
64
+ routingKey?: string;
65
+ }
66
+ declare class RabbitMQPublisher<TTransaction = unknown> implements IPublisher {
67
+ private readonly channel;
68
+ private readonly exchange;
69
+ private readonly routingKey;
70
+ private readonly publisher;
71
+ constructor(bus: IOutboxEventBus<TTransaction>, config: RabbitMQPublisherConfig);
72
+ subscribe(eventTypes: string[]): void;
73
+ }
74
+ //#endregion
75
+ export { RabbitMQPublisher, RabbitMQPublisherConfig };
76
+ //# sourceMappingURL=index.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.mts","names":[],"sources":["../../../core/src/types/types.ts","../../../core/src/types/interfaces.ts","../src/rabbitmq-publisher.ts"],"sourcesContent":[],"mappings":";;;KAEK,6BAA6B,KAAK,KAAK,GAAG,KAAK,QAAQ,KAAK,GAAG;KAExD;EAFP,EAAA,EAAA,MAAA;EAA6B,IAAA,EAI1B,CAJ0B;EAAU,OAAA,EAKjC,CALiC;EAAG,UAAA,EAMjC,IANiC;EAAR,QAAA,CAAA,EAO1B,MAP0B,CAAA,MAAA,EAAA,OAAA,CAAA;CAA0B;AAAG,KAUxD,cAVwD,CAAA,UAAA,MAAA,GAAA,MAAA,EAAA,IAAA,OAAA,CAAA,GAUC,QAVD,CAUU,CAVV,EAUa,CAVb,CAAA,GAAA;EAAR,KAAA,CAAA,EAAA,MAAA;EAAR,UAAA,EAAA,MAAA;EAAO,aAAA,CAAA,EAazC,IAbyC;AAE3D,CAAA;AAEQ,KAYI,aAZJ,CAAA,UAAA,MAAA,GAAA,MAAA,EAAA,IAAA,OAAA,CAAA,GAY4D,SAZ5D,CAaN,QAbM,CAaG,CAbH,EAaM,CAbN,CAAA,EAAA,IAAA,GAAA,YAAA,CAAA;AAEM,KAmBF,WAAA,GAnBE,CAAA,GAAA,IAAA,EAAA,OAAA,EAAA,EAAA,GAAA,OAAA;AACK,KAsBP,YAAA,GAtBO;EAGP,WAA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AAA,KAAA,ECiCG,QDjCH,CCiCY,CDjCZ,ECiCe,CDjCf,CAAA,EAAA,GCiCsB,ODjCtB,CAAA,IAAA,CAAA,EAAA,GAAA,IAAA;EAAI,OAAA,EAAA,CAAA,UAAA,MAAA,EAAA,IAAA,OAAA,CAAA,CAAA,SAAA,ECoCP,CDpCO,EAAA,SAAA,CAAA,EAAA,MAAA,EAAA,GCsCf,ODtCe,CCsCP,QDtCO,CCsCE,CDtCF,ECsCK,CDtCL,CAAA,CAAA;EAGV,eAAA,EAAa,GAAA,GCoCA,ODpCA,CCoCQ,cDpCR,EAAA,CAAA;EACd,WAAA,EAAA,CAAA,QAAA,EAAA,MAAA,EAAA,EAAA,GCoC4B,ODpC5B,CAAA,IAAA,CAAA;;AAAT,UCuCe,UAAA,CDvCf;EADkE,SAAA,CAAA,UAAA,EAAA,MAAA,EAAA,CAAA,EAAA,IAAA;;;;UEdnD,uBAAA,SAAgC;EFF5C,OAAA,EEGM,OFHG;EAAoB,QAAA,EAAA,MAAA;EAAU,UAAA,CAAA,EAAA,MAAA;;AAAL,cEQ1B,iBFR0B,CAAA,eAAA,OAAA,CAAA,YEQ2B,UFR3B,CAAA;EAA0B,iBAAA,OAAA;EAAG,iBAAA,QAAA;EAAR,iBAAA,UAAA;EAAR,iBAAA,SAAA;EAAO,WAAA,CAAA,GAAA,EEcxC,eFdwC,CEcxB,YFdwB,CAAA,EAAA,MAAA,EEcD,uBFdC;EAE/C,SAAA,CAAA,UAAQ,EAAA,MAAA,EAAA,CAAA,EAAA,IAAA"}
package/dist/index.mjs ADDED
@@ -0,0 +1,34 @@
1
+ import { BackpressureError, EventPublisher } from "outbox-event-bus";
2
+
3
+ //#region src/rabbitmq-publisher.ts
4
+ var RabbitMQPublisher = class {
5
+ channel;
6
+ exchange;
7
+ routingKey;
8
+ publisher;
9
+ constructor(bus, config) {
10
+ this.channel = config.channel;
11
+ this.exchange = config.exchange;
12
+ this.routingKey = config.routingKey ?? "";
13
+ this.publisher = new EventPublisher(bus, config);
14
+ }
15
+ subscribe(eventTypes) {
16
+ this.publisher.subscribe(eventTypes, async (events) => {
17
+ for (const event of events) {
18
+ const message = JSON.stringify(event);
19
+ const routingKey = this.routingKey || event.type;
20
+ if (!this.channel.publish(this.exchange, routingKey, Buffer.from(message), {
21
+ contentType: "application/json",
22
+ headers: {
23
+ eventType: event.type,
24
+ eventId: event.id
25
+ }
26
+ })) throw new BackpressureError("RabbitMQ channel");
27
+ }
28
+ });
29
+ }
30
+ };
31
+
32
+ //#endregion
33
+ export { RabbitMQPublisher };
34
+ //# sourceMappingURL=index.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.mjs","names":[],"sources":["../src/rabbitmq-publisher.ts"],"sourcesContent":["import type { Channel } from \"amqplib\"\nimport type { BusEvent, IOutboxEventBus, IPublisher, PublisherConfig } from \"outbox-event-bus\"\nimport { BackpressureError, EventPublisher } from \"outbox-event-bus\"\n\nexport interface RabbitMQPublisherConfig extends PublisherConfig {\n channel: Channel\n exchange: string\n routingKey?: string\n}\n\nexport class RabbitMQPublisher<TTransaction = unknown> implements IPublisher {\n private readonly channel: Channel\n private readonly exchange: string\n private readonly routingKey: string\n private readonly publisher: EventPublisher<TTransaction>\n\n constructor(bus: IOutboxEventBus<TTransaction>, config: RabbitMQPublisherConfig) {\n this.channel = config.channel\n this.exchange = config.exchange\n this.routingKey = config.routingKey ?? \"\"\n this.publisher = new EventPublisher(bus, config)\n }\n\n subscribe(eventTypes: string[]): void {\n this.publisher.subscribe(eventTypes, async (events: BusEvent[]) => {\n for (const event of events) {\n const message = JSON.stringify(event)\n const routingKey = this.routingKey || event.type\n\n const published = this.channel.publish(this.exchange, routingKey, Buffer.from(message), {\n contentType: \"application/json\",\n headers: {\n eventType: event.type,\n eventId: event.id,\n },\n })\n\n if (!published) {\n throw new BackpressureError(\"RabbitMQ channel\")\n }\n }\n })\n }\n}\n"],"mappings":";;;AAUA,IAAa,oBAAb,MAA6E;CAC3E,AAAiB;CACjB,AAAiB;CACjB,AAAiB;CACjB,AAAiB;CAEjB,YAAY,KAAoC,QAAiC;AAC/E,OAAK,UAAU,OAAO;AACtB,OAAK,WAAW,OAAO;AACvB,OAAK,aAAa,OAAO,cAAc;AACvC,OAAK,YAAY,IAAI,eAAe,KAAK,OAAO;;CAGlD,UAAU,YAA4B;AACpC,OAAK,UAAU,UAAU,YAAY,OAAO,WAAuB;AACjE,QAAK,MAAM,SAAS,QAAQ;IAC1B,MAAM,UAAU,KAAK,UAAU,MAAM;IACrC,MAAM,aAAa,KAAK,cAAc,MAAM;AAU5C,QAAI,CARc,KAAK,QAAQ,QAAQ,KAAK,UAAU,YAAY,OAAO,KAAK,QAAQ,EAAE;KACtF,aAAa;KACb,SAAS;MACP,WAAW,MAAM;MACjB,SAAS,MAAM;MAChB;KACF,CAAC,CAGA,OAAM,IAAI,kBAAkB,mBAAmB;;IAGnD"}
package/package.json ADDED
@@ -0,0 +1,52 @@
1
+ {
2
+ "name": "@outbox-event-bus/rabbitmq-publisher",
3
+ "version": "1.0.0",
4
+ "private": false,
5
+ "license": "MIT",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "https://github.com/dunika/outbox-event-bus.git",
9
+ "directory": "publishers/rabbitmq"
10
+ },
11
+ "homepage": "https://github.com/dunika/outbox-event-bus/tree/main/publishers/rabbitmq#readme",
12
+ "type": "module",
13
+ "main": "./dist/index.cjs",
14
+ "module": "./dist/index.mjs",
15
+ "types": "./dist/index.d.mts",
16
+ "exports": {
17
+ ".": {
18
+ "import": {
19
+ "types": "./dist/index.d.mts",
20
+ "default": "./dist/index.mjs"
21
+ },
22
+ "require": {
23
+ "types": "./dist/index.d.cts",
24
+ "default": "./dist/index.cjs"
25
+ }
26
+ }
27
+ },
28
+ "files": [
29
+ "dist",
30
+ "src",
31
+ "README.md",
32
+ "LICENSE"
33
+ ],
34
+ "dependencies": {
35
+ "outbox-event-bus": "1.0.0"
36
+ },
37
+ "peerDependencies": {
38
+ "amqplib": "^0.10.9"
39
+ },
40
+ "devDependencies": {
41
+ "@types/amqplib": "^0.10.8",
42
+ "@types/node": "^25.0.3",
43
+ "tsdown": "^0.18.2",
44
+ "typescript": "5.9.3",
45
+ "vitest": "^4.0.16",
46
+ "@outbox-event-bus/config": "0.0.1"
47
+ },
48
+ "scripts": {
49
+ "build": "tsdown",
50
+ "test": "vitest run"
51
+ }
52
+ }
package/src/index.ts ADDED
@@ -0,0 +1 @@
1
+ export * from "./rabbitmq-publisher"
@@ -0,0 +1,44 @@
1
+ import { BackpressureError } from "outbox-event-bus"
2
+ import { beforeEach, describe, expect, it, vi } from "vitest"
3
+ import { RabbitMQPublisher } from "./rabbitmq-publisher"
4
+
5
+ describe("RabbitMQPublisher", () => {
6
+ let mockChannel: any
7
+ let _mockConnection: any
8
+ let mockBus: any
9
+
10
+ beforeEach(() => {
11
+ mockChannel = {
12
+ assertExchange: vi.fn().mockResolvedValue({}),
13
+ publish: vi.fn().mockReturnValue(true),
14
+ }
15
+ _mockConnection = {
16
+ createChannel: vi.fn().mockResolvedValue(mockChannel),
17
+ }
18
+ mockBus = {
19
+ subscribe: vi.fn(),
20
+ }
21
+ })
22
+
23
+ it("should throw BackpressureError when channel buffer is full", async () => {
24
+ const publisher = new RabbitMQPublisher(mockBus, {
25
+ channel: mockChannel,
26
+ exchange: "test-exchange",
27
+ })
28
+
29
+ publisher.subscribe(["test-event"])
30
+ const handler = mockBus.subscribe.mock.calls[0][1]
31
+
32
+ // Simulate backpressure
33
+ mockChannel.publish.mockReturnValue(false)
34
+
35
+ const event = {
36
+ id: "1",
37
+ type: "test-event",
38
+ payload: {},
39
+ occurredAt: new Date(),
40
+ }
41
+
42
+ await expect(handler([event])).rejects.toThrow(BackpressureError)
43
+ })
44
+ })
@@ -0,0 +1,44 @@
1
+ import type { Channel } from "amqplib"
2
+ import type { BusEvent, IOutboxEventBus, IPublisher, PublisherConfig } from "outbox-event-bus"
3
+ import { BackpressureError, EventPublisher } from "outbox-event-bus"
4
+
5
+ export interface RabbitMQPublisherConfig extends PublisherConfig {
6
+ channel: Channel
7
+ exchange: string
8
+ routingKey?: string
9
+ }
10
+
11
+ export class RabbitMQPublisher<TTransaction = unknown> implements IPublisher {
12
+ private readonly channel: Channel
13
+ private readonly exchange: string
14
+ private readonly routingKey: string
15
+ private readonly publisher: EventPublisher<TTransaction>
16
+
17
+ constructor(bus: IOutboxEventBus<TTransaction>, config: RabbitMQPublisherConfig) {
18
+ this.channel = config.channel
19
+ this.exchange = config.exchange
20
+ this.routingKey = config.routingKey ?? ""
21
+ this.publisher = new EventPublisher(bus, config)
22
+ }
23
+
24
+ subscribe(eventTypes: string[]): void {
25
+ this.publisher.subscribe(eventTypes, async (events: BusEvent[]) => {
26
+ for (const event of events) {
27
+ const message = JSON.stringify(event)
28
+ const routingKey = this.routingKey || event.type
29
+
30
+ const published = this.channel.publish(this.exchange, routingKey, Buffer.from(message), {
31
+ contentType: "application/json",
32
+ headers: {
33
+ eventType: event.type,
34
+ eventId: event.id,
35
+ },
36
+ })
37
+
38
+ if (!published) {
39
+ throw new BackpressureError("RabbitMQ channel")
40
+ }
41
+ }
42
+ })
43
+ }
44
+ }