amqp-extension 4.0.0 → 5.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.
Files changed (46) hide show
  1. package/README.MD +284 -40
  2. package/dist/index.d.mts +287 -0
  3. package/dist/index.mjs +502 -0
  4. package/dist/index.mjs.map +1 -0
  5. package/package.json +53 -39
  6. package/.editorconfig +0 -13
  7. package/.eslintrc +0 -27
  8. package/.husky/commit-msg +0 -4
  9. package/CHANGELOG.md +0 -149
  10. package/commitlint.config.js +0 -10
  11. package/dist/config/index.d.ts +0 -3
  12. package/dist/config/index.js +0 -25
  13. package/dist/config/module.d.ts +0 -2
  14. package/dist/config/module.js +0 -23
  15. package/dist/config/type.d.ts +0 -13
  16. package/dist/config/type.js +0 -8
  17. package/dist/consume/constants.d.ts +0 -1
  18. package/dist/consume/constants.js +0 -10
  19. package/dist/consume/index.d.ts +0 -3
  20. package/dist/consume/index.js +0 -25
  21. package/dist/consume/type.d.ts +0 -24
  22. package/dist/consume/type.js +0 -8
  23. package/dist/consume/utils.d.ts +0 -2
  24. package/dist/consume/utils.js +0 -16
  25. package/dist/exchange/constants.d.ts +0 -6
  26. package/dist/exchange/constants.js +0 -10
  27. package/dist/exchange/index.d.ts +0 -3
  28. package/dist/exchange/index.js +0 -19
  29. package/dist/exchange/type.d.ts +0 -8
  30. package/dist/exchange/type.js +0 -2
  31. package/dist/exchange/utils.d.ts +0 -4
  32. package/dist/exchange/utils.js +0 -13
  33. package/dist/index.d.ts +0 -6
  34. package/dist/index.js +0 -28
  35. package/dist/module.d.ts +0 -18
  36. package/dist/module.js +0 -187
  37. package/dist/publish/index.d.ts +0 -1
  38. package/dist/publish/index.js +0 -23
  39. package/dist/publish/type.d.ts +0 -7
  40. package/dist/publish/type.js +0 -8
  41. package/dist/type.d.ts +0 -14
  42. package/dist/type.js +0 -2
  43. package/dist/utils.d.ts +0 -2
  44. package/dist/utils.js +0 -23
  45. package/release.config.js +0 -3
  46. package/tsconfig.eslint.json +0 -12
