rxjs-rabbitmq 2.2.0 → 2.2.2
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 +463 -65
- package/dist/rabbitmq-client.helper.d.ts +57 -3
- package/dist/rabbitmq-client.helper.js +303 -71
- package/dist/rabbitmq-client.helper.js.map +1 -1
- package/dist/types.d.ts +56 -10
- package/dist/utils.d.ts +4 -16
- package/dist/utils.js +12 -3
- package/dist/utils.js.map +1 -1
- package/package.json +2 -2
- package/src/rabbitmq-client.helper.ts +392 -82
- package/src/types.ts +60 -10
- package/src/utils.ts +13 -4
- package/test/rabbitmq-client.test.js +115 -0
package/src/types.ts
CHANGED
|
@@ -11,10 +11,11 @@ import {
|
|
|
11
11
|
|
|
12
12
|
export type MapType<T = any> = { [key:string]: T };
|
|
13
13
|
|
|
14
|
-
export type RmqMessageTypeHandler = (event: RmqEventMessage) => Promise<void
|
|
14
|
+
export type RmqMessageTypeHandler = (event: RmqEventMessage) => Promise<void> | void;
|
|
15
15
|
export type RmqHandleMessageTypeConfig = { messageName: string, callbackHandler: RmqMessageTypeHandler };
|
|
16
16
|
export type RmqHandleMessageTypeConfigs = string[] | RmqHandleMessageTypeConfig[];
|
|
17
17
|
export type ExchangeType = 'direct' | 'topic' | 'headers' | 'fanout';
|
|
18
|
+
export type RmqPayload = unknown;
|
|
18
19
|
|
|
19
20
|
|
|
20
21
|
export interface QueueConfig {
|
|
@@ -98,40 +99,86 @@ export interface QueueExchangeBindingConfig {
|
|
|
98
99
|
routingKey: string
|
|
99
100
|
}
|
|
100
101
|
|
|
102
|
+
export type DeadLetterQueueConfig = {
|
|
103
|
+
sourceQueue: string,
|
|
104
|
+
deadLetterQueue?: string,
|
|
105
|
+
deadLetterExchange?: string,
|
|
106
|
+
deadLetterRoutingKey?: string,
|
|
107
|
+
queueOptions?: Options.AssertQueue,
|
|
108
|
+
deadLetterQueueOptions?: Options.AssertQueue,
|
|
109
|
+
maxLength?: number,
|
|
110
|
+
messageTtlMs?: number,
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
export type HandleMessageErrorOptions = {
|
|
114
|
+
reason?: string,
|
|
115
|
+
requeue?: boolean,
|
|
116
|
+
deadLetterQueue?: string,
|
|
117
|
+
deadLetterExchange?: string,
|
|
118
|
+
deadLetterRoutingKey?: string,
|
|
119
|
+
retryQueue?: string,
|
|
120
|
+
retryDelayMs?: number,
|
|
121
|
+
loggerMessage?: string,
|
|
122
|
+
};
|
|
123
|
+
|
|
101
124
|
export type RmqEventRequestResponse <T = any> = Promise<RmqEventMessage<T>>;
|
|
102
125
|
|
|
103
|
-
export type RmqSendMessageParams = {
|
|
126
|
+
export type RmqSendMessageParams<T = unknown> = {
|
|
104
127
|
queue: string,
|
|
105
|
-
data:
|
|
128
|
+
data: T,
|
|
106
129
|
publishOptions: Options.Publish
|
|
107
130
|
}
|
|
108
131
|
|
|
109
|
-
export type RmqSendRequestParams = {
|
|
132
|
+
export type RmqSendRequestParams<T = unknown> = {
|
|
110
133
|
queue: string,
|
|
111
|
-
data:
|
|
134
|
+
data: T,
|
|
112
135
|
publishOptions: Options.Publish,
|
|
113
136
|
}
|
|
114
137
|
|
|
115
|
-
export type RmqPublishEventParams<T =
|
|
138
|
+
export type RmqPublishEventParams<T = unknown> = {
|
|
116
139
|
exchange: string,
|
|
117
140
|
data: T,
|
|
118
141
|
routingKey?: string,
|
|
119
142
|
publishOptions: Options.Publish
|
|
120
143
|
}
|
|
121
144
|
|
|
122
|
-
export type RmqEventMessage<T =
|
|
145
|
+
export type RmqEventMessage<T = unknown> = {
|
|
123
146
|
data: T,
|
|
124
147
|
message: ConsumeMessage,
|
|
125
148
|
ack: () => void;
|
|
126
149
|
publishEvent: (options: RmqPublishEventParams) => void,
|
|
127
|
-
sendMessage: (options: RmqSendMessageParams) => void,
|
|
128
|
-
sendRequest: <
|
|
150
|
+
sendMessage: <U = unknown>(options: RmqSendMessageParams<U>) => void,
|
|
151
|
+
sendRequest: <U = unknown>(options: RmqSendRequestParams<U>) => Promise<RmqEventMessage<U>>,
|
|
129
152
|
};
|
|
130
153
|
|
|
131
|
-
export type RmqEventHandler = (event: RmqEventMessage) =>
|
|
154
|
+
export type RmqEventHandler = (event: RmqEventMessage) => unknown;
|
|
132
155
|
|
|
133
156
|
export type AckFn = (message: Message) => void;
|
|
134
157
|
|
|
158
|
+
export type RabbitMqLogger = {
|
|
159
|
+
debug: (...args: any[]) => void;
|
|
160
|
+
info: (...args: any[]) => void;
|
|
161
|
+
warn: (...args: any[]) => void;
|
|
162
|
+
error: (...args: any[]) => void;
|
|
163
|
+
};
|
|
164
|
+
|
|
165
|
+
export type RabbitMqMetrics = {
|
|
166
|
+
messagesReceived: number;
|
|
167
|
+
messagesPublished: number;
|
|
168
|
+
messagesAcked: number;
|
|
169
|
+
messagesNacked: number;
|
|
170
|
+
messagesRetried: number;
|
|
171
|
+
rpcRequests: number;
|
|
172
|
+
rpcResponses: number;
|
|
173
|
+
reconnects: number;
|
|
174
|
+
};
|
|
175
|
+
|
|
176
|
+
export type RabbitMqSerializer = {
|
|
177
|
+
serialize: (value: unknown) => Buffer;
|
|
178
|
+
deserialize: (value: Buffer, encoding?: BufferEncoding) => unknown;
|
|
179
|
+
};
|
|
180
|
+
|
|
181
|
+
export type RabbitMqSerializerMap = Record<string, RabbitMqSerializer>;
|
|
135
182
|
|
|
136
183
|
/**
|
|
137
184
|
|
|
@@ -145,6 +192,9 @@ export type RabbitMqInitConfig = {
|
|
|
145
192
|
dontSendToReplyQueueOnPublish?: boolean,
|
|
146
193
|
autoAckUnhandledMessageTypes?: boolean,
|
|
147
194
|
pushUnhandledMessageTypesToDefaultHandler?: boolean,
|
|
195
|
+
logger?: Partial<RabbitMqLogger>,
|
|
196
|
+
metrics?: boolean,
|
|
197
|
+
rpcRequestTimeoutMs?: number,
|
|
148
198
|
delayStart?: number,
|
|
149
199
|
connection_url: string,
|
|
150
200
|
retryAttempts: number,
|
package/src/utils.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { MapType } from "./types";
|
|
1
|
+
import { MapType, RabbitMqSerializer, RabbitMqSerializerMap } from "./types";
|
|
2
2
|
|
|
3
3
|
// https://github.com/Foo-Foo-MQ/foo-foo-mq/blob/main/src/index.js
|
|
4
|
-
export const SERIALIZERS = {
|
|
4
|
+
export const SERIALIZERS: RabbitMqSerializerMap = {
|
|
5
5
|
'application/json': {
|
|
6
6
|
deserialize: (bytes: any, encoding: any) => {
|
|
7
7
|
return JSON.parse(bytes.toString(encoding || 'utf8'));
|
|
@@ -31,12 +31,21 @@ export const SERIALIZERS = {
|
|
|
31
31
|
deserialize: (bytes: any, encoding: any) => {
|
|
32
32
|
return bytes.toString(encoding || 'utf8');
|
|
33
33
|
},
|
|
34
|
-
serialize: (
|
|
35
|
-
return Buffer.from(
|
|
34
|
+
serialize: (value: unknown) => {
|
|
35
|
+
return Buffer.from(String(value), 'utf8');
|
|
36
36
|
}
|
|
37
37
|
}
|
|
38
38
|
};
|
|
39
39
|
|
|
40
|
+
export const registerSerializer = (contentType: string, serializer: RabbitMqSerializer) => {
|
|
41
|
+
SERIALIZERS[contentType] = serializer;
|
|
42
|
+
return SERIALIZERS[contentType];
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
export const getSerializer = (contentType: string) => {
|
|
46
|
+
return SERIALIZERS[contentType] || null;
|
|
47
|
+
};
|
|
48
|
+
|
|
40
49
|
export const mapify = <T> (list: T[], key: string | number): MapType<T> => {
|
|
41
50
|
return list.reduce((map: MapType<T>, item: T) => {
|
|
42
51
|
map[ item[key] ] = item;
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
const test = require('node:test');
|
|
2
|
+
const assert = require('node:assert/strict');
|
|
3
|
+
const { RabbitMQClient, registerSerializer } = require('../dist');
|
|
4
|
+
|
|
5
|
+
test('client supports clean shutdown and metrics inspection without a live connection', async () => {
|
|
6
|
+
const client = new RabbitMQClient({
|
|
7
|
+
stopAutoInit: true,
|
|
8
|
+
metrics: true,
|
|
9
|
+
connection_url: 'amqp://localhost',
|
|
10
|
+
retryAttempts: 1,
|
|
11
|
+
retryDelay: 50,
|
|
12
|
+
queues: [],
|
|
13
|
+
exchanges: [],
|
|
14
|
+
bindings: []
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
assert.deepEqual(client.getMetrics(), {
|
|
18
|
+
messagesReceived: 0,
|
|
19
|
+
messagesPublished: 0,
|
|
20
|
+
messagesAcked: 0,
|
|
21
|
+
messagesNacked: 0,
|
|
22
|
+
messagesRetried: 0,
|
|
23
|
+
rpcRequests: 0,
|
|
24
|
+
rpcResponses: 0,
|
|
25
|
+
reconnects: 0
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
await assert.doesNotReject(() => client.close());
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
test('custom serializers can be registered and used', () => {
|
|
32
|
+
const contentType = 'application/test+json';
|
|
33
|
+
|
|
34
|
+
const serializer = {
|
|
35
|
+
serialize: (value) => Buffer.from(JSON.stringify(value)),
|
|
36
|
+
deserialize: (bytes) => JSON.parse(bytes.toString('utf8'))
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
assert.doesNotThrow(() => {
|
|
40
|
+
registerSerializer(contentType, serializer);
|
|
41
|
+
RabbitMQClient.registerSerializer(contentType, serializer);
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
const payload = { ok: true, count: 2 };
|
|
45
|
+
assert.deepEqual(
|
|
46
|
+
registerSerializer(contentType, serializer).deserialize(registerSerializer(contentType, serializer).serialize(payload)),
|
|
47
|
+
payload
|
|
48
|
+
);
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
test('dead letter setup and error handling can be configured through the client helper', async () => {
|
|
52
|
+
const client = new RabbitMQClient({
|
|
53
|
+
stopAutoInit: true,
|
|
54
|
+
metrics: false,
|
|
55
|
+
connection_url: 'amqp://localhost',
|
|
56
|
+
retryAttempts: 1,
|
|
57
|
+
retryDelay: 10,
|
|
58
|
+
queues: [],
|
|
59
|
+
exchanges: [],
|
|
60
|
+
bindings: []
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
const calls = [];
|
|
64
|
+
client.channel = {
|
|
65
|
+
assertExchange: async (name, type, options) => {
|
|
66
|
+
calls.push(['assertExchange', name, type, options]);
|
|
67
|
+
return { exchange: name };
|
|
68
|
+
},
|
|
69
|
+
assertQueue: async (name, options) => {
|
|
70
|
+
calls.push(['assertQueue', name, options]);
|
|
71
|
+
return { queue: name, messageCount: 0, consumerCount: 0 };
|
|
72
|
+
},
|
|
73
|
+
bindQueue: async (queue, exchange, routingKey) => {
|
|
74
|
+
calls.push(['bindQueue', queue, exchange, routingKey]);
|
|
75
|
+
return {};
|
|
76
|
+
},
|
|
77
|
+
sendToQueue: (queue, content, options) => {
|
|
78
|
+
calls.push(['sendToQueue', queue, content, options]);
|
|
79
|
+
return {};
|
|
80
|
+
},
|
|
81
|
+
nack: (message, allUpTo, requeue) => {
|
|
82
|
+
calls.push(['nack', message, allUpTo, requeue]);
|
|
83
|
+
},
|
|
84
|
+
ack: (message) => {
|
|
85
|
+
calls.push(['ack', message]);
|
|
86
|
+
}
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
const result = await client.setupDeadLetterQueue({
|
|
90
|
+
sourceQueue: 'jobs',
|
|
91
|
+
deadLetterQueue: 'jobs.dlq',
|
|
92
|
+
deadLetterExchange: 'jobs.dlx',
|
|
93
|
+
deadLetterRoutingKey: 'jobs.dlq',
|
|
94
|
+
queueOptions: { durable: true },
|
|
95
|
+
deadLetterQueueOptions: { durable: true },
|
|
96
|
+
messageTtlMs: 5000,
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
assert.equal(result.deadLetterQueue, 'jobs.dlq');
|
|
100
|
+
assert.equal(result.deadLetterExchange, 'jobs.dlx');
|
|
101
|
+
|
|
102
|
+
const message = {
|
|
103
|
+
fields: { routingKey: 'jobs' },
|
|
104
|
+
properties: { headers: {}, contentType: 'application/json' },
|
|
105
|
+
content: Buffer.from('boom')
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
const outcome = client.handleMessageError(message, new Error('failed processing'), {
|
|
109
|
+
deadLetterQueue: 'jobs.dlq',
|
|
110
|
+
reason: 'processing failed'
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
assert.equal(outcome.action, 'dead-letter');
|
|
114
|
+
assert.equal(calls.some((call) => call[0] === 'sendToQueue' && call[1] === 'jobs.dlq'), true);
|
|
115
|
+
});
|