UNPKG

@cloudamqp/amqp-client

Version:

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

39 lines 1.61 kB
import { serializeAndEncode } from "./amqp-codec-registry.js"; export class AMQPRPCServer { constructor(session) { this.subscription = null; this.session = session; } async start(queue, handler, prefetch = 1) { if (this.subscription) throw new Error("RPC server already started"); const q = await this.session.queue(queue); this.subscription = await q.subscribe({ prefetch, noAck: false, requeueOnNack: false }, async (msg) => { const { replyTo, correlationId } = msg.properties; if (!replyTo) { await msg.nack(false); return; } const result = await handler(msg); const replyProps = {}; if (correlationId !== undefined) replyProps.correlationId = correlationId; const defaults = {}; if (this.session.defaultContentType) defaults.contentType = this.session.defaultContentType; if (this.session.defaultContentEncoding) defaults.contentEncoding = this.session.defaultContentEncoding; const encoded = await serializeAndEncode(this.session.parsers ?? {}, this.session.coders ?? {}, result, replyProps, defaults); await msg.channel.basicPublish("", replyTo, encoded.body, encoded.properties); }); return this; } async close() { const sub = this.subscription; if (!sub) return; this.subscription = null; await sub.cancel(); } } //# sourceMappingURL=amqp-rpc-server.js.map