Source: src/fayeExtension.js

/**
 * 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;
  #onError;

  /**
   * 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;
  }

  /**
   * Setter for #onError
   * 
   * @param value {Function} Callback to call when subscribe comes back with an error.
   */
  set onError(value) {
    this.#onError = 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);
        }

        if (!message.successful) {
          this.#onError(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) {
            if (message.data.v1) {
              message.data.v1.returnChannel = this.#responseChannel;
            }
            else {
              message.data.response_channel = 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;