@linagora/rabbitmq-client 0.5.0 → 0.6.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 CHANGED
@@ -109,7 +109,7 @@ await client.subscribe('events', 'order.placed', 'order-queue', handler, {
109
109
  })
110
110
  ```
111
111
 
112
- One queue can take messages from several exchanges. Pass the other bindings in `bindings`; they share the queue, its consumer and its DLQ, and are restored on reconnection. Name the dead letter exchange after the queue with `deadLetterExchange`, so it does not live under another service's exchange. For exchanges another service owns, `passiveExchanges` checks they exist instead of declaring them, and `subscribe` fails when one is missing:
112
+ One queue can take messages from several exchanges. Pass the other bindings in `bindings`; they share the queue, its consumer and its DLQ, and are restored on reconnection. Name the dead letter exchange after the queue with `deadLetterExchange`, so it does not live under another service's exchange. For exchanges another service owns, `passiveExchanges` checks they exist instead of declaring them, and `subscribe` fails when one is missing. Pass `true` for every exchange, or a list of names to check those and declare the rest:
113
113
 
114
114
  ```typescript
115
115
  await client.subscribe('space', 'twake.space.#', 'twake-space', handler, {
package/dist/index.cjs CHANGED
@@ -511,13 +511,15 @@ var RabbitMQClient = class {
511
511
  await channel.assertExchange(dlxExchange, "topic", { durable: true });
512
512
  await channel.assertQueue(dlqQueue, { durable: true });
513
513
  await channel.bindQueue(dlqQueue, dlxExchange, dlqRoutingKey);
514
- const missing = bindings.map((b) => b.exchange).filter((name) => !this.assertedExchanges.has(name));
515
- if (options?.passiveExchanges && missing.length > 0) {
516
- await this.checkExchanges(missing);
517
- } else {
518
- for (const name of missing) {
519
- await channel.assertExchange(name, "topic", { durable: true });
520
- }
514
+ const passive = options?.passiveExchanges;
515
+ const isPassive = (name) => passive === true || Array.isArray(passive) && passive.includes(name);
516
+ const missing = [...new Set(bindings.map((b) => b.exchange))].filter((name) => !this.assertedExchanges.has(name));
517
+ const toCheck = missing.filter(isPassive);
518
+ if (toCheck.length > 0) {
519
+ await this.checkExchanges(toCheck);
520
+ }
521
+ for (const name of missing.filter((name2) => !isPassive(name2))) {
522
+ await channel.assertExchange(name, "topic", { durable: true });
521
523
  }
522
524
  for (const name of missing) this.assertedExchanges.add(name);
523
525
  const queueType = options?.queueArguments?.["x-queue-type"] ?? "quorum";
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/index.ts","../src/client.ts","../src/logger.ts","../src/semaphore.ts"],"sourcesContent":["export { DeadLetterError, RabbitMQClient, UnroutableMessageError } from './client.js'\nexport { defaultLogger, silentLogger } from './logger.js'\nexport type {\n ILogger,\n PublishOptions,\n RabbitMQBinding,\n RabbitMQClientOptions,\n RabbitMQHooks,\n RabbitMQMessage,\n RabbitMQMessageHandler,\n RabbitMQMessageProperties,\n RabbitMQSubscription,\n SubscribeOptions,\n} from './types.js'\n","import amqp from 'amqplib'\nimport type {\n ILogger,\n PublishOptions,\n RabbitMQClientOptions,\n RabbitMQHooks,\n RabbitMQMessage,\n RabbitMQMessageHandler,\n RabbitMQMessageProperties,\n RabbitMQSubscription,\n SubscribeOptions,\n} from './types.js'\nimport { defaultLogger, forLibraryCalls } from './logger.js'\nimport { Semaphore } from './semaphore.js'\n\nconst DEFAULT_PREFETCH = 10\nconst MAX_PUBLISH_RETRY_DELAY_MS = 60_000\n\nconst DEFAULTS = {\n maxRetries: 3,\n retryDelay: 1000,\n connectionRetryDelay: 5000,\n initMaxAttempts: 5,\n publishMaxAttempts: 5,\n prefetch: DEFAULT_PREFETCH,\n closeTimeout: 5000,\n} as const\n\nfunction backoff(baseDelay: number, attempt: number, maxDelay: number): number {\n return Math.min(baseDelay * Math.pow(2, attempt - 1), maxDelay)\n}\n\n/** A `mandatory` publish the broker could route to no queue. */\nexport class UnroutableMessageError extends Error {\n constructor(\n readonly exchange: string,\n readonly routingKey: string,\n ) {\n super(`No queue is bound to receive ${exchange || '(default exchange)'}/${routingKey}`)\n this.name = 'UnroutableMessageError'\n }\n}\n\n/** Thrown by a handler to send its message to the DLQ at once, without retrying. */\nexport class DeadLetterError extends Error {\n constructor(message: string, options?: { cause?: unknown }) {\n super(message, options)\n this.name = 'DeadLetterError'\n }\n}\n\n/**\n * Production-grade RabbitMQ client with confirm channels, automatic DLQ\n * infrastructure, reconnection with subscription restoration, and\n * exponential-backoff publishing.\n *\n * @example\n * ```ts\n * const client = new RabbitMQClient({ url: 'amqp://localhost' })\n * await client.init()\n * await client.publish('events', 'user.created', { userId: '123' })\n * await client.subscribe('events', 'user.created', 'user-service', handler)\n * ```\n */\nexport class RabbitMQClient {\n private connection: amqp.ChannelModel | null = null\n private channel: amqp.ConfirmChannel | null = null\n private connected = false\n private closing = false\n private subscriptions: RabbitMQSubscription[] = []\n private readonly options: Required<Omit<RabbitMQClientOptions, 'logger' | 'hooks'>>\n private readonly logger: ILogger\n private readonly hooks: RabbitMQHooks\n private initializationPromise: Promise<void> | null = null\n private reconnectionPromise: Promise<void> | null = null\n private assertedExchanges = new Set<string>()\n // One per channel, so a late return on a replaced channel reaches no waiter.\n private returnWaiters = new Set<(msg: amqp.Message) => void>()\n private consumerTags = new Map<string, string>()\n // Per-queue concurrency limiter, keyed by queue name. Persisted across\n // reconnects (unlike consumerTags) so handlers still running when a\n // connection drops keep holding their permits, and the new consumer cannot\n // exceed the ceiling while the old pipeline drains. `null` = no limit.\n private subscriptionSemaphores = new Map<string, Semaphore | null>()\n private retryWakers = new Set<() => void>()\n private inflightCount = 0\n private drainResolve: (() => void) | null = null\n\n constructor(options: RabbitMQClientOptions) {\n this.options = {\n url: options.url,\n maxRetries: options.maxRetries ?? DEFAULTS.maxRetries,\n retryDelay: options.retryDelay ?? DEFAULTS.retryDelay,\n connectionRetryDelay: options.connectionRetryDelay ?? DEFAULTS.connectionRetryDelay,\n initMaxAttempts: options.initMaxAttempts ?? DEFAULTS.initMaxAttempts,\n publishMaxAttempts: options.publishMaxAttempts ?? DEFAULTS.publishMaxAttempts,\n prefetch: options.prefetch ?? DEFAULTS.prefetch,\n // Default concurrency tracks prefetch, preserving the previous behaviour\n // (handlers ran fire-and-forget, so up to `prefetch` ran at once) while\n // making the ceiling explicit and independently tunable. A value <= 0\n // (including an unlimited `prefetch: 0`) means \"no concurrency limit\".\n concurrency: options.concurrency ?? options.prefetch ?? DEFAULTS.prefetch,\n closeTimeout: options.closeTimeout ?? DEFAULTS.closeTimeout,\n }\n this.logger = forLibraryCalls(options.logger ?? defaultLogger)\n this.hooks = options.hooks ?? {}\n }\n\n /**\n * Opens a connection and creates a confirm channel. Retries up to\n * `initMaxAttempts` times, then throws. Idempotent and safe to call\n * concurrently — duplicate calls share the same in-flight promise.\n */\n async init(): Promise<void> {\n if (this.connection && this.connected) {\n return\n }\n if (this.initializationPromise) {\n return this.initializationPromise\n }\n // Attach a no-op catch to prevent Node from briefly treating this as an\n // unhandled rejection before the await below registers its own handler.\n this.initializationPromise = this.doConnect(this.options.initMaxAttempts)\n this.initializationPromise.catch(() => undefined)\n try {\n await this.initializationPromise\n } finally {\n this.initializationPromise = null\n }\n }\n\n private async doConnect(maxAttempts?: number): Promise<void> {\n this.assertedExchanges.clear()\n this.consumerTags.clear()\n // A channel failure leaves its connection open; drop it so it does not\n // outlive the client and keep the process alive.\n this.connection?.close().catch(() => undefined)\n this.connection = null\n let attempts = 0\n while (!this.connected && !this.closing) {\n try {\n const connection = await amqp.connect(this.options.url)\n this.connection = connection\n this.logger.info('Connected to server')\n const channel = await connection.createConfirmChannel()\n this.channel = channel\n // Retries sleeping on the replaced channel give up; the broker redelivers their messages.\n this.wakeRetries()\n const returnWaiters = new Set<(msg: amqp.Message) => void>()\n this.returnWaiters = returnWaiters\n channel.on('return', (msg: amqp.Message) => {\n for (const waiter of returnWaiters) waiter(msg)\n })\n this.logger.info('Confirm channel created')\n await channel.prefetch(this.options.prefetch)\n this.logger.info('Channel prefetch set', { prefetch: this.options.prefetch })\n\n // Events from a connection or channel a reconnect has replaced must not\n // tear down the current one.\n connection.on('error', (error: Error) => {\n if (connection !== this.connection) return\n this.connected = false\n this.logger.error('Connection error', { error })\n })\n connection.on('close', () => {\n if (connection !== this.connection) return\n this.connected = false\n this.logger.warn('Connection closed')\n this.reconnectWithRetry()\n })\n channel.on('error', (error: Error) => {\n if (channel !== this.channel) return\n this.logger.error('Channel error', { error })\n this.handleChannelFailure()\n })\n channel.on('close', () => {\n if (channel !== this.channel) return\n this.logger.warn('Channel closed')\n this.handleChannelFailure()\n })\n\n this.connected = true\n this.logger.info('Client initialized successfully')\n } catch (error) {\n attempts++\n this.connected = false\n if (maxAttempts !== undefined && attempts >= maxAttempts) {\n this.logger.error('Connection failed after maximum attempts', { error, attempts, maxAttempts })\n throw new Error(\n `Failed to connect to RabbitMQ after ${attempts} attempts. ` +\n 'Check RABBITMQ_URL configuration and RabbitMQ server availability.',\n )\n }\n this.logger.warn('Connection attempt failed, retrying...', {\n error,\n attempt: attempts,\n maxAttempts: maxAttempts ?? 'unlimited',\n retryDelayMs: this.options.connectionRetryDelay,\n })\n await this.sleep(this.options.connectionRetryDelay)\n }\n }\n if (!this.connected) {\n throw new Error('RabbitMQ client closed while connecting')\n }\n }\n\n private handleChannelFailure(): void {\n if (this.connected) {\n this.connected = false\n this.reconnectWithRetry()\n }\n }\n\n /**\n * Returns whether the client currently has an active connection and channel.\n */\n isConnected(): boolean {\n return this.connected\n }\n\n /**\n * Publishes a JSON message to a topic exchange with publisher confirms, or\n * straight to a queue through the default exchange (`''`, routing key = queue name).\n * Retries with exponential backoff (capped at 60 s), forcing a reconnect\n * on each failure. Throws after `publishMaxAttempts` exhausted.\n */\n async publish(\n exchange: string,\n routingKey: string,\n message: RabbitMQMessage,\n options?: PublishOptions,\n ): Promise<void> {\n let attempts = 0\n const maxAttempts = options?.maxAttempts ?? this.options.publishMaxAttempts\n const baseDelay = this.options.connectionRetryDelay\n const content = Buffer.from(JSON.stringify(message))\n // Taken once, not per attempt: a retried publish must not look newer than\n // a message published after it succeeded.\n const timestamp = Math.floor(Date.now() / 1000)\n\n while (attempts < maxAttempts) {\n try {\n if (!this.connected) {\n await this.reconnectWithRetry()\n }\n\n if (!this.channel) {\n throw new Error('Channel not available')\n }\n\n // The default exchange ('') always exists and the broker refuses to\n // declare it, so publishing straight to a queue skips the assertion.\n if (exchange !== '' && !this.assertedExchanges.has(exchange)) {\n await this.channel.assertExchange(exchange, 'topic', { durable: true })\n this.assertedExchanges.add(exchange)\n }\n\n const channel = this.channel\n const returnWaiters = this.returnWaiters\n // The broker sends basic.return before the confirm of the same message,\n // so the flag is settled by the time waitForConfirms resolves. A return\n // carries no delivery tag, so it is matched on where it was sent plus the\n // message id, or the content when there is none.\n let returned = false\n const messageId = options?.messageId\n const onReturn = (msg: amqp.Message) => {\n if (msg.fields.exchange !== exchange || msg.fields.routingKey !== routingKey) return\n if (messageId !== undefined ? msg.properties.messageId === messageId : msg.content.equals(content)) {\n returned = true\n }\n }\n if (options?.mandatory) returnWaiters.add(onReturn)\n\n try {\n channel.publish(exchange, routingKey, content, {\n persistent: true,\n timestamp,\n headers: options?.headers,\n correlationId: options?.correlationId,\n messageId: options?.messageId,\n expiration: options?.expiration,\n mandatory: options?.mandatory,\n })\n\n await channel.waitForConfirms()\n } finally {\n returnWaiters.delete(onReturn)\n }\n\n if (returned) {\n throw new UnroutableMessageError(exchange, routingKey)\n }\n\n if (attempts > 0) {\n this.logger.info('Published message after retries', { exchange, routingKey, messageSize: content.length, attempts })\n } else {\n this.logger.info('Published message', { exchange, routingKey, messageSize: content.length })\n }\n this.logger.debug('Published message payload', { exchange, routingKey, payload: message })\n\n this.callHook(this.hooks.onPublish, { exchange, routingKey, attempts: attempts + 1 })\n\n return\n } catch (error) {\n // The broker answered, and the same routing would fail the same way.\n if (error instanceof UnroutableMessageError) {\n this.logger.warn('Published message was unroutable', { exchange, routingKey })\n throw error\n }\n\n attempts++\n this.connected = false\n\n if (attempts >= maxAttempts) {\n this.logger.error('Publish failed after max attempts', { error, exchange, routingKey, attempts, maxAttempts })\n throw new Error(\n `Failed to publish to ${exchange || '(default exchange)'}/${routingKey} after ${attempts} attempts: ${error instanceof Error ? error.message : String(error)}`,\n )\n }\n\n const retryDelay = backoff(baseDelay, attempts, MAX_PUBLISH_RETRY_DELAY_MS)\n\n this.logger.warn('Publish attempt failed, retrying', { error, exchange, routingKey, attempt: attempts, maxAttempts, retryDelayMs: retryDelay })\n\n await this.sleep(retryDelay)\n }\n }\n }\n\n /**\n * Gracefully shuts down the client. Waits up to `closeTimeout` ms for\n * in-flight message handlers to finish before closing the channel and\n * connection. Pass `false` to preserve subscriptions for a later\n * `init()` / reconnect cycle.\n */\n async close(clearSubscriptions = true): Promise<void> {\n // Closing the channel fires its 'close' handler, which would otherwise\n // reconnect and leave a connection behind that keeps the process alive.\n this.closing = true\n try {\n // A connect already running gives up after its current attempt; whatever\n // it opened is closed below.\n await Promise.allSettled([this.initializationPromise, this.reconnectionPromise])\n this.wakeRetries()\n if (this.inflightCount > 0) {\n this.logger.info('Waiting for in-flight messages to drain', { inflightCount: this.inflightCount })\n await this.waitForDrain(this.options.closeTimeout)\n }\n try {\n await this.channel?.close()\n } finally {\n await this.connection?.close()\n }\n this.connection = null\n this.channel = null\n this.connected = false\n this.initializationPromise = null\n this.reconnectionPromise = null\n this.assertedExchanges.clear()\n this.consumerTags.clear()\n if (clearSubscriptions) {\n this.subscriptions = []\n this.subscriptionSemaphores.clear()\n }\n this.logger.info('Connection closed')\n } catch (error) {\n this.logger.error('Error closing connection', { error })\n throw error\n } finally {\n this.closing = false\n }\n }\n\n private async reconnectWithRetry(): Promise<void> {\n if (this.closing) return\n if (this.reconnectionPromise) {\n return this.reconnectionPromise\n }\n this.connected = false\n this.logger.info('Starting reconnection...')\n this.reconnectionPromise = this.doReconnect()\n this.reconnectionPromise.catch(() => undefined)\n try {\n await this.reconnectionPromise\n } catch (error) {\n // The event handlers call this without awaiting it, so a reconnect that\n // close() interrupted must not surface as an unhandled rejection.\n if (!this.closing) throw error\n } finally {\n this.reconnectionPromise = null\n }\n }\n\n private async doReconnect(): Promise<void> {\n await this.doConnect()\n await this.resubscribeAll()\n }\n\n private async resubscribeAll(): Promise<void> {\n if (this.subscriptions.length === 0) {\n return\n }\n this.logger.info('Re-establishing subscriptions', { count: this.subscriptions.length })\n const subs = [...this.subscriptions]\n const results = await Promise.allSettled(\n subs.map(async (sub) => {\n await this.setupSubscription(sub)\n return sub.queue\n }),\n )\n const succeeded: string[] = []\n const failed: string[] = []\n results.forEach((result, index) => {\n if (result.status === 'fulfilled') {\n succeeded.push(result.value)\n } else {\n const queue = subs[index].queue\n failed.push(queue)\n this.logger.error('Failed to re-subscribe to queue', { error: result.reason, queue })\n }\n })\n if (succeeded.length > 0) {\n this.logger.info('Successfully re-subscribed to queues', { count: succeeded.length, queues: succeeded })\n }\n if (failed.length > 0) {\n this.logger.warn('Some subscriptions failed to restore', { count: failed.length, queues: failed })\n }\n this.callHook(this.hooks.onReconnect, { subscriptionsRestored: succeeded.length, subscriptionsFailed: failed.length })\n }\n\n /**\n * Subscribes to a queue with automatic DLQ infrastructure setup.\n *\n * Pass `options.queueArguments` to override the default quorum-queue\n * arguments (merged with the DLQ wiring defaults).\n */\n async subscribe(\n exchange: string,\n routingKey: string,\n queue: string,\n handler: RabbitMQMessageHandler,\n options?: SubscribeOptions,\n ): Promise<void> {\n if (!this.connected || !this.channel) {\n throw new Error('RabbitMQ client not connected. Call init() first.')\n }\n const sub: RabbitMQSubscription = { exchange, routingKey, queue, handler, options }\n const existingIndex = this.subscriptions.findIndex((s) => s.queue === queue)\n const previous = existingIndex === -1 ? undefined : this.subscriptions[existingIndex]\n if (existingIndex === -1) {\n this.subscriptions.push(sub)\n } else {\n this.subscriptions[existingIndex] = sub\n // Re-subscribing may change concurrency; drop the old limiter so\n // setupSubscription rebuilds it from the new options. (A reconnect goes\n // through setupSubscription directly and keeps the existing limiter.)\n this.subscriptionSemaphores.delete(queue)\n }\n try {\n await this.setupSubscription(sub)\n } catch (error) {\n // A subscription that cannot be set up must not be retried on every reconnect.\n this.subscriptions = previous\n ? this.subscriptions.map((s) => (s === sub ? previous : s))\n : this.subscriptions.filter((s) => s !== sub)\n throw error\n }\n }\n\n /**\n * Cancels a queue subscription and removes it from the restoration list.\n */\n async unsubscribe(queue: string): Promise<void> {\n const tag = this.consumerTags.get(queue)\n if (tag && this.channel) {\n await this.channel.cancel(tag)\n }\n this.consumerTags.delete(queue)\n this.subscriptionSemaphores.delete(queue)\n this.subscriptions = this.subscriptions.filter((s) => s.queue !== queue)\n this.logger.info('Unsubscribed from queue', { queue })\n }\n\n private async setupSubscription(sub: RabbitMQSubscription): Promise<void> {\n if (!this.channel) {\n throw new Error('Channel not available')\n }\n // Bind this consumer to the exact channel that will deliver its messages,\n // so ack/nack always target that channel even after a reconnect swaps\n // `this.channel`.\n const channel = this.channel\n const { exchange, routingKey, queue, handler, options } = sub\n const dlxExchange = options?.deadLetterExchange ?? `${exchange}.dlx`\n const dlqQueue = `${queue}.dlq`\n // Set once on the queue, so every binding's dead letters reach the DLQ.\n const dlqRoutingKey = `${routingKey}.dead`\n const bindings = [{ exchange, routingKey }, ...(options?.bindings ?? [])]\n\n await channel.assertExchange(dlxExchange, 'topic', { durable: true })\n await channel.assertQueue(dlqQueue, { durable: true })\n await channel.bindQueue(dlqQueue, dlxExchange, dlqRoutingKey)\n\n const missing = bindings.map((b) => b.exchange).filter((name) => !this.assertedExchanges.has(name))\n if (options?.passiveExchanges && missing.length > 0) {\n await this.checkExchanges(missing)\n } else {\n for (const name of missing) {\n await channel.assertExchange(name, 'topic', { durable: true })\n }\n }\n for (const name of missing) this.assertedExchanges.add(name)\n\n const queueType = options?.queueArguments?.['x-queue-type'] ?? 'quorum'\n const queueArgs: Record<string, unknown> = {\n 'x-queue-type': queueType,\n 'x-overflow': 'reject-publish',\n }\n // at-least-once DLQ strategy is only supported by quorum queues\n if (queueType === 'quorum') {\n queueArgs['x-dead-letter-strategy'] = 'at-least-once'\n }\n\n await channel.assertQueue(queue, {\n durable: true,\n deadLetterExchange: dlxExchange,\n deadLetterRoutingKey: dlqRoutingKey,\n arguments: { ...queueArgs, ...options?.queueArguments },\n })\n\n for (const binding of bindings) {\n await channel.bindQueue(queue, binding.exchange, binding.routingKey)\n }\n // Reuse the queue's existing limiter across reconnects; only build a new\n // one the first time (or after subscribe()/unsubscribe() cleared it).\n // A concurrency <= 0 means \"no limit\" (null), matching an unlimited prefetch.\n if (!this.subscriptionSemaphores.has(queue)) {\n const concurrency = options?.concurrency ?? this.options.concurrency\n this.subscriptionSemaphores.set(queue, concurrency > 0 ? new Semaphore(concurrency) : null)\n }\n const semaphore = this.subscriptionSemaphores.get(queue) ?? null\n const { consumerTag } = await channel.consume(\n queue,\n (message) => {\n if (message) {\n this.dispatch(message, handler, semaphore, channel, options)\n }\n },\n { noAck: false },\n )\n this.consumerTags.set(queue, consumerTag)\n this.logger.info('Subscribed to queue', { queue, bindings })\n }\n\n // A failed check closes the channel it ran on, so it runs on a throwaway one\n // rather than take down the shared channel and every consumer on it.\n private async checkExchanges(exchanges: string[]): Promise<void> {\n if (!this.connection) {\n throw new Error('Connection not available')\n }\n const probe = await this.connection.createChannel()\n probe.on('error', () => undefined)\n try {\n for (const name of exchanges) {\n await probe.checkExchange(name)\n }\n } finally {\n await probe.close().catch(() => undefined)\n }\n }\n\n /**\n * Gates a delivered message on the subscription's concurrency semaphore, then\n * processes it. A message counts as in-flight from delivery until its handler\n * settles (including time spent waiting for a permit), so graceful `close()`\n * drains queued messages as well as actively-processing ones.\n */\n private dispatch(\n message: amqp.ConsumeMessage,\n handler: RabbitMQMessageHandler,\n semaphore: Semaphore | null,\n channel: amqp.ConfirmChannel,\n options: SubscribeOptions | undefined,\n ): void {\n this.inflightCount++\n const acquire = semaphore ? semaphore.acquire() : Promise.resolve()\n acquire\n .then(() => this.handleWithRetry(message, handler, channel, options))\n .catch((error) => {\n this.logger.error('Unhandled error in message handler', { error })\n })\n .finally(() => {\n semaphore?.release()\n this.inflightCount--\n if (this.inflightCount === 0 && this.drainResolve) {\n this.drainResolve()\n }\n })\n }\n\n private async handleWithRetry(\n message: amqp.ConsumeMessage,\n handler: RabbitMQMessageHandler,\n channel: amqp.ConfirmChannel,\n options: SubscribeOptions | undefined,\n ): Promise<void> {\n // `channel` is the one that delivered this message. If a reconnect has\n // since replaced it, this message was never acked and the broker will\n // redeliver it on the new channel, so drop this stale attempt rather than\n // run the handler again or ack a tag the new channel does not know.\n if (channel !== this.channel) {\n this.logger.warn('Skipping message from a superseded channel; it will be redelivered')\n return\n }\n const startTime = Date.now()\n let attempts = 0\n const routingKey = message.fields.routingKey\n const exchange = message.fields.exchange\n\n let content: RabbitMQMessage\n try {\n content = JSON.parse(message.content.toString())\n } catch (parseError) {\n const rawContent = message.content.toString()\n const rawPreview = rawContent.substring(0, 100)\n this.logger.error('Failed to parse message JSON, sending to DLQ', {\n error: parseError,\n exchange,\n routingKey,\n rawContentPreview: rawPreview + (rawContent.length > 100 ? '...' : ''),\n })\n if (this.settle(channel, message, 'nack')) {\n this.callHook(this.hooks.onMessageDlq, { exchange, routingKey, duration: 0, reason: 'invalid_json' })\n }\n return\n }\n\n this.logger.debug('Message received, processing', { exchange, routingKey, payload: content })\n\n const properties: RabbitMQMessageProperties = {\n exchange,\n routingKey,\n headers: message.properties.headers ?? {},\n timestamp: message.properties.timestamp,\n messageId: message.properties.messageId,\n correlationId: message.properties.correlationId,\n }\n\n const maxRetries = options?.maxRetries ?? this.options.maxRetries\n while (attempts < maxRetries) {\n // Only the handler call belongs in this try. Acking inside it would make\n // a dead channel look like a failed handler and re-run its side effects.\n try {\n await handler(content, properties)\n } catch (error) {\n attempts++\n // By name, not instanceof: the ESM and CJS builds each define their own class.\n if (error instanceof Error && error.name === 'DeadLetterError') {\n const duration = Date.now() - startTime\n this.logger.warn('Handler dead-lettered the message', { error: error.message, exchange, routingKey })\n if (this.settle(channel, message, 'nack')) {\n this.callHook(this.hooks.onMessageDlq, { exchange, routingKey, duration, reason: 'dead_letter_error' })\n }\n return\n }\n this.logger.error('Handler failed', {\n error: error instanceof Error ? error.message : error,\n stack: error instanceof Error ? error.stack : undefined,\n exchange,\n routingKey,\n attempt: attempts,\n maxRetries,\n })\n if (attempts < maxRetries) {\n await this.sleepUntilClose(this.retryDelayFor(attempts, options))\n if (this.closing || channel !== this.channel) {\n this.logger.warn('Stopped retrying a message on close or reconnect; it will be redelivered', { exchange, routingKey })\n return\n }\n }\n continue\n }\n const duration = Date.now() - startTime\n this.logger.info('Message processed successfully', { exchange, routingKey, duration, attempts: attempts + 1 })\n if (this.settle(channel, message, 'ack')) {\n this.callHook(this.hooks.onMessageProcessed, { exchange, routingKey, duration, attempts: attempts + 1 })\n }\n return\n }\n\n const duration = Date.now() - startTime\n this.logger.error('Message failed after max retries, sending to DLQ', { exchange, routingKey, maxRetries, duration })\n if (this.settle(channel, message, 'nack')) {\n this.callHook(this.hooks.onMessageDlq, { exchange, routingKey, duration, reason: 'max_retries_exhausted' })\n }\n }\n\n /**\n * Acks or nacks a delivery, reporting whether the broker accepted it.\n *\n * A settle failure is a transport error, never a handler failure: by this\n * point the handler has already run, so it must not be retried just because\n * the channel went away. The delivery stays unacked and the broker redelivers\n * it once the channel closes.\n */\n private settle(\n channel: amqp.ConfirmChannel,\n message: amqp.ConsumeMessage,\n action: 'ack' | 'nack',\n ): boolean {\n // A handler can run for minutes, so the channel that delivered this\n // message may have been replaced by a reconnect since the entry check.\n // Settling on it would throw, and replaying the delivery tag on the\n // current channel would settle an unrelated message, since tags are scoped\n // to the channel that issued them. The broker requeues a closed channel's\n // unacked deliveries, so let it be redelivered instead.\n if (channel !== this.channel) {\n this.logger.warn('Skipping message from a superseded channel; it will be redelivered')\n return false\n }\n try {\n if (action === 'ack') {\n channel.ack(message)\n } else {\n channel.nack(message, false, false)\n }\n return true\n } catch (error) {\n this.logger.error('Failed to settle message; leaving it unacked for redelivery', {\n error,\n action,\n exchange: message.fields.exchange,\n routingKey: message.fields.routingKey,\n })\n return false\n }\n }\n\n /**\n * Lightweight liveness probe: creates and immediately deletes a temporary\n * exclusive queue. Returns `false` when disconnected or on broker error.\n */\n async checkHealth(): Promise<boolean> {\n if (!this.connected || !this.channel) {\n return false\n }\n try {\n const { queue } = await this.channel.assertQueue('', {\n exclusive: true,\n autoDelete: true,\n })\n await this.channel.deleteQueue(queue)\n return true\n } catch (error) {\n this.logger.warn('Health check failed', { error })\n return false\n }\n }\n\n private waitForDrain(timeout: number): Promise<void> {\n if (this.inflightCount === 0) return Promise.resolve()\n return new Promise<void>((resolve) => {\n const timer = setTimeout(() => {\n this.logger.warn('Close timeout reached with messages still in flight', { inflightCount: this.inflightCount })\n this.drainResolve = null\n resolve()\n }, timeout)\n this.drainResolve = () => {\n clearTimeout(timer)\n this.drainResolve = null\n resolve()\n }\n })\n }\n\n /** Invokes a hook callback, swallowing errors so hooks never break message flow. */\n private callHook<T>(hook: ((info: T) => void) | undefined, info: T): void {\n try { hook?.(info) } catch (error) { this.logger.debug('Observability hook error', { error }) }\n }\n\n private sleep(ms: number): Promise<void> {\n return new Promise((resolve) => setTimeout(resolve, ms))\n }\n\n private retryDelayFor(attempts: number, options: SubscribeOptions | undefined): number {\n const { retryDelay } = this.options\n if (options?.maxRetryDelay === undefined) return retryDelay\n return backoff(retryDelay, attempts, options.maxRetryDelay)\n }\n\n private wakeRetries(): void {\n for (const wake of [...this.retryWakers]) wake()\n }\n\n /** Also cut short by a reconnect, via wakeRetries(). */\n private sleepUntilClose(ms: number): Promise<void> {\n if (this.closing) return Promise.resolve()\n return new Promise((resolve) => {\n const wake = () => {\n clearTimeout(timer)\n this.retryWakers.delete(wake)\n resolve()\n }\n const timer = setTimeout(wake, ms)\n this.retryWakers.add(wake)\n })\n }\n}\n","import type { ILogger } from './types.js'\n\n/**\n * Default logger used when the consumer does not inject one.\n *\n * Forwards `warn` and `error` to the console so connection drops, channel\n * errors, and failed handlers stay visible out of the box, while staying\n * silent for `info`/`debug` so routine operation never pollutes the\n * consumer's output. Pass `silentLogger` for zero output, or any `ILogger`\n * (console, Winston, pino, Bunyan) to take full control.\n */\nexport const defaultLogger: ILogger = {\n debug: () => {},\n info: () => {},\n warn: (message, ...args) => console.warn('[rabbitmq-client]', message, ...args),\n error: (message, ...args) => console.error('[rabbitmq-client]', message, ...args),\n}\n\ntype Level = keyof ILogger\ntype ObjectFirst = (context: object, message: string) => void\n\n/**\n * pino takes `(context, message)` and drops a context passed after the\n * message, and only serializes an Error under `err`. It is the one logger\n * with a `bindings()` method, so it is recognised by that and called its way.\n */\nexport const forLibraryCalls = (logger: ILogger): ILogger => {\n if (typeof (logger as { bindings?: unknown }).bindings !== 'function') return logger\n const call =\n (level: Level) =>\n (message: string, context?: unknown): void => {\n const log = (logger[level] as unknown as ObjectFirst).bind(logger)\n if (typeof context !== 'object' || context === null) return log({}, message)\n const { error, ...rest } = context as { error?: unknown }\n log(error === undefined ? rest : { ...rest, err: error }, message)\n }\n return { debug: call('debug'), info: call('info'), warn: call('warn'), error: call('error') }\n}\n\n/**\n * No-op logger that discards every message. Use this to silence the client\n * entirely, including the `warn`/`error` output the default logger emits.\n */\nexport const silentLogger: ILogger = {\n debug: () => {},\n info: () => {},\n warn: () => {},\n error: () => {},\n}\n","/**\n * Minimal FIFO counting semaphore used to cap how many message handlers run\n * concurrently, independently of channel prefetch.\n *\n * Prefetch bounds how many messages the broker delivers into consumer memory;\n * the semaphore bounds how many of those are actively being processed at once.\n * Keeping the two separate lets a consumer buffer a large prefetch window while\n * still protecting a slow downstream (a database, an HTTP API) from a burst.\n */\nexport class Semaphore {\n private permits: number\n private readonly waiters: Array<() => void> = []\n\n constructor(permits: number) {\n if (!Number.isInteger(permits) || permits < 1) {\n throw new Error(`Semaphore requires a positive integer of permits, got ${permits}`)\n }\n this.permits = permits\n }\n\n /**\n * Acquires a permit, resolving immediately when one is free or queueing\n * (FIFO) until another holder releases. Always pair with `release()` in a\n * `finally` so a throwing handler never leaks a permit.\n */\n acquire(): Promise<void> {\n if (this.permits > 0) {\n this.permits--\n return Promise.resolve()\n }\n return new Promise<void>((resolve) => {\n this.waiters.push(resolve)\n })\n }\n\n /**\n * Releases a permit. Hands it directly to the next waiter if any are queued,\n * otherwise returns it to the pool.\n */\n release(): void {\n const next = this.waiters.shift()\n if (next) {\n next()\n } else {\n this.permits++\n }\n }\n\n /** Permits currently available. Exposed for tests and introspection. */\n get available(): number {\n return this.permits\n }\n\n /** Number of callers currently queued waiting for a permit. */\n get waiting(): number {\n return this.waiters.length\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,qBAAiB;;;ACWV,IAAM,gBAAyB;AAAA,EACpC,OAAO,MAAM;AAAA,EAAC;AAAA,EACd,MAAM,MAAM;AAAA,EAAC;AAAA,EACb,MAAM,CAAC,YAAY,SAAS,QAAQ,KAAK,qBAAqB,SAAS,GAAG,IAAI;AAAA,EAC9E,OAAO,CAAC,YAAY,SAAS,QAAQ,MAAM,qBAAqB,SAAS,GAAG,IAAI;AAClF;AAUO,IAAM,kBAAkB,CAAC,WAA6B;AAC3D,MAAI,OAAQ,OAAkC,aAAa,WAAY,QAAO;AAC9E,QAAM,OACJ,CAAC,UACD,CAAC,SAAiB,YAA4B;AAC5C,UAAM,MAAO,OAAO,KAAK,EAA6B,KAAK,MAAM;AACjE,QAAI,OAAO,YAAY,YAAY,YAAY,KAAM,QAAO,IAAI,CAAC,GAAG,OAAO;AAC3E,UAAM,EAAE,OAAO,GAAG,KAAK,IAAI;AAC3B,QAAI,UAAU,SAAY,OAAO,EAAE,GAAG,MAAM,KAAK,MAAM,GAAG,OAAO;AAAA,EACnE;AACF,SAAO,EAAE,OAAO,KAAK,OAAO,GAAG,MAAM,KAAK,MAAM,GAAG,MAAM,KAAK,MAAM,GAAG,OAAO,KAAK,OAAO,EAAE;AAC9F;AAMO,IAAM,eAAwB;AAAA,EACnC,OAAO,MAAM;AAAA,EAAC;AAAA,EACd,MAAM,MAAM;AAAA,EAAC;AAAA,EACb,MAAM,MAAM;AAAA,EAAC;AAAA,EACb,OAAO,MAAM;AAAA,EAAC;AAChB;;;ACvC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,EAAE,WAAW,IAAI,KAAK;AAC5B,QAAI,SAAS,kBAAkB,OAAW,QAAO;AACjD,WAAO,QAAQ,YAAY,UAAU,QAAQ,aAAa;AAAA,EAC5D;AAAA,EAEQ,cAAoB;AAC1B,eAAW,QAAQ,CAAC,GAAG,KAAK,WAAW,EAAG,MAAK;AAAA,EACjD;AAAA;AAAA,EAGQ,gBAAgB,IAA2B;AACjD,QAAI,KAAK,QAAS,QAAO,QAAQ,QAAQ;AACzC,WAAO,IAAI,QAAQ,CAAC,YAAY;AAC9B,YAAM,OAAO,MAAM;AACjB,qBAAa,KAAK;AAClB,aAAK,YAAY,OAAO,IAAI;AAC5B,gBAAQ;AAAA,MACV;AACA,YAAM,QAAQ,WAAW,MAAM,EAAE;AACjC,WAAK,YAAY,IAAI,IAAI;AAAA,IAC3B,CAAC;AAAA,EACH;AACF;","names":["amqp","duration"]}
1
+ {"version":3,"sources":["../src/index.ts","../src/client.ts","../src/logger.ts","../src/semaphore.ts"],"sourcesContent":["export { DeadLetterError, RabbitMQClient, UnroutableMessageError } from './client.js'\nexport { defaultLogger, silentLogger } from './logger.js'\nexport type {\n ILogger,\n PublishOptions,\n RabbitMQBinding,\n RabbitMQClientOptions,\n RabbitMQHooks,\n RabbitMQMessage,\n RabbitMQMessageHandler,\n RabbitMQMessageProperties,\n RabbitMQSubscription,\n SubscribeOptions,\n} from './types.js'\n","import amqp from 'amqplib'\nimport type {\n ILogger,\n PublishOptions,\n RabbitMQClientOptions,\n RabbitMQHooks,\n RabbitMQMessage,\n RabbitMQMessageHandler,\n RabbitMQMessageProperties,\n RabbitMQSubscription,\n SubscribeOptions,\n} from './types.js'\nimport { defaultLogger, forLibraryCalls } from './logger.js'\nimport { Semaphore } from './semaphore.js'\n\nconst DEFAULT_PREFETCH = 10\nconst MAX_PUBLISH_RETRY_DELAY_MS = 60_000\n\nconst DEFAULTS = {\n maxRetries: 3,\n retryDelay: 1000,\n connectionRetryDelay: 5000,\n initMaxAttempts: 5,\n publishMaxAttempts: 5,\n prefetch: DEFAULT_PREFETCH,\n closeTimeout: 5000,\n} as const\n\nfunction backoff(baseDelay: number, attempt: number, maxDelay: number): number {\n return Math.min(baseDelay * Math.pow(2, attempt - 1), maxDelay)\n}\n\n/** A `mandatory` publish the broker could route to no queue. */\nexport class UnroutableMessageError extends Error {\n constructor(\n readonly exchange: string,\n readonly routingKey: string,\n ) {\n super(`No queue is bound to receive ${exchange || '(default exchange)'}/${routingKey}`)\n this.name = 'UnroutableMessageError'\n }\n}\n\n/** Thrown by a handler to send its message to the DLQ at once, without retrying. */\nexport class DeadLetterError extends Error {\n constructor(message: string, options?: { cause?: unknown }) {\n super(message, options)\n this.name = 'DeadLetterError'\n }\n}\n\n/**\n * Production-grade RabbitMQ client with confirm channels, automatic DLQ\n * infrastructure, reconnection with subscription restoration, and\n * exponential-backoff publishing.\n *\n * @example\n * ```ts\n * const client = new RabbitMQClient({ url: 'amqp://localhost' })\n * await client.init()\n * await client.publish('events', 'user.created', { userId: '123' })\n * await client.subscribe('events', 'user.created', 'user-service', handler)\n * ```\n */\nexport class RabbitMQClient {\n private connection: amqp.ChannelModel | null = null\n private channel: amqp.ConfirmChannel | null = null\n private connected = false\n private closing = false\n private subscriptions: RabbitMQSubscription[] = []\n private readonly options: Required<Omit<RabbitMQClientOptions, 'logger' | 'hooks'>>\n private readonly logger: ILogger\n private readonly hooks: RabbitMQHooks\n private initializationPromise: Promise<void> | null = null\n private reconnectionPromise: Promise<void> | null = null\n private assertedExchanges = new Set<string>()\n // One per channel, so a late return on a replaced channel reaches no waiter.\n private returnWaiters = new Set<(msg: amqp.Message) => void>()\n private consumerTags = new Map<string, string>()\n // Per-queue concurrency limiter, keyed by queue name. Persisted across\n // reconnects (unlike consumerTags) so handlers still running when a\n // connection drops keep holding their permits, and the new consumer cannot\n // exceed the ceiling while the old pipeline drains. `null` = no limit.\n private subscriptionSemaphores = new Map<string, Semaphore | null>()\n private retryWakers = new Set<() => void>()\n private inflightCount = 0\n private drainResolve: (() => void) | null = null\n\n constructor(options: RabbitMQClientOptions) {\n this.options = {\n url: options.url,\n maxRetries: options.maxRetries ?? DEFAULTS.maxRetries,\n retryDelay: options.retryDelay ?? DEFAULTS.retryDelay,\n connectionRetryDelay: options.connectionRetryDelay ?? DEFAULTS.connectionRetryDelay,\n initMaxAttempts: options.initMaxAttempts ?? DEFAULTS.initMaxAttempts,\n publishMaxAttempts: options.publishMaxAttempts ?? DEFAULTS.publishMaxAttempts,\n prefetch: options.prefetch ?? DEFAULTS.prefetch,\n // Default concurrency tracks prefetch, preserving the previous behaviour\n // (handlers ran fire-and-forget, so up to `prefetch` ran at once) while\n // making the ceiling explicit and independently tunable. A value <= 0\n // (including an unlimited `prefetch: 0`) means \"no concurrency limit\".\n concurrency: options.concurrency ?? options.prefetch ?? DEFAULTS.prefetch,\n closeTimeout: options.closeTimeout ?? DEFAULTS.closeTimeout,\n }\n this.logger = forLibraryCalls(options.logger ?? defaultLogger)\n this.hooks = options.hooks ?? {}\n }\n\n /**\n * Opens a connection and creates a confirm channel. Retries up to\n * `initMaxAttempts` times, then throws. Idempotent and safe to call\n * concurrently — duplicate calls share the same in-flight promise.\n */\n async init(): Promise<void> {\n if (this.connection && this.connected) {\n return\n }\n if (this.initializationPromise) {\n return this.initializationPromise\n }\n // Attach a no-op catch to prevent Node from briefly treating this as an\n // unhandled rejection before the await below registers its own handler.\n this.initializationPromise = this.doConnect(this.options.initMaxAttempts)\n this.initializationPromise.catch(() => undefined)\n try {\n await this.initializationPromise\n } finally {\n this.initializationPromise = null\n }\n }\n\n private async doConnect(maxAttempts?: number): Promise<void> {\n this.assertedExchanges.clear()\n this.consumerTags.clear()\n // A channel failure leaves its connection open; drop it so it does not\n // outlive the client and keep the process alive.\n this.connection?.close().catch(() => undefined)\n this.connection = null\n let attempts = 0\n while (!this.connected && !this.closing) {\n try {\n const connection = await amqp.connect(this.options.url)\n this.connection = connection\n this.logger.info('Connected to server')\n const channel = await connection.createConfirmChannel()\n this.channel = channel\n // Retries sleeping on the replaced channel give up; the broker redelivers their messages.\n this.wakeRetries()\n const returnWaiters = new Set<(msg: amqp.Message) => void>()\n this.returnWaiters = returnWaiters\n channel.on('return', (msg: amqp.Message) => {\n for (const waiter of returnWaiters) waiter(msg)\n })\n this.logger.info('Confirm channel created')\n await channel.prefetch(this.options.prefetch)\n this.logger.info('Channel prefetch set', { prefetch: this.options.prefetch })\n\n // Events from a connection or channel a reconnect has replaced must not\n // tear down the current one.\n connection.on('error', (error: Error) => {\n if (connection !== this.connection) return\n this.connected = false\n this.logger.error('Connection error', { error })\n })\n connection.on('close', () => {\n if (connection !== this.connection) return\n this.connected = false\n this.logger.warn('Connection closed')\n this.reconnectWithRetry()\n })\n channel.on('error', (error: Error) => {\n if (channel !== this.channel) return\n this.logger.error('Channel error', { error })\n this.handleChannelFailure()\n })\n channel.on('close', () => {\n if (channel !== this.channel) return\n this.logger.warn('Channel closed')\n this.handleChannelFailure()\n })\n\n this.connected = true\n this.logger.info('Client initialized successfully')\n } catch (error) {\n attempts++\n this.connected = false\n if (maxAttempts !== undefined && attempts >= maxAttempts) {\n this.logger.error('Connection failed after maximum attempts', { error, attempts, maxAttempts })\n throw new Error(\n `Failed to connect to RabbitMQ after ${attempts} attempts. ` +\n 'Check RABBITMQ_URL configuration and RabbitMQ server availability.',\n )\n }\n this.logger.warn('Connection attempt failed, retrying...', {\n error,\n attempt: attempts,\n maxAttempts: maxAttempts ?? 'unlimited',\n retryDelayMs: this.options.connectionRetryDelay,\n })\n await this.sleep(this.options.connectionRetryDelay)\n }\n }\n if (!this.connected) {\n throw new Error('RabbitMQ client closed while connecting')\n }\n }\n\n private handleChannelFailure(): void {\n if (this.connected) {\n this.connected = false\n this.reconnectWithRetry()\n }\n }\n\n /**\n * Returns whether the client currently has an active connection and channel.\n */\n isConnected(): boolean {\n return this.connected\n }\n\n /**\n * Publishes a JSON message to a topic exchange with publisher confirms, or\n * straight to a queue through the default exchange (`''`, routing key = queue name).\n * Retries with exponential backoff (capped at 60 s), forcing a reconnect\n * on each failure. Throws after `publishMaxAttempts` exhausted.\n */\n async publish(\n exchange: string,\n routingKey: string,\n message: RabbitMQMessage,\n options?: PublishOptions,\n ): Promise<void> {\n let attempts = 0\n const maxAttempts = options?.maxAttempts ?? this.options.publishMaxAttempts\n const baseDelay = this.options.connectionRetryDelay\n const content = Buffer.from(JSON.stringify(message))\n // Taken once, not per attempt: a retried publish must not look newer than\n // a message published after it succeeded.\n const timestamp = Math.floor(Date.now() / 1000)\n\n while (attempts < maxAttempts) {\n try {\n if (!this.connected) {\n await this.reconnectWithRetry()\n }\n\n if (!this.channel) {\n throw new Error('Channel not available')\n }\n\n // The default exchange ('') always exists and the broker refuses to\n // declare it, so publishing straight to a queue skips the assertion.\n if (exchange !== '' && !this.assertedExchanges.has(exchange)) {\n await this.channel.assertExchange(exchange, 'topic', { durable: true })\n this.assertedExchanges.add(exchange)\n }\n\n const channel = this.channel\n const returnWaiters = this.returnWaiters\n // The broker sends basic.return before the confirm of the same message,\n // so the flag is settled by the time waitForConfirms resolves. A return\n // carries no delivery tag, so it is matched on where it was sent plus the\n // message id, or the content when there is none.\n let returned = false\n const messageId = options?.messageId\n const onReturn = (msg: amqp.Message) => {\n if (msg.fields.exchange !== exchange || msg.fields.routingKey !== routingKey) return\n if (messageId !== undefined ? msg.properties.messageId === messageId : msg.content.equals(content)) {\n returned = true\n }\n }\n if (options?.mandatory) returnWaiters.add(onReturn)\n\n try {\n channel.publish(exchange, routingKey, content, {\n persistent: true,\n timestamp,\n headers: options?.headers,\n correlationId: options?.correlationId,\n messageId: options?.messageId,\n expiration: options?.expiration,\n mandatory: options?.mandatory,\n })\n\n await channel.waitForConfirms()\n } finally {\n returnWaiters.delete(onReturn)\n }\n\n if (returned) {\n throw new UnroutableMessageError(exchange, routingKey)\n }\n\n if (attempts > 0) {\n this.logger.info('Published message after retries', { exchange, routingKey, messageSize: content.length, attempts })\n } else {\n this.logger.info('Published message', { exchange, routingKey, messageSize: content.length })\n }\n this.logger.debug('Published message payload', { exchange, routingKey, payload: message })\n\n this.callHook(this.hooks.onPublish, { exchange, routingKey, attempts: attempts + 1 })\n\n return\n } catch (error) {\n // The broker answered, and the same routing would fail the same way.\n if (error instanceof UnroutableMessageError) {\n this.logger.warn('Published message was unroutable', { exchange, routingKey })\n throw error\n }\n\n attempts++\n this.connected = false\n\n if (attempts >= maxAttempts) {\n this.logger.error('Publish failed after max attempts', { error, exchange, routingKey, attempts, maxAttempts })\n throw new Error(\n `Failed to publish to ${exchange || '(default exchange)'}/${routingKey} after ${attempts} attempts: ${error instanceof Error ? error.message : String(error)}`,\n )\n }\n\n const retryDelay = backoff(baseDelay, attempts, MAX_PUBLISH_RETRY_DELAY_MS)\n\n this.logger.warn('Publish attempt failed, retrying', { error, exchange, routingKey, attempt: attempts, maxAttempts, retryDelayMs: retryDelay })\n\n await this.sleep(retryDelay)\n }\n }\n }\n\n /**\n * Gracefully shuts down the client. Waits up to `closeTimeout` ms for\n * in-flight message handlers to finish before closing the channel and\n * connection. Pass `false` to preserve subscriptions for a later\n * `init()` / reconnect cycle.\n */\n async close(clearSubscriptions = true): Promise<void> {\n // Closing the channel fires its 'close' handler, which would otherwise\n // reconnect and leave a connection behind that keeps the process alive.\n this.closing = true\n try {\n // A connect already running gives up after its current attempt; whatever\n // it opened is closed below.\n await Promise.allSettled([this.initializationPromise, this.reconnectionPromise])\n this.wakeRetries()\n if (this.inflightCount > 0) {\n this.logger.info('Waiting for in-flight messages to drain', { inflightCount: this.inflightCount })\n await this.waitForDrain(this.options.closeTimeout)\n }\n try {\n await this.channel?.close()\n } finally {\n await this.connection?.close()\n }\n this.connection = null\n this.channel = null\n this.connected = false\n this.initializationPromise = null\n this.reconnectionPromise = null\n this.assertedExchanges.clear()\n this.consumerTags.clear()\n if (clearSubscriptions) {\n this.subscriptions = []\n this.subscriptionSemaphores.clear()\n }\n this.logger.info('Connection closed')\n } catch (error) {\n this.logger.error('Error closing connection', { error })\n throw error\n } finally {\n this.closing = false\n }\n }\n\n private async reconnectWithRetry(): Promise<void> {\n if (this.closing) return\n if (this.reconnectionPromise) {\n return this.reconnectionPromise\n }\n this.connected = false\n this.logger.info('Starting reconnection...')\n this.reconnectionPromise = this.doReconnect()\n this.reconnectionPromise.catch(() => undefined)\n try {\n await this.reconnectionPromise\n } catch (error) {\n // The event handlers call this without awaiting it, so a reconnect that\n // close() interrupted must not surface as an unhandled rejection.\n if (!this.closing) throw error\n } finally {\n this.reconnectionPromise = null\n }\n }\n\n private async doReconnect(): Promise<void> {\n await this.doConnect()\n await this.resubscribeAll()\n }\n\n private async resubscribeAll(): Promise<void> {\n if (this.subscriptions.length === 0) {\n return\n }\n this.logger.info('Re-establishing subscriptions', { count: this.subscriptions.length })\n const subs = [...this.subscriptions]\n const results = await Promise.allSettled(\n subs.map(async (sub) => {\n await this.setupSubscription(sub)\n return sub.queue\n }),\n )\n const succeeded: string[] = []\n const failed: string[] = []\n results.forEach((result, index) => {\n if (result.status === 'fulfilled') {\n succeeded.push(result.value)\n } else {\n const queue = subs[index].queue\n failed.push(queue)\n this.logger.error('Failed to re-subscribe to queue', { error: result.reason, queue })\n }\n })\n if (succeeded.length > 0) {\n this.logger.info('Successfully re-subscribed to queues', { count: succeeded.length, queues: succeeded })\n }\n if (failed.length > 0) {\n this.logger.warn('Some subscriptions failed to restore', { count: failed.length, queues: failed })\n }\n this.callHook(this.hooks.onReconnect, { subscriptionsRestored: succeeded.length, subscriptionsFailed: failed.length })\n }\n\n /**\n * Subscribes to a queue with automatic DLQ infrastructure setup.\n *\n * Pass `options.queueArguments` to override the default quorum-queue\n * arguments (merged with the DLQ wiring defaults).\n */\n async subscribe(\n exchange: string,\n routingKey: string,\n queue: string,\n handler: RabbitMQMessageHandler,\n options?: SubscribeOptions,\n ): Promise<void> {\n if (!this.connected || !this.channel) {\n throw new Error('RabbitMQ client not connected. Call init() first.')\n }\n const sub: RabbitMQSubscription = { exchange, routingKey, queue, handler, options }\n const existingIndex = this.subscriptions.findIndex((s) => s.queue === queue)\n const previous = existingIndex === -1 ? undefined : this.subscriptions[existingIndex]\n if (existingIndex === -1) {\n this.subscriptions.push(sub)\n } else {\n this.subscriptions[existingIndex] = sub\n // Re-subscribing may change concurrency; drop the old limiter so\n // setupSubscription rebuilds it from the new options. (A reconnect goes\n // through setupSubscription directly and keeps the existing limiter.)\n this.subscriptionSemaphores.delete(queue)\n }\n try {\n await this.setupSubscription(sub)\n } catch (error) {\n // A subscription that cannot be set up must not be retried on every reconnect.\n this.subscriptions = previous\n ? this.subscriptions.map((s) => (s === sub ? previous : s))\n : this.subscriptions.filter((s) => s !== sub)\n throw error\n }\n }\n\n /**\n * Cancels a queue subscription and removes it from the restoration list.\n */\n async unsubscribe(queue: string): Promise<void> {\n const tag = this.consumerTags.get(queue)\n if (tag && this.channel) {\n await this.channel.cancel(tag)\n }\n this.consumerTags.delete(queue)\n this.subscriptionSemaphores.delete(queue)\n this.subscriptions = this.subscriptions.filter((s) => s.queue !== queue)\n this.logger.info('Unsubscribed from queue', { queue })\n }\n\n private async setupSubscription(sub: RabbitMQSubscription): Promise<void> {\n if (!this.channel) {\n throw new Error('Channel not available')\n }\n // Bind this consumer to the exact channel that will deliver its messages,\n // so ack/nack always target that channel even after a reconnect swaps\n // `this.channel`.\n const channel = this.channel\n const { exchange, routingKey, queue, handler, options } = sub\n const dlxExchange = options?.deadLetterExchange ?? `${exchange}.dlx`\n const dlqQueue = `${queue}.dlq`\n // Set once on the queue, so every binding's dead letters reach the DLQ.\n const dlqRoutingKey = `${routingKey}.dead`\n const bindings = [{ exchange, routingKey }, ...(options?.bindings ?? [])]\n\n await channel.assertExchange(dlxExchange, 'topic', { durable: true })\n await channel.assertQueue(dlqQueue, { durable: true })\n await channel.bindQueue(dlqQueue, dlxExchange, dlqRoutingKey)\n\n const passive = options?.passiveExchanges\n const isPassive = (name: string) => passive === true || (Array.isArray(passive) && passive.includes(name))\n const missing = [...new Set(bindings.map((b) => b.exchange))].filter((name) => !this.assertedExchanges.has(name))\n const toCheck = missing.filter(isPassive)\n if (toCheck.length > 0) {\n await this.checkExchanges(toCheck)\n }\n for (const name of missing.filter((name) => !isPassive(name))) {\n await channel.assertExchange(name, 'topic', { durable: true })\n }\n for (const name of missing) this.assertedExchanges.add(name)\n\n const queueType = options?.queueArguments?.['x-queue-type'] ?? 'quorum'\n const queueArgs: Record<string, unknown> = {\n 'x-queue-type': queueType,\n 'x-overflow': 'reject-publish',\n }\n // at-least-once DLQ strategy is only supported by quorum queues\n if (queueType === 'quorum') {\n queueArgs['x-dead-letter-strategy'] = 'at-least-once'\n }\n\n await channel.assertQueue(queue, {\n durable: true,\n deadLetterExchange: dlxExchange,\n deadLetterRoutingKey: dlqRoutingKey,\n arguments: { ...queueArgs, ...options?.queueArguments },\n })\n\n for (const binding of bindings) {\n await channel.bindQueue(queue, binding.exchange, binding.routingKey)\n }\n // Reuse the queue's existing limiter across reconnects; only build a new\n // one the first time (or after subscribe()/unsubscribe() cleared it).\n // A concurrency <= 0 means \"no limit\" (null), matching an unlimited prefetch.\n if (!this.subscriptionSemaphores.has(queue)) {\n const concurrency = options?.concurrency ?? this.options.concurrency\n this.subscriptionSemaphores.set(queue, concurrency > 0 ? new Semaphore(concurrency) : null)\n }\n const semaphore = this.subscriptionSemaphores.get(queue) ?? null\n const { consumerTag } = await channel.consume(\n queue,\n (message) => {\n if (message) {\n this.dispatch(message, handler, semaphore, channel, options)\n }\n },\n { noAck: false },\n )\n this.consumerTags.set(queue, consumerTag)\n this.logger.info('Subscribed to queue', { queue, bindings })\n }\n\n // A failed check closes the channel it ran on, so it runs on a throwaway one\n // rather than take down the shared channel and every consumer on it.\n private async checkExchanges(exchanges: string[]): Promise<void> {\n if (!this.connection) {\n throw new Error('Connection not available')\n }\n const probe = await this.connection.createChannel()\n probe.on('error', () => undefined)\n try {\n for (const name of exchanges) {\n await probe.checkExchange(name)\n }\n } finally {\n await probe.close().catch(() => undefined)\n }\n }\n\n /**\n * Gates a delivered message on the subscription's concurrency semaphore, then\n * processes it. A message counts as in-flight from delivery until its handler\n * settles (including time spent waiting for a permit), so graceful `close()`\n * drains queued messages as well as actively-processing ones.\n */\n private dispatch(\n message: amqp.ConsumeMessage,\n handler: RabbitMQMessageHandler,\n semaphore: Semaphore | null,\n channel: amqp.ConfirmChannel,\n options: SubscribeOptions | undefined,\n ): void {\n this.inflightCount++\n const acquire = semaphore ? semaphore.acquire() : Promise.resolve()\n acquire\n .then(() => this.handleWithRetry(message, handler, channel, options))\n .catch((error) => {\n this.logger.error('Unhandled error in message handler', { error })\n })\n .finally(() => {\n semaphore?.release()\n this.inflightCount--\n if (this.inflightCount === 0 && this.drainResolve) {\n this.drainResolve()\n }\n })\n }\n\n private async handleWithRetry(\n message: amqp.ConsumeMessage,\n handler: RabbitMQMessageHandler,\n channel: amqp.ConfirmChannel,\n options: SubscribeOptions | undefined,\n ): Promise<void> {\n // `channel` is the one that delivered this message. If a reconnect has\n // since replaced it, this message was never acked and the broker will\n // redeliver it on the new channel, so drop this stale attempt rather than\n // run the handler again or ack a tag the new channel does not know.\n if (channel !== this.channel) {\n this.logger.warn('Skipping message from a superseded channel; it will be redelivered')\n return\n }\n const startTime = Date.now()\n let attempts = 0\n const routingKey = message.fields.routingKey\n const exchange = message.fields.exchange\n\n let content: RabbitMQMessage\n try {\n content = JSON.parse(message.content.toString())\n } catch (parseError) {\n const rawContent = message.content.toString()\n const rawPreview = rawContent.substring(0, 100)\n this.logger.error('Failed to parse message JSON, sending to DLQ', {\n error: parseError,\n exchange,\n routingKey,\n rawContentPreview: rawPreview + (rawContent.length > 100 ? '...' : ''),\n })\n if (this.settle(channel, message, 'nack')) {\n this.callHook(this.hooks.onMessageDlq, { exchange, routingKey, duration: 0, reason: 'invalid_json' })\n }\n return\n }\n\n this.logger.debug('Message received, processing', { exchange, routingKey, payload: content })\n\n const properties: RabbitMQMessageProperties = {\n exchange,\n routingKey,\n headers: message.properties.headers ?? {},\n timestamp: message.properties.timestamp,\n messageId: message.properties.messageId,\n correlationId: message.properties.correlationId,\n }\n\n const maxRetries = options?.maxRetries ?? this.options.maxRetries\n while (attempts < maxRetries) {\n // Only the handler call belongs in this try. Acking inside it would make\n // a dead channel look like a failed handler and re-run its side effects.\n try {\n await handler(content, properties)\n } catch (error) {\n attempts++\n // By name, not instanceof: the ESM and CJS builds each define their own class.\n if (error instanceof Error && error.name === 'DeadLetterError') {\n const duration = Date.now() - startTime\n this.logger.warn('Handler dead-lettered the message', { error: error.message, exchange, routingKey })\n if (this.settle(channel, message, 'nack')) {\n this.callHook(this.hooks.onMessageDlq, { exchange, routingKey, duration, reason: 'dead_letter_error' })\n }\n return\n }\n this.logger.error('Handler failed', {\n error: error instanceof Error ? error.message : error,\n stack: error instanceof Error ? error.stack : undefined,\n exchange,\n routingKey,\n attempt: attempts,\n maxRetries,\n })\n if (attempts < maxRetries) {\n await this.sleepUntilClose(this.retryDelayFor(attempts, options))\n if (this.closing || channel !== this.channel) {\n this.logger.warn('Stopped retrying a message on close or reconnect; it will be redelivered', { exchange, routingKey })\n return\n }\n }\n continue\n }\n const duration = Date.now() - startTime\n this.logger.info('Message processed successfully', { exchange, routingKey, duration, attempts: attempts + 1 })\n if (this.settle(channel, message, 'ack')) {\n this.callHook(this.hooks.onMessageProcessed, { exchange, routingKey, duration, attempts: attempts + 1 })\n }\n return\n }\n\n const duration = Date.now() - startTime\n this.logger.error('Message failed after max retries, sending to DLQ', { exchange, routingKey, maxRetries, duration })\n if (this.settle(channel, message, 'nack')) {\n this.callHook(this.hooks.onMessageDlq, { exchange, routingKey, duration, reason: 'max_retries_exhausted' })\n }\n }\n\n /**\n * Acks or nacks a delivery, reporting whether the broker accepted it.\n *\n * A settle failure is a transport error, never a handler failure: by this\n * point the handler has already run, so it must not be retried just because\n * the channel went away. The delivery stays unacked and the broker redelivers\n * it once the channel closes.\n */\n private settle(\n channel: amqp.ConfirmChannel,\n message: amqp.ConsumeMessage,\n action: 'ack' | 'nack',\n ): boolean {\n // A handler can run for minutes, so the channel that delivered this\n // message may have been replaced by a reconnect since the entry check.\n // Settling on it would throw, and replaying the delivery tag on the\n // current channel would settle an unrelated message, since tags are scoped\n // to the channel that issued them. The broker requeues a closed channel's\n // unacked deliveries, so let it be redelivered instead.\n if (channel !== this.channel) {\n this.logger.warn('Skipping message from a superseded channel; it will be redelivered')\n return false\n }\n try {\n if (action === 'ack') {\n channel.ack(message)\n } else {\n channel.nack(message, false, false)\n }\n return true\n } catch (error) {\n this.logger.error('Failed to settle message; leaving it unacked for redelivery', {\n error,\n action,\n exchange: message.fields.exchange,\n routingKey: message.fields.routingKey,\n })\n return false\n }\n }\n\n /**\n * Lightweight liveness probe: creates and immediately deletes a temporary\n * exclusive queue. Returns `false` when disconnected or on broker error.\n */\n async checkHealth(): Promise<boolean> {\n if (!this.connected || !this.channel) {\n return false\n }\n try {\n const { queue } = await this.channel.assertQueue('', {\n exclusive: true,\n autoDelete: true,\n })\n await this.channel.deleteQueue(queue)\n return true\n } catch (error) {\n this.logger.warn('Health check failed', { error })\n return false\n }\n }\n\n private waitForDrain(timeout: number): Promise<void> {\n if (this.inflightCount === 0) return Promise.resolve()\n return new Promise<void>((resolve) => {\n const timer = setTimeout(() => {\n this.logger.warn('Close timeout reached with messages still in flight', { inflightCount: this.inflightCount })\n this.drainResolve = null\n resolve()\n }, timeout)\n this.drainResolve = () => {\n clearTimeout(timer)\n this.drainResolve = null\n resolve()\n }\n })\n }\n\n /** Invokes a hook callback, swallowing errors so hooks never break message flow. */\n private callHook<T>(hook: ((info: T) => void) | undefined, info: T): void {\n try { hook?.(info) } catch (error) { this.logger.debug('Observability hook error', { error }) }\n }\n\n private sleep(ms: number): Promise<void> {\n return new Promise((resolve) => setTimeout(resolve, ms))\n }\n\n private retryDelayFor(attempts: number, options: SubscribeOptions | undefined): number {\n const { retryDelay } = this.options\n if (options?.maxRetryDelay === undefined) return retryDelay\n return backoff(retryDelay, attempts, options.maxRetryDelay)\n }\n\n private wakeRetries(): void {\n for (const wake of [...this.retryWakers]) wake()\n }\n\n /** Also cut short by a reconnect, via wakeRetries(). */\n private sleepUntilClose(ms: number): Promise<void> {\n if (this.closing) return Promise.resolve()\n return new Promise((resolve) => {\n const wake = () => {\n clearTimeout(timer)\n this.retryWakers.delete(wake)\n resolve()\n }\n const timer = setTimeout(wake, ms)\n this.retryWakers.add(wake)\n })\n }\n}\n","import type { ILogger } from './types.js'\n\n/**\n * Default logger used when the consumer does not inject one.\n *\n * Forwards `warn` and `error` to the console so connection drops, channel\n * errors, and failed handlers stay visible out of the box, while staying\n * silent for `info`/`debug` so routine operation never pollutes the\n * consumer's output. Pass `silentLogger` for zero output, or any `ILogger`\n * (console, Winston, pino, Bunyan) to take full control.\n */\nexport const defaultLogger: ILogger = {\n debug: () => {},\n info: () => {},\n warn: (message, ...args) => console.warn('[rabbitmq-client]', message, ...args),\n error: (message, ...args) => console.error('[rabbitmq-client]', message, ...args),\n}\n\ntype Level = keyof ILogger\ntype ObjectFirst = (context: object, message: string) => void\n\n/**\n * pino takes `(context, message)` and drops a context passed after the\n * message, and only serializes an Error under `err`. It is the one logger\n * with a `bindings()` method, so it is recognised by that and called its way.\n */\nexport const forLibraryCalls = (logger: ILogger): ILogger => {\n if (typeof (logger as { bindings?: unknown }).bindings !== 'function') return logger\n const call =\n (level: Level) =>\n (message: string, context?: unknown): void => {\n const log = (logger[level] as unknown as ObjectFirst).bind(logger)\n if (typeof context !== 'object' || context === null) return log({}, message)\n const { error, ...rest } = context as { error?: unknown }\n log(error === undefined ? rest : { ...rest, err: error }, message)\n }\n return { debug: call('debug'), info: call('info'), warn: call('warn'), error: call('error') }\n}\n\n/**\n * No-op logger that discards every message. Use this to silence the client\n * entirely, including the `warn`/`error` output the default logger emits.\n */\nexport const silentLogger: ILogger = {\n debug: () => {},\n info: () => {},\n warn: () => {},\n error: () => {},\n}\n","/**\n * Minimal FIFO counting semaphore used to cap how many message handlers run\n * concurrently, independently of channel prefetch.\n *\n * Prefetch bounds how many messages the broker delivers into consumer memory;\n * the semaphore bounds how many of those are actively being processed at once.\n * Keeping the two separate lets a consumer buffer a large prefetch window while\n * still protecting a slow downstream (a database, an HTTP API) from a burst.\n */\nexport class Semaphore {\n private permits: number\n private readonly waiters: Array<() => void> = []\n\n constructor(permits: number) {\n if (!Number.isInteger(permits) || permits < 1) {\n throw new Error(`Semaphore requires a positive integer of permits, got ${permits}`)\n }\n this.permits = permits\n }\n\n /**\n * Acquires a permit, resolving immediately when one is free or queueing\n * (FIFO) until another holder releases. Always pair with `release()` in a\n * `finally` so a throwing handler never leaks a permit.\n */\n acquire(): Promise<void> {\n if (this.permits > 0) {\n this.permits--\n return Promise.resolve()\n }\n return new Promise<void>((resolve) => {\n this.waiters.push(resolve)\n })\n }\n\n /**\n * Releases a permit. Hands it directly to the next waiter if any are queued,\n * otherwise returns it to the pool.\n */\n release(): void {\n const next = this.waiters.shift()\n if (next) {\n next()\n } else {\n this.permits++\n }\n }\n\n /** Permits currently available. Exposed for tests and introspection. */\n get available(): number {\n return this.permits\n }\n\n /** Number of callers currently queued waiting for a permit. */\n get waiting(): number {\n return this.waiters.length\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,qBAAiB;;;ACWV,IAAM,gBAAyB;AAAA,EACpC,OAAO,MAAM;AAAA,EAAC;AAAA,EACd,MAAM,MAAM;AAAA,EAAC;AAAA,EACb,MAAM,CAAC,YAAY,SAAS,QAAQ,KAAK,qBAAqB,SAAS,GAAG,IAAI;AAAA,EAC9E,OAAO,CAAC,YAAY,SAAS,QAAQ,MAAM,qBAAqB,SAAS,GAAG,IAAI;AAClF;AAUO,IAAM,kBAAkB,CAAC,WAA6B;AAC3D,MAAI,OAAQ,OAAkC,aAAa,WAAY,QAAO;AAC9E,QAAM,OACJ,CAAC,UACD,CAAC,SAAiB,YAA4B;AAC5C,UAAM,MAAO,OAAO,KAAK,EAA6B,KAAK,MAAM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":["amqp","name","duration"]}
package/dist/index.d.cts CHANGED
@@ -1,5 +1,5 @@
1
- import { R as RabbitMQClientOptions, a as RabbitMQMessage, P as PublishOptions, b as RabbitMQMessageHandler, S as SubscribeOptions, I as ILogger } from './types-Cjg-65Ef.cjs';
2
- export { c as RabbitMQBinding, d as RabbitMQHooks, e as RabbitMQMessageProperties, f as RabbitMQSubscription } from './types-Cjg-65Ef.cjs';
1
+ import { R as RabbitMQClientOptions, a as RabbitMQMessage, P as PublishOptions, b as RabbitMQMessageHandler, S as SubscribeOptions, I as ILogger } from './types-A2viogDe.cjs';
2
+ export { c as RabbitMQBinding, d as RabbitMQHooks, e as RabbitMQMessageProperties, f as RabbitMQSubscription } from './types-A2viogDe.cjs';
3
3
 
4
4
  /** A `mandatory` publish the broker could route to no queue. */
5
5
  declare class UnroutableMessageError extends Error {
package/dist/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- import { R as RabbitMQClientOptions, a as RabbitMQMessage, P as PublishOptions, b as RabbitMQMessageHandler, S as SubscribeOptions, I as ILogger } from './types-Cjg-65Ef.js';
2
- export { c as RabbitMQBinding, d as RabbitMQHooks, e as RabbitMQMessageProperties, f as RabbitMQSubscription } from './types-Cjg-65Ef.js';
1
+ import { R as RabbitMQClientOptions, a as RabbitMQMessage, P as PublishOptions, b as RabbitMQMessageHandler, S as SubscribeOptions, I as ILogger } from './types-A2viogDe.js';
2
+ export { c as RabbitMQBinding, d as RabbitMQHooks, e as RabbitMQMessageProperties, f as RabbitMQSubscription } from './types-A2viogDe.js';
3
3
 
4
4
  /** A `mandatory` publish the broker could route to no queue. */
5
5
  declare class UnroutableMessageError extends Error {
package/dist/index.js CHANGED
@@ -471,13 +471,15 @@ var RabbitMQClient = class {
471
471
  await channel.assertExchange(dlxExchange, "topic", { durable: true });
472
472
  await channel.assertQueue(dlqQueue, { durable: true });
473
473
  await channel.bindQueue(dlqQueue, dlxExchange, dlqRoutingKey);
474
- const missing = bindings.map((b) => b.exchange).filter((name) => !this.assertedExchanges.has(name));
475
- if (options?.passiveExchanges && missing.length > 0) {
476
- await this.checkExchanges(missing);
477
- } else {
478
- for (const name of missing) {
479
- await channel.assertExchange(name, "topic", { durable: true });
480
- }
474
+ const passive = options?.passiveExchanges;
475
+ const isPassive = (name) => passive === true || Array.isArray(passive) && passive.includes(name);
476
+ const missing = [...new Set(bindings.map((b) => b.exchange))].filter((name) => !this.assertedExchanges.has(name));
477
+ const toCheck = missing.filter(isPassive);
478
+ if (toCheck.length > 0) {
479
+ await this.checkExchanges(toCheck);
480
+ }
481
+ for (const name of missing.filter((name2) => !isPassive(name2))) {
482
+ await channel.assertExchange(name, "topic", { durable: true });
481
483
  }
482
484
  for (const name of missing) this.assertedExchanges.add(name);
483
485
  const queueType = options?.queueArguments?.["x-queue-type"] ?? "quorum";
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/client.ts","../src/logger.ts","../src/semaphore.ts"],"sourcesContent":["import amqp from 'amqplib'\nimport type {\n ILogger,\n PublishOptions,\n RabbitMQClientOptions,\n RabbitMQHooks,\n RabbitMQMessage,\n RabbitMQMessageHandler,\n RabbitMQMessageProperties,\n RabbitMQSubscription,\n SubscribeOptions,\n} from './types.js'\nimport { defaultLogger, forLibraryCalls } from './logger.js'\nimport { Semaphore } from './semaphore.js'\n\nconst DEFAULT_PREFETCH = 10\nconst MAX_PUBLISH_RETRY_DELAY_MS = 60_000\n\nconst DEFAULTS = {\n maxRetries: 3,\n retryDelay: 1000,\n connectionRetryDelay: 5000,\n initMaxAttempts: 5,\n publishMaxAttempts: 5,\n prefetch: DEFAULT_PREFETCH,\n closeTimeout: 5000,\n} as const\n\nfunction backoff(baseDelay: number, attempt: number, maxDelay: number): number {\n return Math.min(baseDelay * Math.pow(2, attempt - 1), maxDelay)\n}\n\n/** A `mandatory` publish the broker could route to no queue. */\nexport class UnroutableMessageError extends Error {\n constructor(\n readonly exchange: string,\n readonly routingKey: string,\n ) {\n super(`No queue is bound to receive ${exchange || '(default exchange)'}/${routingKey}`)\n this.name = 'UnroutableMessageError'\n }\n}\n\n/** Thrown by a handler to send its message to the DLQ at once, without retrying. */\nexport class DeadLetterError extends Error {\n constructor(message: string, options?: { cause?: unknown }) {\n super(message, options)\n this.name = 'DeadLetterError'\n }\n}\n\n/**\n * Production-grade RabbitMQ client with confirm channels, automatic DLQ\n * infrastructure, reconnection with subscription restoration, and\n * exponential-backoff publishing.\n *\n * @example\n * ```ts\n * const client = new RabbitMQClient({ url: 'amqp://localhost' })\n * await client.init()\n * await client.publish('events', 'user.created', { userId: '123' })\n * await client.subscribe('events', 'user.created', 'user-service', handler)\n * ```\n */\nexport class RabbitMQClient {\n private connection: amqp.ChannelModel | null = null\n private channel: amqp.ConfirmChannel | null = null\n private connected = false\n private closing = false\n private subscriptions: RabbitMQSubscription[] = []\n private readonly options: Required<Omit<RabbitMQClientOptions, 'logger' | 'hooks'>>\n private readonly logger: ILogger\n private readonly hooks: RabbitMQHooks\n private initializationPromise: Promise<void> | null = null\n private reconnectionPromise: Promise<void> | null = null\n private assertedExchanges = new Set<string>()\n // One per channel, so a late return on a replaced channel reaches no waiter.\n private returnWaiters = new Set<(msg: amqp.Message) => void>()\n private consumerTags = new Map<string, string>()\n // Per-queue concurrency limiter, keyed by queue name. Persisted across\n // reconnects (unlike consumerTags) so handlers still running when a\n // connection drops keep holding their permits, and the new consumer cannot\n // exceed the ceiling while the old pipeline drains. `null` = no limit.\n private subscriptionSemaphores = new Map<string, Semaphore | null>()\n private retryWakers = new Set<() => void>()\n private inflightCount = 0\n private drainResolve: (() => void) | null = null\n\n constructor(options: RabbitMQClientOptions) {\n this.options = {\n url: options.url,\n maxRetries: options.maxRetries ?? DEFAULTS.maxRetries,\n retryDelay: options.retryDelay ?? DEFAULTS.retryDelay,\n connectionRetryDelay: options.connectionRetryDelay ?? DEFAULTS.connectionRetryDelay,\n initMaxAttempts: options.initMaxAttempts ?? DEFAULTS.initMaxAttempts,\n publishMaxAttempts: options.publishMaxAttempts ?? DEFAULTS.publishMaxAttempts,\n prefetch: options.prefetch ?? DEFAULTS.prefetch,\n // Default concurrency tracks prefetch, preserving the previous behaviour\n // (handlers ran fire-and-forget, so up to `prefetch` ran at once) while\n // making the ceiling explicit and independently tunable. A value <= 0\n // (including an unlimited `prefetch: 0`) means \"no concurrency limit\".\n concurrency: options.concurrency ?? options.prefetch ?? DEFAULTS.prefetch,\n closeTimeout: options.closeTimeout ?? DEFAULTS.closeTimeout,\n }\n this.logger = forLibraryCalls(options.logger ?? defaultLogger)\n this.hooks = options.hooks ?? {}\n }\n\n /**\n * Opens a connection and creates a confirm channel. Retries up to\n * `initMaxAttempts` times, then throws. Idempotent and safe to call\n * concurrently — duplicate calls share the same in-flight promise.\n */\n async init(): Promise<void> {\n if (this.connection && this.connected) {\n return\n }\n if (this.initializationPromise) {\n return this.initializationPromise\n }\n // Attach a no-op catch to prevent Node from briefly treating this as an\n // unhandled rejection before the await below registers its own handler.\n this.initializationPromise = this.doConnect(this.options.initMaxAttempts)\n this.initializationPromise.catch(() => undefined)\n try {\n await this.initializationPromise\n } finally {\n this.initializationPromise = null\n }\n }\n\n private async doConnect(maxAttempts?: number): Promise<void> {\n this.assertedExchanges.clear()\n this.consumerTags.clear()\n // A channel failure leaves its connection open; drop it so it does not\n // outlive the client and keep the process alive.\n this.connection?.close().catch(() => undefined)\n this.connection = null\n let attempts = 0\n while (!this.connected && !this.closing) {\n try {\n const connection = await amqp.connect(this.options.url)\n this.connection = connection\n this.logger.info('Connected to server')\n const channel = await connection.createConfirmChannel()\n this.channel = channel\n // Retries sleeping on the replaced channel give up; the broker redelivers their messages.\n this.wakeRetries()\n const returnWaiters = new Set<(msg: amqp.Message) => void>()\n this.returnWaiters = returnWaiters\n channel.on('return', (msg: amqp.Message) => {\n for (const waiter of returnWaiters) waiter(msg)\n })\n this.logger.info('Confirm channel created')\n await channel.prefetch(this.options.prefetch)\n this.logger.info('Channel prefetch set', { prefetch: this.options.prefetch })\n\n // Events from a connection or channel a reconnect has replaced must not\n // tear down the current one.\n connection.on('error', (error: Error) => {\n if (connection !== this.connection) return\n this.connected = false\n this.logger.error('Connection error', { error })\n })\n connection.on('close', () => {\n if (connection !== this.connection) return\n this.connected = false\n this.logger.warn('Connection closed')\n this.reconnectWithRetry()\n })\n channel.on('error', (error: Error) => {\n if (channel !== this.channel) return\n this.logger.error('Channel error', { error })\n this.handleChannelFailure()\n })\n channel.on('close', () => {\n if (channel !== this.channel) return\n this.logger.warn('Channel closed')\n this.handleChannelFailure()\n })\n\n this.connected = true\n this.logger.info('Client initialized successfully')\n } catch (error) {\n attempts++\n this.connected = false\n if (maxAttempts !== undefined && attempts >= maxAttempts) {\n this.logger.error('Connection failed after maximum attempts', { error, attempts, maxAttempts })\n throw new Error(\n `Failed to connect to RabbitMQ after ${attempts} attempts. ` +\n 'Check RABBITMQ_URL configuration and RabbitMQ server availability.',\n )\n }\n this.logger.warn('Connection attempt failed, retrying...', {\n error,\n attempt: attempts,\n maxAttempts: maxAttempts ?? 'unlimited',\n retryDelayMs: this.options.connectionRetryDelay,\n })\n await this.sleep(this.options.connectionRetryDelay)\n }\n }\n if (!this.connected) {\n throw new Error('RabbitMQ client closed while connecting')\n }\n }\n\n private handleChannelFailure(): void {\n if (this.connected) {\n this.connected = false\n this.reconnectWithRetry()\n }\n }\n\n /**\n * Returns whether the client currently has an active connection and channel.\n */\n isConnected(): boolean {\n return this.connected\n }\n\n /**\n * Publishes a JSON message to a topic exchange with publisher confirms, or\n * straight to a queue through the default exchange (`''`, routing key = queue name).\n * Retries with exponential backoff (capped at 60 s), forcing a reconnect\n * on each failure. Throws after `publishMaxAttempts` exhausted.\n */\n async publish(\n exchange: string,\n routingKey: string,\n message: RabbitMQMessage,\n options?: PublishOptions,\n ): Promise<void> {\n let attempts = 0\n const maxAttempts = options?.maxAttempts ?? this.options.publishMaxAttempts\n const baseDelay = this.options.connectionRetryDelay\n const content = Buffer.from(JSON.stringify(message))\n // Taken once, not per attempt: a retried publish must not look newer than\n // a message published after it succeeded.\n const timestamp = Math.floor(Date.now() / 1000)\n\n while (attempts < maxAttempts) {\n try {\n if (!this.connected) {\n await this.reconnectWithRetry()\n }\n\n if (!this.channel) {\n throw new Error('Channel not available')\n }\n\n // The default exchange ('') always exists and the broker refuses to\n // declare it, so publishing straight to a queue skips the assertion.\n if (exchange !== '' && !this.assertedExchanges.has(exchange)) {\n await this.channel.assertExchange(exchange, 'topic', { durable: true })\n this.assertedExchanges.add(exchange)\n }\n\n const channel = this.channel\n const returnWaiters = this.returnWaiters\n // The broker sends basic.return before the confirm of the same message,\n // so the flag is settled by the time waitForConfirms resolves. A return\n // carries no delivery tag, so it is matched on where it was sent plus the\n // message id, or the content when there is none.\n let returned = false\n const messageId = options?.messageId\n const onReturn = (msg: amqp.Message) => {\n if (msg.fields.exchange !== exchange || msg.fields.routingKey !== routingKey) return\n if (messageId !== undefined ? msg.properties.messageId === messageId : msg.content.equals(content)) {\n returned = true\n }\n }\n if (options?.mandatory) returnWaiters.add(onReturn)\n\n try {\n channel.publish(exchange, routingKey, content, {\n persistent: true,\n timestamp,\n headers: options?.headers,\n correlationId: options?.correlationId,\n messageId: options?.messageId,\n expiration: options?.expiration,\n mandatory: options?.mandatory,\n })\n\n await channel.waitForConfirms()\n } finally {\n returnWaiters.delete(onReturn)\n }\n\n if (returned) {\n throw new UnroutableMessageError(exchange, routingKey)\n }\n\n if (attempts > 0) {\n this.logger.info('Published message after retries', { exchange, routingKey, messageSize: content.length, attempts })\n } else {\n this.logger.info('Published message', { exchange, routingKey, messageSize: content.length })\n }\n this.logger.debug('Published message payload', { exchange, routingKey, payload: message })\n\n this.callHook(this.hooks.onPublish, { exchange, routingKey, attempts: attempts + 1 })\n\n return\n } catch (error) {\n // The broker answered, and the same routing would fail the same way.\n if (error instanceof UnroutableMessageError) {\n this.logger.warn('Published message was unroutable', { exchange, routingKey })\n throw error\n }\n\n attempts++\n this.connected = false\n\n if (attempts >= maxAttempts) {\n this.logger.error('Publish failed after max attempts', { error, exchange, routingKey, attempts, maxAttempts })\n throw new Error(\n `Failed to publish to ${exchange || '(default exchange)'}/${routingKey} after ${attempts} attempts: ${error instanceof Error ? error.message : String(error)}`,\n )\n }\n\n const retryDelay = backoff(baseDelay, attempts, MAX_PUBLISH_RETRY_DELAY_MS)\n\n this.logger.warn('Publish attempt failed, retrying', { error, exchange, routingKey, attempt: attempts, maxAttempts, retryDelayMs: retryDelay })\n\n await this.sleep(retryDelay)\n }\n }\n }\n\n /**\n * Gracefully shuts down the client. Waits up to `closeTimeout` ms for\n * in-flight message handlers to finish before closing the channel and\n * connection. Pass `false` to preserve subscriptions for a later\n * `init()` / reconnect cycle.\n */\n async close(clearSubscriptions = true): Promise<void> {\n // Closing the channel fires its 'close' handler, which would otherwise\n // reconnect and leave a connection behind that keeps the process alive.\n this.closing = true\n try {\n // A connect already running gives up after its current attempt; whatever\n // it opened is closed below.\n await Promise.allSettled([this.initializationPromise, this.reconnectionPromise])\n this.wakeRetries()\n if (this.inflightCount > 0) {\n this.logger.info('Waiting for in-flight messages to drain', { inflightCount: this.inflightCount })\n await this.waitForDrain(this.options.closeTimeout)\n }\n try {\n await this.channel?.close()\n } finally {\n await this.connection?.close()\n }\n this.connection = null\n this.channel = null\n this.connected = false\n this.initializationPromise = null\n this.reconnectionPromise = null\n this.assertedExchanges.clear()\n this.consumerTags.clear()\n if (clearSubscriptions) {\n this.subscriptions = []\n this.subscriptionSemaphores.clear()\n }\n this.logger.info('Connection closed')\n } catch (error) {\n this.logger.error('Error closing connection', { error })\n throw error\n } finally {\n this.closing = false\n }\n }\n\n private async reconnectWithRetry(): Promise<void> {\n if (this.closing) return\n if (this.reconnectionPromise) {\n return this.reconnectionPromise\n }\n this.connected = false\n this.logger.info('Starting reconnection...')\n this.reconnectionPromise = this.doReconnect()\n this.reconnectionPromise.catch(() => undefined)\n try {\n await this.reconnectionPromise\n } catch (error) {\n // The event handlers call this without awaiting it, so a reconnect that\n // close() interrupted must not surface as an unhandled rejection.\n if (!this.closing) throw error\n } finally {\n this.reconnectionPromise = null\n }\n }\n\n private async doReconnect(): Promise<void> {\n await this.doConnect()\n await this.resubscribeAll()\n }\n\n private async resubscribeAll(): Promise<void> {\n if (this.subscriptions.length === 0) {\n return\n }\n this.logger.info('Re-establishing subscriptions', { count: this.subscriptions.length })\n const subs = [...this.subscriptions]\n const results = await Promise.allSettled(\n subs.map(async (sub) => {\n await this.setupSubscription(sub)\n return sub.queue\n }),\n )\n const succeeded: string[] = []\n const failed: string[] = []\n results.forEach((result, index) => {\n if (result.status === 'fulfilled') {\n succeeded.push(result.value)\n } else {\n const queue = subs[index].queue\n failed.push(queue)\n this.logger.error('Failed to re-subscribe to queue', { error: result.reason, queue })\n }\n })\n if (succeeded.length > 0) {\n this.logger.info('Successfully re-subscribed to queues', { count: succeeded.length, queues: succeeded })\n }\n if (failed.length > 0) {\n this.logger.warn('Some subscriptions failed to restore', { count: failed.length, queues: failed })\n }\n this.callHook(this.hooks.onReconnect, { subscriptionsRestored: succeeded.length, subscriptionsFailed: failed.length })\n }\n\n /**\n * Subscribes to a queue with automatic DLQ infrastructure setup.\n *\n * Pass `options.queueArguments` to override the default quorum-queue\n * arguments (merged with the DLQ wiring defaults).\n */\n async subscribe(\n exchange: string,\n routingKey: string,\n queue: string,\n handler: RabbitMQMessageHandler,\n options?: SubscribeOptions,\n ): Promise<void> {\n if (!this.connected || !this.channel) {\n throw new Error('RabbitMQ client not connected. Call init() first.')\n }\n const sub: RabbitMQSubscription = { exchange, routingKey, queue, handler, options }\n const existingIndex = this.subscriptions.findIndex((s) => s.queue === queue)\n const previous = existingIndex === -1 ? undefined : this.subscriptions[existingIndex]\n if (existingIndex === -1) {\n this.subscriptions.push(sub)\n } else {\n this.subscriptions[existingIndex] = sub\n // Re-subscribing may change concurrency; drop the old limiter so\n // setupSubscription rebuilds it from the new options. (A reconnect goes\n // through setupSubscription directly and keeps the existing limiter.)\n this.subscriptionSemaphores.delete(queue)\n }\n try {\n await this.setupSubscription(sub)\n } catch (error) {\n // A subscription that cannot be set up must not be retried on every reconnect.\n this.subscriptions = previous\n ? this.subscriptions.map((s) => (s === sub ? previous : s))\n : this.subscriptions.filter((s) => s !== sub)\n throw error\n }\n }\n\n /**\n * Cancels a queue subscription and removes it from the restoration list.\n */\n async unsubscribe(queue: string): Promise<void> {\n const tag = this.consumerTags.get(queue)\n if (tag && this.channel) {\n await this.channel.cancel(tag)\n }\n this.consumerTags.delete(queue)\n this.subscriptionSemaphores.delete(queue)\n this.subscriptions = this.subscriptions.filter((s) => s.queue !== queue)\n this.logger.info('Unsubscribed from queue', { queue })\n }\n\n private async setupSubscription(sub: RabbitMQSubscription): Promise<void> {\n if (!this.channel) {\n throw new Error('Channel not available')\n }\n // Bind this consumer to the exact channel that will deliver its messages,\n // so ack/nack always target that channel even after a reconnect swaps\n // `this.channel`.\n const channel = this.channel\n const { exchange, routingKey, queue, handler, options } = sub\n const dlxExchange = options?.deadLetterExchange ?? `${exchange}.dlx`\n const dlqQueue = `${queue}.dlq`\n // Set once on the queue, so every binding's dead letters reach the DLQ.\n const dlqRoutingKey = `${routingKey}.dead`\n const bindings = [{ exchange, routingKey }, ...(options?.bindings ?? [])]\n\n await channel.assertExchange(dlxExchange, 'topic', { durable: true })\n await channel.assertQueue(dlqQueue, { durable: true })\n await channel.bindQueue(dlqQueue, dlxExchange, dlqRoutingKey)\n\n const missing = bindings.map((b) => b.exchange).filter((name) => !this.assertedExchanges.has(name))\n if (options?.passiveExchanges && missing.length > 0) {\n await this.checkExchanges(missing)\n } else {\n for (const name of missing) {\n await channel.assertExchange(name, 'topic', { durable: true })\n }\n }\n for (const name of missing) this.assertedExchanges.add(name)\n\n const queueType = options?.queueArguments?.['x-queue-type'] ?? 'quorum'\n const queueArgs: Record<string, unknown> = {\n 'x-queue-type': queueType,\n 'x-overflow': 'reject-publish',\n }\n // at-least-once DLQ strategy is only supported by quorum queues\n if (queueType === 'quorum') {\n queueArgs['x-dead-letter-strategy'] = 'at-least-once'\n }\n\n await channel.assertQueue(queue, {\n durable: true,\n deadLetterExchange: dlxExchange,\n deadLetterRoutingKey: dlqRoutingKey,\n arguments: { ...queueArgs, ...options?.queueArguments },\n })\n\n for (const binding of bindings) {\n await channel.bindQueue(queue, binding.exchange, binding.routingKey)\n }\n // Reuse the queue's existing limiter across reconnects; only build a new\n // one the first time (or after subscribe()/unsubscribe() cleared it).\n // A concurrency <= 0 means \"no limit\" (null), matching an unlimited prefetch.\n if (!this.subscriptionSemaphores.has(queue)) {\n const concurrency = options?.concurrency ?? this.options.concurrency\n this.subscriptionSemaphores.set(queue, concurrency > 0 ? new Semaphore(concurrency) : null)\n }\n const semaphore = this.subscriptionSemaphores.get(queue) ?? null\n const { consumerTag } = await channel.consume(\n queue,\n (message) => {\n if (message) {\n this.dispatch(message, handler, semaphore, channel, options)\n }\n },\n { noAck: false },\n )\n this.consumerTags.set(queue, consumerTag)\n this.logger.info('Subscribed to queue', { queue, bindings })\n }\n\n // A failed check closes the channel it ran on, so it runs on a throwaway one\n // rather than take down the shared channel and every consumer on it.\n private async checkExchanges(exchanges: string[]): Promise<void> {\n if (!this.connection) {\n throw new Error('Connection not available')\n }\n const probe = await this.connection.createChannel()\n probe.on('error', () => undefined)\n try {\n for (const name of exchanges) {\n await probe.checkExchange(name)\n }\n } finally {\n await probe.close().catch(() => undefined)\n }\n }\n\n /**\n * Gates a delivered message on the subscription's concurrency semaphore, then\n * processes it. A message counts as in-flight from delivery until its handler\n * settles (including time spent waiting for a permit), so graceful `close()`\n * drains queued messages as well as actively-processing ones.\n */\n private dispatch(\n message: amqp.ConsumeMessage,\n handler: RabbitMQMessageHandler,\n semaphore: Semaphore | null,\n channel: amqp.ConfirmChannel,\n options: SubscribeOptions | undefined,\n ): void {\n this.inflightCount++\n const acquire = semaphore ? semaphore.acquire() : Promise.resolve()\n acquire\n .then(() => this.handleWithRetry(message, handler, channel, options))\n .catch((error) => {\n this.logger.error('Unhandled error in message handler', { error })\n })\n .finally(() => {\n semaphore?.release()\n this.inflightCount--\n if (this.inflightCount === 0 && this.drainResolve) {\n this.drainResolve()\n }\n })\n }\n\n private async handleWithRetry(\n message: amqp.ConsumeMessage,\n handler: RabbitMQMessageHandler,\n channel: amqp.ConfirmChannel,\n options: SubscribeOptions | undefined,\n ): Promise<void> {\n // `channel` is the one that delivered this message. If a reconnect has\n // since replaced it, this message was never acked and the broker will\n // redeliver it on the new channel, so drop this stale attempt rather than\n // run the handler again or ack a tag the new channel does not know.\n if (channel !== this.channel) {\n this.logger.warn('Skipping message from a superseded channel; it will be redelivered')\n return\n }\n const startTime = Date.now()\n let attempts = 0\n const routingKey = message.fields.routingKey\n const exchange = message.fields.exchange\n\n let content: RabbitMQMessage\n try {\n content = JSON.parse(message.content.toString())\n } catch (parseError) {\n const rawContent = message.content.toString()\n const rawPreview = rawContent.substring(0, 100)\n this.logger.error('Failed to parse message JSON, sending to DLQ', {\n error: parseError,\n exchange,\n routingKey,\n rawContentPreview: rawPreview + (rawContent.length > 100 ? '...' : ''),\n })\n if (this.settle(channel, message, 'nack')) {\n this.callHook(this.hooks.onMessageDlq, { exchange, routingKey, duration: 0, reason: 'invalid_json' })\n }\n return\n }\n\n this.logger.debug('Message received, processing', { exchange, routingKey, payload: content })\n\n const properties: RabbitMQMessageProperties = {\n exchange,\n routingKey,\n headers: message.properties.headers ?? {},\n timestamp: message.properties.timestamp,\n messageId: message.properties.messageId,\n correlationId: message.properties.correlationId,\n }\n\n const maxRetries = options?.maxRetries ?? this.options.maxRetries\n while (attempts < maxRetries) {\n // Only the handler call belongs in this try. Acking inside it would make\n // a dead channel look like a failed handler and re-run its side effects.\n try {\n await handler(content, properties)\n } catch (error) {\n attempts++\n // By name, not instanceof: the ESM and CJS builds each define their own class.\n if (error instanceof Error && error.name === 'DeadLetterError') {\n const duration = Date.now() - startTime\n this.logger.warn('Handler dead-lettered the message', { error: error.message, exchange, routingKey })\n if (this.settle(channel, message, 'nack')) {\n this.callHook(this.hooks.onMessageDlq, { exchange, routingKey, duration, reason: 'dead_letter_error' })\n }\n return\n }\n this.logger.error('Handler failed', {\n error: error instanceof Error ? error.message : error,\n stack: error instanceof Error ? error.stack : undefined,\n exchange,\n routingKey,\n attempt: attempts,\n maxRetries,\n })\n if (attempts < maxRetries) {\n await this.sleepUntilClose(this.retryDelayFor(attempts, options))\n if (this.closing || channel !== this.channel) {\n this.logger.warn('Stopped retrying a message on close or reconnect; it will be redelivered', { exchange, routingKey })\n return\n }\n }\n continue\n }\n const duration = Date.now() - startTime\n this.logger.info('Message processed successfully', { exchange, routingKey, duration, attempts: attempts + 1 })\n if (this.settle(channel, message, 'ack')) {\n this.callHook(this.hooks.onMessageProcessed, { exchange, routingKey, duration, attempts: attempts + 1 })\n }\n return\n }\n\n const duration = Date.now() - startTime\n this.logger.error('Message failed after max retries, sending to DLQ', { exchange, routingKey, maxRetries, duration })\n if (this.settle(channel, message, 'nack')) {\n this.callHook(this.hooks.onMessageDlq, { exchange, routingKey, duration, reason: 'max_retries_exhausted' })\n }\n }\n\n /**\n * Acks or nacks a delivery, reporting whether the broker accepted it.\n *\n * A settle failure is a transport error, never a handler failure: by this\n * point the handler has already run, so it must not be retried just because\n * the channel went away. The delivery stays unacked and the broker redelivers\n * it once the channel closes.\n */\n private settle(\n channel: amqp.ConfirmChannel,\n message: amqp.ConsumeMessage,\n action: 'ack' | 'nack',\n ): boolean {\n // A handler can run for minutes, so the channel that delivered this\n // message may have been replaced by a reconnect since the entry check.\n // Settling on it would throw, and replaying the delivery tag on the\n // current channel would settle an unrelated message, since tags are scoped\n // to the channel that issued them. The broker requeues a closed channel's\n // unacked deliveries, so let it be redelivered instead.\n if (channel !== this.channel) {\n this.logger.warn('Skipping message from a superseded channel; it will be redelivered')\n return false\n }\n try {\n if (action === 'ack') {\n channel.ack(message)\n } else {\n channel.nack(message, false, false)\n }\n return true\n } catch (error) {\n this.logger.error('Failed to settle message; leaving it unacked for redelivery', {\n error,\n action,\n exchange: message.fields.exchange,\n routingKey: message.fields.routingKey,\n })\n return false\n }\n }\n\n /**\n * Lightweight liveness probe: creates and immediately deletes a temporary\n * exclusive queue. Returns `false` when disconnected or on broker error.\n */\n async checkHealth(): Promise<boolean> {\n if (!this.connected || !this.channel) {\n return false\n }\n try {\n const { queue } = await this.channel.assertQueue('', {\n exclusive: true,\n autoDelete: true,\n })\n await this.channel.deleteQueue(queue)\n return true\n } catch (error) {\n this.logger.warn('Health check failed', { error })\n return false\n }\n }\n\n private waitForDrain(timeout: number): Promise<void> {\n if (this.inflightCount === 0) return Promise.resolve()\n return new Promise<void>((resolve) => {\n const timer = setTimeout(() => {\n this.logger.warn('Close timeout reached with messages still in flight', { inflightCount: this.inflightCount })\n this.drainResolve = null\n resolve()\n }, timeout)\n this.drainResolve = () => {\n clearTimeout(timer)\n this.drainResolve = null\n resolve()\n }\n })\n }\n\n /** Invokes a hook callback, swallowing errors so hooks never break message flow. */\n private callHook<T>(hook: ((info: T) => void) | undefined, info: T): void {\n try { hook?.(info) } catch (error) { this.logger.debug('Observability hook error', { error }) }\n }\n\n private sleep(ms: number): Promise<void> {\n return new Promise((resolve) => setTimeout(resolve, ms))\n }\n\n private retryDelayFor(attempts: number, options: SubscribeOptions | undefined): number {\n const { retryDelay } = this.options\n if (options?.maxRetryDelay === undefined) return retryDelay\n return backoff(retryDelay, attempts, options.maxRetryDelay)\n }\n\n private wakeRetries(): void {\n for (const wake of [...this.retryWakers]) wake()\n }\n\n /** Also cut short by a reconnect, via wakeRetries(). */\n private sleepUntilClose(ms: number): Promise<void> {\n if (this.closing) return Promise.resolve()\n return new Promise((resolve) => {\n const wake = () => {\n clearTimeout(timer)\n this.retryWakers.delete(wake)\n resolve()\n }\n const timer = setTimeout(wake, ms)\n this.retryWakers.add(wake)\n })\n }\n}\n","import type { ILogger } from './types.js'\n\n/**\n * Default logger used when the consumer does not inject one.\n *\n * Forwards `warn` and `error` to the console so connection drops, channel\n * errors, and failed handlers stay visible out of the box, while staying\n * silent for `info`/`debug` so routine operation never pollutes the\n * consumer's output. Pass `silentLogger` for zero output, or any `ILogger`\n * (console, Winston, pino, Bunyan) to take full control.\n */\nexport const defaultLogger: ILogger = {\n debug: () => {},\n info: () => {},\n warn: (message, ...args) => console.warn('[rabbitmq-client]', message, ...args),\n error: (message, ...args) => console.error('[rabbitmq-client]', message, ...args),\n}\n\ntype Level = keyof ILogger\ntype ObjectFirst = (context: object, message: string) => void\n\n/**\n * pino takes `(context, message)` and drops a context passed after the\n * message, and only serializes an Error under `err`. It is the one logger\n * with a `bindings()` method, so it is recognised by that and called its way.\n */\nexport const forLibraryCalls = (logger: ILogger): ILogger => {\n if (typeof (logger as { bindings?: unknown }).bindings !== 'function') return logger\n const call =\n (level: Level) =>\n (message: string, context?: unknown): void => {\n const log = (logger[level] as unknown as ObjectFirst).bind(logger)\n if (typeof context !== 'object' || context === null) return log({}, message)\n const { error, ...rest } = context as { error?: unknown }\n log(error === undefined ? rest : { ...rest, err: error }, message)\n }\n return { debug: call('debug'), info: call('info'), warn: call('warn'), error: call('error') }\n}\n\n/**\n * No-op logger that discards every message. Use this to silence the client\n * entirely, including the `warn`/`error` output the default logger emits.\n */\nexport const silentLogger: ILogger = {\n debug: () => {},\n info: () => {},\n warn: () => {},\n error: () => {},\n}\n","/**\n * Minimal FIFO counting semaphore used to cap how many message handlers run\n * concurrently, independently of channel prefetch.\n *\n * Prefetch bounds how many messages the broker delivers into consumer memory;\n * the semaphore bounds how many of those are actively being processed at once.\n * Keeping the two separate lets a consumer buffer a large prefetch window while\n * still protecting a slow downstream (a database, an HTTP API) from a burst.\n */\nexport class Semaphore {\n private permits: number\n private readonly waiters: Array<() => void> = []\n\n constructor(permits: number) {\n if (!Number.isInteger(permits) || permits < 1) {\n throw new Error(`Semaphore requires a positive integer of permits, got ${permits}`)\n }\n this.permits = permits\n }\n\n /**\n * Acquires a permit, resolving immediately when one is free or queueing\n * (FIFO) until another holder releases. Always pair with `release()` in a\n * `finally` so a throwing handler never leaks a permit.\n */\n acquire(): Promise<void> {\n if (this.permits > 0) {\n this.permits--\n return Promise.resolve()\n }\n return new Promise<void>((resolve) => {\n this.waiters.push(resolve)\n })\n }\n\n /**\n * Releases a permit. Hands it directly to the next waiter if any are queued,\n * otherwise returns it to the pool.\n */\n release(): void {\n const next = this.waiters.shift()\n if (next) {\n next()\n } else {\n this.permits++\n }\n }\n\n /** Permits currently available. Exposed for tests and introspection. */\n get available(): number {\n return this.permits\n }\n\n /** Number of callers currently queued waiting for a permit. */\n get waiting(): number {\n return this.waiters.length\n }\n}\n"],"mappings":";AAAA,OAAO,UAAU;;;ACWV,IAAM,gBAAyB;AAAA,EACpC,OAAO,MAAM;AAAA,EAAC;AAAA,EACd,MAAM,MAAM;AAAA,EAAC;AAAA,EACb,MAAM,CAAC,YAAY,SAAS,QAAQ,KAAK,qBAAqB,SAAS,GAAG,IAAI;AAAA,EAC9E,OAAO,CAAC,YAAY,SAAS,QAAQ,MAAM,qBAAqB,SAAS,GAAG,IAAI;AAClF;AAUO,IAAM,kBAAkB,CAAC,WAA6B;AAC3D,MAAI,OAAQ,OAAkC,aAAa,WAAY,QAAO;AAC9E,QAAM,OACJ,CAAC,UACD,CAAC,SAAiB,YAA4B;AAC5C,UAAM,MAAO,OAAO,KAAK,EAA6B,KAAK,MAAM;AACjE,QAAI,OAAO,YAAY,YAAY,YAAY,KAAM,QAAO,IAAI,CAAC,GAAG,OAAO;AAC3E,UAAM,EAAE,OAAO,GAAG,KAAK,IAAI;AAC3B,QAAI,UAAU,SAAY,OAAO,EAAE,GAAG,MAAM,KAAK,MAAM,GAAG,OAAO;AAAA,EACnE;AACF,SAAO,EAAE,OAAO,KAAK,OAAO,GAAG,MAAM,KAAK,MAAM,GAAG,MAAM,KAAK,MAAM,GAAG,OAAO,KAAK,OAAO,EAAE;AAC9F;AAMO,IAAM,eAAwB;AAAA,EACnC,OAAO,MAAM;AAAA,EAAC;AAAA,EACd,MAAM,MAAM;AAAA,EAAC;AAAA,EACb,MAAM,MAAM;AAAA,EAAC;AA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EAEQ,cAAc,UAAkB,SAA+C;AACrF,UAAM,EAAE,WAAW,IAAI,KAAK;AAC5B,QAAI,SAAS,kBAAkB,OAAW,QAAO;AACjD,WAAO,QAAQ,YAAY,UAAU,QAAQ,aAAa;AAAA,EAC5D;AAAA,EAEQ,cAAoB;AAC1B,eAAW,QAAQ,CAAC,GAAG,KAAK,WAAW,EAAG,MAAK;AAAA,EACjD;AAAA;AAAA,EAGQ,gBAAgB,IAA2B;AACjD,QAAI,KAAK,QAAS,QAAO,QAAQ,QAAQ;AACzC,WAAO,IAAI,QAAQ,CAAC,YAAY;AAC9B,YAAM,OAAO,MAAM;AACjB,qBAAa,KAAK;AAClB,aAAK,YAAY,OAAO,IAAI;AAC5B,gBAAQ;AAAA,MACV;AACA,YAAM,QAAQ,WAAW,MAAM,EAAE;AACjC,WAAK,YAAY,IAAI,IAAI;AAAA,IAC3B,CAAC;AAAA,EACH;AACF;","names":["duration"]}
1
+ {"version":3,"sources":["../src/client.ts","../src/logger.ts","../src/semaphore.ts"],"sourcesContent":["import amqp from 'amqplib'\nimport type {\n ILogger,\n PublishOptions,\n RabbitMQClientOptions,\n RabbitMQHooks,\n RabbitMQMessage,\n RabbitMQMessageHandler,\n RabbitMQMessageProperties,\n RabbitMQSubscription,\n SubscribeOptions,\n} from './types.js'\nimport { defaultLogger, forLibraryCalls } from './logger.js'\nimport { Semaphore } from './semaphore.js'\n\nconst DEFAULT_PREFETCH = 10\nconst MAX_PUBLISH_RETRY_DELAY_MS = 60_000\n\nconst DEFAULTS = {\n maxRetries: 3,\n retryDelay: 1000,\n connectionRetryDelay: 5000,\n initMaxAttempts: 5,\n publishMaxAttempts: 5,\n prefetch: DEFAULT_PREFETCH,\n closeTimeout: 5000,\n} as const\n\nfunction backoff(baseDelay: number, attempt: number, maxDelay: number): number {\n return Math.min(baseDelay * Math.pow(2, attempt - 1), maxDelay)\n}\n\n/** A `mandatory` publish the broker could route to no queue. */\nexport class UnroutableMessageError extends Error {\n constructor(\n readonly exchange: string,\n readonly routingKey: string,\n ) {\n super(`No queue is bound to receive ${exchange || '(default exchange)'}/${routingKey}`)\n this.name = 'UnroutableMessageError'\n }\n}\n\n/** Thrown by a handler to send its message to the DLQ at once, without retrying. */\nexport class DeadLetterError extends Error {\n constructor(message: string, options?: { cause?: unknown }) {\n super(message, options)\n this.name = 'DeadLetterError'\n }\n}\n\n/**\n * Production-grade RabbitMQ client with confirm channels, automatic DLQ\n * infrastructure, reconnection with subscription restoration, and\n * exponential-backoff publishing.\n *\n * @example\n * ```ts\n * const client = new RabbitMQClient({ url: 'amqp://localhost' })\n * await client.init()\n * await client.publish('events', 'user.created', { userId: '123' })\n * await client.subscribe('events', 'user.created', 'user-service', handler)\n * ```\n */\nexport class RabbitMQClient {\n private connection: amqp.ChannelModel | null = null\n private channel: amqp.ConfirmChannel | null = null\n private connected = false\n private closing = false\n private subscriptions: RabbitMQSubscription[] = []\n private readonly options: Required<Omit<RabbitMQClientOptions, 'logger' | 'hooks'>>\n private readonly logger: ILogger\n private readonly hooks: RabbitMQHooks\n private initializationPromise: Promise<void> | null = null\n private reconnectionPromise: Promise<void> | null = null\n private assertedExchanges = new Set<string>()\n // One per channel, so a late return on a replaced channel reaches no waiter.\n private returnWaiters = new Set<(msg: amqp.Message) => void>()\n private consumerTags = new Map<string, string>()\n // Per-queue concurrency limiter, keyed by queue name. Persisted across\n // reconnects (unlike consumerTags) so handlers still running when a\n // connection drops keep holding their permits, and the new consumer cannot\n // exceed the ceiling while the old pipeline drains. `null` = no limit.\n private subscriptionSemaphores = new Map<string, Semaphore | null>()\n private retryWakers = new Set<() => void>()\n private inflightCount = 0\n private drainResolve: (() => void) | null = null\n\n constructor(options: RabbitMQClientOptions) {\n this.options = {\n url: options.url,\n maxRetries: options.maxRetries ?? DEFAULTS.maxRetries,\n retryDelay: options.retryDelay ?? DEFAULTS.retryDelay,\n connectionRetryDelay: options.connectionRetryDelay ?? DEFAULTS.connectionRetryDelay,\n initMaxAttempts: options.initMaxAttempts ?? DEFAULTS.initMaxAttempts,\n publishMaxAttempts: options.publishMaxAttempts ?? DEFAULTS.publishMaxAttempts,\n prefetch: options.prefetch ?? DEFAULTS.prefetch,\n // Default concurrency tracks prefetch, preserving the previous behaviour\n // (handlers ran fire-and-forget, so up to `prefetch` ran at once) while\n // making the ceiling explicit and independently tunable. A value <= 0\n // (including an unlimited `prefetch: 0`) means \"no concurrency limit\".\n concurrency: options.concurrency ?? options.prefetch ?? DEFAULTS.prefetch,\n closeTimeout: options.closeTimeout ?? DEFAULTS.closeTimeout,\n }\n this.logger = forLibraryCalls(options.logger ?? defaultLogger)\n this.hooks = options.hooks ?? {}\n }\n\n /**\n * Opens a connection and creates a confirm channel. Retries up to\n * `initMaxAttempts` times, then throws. Idempotent and safe to call\n * concurrently — duplicate calls share the same in-flight promise.\n */\n async init(): Promise<void> {\n if (this.connection && this.connected) {\n return\n }\n if (this.initializationPromise) {\n return this.initializationPromise\n }\n // Attach a no-op catch to prevent Node from briefly treating this as an\n // unhandled rejection before the await below registers its own handler.\n this.initializationPromise = this.doConnect(this.options.initMaxAttempts)\n this.initializationPromise.catch(() => undefined)\n try {\n await this.initializationPromise\n } finally {\n this.initializationPromise = null\n }\n }\n\n private async doConnect(maxAttempts?: number): Promise<void> {\n this.assertedExchanges.clear()\n this.consumerTags.clear()\n // A channel failure leaves its connection open; drop it so it does not\n // outlive the client and keep the process alive.\n this.connection?.close().catch(() => undefined)\n this.connection = null\n let attempts = 0\n while (!this.connected && !this.closing) {\n try {\n const connection = await amqp.connect(this.options.url)\n this.connection = connection\n this.logger.info('Connected to server')\n const channel = await connection.createConfirmChannel()\n this.channel = channel\n // Retries sleeping on the replaced channel give up; the broker redelivers their messages.\n this.wakeRetries()\n const returnWaiters = new Set<(msg: amqp.Message) => void>()\n this.returnWaiters = returnWaiters\n channel.on('return', (msg: amqp.Message) => {\n for (const waiter of returnWaiters) waiter(msg)\n })\n this.logger.info('Confirm channel created')\n await channel.prefetch(this.options.prefetch)\n this.logger.info('Channel prefetch set', { prefetch: this.options.prefetch })\n\n // Events from a connection or channel a reconnect has replaced must not\n // tear down the current one.\n connection.on('error', (error: Error) => {\n if (connection !== this.connection) return\n this.connected = false\n this.logger.error('Connection error', { error })\n })\n connection.on('close', () => {\n if (connection !== this.connection) return\n this.connected = false\n this.logger.warn('Connection closed')\n this.reconnectWithRetry()\n })\n channel.on('error', (error: Error) => {\n if (channel !== this.channel) return\n this.logger.error('Channel error', { error })\n this.handleChannelFailure()\n })\n channel.on('close', () => {\n if (channel !== this.channel) return\n this.logger.warn('Channel closed')\n this.handleChannelFailure()\n })\n\n this.connected = true\n this.logger.info('Client initialized successfully')\n } catch (error) {\n attempts++\n this.connected = false\n if (maxAttempts !== undefined && attempts >= maxAttempts) {\n this.logger.error('Connection failed after maximum attempts', { error, attempts, maxAttempts })\n throw new Error(\n `Failed to connect to RabbitMQ after ${attempts} attempts. ` +\n 'Check RABBITMQ_URL configuration and RabbitMQ server availability.',\n )\n }\n this.logger.warn('Connection attempt failed, retrying...', {\n error,\n attempt: attempts,\n maxAttempts: maxAttempts ?? 'unlimited',\n retryDelayMs: this.options.connectionRetryDelay,\n })\n await this.sleep(this.options.connectionRetryDelay)\n }\n }\n if (!this.connected) {\n throw new Error('RabbitMQ client closed while connecting')\n }\n }\n\n private handleChannelFailure(): void {\n if (this.connected) {\n this.connected = false\n this.reconnectWithRetry()\n }\n }\n\n /**\n * Returns whether the client currently has an active connection and channel.\n */\n isConnected(): boolean {\n return this.connected\n }\n\n /**\n * Publishes a JSON message to a topic exchange with publisher confirms, or\n * straight to a queue through the default exchange (`''`, routing key = queue name).\n * Retries with exponential backoff (capped at 60 s), forcing a reconnect\n * on each failure. Throws after `publishMaxAttempts` exhausted.\n */\n async publish(\n exchange: string,\n routingKey: string,\n message: RabbitMQMessage,\n options?: PublishOptions,\n ): Promise<void> {\n let attempts = 0\n const maxAttempts = options?.maxAttempts ?? this.options.publishMaxAttempts\n const baseDelay = this.options.connectionRetryDelay\n const content = Buffer.from(JSON.stringify(message))\n // Taken once, not per attempt: a retried publish must not look newer than\n // a message published after it succeeded.\n const timestamp = Math.floor(Date.now() / 1000)\n\n while (attempts < maxAttempts) {\n try {\n if (!this.connected) {\n await this.reconnectWithRetry()\n }\n\n if (!this.channel) {\n throw new Error('Channel not available')\n }\n\n // The default exchange ('') always exists and the broker refuses to\n // declare it, so publishing straight to a queue skips the assertion.\n if (exchange !== '' && !this.assertedExchanges.has(exchange)) {\n await this.channel.assertExchange(exchange, 'topic', { durable: true })\n this.assertedExchanges.add(exchange)\n }\n\n const channel = this.channel\n const returnWaiters = this.returnWaiters\n // The broker sends basic.return before the confirm of the same message,\n // so the flag is settled by the time waitForConfirms resolves. A return\n // carries no delivery tag, so it is matched on where it was sent plus the\n // message id, or the content when there is none.\n let returned = false\n const messageId = options?.messageId\n const onReturn = (msg: amqp.Message) => {\n if (msg.fields.exchange !== exchange || msg.fields.routingKey !== routingKey) return\n if (messageId !== undefined ? msg.properties.messageId === messageId : msg.content.equals(content)) {\n returned = true\n }\n }\n if (options?.mandatory) returnWaiters.add(onReturn)\n\n try {\n channel.publish(exchange, routingKey, content, {\n persistent: true,\n timestamp,\n headers: options?.headers,\n correlationId: options?.correlationId,\n messageId: options?.messageId,\n expiration: options?.expiration,\n mandatory: options?.mandatory,\n })\n\n await channel.waitForConfirms()\n } finally {\n returnWaiters.delete(onReturn)\n }\n\n if (returned) {\n throw new UnroutableMessageError(exchange, routingKey)\n }\n\n if (attempts > 0) {\n this.logger.info('Published message after retries', { exchange, routingKey, messageSize: content.length, attempts })\n } else {\n this.logger.info('Published message', { exchange, routingKey, messageSize: content.length })\n }\n this.logger.debug('Published message payload', { exchange, routingKey, payload: message })\n\n this.callHook(this.hooks.onPublish, { exchange, routingKey, attempts: attempts + 1 })\n\n return\n } catch (error) {\n // The broker answered, and the same routing would fail the same way.\n if (error instanceof UnroutableMessageError) {\n this.logger.warn('Published message was unroutable', { exchange, routingKey })\n throw error\n }\n\n attempts++\n this.connected = false\n\n if (attempts >= maxAttempts) {\n this.logger.error('Publish failed after max attempts', { error, exchange, routingKey, attempts, maxAttempts })\n throw new Error(\n `Failed to publish to ${exchange || '(default exchange)'}/${routingKey} after ${attempts} attempts: ${error instanceof Error ? error.message : String(error)}`,\n )\n }\n\n const retryDelay = backoff(baseDelay, attempts, MAX_PUBLISH_RETRY_DELAY_MS)\n\n this.logger.warn('Publish attempt failed, retrying', { error, exchange, routingKey, attempt: attempts, maxAttempts, retryDelayMs: retryDelay })\n\n await this.sleep(retryDelay)\n }\n }\n }\n\n /**\n * Gracefully shuts down the client. Waits up to `closeTimeout` ms for\n * in-flight message handlers to finish before closing the channel and\n * connection. Pass `false` to preserve subscriptions for a later\n * `init()` / reconnect cycle.\n */\n async close(clearSubscriptions = true): Promise<void> {\n // Closing the channel fires its 'close' handler, which would otherwise\n // reconnect and leave a connection behind that keeps the process alive.\n this.closing = true\n try {\n // A connect already running gives up after its current attempt; whatever\n // it opened is closed below.\n await Promise.allSettled([this.initializationPromise, this.reconnectionPromise])\n this.wakeRetries()\n if (this.inflightCount > 0) {\n this.logger.info('Waiting for in-flight messages to drain', { inflightCount: this.inflightCount })\n await this.waitForDrain(this.options.closeTimeout)\n }\n try {\n await this.channel?.close()\n } finally {\n await this.connection?.close()\n }\n this.connection = null\n this.channel = null\n this.connected = false\n this.initializationPromise = null\n this.reconnectionPromise = null\n this.assertedExchanges.clear()\n this.consumerTags.clear()\n if (clearSubscriptions) {\n this.subscriptions = []\n this.subscriptionSemaphores.clear()\n }\n this.logger.info('Connection closed')\n } catch (error) {\n this.logger.error('Error closing connection', { error })\n throw error\n } finally {\n this.closing = false\n }\n }\n\n private async reconnectWithRetry(): Promise<void> {\n if (this.closing) return\n if (this.reconnectionPromise) {\n return this.reconnectionPromise\n }\n this.connected = false\n this.logger.info('Starting reconnection...')\n this.reconnectionPromise = this.doReconnect()\n this.reconnectionPromise.catch(() => undefined)\n try {\n await this.reconnectionPromise\n } catch (error) {\n // The event handlers call this without awaiting it, so a reconnect that\n // close() interrupted must not surface as an unhandled rejection.\n if (!this.closing) throw error\n } finally {\n this.reconnectionPromise = null\n }\n }\n\n private async doReconnect(): Promise<void> {\n await this.doConnect()\n await this.resubscribeAll()\n }\n\n private async resubscribeAll(): Promise<void> {\n if (this.subscriptions.length === 0) {\n return\n }\n this.logger.info('Re-establishing subscriptions', { count: this.subscriptions.length })\n const subs = [...this.subscriptions]\n const results = await Promise.allSettled(\n subs.map(async (sub) => {\n await this.setupSubscription(sub)\n return sub.queue\n }),\n )\n const succeeded: string[] = []\n const failed: string[] = []\n results.forEach((result, index) => {\n if (result.status === 'fulfilled') {\n succeeded.push(result.value)\n } else {\n const queue = subs[index].queue\n failed.push(queue)\n this.logger.error('Failed to re-subscribe to queue', { error: result.reason, queue })\n }\n })\n if (succeeded.length > 0) {\n this.logger.info('Successfully re-subscribed to queues', { count: succeeded.length, queues: succeeded })\n }\n if (failed.length > 0) {\n this.logger.warn('Some subscriptions failed to restore', { count: failed.length, queues: failed })\n }\n this.callHook(this.hooks.onReconnect, { subscriptionsRestored: succeeded.length, subscriptionsFailed: failed.length })\n }\n\n /**\n * Subscribes to a queue with automatic DLQ infrastructure setup.\n *\n * Pass `options.queueArguments` to override the default quorum-queue\n * arguments (merged with the DLQ wiring defaults).\n */\n async subscribe(\n exchange: string,\n routingKey: string,\n queue: string,\n handler: RabbitMQMessageHandler,\n options?: SubscribeOptions,\n ): Promise<void> {\n if (!this.connected || !this.channel) {\n throw new Error('RabbitMQ client not connected. Call init() first.')\n }\n const sub: RabbitMQSubscription = { exchange, routingKey, queue, handler, options }\n const existingIndex = this.subscriptions.findIndex((s) => s.queue === queue)\n const previous = existingIndex === -1 ? undefined : this.subscriptions[existingIndex]\n if (existingIndex === -1) {\n this.subscriptions.push(sub)\n } else {\n this.subscriptions[existingIndex] = sub\n // Re-subscribing may change concurrency; drop the old limiter so\n // setupSubscription rebuilds it from the new options. (A reconnect goes\n // through setupSubscription directly and keeps the existing limiter.)\n this.subscriptionSemaphores.delete(queue)\n }\n try {\n await this.setupSubscription(sub)\n } catch (error) {\n // A subscription that cannot be set up must not be retried on every reconnect.\n this.subscriptions = previous\n ? this.subscriptions.map((s) => (s === sub ? previous : s))\n : this.subscriptions.filter((s) => s !== sub)\n throw error\n }\n }\n\n /**\n * Cancels a queue subscription and removes it from the restoration list.\n */\n async unsubscribe(queue: string): Promise<void> {\n const tag = this.consumerTags.get(queue)\n if (tag && this.channel) {\n await this.channel.cancel(tag)\n }\n this.consumerTags.delete(queue)\n this.subscriptionSemaphores.delete(queue)\n this.subscriptions = this.subscriptions.filter((s) => s.queue !== queue)\n this.logger.info('Unsubscribed from queue', { queue })\n }\n\n private async setupSubscription(sub: RabbitMQSubscription): Promise<void> {\n if (!this.channel) {\n throw new Error('Channel not available')\n }\n // Bind this consumer to the exact channel that will deliver its messages,\n // so ack/nack always target that channel even after a reconnect swaps\n // `this.channel`.\n const channel = this.channel\n const { exchange, routingKey, queue, handler, options } = sub\n const dlxExchange = options?.deadLetterExchange ?? `${exchange}.dlx`\n const dlqQueue = `${queue}.dlq`\n // Set once on the queue, so every binding's dead letters reach the DLQ.\n const dlqRoutingKey = `${routingKey}.dead`\n const bindings = [{ exchange, routingKey }, ...(options?.bindings ?? [])]\n\n await channel.assertExchange(dlxExchange, 'topic', { durable: true })\n await channel.assertQueue(dlqQueue, { durable: true })\n await channel.bindQueue(dlqQueue, dlxExchange, dlqRoutingKey)\n\n const passive = options?.passiveExchanges\n const isPassive = (name: string) => passive === true || (Array.isArray(passive) && passive.includes(name))\n const missing = [...new Set(bindings.map((b) => b.exchange))].filter((name) => !this.assertedExchanges.has(name))\n const toCheck = missing.filter(isPassive)\n if (toCheck.length > 0) {\n await this.checkExchanges(toCheck)\n }\n for (const name of missing.filter((name) => !isPassive(name))) {\n await channel.assertExchange(name, 'topic', { durable: true })\n }\n for (const name of missing) this.assertedExchanges.add(name)\n\n const queueType = options?.queueArguments?.['x-queue-type'] ?? 'quorum'\n const queueArgs: Record<string, unknown> = {\n 'x-queue-type': queueType,\n 'x-overflow': 'reject-publish',\n }\n // at-least-once DLQ strategy is only supported by quorum queues\n if (queueType === 'quorum') {\n queueArgs['x-dead-letter-strategy'] = 'at-least-once'\n }\n\n await channel.assertQueue(queue, {\n durable: true,\n deadLetterExchange: dlxExchange,\n deadLetterRoutingKey: dlqRoutingKey,\n arguments: { ...queueArgs, ...options?.queueArguments },\n })\n\n for (const binding of bindings) {\n await channel.bindQueue(queue, binding.exchange, binding.routingKey)\n }\n // Reuse the queue's existing limiter across reconnects; only build a new\n // one the first time (or after subscribe()/unsubscribe() cleared it).\n // A concurrency <= 0 means \"no limit\" (null), matching an unlimited prefetch.\n if (!this.subscriptionSemaphores.has(queue)) {\n const concurrency = options?.concurrency ?? this.options.concurrency\n this.subscriptionSemaphores.set(queue, concurrency > 0 ? new Semaphore(concurrency) : null)\n }\n const semaphore = this.subscriptionSemaphores.get(queue) ?? null\n const { consumerTag } = await channel.consume(\n queue,\n (message) => {\n if (message) {\n this.dispatch(message, handler, semaphore, channel, options)\n }\n },\n { noAck: false },\n )\n this.consumerTags.set(queue, consumerTag)\n this.logger.info('Subscribed to queue', { queue, bindings })\n }\n\n // A failed check closes the channel it ran on, so it runs on a throwaway one\n // rather than take down the shared channel and every consumer on it.\n private async checkExchanges(exchanges: string[]): Promise<void> {\n if (!this.connection) {\n throw new Error('Connection not available')\n }\n const probe = await this.connection.createChannel()\n probe.on('error', () => undefined)\n try {\n for (const name of exchanges) {\n await probe.checkExchange(name)\n }\n } finally {\n await probe.close().catch(() => undefined)\n }\n }\n\n /**\n * Gates a delivered message on the subscription's concurrency semaphore, then\n * processes it. A message counts as in-flight from delivery until its handler\n * settles (including time spent waiting for a permit), so graceful `close()`\n * drains queued messages as well as actively-processing ones.\n */\n private dispatch(\n message: amqp.ConsumeMessage,\n handler: RabbitMQMessageHandler,\n semaphore: Semaphore | null,\n channel: amqp.ConfirmChannel,\n options: SubscribeOptions | undefined,\n ): void {\n this.inflightCount++\n const acquire = semaphore ? semaphore.acquire() : Promise.resolve()\n acquire\n .then(() => this.handleWithRetry(message, handler, channel, options))\n .catch((error) => {\n this.logger.error('Unhandled error in message handler', { error })\n })\n .finally(() => {\n semaphore?.release()\n this.inflightCount--\n if (this.inflightCount === 0 && this.drainResolve) {\n this.drainResolve()\n }\n })\n }\n\n private async handleWithRetry(\n message: amqp.ConsumeMessage,\n handler: RabbitMQMessageHandler,\n channel: amqp.ConfirmChannel,\n options: SubscribeOptions | undefined,\n ): Promise<void> {\n // `channel` is the one that delivered this message. If a reconnect has\n // since replaced it, this message was never acked and the broker will\n // redeliver it on the new channel, so drop this stale attempt rather than\n // run the handler again or ack a tag the new channel does not know.\n if (channel !== this.channel) {\n this.logger.warn('Skipping message from a superseded channel; it will be redelivered')\n return\n }\n const startTime = Date.now()\n let attempts = 0\n const routingKey = message.fields.routingKey\n const exchange = message.fields.exchange\n\n let content: RabbitMQMessage\n try {\n content = JSON.parse(message.content.toString())\n } catch (parseError) {\n const rawContent = message.content.toString()\n const rawPreview = rawContent.substring(0, 100)\n this.logger.error('Failed to parse message JSON, sending to DLQ', {\n error: parseError,\n exchange,\n routingKey,\n rawContentPreview: rawPreview + (rawContent.length > 100 ? '...' : ''),\n })\n if (this.settle(channel, message, 'nack')) {\n this.callHook(this.hooks.onMessageDlq, { exchange, routingKey, duration: 0, reason: 'invalid_json' })\n }\n return\n }\n\n this.logger.debug('Message received, processing', { exchange, routingKey, payload: content })\n\n const properties: RabbitMQMessageProperties = {\n exchange,\n routingKey,\n headers: message.properties.headers ?? {},\n timestamp: message.properties.timestamp,\n messageId: message.properties.messageId,\n correlationId: message.properties.correlationId,\n }\n\n const maxRetries = options?.maxRetries ?? this.options.maxRetries\n while (attempts < maxRetries) {\n // Only the handler call belongs in this try. Acking inside it would make\n // a dead channel look like a failed handler and re-run its side effects.\n try {\n await handler(content, properties)\n } catch (error) {\n attempts++\n // By name, not instanceof: the ESM and CJS builds each define their own class.\n if (error instanceof Error && error.name === 'DeadLetterError') {\n const duration = Date.now() - startTime\n this.logger.warn('Handler dead-lettered the message', { error: error.message, exchange, routingKey })\n if (this.settle(channel, message, 'nack')) {\n this.callHook(this.hooks.onMessageDlq, { exchange, routingKey, duration, reason: 'dead_letter_error' })\n }\n return\n }\n this.logger.error('Handler failed', {\n error: error instanceof Error ? error.message : error,\n stack: error instanceof Error ? error.stack : undefined,\n exchange,\n routingKey,\n attempt: attempts,\n maxRetries,\n })\n if (attempts < maxRetries) {\n await this.sleepUntilClose(this.retryDelayFor(attempts, options))\n if (this.closing || channel !== this.channel) {\n this.logger.warn('Stopped retrying a message on close or reconnect; it will be redelivered', { exchange, routingKey })\n return\n }\n }\n continue\n }\n const duration = Date.now() - startTime\n this.logger.info('Message processed successfully', { exchange, routingKey, duration, attempts: attempts + 1 })\n if (this.settle(channel, message, 'ack')) {\n this.callHook(this.hooks.onMessageProcessed, { exchange, routingKey, duration, attempts: attempts + 1 })\n }\n return\n }\n\n const duration = Date.now() - startTime\n this.logger.error('Message failed after max retries, sending to DLQ', { exchange, routingKey, maxRetries, duration })\n if (this.settle(channel, message, 'nack')) {\n this.callHook(this.hooks.onMessageDlq, { exchange, routingKey, duration, reason: 'max_retries_exhausted' })\n }\n }\n\n /**\n * Acks or nacks a delivery, reporting whether the broker accepted it.\n *\n * A settle failure is a transport error, never a handler failure: by this\n * point the handler has already run, so it must not be retried just because\n * the channel went away. The delivery stays unacked and the broker redelivers\n * it once the channel closes.\n */\n private settle(\n channel: amqp.ConfirmChannel,\n message: amqp.ConsumeMessage,\n action: 'ack' | 'nack',\n ): boolean {\n // A handler can run for minutes, so the channel that delivered this\n // message may have been replaced by a reconnect since the entry check.\n // Settling on it would throw, and replaying the delivery tag on the\n // current channel would settle an unrelated message, since tags are scoped\n // to the channel that issued them. The broker requeues a closed channel's\n // unacked deliveries, so let it be redelivered instead.\n if (channel !== this.channel) {\n this.logger.warn('Skipping message from a superseded channel; it will be redelivered')\n return false\n }\n try {\n if (action === 'ack') {\n channel.ack(message)\n } else {\n channel.nack(message, false, false)\n }\n return true\n } catch (error) {\n this.logger.error('Failed to settle message; leaving it unacked for redelivery', {\n error,\n action,\n exchange: message.fields.exchange,\n routingKey: message.fields.routingKey,\n })\n return false\n }\n }\n\n /**\n * Lightweight liveness probe: creates and immediately deletes a temporary\n * exclusive queue. Returns `false` when disconnected or on broker error.\n */\n async checkHealth(): Promise<boolean> {\n if (!this.connected || !this.channel) {\n return false\n }\n try {\n const { queue } = await this.channel.assertQueue('', {\n exclusive: true,\n autoDelete: true,\n })\n await this.channel.deleteQueue(queue)\n return true\n } catch (error) {\n this.logger.warn('Health check failed', { error })\n return false\n }\n }\n\n private waitForDrain(timeout: number): Promise<void> {\n if (this.inflightCount === 0) return Promise.resolve()\n return new Promise<void>((resolve) => {\n const timer = setTimeout(() => {\n this.logger.warn('Close timeout reached with messages still in flight', { inflightCount: this.inflightCount })\n this.drainResolve = null\n resolve()\n }, timeout)\n this.drainResolve = () => {\n clearTimeout(timer)\n this.drainResolve = null\n resolve()\n }\n })\n }\n\n /** Invokes a hook callback, swallowing errors so hooks never break message flow. */\n private callHook<T>(hook: ((info: T) => void) | undefined, info: T): void {\n try { hook?.(info) } catch (error) { this.logger.debug('Observability hook error', { error }) }\n }\n\n private sleep(ms: number): Promise<void> {\n return new Promise((resolve) => setTimeout(resolve, ms))\n }\n\n private retryDelayFor(attempts: number, options: SubscribeOptions | undefined): number {\n const { retryDelay } = this.options\n if (options?.maxRetryDelay === undefined) return retryDelay\n return backoff(retryDelay, attempts, options.maxRetryDelay)\n }\n\n private wakeRetries(): void {\n for (const wake of [...this.retryWakers]) wake()\n }\n\n /** Also cut short by a reconnect, via wakeRetries(). */\n private sleepUntilClose(ms: number): Promise<void> {\n if (this.closing) return Promise.resolve()\n return new Promise((resolve) => {\n const wake = () => {\n clearTimeout(timer)\n this.retryWakers.delete(wake)\n resolve()\n }\n const timer = setTimeout(wake, ms)\n this.retryWakers.add(wake)\n })\n }\n}\n","import type { ILogger } from './types.js'\n\n/**\n * Default logger used when the consumer does not inject one.\n *\n * Forwards `warn` and `error` to the console so connection drops, channel\n * errors, and failed handlers stay visible out of the box, while staying\n * silent for `info`/`debug` so routine operation never pollutes the\n * consumer's output. Pass `silentLogger` for zero output, or any `ILogger`\n * (console, Winston, pino, Bunyan) to take full control.\n */\nexport const defaultLogger: ILogger = {\n debug: () => {},\n info: () => {},\n warn: (message, ...args) => console.warn('[rabbitmq-client]', message, ...args),\n error: (message, ...args) => console.error('[rabbitmq-client]', message, ...args),\n}\n\ntype Level = keyof ILogger\ntype ObjectFirst = (context: object, message: string) => void\n\n/**\n * pino takes `(context, message)` and drops a context passed after the\n * message, and only serializes an Error under `err`. It is the one logger\n * with a `bindings()` method, so it is recognised by that and called its way.\n */\nexport const forLibraryCalls = (logger: ILogger): ILogger => {\n if (typeof (logger as { bindings?: unknown }).bindings !== 'function') return logger\n const call =\n (level: Level) =>\n (message: string, context?: unknown): void => {\n const log = (logger[level] as unknown as ObjectFirst).bind(logger)\n if (typeof context !== 'object' || context === null) return log({}, message)\n const { error, ...rest } = context as { error?: unknown }\n log(error === undefined ? rest : { ...rest, err: error }, message)\n }\n return { debug: call('debug'), info: call('info'), warn: call('warn'), error: call('error') }\n}\n\n/**\n * No-op logger that discards every message. Use this to silence the client\n * entirely, including the `warn`/`error` output the default logger emits.\n */\nexport const silentLogger: ILogger = {\n debug: () => {},\n info: () => {},\n warn: () => {},\n error: () => {},\n}\n","/**\n * Minimal FIFO counting semaphore used to cap how many message handlers run\n * concurrently, independently of channel prefetch.\n *\n * Prefetch bounds how many messages the broker delivers into consumer memory;\n * the semaphore bounds how many of those are actively being processed at once.\n * Keeping the two separate lets a consumer buffer a large prefetch window while\n * still protecting a slow downstream (a database, an HTTP API) from a burst.\n */\nexport class Semaphore {\n private permits: number\n private readonly waiters: Array<() => void> = []\n\n constructor(permits: number) {\n if (!Number.isInteger(permits) || permits < 1) {\n throw new Error(`Semaphore requires a positive integer of permits, got ${permits}`)\n }\n this.permits = permits\n }\n\n /**\n * Acquires a permit, resolving immediately when one is free or queueing\n * (FIFO) until another holder releases. Always pair with `release()` in a\n * `finally` so a throwing handler never leaks a permit.\n */\n acquire(): Promise<void> {\n if (this.permits > 0) {\n this.permits--\n return Promise.resolve()\n }\n return new Promise<void>((resolve) => {\n this.waiters.push(resolve)\n })\n }\n\n /**\n * Releases a permit. Hands it directly to the next waiter if any are queued,\n * otherwise returns it to the pool.\n */\n release(): void {\n const next = this.waiters.shift()\n if (next) {\n next()\n } else {\n this.permits++\n }\n }\n\n /** Permits currently available. Exposed for tests and introspection. */\n get available(): number {\n return this.permits\n }\n\n /** Number of callers currently queued waiting for a permit. */\n get waiting(): number {\n return this.waiters.length\n }\n}\n"],"mappings":";AAAA,OAAO,UAAU;;;ACWV,IAAM,gBAAyB;AAAA,EACpC,OAAO,MAAM;AAAA,EAAC;AAAA,EACd,MAAM,MAAM;AAAA,EAAC;AAAA,EACb,MAAM,CAAC,YAAY,SAAS,QAAQ,KAAK,qBAAqB,SAAS,GAAG,IAAI;AAAA,EAC9E,OAAO,CAAC,YAAY,SAAS,QAAQ,MAAM,qBAAqB,SAAS,GAAG,IAAI;AAClF;AAUO,IAAM,kBAAkB,CAAC,WAA6B;AAC3D,MAAI,OAAQ,OAAkC,aAAa,WAAY,QAAO;AAC9E,QAAM,OACJ,CAAC,UACD,CAAC,SAAiB,YAA4B;AAC5C,UAAM,MAAO,OAAO,KAAK,EAA6B,KAAK,MAAM;AACjE,QAAI,OAAO,YAAY,YAAY,YAAY,KAAM,QAAO,IAAI,CAAC,GAAG,OAAO;AAC3E,UAAM,EAAE,OAAO,GAAG,KAAK,IAAI;AAC3B,QAAI,UAAU,SAAY,OAAO,EAAE,GAAG,MAAM,KAAK,MAAM,GAAG,OAAO;AAAA,EACnE;AACF,SAAO,EAAE,OAAO,KAAK,OAAO,GAAG,MAAM,KAAK,MAAM,GAAG,MAAM,KAAK,MAAM,GAAG,OAAO,KAAK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,KAAK;AAClB,aAAK,eAAe;AACpB,gBAAQ;AAAA,MACV;AAAA,IACF,CAAC;AAAA,EACH;AAAA;AAAA,EAGQ,SAAY,MAAuC,MAAe;AACxE,QAAI;AAAE,aAAO,IAAI;AAAA,IAAE,SAAS,OAAO;AAAE,WAAK,OAAO,MAAM,4BAA4B,EAAE,MAAM,CAAC;AAAA,IAAE;AAAA,EAChG;AAAA,EAEQ,MAAM,IAA2B;AACvC,WAAO,IAAI,QAAQ,CAAC,YAAY,WAAW,SAAS,EAAE,CAAC;AAAA,EACzD;AAAA,EAEQ,cAAc,UAAkB,SAA+C;AACrF,UAAM,EAAE,WAAW,IAAI,KAAK;AAC5B,QAAI,SAAS,kBAAkB,OAAW,QAAO;AACjD,WAAO,QAAQ,YAAY,UAAU,QAAQ,aAAa;AAAA,EAC5D;AAAA,EAEQ,cAAoB;AAC1B,eAAW,QAAQ,CAAC,GAAG,KAAK,WAAW,EAAG,MAAK;AAAA,EACjD;AAAA;AAAA,EAGQ,gBAAgB,IAA2B;AACjD,QAAI,KAAK,QAAS,QAAO,QAAQ,QAAQ;AACzC,WAAO,IAAI,QAAQ,CAAC,YAAY;AAC9B,YAAM,OAAO,MAAM;AACjB,qBAAa,KAAK;AAClB,aAAK,YAAY,OAAO,IAAI;AAC5B,gBAAQ;AAAA,MACV;AACA,YAAM,QAAQ,WAAW,MAAM,EAAE;AACjC,WAAK,YAAY,IAAI,IAAI;AAAA,IAC3B,CAAC;AAAA,EACH;AACF;","names":["name","duration"]}
@@ -1,5 +1,5 @@
1
1
  import { EventEmitter } from 'events';
2
- import { e as RabbitMQMessageProperties } from '../types-Cjg-65Ef.cjs';
2
+ import { e as RabbitMQMessageProperties } from '../types-A2viogDe.cjs';
3
3
 
4
4
  interface MockFn {
5
5
  (...args: unknown[]): unknown;
@@ -1,5 +1,5 @@
1
1
  import { EventEmitter } from 'events';
2
- import { e as RabbitMQMessageProperties } from '../types-Cjg-65Ef.js';
2
+ import { e as RabbitMQMessageProperties } from '../types-A2viogDe.js';
3
3
 
4
4
  interface MockFn {
5
5
  (...args: unknown[]): unknown;
@@ -66,9 +66,10 @@ interface SubscribeOptions {
66
66
  deadLetterExchange?: string;
67
67
  /**
68
68
  * Check that the source exchanges exist instead of declaring them, for
69
- * exchanges another service owns. Subscribing fails when one is missing.
69
+ * exchanges another service owns: `true` for all of them, or a list of
70
+ * names. Subscribing fails when one is missing.
70
71
  */
71
- passiveExchanges?: boolean;
72
+ passiveExchanges?: boolean | string[];
72
73
  /** Max handler attempts before the DLQ for this queue (overrides the client default); `Infinity` retries until the handler succeeds */
73
74
  maxRetries?: number;
74
75
  /**
@@ -66,9 +66,10 @@ interface SubscribeOptions {
66
66
  deadLetterExchange?: string;
67
67
  /**
68
68
  * Check that the source exchanges exist instead of declaring them, for
69
- * exchanges another service owns. Subscribing fails when one is missing.
69
+ * exchanges another service owns: `true` for all of them, or a list of
70
+ * names. Subscribing fails when one is missing.
70
71
  */
71
- passiveExchanges?: boolean;
72
+ passiveExchanges?: boolean | string[];
72
73
  /** Max handler attempts before the DLQ for this queue (overrides the client default); `Infinity` retries until the handler succeeds */
73
74
  maxRetries?: number;
74
75
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@linagora/rabbitmq-client",
3
- "version": "0.5.0",
3
+ "version": "0.6.0",
4
4
  "description": "Production-grade RabbitMQ client with confirm channels, DLQ infrastructure, auto-reconnection, exponential backoff, graceful shutdown, and observability hooks",
5
5
  "type": "module",
6
6
  "exports": {