UNPKG

@cloudamqp/amqp-client

Version:

AMQP 0-9-1 client, both for browsers (WebSocket) and node (TCP Socket)

126 lines 5.67 kB
import type { AMQPMessage } from "./amqp-message.js"; import type { ConsumeParams, MessageCount } from "./amqp-channel.js"; import type { AMQPProperties } from "./amqp-properties.js"; import { AMQPSubscription, AMQPGeneratorSubscription } from "./amqp-subscription.js"; import type { AMQPExchange } from "./amqp-exchange.js"; import type { ResolveBody } from "./amqp-publisher.js"; import type { ParserMap, CoderMap } from "./amqp-codec-registry.js"; /** * Options for {@link AMQPQueue#subscribe}. * Combines consumer parameters with channel-level prefetch. */ export type QueueSubscribeParams = ConsumeParams & { /** Per-consumer prefetch limit (sets QoS on the channel before consuming). */ prefetch?: number; /** * Whether to requeue messages that are nacked due to a callback error. * Defaults to `true`. */ requeueOnNack?: boolean; }; /** Options for {@link AMQPQueue#publish}. */ export type QueuePublishOptions<T> = Omit<AMQPProperties, "contentType"> & { /** Wait for broker confirmation. Defaults to `true`. */ confirm?: boolean; /** * Ask the broker to return the message if it can't be routed to a queue. * Returned messages are delivered to the session-level `onreturn` handler * when one is configured; otherwise the default channel handler just * logs them. Defaults to `false`. */ mandatory?: boolean; contentType?: T; }; /** * High-level queue handle returned by {@link AMQPSession#queue}. * * All operations are reconnect-safe: they acquire a session channel on each * call, so they work transparently after a reconnection. `subscribe` provides * automatic consumer recovery. `publish` waits for a broker confirm; use * Pass `{ confirm: false }` to skip the wait. */ export declare class AMQPQueue<P extends ParserMap = {}, C extends CoderMap = {}, KP extends keyof P & string = never, KC extends keyof C & string = never> { /** Queue name. */ readonly name: string; private readonly session; private readonly subscriptions; /** * Publish a message directly to this queue (via the default exchange). * * When the session has parsers configured, `body` can be any value accepted * by the matching parser's `serialize` method. Without parsers, `body` must * be a string, Buffer, Uint8Array, or null. * * Defaults: `confirm: true`, `deliveryMode: 2` (persistent). Pass * `deliveryMode: 1` to send a transient message. * * @param options - publish properties; set `confirm: false` to skip broker confirmation * @returns `this` for chaining */ publish<O extends keyof P & string = KP>(body: ResolveBody<P, O>, options?: QueuePublishOptions<O>): Promise<AMQPQueue<P, C, KP, KC>>; /** Subscribe with a callback. Messages are acked after the callback returns, nacked on error. */ subscribe(callback: (msg: AMQPMessage<P>) => void | Promise<void>): Promise<AMQPSubscription>; /** Subscribe with a callback and custom params. */ subscribe(params: QueueSubscribeParams, callback: (msg: AMQPMessage<P>) => void | Promise<void>): Promise<AMQPSubscription>; /** * Subscribe via an async iterator. Messages continue yielding across reconnections. * @example * ```ts * for await (const msg of await q.subscribe()) { * console.log(msg.bodyString()) * await msg.ack() * } * ``` */ subscribe(params?: QueueSubscribeParams): Promise<AMQPGeneratorSubscription<P>>; /** * Poll the queue for a single message. * @param [params.noAck=true] - automatically acknowledge on delivery */ get(params?: { noAck?: boolean; }): Promise<AMQPMessage<P> | null>; /** * Wait for a single message, then cancel the underlying consumer. * Pairs with {@link get} (zero-wait poll) and {@link subscribe} (long-lived): * - `get()` — is there a message right now? null otherwise. * - `consumeOne({ timeout })` — wait up to N ms for one message. * - `subscribe(cb)` — keep delivering until cancelled. * * Throws when `timeout` elapses with no delivery — distinct from `get()`'s * `null` return, so callers can't conflate "nothing" with "deadline missed". * * The library acks the delivered message before resolving and uses * wire-level `noAck: false` with `prefetch: 1` so the broker holds the * queue at a single in-flight delivery; messages beyond the one returned * stay in the queue for the next consumer. * * @param [options.timeout] - max wait in milliseconds (omit to wait forever) */ consumeOne(options?: { timeout?: number; }): Promise<AMQPMessage<P>>; /** * Bind this queue to an exchange. * @returns `this` for chaining */ bind(exchange: string | AMQPExchange<P, C, KP, KC>, routingKey?: string, args?: Record<string, unknown>): Promise<AMQPQueue<P, C, KP, KC>>; /** * Remove a binding between this queue and an exchange. * @returns `this` for chaining */ unbind(exchange: string | AMQPExchange<P, C, KP, KC>, routingKey?: string, args?: Record<string, unknown>): Promise<AMQPQueue<P, C, KP, KC>>; /** Purge all messages from this queue. */ purge(): Promise<MessageCount>; /** * Delete this queue. * @param [params.ifUnused=false] - only delete if the queue has no consumers * @param [params.ifEmpty=false] - only delete if the queue is empty */ delete(params?: { ifUnused?: boolean; ifEmpty?: boolean; }): Promise<MessageCount>; private openConsumer; } //# sourceMappingURL=amqp-queue.d.ts.map