All files / lib/infrastructure publisher.js

80.76% Statements 21/26
0% Branches 0/2
90.9% Functions 10/11
84% Lines 21/25

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148          3x 3x                                                                                           5x 5x 5x             2x               1x               1x               3x               3x             5x 5x 5x         5x                             1x 1x             1x 1x                   1x 1x       3x  
 
/**
 * @module Publisher
 */
 
const Middleware = require('../middlewares/wrapper');
const { v4 } = require('uuid');
 
/**
 * Publisher
 */
 
class Publisher {
  /**
   * посилання на amqp-manager
   * {@link  ???} AmqpManager*/
  #manager
 
  /**
   * @property {URL} connection - посилання на з'єднання з amqp
   */
  #connection;
 
  /**
   * @property {Object} client - UUID клієнта генерується під час створення екземпляра класу
   */
  #client;
 
  /**
   * @property {Object} options - налаштування Публікувальника повідомлень
   */
  #options;
 
  /**
   * @property {Object} channel - посилання на екземпляр каналу
   */
  #channel;
 
  /**
   * @property {URL} middleware - посилання на ланцюжок оброблення повыдомлень
   * {@link use}
   * {@link execute} (../middlewares/wrapper.js)
   */
  #middleware;
 
/**
 * створює екземпляр публікувальника
 * @param manager - примірник amqp-manager {@link ???}(./amqp-manager.js)
 * @param options - налаштування Публікувальника {@link options}
 * @property client - {@see client}
 * */
  constructor(manager, options) {
    this.#manager = manager;
    this.#client = v4();
    this.#options = options;
  }
 
  /** гетер для з'єднання з amqp
   * @returns {Object} - {@see connection}
   * */
  get connection() {
    return this.#connection;
  }
 
  /**
   * гетер для UUID клієнта
   * @returns {Object} - {@see client}
   */
  get client() {
    return this.#client;
  }
 
  /**
   * гетер для налаштування Публікувальника повідомлень
   * @returns {Object} - {@see client}
   */
  get options() {
    return this.#options;
  }
 
  /**
   * гетер для посилання на екземпляр каналу
   * @returns {URL} - {@see client}
   */
  get channel() {
    return this.#channel;
  }
 
  /**
   * гетер для посилання на ланцюжок оброблення повыдомлень
   * @returns {URL} - {@see client}
   */
  get middleware() {
    return this.#middleware;
  }
 
  /**
   * Ініціалізація Publisher-залежностей
   */
  async moduleInit() {
    this.#connection = await this.#manager.getConnection(this.#client, this.#options.amqp);
    this.#channel = await this.#connection.createChannel();
    await this.#channel.assertExchange(
      this.#options.exchange.name,
      this.#options.exchange.mode,
      this.#options.exchange.options,
    );
    this.#middleware = new Middleware(async (err, msg, next) => {
      if (err) throw err;
      await this.#channel.publish(
        this.#options.exchange.name,
        '',
        Buffer.from(msg.content),
      );
      next();
    });
  }
  /**
   * Додає callback до ланцюжка оброблення повідомлень @link (#middleware)
   * @param callback
   * */
  use(callback) {
    this.#middleware.use(callback);
    return this;
  }
  /**
   * Відправлення повідомлення при цьому виконує ланцюжок оброблення повідомлення повідомлення {@link #middleware}
   * @param msg - повідомлення
   */
  async send(msg) {
    try {
      await this.#middleware.execute({ content: msg });
    } catch (err) {
      throw err;
    }
  }
 
  /**
   * Закриває канал по клієнту
   */
  async close() {
    await this.#channel.close();
    this.#manager.closeConnection(this.#client);
  }
}
 
module.exports = Publisher;