1- import { AmqpQueueInfo , QueueInfo } from "./queue.js"
1+ import { AmqpQueue , Queue , QueueOptions , QueueType } from "./queue.js"
22import {
3- generate_uuid ,
3+ EventContext ,
44 Receiver ,
55 ReceiverEvents ,
66 ReceiverOptions ,
@@ -9,11 +9,8 @@ import {
99 SenderEvents ,
1010 SenderOptions ,
1111} from "rhea"
12-
13- type QueueOptions = {
14- exclusive : boolean
15- auto_delete : boolean
16- }
12+ import { AmqpEndpoints , AmqpMethods , MessageBuilder , ME } from "./message_builder.js"
13+ import { CreateQueueResponseDecoder , DeleteQueueResponseDecoder } from "./response_decoder.js"
1714
1815type LinkOpenEvents = SenderEvents . senderOpen | ReceiverEvents . receiverOpen
1916type LinkErrorEvents = SenderEvents . senderError | ReceiverEvents . receiverError
@@ -31,27 +28,26 @@ const MANAGEMENT_NODE_CONFIGURATION: SenderOptions | ReceiverOptions = {
3128}
3229
3330export interface Management {
34- declareQueue : ( queueName : string , options : Partial < QueueOptions > ) => QueueInfo
31+ declareQueue : ( queueName : string , options ?: Partial < QueueOptions > ) => Promise < Queue >
32+ deleteQueue : ( queueName : string ) => Promise < boolean >
3533 close : ( ) => void
3634}
3735
3836export class AmqpManagement implements Management {
3937 static async create ( connection : RheaConnection ) : Promise < AmqpManagement > {
4038 const senderLink = await AmqpManagement . openSender ( connection )
4139 const receiverLink = await AmqpManagement . openReceiver ( connection )
42- return new AmqpManagement ( senderLink , receiverLink )
40+ return new AmqpManagement ( connection , senderLink , receiverLink )
4341 }
4442
4543 constructor (
46- // private readonly connection: RheaConnection,
44+ private readonly connection : RheaConnection ,
4745 private senderLink : Sender ,
4846 private receiverLink : Receiver
4947 ) {
5048 console . log ( this . receiverLink . is_open ( ) )
5149 }
5250
53- async close ( ) { }
54-
5551 private static async openReceiver ( connection : RheaConnection ) : Promise < Receiver > {
5652 return AmqpManagement . openLink < Receiver > (
5753 connection ,
@@ -90,18 +86,76 @@ export class AmqpManagement implements Management {
9086 } )
9187 }
9288
93- declareQueue ( queueName : string , options : Partial < QueueOptions > = { } ) : QueueInfo {
94- // decode the response
95- // create queueInfo
89+ close ( ) : void {
90+ if ( this . connection . is_closed ( ) ) return
91+
92+ this . closeSender ( )
93+ this . closeReceiver ( )
94+ }
95+
96+ private closeSender ( ) : void {
97+ this . senderLink . close ( )
98+ }
99+
100+ private closeReceiver ( ) : void {
101+ this . senderLink . close ( )
102+ }
103+
104+ async declareQueue ( queueName : string , options : Partial < QueueOptions > = { } ) : Promise < Queue > {
105+ return new Promise ( ( res , rej ) => {
106+ this . receiverLink . once ( ReceiverEvents . message , ( context : EventContext ) => {
107+ if ( ! context . message ) {
108+ return rej ( new Error ( "Receiver has not received any message" ) )
109+ }
110+
111+ const response = new CreateQueueResponseDecoder ( ) . decodeFrom ( context . message , String ( message . message_id ) )
112+ if ( response . status === "error" ) {
113+ return rej ( response . error )
114+ }
96115
97- this . senderLink . send ( {
98- message_id : generate_uuid ( ) ,
99- to : `/queues/${ encodeURIComponent ( queueName ) } ` ,
100- reply_to : "$me" ,
101- subject : "PUT" ,
102- body : options ,
116+ return res ( new AmqpQueue ( response . body ) )
117+ } )
118+
119+ const message = new MessageBuilder ( )
120+ . sendTo ( `/${ AmqpEndpoints . Queues } /${ encodeURIComponent ( queueName ) } ` )
121+ . setReplyTo ( ME )
122+ . setAmqpMethod ( AmqpMethods . PUT )
123+ . setBody ( {
124+ exclusive : options . exclusive ?? false ,
125+ durable : options . durable ?? false ,
126+ auto_delete : options . autoDelete ?? false ,
127+ arguments : buildArgumentsFrom ( options . type , options . arguments ) ,
128+ } )
129+ . build ( )
130+ this . senderLink . send ( message )
103131 } )
132+ }
133+
134+ async deleteQueue ( queueName : string ) : Promise < boolean > {
135+ return new Promise ( ( res , rej ) => {
136+ this . receiverLink . once ( ReceiverEvents . message , ( context : EventContext ) => {
137+ if ( ! context . message ) {
138+ return rej ( new Error ( "Receiver has not received any message" ) )
139+ }
140+
141+ const response = new DeleteQueueResponseDecoder ( ) . decodeFrom ( context . message , String ( message . message_id ) )
142+ if ( response . status === "error" ) {
143+ return rej ( response . error )
144+ }
104145
105- return new AmqpQueueInfo ( { name : queueName } )
146+ return res ( true )
147+ } )
148+
149+ const message = new MessageBuilder ( )
150+ . sendTo ( `/${ AmqpEndpoints . Queues } /${ encodeURIComponent ( queueName ) } ` )
151+ . setReplyTo ( ME )
152+ . setAmqpMethod ( AmqpMethods . DELETE )
153+ . build ( )
154+ this . senderLink . send ( message )
155+ } )
106156 }
107157}
158+
159+ function buildArgumentsFrom ( queueType ?: QueueType , queueOptions ?: Record < string , string > ) {
160+ return { ...( queueOptions ?? { } ) , ...( queueType ? { "x-queue-type" : queueType } : { } ) }
161+ }
0 commit comments