# All files / microservices/client client-rmq.ts

90.63% Statements 58/64
95% Branches 19/20
80% Functions 16/20
91.94% Lines 57/62

    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
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    1721x
    1x
    1x
    1x
    1x
    1x

    1x

    1x

    1x

    1x
    2x

    2x
    2x

    2x
    2x
    2x

    2x

    2x

    2x

    2x

    2x
    2x

    2x
    2x

    2x
    2x

    2x

    3x

    3x
    3x

    3x
    3x

    3x

    2x
    2x

    2x

    1x

    1x

    1x

    4x
    4x

    4x

    4x
    4x
    4x

    4x

    3x
    3x
    3x

    3x
    3x

    3x

    3x
    3x
    2x

    3x

    3x

     import { Logger } from '@nestjs/common/services/logger.service';
    import { loadPackage } from '@nestjs/common/utils/load-package.util';
    import { randomStringGenerator } from '@nestjs/common/utils/random-string-generator.util';
    import { EventEmitter } from 'events';
    import { fromEvent, merge, Observable } from 'rxjs';
    import { first, map, share, switchMap } from 'rxjs/operators';
    import { ClientOptions, RmqOptions } from '../interfaces';
    import {
      DISCONNECT_EVENT,
      ERROR_EVENT,
      RQM_DEFAULT_IS_GLOBAL_PREFETCH_COUNT,
      RQM_DEFAULT_PREFETCH_COUNT,
      RQM_DEFAULT_QUEUE,
      RQM_DEFAULT_QUEUE_OPTIONS,
      RQM_DEFAULT_URL,
    } from './../constants';
    import { WritePacket } from './../interfaces';
    import { ClientProxy } from './client-proxy';

    let rqmPackage: any = {};

    export class ClientRMQ extends ClientProxy {
      protected readonly logger = new Logger(ClientProxy.name);
      protected connection: Promise<any>;
      protected client: any = null;
      protected channel: any = null;
      protected urls: string[];
      protected queue: string;
      protected prefetchCount: number;
      protected isGlobalPrefetchCount: boolean;
      protected queueOptions: any;
      protected replyQueue: string;
      protected responseEmitter: EventEmitter;

      constructor(protected readonly options: ClientOptions['options']) {
        super();
        this.urls = this.getOptionsProp<RmqOptions>(this.options, 'urls') || [
          RQM_DEFAULT_URL,
        ];
        this.queue =
          this.getOptionsProp<RmqOptions>(this.options, 'queue') ||
          RQM_DEFAULT_QUEUE;
        this.prefetchCount =
          this.getOptionsProp<RmqOptions>(this.options, 'prefetchCount') ||
          RQM_DEFAULT_PREFETCH_COUNT;
        this.isGlobalPrefetchCount =
          this.getOptionsProp<RmqOptions>(this.options, 'isGlobalPrefetchCount') ||
          RQM_DEFAULT_IS_GLOBAL_PREFETCH_COUNT;
        this.queueOptions =
          this.getOptionsProp<RmqOptions>(this.options, 'queueOptions') ||
          RQM_DEFAULT_QUEUE_OPTIONS;

        loadPackage('amqplib', ClientRMQ.name);
        rqmPackage = loadPackage('amqp-connection-manager', ClientRMQ.name);
      }

      public close(): void {
        this.channel && this.channel.close();
        this.client && this.client.close();
      }

      public consumeChannel() {
        this.channel.addSetup(channel =>
          channel.consume(
            this.replyQueue,
            msg => this.responseEmitter.emit(msg.properties.correlationId, msg),
            { noAck: true },
          ),
        );
      }

      public connect(): Promise<any> {
        Iif (this.client) {
          return this.connection;
        }
        this.client = this.createClient();
        this.handleError(this.client);

        const connect$ = this.connect$(this.client);
        this.connection = this.mergeDisconnectEvent(this.client, connect$)
          .pipe(switchMap(() => this.createChannel()), share())
          .toPromise();
        return this.connection;
      }

      public createChannel(): Promise<void> {
        return new Promise(resolve => {
          this.channel = this.client.createChannel({
            json: false,
            setup: channel => this.setupChannel(channel, resolve),
          });
        });
      }

      public createClient<T = any>(): T {
        return rqmPackage.connect(this.urls) as T;
      }

      public mergeDisconnectEvent<T = any>(
        instance: any,
        source$: Observable<T>,
      ): Observable<T> {
        const close$ = fromEvent(instance, DISCONNECT_EVENT).pipe(
          map(err => {
            throw err;
          }),
        );
        return merge(source$, close$).pipe(first());
      }

      public async setupChannel(channel: any, resolve: Function) {
        await channel.assertQueue(this.queue, this.queueOptions);
        await channel.prefetch(this.prefetchCount, this.isGlobalPrefetchCount);

        this.replyQueue = (await channel.assertQueue('', {
          exclusive: true,
        })).queue;

        this.responseEmitter = new EventEmitter();
        this.responseEmitter.setMaxListeners(0);
        this.consumeChannel();

        resolve();
      }

      protected publish(
        message: any,
        callback: (packet: WritePacket) => any,
      ): Function {
        try {
          const correlationId = randomStringGenerator();
          const listener = ({ content }) =>
            this.handleMessage(JSON.parse(content.toString()), callback);

          this.responseEmitter.on(correlationId, listener);
          this.channel.sendToQueue(
            this.queue,
            Buffer.from(JSON.stringify(message)),
            {
              replyTo: this.replyQueue,
              correlationId,
            },
          );
          return () => this.responseEmitter.removeListener(correlationId, listener);
        } catch (err) {
          callback({ err });
        }
      }

      public handleMessage(
        packet: WritePacket,
        callback: (packet: WritePacket) => any,
      ) {
        const { err, response, isDisposed } = packet;
        if (isDisposed || err) {
          callback({
            err,
            response: null,
            isDisposed: true,
          });
        }
        callback({
          err,
          response,
        });
      }

      public handleError(client: any): void {
        client.addListener(ERROR_EVENT, err => this.logger.error(err));
      }
    }