# All files / websockets gateway-metadata-explorer.ts

95% Statements 19/20
66.67% Branches 4/6
100% Functions 5/5
95% Lines 19/20

    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
    651x

    1x

    1x
    30x

    1x
    1x

    3x

    5x
    5x

    5x
    2x

    3x
    3x

    1x
    2x

    2x
    2x

    2x
    2x

     import { isFunction, isUndefined } from '@nestjs/common/utils/shared.utils';
    import { MetadataScanner } from '@nestjs/core/metadata-scanner';
    import { Observable } from 'rxjs';
    import {
      GATEWAY_SERVER_METADATA,
      MESSAGE_MAPPING_METADATA,
      MESSAGE_METADATA,
    } from './constants';
    import { NestGateway } from './interfaces/nest-gateway.interface';

    export interface MessageMappingProperties {
      message: any;
      callback: (...args) => Observable<any> | Promise<any> | any;
    }

    export class GatewayMetadataExplorer {
      constructor(private readonly metadataScanner: MetadataScanner) {}

      public explore(instance: NestGateway): MessageMappingProperties[] {
        const instancePrototype = Object.getPrototypeOf(instance);
        return this.metadataScanner.scanFromPrototype<
          NestGateway,
          MessageMappingProperties
        >(instance, instancePrototype, method =>
          this.exploreMethodMetadata(instancePrototype, method),
        );
      }

      public exploreMethodMetadata(
        instancePrototype,
        methodName: string,
      ): MessageMappingProperties {
        const callback = instancePrototype[methodName];
        const isMessageMapping = Reflect.getMetadata(
          MESSAGE_MAPPING_METADATA,
          callback,
        );
        if (isUndefined(isMessageMapping)) {
          return null;
        }
        const message = Reflect.getMetadata(MESSAGE_METADATA, callback);
        return {
          callback,
          message,
        };
      }

      public *scanForServerHooks(instance: NestGateway): IterableIterator<string> {
        for (const propertyKey in instance) {
          Iif (isFunction(propertyKey)) {
            continue;
          }
          const property = String(propertyKey);
          const isServer = Reflect.getMetadata(
            GATEWAY_SERVER_METADATA,
            instance,
            property,
          );
          Eif (!isUndefined(isServer)) {
            yield property;
          }
        }
      }
    }