@@ -2,6 +2,8 @@ import * as winston from "winston";
22import * as amqplib from "amqplib" ;
33import { Util } from "./Util" ;
44import { Config } from "./Config" ;
5+ import { cli } from "winston/lib/winston/config" ;
6+ import { Crypt } from "./Crypt" ;
57
68type QueueOnMessage = ( msg : string , options : QueueMessageOptions , ack : any , done : any ) => void ;
79interface IHashTable < T > {
@@ -104,6 +106,16 @@ export class amqpwrapper {
104106 // Bad idear ...
105107 // this.AssertExchangeOptions.arguments['alternate-exchange'] = Config.deadLetterExchange;
106108 //}
109+
110+ if ( ! Util . IsNullEmpty ( Config . amqp_dlx ) ) {
111+ this . AssertQueueOptions . arguments = { } ;
112+ this . AssertQueueOptions . arguments [ 'x-dead-letter-exchange' ] = Config . amqp_dlx ;
113+ // // arguments: {
114+ // // 'x-dead-letter-exchange': Config.amqp_dlx_prefix + queue,
115+ // // 'x-dead-letter-routing-key': Config.amqp_dlrk_prefix + queue,
116+ // // 'x-message-ttl': Config.amqp_message_ttl
117+
118+ }
107119 }
108120 private timeout : NodeJS . Timeout = null ;
109121 async connect ( ) : Promise < void > {
@@ -131,7 +143,7 @@ export class amqpwrapper {
131143 if ( ! Util . IsNullEmpty ( this . replyqueue ) ) {
132144 delete this . queues [ this . replyqueue ] ;
133145 }
134- this . replyqueue = await this . AddQueueConsumer ( "" , null , ( msg : any , options : QueueMessageOptions , ack : any , done : any ) => {
146+ this . replyqueue = await this . AddQueueConsumer ( "" , null , null , ( msg : any , options : QueueMessageOptions , ack : any , done : any ) => {
135147 if ( ! Util . IsNullUndefinded ( this . activecalls [ options . correlationId ] ) ) {
136148 this . activecalls [ options . correlationId ] . resolve ( msg ) ;
137149 this . activecalls [ options . correlationId ] = null ;
@@ -163,13 +175,13 @@ export class amqpwrapper {
163175 var keys = Object . keys ( this . exchanges ) ;
164176 for ( var i = 0 ; i < keys . length ; i ++ ) {
165177 var q1 : amqpexchange = this . exchanges [ keys [ i ] ] ;
166- this . AddExchangeConsumer ( q1 . exchange , q1 . algorithm , q1 . routingkey , q1 . ExchangeOptions , q1 . callback ) ;
178+ this . AddExchangeConsumer ( q1 . exchange , q1 . algorithm , q1 . routingkey , q1 . ExchangeOptions , null , q1 . callback ) ;
167179 }
168180 var keys = Object . keys ( this . queues ) ;
169181 for ( var i = 0 ; i < keys . length ; i ++ ) {
170182 if ( keys [ i ] != this . replyqueue ) {
171183 var q2 : amqpqueue = this . queues [ keys [ i ] ] ;
172- this . AddQueueConsumer ( q2 . queue , q2 . QueueOptions , q2 . callback ) ;
184+ this . AddQueueConsumer ( q2 . queue , q2 . QueueOptions , null , q2 . callback ) ;
173185 }
174186 }
175187 }
@@ -183,8 +195,16 @@ export class amqpwrapper {
183195 await this . channel . cancel ( q . consumerTag ) ;
184196 delete this . queues [ q . queue ] ;
185197 }
186- async AddQueueConsumer ( queue : string , QueueOptions : any , callback : QueueOnMessage ) : Promise < string > {
198+ async AddQueueConsumer ( queue : string , QueueOptions : any , jwt : string , callback : QueueOnMessage ) : Promise < string > {
187199 var q : amqpqueue = null ;
200+ if ( Config . amqp_force_queue_prefix && ! Util . IsNullEmpty ( jwt ) ) {
201+ var tuser = Crypt . verityToken ( jwt ) ;
202+ var name = tuser . username . split ( "@" ) . join ( "" ) . split ( "." ) . join ( "" ) ;
203+ name = name . toLowerCase ( ) ;
204+ var isrole = tuser . roles . filter ( x => x . _id == queue ) ;
205+ if ( isrole . length == 0 && tuser . _id != queue ) queue = name + queue ;
206+ }
207+
188208 if ( this . exchanges [ queue ] != null ) {
189209 q = this . queues [ queue ] ;
190210 } else {
@@ -211,8 +231,15 @@ export class amqpwrapper {
211231 this . queues [ q . queue ] = q ;
212232 return q . queue ;
213233 }
214- async AddExchangeConsumer ( exchange : string , algorithm : string , routingkey : string , ExchangeOptions : any , callback : QueueOnMessage ) : Promise < void > {
234+ async AddExchangeConsumer ( exchange : string , algorithm : string , routingkey : string , ExchangeOptions : any , jwt : string , callback : QueueOnMessage ) : Promise < void > {
215235 var q : amqpexchange = null ;
236+ if ( Config . amqp_force_exchange_prefix && ! Util . IsNullEmpty ( jwt ) ) {
237+ var tuser = Crypt . verityToken ( jwt ) ;
238+ var name = tuser . username . split ( "@" ) . join ( "" ) . split ( "." ) . join ( "" ) ;
239+ name = name . toLowerCase ( ) ;
240+ exchange = name + exchange ;
241+ }
242+
216243 if ( this . exchanges [ exchange ] != null ) {
217244 q = this . exchanges [ exchange ] ;
218245 } else {
@@ -224,7 +251,12 @@ export class amqpwrapper {
224251 q . ExchangeOptions = new Object ( ( ExchangeOptions != null ? ExchangeOptions : this . AssertExchangeOptions ) ) ;
225252 q . exchange = exchange ; q . algorithm = algorithm ; q . routingkey = routingkey ; q . callback = callback ;
226253 this . _ok = await this . channel . assertExchange ( q . exchange , q . algorithm , q . ExchangeOptions ) ;
227- q . queue = await this . AddQueueConsumer ( "" , null , q . callback ) ;
254+ var AssertQueueOptions = null ;
255+ if ( ! Util . IsNullEmpty ( Config . amqp_dlx ) && exchange == Config . amqp_dlx ) {
256+ AssertQueueOptions = Object . create ( this . AssertQueueOptions ) ;
257+ delete AssertQueueOptions . arguments ;
258+ }
259+ q . queue = await this . AddQueueConsumer ( "" , AssertQueueOptions , jwt , q . callback ) ;
228260 this . channel . bindQueue ( q . queue , q . exchange , q . routingkey ) ;
229261 this . _logger . info ( "[AMQP] Added exchange consumer " + q . exchange ) ;
230262 this . exchanges [ exchange ] = q ;
@@ -254,13 +286,14 @@ export class amqpwrapper {
254286 }
255287 this . channel . ack ( msg ) ;
256288 } , ( result ) => {
257- if ( msg != null && ! Util . IsNullEmpty ( replyTo ) ) {
258- try {
259- this . channel . sendToQueue ( replyTo , Buffer . from ( result ) , { correlationId : msg . properties . correlationId } ) ;
260- } catch ( error ) {
261- console . error ( "Error sending response to " + replyTo + " " + JSON . stringify ( error ) )
262- }
263- }
289+ // ROLLBACK
290+ // if (msg != null && !Util.IsNullEmpty(replyTo)) {
291+ // try {
292+ // this.channel.sendToQueue(replyTo, Buffer.from(result), { correlationId: msg.properties.correlationId });
293+ // } catch (error) {
294+ // console.error("Error sending response to " + replyTo + " " + JSON.stringify(error))
295+ // }
296+ // }
264297 } ) ;
265298 }
266299 async sendWithReply ( exchange : string , queue : string , data : any , expiration : number , correlationId : string ) : Promise < string > {
0 commit comments