UNPKG

mqrpc

Version:

💫 Easy RPC over RabbitMQ

56 lines (55 loc) • 2.73 kB
import { Channel, Message } from 'amqplib'; import { ClientPayload, TimeoutDesc } from '../common'; /** * Sends an ack message to the client. This indicates the server has received a * procedure call and will start handling it. * * @param {Channel} channel The Amqp channel to use. * @param {Message} message The client's original message. */ export declare const ack: (channel: Channel, message: Message) => Promise<void>; /** * Sends a `wait` message to the client. This indicates the server is still * processing the call and the client should keep waiting for the reply. * * @param {Channel} channel The Amqp channel to use. * @param {Message} message The client's original message. */ export declare const wait: (channel: Channel, message: Message) => Promise<void>; /** * Replies to the client with a given response. Because this is a reply, the client's * message will be `ack`ed in the channel. * * If the `response` is an error, it will be serialized according to the error's config. * Otherwise, the response is serialized and sent as-is. * * @param {Channel} channel The Amqp channel to use. * @param {Message} message The client's original message. * @param {RpcServerError | any} response What to send back to the client. */ export declare const reply: (channel: Channel, message: Message, response?: any) => Promise<void>; /** * Handles messages on the .call queue. If the call is valid, its contents are * returned, otherwise an error is thrown. * * @param {Channel} channel The AMQP channel where the message arrived from. * @param {Message} message The AMQP message received from the queue. * @return {ClientPayload} The message content, if valid. * @throws {Error} If the content cannot be retrieved or the message * is in any way invalid. */ export declare const extractCallContent: (message: Message) => ClientPayload; /** * Returns a function that, when called with another function, sends the first * `ack` message to the client and executes the given function. While waiting * for a return, if `idleTimeout` is set, every idleTimeout / 3 a `wait` * message is sent to the client. * * The inner function will return or raise whatever the given function returns. * * @param {Channel} channel The AMQP channel where the message arrived from. * @param {Message} message The AMQP message received from the queue. * @param {TimeoutDesc} timeouts The timeouts set on the RpcClient. * @returns {Function} */ export declare const whileSendingHeartbeats: (channel: Channel, message: Message, timeouts: TimeoutDesc) => <T>(fn: () => Promise<T>) => Promise<T>;