|
1 | | -import { AmqpQueueSpec, QueueSpec } from "./queue.js" |
| 1 | +import { AmqpQueue, Queue, QueueOptions, QueueType } from "./queue.js" |
| 2 | +import { |
| 3 | + EventContext, |
| 4 | + Receiver, |
| 5 | + ReceiverEvents, |
| 6 | + ReceiverOptions, |
| 7 | + Connection as RheaConnection, |
| 8 | + Sender, |
| 9 | + SenderEvents, |
| 10 | + SenderOptions, |
| 11 | +} from "rhea" |
| 12 | +import { AmqpEndpoints, AmqpMethods, MessageBuilder, ME } from "./message_builder.js" |
| 13 | +import { CreateQueueResponseDecoder, DeleteQueueResponseDecoder } from "./response_decoder.js" |
| 14 | + |
| 15 | +type LinkOpenEvents = SenderEvents.senderOpen | ReceiverEvents.receiverOpen |
| 16 | +type LinkErrorEvents = SenderEvents.senderError | ReceiverEvents.receiverError |
| 17 | +type OpenLinkMethods = |
| 18 | + | ((options?: SenderOptions | string) => Sender) |
| 19 | + | ((options?: ReceiverOptions | string) => Receiver) |
| 20 | + |
| 21 | +const MANAGEMENT_NODE_CONFIGURATION: SenderOptions | ReceiverOptions = { |
| 22 | + snd_settle_mode: 1, |
| 23 | + rcv_settle_mode: 0, |
| 24 | + name: "management-link-pair", |
| 25 | + target: { address: "/management", expiry_policy: "LINK_DETACH", timeout: 0, dynamic: false }, |
| 26 | + source: { address: "/management", expiry_policy: "LINK_DETACH", timeout: 0, dynamic: false, durable: 0 }, |
| 27 | + properties: { paired: true }, |
| 28 | +} |
2 | 29 |
|
3 | 30 | export interface Management { |
4 | | - queue: (queueName: string) => QueueSpec |
| 31 | + declareQueue: (queueName: string, options?: Partial<QueueOptions>) => Promise<Queue> |
| 32 | + deleteQueue: (queueName: string) => Promise<boolean> |
5 | 33 | close: () => void |
6 | 34 | } |
7 | 35 |
|
8 | 36 | export class AmqpManagement implements Management { |
9 | | - constructor() {} |
| 37 | + static async create(connection: RheaConnection): Promise<AmqpManagement> { |
| 38 | + const senderLink = await AmqpManagement.openSender(connection) |
| 39 | + const receiverLink = await AmqpManagement.openReceiver(connection) |
| 40 | + return new AmqpManagement(connection, senderLink, receiverLink) |
| 41 | + } |
| 42 | + |
| 43 | + constructor( |
| 44 | + private readonly connection: RheaConnection, |
| 45 | + private senderLink: Sender, |
| 46 | + private receiverLink: Receiver |
| 47 | + ) { |
| 48 | + console.log(this.receiverLink.is_open()) |
| 49 | + } |
| 50 | + |
| 51 | + private static async openReceiver(connection: RheaConnection): Promise<Receiver> { |
| 52 | + return AmqpManagement.openLink<Receiver>( |
| 53 | + connection, |
| 54 | + ReceiverEvents.receiverOpen, |
| 55 | + ReceiverEvents.receiverError, |
| 56 | + connection.open_receiver.bind(connection), |
| 57 | + MANAGEMENT_NODE_CONFIGURATION |
| 58 | + ) |
| 59 | + } |
| 60 | + |
| 61 | + private static async openSender(connection: RheaConnection): Promise<Sender> { |
| 62 | + return AmqpManagement.openLink<Sender>( |
| 63 | + connection, |
| 64 | + SenderEvents.senderOpen, |
| 65 | + SenderEvents.senderError, |
| 66 | + connection.open_sender.bind(connection), |
| 67 | + MANAGEMENT_NODE_CONFIGURATION |
| 68 | + ) |
| 69 | + } |
| 70 | + |
| 71 | + private static async openLink<T extends Sender | Receiver>( |
| 72 | + connection: RheaConnection, |
| 73 | + successEvent: LinkOpenEvents, |
| 74 | + errorEvent: LinkErrorEvents, |
| 75 | + openMethod: OpenLinkMethods, |
| 76 | + config?: SenderOptions | ReceiverOptions | string |
| 77 | + ): Promise<T> { |
| 78 | + return new Promise((res, rej) => { |
| 79 | + connection.once(successEvent, (context) => { |
| 80 | + return res(context.receiver || context.sender) |
| 81 | + }) |
| 82 | + connection.once(errorEvent, (context) => { |
| 83 | + return rej(context.connection.error) |
| 84 | + }) |
| 85 | + openMethod(config) |
| 86 | + }) |
| 87 | + } |
| 88 | + |
| 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 | + } |
10 | 115 |
|
11 | | - close() {} |
| 116 | + return res(new AmqpQueue(response.body)) |
| 117 | + }) |
12 | 118 |
|
13 | | - queue(queueName: string): QueueSpec { |
14 | | - return new AmqpQueueSpec().name(queueName) |
| 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) |
| 131 | + }) |
15 | 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 | + } |
| 145 | + |
| 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 | + }) |
| 156 | + } |
| 157 | +} |
| 158 | + |
| 159 | +function buildArgumentsFrom(queueType?: QueueType, queueOptions?: Record<string, string>) { |
| 160 | + return { ...(queueOptions ?? {}), ...(queueType ? { "x-queue-type": queueType } : {}) } |
16 | 161 | } |
0 commit comments