Check.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. var _Utils = _interopRequireDefault(require("../Utils"));
  7. var _lodash = require("lodash");
  8. function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
  9. /**
  10. * @module SecurityCheck
  11. */
  12. /**
  13. * A security check.
  14. * @class
  15. */
  16. class Check {
  17. /**
  18. * Constructs a new security check.
  19. * @param {Object} params The parameters.
  20. * @param {String} params.title The title.
  21. * @param {String} params.warning The warning message if the check fails.
  22. * @param {String} params.solution The solution to fix the check.
  23. * @param {Promise} params.check The check as synchronous or asynchronous function.
  24. */
  25. constructor(params) {
  26. this._validateParams(params);
  27. const {
  28. title,
  29. warning,
  30. solution,
  31. check
  32. } = params;
  33. this.title = title;
  34. this.warning = warning;
  35. this.solution = solution;
  36. this.check = check;
  37. // Set default properties
  38. this._checkState = CheckState.none;
  39. this.error;
  40. }
  41. /**
  42. * Returns the current check state.
  43. * @return {CheckState} The check state.
  44. */
  45. checkState() {
  46. return this._checkState;
  47. }
  48. async run() {
  49. // Get check as synchronous or asynchronous function
  50. const check = this.check instanceof Promise ? await this.check : this.check;
  51. // Run check
  52. try {
  53. check();
  54. this._checkState = CheckState.success;
  55. } catch (e) {
  56. this.stateFailError = e;
  57. this._checkState = CheckState.fail;
  58. }
  59. }
  60. /**
  61. * Validates the constructor parameters.
  62. * @param {Object} params The parameters to validate.
  63. */
  64. _validateParams(params) {
  65. _Utils.default.validateParams(params, {
  66. group: {
  67. t: 'string',
  68. v: _lodash.isString
  69. },
  70. title: {
  71. t: 'string',
  72. v: _lodash.isString
  73. },
  74. warning: {
  75. t: 'string',
  76. v: _lodash.isString
  77. },
  78. solution: {
  79. t: 'string',
  80. v: _lodash.isString
  81. },
  82. check: {
  83. t: 'function',
  84. v: _lodash.isFunction
  85. }
  86. });
  87. }
  88. }
  89. /**
  90. * The check state.
  91. */
  92. const CheckState = Object.freeze({
  93. none: 'none',
  94. fail: 'fail',
  95. success: 'success'
  96. });
  97. var _default = exports.default = Check;
  98. module.exports = {
  99. Check,
  100. CheckState
  101. };
  102. //# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJuYW1lcyI6WyJfVXRpbHMiLCJfaW50ZXJvcFJlcXVpcmVEZWZhdWx0IiwicmVxdWlyZSIsIl9sb2Rhc2giLCJlIiwiX19lc01vZHVsZSIsImRlZmF1bHQiLCJDaGVjayIsImNvbnN0cnVjdG9yIiwicGFyYW1zIiwiX3ZhbGlkYXRlUGFyYW1zIiwidGl0bGUiLCJ3YXJuaW5nIiwic29sdXRpb24iLCJjaGVjayIsIl9jaGVja1N0YXRlIiwiQ2hlY2tTdGF0ZSIsIm5vbmUiLCJlcnJvciIsImNoZWNrU3RhdGUiLCJydW4iLCJQcm9taXNlIiwic3VjY2VzcyIsInN0YXRlRmFpbEVycm9yIiwiZmFpbCIsIlV0aWxzIiwidmFsaWRhdGVQYXJhbXMiLCJncm91cCIsInQiLCJ2IiwiaXNTdHJpbmciLCJpc0Z1bmN0aW9uIiwiT2JqZWN0IiwiZnJlZXplIiwiX2RlZmF1bHQiLCJleHBvcnRzIiwibW9kdWxlIl0sInNvdXJjZXMiOlsiLi4vLi4vc3JjL1NlY3VyaXR5L0NoZWNrLmpzIl0sInNvdXJjZXNDb250ZW50IjpbIi8qKlxuICogQG1vZHVsZSBTZWN1cml0eUNoZWNrXG4gKi9cblxuaW1wb3J0IFV0aWxzIGZyb20gJy4uL1V0aWxzJztcbmltcG9ydCB7IGlzRnVuY3Rpb24sIGlzU3RyaW5nIH0gZnJvbSAnbG9kYXNoJztcblxuLyoqXG4gKiBBIHNlY3VyaXR5IGNoZWNrLlxuICogQGNsYXNzXG4gKi9cbmNsYXNzIENoZWNrIHtcbiAgLyoqXG4gICAqIENvbnN0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