all files / dist/satellites/ helpers.js

93.86% Statements 107/114
68.63% Branches 35/51
96.55% Functions 28/29
96.63% Lines 86/89
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 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282          10×             27×             30× 30×     30× 30×         30×           30× 17×     30× 73×     30× 29×                               59× 59× 59×   59× 29× 29×                                                           30× 30×   30×             30×                                                       30× 30×   30× 13× 13×     30×     30× 30×   30×   30× 30× 30×     30× 30×                                                                                                                                                        
'use strict';
 
Object.defineProperty(exports, "__esModule", {
  value: true
});
 
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; Eif ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { Eif (protoProps) defineProperties(Constructor.prototype, protoProps); Iif (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
 
var _nodeUuid = require('node-uuid');
 
var _nodeUuid2 = _interopRequireDefault(_nodeUuid);
 
var _genericServer = require('../genericServer');
 
var _genericServer2 = _interopRequireDefault(_genericServer);
 
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
 
function _classCallCheck(instance, Constructor) { Iif (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
 
function _possibleConstructorReturn(self, call) { Iif (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
 
function _inherits(subClass, superClass) { Iif (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); Eif (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
 
var TestServer = function (_GenericServer) {
  _inherits(TestServer, _GenericServer);
 
  function TestServer(api, type, options, attributes) {
    _classCallCheck(this, TestServer);
 
    var _this = _possibleConstructorReturn(this, Object.getPrototypeOf(TestServer).call(this, api, type, options, attributes));
 
    _this.on('connection', function (connection) {
      connection.messages = [];
      connection.actionCallbacks = {};
    });
 
    _this.on('actionComplete', function (data) {
      data.response.messageCount = data.messageCount;
      data.response.serverInformation = {
        serverName: api.config.general.serverName,
        apiVersion: api.config.general.apiVersion
      };
 
      data.response.requesterInformation = {
        id: data.connection.id,
        remoteIP: data.connection.remoteIP,
        receivedParams: {}
      };
 
      if (data.response.error) {
        data.response.error = api.config.errors.serializers.servers.helper(data.response.error);
      }
 
      for (var k in data.params) {
        data.response.requesterInformation.receivedParams[k] = data.params[k];
      }
 
      if (data.toRender === true) {
        _this.sendMessage(data.connection, data.response, data.messageCount);
      }
    });
    return _this;
  }
 
  _createClass(TestServer, [{
    key: 'start',
    value: function start(next) {
      this.api.log('loading the testServer', 'warning');
      next();
    }
  }, {
    key: 'stop',
    value: function stop(next) {
      next();
    }
  }, {
    key: 'sendMessage',
    value: function sendMessage(connection, message, messageCount) {
      process.nextTick(function () {
        message.messageCount = messageCount;
        connection.messages.push(message);
 
        if (typeof connection.actionCallbacks[messageCount] === 'function') {
          connection.actionCallbacks[messageCount](message, connection);
          delete connection.actionCallbacks[messageCount];
        }
      });
    }
  }, {
    key: 'goodbye',
    value: function goodbye() {}
  }]);
 
  return TestServer;
}(_genericServer2.default);
 
var Helpers = function () {
 
  /**
   * Create a new instance of Helpers class.
   *
   * @param api
   */
 
  function Helpers(api) {
    _classCallCheck(this, Helpers);
 
    this.api = null;
    this.api = api;
  }
 
  /**
   * API reference object.
   *
   * @type {null}
   */
 
 
  _createClass(Helpers, [{
    key: 'connection',
    value: function connection() {
      var self = this;
      var id = _nodeUuid2.default.v4();
 
      self.api.servers.servers.testServer.buildConnection({
        id: id,
        rawConnection: {},
        remoteAddress: 'testServer',
        remotePort: 0
      });
 
      return self.api.connections.connections[id];
    }
  }, {
    key: 'initialize',
    value: function initialize(api, options, next) {
      var type = 'testServer';
      var attributes = {
        canChat: true,
        logConnections: false,
        logExits: false,
        sendWelcomeMessage: true,
        verbs: api.connections.allowedVerbs
      };
 
      next(new TestServer(api, type, options, attributes));
    }
 
    /**
     * Run an action.
     *
     * This creates a fake connection to process the action
     * and return the result on the callback function.
     *
     * @param actionName  Action to be executed.
     * @param input       Action parameters.
     * @param next        Callback function.
     */
 
  }, {
    key: 'runAction',
    value: function runAction(actionName, input, next) {
      var self = this;
      var connection = void 0;
 
      if (typeof input === 'function' && !next) {
        next = input;
        input = {};
      }
 
      Iif (input.id && input.type === 'testServer') {
        connection = input;
      } else {
        connection = self.connection();
        connection.params = input;
      }
      connection.params.action = actionName;
 
      connection.messageCount++;
      Eif (typeof next === 'function') {
        connection.actionCallbacks[connection.messageCount] = next;
      }
 
      process.nextTick(function () {
        self.api.servers.servers.testServer.processAction(connection);
      });
    }
 
    /**
     * Execute a task.
     *
     * @param taskName  Task to be executed.
     * @param params    Task parameters.
     * @param next      Callback function.
     */
 
  }, {
    key: 'runTask',
    value: function runTask(taskName, params, next) {
      var self = this;
      self.api.tasks.tasks[taskName].run(self.api, params, next);
    }
  }]);
 
  return Helpers;
}();
 
var _class = function () {
  function _class() {
    _classCallCheck(this, _class);
 
    this.loadPriority = 800;
    this.startPriority = 800;
  }
 
  /**
   * Satellite load priority.
   *
   * @type {number}
   */
 
 
  /**
   * Satellite start priority.
   *
   * @type {number}
   */
 
 
  _createClass(_class, [{
    key: 'load',
 
 
    /**
     * Satellite loading function.
     *
     * @param api   API object reference.
     * @param next  Callback function.
     */
    value: function load(api, next) {
      Eif (api.env === 'test') {
        // put the helpers available to all platform
        api.helpers = new Helpers(api);
      }
 
      // finish the satellite load
      next();
    }
 
    /**
     * Satellite starting function.
     *
     * @param api   API object reference.
     * @param next  Callback function.
     */
 
  }, {
    key: 'start',
    value: function start(api, next) {
      Eif (api.env === 'test') {
        new api.helpers.initialize(api, {}, function (serverObject) {
          api.servers.servers.testServer = serverObject;
          api.servers.servers.testServer.start(function () {
            return next();
          });
        });
 
        return;
      }
 
      // finish the satellite start
      next();
    }
  }]);
 
  return _class;
}();
 
exports.default = _class;
//# sourceMappingURL=helpers.js.map