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 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 | 1× 6× 1× 1× 1× 18× 1× 1× 9× 9× 9× 9× 9× 9× 9× 1× 4× 4× 4× 4× 4× 4× 4× 4× 9× 9× 9× 81× 81× 81× 171× 171× 81× 81× 81× 81× 171× 171× 17× 171× 162× 171× 171× 171× 162× 171× 171× 171× 171× 171× 171× 171× 6× 171× 18× 171× 19× 171× 171× 171× 171× 171× 17× 17× 17× 9× 17× 17× 17× 1× 16× 16× 14× 14× 1× 1× 1× 9× 9× 1× 9× 9× 9× 18× 81× 9× 1× 1× | '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; }; }(); // initializer modules var _utils = require('../utils'); var _utils2 = _interopRequireDefault(_utils); 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"); } } /** * This class manage all actions. */ var Actions = function () { /** * Create a new actions manager instance. * * @param api */ /** * Hash map with the middleware by actions. * * @type {{}} */ /** * Hash map with the registered actions. * * @type {{}} */ function Actions(api) { _classCallCheck(this, Actions); this.api = null; this.actions = {}; this.versions = {}; this.middleware = {}; this.globalMiddleware = []; this.api = api; } /** * Execute an action. * * This allow developers call actions internally. * * @param actionName Name of the action to be called. * @param params Action parameters. * @param callback Callback function. */ /** * Global middleware. * * @type {Array} */ /** * Separate actions by version. * * @type {{}} */ /** * API reference. * * @type {null} */ _createClass(Actions, [{ key: 'call', value: function call(actionName, params, callback) { var self = this; // create a new connection object var connection = new self.api.connection(self.api, { type: 'internal', remotePort: 0, remoteIP: 0, rawConnection: {} }); // set connection params connection.params = params; // set action who must be called connection.params.action = actionName; // create a new ActionProcessor instance var actionProcessor = new self.api.actionProcessor(self.api, connection, function (data) { // execute the callback on the connection destroy event connection.destroy(function () { return callback(data.response.error, data.response); }); }); // process the action actionProcessor.processAction(); } /** * This loads some system action. * * Available action: * - status: give information about the name and the server status. */ }, { key: 'loadSystemActions', value: function loadSystemActions() { var self = this; // only load this if the system actions are enabled // // @see api.configs.enableSystemActions Eif (self.api.config.enableSystemActions !== true) { return; } // add an action to give some information about the server status self.versions.status = [1]; self.actions.status = { '1': { name: 'status', description: 'Is a system action to show the server status', run: function run(api, action, next) { // finish the action execution next(); } } }; } /** * Load a new action file. * * @param fullFilePath * @param reload */ }, { key: 'loadFile', value: function loadFile(fullFilePath) { var reload = arguments.length <= 1 || arguments[1] === undefined ? false : arguments[1]; var self = this; var loadMessage = function loadMessage(action) { Iif (reload) { self.api.log('action (re)loaded: ' + action.name + ' @ v' + action.version + ', ' + fullFilePath, 'debug'); } else { self.api.log('action loaded: ' + action.name + ' @ v' + action.version + ', ' + fullFilePath, 'debug'); } }; // watch for changes on the action file self.api.configs.watchFileAndAct(fullFilePath, function () { self.loadFile(fullFilePath, true); self.api.params.buildPostVariables(); self.api.routes.loadRoutes(); }); // try load the action try { // load action file var collection = require(fullFilePath); // iterate all collection definitions for (var i in collection) { // get action object var _action = collection[i]; // if there is no version defined set it to 1.0 if (_action.version === null || _action.version === undefined) { _action.version = 1.0; } // if the action not exists create a new entry on the hash map if (self.actions[_action.name] === null || self.actions[_action.name] === undefined) { self.actions[_action.name] = {}; } // if the action exists and are protected return now Iif (self.actions[_action.name][_action.version] !== undefined && self.actions[_action.name][_action.version].protected !== undefined && self.actions[_action.name][_action.version].protected === true) { return; } // put the action on correct version slot self.actions[_action.name][_action.version] = _action; if (self.versions[_action.name] === null || self.versions[_action.name] === undefined) { self.versions[_action.name] = []; } self.versions[_action.name].push(_action.version); self.versions[_action.name].sort(); // validate the action data self.validateAction(self.actions[_action.name][_action.version]); // send a log message loadMessage(_action); } } catch (err) { try { self.api.exceptionHandlers.loader(fullFilePath, err); delete self.actions[action.name][action.version]; } catch (err2) { throw err; } } } /** * Validate some action requirements. * * @param action Action object to be validated. */ }, { key: 'validateAction', value: function validateAction(action) { var self = this; // fail function var fail = function fail(msg) { return self.api.log(msg, 'error'); }; // initialize inputs property if (action.inputs === undefined) { action.inputs = {}; } // initialize private property if (action.private === undefined) { action.private = false; } // initialize protected property if (action.protected === undefined) { action.protected = false; } // the name, description, run properties are required Iif (typeof action.name !== 'string' || action.name.length < 1) { fail('an action is missing \'action.name\''); return false; } else Iif (typeof action.description !== 'string' || action.description.length < 1) { fail('Action ' + action.name + ' is missing \'action.description\''); return false; } else Iif (typeof action.run !== 'function') { fail('Action ' + action.run + ' has no run method'); return false; } else Iif (self.api.connections !== null && self.api.connections.allowedVerbs.indexOf(action.name) >= 0) { fail(action.run + ' is a reserved verb for connections. Choose a new name'); return false; } else { return true; } } /** * Add a new middleware. * * @param data Middleware to be added. */ }, { key: 'addMiddleware', value: function addMiddleware(data) { var self = this; // middleware require a name Iif (!data.name) { throw new Error('middleware.name is required'); } // if there is no defined priority use the default if (!data.priority) { data.priority = self.api.config.general.defaultMiddlewarePriority; } // ensure the priority is a number data.priority = Number(data.priority); // save the new middleware self.middleware[data.name] = data; // if this is a local middleware return now if (data.global !== true) { return; } // push the new middleware to the global list self.globalMiddleware.push(data.name); // sort the global middleware array self.globalMiddleware.sort(function (a, b) { Iif (self.middleware[a].priority > self.middleware[b].priority) { return 1; } return -1; }); } }]); return Actions; }(); /** * Initializer to load the actions features into the Engine. */ var _class = function () { function _class() { _classCallCheck(this, _class); this.loadPriority = 410; } /** * Initializer load priority. * * @type {number} */ _createClass(_class, [{ key: 'load', /** * Initializer load function. * * @param api API reference * @param next Callback function */ value: function load(api, next) { // add the actions class to the api api.actions = new Actions(api); // load system actions api.actions.loadSystemActions(); // iterate all modules and load all actions api.modules.modulesPaths.forEach(function (modulePath) { // get all files from the module "actions" folder _utils2.default.recursiveDirectoryGlob(modulePath + '/actions').forEach(function (actionFile) { return api.actions.loadFile(actionFile); }); }); // finish initializer loading next(); } }]); return _class; }(); exports.default = _class; //# sourceMappingURL=actions.js.map |