amqp-extension 4.0.0 → 5.1.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 +328 -40
- package/dist/index.d.mts +356 -0
- package/dist/index.mjs +648 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +53 -39
- package/.editorconfig +0 -13
- package/.eslintrc +0 -27
- package/.husky/commit-msg +0 -4
- package/CHANGELOG.md +0 -149
- package/commitlint.config.js +0 -10
- package/dist/config/index.d.ts +0 -3
- package/dist/config/index.js +0 -25
- package/dist/config/module.d.ts +0 -2
- package/dist/config/module.js +0 -23
- package/dist/config/type.d.ts +0 -13
- package/dist/config/type.js +0 -8
- package/dist/consume/constants.d.ts +0 -1
- package/dist/consume/constants.js +0 -10
- package/dist/consume/index.d.ts +0 -3
- package/dist/consume/index.js +0 -25
- package/dist/consume/type.d.ts +0 -24
- package/dist/consume/type.js +0 -8
- package/dist/consume/utils.d.ts +0 -2
- package/dist/consume/utils.js +0 -16
- package/dist/exchange/constants.d.ts +0 -6
- package/dist/exchange/constants.js +0 -10
- package/dist/exchange/index.d.ts +0 -3
- package/dist/exchange/index.js +0 -19
- package/dist/exchange/type.d.ts +0 -8
- package/dist/exchange/type.js +0 -2
- package/dist/exchange/utils.d.ts +0 -4
- package/dist/exchange/utils.js +0 -13
- package/dist/index.d.ts +0 -6
- package/dist/index.js +0 -28
- package/dist/module.d.ts +0 -18
- package/dist/module.js +0 -187
- package/dist/publish/index.d.ts +0 -1
- package/dist/publish/index.js +0 -23
- package/dist/publish/type.d.ts +0 -7
- package/dist/publish/type.js +0 -8
- package/dist/type.d.ts +0 -14
- package/dist/type.js +0 -2
- package/dist/utils.d.ts +0 -2
- package/dist/utils.js +0 -23
- package/release.config.js +0 -3
- package/tsconfig.eslint.json +0 -12
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,356 @@
|
|
|
1
|
+
import { BaseError, ErrorInput } from "@ebec/core";
|
|
2
|
+
import { AmqpConnectionManagerOptions, connect } from "amqp-connection-manager";
|
|
3
|
+
import { Channel, ConsumeMessage, Options } from "amqplib";
|
|
4
|
+
//#region src/consume/constants.d.ts
|
|
5
|
+
export declare const ConsumeHandlerAnyKey = "$any";
|
|
6
|
+
//#endregion
|
|
7
|
+
//#region src/queue/types.d.ts
|
|
8
|
+
/**
|
|
9
|
+
* Options of a named, durable queue, accepted flat on both ConsumeOptions and PublishOptions.
|
|
10
|
+
* They map to queue arguments (x-*) and are not validated client-side:
|
|
11
|
+
* the broker rejects an invalid combination with a BrokerError.
|
|
12
|
+
* Publisher and consumer must pass the same options: a broker rejects
|
|
13
|
+
* re-declaring a queue with different arguments.
|
|
14
|
+
*/
|
|
15
|
+
export type QueueOptions = {
|
|
16
|
+
/**
|
|
17
|
+
* Queue type (x-queue-type).
|
|
18
|
+
*
|
|
19
|
+
* Default: the broker's default (classic)
|
|
20
|
+
*/
|
|
21
|
+
queueType?: 'classic' | 'quorum';
|
|
22
|
+
/**
|
|
23
|
+
* Drop or dead-letter a message after this many failed deliveries
|
|
24
|
+
* (x-delivery-limit). Quorum queues only. Counts requeued deliveries, so it needs
|
|
25
|
+
* requeueOnFailure: true (or a redelivery after a disconnect).
|
|
26
|
+
*/
|
|
27
|
+
deliveryLimit?: number;
|
|
28
|
+
/**
|
|
29
|
+
* Exchange that rejected or expired messages are republished to (x-dead-letter-exchange).
|
|
30
|
+
*/
|
|
31
|
+
deadLetterExchange?: string;
|
|
32
|
+
/**
|
|
33
|
+
* Routing key for dead-lettered messages, instead of their original one
|
|
34
|
+
* (x-dead-letter-routing-key).
|
|
35
|
+
*/
|
|
36
|
+
deadLetterRoutingKey?: string;
|
|
37
|
+
/**
|
|
38
|
+
* Expire messages after this many milliseconds in the queue (x-message-ttl).
|
|
39
|
+
*/
|
|
40
|
+
messageTtl?: number;
|
|
41
|
+
/**
|
|
42
|
+
* Maximum number of ready messages; the oldest are dropped or dead-lettered
|
|
43
|
+
* beyond it (x-max-length).
|
|
44
|
+
*/
|
|
45
|
+
maxLength?: number;
|
|
46
|
+
/**
|
|
47
|
+
* Deliver to one consumer at a time, in publish order, failing over to
|
|
48
|
+
* another consumer when it goes away (x-single-active-consumer).
|
|
49
|
+
*
|
|
50
|
+
* Default: false
|
|
51
|
+
*/
|
|
52
|
+
singleActiveConsumer?: boolean;
|
|
53
|
+
};
|
|
54
|
+
//#endregion
|
|
55
|
+
//#region src/publish/types.d.ts
|
|
56
|
+
/**
|
|
57
|
+
* The AMQP properties of a published message (type, messageId, headers, persistent, …).
|
|
58
|
+
*/
|
|
59
|
+
type PublishProperties = Options.Publish;
|
|
60
|
+
/**
|
|
61
|
+
* The properties a message is re-published with for a retry.
|
|
62
|
+
*/
|
|
63
|
+
export type RepublishOptions = PublishProperties;
|
|
64
|
+
export type PublishOptions = PublishProperties & QueueOptions & {
|
|
65
|
+
/**
|
|
66
|
+
* Reject with ClientError (PUBLISH_TIMEOUT) when the message is not
|
|
67
|
+
* handed to the broker within this many milliseconds, e.g. while the
|
|
68
|
+
* broker is unreachable and the connection manager is reconnecting.
|
|
69
|
+
*
|
|
70
|
+
* Default: undefined (wait for the reconnect)
|
|
71
|
+
*/
|
|
72
|
+
timeout?: number;
|
|
73
|
+
};
|
|
74
|
+
export type PublishContext = {
|
|
75
|
+
to: string;
|
|
76
|
+
data: any;
|
|
77
|
+
options?: PublishOptions;
|
|
78
|
+
};
|
|
79
|
+
//#endregion
|
|
80
|
+
//#region src/consume/types.d.ts
|
|
81
|
+
export type ConsumeMessageHandler = (message: ConsumeMessage, channel: Channel) => Promise<void> | void;
|
|
82
|
+
export type ConsumeHandlers = {
|
|
83
|
+
[ConsumeHandlerAnyKey]?: ConsumeMessageHandler;
|
|
84
|
+
} & {
|
|
85
|
+
[key: string]: ConsumeMessageHandler;
|
|
86
|
+
};
|
|
87
|
+
export type ConsumeRetryOptions = {
|
|
88
|
+
/**
|
|
89
|
+
* Delivery attempts before a failing message is dropped (acked, onError gets ATTEMPTS_EXHAUSTED).
|
|
90
|
+
*
|
|
91
|
+
* Default: 5
|
|
92
|
+
*/
|
|
93
|
+
maxAttempts?: number;
|
|
94
|
+
/**
|
|
95
|
+
* Base of the exponential backoff in ms, capped at 30s.
|
|
96
|
+
*
|
|
97
|
+
* Default: 1000
|
|
98
|
+
*/
|
|
99
|
+
delay?: number;
|
|
100
|
+
};
|
|
101
|
+
export type ConsumeOptions = Options.Consume & QueueOptions & {
|
|
102
|
+
/**
|
|
103
|
+
* Default: false
|
|
104
|
+
*/
|
|
105
|
+
requeueOnFailure?: boolean;
|
|
106
|
+
/**
|
|
107
|
+
* Default: undefined
|
|
108
|
+
*/
|
|
109
|
+
prefetchCount?: number;
|
|
110
|
+
/**
|
|
111
|
+
* Named exchanges only: consume through this durable, named queue instead of
|
|
112
|
+
* an exclusive, temporary one. Once declared by a consume, it keeps messages while
|
|
113
|
+
* no consumer runs, and consumers sharing it split the messages. An empty name
|
|
114
|
+
* throws. Ignored on the default exchange.
|
|
115
|
+
*
|
|
116
|
+
* Default: undefined
|
|
117
|
+
*/
|
|
118
|
+
queue?: string;
|
|
119
|
+
/**
|
|
120
|
+
* Retry a message whose handler threw: re-publish it to the queue it came from with
|
|
121
|
+
* the next attempt in its x-attempt header (after a backoff), then ack the original.
|
|
122
|
+
* Also guards against redeliveries of messages with a messageId: a copy of a message
|
|
123
|
+
* still being handled in this consumer is not run again, and a redelivery of one that
|
|
124
|
+
* is not (the consumer died mid-handling) counts as a failed attempt.
|
|
125
|
+
* Ignored with noAck. A missing handler is not retried.
|
|
126
|
+
*
|
|
127
|
+
* Default: undefined (no retry)
|
|
128
|
+
*/
|
|
129
|
+
retry?: ConsumeRetryOptions;
|
|
130
|
+
/**
|
|
131
|
+
* With retry: hold a redelivered copy of a message still being handled until the
|
|
132
|
+
* original settles, instead of acking it right away. Acking it would free the
|
|
133
|
+
* prefetch slot and let the next message of an ordered stream run next to the original.
|
|
134
|
+
*
|
|
135
|
+
* Default: false
|
|
136
|
+
*/
|
|
137
|
+
ordered?: boolean;
|
|
138
|
+
/**
|
|
139
|
+
* Called after a message was nacked, retried (or not settled, with noAck) because its handler threw
|
|
140
|
+
* (with that error) or no handler matched (with a ClientError, code HANDLER_MISSING), and after
|
|
141
|
+
* a message was dropped because its retry attempts were exhausted (with a ClientError, code
|
|
142
|
+
* ATTEMPTS_EXHAUSTED, the last handler error as cause).
|
|
143
|
+
* Its own errors are swallowed.
|
|
144
|
+
*/
|
|
145
|
+
onError?: (error: unknown, message: ConsumeMessage) => Promise<void> | void;
|
|
146
|
+
};
|
|
147
|
+
/**
|
|
148
|
+
* Resolved by consume(). `await using consumer = await client.consume(...)` cancels it when the scope ends.
|
|
149
|
+
*/
|
|
150
|
+
export type Consumer = {
|
|
151
|
+
/**
|
|
152
|
+
* Stop receiving messages, wait for the handlers still running (their messages are settled),
|
|
153
|
+
* then close the channel. Never rejects; calling it again returns the same promise.
|
|
154
|
+
*/
|
|
155
|
+
cancel(): Promise<void>;
|
|
156
|
+
[Symbol.asyncDispose](): Promise<void>;
|
|
157
|
+
};
|
|
158
|
+
export type ConsumeContext = {
|
|
159
|
+
from: string;
|
|
160
|
+
options?: ConsumeOptions;
|
|
161
|
+
handlers: ConsumeHandlers;
|
|
162
|
+
};
|
|
163
|
+
//#endregion
|
|
164
|
+
//#region src/consume/retry.d.ts
|
|
165
|
+
/**
|
|
166
|
+
* The 1-based delivery attempt of a re-published message.
|
|
167
|
+
*/
|
|
168
|
+
export declare const ATTEMPT_HEADER = "x-attempt";
|
|
169
|
+
/**
|
|
170
|
+
* The attempt a message is on. A missing or invalid header (a first delivery, an
|
|
171
|
+
* older publisher counting elsewhere) is attempt 1: at worst a message is retried
|
|
172
|
+
* more often, never dropped early.
|
|
173
|
+
*/
|
|
174
|
+
export declare function resolveAttempt(properties: {
|
|
175
|
+
headers?: Record<string, unknown>;
|
|
176
|
+
}): number;
|
|
177
|
+
//#endregion
|
|
178
|
+
//#region src/errors/base.d.ts
|
|
179
|
+
/**
|
|
180
|
+
* Root of every error thrown by amqp-extension.
|
|
181
|
+
*
|
|
182
|
+
* instanceof is duck-typed through @ebec/core's marker chain, so it also matches errors
|
|
183
|
+
* from a duplicate copy of the library. Every subclass overrides Symbol.hasInstance with
|
|
184
|
+
* its own marker, otherwise it would inherit this one and match every library error.
|
|
185
|
+
*/
|
|
186
|
+
export declare class AmqpExtensionError extends BaseError {
|
|
187
|
+
static [Symbol.hasInstance](input: unknown): boolean;
|
|
188
|
+
constructor(input?: ErrorInput);
|
|
189
|
+
}
|
|
190
|
+
//#endregion
|
|
191
|
+
//#region src/errors/types.d.ts
|
|
192
|
+
export type BrokerReply = {
|
|
193
|
+
replyCode: number;
|
|
194
|
+
classId?: number;
|
|
195
|
+
methodId?: number;
|
|
196
|
+
};
|
|
197
|
+
/**
|
|
198
|
+
* An amqplib error for a broker rejection (code = AMQP reply code).
|
|
199
|
+
*/
|
|
200
|
+
export type AmqpReplyError = Error & {
|
|
201
|
+
code: number;
|
|
202
|
+
classId?: number;
|
|
203
|
+
methodId?: number;
|
|
204
|
+
};
|
|
205
|
+
//#endregion
|
|
206
|
+
//#region src/errors/broker.d.ts
|
|
207
|
+
/**
|
|
208
|
+
* The broker rejected an operation, e.g. 406 PRECONDITION_FAILED or 403 ACCESS_REFUSED.
|
|
209
|
+
*/
|
|
210
|
+
export declare class BrokerError extends AmqpExtensionError {
|
|
211
|
+
static [Symbol.hasInstance](input: unknown): boolean;
|
|
212
|
+
readonly replyCode: number;
|
|
213
|
+
readonly classId?: number;
|
|
214
|
+
readonly methodId?: number;
|
|
215
|
+
constructor(input: ErrorInput, reply: BrokerReply);
|
|
216
|
+
toJSON(): {
|
|
217
|
+
replyCode: number;
|
|
218
|
+
classId: number | undefined;
|
|
219
|
+
methodId: number | undefined;
|
|
220
|
+
name: string;
|
|
221
|
+
message: string;
|
|
222
|
+
code: string;
|
|
223
|
+
cause?: unknown;
|
|
224
|
+
errors?: unknown[];
|
|
225
|
+
issues?: readonly import("@ebec/core").Issue[];
|
|
226
|
+
"@instanceof": string[];
|
|
227
|
+
};
|
|
228
|
+
static fromAmqp(err: AmqpReplyError): BrokerError;
|
|
229
|
+
}
|
|
230
|
+
//#endregion
|
|
231
|
+
//#region src/errors/client.d.ts
|
|
232
|
+
export declare class ClientError extends AmqpExtensionError {
|
|
233
|
+
static [Symbol.hasInstance](input: unknown): boolean;
|
|
234
|
+
constructor(input?: ErrorInput);
|
|
235
|
+
static closed(): ClientError;
|
|
236
|
+
static channelClosed(cause?: unknown): ClientError;
|
|
237
|
+
static handlerMissing(type?: string): ClientError;
|
|
238
|
+
static attemptsExhausted(type?: string, cause?: unknown): ClientError;
|
|
239
|
+
static timeout(cause?: unknown): ClientError;
|
|
240
|
+
}
|
|
241
|
+
//#endregion
|
|
242
|
+
//#region src/errors/serialization.d.ts
|
|
243
|
+
export declare class SerializationError extends AmqpExtensionError {
|
|
244
|
+
static [Symbol.hasInstance](input: unknown): boolean;
|
|
245
|
+
constructor(input?: ErrorInput);
|
|
246
|
+
static payload(cause?: unknown): SerializationError;
|
|
247
|
+
}
|
|
248
|
+
//#endregion
|
|
249
|
+
//#region src/errors/topology.d.ts
|
|
250
|
+
export declare class TopologyError extends AmqpExtensionError {
|
|
251
|
+
static [Symbol.hasInstance](input: unknown): boolean;
|
|
252
|
+
constructor(input?: ErrorInput);
|
|
253
|
+
static queueNameEmpty(): TopologyError;
|
|
254
|
+
static exchangeNameMissing(): TopologyError;
|
|
255
|
+
static destinationMissing(): TopologyError;
|
|
256
|
+
}
|
|
257
|
+
//#endregion
|
|
258
|
+
//#region src/errors/check.d.ts
|
|
259
|
+
export declare function isAmqpExtensionError(input: unknown): input is AmqpExtensionError;
|
|
260
|
+
export declare function isTopologyError(input: unknown): input is TopologyError;
|
|
261
|
+
export declare function isClientError(input: unknown): input is ClientError;
|
|
262
|
+
export declare function isBrokerError(input: unknown): input is BrokerError;
|
|
263
|
+
export declare function isSerializationError(input: unknown): input is SerializationError;
|
|
264
|
+
//#endregion
|
|
265
|
+
//#region src/errors/constants.d.ts
|
|
266
|
+
export declare enum ErrorCode {
|
|
267
|
+
QUEUE_NAME_EMPTY = "QUEUE_NAME_EMPTY",
|
|
268
|
+
EXCHANGE_NAME_MISSING = "EXCHANGE_NAME_MISSING",
|
|
269
|
+
DESTINATION_MISSING = "DESTINATION_MISSING",
|
|
270
|
+
CLIENT_CLOSED = "CLIENT_CLOSED",
|
|
271
|
+
CHANNEL_CLOSED = "CHANNEL_CLOSED",
|
|
272
|
+
PUBLISH_TIMEOUT = "PUBLISH_TIMEOUT",
|
|
273
|
+
HANDLER_MISSING = "HANDLER_MISSING",
|
|
274
|
+
ATTEMPTS_EXHAUSTED = "ATTEMPTS_EXHAUSTED",
|
|
275
|
+
BROKER_REJECTED = "BROKER_REJECTED",
|
|
276
|
+
SERIALIZATION_FAILED = "SERIALIZATION_FAILED"
|
|
277
|
+
}
|
|
278
|
+
export declare const AMQP_EXTENSION_ERROR_MARKER: unique symbol;
|
|
279
|
+
export declare const TOPOLOGY_ERROR_MARKER: unique symbol;
|
|
280
|
+
export declare const CLIENT_ERROR_MARKER: unique symbol;
|
|
281
|
+
export declare const BROKER_ERROR_MARKER: unique symbol;
|
|
282
|
+
export declare const SERIALIZATION_ERROR_MARKER: unique symbol;
|
|
283
|
+
//#endregion
|
|
284
|
+
//#region src/exchange/constants.d.ts
|
|
285
|
+
export declare enum ExchangeType {
|
|
286
|
+
FANOUT = "fanout",
|
|
287
|
+
DIRECT = "direct",
|
|
288
|
+
TOPIC = "topic",
|
|
289
|
+
HEADERS = "headers"
|
|
290
|
+
}
|
|
291
|
+
//#endregion
|
|
292
|
+
//#region src/exchange/types.d.ts
|
|
293
|
+
export type ExchangeOptions = Options.AssertExchange;
|
|
294
|
+
export type Exchange = {
|
|
295
|
+
type: `${ExchangeType}`;
|
|
296
|
+
name?: string;
|
|
297
|
+
options?: ExchangeOptions;
|
|
298
|
+
};
|
|
299
|
+
//#endregion
|
|
300
|
+
//#region src/exchange/utils.d.ts
|
|
301
|
+
export declare function isDefaultExchange(input?: `${ExchangeType}`): input is ExchangeType.DIRECT;
|
|
302
|
+
//#endregion
|
|
303
|
+
//#region src/types.d.ts
|
|
304
|
+
export type Connection = ReturnType<typeof connect>;
|
|
305
|
+
export type ConnectionOptions = Options.Connect | string;
|
|
306
|
+
export type ClientContext = {
|
|
307
|
+
connection?: Connection;
|
|
308
|
+
connectionOptions?: ConnectionOptions;
|
|
309
|
+
/**
|
|
310
|
+
* Passed to amqp-connection-manager's connect(), e.g. reconnectTimeInSeconds.
|
|
311
|
+
* Ignored when a connection is given.
|
|
312
|
+
*/
|
|
313
|
+
connectionManagerOptions?: AmqpConnectionManagerOptions;
|
|
314
|
+
exchange?: Exchange;
|
|
315
|
+
publishOptions?: PublishOptions;
|
|
316
|
+
consumeOptions?: ConsumeOptions;
|
|
317
|
+
};
|
|
318
|
+
//#endregion
|
|
319
|
+
//#region src/module.d.ts
|
|
320
|
+
export declare class Client {
|
|
321
|
+
readonly connection: Connection;
|
|
322
|
+
protected publishOptions: PublishOptions;
|
|
323
|
+
protected consumeOptions: ConsumeOptions;
|
|
324
|
+
protected exchange: Exchange;
|
|
325
|
+
constructor(ctx: ClientContext);
|
|
326
|
+
of(exchange: Exchange): Client;
|
|
327
|
+
/**
|
|
328
|
+
* Close the (shared) connection and all its channels.
|
|
329
|
+
*/
|
|
330
|
+
close(): Promise<void>;
|
|
331
|
+
/**
|
|
332
|
+
* `await using client = new Client(...)` closes the client when the scope ends.
|
|
333
|
+
* Like close(), it closes the shared connection, so use it on the root client, not on of() siblings.
|
|
334
|
+
*/
|
|
335
|
+
[Symbol.asyncDispose](): Promise<void>;
|
|
336
|
+
protected assertOpen(): void;
|
|
337
|
+
consume(ctx: ConsumeContext): Promise<Consumer>;
|
|
338
|
+
consume(from: string, handlers: ConsumeHandlers): Promise<Consumer>;
|
|
339
|
+
consume(from: string, options: ConsumeOptions, handlers: ConsumeHandlers): Promise<Consumer>;
|
|
340
|
+
/**
|
|
341
|
+
* Re-publish a message for a retry, through the default exchange by queue name:
|
|
342
|
+
* that reaches the queue whatever exchange delivered it, without re-declaring it.
|
|
343
|
+
* Its own channel, not the consume channel: that one may have been reset while
|
|
344
|
+
* the handler ran, and this one waits for the broker's confirm.
|
|
345
|
+
*
|
|
346
|
+
* ponytail: a server-named queue is gone after a reconnect, so a retry of a
|
|
347
|
+
* message from before it is unroutable and dropped by the broker.
|
|
348
|
+
*/
|
|
349
|
+
protected republish(queue: string, content: Buffer, options: RepublishOptions): Promise<void>;
|
|
350
|
+
publish(ctx: PublishContext): Promise<boolean>;
|
|
351
|
+
publish(to: string, data: any): Promise<boolean>;
|
|
352
|
+
publish(to: string, data: any, options: PublishOptions): Promise<boolean>;
|
|
353
|
+
}
|
|
354
|
+
//#endregion
|
|
355
|
+
export type { ConsumeMessage };
|
|
356
|
+
//# sourceMappingURL=index.d.mts.map
|