# All files noise-server.ts

100% Statements 37/37
100% Branches 6/6
100% Functions 12/12
100% Lines 33/33

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

    1x

    1x
    1x

    1x

    9x

    9x
    9x

    9x

    9x

    8x

    8x
    8x
    8x
    8x
    8x

    8x

    1x
    1x
    1x
    1x
    1x

    1x

    5x

    1x

    5x
    5x

    2x

    1x

    1x

     import * as crypto from '@node-dlc/crypto';
    import assert from 'assert';
    import { EventEmitter } from 'events';
    import { AddressInfo, Server, Socket } from 'net';

    import { NoiseError } from './noise-error';
    import { NoiseServerListenOptions } from './noise-server-listen-options';
    import { NoiseServerOptions } from './noise-server-options';
    import { NoiseSocket } from './noise-socket';
    import { NoiseState } from './noise-state';

    export class NoiseServer extends EventEmitter {
      /**
       * Local private key as a 32-byte private key for elliptic curve
       * secp256k1 used by the server
       */
      public ls: Buffer;

      /**
       * Function for creating the ephemeral private key used by each
       * connection
       */
      public esFactory: () => Buffer;

      /**
       * Socket server that is created
       */
      private _server: Server;

      /**
       * NoiseServer is a TCP server that wraps socket instances
       * with the NoiseSocket class. This class is an event emitter
       * with the same interface as net.Server.
       *
       * The constructor take arguments that will be used by the
       * NoiseState.
       */
      constructor(
        opts: NoiseServerOptions,
        connListener?: (socket: NoiseSocket) => void,
      ) {
        super();

        // localSecret assertions
        assert.ok(Buffer.isBuffer(opts.ls), new NoiseError('ls must be a buffer'));
        assert.ok(
          crypto.validPrivateKey(opts.ls),
          new NoiseError('ls must be valid public key'),
        );
        this.ls = opts.ls;

        // ephemeral secret factory
        assert.ok(
          !opts.esFactory || typeof opts.esFactory === 'function',
          new NoiseError('esFactory must be a function'),
        );
        this.esFactory = opts.esFactory || crypto.createPrivateKey;

        // construct and bind the server
        this._server = new Server();
        this._server.on('connection', this._onConnection.bind(this));
        this._server.on('error', (err) => this.emit('error', err));
        this._server.on('close', () => this.emit('close'));
        this._server.on('listening', () => this.emit('listening'));

        if (connListener) this.on('connection', connListener);
      }

      /**
       * Called when the socket receives a new socket connection.
       * Emits the `connection` event.
       */
      public _onConnection(socket: Socket): void {
        const ls = this.ls;
        const es = this.esFactory();
        const noiseState = new NoiseState({ ls, es });
        const noiseSocket = new NoiseSocket({ socket, noiseState });
        this.emit('connection', noiseSocket);
      }

      /**
       * Returns the address the server is listeening on. If the server is
       * not listening, it will return undefined.
       */
      public address(): string | AddressInfo {
        return this._server.address();
      }

      /**
       * Stops the server from accepting new connections and keeps existing connections open.
       * This function is asynchronous, the server is fully closed when all connections are
       * ended and the server emits a `close` event. The optional `cb` event will be called
       * once the `close` event occurs.
       */
      public close(cb?: (err?: Error) => void): void {
        this._server.close(cb);
      }

      /**
       * Asynchronously get the number of concurrent connections on the server. Works when sockets were sent to forks.
       */
      public getConnections(cb: (error: Error, count: number) => void): void {
        this._server.getConnections(cb);
      }

      /**
       * Start a server listening for connections. This method is asyncrhonous,
       * once the server has started listening, the `listening` event will be
       * emitted.
       * @param callback Called when the server is listening. Automatically binds
       * the function to the `listening` event.
       */
      public listen(opts: NoiseServerListenOptions, callback?: () => void): this {
        this._server.listen(opts, callback);
        return this;
      }

      /**
       * Indicates whether or not the server is listening for connections.
       */
      public get listening(): boolean {
        return this._server.listening;
      }

      /**
       * Set this property to reject connections when the server's connection count gets high.
       */
      public get maxConnections(): number {
        return this._server.maxConnections;
      }

      public set maxConnections(val: number) {
        this._server.maxConnections = val;
      }
    }