# All files / websockets socket-server-provider.ts

100% Statements 24/24
83.33% Branches 5/6
100% Functions 6/6
100% Lines 23/23

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

    1x

    1x

    26x
    26x

    2x
    2x

    1x
    1x
    1x
    1x

    1x
    1x

    2x
    2x
    1x

    1x

    1x
    1x
    1x

    1x
    1x

    1x

     import { SocketsContainer } from './container';
    import { ObservableSocket } from './observable-socket';
    import { ObservableSocketServer } from './interfaces/observable-socket-server.interface';
    import { validatePath } from '@nestjs/common/utils/shared.utils';
    import { ApplicationConfig } from '@nestjs/core/application-config';

    export class SocketServerProvider {
      constructor(
        private readonly socketsContainer: SocketsContainer,
        private readonly applicationConfig: ApplicationConfig,
      ) {}

      public scanForSocketServer(
        options: any,
        port: number,
      ): ObservableSocketServer {
        const observableServer = this.socketsContainer.getServerByPort(port);
        return observableServer
          ? this.createWithNamespace(options, port, observableServer)
          : this.createSocketServer(options, port);
      }

      private createSocketServer(
        options: any,
        port: number,
      ): ObservableSocketServer {
        const { namespace, server, ...opt } = options;
        const adapter = this.applicationConfig.getIoAdapter();
        const ioServer = adapter.create(port, opt);
        const observableSocket = ObservableSocket.create(ioServer);

        this.socketsContainer.addServer(null, port, observableSocket);
        return this.createWithNamespace(options, port, observableSocket);
      }

      private createWithNamespace(
        options: any,
        port: number,
        observableSocket: ObservableSocketServer,
      ): ObservableSocketServer {
        const { namespace } = options;
        if (!namespace) {
          return observableSocket;
        }
        const namespaceServer = this.getServerOfNamespace(
          options,
          port,
          observableSocket.server,
        );
        const observableNamespaceSocket = ObservableSocket.create(namespaceServer);
        this.socketsContainer.addServer(namespace, port, observableNamespaceSocket);
        return observableNamespaceSocket;
      }

      private getServerOfNamespace(options: any, port: number, server) {
        const adapter = this.applicationConfig.getIoAdapter();
        return adapter.create(port, {
          ...options,
          namespace: this.validateNamespace(options.namespace || ''),
          server,
        });
      }

      private validateNamespace(namespace: string): string {
        return validatePath(namespace);
      }
    }