package/dist/module.js DELETED
@@ -1,187 +0,0 @@
1
- "use strict";
2
- /*
3
- * Copyright (c) 2024.
4
- * Author Peter Placzek (tada5hi)
5
- * For the full copyright and license information,
6
- * view the LICENSE file that was distributed with this source code.
7
- */
8
- var __importDefault = (this && this.__importDefault) || function (mod) {
9
- return (mod && mod.__esModule) ? mod : { "default": mod };
10
- };
11
- Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.Client = void 0;
13
- const amqp_connection_manager_1 = require("amqp-connection-manager");
14
- const node_process_1 = __importDefault(require("node:process"));
15
- const consume_1 = require("./consume");
16
- const exchange_1 = require("./exchange");
17
- const utils_1 = require("./utils");
18
- class Client {
19
- constructor(ctx) {
20
- if (ctx.connection) {
21
- this.connection = ctx.connection;
22
- }
23
- else {
24
- this.connection = (0, amqp_connection_manager_1.connect)(ctx.connectionOptions, {
25
- reconnectTimeInSeconds: 60,
26
- });
27
- node_process_1.default.once('SIGINT', async () => {
28
- if (this.connection) {
29
- await this.connection.close();
30
- }
31
- });
32
- }
33
- this.publishOptions = ctx.publishOptions || {};
34
- this.consumeOptions = ctx.consumeOptions || {};
35
- this.exchange = ctx.exchange || {
36
- type: exchange_1.ExchangeType.DIRECT,
37
- name: '',
38
- };
39
- }
40
- of(exchange) {
41
- return new Client({
42
- connection: this.connection,
43
- publishOptions: this.publishOptions,
44
- consumeOptions: this.consumeOptions,
45
- exchange,
46
- });
47
- }
48
- async consume(_ctxOrFrom, _optionsOrHandlers, _handlers) {
49
- let from;
50
- let handlers;
51
- let options;
52
- if (typeof _ctxOrFrom === 'string') {
53
- from = _ctxOrFrom;
54
- if ((0, consume_1.isConsumeHandlers)(_optionsOrHandlers)) {
55
- handlers = _optionsOrHandlers;
56
- options = this.consumeOptions;
57
- }
58
- else {
59
- handlers = _handlers;
60
- options = (0, utils_1.mergeOptions)(_optionsOrHandlers, this.consumeOptions);
61
- }
62
- }
63
- else {
64
- from = _ctxOrFrom.from;
65
- handlers = _ctxOrFrom.handlers;
66
- if (_ctxOrFrom.options) {
67
- options = (0, utils_1.mergeOptions)(_ctxOrFrom.options, this.consumeOptions);
68
- }
69
- else {
70
- options = this.consumeOptions;
71
- }
72
- }
73
- const requeueOnFailure = options.requeueOnFailure ?? false;
74
- const handleMessage = async (message, ch) => {
75
- if (!message) {
76
- return;
77
- }
78
- const handler = handlers[message.properties.type] ||
79
- handlers[consume_1.ConsumeHandlerAnyKey];
80
- if (typeof handler === 'undefined') {
81
- if (!options.noAck) {
82
- ch.nack(message, undefined, requeueOnFailure);
83
- }
84
- return;
85
- }
86
- try {
87
- await handler(message, ch);
88
- if (!options.noAck) {
89
- ch.ack(message);
90
- }
91
- }
92
- catch (e) {
93
- if (!options.noAck) {
94
- ch.nack(message, undefined, requeueOnFailure);
95
- }
96
- }
97
- };
98
- const channel = this.connection.createChannel({
99
- setup: async (channel) => {
100
- let queueName = from;
101
- if ((0, exchange_1.isDefaultExchange)(this.exchange.type)) {
102
- if (queueName === '') {
103
- throw new Error('The queue name can not be empty if the default exchange is selected.');
104
- }
105
- await channel.assertQueue(queueName, {
106
- durable: true,
107
- });
108
- }
109
- else {
110
- await channel.assertExchange(this.exchange.name || '', this.exchange.type, this.exchange.options);
111
- const assertionQueue = await channel.assertQueue('', {
112
- durable: false,
113
- autoDelete: true,
114
- exclusive: true,
115
- });
116
- await channel.bindQueue(assertionQueue.queue, this.exchange.name || '', from || '');
117
- queueName = assertionQueue.queue;
118
- }
119
- if (typeof options.prefetchCount === 'number') {
120
- await channel.prefetch(options.prefetchCount);
121
- }
122
- await channel.consume(queueName, (message) => handleMessage(message, channel), options);
123
- },
124
- });
125
- return channel.waitForConnect();
126
- }
127
- async publish(_toOrCtx, _data, _options) {
128
- let buffer;
129
- let to;
130
- let data;
131
- let options;
132
- if (typeof _toOrCtx === 'string') {
133
- to = _toOrCtx;
134
- data = _data;
135
- if (_options) {
136
- options = (0, utils_1.mergeOptions)(_options, this.publishOptions);
137
- }
138
- else {
139
- options = this.publishOptions;
140
- }
141
- }
142
- else {
143
- to = _toOrCtx.to;
144
- data = _toOrCtx.data;
145
- if (_toOrCtx.options) {
146
- options = (0, utils_1.mergeOptions)(_toOrCtx.options, this.publishOptions);
147
- }
148
- else {
149
- options = this.publishOptions;
150
- }
151
- }
152
- if (Buffer.isBuffer(data)) {
153
- buffer = data;
154
- }
155
- else if (typeof data === 'string') {
156
- buffer = Buffer.from(data, 'utf-8');
157
- }
158
- else {
159
- buffer = Buffer.from(JSON.stringify(data), 'utf-8');
160
- }
161
- const { connection } = this;
162
- const channel = connection.createChannel({
163
- setup: async (channel) => {
164
- if ((0, exchange_1.isDefaultExchange)(this.exchange.type)) {
165
- if (!to) {
166
- throw new Error('The queue name can not be empty if the default exchange is selected.');
167
- }
168
- await channel.assertQueue(to, {
169
- durable: true,
170
- });
171
- return;
172
- }
173
- if (!this.exchange.name) {
174
- throw new SyntaxError('A non default exchange requires a name.');
175
- }
176
- await channel.assertExchange(this.exchange.name, this.exchange.type, this.exchange.options);
177
- },
178
- });
179
- if (!to) {
180
- throw new SyntaxError('The publish operation requires a destination.');
181
- }
182
- const published = await channel.publish(this.exchange.name || '', to, buffer, options);
183
- await channel.close();
184
- return published;
185
- }
186
- }
187
- exports.Client = Client;
@@ -1 +0,0 @@
1
- export * from './type';
@@ -1,23 +0,0 @@
1
- "use strict";
2
- /*
3
- * Copyright (c) 2022.
4
- * Author Peter Placzek (tada5hi)
5
- * For the full copyright and license information,
6
- * view the LICENSE file that was distributed with this source code.
7
- */
8
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
9
- if (k2 === undefined) k2 = k;
10
- var desc = Object.getOwnPropertyDescriptor(m, k);
11
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
12
- desc = { enumerable: true, get: function() { return m[k]; } };
13
- }
14
- Object.defineProperty(o, k2, desc);
15
- }) : (function(o, m, k, k2) {
16
- if (k2 === undefined) k2 = k;
17
- o[k2] = m[k];
18
- }));
19
- var __exportStar = (this && this.__exportStar) || function(m, exports) {
20
- for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
21
- };
22
- Object.defineProperty(exports, "__esModule", { value: true });
23
- __exportStar(require("./type"), exports);
@@ -1,7 +0,0 @@
1
- import type { Options } from 'amqplib';
2
- export type PublishOptions = Options.Publish;
3
- export type PublishContext = {
4
- to: string;
5
- data: any;
6
- options?: PublishOptions;
7
- };
@@ -1,8 +0,0 @@
1
- "use strict";
2
- /*
3
- * Copyright (c) 2022.
4
- * Author Peter Placzek (tada5hi)
5
- * For the full copyright and license information,
6
- * view the LICENSE file that was distributed with this source code.
7
- */
8
- Object.defineProperty(exports, "__esModule", { value: true });
package/dist/type.d.ts DELETED
@@ -1,14 +0,0 @@
1
- import type { connect } from 'amqp-connection-manager';
2
- import type { Options } from 'amqplib';
3
- import type { ConsumeOptions } from './consume';
4
- import type { Exchange } from './exchange';
5
- import type { PublishOptions } from './publish';
6
- export type Connection = ReturnType<typeof connect>;
7
- export type ConnectionOptions = Options.Connect | string;
8
- export type ClientContext = {
9
- connection?: Connection;
10
- connectionOptions?: ConnectionOptions;
11
- exchange?: Exchange;
12
- publishOptions?: PublishOptions;
13
- consumeOptions?: ConsumeOptions;
14
- };
package/dist/type.js DELETED
@@ -1,2 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
package/dist/utils.d.ts DELETED
@@ -1,2 +0,0 @@
1
- declare const mergeOptions: import("smob").Merger;
2
- export { mergeOptions, };
package/dist/utils.js DELETED
@@ -1,23 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.mergeOptions = void 0;
4
- const smob_1 = require("smob");
5
- const exchange_1 = require("./exchange");
6
- const mergeOptions = (0, smob_1.createMerger)({
7
- priority: 'left',
8
- inPlace: false,
9
- clone: true,
10
- array: true,
11
- arrayDistinct: true,
12
- strategy(target, key, value) {
13
- if (key !== 'exchange') {
14
- return undefined;
15
- }
16
- if (!(0, exchange_1.isExchange)(target[key]) ||
17
- !(0, exchange_1.isExchange)(value)) {
18
- return undefined;
19
- }
20
- return target;
21
- },
22
- });
23
- exports.mergeOptions = mergeOptions;
package/release.config.js DELETED
@@ -1,3 +0,0 @@
1
- module.exports = {
2
- "extends": "@tada5hi/semantic-release"
3
- }
@@ -1,12 +0,0 @@
1
- {
2
- "extends": ["./tsconfig.json"],
3
- "include": [
4
- "src/**/*.ts",
5
- "test/**/*.ts",
6
- "release.config.js",
7
- "commitlint.config.js"
8
- ],
9
- "exclude": [
10
- "node_modules/**/*"
11
- ]
12
- }