@mqueue/stompjs
Version:
MQueue adapter for STOMP
2 lines • 2.13 kB
JavaScript
import {Client,ActivationState}from'@stomp/stompjs';import {WebSocket as WebSocket$1}from'ws';var i=class{constructor(n,t){this.client=n;this.destination=t;}type="stomp";_subscription;static async connect(n,t,r={}){let e=new Client({...r,webSocketFactory:()=>new WebSocket$1(n)}),p=new Promise((u,s)=>{e.onConnect=()=>u(),e.onStompError=o=>s(new Error(o.body)),e.onWebSocketError=o=>s(new Error("WebSocket error: "+o));});return e.activate(),await p,new this(e,t)}async healthcheck(){if(!this.client.connected||this.client.state!==ActivationState.ACTIVE)throw new Error("STOMP Receiver unexpectedly disconnected")}async close(){let n=new Promise(t=>{this.client.onWebSocketClose=()=>t(),this.client.onDisconnect=()=>t();});this._subscription?.unsubscribe(),await this.client.deactivate(),await n;}async consume(n){this._subscription=this.client.subscribe(this.destination,async t=>{await n({accept:async()=>{t.ack();},reject:async()=>{t.nack();},transport:{name:this.destination},message:{headers:t.headers,body:Buffer.from(t.isBinaryBody?t.binaryBody:t.body)}});});}};var c=class{constructor(n,t){this.client=n;this.destination=t;}type="stomp";static async connect(n,t,r={}){let e=new Client({...r,webSocketFactory:()=>new WebSocket(n)}),p=new Promise((u,s)=>{e.onConnect=()=>u(),e.onStompError=o=>s(new Error(o.body)),e.onWebSocketError=o=>s(new Error("WebSocket error: "+o));});return e.activate(),await p,new this(e,t)}async healthcheck(){if(!this.client.connected||this.client.state!==ActivationState.ACTIVE)throw new Error("STOMP Sender unexpectedly disconnected")}async close(){let n=new Promise(t=>{this.client.onWebSocketClose=()=>t(),this.client.onDisconnect=()=>t();});await this.client.deactivate(),await n;}async sendMessage(n){let t={};for(let[r,e]of Object.entries(n.headers))e!==void 0&&(t[r]=Array.isArray(e)?e.join(";"):e);this.client.publish({destination:this.destination,headers:t,binaryBody:n.body});}};var a=class{constructor(){}static Outgoing=c;static Incoming=i};export{i as StompIncomingQueue,c as StompOutgoingQueue,a as StompQueue};//# sourceMappingURL=index.js.map
//# sourceMappingURL=index.js.map