rapid-mq
Version:
A simple and fast RabbitMQ client for Node.js
2 lines • 7.55 kB
JavaScript
"use strict";var E=Object.create;var u=Object.defineProperty;var q=Object.getOwnPropertyDescriptor;var N=Object.getOwnPropertyNames;var T=Object.getPrototypeOf,R=Object.prototype.hasOwnProperty;var I=(i,n)=>{for(var e in n)u(i,e,{get:n[e],enumerable:!0})},f=(i,n,e,t)=>{if(n&&typeof n=="object"||typeof n=="function")for(let r of N(n))!R.call(i,r)&&r!==e&&u(i,r,{get:()=>n[r],enumerable:!(t=q(n,r))||t.enumerable});return i};var x=(i,n,e)=>(e=i!=null?E(T(i)):{},f(n||!i||!i.__esModule?u(e,"default",{value:i,enumerable:!0}):e,i)),k=i=>f(u({},"__esModule",{value:!0}),i);var O={};I(O,{DefaultRapidEncoder:()=>h,DirectMessager:()=>m,PubSubMessager:()=>d,RapidConnector:()=>p,RpcMessager:()=>_});module.exports=k(O);var b=x(require("amqplib"));var h=class{async encode(n){let e=typeof n,t;return e==="object"&&n instanceof Date&&(e="date",n=n.getTime()),t=JSON.stringify([e,n]),Buffer.from(t,"utf-8")}async decode(n){let[e,t]=JSON.parse(n.toString("utf-8"));switch(e){case"date":return new Date(t);default:return t}}};var p=class{constructor(n){this.options=n;this._connection=null;this._isConnected=!1;if(!n.url)throw new Error("URL is required");if(!n.appId)throw new Error("App ID is required");this._url=n.url,this._appId=n.appId,this._encoder=n.encoder||new h}async connect(){if(!this._isConnected)return this._initPromise?await this._initPromise:(this._initPromise=(async()=>{try{this._connection=await b.connect(this._url),this._isConnected=!0}catch(n){throw this._isConnected=!1,n}})(),await this._initPromise)}async disconnect(){if(!(!this._isConnected||!this._connection))try{await this._connection.close(),this._isConnected=!1,this._connection=null}catch(n){throw n}}get connection(){if(!this._isConnected||!this._connection)throw new Error("Connection is not established");return this._connection}get connected(){return this._isConnected}get appId(){return this._appId}get encoder(){return this._encoder}};var a=class{constructor(n,e,t,r){this._connector=n;this._exchangeName=e;this._durable=t;this._exclusive=r}get connector(){return this._connector}get exchangeName(){return this._exchangeName}};var d=class extends a{constructor(e){if(!e.connector)throw new Error("RapidConnector is required");if(!e.appGroup)throw new Error("App group is required");super(e.connector,e.exchangeName||"pubsub-exchange",e.durable??!0,e.exclusive??!1);this._channel=null;this._appGroup=e.appGroup}get appGroup(){return this._appGroup}async initialize(){if(!this._channel)return this._initPromise?await this._initPromise:(this._initPromise=(async()=>{this._connector.connected||await this._connector.connect();let e=await this._connector.connection.createChannel();if(!e)throw new Error("Connection is not established");e.assertExchange(this._exchangeName,"topic",{durable:this._durable}),this._channel=e})(),await this._initPromise)}async publish(e,t,r){if(!this._channel)throw new Error("Channel is not initialized");let o=await this._connector.encoder.encode(t,this._exchangeName,e);return this._channel.publish(this._exchangeName,e,o,{expiration:r})}async subscribe(e,t){if(!this._channel)throw new Error("Channel is not initialized");let r=await this._channel.assertQueue(`${this._appGroup}.${e}`,{durable:this._durable,exclusive:this._exclusive});await this._channel.bindQueue(r.queue,this._exchangeName,e),await this._channel.bindQueue(r.queue,this._exchangeName,`${this._appGroup}.${e}`),this._channel.consume(r.queue,async o=>{try{if(o!==null){let s=await this._connector.encoder.decode(o.content,this._exchangeName,e);await t(s)}}catch(s){console.error("Error processing message:",s)}},{noAck:!0})}};var v=x(require("crypto")),P=require("stream");var _=class extends a{constructor(e){if(!e.connector)throw new Error("RapidConnector is required");super(e.connector,e.exchangeName||"rpc-exchange",e.durable??!0,e.exclusive??!1);this._channel=null;this._responseQueue="amq.rabbitmq.reply-to";this._timeout=(e.timeoutInSec||5)*1e3,this._emitter=e.emitter||new P.EventEmitter}async initialize(){if(!this._channel)return this._initPromise?await this._initPromise:(this._initPromise=(async()=>{this._connector.connected||await this._connector.connect();let e=await this._connector.connection.createChannel();if(!this._channel)throw new Error("Connection is not established");await e.assertExchange(this._exchangeName,"direct",{durable:this._durable}),await e.consume(this._responseQueue,t=>{t&&t.properties.correlationId&&this._emitter.emit(`rpc:${t.properties.correlationId}`,t.content)},{noAck:!0}),this._channel=e})(),await this._initPromise)}async call(e,...t){if(!this._channel)throw new Error("Channel is not initialized");return new Promise(async(r,o)=>{let s=v.randomUUID(),l=await this._connector.encoder.encode(t,this._exchangeName,e),C=setTimeout(()=>{this._emitter.removeListener(`rpc:${s}`,g),o(new Error(`RPC call to ${e} timed out after ${this._timeout/1e3} seconds`))},this._timeout),g=async w=>{if(clearTimeout(C),w){let y=await this._connector.encoder.decode(w,this._exchangeName,e);r(y)}else o(new Error(`No response received for RPC call to ${e}`))};this._emitter.once(`rpc:${s}`,g),this._channel.publish(this._exchangeName,e,l,{replyTo:this._responseQueue,correlationId:s,expiration:this._timeout})})}async server(e,t){if(!this._channel)throw new Error("Channel is not initialized");let r=await this._channel.assertQueue(e,{durable:this._durable,exclusive:this._exclusive});await this._channel.bindQueue(r.queue,this._exchangeName,e),this._channel.consume(r.queue,async o=>{try{if(o!==null){let s=await this._connector.encoder.decode(o.content,this._exchangeName,e),l=await t(...s);o.properties.replyTo&&o.properties.correlationId&&this._channel.sendToQueue(o.properties.replyTo,await this._connector.encoder.encode(l,this._exchangeName,e),{correlationId:o.properties.correlationId})}}catch(s){console.error(`Error processing RPC call for method ${e}:`,s)}},{noAck:!0})}};var m=class extends a{constructor(e){if(!e.connector)throw new Error("RapidConnector is required");if(!e.consumerTag)throw new Error("Consumer tag is required");super(e.connector,e.exchangeName||"direct-exchange",e.durable??!0,e.exclusive??!1);this._channel=null;this._consumerTag=e.consumerTag}get consumerTag(){return this._consumerTag}async initialize(){if(!this._channel)return this._initPromise?await this._initPromise:(this._initPromise=(async()=>{this._connector.connected||await this._connector.connect();let e=await this._connector.connection.createChannel();if(!e)throw new Error("Connection is not established");await e.assertExchange(this._exchangeName,"direct",{durable:this._durable});let t=await e.assertQueue(`${this._consumerTag}.queue`,{durable:this._durable,exclusive:this._exclusive});await e.bindQueue(t.queue,this._exchangeName,t.queue),await e.bindQueue(t.queue,this._exchangeName,this._consumerTag),this._channel=e})(),await this._initPromise)}async send(e,t,r){if(!this._channel)throw new Error("Channel is not initialized");let o=await this._connector.encoder.encode(t,this._exchangeName,e);return this._channel.publish(this._exchangeName,e,o,{expiration:r})}async listen(e){if(!this._channel)throw new Error("Channel is not initialized");await this._channel.consume(`${this._consumerTag}.queue`,async t=>{try{if(t&&t.content){let r=await this._connector.encoder.decode(t.content,this._exchangeName,this._consumerTag);await e(r)}}catch(r){console.error("Error processing message:",r)}},{noAck:!0})}};0&&(module.exports={DefaultRapidEncoder,DirectMessager,PubSubMessager,RapidConnector,RpcMessager});
//# sourceMappingURL=index.js.map