UNPKG

secure-event-emitter

Version:

SecureEventEmitter is a tiny javascript package that uses restrict rules and mechanisms to build safer and protected event-driven architecture. It's similar to nodejs EventEmitter, but dictates stricter rules to prevent misuse.

91 lines (72 loc) 4.21 kB
"use strict"; function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); } function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); } function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); } function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; } var _require = require('./utils'), isSymbol = _require.isSymbol, isArray = _require.isArray, isUndefined = _require.isUndefined, isFunction = _require.isFunction, isString = _require.isString; var error_if_invalid_emitterKey = function error_if_invalid_emitterKey(emitterKey) { if (!isSymbol(emitterKey) && !isString(emitterKey)) { throw new TypeError('SecureEventEmitter emitterKey must be a Symbol or String type'); } }; var error_if_unlock_failed = function error_if_unlock_failed(unknownEmitterKey, rightEmitterKey) { if (unknownEmitterKey !== rightEmitterKey) { throw new TypeError("Unlock Failed - use the ".concat(String(rightEmitterKey), " emitterKey to unlock emitter, if dont have access the ").concat(String(rightEmitterKey), " emitterKey, which means that event emitting from there is not allowed")); } }; var error_if_invalid_eventTypes = function error_if_invalid_eventTypes(eventTypes) { if (!isArray(eventTypes)) { throw new TypeError('SecureEventEmitter eventTypes must be an array'); } if (!eventTypes.length) { throw new TypeError('SecureEventEmitter eventTypes must be a not empty array'); } for (var i = 0; i < eventTypes.length; ++i) { error_if_invalid_eventType(eventTypes[i]); } }; var error_if_invalid_eventType = function error_if_invalid_eventType(eventType) { if (!isString(eventType)) { throw new TypeError("eventType must be a string. \n\n Invalid eventType:\n ".concat(eventType, "\n\n")); } }; var error_if_eventType_not_exist = function error_if_eventType_not_exist(eventType, eventTypes) { if (!eventTypes.includes(eventType)) { throw new TypeError("eventType: ".concat(eventType, " not exist - ").concat(eventTypes)); } }; var error_if_invalid_validate = function error_if_invalid_validate(validatorFunction) { if (!isUndefined(validatorFunction) && !isFunction(validatorFunction)) { throw new TypeError('SecureEventEmitter validatorFunction must be a function'); } }; var error_if_validation_failed = function error_if_validation_failed(validatorFunction, args) { if (validatorFunction) { var errorMessage = validatorFunction.apply(void 0, _toConsumableArray(args)); if (errorMessage && isString(errorMessage)) { throw new TypeError(errorMessage); } } }; var error_if_invalid_listener = function error_if_invalid_listener(listener) { if (!isFunction(listener)) { throw new TypeError("listener must be a function: \n".concat(listener, "\n")); } }; module.exports = { error_if_invalid_emitterKey: error_if_invalid_emitterKey, error_if_unlock_failed: error_if_unlock_failed, error_if_invalid_eventType: error_if_invalid_eventType, error_if_invalid_eventTypes: error_if_invalid_eventTypes, error_if_eventType_not_exist: error_if_eventType_not_exist, error_if_invalid_validate: error_if_invalid_validate, error_if_validation_failed: error_if_validation_failed, error_if_invalid_listener: error_if_invalid_listener };