@mamindom/common 1.0.119 → 1.0.121

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/dist/index.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  export * from './enum';
2
2
  export * from './grpc';
3
3
  export * from './rbac';
4
+ export * from './rmq';
4
5
  export * from './utils';
package/dist/index.js CHANGED
@@ -17,4 +17,5 @@ Object.defineProperty(exports, "__esModule", { value: true });
17
17
  __exportStar(require("./enum"), exports);
18
18
  __exportStar(require("./grpc"), exports);
19
19
  __exportStar(require("./rbac"), exports);
20
+ __exportStar(require("./rmq"), exports);
20
21
  __exportStar(require("./utils"), exports);
@@ -0,0 +1,37 @@
1
+ /**
2
+ * Convention: dead-letter exchange and queue names are derived from the
3
+ * primary queue name. `cart_queue` → `cart.dlx` + `cart.dlq`. The `_queue`
4
+ * or `-queue` suffix is stripped; if absent, the whole name is used.
5
+ *
6
+ * Keeping this as a pure function (instead of env vars) prevents the class
7
+ * of bugs where producer and consumer disagree on DLX naming and trigger
8
+ * PRECONDITION_FAILED on queue declare.
9
+ */
10
+ export declare function dlxNameFor(queue: string): string;
11
+ export declare function dlqNameFor(queue: string): string;
12
+ /**
13
+ * queueOptions.arguments needed for a queue that should dead-letter into its
14
+ * conventional DLX. Use in `app.connectMicroservice(...)` and in every
15
+ * `ClientsModule.registerAsync` that declares the same queue, so producer
16
+ * and consumer declares stay equivalent.
17
+ */
18
+ export declare function rmqQueueDlxArguments(queue: string): {
19
+ 'x-dead-letter-exchange': string;
20
+ };
21
+ export interface BootstrapRmqDlxOptions {
22
+ url: string;
23
+ queue: string;
24
+ }
25
+ /**
26
+ * Idempotently declares the DLX trio for the given queue: fanout exchange +
27
+ * DLQ queue + binding. Run once at service bootstrap, before connecting RMQ
28
+ * microservice. Safe to repeat — RabbitMQ no-ops on identical re-declares.
29
+ *
30
+ * `amqplib` is loaded lazily so services that import other helpers from this
31
+ * module but don't actually run RMQ (e.g. gateway-service) don't need amqplib
32
+ * in their node_modules.
33
+ *
34
+ * Why fanout: dead-lettered messages keep their original routing key, but
35
+ * we want every message into the DLX to land in the DLQ regardless of key.
36
+ */
37
+ export declare function bootstrapRmqDlx(opts: BootstrapRmqDlxOptions): Promise<void>;
@@ -0,0 +1,69 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.dlxNameFor = dlxNameFor;
4
+ exports.dlqNameFor = dlqNameFor;
5
+ exports.rmqQueueDlxArguments = rmqQueueDlxArguments;
6
+ exports.bootstrapRmqDlx = bootstrapRmqDlx;
7
+ const common_1 = require("@nestjs/common");
8
+ /**
9
+ * Convention: dead-letter exchange and queue names are derived from the
10
+ * primary queue name. `cart_queue` → `cart.dlx` + `cart.dlq`. The `_queue`
11
+ * or `-queue` suffix is stripped; if absent, the whole name is used.
12
+ *
13
+ * Keeping this as a pure function (instead of env vars) prevents the class
14
+ * of bugs where producer and consumer disagree on DLX naming and trigger
15
+ * PRECONDITION_FAILED on queue declare.
16
+ */
17
+ function dlxNameFor(queue) {
18
+ return `${stripQueueSuffix(queue)}.dlx`;
19
+ }
20
+ function dlqNameFor(queue) {
21
+ return `${stripQueueSuffix(queue)}.dlq`;
22
+ }
23
+ function stripQueueSuffix(queue) {
24
+ return queue.replace(/[_-]queue$/, '');
25
+ }
26
+ /**
27
+ * queueOptions.arguments needed for a queue that should dead-letter into its
28
+ * conventional DLX. Use in `app.connectMicroservice(...)` and in every
29
+ * `ClientsModule.registerAsync` that declares the same queue, so producer
30
+ * and consumer declares stay equivalent.
31
+ */
32
+ function rmqQueueDlxArguments(queue) {
33
+ return { 'x-dead-letter-exchange': dlxNameFor(queue) };
34
+ }
35
+ /**
36
+ * Idempotently declares the DLX trio for the given queue: fanout exchange +
37
+ * DLQ queue + binding. Run once at service bootstrap, before connecting RMQ
38
+ * microservice. Safe to repeat — RabbitMQ no-ops on identical re-declares.
39
+ *
40
+ * `amqplib` is loaded lazily so services that import other helpers from this
41
+ * module but don't actually run RMQ (e.g. gateway-service) don't need amqplib
42
+ * in their node_modules.
43
+ *
44
+ * Why fanout: dead-lettered messages keep their original routing key, but
45
+ * we want every message into the DLX to land in the DLQ regardless of key.
46
+ */
47
+ async function bootstrapRmqDlx(opts) {
48
+ const logger = new common_1.Logger('RmqDlxBootstrap');
49
+ const dlx = dlxNameFor(opts.queue);
50
+ const dlq = dlqNameFor(opts.queue);
51
+ // eslint-disable-next-line @typescript-eslint/no-require-imports
52
+ const amqp = require('amqplib');
53
+ const connection = await amqp.connect(opts.url);
54
+ try {
55
+ const channel = await connection.createChannel();
56
+ try {
57
+ await channel.assertExchange(dlx, 'fanout', { durable: true });
58
+ await channel.assertQueue(dlq, { durable: true });
59
+ await channel.bindQueue(dlq, dlx, '');
60
+ logger.log(`DLX ready for ${opts.queue}: ${dlx} → ${dlq}`);
61
+ }
62
+ finally {
63
+ await channel.close();
64
+ }
65
+ }
66
+ finally {
67
+ await connection.close();
68
+ }
69
+ }
@@ -0,0 +1 @@
1
+ export * from './bootstrap-dlx';
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./bootstrap-dlx"), exports);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mamindom/common",
3
- "version": "1.0.119",
3
+ "version": "1.0.121",
4
4
  "type": "commonjs",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -16,7 +16,9 @@
16
16
  },
17
17
  "devDependencies": {
18
18
  "@mamindom/core": "^1.0.4",
19
+ "@types/amqplib": "^0.10.5",
19
20
  "@types/node": "^25.3.5",
21
+ "amqplib": "^0.10.9",
20
22
  "prettier": "^3.8.1",
21
23
  "typescript": "^5.9.3"
22
24
  },
@@ -25,5 +27,8 @@
25
27
  "@nestjs/common": "^11.1.16",
26
28
  "@nestjs/config": "^4.0.3",
27
29
  "@nestjs/microservices": "^11.1.16"
30
+ },
31
+ "peerDependencies": {
32
+ "amqplib": "^0.10.0"
28
33
  }
29
34
  }