All files fayeExtension.js

100% Statements 177/177
100% Branches 29/29
100% Functions 11/11
100% Lines 177/177

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 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 172 173 174 175 176 177 1781x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 33x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 33x 33x 33x 33x 33x 33x 33x 33x 33x 1x 1x 1x 1x 1x 1x 1x 5x 5x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 9x 9x 1x 1x 1x 1x 1x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 8x 8x 1x 1x 1x 1x 1x 1x 1x 1x 19x 19x 6x 5x 5x 6x 6x 2x 2x 2x 2x 2x 1x 1x 2x 6x 6x 1x 1x 6x 6x 1x 1x 6x 6x 6x 19x 5x 5x 5x 1x 1x 3x 5x 1x 1x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 5x 1x 1x 1x 1x 1x 1x 2x 5x 2x 2x 2x 2x 2x 5x 1x 1x 3x 3x 3x 19x 19x 1x 1x 1x  
/**
 * Faye client extension implementation.
 *
 * Implements extension for faye client to sign and send and
 * receive messages with IdealNet
 *
 * @author michael whobrey <mwhobrey@idealss.com>
 */
 
const crypto = require('crypto');
const { signRequest, base64Encode, generateSalt, log } = require('./utils');
const { constants } = require('./constants');
 
/**
 * Extension class to neatly wrap up faye client extensions.
 *
 * Keeps up with all vars needed to properly handle incoming and outgoing extension
 * operations.
 */
class Extension {
  #api;
  #group;
  #siteId;
  #token;
  #key;
  #clientId;
  #responseChannel;
  #isSubscribed;
  #doSubscribe;
  #onSubscribed;
 
  /**
   * Constructor for Extension.
   */
  constructor(_api, _group, _siteId, _token, _key) {
    this.#api = _api;
    this.#group = _group;
    this.#siteId = _siteId;
    this.#token = _token;
    this.#key = _key;
    this.#clientId = '';
    this.#responseChannel = '';
    this.#isSubscribed = false;
  }
 
  /**
   * Getter for #responseChannel property.
   *
   * @returns {string} The response channel
   */
  get responseChannel() {
    return this.#responseChannel;
  }
 
  /**
   * Getter for #doSubscribe.
   */
  get doSubscribe() {
    return this.#doSubscribe;
  }
 
  /**
   * Setter for #doSubscribe.
   *
   * @param value {function} Function to call for subscribing.
   */
  set doSubscribe(value) {
    this.#doSubscribe = value;
  }
 
  /**
   * Getter for #isSubscribed.
   */
  get isSubscribed() {
    return this.#isSubscribed;
  }
 
  /**
   * Setter for #isSubscribed.
   *
   * @param value {boolean} Whether or not the client subscribed.
   */
  set isSubscribed(value) {
    this.#isSubscribed = value;
  }
 
  /**
   * Setter for #onSubscribed
   *
   * @param value {Function} Callback to call when subscribe comes back successfully.
   */
  set onSubscribed(value) {
    this.#onSubscribed = value;
  }
 
  /**
   * Extension method to add on functionality to
   * standard faye client.
   *
   * @param messageBack {Function} Function to return results to the caller.
   */
  extend = (messageBack) => {
    return {
      incoming: (message, callback) => {
        if (message.channel !== constants.messages.connect) {
          log("Incoming pubsub message:", message);
        }
 
        if ((message.channel === constants.messages.handshake) && (message.successful)) {
          let obj = JSON.parse(JSON.stringify(message));
          this.#clientId = obj.clientId;
          this.#responseChannel = "/" + this.#api + "/" + this.#group + "/" + this.#clientId + "/response";
 
          if ((!this.#isSubscribed) && (this.#doSubscribe)) {
            this.#doSubscribe(this.#responseChannel);
          }
        }
 
        if ((message.channel === constants.messages.subscribe) && (message.successful)) {
          this.#onSubscribed();
        }
 
        if (message.channel === this.#responseChannel) {
          messageBack(message);
        }
 
        return callback(message);
      },
      outgoing: (message, callback) => {
        try {
          if ((message.channel === constants.messages.handshake) ||
            (message.channel === constants.messages.connect)){
            return callback(message);
          }
 
          if ((message.data) && (message.data.v1)) {
            message.data.v1.returnChannel = this.#responseChannel;
          }
 
          const jsonString = JSON.stringify(message);
          const json = message;
          const salt = generateSalt();
 
          message.ext = {
            "api": this.#api,
            "token": this.#token,
            "salt": base64Encode(salt),
            "signature": signRequest(jsonString, this.#key, salt),
            "message": '',
            "data": base64Encode(jsonString)
          };
 
          if (message.channel === constants.messages.subscribe) {
            message.ext.message = constants.messages.subscribe;
 
            log("Outgoing subscribe message: ", message);
 
            return callback(message)
          }
 
          if (json.data !== 'undefined' && json.data != null) {
            message.ext.message = json.data.message;
            message.data = json.data;
          }
 
          log("Outgoing pubsub message: ", message);
        } catch (err) {
          log("Error during outgoing message: ", err.message);
        }
 
        callback(message);
      }
    }
  };
}
 
module.exports = Extension;