@cloudamqp/amqp-client
Version:
AMQP 0-9-1 client, both for browsers (WebSocket) and node (TCP Socket)
42 lines • 1.97 kB
JavaScript
import { serializeAndEncode } from "./amqp-codec-registry.js";
export class AMQPExchange {
constructor(session, name) {
this.session = session;
this.name = name;
}
async publish(body, options = {}) {
const { confirm = true, routingKey = "", mandatory = false, ...properties } = options;
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 ?? {}, body, properties, defaults);
if (encoded.properties.deliveryMode === undefined)
encoded.properties.deliveryMode = 2;
if (confirm) {
const ch = await this.session.getConfirmChannel();
await ch.basicPublish(this.name, routingKey, encoded.body, encoded.properties, mandatory);
}
else {
await this.session.withOpsChannel(async (ch) => {
await ch.basicPublish(this.name, routingKey, encoded.body, encoded.properties, mandatory);
});
}
return this;
}
async bind(source, routingKey = "", args = {}) {
const sourceName = typeof source === "string" ? source : source.name;
await this.session.withOpsChannel((ch) => ch.exchangeBind(this.name, sourceName, routingKey, args));
return this;
}
async unbind(source, routingKey = "", args = {}) {
const sourceName = typeof source === "string" ? source : source.name;
await this.session.withOpsChannel((ch) => ch.exchangeUnbind(this.name, sourceName, routingKey, args));
return this;
}
async delete(params) {
await this.session.withOpsChannel((ch) => ch.exchangeDelete(this.name, params));
}
}
//# sourceMappingURL=amqp-exchange.js.map