@cloudamqp/amqp-client
Version:
AMQP 0-9-1 client, both for browsers (WebSocket) and node (TCP Socket)
43 lines • 1.79 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.AMQPRPCServer = void 0;
const amqp_codec_registry_js_1 = require("./amqp-codec-registry.js");
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 (0, amqp_codec_registry_js_1.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();
}
}
exports.AMQPRPCServer = AMQPRPCServer;
//# sourceMappingURL=amqp-rpc-server.js.map