All files / src/protocols/abstract common.js

93.88% Statements 46/49
90.91% Branches 20/22
86.67% Functions 13/15
93.88% Lines 46/49

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      1x 1x 1x 1x       132x   132x 132x 132x 132x   132x 132x   132x 181x                 59x       54x       43x       2x       2x                                               17x 17x             3x 3x       10x 1x 1x       9x 9x   9x 6x   6x 4x   4x   4x 1x     4x     2x     9x   9x       4x               15x 15x 2x 2x 2x     15x         1x  
'use strict';
 
const
  KuzzleError = require('../../KuzzleError'),
  uuidv4 = require('../../uuidv4'),
  KuzzleEventEmitter = require('../../eventEmitter'),
  PendingRequest = require('./pendingRequest');
 
class AbstractWrapper extends KuzzleEventEmitter {
  constructor (host, options = {}) {
    super();
 
    this._pendingRequests = new Map();
    this._host = host;
    this._port = typeof options.port === 'number' ? options.port : 7512;
    this._ssl = typeof options.sslConnection === 'boolean' ? options.sslConnection : false;
 
    this.id = uuidv4();
    this.state = 'offline';
 
    Object.keys(options).forEach(opt => {
      Iif ( Object.prototype.hasOwnProperty.call(this, opt)
        && Object.getOwnPropertyDescriptor(this, opt).writable
      ) {
        this[opt] = options[opt];
      }
    });
  }
 
  get host () {
    return this._host;
  }
 
  get port () {
    return this._port;
  }
 
  get ssl () {
    return this._ssl;
  }
 
  get connected () {
    return this.state === 'connected';
  }
 
  get pendingRequests () {
    return this._pendingRequests;
  }
 
  /**
   * @abstract
   * @returns {Promise<any>}
   */
  connect () {
    throw new Error('Method "connect" is not implemented');
  }
 
  /**
   * @abstract
   * @param request
   * @returns {Promise<any>}
   */
  send () {
    throw new Error('Method "send" is not implemented');
  }
 
  /**
   * Called when the client's connection is established
   */
  clientConnected (state, wasConnected) {
    this.state = state || 'ready';
    this.emit(wasConnected && 'reconnect' || 'connect');
  }
 
  /**
   * Called when the client's connection is closed
   */
  close () {
    this.state = 'offline';
    this.clear();
  }
 
  query (request) {
    if (!this.isReady()) {
      this.emit('discarded', request);
      return Promise.reject(new Error(`Unable to execute request: not connected to a Kuzzle server.
Discarded request: ${JSON.stringify(request)}`));
    }
 
    const pending = new PendingRequest(request);
    this._pendingRequests.set(request.requestId, pending);
 
    this.once(request.requestId, response => {
      this._pendingRequests.delete(request.requestId);
 
      if (response.error) {
        const error = new KuzzleError(response.error);
 
        this.emit('queryError', error, request);
 
        if (request.action !== 'logout' && error.message === 'Token expired') {
          this.emit('tokenExpired');
        }
 
        return pending.reject(error);
      }
 
      pending.resolve(response);
    });
 
    this.send(request);
 
    return pending.promise;
  }
 
  isReady () {
    return this.state === 'ready';
  }
 
  /**
   * Clear pendings requests.
   * Emits an event for each discarded pending request.
   */
  clear () {
    const rejectedError = new Error('Network error: request was sent but no response has been received');
    for (const pending of this._pendingRequests.values()) {
      pending.reject(rejectedError);
      this.removeAllListeners(pending.request.requestId);
      this.emit('discarded', pending.request);
    }
 
    this._pendingRequests.clear();
  }
 
}
 
module.exports = AbstractWrapper;