@zdavison/matador 2.0.1
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/cli.ts +453 -0
- package/dist/checkpoint/context.d.ts +59 -0
- package/dist/checkpoint/context.d.ts.map +1 -0
- package/dist/checkpoint/context.js +140 -0
- package/dist/checkpoint/context.test.d.ts +2 -0
- package/dist/checkpoint/context.test.d.ts.map +1 -0
- package/dist/checkpoint/context.test.js +424 -0
- package/dist/checkpoint/index.d.ts +7 -0
- package/dist/checkpoint/index.d.ts.map +1 -0
- package/dist/checkpoint/index.js +6 -0
- package/dist/checkpoint/stores/memory.d.ts +29 -0
- package/dist/checkpoint/stores/memory.d.ts.map +1 -0
- package/dist/checkpoint/stores/memory.js +39 -0
- package/dist/checkpoint/stores/noop.d.ts +14 -0
- package/dist/checkpoint/stores/noop.d.ts.map +1 -0
- package/dist/checkpoint/stores/noop.js +18 -0
- package/dist/checkpoint/stores/stores.test.d.ts +2 -0
- package/dist/checkpoint/stores/stores.test.d.ts.map +1 -0
- package/dist/checkpoint/stores/stores.test.js +146 -0
- package/dist/checkpoint/types.d.ts +119 -0
- package/dist/checkpoint/types.d.ts.map +1 -0
- package/dist/checkpoint/types.js +1 -0
- package/dist/codec/codec.d.ts +29 -0
- package/dist/codec/codec.d.ts.map +1 -0
- package/dist/codec/codec.js +15 -0
- package/dist/codec/header-aware-codec.d.ts +36 -0
- package/dist/codec/header-aware-codec.d.ts.map +1 -0
- package/dist/codec/header-aware-codec.js +1 -0
- package/dist/codec/index.d.ts +6 -0
- package/dist/codec/index.d.ts.map +1 -0
- package/dist/codec/index.js +3 -0
- package/dist/codec/json-codec.d.ts +13 -0
- package/dist/codec/json-codec.d.ts.map +1 -0
- package/dist/codec/json-codec.js +64 -0
- package/dist/codec/rabbitmq-codec.d.ts +28 -0
- package/dist/codec/rabbitmq-codec.d.ts.map +1 -0
- package/dist/codec/rabbitmq-codec.js +242 -0
- package/dist/codec/rabbitmq-codec.test.d.ts +2 -0
- package/dist/codec/rabbitmq-codec.test.d.ts.map +1 -0
- package/dist/codec/rabbitmq-codec.test.js +433 -0
- package/dist/core/fanout.d.ts +59 -0
- package/dist/core/fanout.d.ts.map +1 -0
- package/dist/core/fanout.js +121 -0
- package/dist/core/fanout.test.d.ts +2 -0
- package/dist/core/fanout.test.d.ts.map +1 -0
- package/dist/core/fanout.test.js +1055 -0
- package/dist/core/index.d.ts +7 -0
- package/dist/core/index.d.ts.map +1 -0
- package/dist/core/index.js +3 -0
- package/dist/core/matador.d.ts +133 -0
- package/dist/core/matador.d.ts.map +1 -0
- package/dist/core/matador.js +222 -0
- package/dist/core/matador.test.d.ts +2 -0
- package/dist/core/matador.test.d.ts.map +1 -0
- package/dist/core/matador.test.js +446 -0
- package/dist/core/shutdown.d.ts +78 -0
- package/dist/core/shutdown.d.ts.map +1 -0
- package/dist/core/shutdown.js +111 -0
- package/dist/core/shutdown.test.d.ts +2 -0
- package/dist/core/shutdown.test.d.ts.map +1 -0
- package/dist/core/shutdown.test.js +524 -0
- package/dist/errors/checkpoint-errors.d.ts +30 -0
- package/dist/errors/checkpoint-errors.d.ts.map +1 -0
- package/dist/errors/checkpoint-errors.js +49 -0
- package/dist/errors/has-description.d.ts +18 -0
- package/dist/errors/has-description.d.ts.map +1 -0
- package/dist/errors/has-description.js +9 -0
- package/dist/errors/index.d.ts +6 -0
- package/dist/errors/index.d.ts.map +1 -0
- package/dist/errors/index.js +22 -0
- package/dist/errors/matador-errors.d.ts +183 -0
- package/dist/errors/matador-errors.d.ts.map +1 -0
- package/dist/errors/matador-errors.js +376 -0
- package/dist/errors/retry-errors.d.ts +67 -0
- package/dist/errors/retry-errors.d.ts.map +1 -0
- package/dist/errors/retry-errors.js +108 -0
- package/dist/hooks/index.d.ts +4 -0
- package/dist/hooks/index.d.ts.map +1 -0
- package/dist/hooks/index.js +2 -0
- package/dist/hooks/safe-hooks.d.ts +34 -0
- package/dist/hooks/safe-hooks.d.ts.map +1 -0
- package/dist/hooks/safe-hooks.js +135 -0
- package/dist/hooks/types.d.ts +189 -0
- package/dist/hooks/types.d.ts.map +1 -0
- package/dist/hooks/types.js +9 -0
- package/dist/index.cjs +3156 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +23 -0
- package/dist/index.d.ts +23 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +31 -0
- package/dist/index.js.map +1 -0
- package/dist/pipeline/index.d.ts +3 -0
- package/dist/pipeline/index.d.ts.map +1 -0
- package/dist/pipeline/index.js +1 -0
- package/dist/pipeline/pipeline.d.ts +60 -0
- package/dist/pipeline/pipeline.d.ts.map +1 -0
- package/dist/pipeline/pipeline.js +261 -0
- package/dist/pipeline/pipeline.test.d.ts +2 -0
- package/dist/pipeline/pipeline.test.d.ts.map +1 -0
- package/dist/pipeline/pipeline.test.js +1065 -0
- package/dist/retry/index.d.ts +4 -0
- package/dist/retry/index.d.ts.map +1 -0
- package/dist/retry/index.js +1 -0
- package/dist/retry/policy.d.ts +43 -0
- package/dist/retry/policy.d.ts.map +1 -0
- package/dist/retry/policy.js +1 -0
- package/dist/retry/standard-policy.d.ts +44 -0
- package/dist/retry/standard-policy.d.ts.map +1 -0
- package/dist/retry/standard-policy.js +102 -0
- package/dist/retry/standard-policy.test.d.ts +2 -0
- package/dist/retry/standard-policy.test.d.ts.map +1 -0
- package/dist/retry/standard-policy.test.js +190 -0
- package/dist/schema/index.d.ts +4 -0
- package/dist/schema/index.d.ts.map +1 -0
- package/dist/schema/index.js +2 -0
- package/dist/schema/registry.d.ts +63 -0
- package/dist/schema/registry.d.ts.map +1 -0
- package/dist/schema/registry.js +171 -0
- package/dist/schema/registry.test.d.ts +2 -0
- package/dist/schema/registry.test.d.ts.map +1 -0
- package/dist/schema/registry.test.js +278 -0
- package/dist/schema/types.d.ts +158 -0
- package/dist/schema/types.d.ts.map +1 -0
- package/dist/schema/types.js +74 -0
- package/dist/schema/types.test.d.ts +2 -0
- package/dist/schema/types.test.d.ts.map +1 -0
- package/dist/schema/types.test.js +243 -0
- package/dist/topology/builder.d.ts +66 -0
- package/dist/topology/builder.d.ts.map +1 -0
- package/dist/topology/builder.js +156 -0
- package/dist/topology/builder.test.d.ts +2 -0
- package/dist/topology/builder.test.d.ts.map +1 -0
- package/dist/topology/builder.test.js +222 -0
- package/dist/topology/index.d.ts +5 -0
- package/dist/topology/index.d.ts.map +1 -0
- package/dist/topology/index.js +2 -0
- package/dist/topology/types.d.ts +76 -0
- package/dist/topology/types.d.ts.map +1 -0
- package/dist/topology/types.js +18 -0
- package/dist/transport/capabilities.d.ts +66 -0
- package/dist/transport/capabilities.d.ts.map +1 -0
- package/dist/transport/capabilities.js +18 -0
- package/dist/transport/connection-manager.d.ts +95 -0
- package/dist/transport/connection-manager.d.ts.map +1 -0
- package/dist/transport/connection-manager.js +144 -0
- package/dist/transport/index.d.ts +11 -0
- package/dist/transport/index.d.ts.map +1 -0
- package/dist/transport/index.js +5 -0
- package/dist/transport/local/local-transport.d.ts +62 -0
- package/dist/transport/local/local-transport.d.ts.map +1 -0
- package/dist/transport/local/local-transport.js +241 -0
- package/dist/transport/local/local-transport.test.d.ts +2 -0
- package/dist/transport/local/local-transport.test.d.ts.map +1 -0
- package/dist/transport/local/local-transport.test.js +192 -0
- package/dist/transport/multi/multi-transport.d.ts +94 -0
- package/dist/transport/multi/multi-transport.d.ts.map +1 -0
- package/dist/transport/multi/multi-transport.js +184 -0
- package/dist/transport/multi/multi-transport.test.d.ts +2 -0
- package/dist/transport/multi/multi-transport.test.d.ts.map +1 -0
- package/dist/transport/multi/multi-transport.test.js +236 -0
- package/dist/transport/rabbitmq/rabbitmq-transport.d.ts +73 -0
- package/dist/transport/rabbitmq/rabbitmq-transport.d.ts.map +1 -0
- package/dist/transport/rabbitmq/rabbitmq-transport.js +518 -0
- package/dist/transport/transport.d.ts +152 -0
- package/dist/transport/transport.d.ts.map +1 -0
- package/dist/transport/transport.js +1 -0
- package/dist/types/common.d.ts +41 -0
- package/dist/types/common.d.ts.map +1 -0
- package/dist/types/common.js +12 -0
- package/dist/types/envelope.d.ts +82 -0
- package/dist/types/envelope.d.ts.map +1 -0
- package/dist/types/envelope.js +36 -0
- package/dist/types/event.d.ts +96 -0
- package/dist/types/event.d.ts.map +1 -0
- package/dist/types/event.js +26 -0
- package/dist/types/event.test.d.ts +2 -0
- package/dist/types/event.test.d.ts.map +1 -0
- package/dist/types/event.test.js +130 -0
- package/dist/types/index.d.ts +9 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/index.js +4 -0
- package/dist/types/subscriber.d.ts +207 -0
- package/dist/types/subscriber.d.ts.map +1 -0
- package/dist/types/subscriber.js +99 -0
- package/examples/config.ts +126 -0
- package/examples/event.ts +26 -0
- package/examples/order-event.json +19 -0
- package/package.json +66 -0
- package/src/checkpoint/context.test.ts +510 -0
- package/src/checkpoint/context.ts +213 -0
- package/src/checkpoint/index.ts +30 -0
- package/src/checkpoint/stores/memory.ts +47 -0
- package/src/checkpoint/stores/noop.ts +22 -0
- package/src/checkpoint/stores/stores.test.ts +177 -0
- package/src/checkpoint/types.ts +147 -0
- package/src/codec/codec.ts +42 -0
- package/src/codec/header-aware-codec.ts +41 -0
- package/src/codec/index.ts +11 -0
- package/src/codec/json-codec.ts +69 -0
- package/src/codec/rabbitmq-codec.test.ts +516 -0
- package/src/codec/rabbitmq-codec.ts +336 -0
- package/src/core/fanout.test.ts +1351 -0
- package/src/core/fanout.ts +184 -0
- package/src/core/index.ts +12 -0
- package/src/core/matador.test.ts +575 -0
- package/src/core/matador.ts +344 -0
- package/src/core/shutdown.test.ts +853 -0
- package/src/core/shutdown.ts +165 -0
- package/src/errors/checkpoint-errors.ts +62 -0
- package/src/errors/has-description.ts +25 -0
- package/src/errors/index.ts +57 -0
- package/src/errors/matador-errors.ts +477 -0
- package/src/errors/retry-errors.ts +134 -0
- package/src/hooks/index.ts +15 -0
- package/src/hooks/safe-hooks.ts +223 -0
- package/src/hooks/types.ts +248 -0
- package/src/index.ts +231 -0
- package/src/pipeline/index.ts +2 -0
- package/src/pipeline/pipeline.test.ts +1317 -0
- package/src/pipeline/pipeline.ts +371 -0
- package/src/retry/index.ts +4 -0
- package/src/retry/policy.ts +46 -0
- package/src/retry/standard-policy.test.ts +282 -0
- package/src/retry/standard-policy.ts +156 -0
- package/src/schema/index.ts +16 -0
- package/src/schema/registry.test.ts +339 -0
- package/src/schema/registry.ts +226 -0
- package/src/schema/types.test.ts +281 -0
- package/src/schema/types.ts +217 -0
- package/src/topology/builder.test.ts +275 -0
- package/src/topology/builder.ts +199 -0
- package/src/topology/index.ts +15 -0
- package/src/topology/types.ts +109 -0
- package/src/transport/capabilities.ts +88 -0
- package/src/transport/connection-manager.ts +218 -0
- package/src/transport/index.ts +42 -0
- package/src/transport/local/local-transport.test.ts +262 -0
- package/src/transport/local/local-transport.ts +327 -0
- package/src/transport/multi/multi-transport.test.ts +320 -0
- package/src/transport/multi/multi-transport.ts +294 -0
- package/src/transport/rabbitmq/rabbitmq-transport.ts +753 -0
- package/src/transport/transport.ts +200 -0
- package/src/types/common.ts +53 -0
- package/src/types/envelope.ts +152 -0
- package/src/types/event.test.ts +157 -0
- package/src/types/event.ts +118 -0
- package/src/types/index.ts +52 -0
- package/src/types/subscriber.ts +310 -0
- package/test/e2e/multi-transport.e2e.test.ts +236 -0
- package/test/e2e/rabbitmq-transport.e2e.test.ts +327 -0
- package/test/e2e/transport-compliance.e2e.test.ts +505 -0
- package/test/integration/matador.integration.test.ts +634 -0
- package/tsconfig.json +29 -0
- package/tsconfig.tsbuildinfo +1 -0
- package/tsup.config.ts +13 -0
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Transport-agnostic topology definition.
|
|
3
|
+
* Matador owns the topology; transports translate and apply it.
|
|
4
|
+
*/
|
|
5
|
+
export interface Topology {
|
|
6
|
+
/** Namespace prefix for all queues */
|
|
7
|
+
readonly namespace: string;
|
|
8
|
+
|
|
9
|
+
/** Work queues for processing events */
|
|
10
|
+
readonly queues: readonly QueueDefinition[];
|
|
11
|
+
|
|
12
|
+
/** Dead-letter queue configuration */
|
|
13
|
+
readonly deadLetter: DeadLetterConfig;
|
|
14
|
+
|
|
15
|
+
/** Retry queue configuration */
|
|
16
|
+
readonly retry: RetryConfig;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Individual queue definition.
|
|
21
|
+
*/
|
|
22
|
+
export interface QueueDefinition {
|
|
23
|
+
/** Queue name (will be prefixed with namespace) */
|
|
24
|
+
readonly name: string;
|
|
25
|
+
|
|
26
|
+
/** Concurrency for this queue */
|
|
27
|
+
readonly concurrency?: number | undefined;
|
|
28
|
+
|
|
29
|
+
/** Consumer timeout in milliseconds */
|
|
30
|
+
readonly consumerTimeout?: number | undefined;
|
|
31
|
+
|
|
32
|
+
/** Enable priority support if transport allows */
|
|
33
|
+
readonly priorities?: boolean | undefined;
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* When true, the queue name is used exactly as provided without any
|
|
37
|
+
* modification. The namespace prefix will NOT be added, and no other
|
|
38
|
+
* transformations will be applied. Use this for referencing external
|
|
39
|
+
* queues that are not managed by Matador.
|
|
40
|
+
*/
|
|
41
|
+
readonly exact?: boolean | undefined;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Dead-letter queue configuration.
|
|
46
|
+
*/
|
|
47
|
+
export interface DeadLetterConfig {
|
|
48
|
+
/** Unhandled events (schema mismatch) queue */
|
|
49
|
+
readonly unhandled: DeadLetterQueueConfig;
|
|
50
|
+
|
|
51
|
+
/** Undeliverable events (permanent failures) queue */
|
|
52
|
+
readonly undeliverable: DeadLetterQueueConfig;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Configuration for a specific dead-letter queue.
|
|
57
|
+
*/
|
|
58
|
+
export interface DeadLetterQueueConfig {
|
|
59
|
+
/** Whether this DLQ is enabled */
|
|
60
|
+
readonly enabled: boolean;
|
|
61
|
+
|
|
62
|
+
/** Maximum number of messages in the DLQ */
|
|
63
|
+
readonly maxLength?: number | undefined;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Retry queue configuration.
|
|
68
|
+
*/
|
|
69
|
+
export interface RetryConfig {
|
|
70
|
+
/** Enable retry queue with delay */
|
|
71
|
+
readonly enabled: boolean;
|
|
72
|
+
|
|
73
|
+
/** Default retry delay in milliseconds */
|
|
74
|
+
readonly defaultDelayMs: number;
|
|
75
|
+
|
|
76
|
+
/** Maximum retry delay in milliseconds */
|
|
77
|
+
readonly maxDelayMs: number;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Gets the fully qualified queue name with namespace prefix.
|
|
82
|
+
*/
|
|
83
|
+
export function getQualifiedQueueName(
|
|
84
|
+
namespace: string,
|
|
85
|
+
queueName: string,
|
|
86
|
+
): string {
|
|
87
|
+
return `${namespace}.${queueName}`;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Gets the dead-letter queue name for a given queue.
|
|
92
|
+
*/
|
|
93
|
+
export function getDeadLetterQueueName(
|
|
94
|
+
namespace: string,
|
|
95
|
+
queueName: string,
|
|
96
|
+
dlqType: 'unhandled' | 'undeliverable',
|
|
97
|
+
): string {
|
|
98
|
+
return `${namespace}.${queueName}.${dlqType}`;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Gets the retry queue name for a given queue.
|
|
103
|
+
*/
|
|
104
|
+
export function getRetryQueueName(
|
|
105
|
+
namespace: string,
|
|
106
|
+
queueName: string,
|
|
107
|
+
): string {
|
|
108
|
+
return `${namespace}.${queueName}.retry`;
|
|
109
|
+
}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import type { DeliveryMode } from '../types/index.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Describes the native capabilities of a transport.
|
|
5
|
+
* Matador adapts its behavior based on these capabilities.
|
|
6
|
+
*/
|
|
7
|
+
export interface TransportCapabilities {
|
|
8
|
+
/**
|
|
9
|
+
* Delivery semantics the transport supports.
|
|
10
|
+
* - 'at-least-once': Ack after processing (may redeliver on failure)
|
|
11
|
+
* - 'at-most-once': Ack before processing (no redelivery, may lose messages)
|
|
12
|
+
*/
|
|
13
|
+
readonly deliveryModes: readonly DeliveryMode[];
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Transport can delay message delivery natively.
|
|
17
|
+
* - true: Use transport's native delay (BullMQ, RabbitMQ with plugin)
|
|
18
|
+
* - false: Matador handles via retry queue with TTL or external scheduler
|
|
19
|
+
*/
|
|
20
|
+
readonly delayedMessages: boolean;
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Transport can route failed messages to dead-letter queue natively.
|
|
24
|
+
* - 'native': Transport handles DL routing (RabbitMQ DLX)
|
|
25
|
+
* - 'manual': Matador must send() to DLQ then complete() original
|
|
26
|
+
* - 'none': No DL support, Matador logs and discards
|
|
27
|
+
*/
|
|
28
|
+
readonly deadLetterRouting: 'native' | 'manual' | 'none';
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Transport tracks delivery/attempt count natively.
|
|
32
|
+
* - true: Receipt includes accurate attemptNumber from transport
|
|
33
|
+
* - false: Matador tracks attempts in envelope.docket.attempts field
|
|
34
|
+
*/
|
|
35
|
+
readonly attemptTracking: boolean;
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* How transport handles concurrency.
|
|
39
|
+
* - 'prefetch': Channel-based prefetch (RabbitMQ)
|
|
40
|
+
* - 'worker': Worker concurrency setting (BullMQ)
|
|
41
|
+
* - 'partition': Partition-based parallelism (Kafka)
|
|
42
|
+
* - 'none': No concurrency control (Memory)
|
|
43
|
+
*/
|
|
44
|
+
readonly concurrencyModel: 'prefetch' | 'worker' | 'partition' | 'none';
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Message ordering guarantees.
|
|
48
|
+
* - 'none': No ordering guarantee (most transports with multiple consumers)
|
|
49
|
+
* - 'queue': Ordered within queue (single consumer scenarios)
|
|
50
|
+
* - 'partition': Ordered within partition/key (Kafka)
|
|
51
|
+
*/
|
|
52
|
+
readonly ordering: 'none' | 'queue' | 'partition';
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Transport supports message priority.
|
|
56
|
+
* - true: Higher priority messages processed first
|
|
57
|
+
* - false: FIFO only
|
|
58
|
+
*/
|
|
59
|
+
readonly priorities: boolean;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Checks if the transport supports a specific delivery mode.
|
|
64
|
+
*/
|
|
65
|
+
export function supportsDeliveryMode(
|
|
66
|
+
capabilities: TransportCapabilities,
|
|
67
|
+
mode: DeliveryMode,
|
|
68
|
+
): boolean {
|
|
69
|
+
return capabilities.deliveryModes.includes(mode);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Checks if the transport supports native delayed messages.
|
|
74
|
+
*/
|
|
75
|
+
export function supportsDelayedMessages(
|
|
76
|
+
capabilities: TransportCapabilities,
|
|
77
|
+
): boolean {
|
|
78
|
+
return capabilities.delayedMessages;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Checks if the transport has native dead-letter routing.
|
|
83
|
+
*/
|
|
84
|
+
export function hasNativeDeadLetter(
|
|
85
|
+
capabilities: TransportCapabilities,
|
|
86
|
+
): boolean {
|
|
87
|
+
return capabilities.deadLetterRouting === 'native';
|
|
88
|
+
}
|
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Connection state machine states.
|
|
3
|
+
*/
|
|
4
|
+
export type ConnectionState =
|
|
5
|
+
| { readonly status: 'disconnected' }
|
|
6
|
+
| { readonly status: 'connecting'; readonly attempt: number }
|
|
7
|
+
| { readonly status: 'connected' }
|
|
8
|
+
| {
|
|
9
|
+
readonly status: 'reconnecting';
|
|
10
|
+
readonly attempt: number;
|
|
11
|
+
readonly lastError: Error;
|
|
12
|
+
}
|
|
13
|
+
| { readonly status: 'failed'; readonly error: Error };
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Configuration for the connection manager.
|
|
17
|
+
*/
|
|
18
|
+
export interface ConnectionManagerConfig {
|
|
19
|
+
/** Maximum reconnection attempts before giving up (0 = infinite) */
|
|
20
|
+
readonly maxReconnectAttempts: number;
|
|
21
|
+
|
|
22
|
+
/** Initial delay between reconnection attempts (ms) */
|
|
23
|
+
readonly initialReconnectDelay: number;
|
|
24
|
+
|
|
25
|
+
/** Maximum delay between reconnection attempts (ms) */
|
|
26
|
+
readonly maxReconnectDelay: number;
|
|
27
|
+
|
|
28
|
+
/** Backoff multiplier for reconnection delay */
|
|
29
|
+
readonly backoffMultiplier: number;
|
|
30
|
+
|
|
31
|
+
/** Heartbeat interval for connection health (ms), optional */
|
|
32
|
+
readonly heartbeatInterval?: number;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Default configuration values.
|
|
37
|
+
*/
|
|
38
|
+
export const defaultConnectionConfig: ConnectionManagerConfig = {
|
|
39
|
+
maxReconnectAttempts: 0, // Infinite
|
|
40
|
+
initialReconnectDelay: 1000,
|
|
41
|
+
maxReconnectDelay: 30000,
|
|
42
|
+
backoffMultiplier: 2,
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Callback for state change notifications.
|
|
47
|
+
*/
|
|
48
|
+
export type StateChangeCallback = (state: ConnectionState) => void;
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Function that performs the actual connection.
|
|
52
|
+
*/
|
|
53
|
+
export type ConnectFn = () => Promise<void>;
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Function that performs disconnection.
|
|
57
|
+
*/
|
|
58
|
+
export type DisconnectFn = () => Promise<void>;
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Manages connection lifecycle with automatic reconnection.
|
|
62
|
+
*/
|
|
63
|
+
export class ConnectionManager {
|
|
64
|
+
private _state: ConnectionState = { status: 'disconnected' };
|
|
65
|
+
private readonly listeners: Set<StateChangeCallback> = new Set();
|
|
66
|
+
private reconnectTimer: ReturnType<typeof setTimeout> | null = null;
|
|
67
|
+
private readonly config: ConnectionManagerConfig;
|
|
68
|
+
|
|
69
|
+
constructor(
|
|
70
|
+
private readonly connectFn: ConnectFn,
|
|
71
|
+
private readonly disconnectFn: DisconnectFn,
|
|
72
|
+
config: Partial<ConnectionManagerConfig> = {},
|
|
73
|
+
) {
|
|
74
|
+
this.config = { ...defaultConnectionConfig, ...config };
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Current connection state.
|
|
79
|
+
*/
|
|
80
|
+
get state(): ConnectionState {
|
|
81
|
+
return this._state;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Whether currently connected.
|
|
86
|
+
*/
|
|
87
|
+
isConnected(): boolean {
|
|
88
|
+
return this._state.status === 'connected';
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Register a callback for connection state changes.
|
|
93
|
+
*/
|
|
94
|
+
onStateChange(callback: StateChangeCallback): () => void {
|
|
95
|
+
this.listeners.add(callback);
|
|
96
|
+
return () => this.listeners.delete(callback);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Attempt to establish connection.
|
|
101
|
+
*/
|
|
102
|
+
async connect(): Promise<void> {
|
|
103
|
+
if (this._state.status === 'connected') {
|
|
104
|
+
return;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
this.setState({ status: 'connecting', attempt: 1 });
|
|
108
|
+
|
|
109
|
+
try {
|
|
110
|
+
await this.connectFn();
|
|
111
|
+
this.setState({ status: 'connected' });
|
|
112
|
+
} catch (error) {
|
|
113
|
+
const err = error instanceof Error ? error : new Error(String(error));
|
|
114
|
+
await this.handleConnectionFailure(err, 1);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Gracefully disconnect.
|
|
120
|
+
*/
|
|
121
|
+
async disconnect(): Promise<void> {
|
|
122
|
+
this.clearReconnectTimer();
|
|
123
|
+
|
|
124
|
+
if (this._state.status === 'disconnected') {
|
|
125
|
+
return;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
try {
|
|
129
|
+
await this.disconnectFn();
|
|
130
|
+
} finally {
|
|
131
|
+
this.setState({ status: 'disconnected' });
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* Called by transport when connection is lost unexpectedly.
|
|
137
|
+
*/
|
|
138
|
+
async handleConnectionLost(error: Error): Promise<void> {
|
|
139
|
+
if (this._state.status === 'disconnected') {
|
|
140
|
+
return;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
this.setState({ status: 'reconnecting', attempt: 1, lastError: error });
|
|
144
|
+
await this.attemptReconnect(1);
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* Calculate delay for a given attempt using exponential backoff.
|
|
149
|
+
*/
|
|
150
|
+
calculateDelay(attempt: number): number {
|
|
151
|
+
const delay =
|
|
152
|
+
this.config.initialReconnectDelay *
|
|
153
|
+
Math.pow(this.config.backoffMultiplier, attempt - 1);
|
|
154
|
+
return Math.min(delay, this.config.maxReconnectDelay);
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
private setState(state: ConnectionState): void {
|
|
158
|
+
this._state = state;
|
|
159
|
+
for (const listener of this.listeners) {
|
|
160
|
+
try {
|
|
161
|
+
listener(state);
|
|
162
|
+
} catch {
|
|
163
|
+
// Ignore listener errors
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
private async handleConnectionFailure(
|
|
169
|
+
error: Error,
|
|
170
|
+
attempt: number,
|
|
171
|
+
): Promise<void> {
|
|
172
|
+
if (this.shouldGiveUp(attempt)) {
|
|
173
|
+
this.setState({ status: 'failed', error });
|
|
174
|
+
throw error;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
this.setState({
|
|
178
|
+
status: 'reconnecting',
|
|
179
|
+
attempt: attempt + 1,
|
|
180
|
+
lastError: error,
|
|
181
|
+
});
|
|
182
|
+
await this.attemptReconnect(attempt + 1);
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
private shouldGiveUp(attempt: number): boolean {
|
|
186
|
+
return (
|
|
187
|
+
this.config.maxReconnectAttempts > 0 &&
|
|
188
|
+
attempt >= this.config.maxReconnectAttempts
|
|
189
|
+
);
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
private async attemptReconnect(attempt: number): Promise<void> {
|
|
193
|
+
const delay = this.calculateDelay(attempt);
|
|
194
|
+
|
|
195
|
+
await new Promise<void>((resolve) => {
|
|
196
|
+
this.reconnectTimer = setTimeout(resolve, delay);
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
if (this._state.status === 'disconnected') {
|
|
200
|
+
return;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
try {
|
|
204
|
+
await this.connectFn();
|
|
205
|
+
this.setState({ status: 'connected' });
|
|
206
|
+
} catch (error) {
|
|
207
|
+
const err = error instanceof Error ? error : new Error(String(error));
|
|
208
|
+
await this.handleConnectionFailure(err, attempt);
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
private clearReconnectTimer(): void {
|
|
213
|
+
if (this.reconnectTimer !== null) {
|
|
214
|
+
clearTimeout(this.reconnectTimer);
|
|
215
|
+
this.reconnectTimer = null;
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
export type { TransportCapabilities } from './capabilities.js';
|
|
2
|
+
export {
|
|
3
|
+
hasNativeDeadLetter,
|
|
4
|
+
supportsDeliveryMode,
|
|
5
|
+
supportsDelayedMessages,
|
|
6
|
+
} from './capabilities.js';
|
|
7
|
+
|
|
8
|
+
export type {
|
|
9
|
+
MessageHandler,
|
|
10
|
+
MessageReceipt,
|
|
11
|
+
RabbitMQSendOptions,
|
|
12
|
+
RabbitMQSubscribeOptions,
|
|
13
|
+
SendOptions,
|
|
14
|
+
SubscribeOptions,
|
|
15
|
+
Subscription,
|
|
16
|
+
Transport,
|
|
17
|
+
TransportSendOptions,
|
|
18
|
+
TransportSubscribeOptions,
|
|
19
|
+
} from './transport.js';
|
|
20
|
+
|
|
21
|
+
export type {
|
|
22
|
+
ConnectFn,
|
|
23
|
+
ConnectionManagerConfig,
|
|
24
|
+
ConnectionState,
|
|
25
|
+
DisconnectFn,
|
|
26
|
+
StateChangeCallback,
|
|
27
|
+
} from './connection-manager.js';
|
|
28
|
+
export {
|
|
29
|
+
ConnectionManager,
|
|
30
|
+
defaultConnectionConfig,
|
|
31
|
+
} from './connection-manager.js';
|
|
32
|
+
|
|
33
|
+
export { LocalTransport } from './local/local-transport.js';
|
|
34
|
+
|
|
35
|
+
export type {
|
|
36
|
+
MultiTransportConfig,
|
|
37
|
+
MultiTransportHooks,
|
|
38
|
+
} from './multi/multi-transport.js';
|
|
39
|
+
export { MultiTransport } from './multi/multi-transport.js';
|
|
40
|
+
|
|
41
|
+
export type { RabbitMQTransportConfig } from './rabbitmq/rabbitmq-transport.js';
|
|
42
|
+
export { RabbitMQTransport } from './rabbitmq/rabbitmq-transport.js';
|