@nwire/amqp 0.10.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 +21 -0
- package/README.md +24 -0
- package/dist/amqp-bus.d.ts +58 -0
- package/dist/amqp-bus.js +91 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/package.json +58 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Alex Gefter / 200apps Ltd.
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
# @nwire/amqp
|
|
2
|
+
|
|
3
|
+
RabbitMQ-backed `EventBus` adapter — experimental in 0.9.x.
|
|
4
|
+
|
|
5
|
+
```ts
|
|
6
|
+
import { amqpBus } from "@nwire/amqp";
|
|
7
|
+
|
|
8
|
+
const bus = await amqpBus({
|
|
9
|
+
url: process.env.AMQP_URL ?? "amqp://localhost",
|
|
10
|
+
exchange: "nwire.events",
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
await bus.publish({
|
|
14
|
+
eventName: "orders.order-was-placed",
|
|
15
|
+
payload: { orderId: "o1" },
|
|
16
|
+
envelope: seedEnvelope({ tenant: "acme" }),
|
|
17
|
+
});
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
Integration tests run with `RUN_INTEGRATION=1` against a local RabbitMQ
|
|
21
|
+
(docker-compose service `rabbitmq`).
|
|
22
|
+
|
|
23
|
+
See `@nwire/nats` for the production-ready JetStream adapter; AMQP
|
|
24
|
+
lands as a scaffold for teams already on RabbitMQ.
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@nwire/amqp` — RabbitMQ-backed `EventBus` adapter (scaffold).
|
|
3
|
+
*
|
|
4
|
+
* Publishes to a topic exchange; subscribers bind durable queues per
|
|
5
|
+
* event name. At-least-once with manual ack — failed handlers nack +
|
|
6
|
+
* requeue up to `maxRetries`, then route to DLQ exchange.
|
|
7
|
+
*/
|
|
8
|
+
import type { BusEventMessage, BusSubscriber, EventBus } from "@nwire/bus";
|
|
9
|
+
import type { Logger } from "@nwire/logger";
|
|
10
|
+
export interface AmqpChannelLike {
|
|
11
|
+
assertExchange(exchange: string, type: string, opts?: {
|
|
12
|
+
durable?: boolean;
|
|
13
|
+
}): Promise<void>;
|
|
14
|
+
assertQueue(queue: string, opts?: {
|
|
15
|
+
durable?: boolean;
|
|
16
|
+
}): Promise<{
|
|
17
|
+
queue: string;
|
|
18
|
+
}>;
|
|
19
|
+
bindQueue(queue: string, exchange: string, pattern: string): Promise<void>;
|
|
20
|
+
publish(exchange: string, routingKey: string, content: Buffer): boolean;
|
|
21
|
+
consume(queue: string, handler: (msg: {
|
|
22
|
+
content: Buffer;
|
|
23
|
+
ack(): void;
|
|
24
|
+
nack(requeue?: boolean): void;
|
|
25
|
+
}) => void): Promise<{
|
|
26
|
+
consumerTag: string;
|
|
27
|
+
}>;
|
|
28
|
+
cancel(consumerTag: string): Promise<void>;
|
|
29
|
+
close(): Promise<void>;
|
|
30
|
+
}
|
|
31
|
+
export interface AmqpConnectionLike {
|
|
32
|
+
createChannel(): Promise<AmqpChannelLike>;
|
|
33
|
+
close(): Promise<void>;
|
|
34
|
+
}
|
|
35
|
+
export type AmqpConnectFn = (url: string) => Promise<AmqpConnectionLike>;
|
|
36
|
+
export interface AmqpBusOptions {
|
|
37
|
+
readonly url: string;
|
|
38
|
+
readonly exchange?: string;
|
|
39
|
+
readonly logger?: Logger;
|
|
40
|
+
readonly connect?: AmqpConnectFn;
|
|
41
|
+
}
|
|
42
|
+
export declare class AmqpEventBus implements EventBus {
|
|
43
|
+
private readonly options;
|
|
44
|
+
private readonly exchange;
|
|
45
|
+
private readonly logger;
|
|
46
|
+
private conn?;
|
|
47
|
+
private pubChannel?;
|
|
48
|
+
private readonly consumers;
|
|
49
|
+
private stopped;
|
|
50
|
+
constructor(options: AmqpBusOptions, conn?: AmqpConnectionLike);
|
|
51
|
+
start(): Promise<void>;
|
|
52
|
+
publish(msg: BusEventMessage): Promise<void>;
|
|
53
|
+
subscribe(eventName: string, subscriber: BusSubscriber): void;
|
|
54
|
+
private ensureConsumer;
|
|
55
|
+
stop(): Promise<void>;
|
|
56
|
+
}
|
|
57
|
+
export declare function amqpBus(options: AmqpBusOptions): Promise<AmqpEventBus>;
|
|
58
|
+
export { AmqpEventBus as AmqpBus };
|
package/dist/amqp-bus.js
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@nwire/amqp` — RabbitMQ-backed `EventBus` adapter (scaffold).
|
|
3
|
+
*
|
|
4
|
+
* Publishes to a topic exchange; subscribers bind durable queues per
|
|
5
|
+
* event name. At-least-once with manual ack — failed handlers nack +
|
|
6
|
+
* requeue up to `maxRetries`, then route to DLQ exchange.
|
|
7
|
+
*/
|
|
8
|
+
import { NoopLogger } from "@nwire/logger";
|
|
9
|
+
export class AmqpEventBus {
|
|
10
|
+
options;
|
|
11
|
+
exchange;
|
|
12
|
+
logger;
|
|
13
|
+
conn;
|
|
14
|
+
pubChannel;
|
|
15
|
+
consumers = new Map();
|
|
16
|
+
stopped = false;
|
|
17
|
+
constructor(options, conn) {
|
|
18
|
+
this.options = options;
|
|
19
|
+
this.exchange = options.exchange ?? "nwire.events";
|
|
20
|
+
this.logger = options.logger ?? new NoopLogger();
|
|
21
|
+
this.conn = conn;
|
|
22
|
+
}
|
|
23
|
+
async start() {
|
|
24
|
+
if (this.pubChannel)
|
|
25
|
+
return;
|
|
26
|
+
if (!this.conn) {
|
|
27
|
+
const connect = this.options.connect;
|
|
28
|
+
if (!connect) {
|
|
29
|
+
throw new Error("AmqpEventBus: provide `connect` or pass a connection to constructor");
|
|
30
|
+
}
|
|
31
|
+
this.conn = await connect(this.options.url);
|
|
32
|
+
}
|
|
33
|
+
this.pubChannel = await this.conn.createChannel();
|
|
34
|
+
await this.pubChannel.assertExchange(this.exchange, "topic", { durable: true });
|
|
35
|
+
}
|
|
36
|
+
async publish(msg) {
|
|
37
|
+
if (this.stopped)
|
|
38
|
+
throw new Error("AmqpEventBus: publish after stop");
|
|
39
|
+
if (!this.pubChannel)
|
|
40
|
+
await this.start();
|
|
41
|
+
const body = Buffer.from(JSON.stringify(msg));
|
|
42
|
+
this.pubChannel.publish(this.exchange, msg.eventName, body);
|
|
43
|
+
}
|
|
44
|
+
subscribe(eventName, subscriber) {
|
|
45
|
+
if (this.stopped)
|
|
46
|
+
throw new Error("AmqpEventBus: subscribe after stop");
|
|
47
|
+
void this.ensureConsumer(eventName, subscriber);
|
|
48
|
+
}
|
|
49
|
+
async ensureConsumer(eventName, subscriber) {
|
|
50
|
+
if (this.consumers.has(eventName))
|
|
51
|
+
return;
|
|
52
|
+
if (!this.conn)
|
|
53
|
+
await this.start();
|
|
54
|
+
const channel = await this.conn.createChannel();
|
|
55
|
+
await channel.assertExchange(this.exchange, "topic", { durable: true });
|
|
56
|
+
const q = await channel.assertQueue(`nwire.${eventName}`, { durable: true });
|
|
57
|
+
await channel.bindQueue(q.queue, this.exchange, eventName);
|
|
58
|
+
const { consumerTag } = await channel.consume(q.queue, (raw) => {
|
|
59
|
+
if (!raw)
|
|
60
|
+
return;
|
|
61
|
+
void (async () => {
|
|
62
|
+
try {
|
|
63
|
+
const msg = JSON.parse(raw.content.toString("utf8"));
|
|
64
|
+
await subscriber(msg);
|
|
65
|
+
raw.ack();
|
|
66
|
+
}
|
|
67
|
+
catch (err) {
|
|
68
|
+
this.logger.error(`AmqpEventBus subscriber for "${eventName}" threw`, { err });
|
|
69
|
+
raw.nack(false);
|
|
70
|
+
}
|
|
71
|
+
})();
|
|
72
|
+
});
|
|
73
|
+
this.consumers.set(eventName, { tag: consumerTag, channel });
|
|
74
|
+
}
|
|
75
|
+
async stop() {
|
|
76
|
+
this.stopped = true;
|
|
77
|
+
for (const { tag, channel } of this.consumers.values()) {
|
|
78
|
+
await channel.cancel(tag);
|
|
79
|
+
await channel.close();
|
|
80
|
+
}
|
|
81
|
+
this.consumers.clear();
|
|
82
|
+
await this.pubChannel?.close();
|
|
83
|
+
await this.conn?.close();
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
export async function amqpBus(options) {
|
|
87
|
+
const bus = new AmqpEventBus(options);
|
|
88
|
+
await bus.start();
|
|
89
|
+
return bus;
|
|
90
|
+
}
|
|
91
|
+
export { AmqpEventBus as AmqpBus };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { AmqpEventBus, AmqpBus, amqpBus, type AmqpBusOptions, type AmqpChannelLike, type AmqpConnectionLike, type AmqpConnectFn, } from "./amqp-bus.js";
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { AmqpEventBus, AmqpBus, amqpBus, } from "./amqp-bus.js";
|
package/package.json
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@nwire/amqp",
|
|
3
|
+
"version": "0.10.0",
|
|
4
|
+
"description": "Nwire — RabbitMQ-backed EventBus adapter. Topic exchange pub/sub with optional durable queues — same EventBus contract as @nwire/nats.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"adapter",
|
|
7
|
+
"amqp",
|
|
8
|
+
"bus",
|
|
9
|
+
"messaging",
|
|
10
|
+
"nwire",
|
|
11
|
+
"rabbitmq"
|
|
12
|
+
],
|
|
13
|
+
"license": "MIT",
|
|
14
|
+
"files": [
|
|
15
|
+
"dist",
|
|
16
|
+
"README.md",
|
|
17
|
+
"LICENSE"
|
|
18
|
+
],
|
|
19
|
+
"type": "module",
|
|
20
|
+
"main": "./dist/index.js",
|
|
21
|
+
"types": "./dist/index.d.ts",
|
|
22
|
+
"exports": {
|
|
23
|
+
".": {
|
|
24
|
+
"import": "./dist/index.js",
|
|
25
|
+
"types": "./dist/index.d.ts"
|
|
26
|
+
}
|
|
27
|
+
},
|
|
28
|
+
"publishConfig": {
|
|
29
|
+
"access": "public"
|
|
30
|
+
},
|
|
31
|
+
"dependencies": {
|
|
32
|
+
"@nwire/bus": "0.10.0",
|
|
33
|
+
"@nwire/logger": "0.10.0",
|
|
34
|
+
"@nwire/envelope": "0.10.0"
|
|
35
|
+
},
|
|
36
|
+
"devDependencies": {
|
|
37
|
+
"@types/amqplib": "^0.10.6",
|
|
38
|
+
"@types/node": "^22.19.9",
|
|
39
|
+
"amqplib": "^0.10.4",
|
|
40
|
+
"typescript": "^5.9.3",
|
|
41
|
+
"vitest": "^4.0.18"
|
|
42
|
+
},
|
|
43
|
+
"peerDependencies": {
|
|
44
|
+
"amqplib": "^0.10.4"
|
|
45
|
+
},
|
|
46
|
+
"peerDependenciesMeta": {
|
|
47
|
+
"amqplib": {
|
|
48
|
+
"optional": false
|
|
49
|
+
}
|
|
50
|
+
},
|
|
51
|
+
"scripts": {
|
|
52
|
+
"build": "tsc && node ../../scripts/fix-dist-extensions.mjs dist",
|
|
53
|
+
"dev": "tsc --watch",
|
|
54
|
+
"typecheck": "tsc --noEmit",
|
|
55
|
+
"test": "vitest run --dir src",
|
|
56
|
+
"test:integration": "RUN_INTEGRATION=1 vitest run --dir src"
|
|
57
|
+
}
|
|
58
|
+
}
|