mqrpc
Version:
💫 Easy RPC over RabbitMQ
48 lines (47 loc) • 1.6 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const amqp = require("amqplib");
class AmqpClient {
constructor(opts) {
this.prefetchCount = 100;
this.ownConnection = false;
this.ownChannel = false;
this.inited = false;
if (opts.connection)
this.connection = opts.connection;
if (opts.channel)
this.channel = opts.channel;
if (opts.amqpUrl)
this.amqpUrl = opts.amqpUrl;
if (typeof opts.prefetchCount !== 'undefined')
this.prefetchCount = opts.prefetchCount;
this.socketOptions = opts.socketOptions;
}
async init() {
if (!this.amqpUrl && !this.connection && !this.channel) {
throw new Error('Either connection, channel or amqpUrl must be provided');
}
if (this.inited)
return;
if (!this.connection && !this.channel && this.amqpUrl) {
this.connection = await amqp.connect(this.amqpUrl, this.socketOptions);
this.ownConnection = true;
}
if (!this.channel) {
this.channel = await this.connection.createChannel();
await this.channel.prefetch(this.prefetchCount, true);
this.ownChannel = true;
}
this.inited = true;
}
async term() {
if (!this.inited)
return;
if (this.ownChannel)
await this.channel.close();
if (this.ownConnection)
await this.connection.close();
this.inited = false;
}
}
exports.default = AmqpClient;