UNPKG

kindergarten

Version:

Kindergarten is a JavaScript library which helps programmers to achieve modular security using sandbox pattern

87 lines (63 loc) 3.83 kB
'use strict'; exports.__esModule = true; exports.default = undefined; 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; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); var _HeadGoverness2 = require('./HeadGoverness'); var _HeadGoverness3 = _interopRequireDefault(_HeadGoverness2); var _errors = require('../errors'); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _possibleConstructorReturn(self, call) { if (!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) { if (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 } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } /** * A very strict governess! She forces all exposed methods from the sandbox or * perimeter to be governed. This means that all exposed methods must call * `guard()` method. * * Use this governess if you, or you colleagues forget to call `guard()` method * in you exposed methods. * * Note: Strict Governess does not prevent the exposed method to be executed! * Actually it executes that method and throw an error if that method did not * called the `guard()` */ var StrictGoverness = function (_HeadGoverness) { _inherits(StrictGoverness, _HeadGoverness); function StrictGoverness() { var _ref; _classCallCheck(this, StrictGoverness); for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } var _this = _possibleConstructorReturn(this, (_ref = StrictGoverness.__proto__ || Object.getPrototypeOf(StrictGoverness)).call.apply(_ref, [this].concat(args))); _this._guardCount = 0; _this._governedCount = 0; return _this; } _createClass(StrictGoverness, [{ key: 'governed', value: function governed() { for (var _len2 = arguments.length, args = Array(_len2), _key2 = 0; _key2 < _len2; _key2++) { args[_key2] = arguments[_key2]; } var returnVal = _HeadGoverness3.default.prototype.governed.apply(this, args); if (++this._governedCount > this._guardCount && !this.unguarded) { throw new _errors.AccessDenied('All exposed methods must call guard method.'); } // TODO: test for return value return returnVal; } }, { key: 'guard', value: function guard() { ++this._guardCount; // TODO: test for return value for (var _len3 = arguments.length, args = Array(_len3), _key3 = 0; _key3 < _len3; _key3++) { args[_key3] = arguments[_key3]; } return _HeadGoverness3.default.prototype.guard.apply(this, args); } }]); return StrictGoverness; }(_HeadGoverness3.default); exports.default = StrictGoverness;