@kenji71089/evaluation 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (209) hide show
  1. package/.release-please-manifest.json +3 -0
  2. package/README.md +9 -0
  3. package/eslint.config.mjs +27 -0
  4. package/lib/clauseEvaluator.d.ts +22 -0
  5. package/lib/clauseEvaluator.js +299 -0
  6. package/lib/clauseEvaluator.mjs +214 -0
  7. package/lib/dependencyEvaluator.d.ts +6 -0
  8. package/lib/dependencyEvaluator.js +46 -0
  9. package/lib/dependencyEvaluator.mjs +19 -0
  10. package/lib/errors.d.ts +9 -0
  11. package/lib/errors.js +15 -0
  12. package/lib/errors.mjs +12 -0
  13. package/lib/evaluation.d.ts +25 -0
  14. package/lib/evaluation.js +503 -0
  15. package/lib/evaluation.mjs +328 -0
  16. package/lib/google/api/annotations_pb.d.ts +8 -0
  17. package/lib/google/api/annotations_pb.js +40 -0
  18. package/lib/google/api/annotations_pb.mjs +54 -0
  19. package/lib/google/api/annotations_pb_service.d.ts +3 -0
  20. package/lib/google/api/annotations_pb_service.js +3 -0
  21. package/lib/google/api/annotations_pb_service.mjs +3 -0
  22. package/lib/google/api/http_pb.d.ts +132 -0
  23. package/lib/google/api/http_pb.js +860 -0
  24. package/lib/google/api/http_pb.mjs +982 -0
  25. package/lib/google/api/http_pb_service.d.ts +3 -0
  26. package/lib/google/api/http_pb_service.js +3 -0
  27. package/lib/google/api/http_pb_service.mjs +3 -0
  28. package/lib/google/rpc/code_pb.d.ts +26 -0
  29. package/lib/google/rpc/code_pb.js +44 -0
  30. package/lib/google/rpc/code_pb.mjs +48 -0
  31. package/lib/google/rpc/code_pb_service.d.ts +3 -0
  32. package/lib/google/rpc/code_pb_service.js +3 -0
  33. package/lib/google/rpc/code_pb_service.mjs +3 -0
  34. package/lib/google/rpc/error_details_pb.d.ts +322 -0
  35. package/lib/google/rpc/error_details_pb.js +2220 -0
  36. package/lib/google/rpc/error_details_pb.mjs +2499 -0
  37. package/lib/google/rpc/error_details_pb_service.d.ts +3 -0
  38. package/lib/google/rpc/error_details_pb_service.js +3 -0
  39. package/lib/google/rpc/error_details_pb_service.mjs +3 -0
  40. package/lib/google/rpc/status_pb.d.ts +36 -0
  41. package/lib/google/rpc/status_pb.js +235 -0
  42. package/lib/google/rpc/status_pb.mjs +268 -0
  43. package/lib/google/rpc/status_pb_service.d.ts +3 -0
  44. package/lib/google/rpc/status_pb_service.js +3 -0
  45. package/lib/google/rpc/status_pb_service.mjs +3 -0
  46. package/lib/index.d.ts +20 -0
  47. package/lib/index.js +199 -0
  48. package/lib/index.mjs +46 -0
  49. package/lib/modelFactory.d.ts +64 -0
  50. package/lib/modelFactory.js +206 -0
  51. package/lib/modelFactory.mjs +176 -0
  52. package/lib/proto/event/client/event_pb.d.ts +761 -0
  53. package/lib/proto/event/client/event_pb.js +5195 -0
  54. package/lib/proto/event/client/event_pb.mjs +5865 -0
  55. package/lib/proto/event/client/event_pb_service.d.ts +3 -0
  56. package/lib/proto/event/client/event_pb_service.js +3 -0
  57. package/lib/proto/event/client/event_pb_service.mjs +3 -0
  58. package/lib/proto/event/domain/event_pb.d.ts +4518 -0
  59. package/lib/proto/event/domain/event_pb.js +10834 -0
  60. package/lib/proto/event/domain/event_pb.mjs +33315 -0
  61. package/lib/proto/event/domain/event_pb_service.d.ts +3 -0
  62. package/lib/proto/event/domain/event_pb_service.js +3 -0
  63. package/lib/proto/event/domain/event_pb_service.mjs +3 -0
  64. package/lib/proto/event/domain/localized_message_pb.d.ts +29 -0
  65. package/lib/proto/event/domain/localized_message_pb.js +183 -0
  66. package/lib/proto/event/domain/localized_message_pb.mjs +206 -0
  67. package/lib/proto/event/domain/localized_message_pb_service.d.ts +3 -0
  68. package/lib/proto/event/domain/localized_message_pb_service.js +3 -0
  69. package/lib/proto/event/domain/localized_message_pb_service.mjs +3 -0
  70. package/lib/proto/event/service/feature_pb.d.ts +44 -0
  71. package/lib/proto/event/service/feature_pb.js +277 -0
  72. package/lib/proto/event/service/feature_pb.mjs +319 -0
  73. package/lib/proto/event/service/feature_pb_service.d.ts +3 -0
  74. package/lib/proto/event/service/feature_pb_service.js +3 -0
  75. package/lib/proto/event/service/feature_pb_service.mjs +3 -0
  76. package/lib/proto/event/service/segment_pb.d.ts +51 -0
  77. package/lib/proto/event/service/segment_pb.js +324 -0
  78. package/lib/proto/event/service/segment_pb.mjs +375 -0
  79. package/lib/proto/event/service/segment_pb_service.d.ts +3 -0
  80. package/lib/proto/event/service/segment_pb_service.js +3 -0
  81. package/lib/proto/event/service/segment_pb_service.mjs +3 -0
  82. package/lib/proto/event/service/user_pb.d.ts +49 -0
  83. package/lib/proto/event/service/user_pb.js +315 -0
  84. package/lib/proto/event/service/user_pb.mjs +362 -0
  85. package/lib/proto/event/service/user_pb_service.d.ts +3 -0
  86. package/lib/proto/event/service/user_pb_service.js +3 -0
  87. package/lib/proto/event/service/user_pb_service.mjs +3 -0
  88. package/lib/proto/feature/clause_pb.d.ts +57 -0
  89. package/lib/proto/feature/clause_pb.js +277 -0
  90. package/lib/proto/feature/clause_pb.mjs +312 -0
  91. package/lib/proto/feature/clause_pb_service.d.ts +3 -0
  92. package/lib/proto/feature/clause_pb_service.js +3 -0
  93. package/lib/proto/feature/clause_pb_service.mjs +3 -0
  94. package/lib/proto/feature/command_pb.d.ts +1213 -0
  95. package/lib/proto/feature/command_pb.js +8260 -0
  96. package/lib/proto/feature/command_pb.mjs +9275 -0
  97. package/lib/proto/feature/command_pb_service.d.ts +3 -0
  98. package/lib/proto/feature/command_pb_service.js +3 -0
  99. package/lib/proto/feature/command_pb_service.mjs +3 -0
  100. package/lib/proto/feature/evaluation_pb.d.ts +111 -0
  101. package/lib/proto/feature/evaluation_pb.js +685 -0
  102. package/lib/proto/feature/evaluation_pb.mjs +793 -0
  103. package/lib/proto/feature/evaluation_pb_service.d.ts +3 -0
  104. package/lib/proto/feature/evaluation_pb_service.js +3 -0
  105. package/lib/proto/feature/evaluation_pb_service.mjs +3 -0
  106. package/lib/proto/feature/feature_last_used_info_pb.d.ts +45 -0
  107. package/lib/proto/feature/feature_last_used_info_pb.js +283 -0
  108. package/lib/proto/feature/feature_last_used_info_pb.mjs +326 -0
  109. package/lib/proto/feature/feature_last_used_info_pb_service.d.ts +3 -0
  110. package/lib/proto/feature/feature_last_used_info_pb_service.js +3 -0
  111. package/lib/proto/feature/feature_last_used_info_pb_service.mjs +3 -0
  112. package/lib/proto/feature/feature_pb.d.ts +192 -0
  113. package/lib/proto/feature/feature_pb.js +1210 -0
  114. package/lib/proto/feature/feature_pb.mjs +1413 -0
  115. package/lib/proto/feature/feature_pb_service.d.ts +3 -0
  116. package/lib/proto/feature/feature_pb_service.js +3 -0
  117. package/lib/proto/feature/feature_pb_service.mjs +3 -0
  118. package/lib/proto/feature/flag_trigger_pb.d.ts +84 -0
  119. package/lib/proto/feature/flag_trigger_pb.js +452 -0
  120. package/lib/proto/feature/flag_trigger_pb.mjs +525 -0
  121. package/lib/proto/feature/flag_trigger_pb_service.d.ts +3 -0
  122. package/lib/proto/feature/flag_trigger_pb_service.js +3 -0
  123. package/lib/proto/feature/flag_trigger_pb_service.mjs +3 -0
  124. package/lib/proto/feature/prerequisite_pb.d.ts +29 -0
  125. package/lib/proto/feature/prerequisite_pb.js +183 -0
  126. package/lib/proto/feature/prerequisite_pb.mjs +206 -0
  127. package/lib/proto/feature/prerequisite_pb_service.d.ts +3 -0
  128. package/lib/proto/feature/prerequisite_pb_service.js +3 -0
  129. package/lib/proto/feature/prerequisite_pb_service.mjs +3 -0
  130. package/lib/proto/feature/reason_pb.d.ts +40 -0
  131. package/lib/proto/feature/reason_pb.js +196 -0
  132. package/lib/proto/feature/reason_pb.mjs +219 -0
  133. package/lib/proto/feature/reason_pb_service.d.ts +3 -0
  134. package/lib/proto/feature/reason_pb_service.js +3 -0
  135. package/lib/proto/feature/reason_pb_service.mjs +3 -0
  136. package/lib/proto/feature/rule_pb.d.ts +39 -0
  137. package/lib/proto/feature/rule_pb.js +254 -0
  138. package/lib/proto/feature/rule_pb.mjs +291 -0
  139. package/lib/proto/feature/rule_pb_service.d.ts +3 -0
  140. package/lib/proto/feature/rule_pb_service.js +3 -0
  141. package/lib/proto/feature/rule_pb_service.mjs +3 -0
  142. package/lib/proto/feature/segment_pb.d.ts +161 -0
  143. package/lib/proto/feature/segment_pb.js +974 -0
  144. package/lib/proto/feature/segment_pb.mjs +1127 -0
  145. package/lib/proto/feature/segment_pb_service.d.ts +3 -0
  146. package/lib/proto/feature/segment_pb_service.js +3 -0
  147. package/lib/proto/feature/segment_pb_service.mjs +3 -0
  148. package/lib/proto/feature/service_pb.d.ts +2158 -0
  149. package/lib/proto/feature/service_pb.js +5363 -0
  150. package/lib/proto/feature/service_pb.mjs +16348 -0
  151. package/lib/proto/feature/service_pb_service.d.ts +747 -0
  152. package/lib/proto/feature/service_pb_service.js +1424 -0
  153. package/lib/proto/feature/service_pb_service.mjs +1501 -0
  154. package/lib/proto/feature/strategy_pb.d.ts +110 -0
  155. package/lib/proto/feature/strategy_pb.js +712 -0
  156. package/lib/proto/feature/strategy_pb.mjs +803 -0
  157. package/lib/proto/feature/strategy_pb_service.d.ts +3 -0
  158. package/lib/proto/feature/strategy_pb_service.js +3 -0
  159. package/lib/proto/feature/strategy_pb_service.mjs +3 -0
  160. package/lib/proto/feature/target_pb.d.ts +31 -0
  161. package/lib/proto/feature/target_pb.js +207 -0
  162. package/lib/proto/feature/target_pb.mjs +232 -0
  163. package/lib/proto/feature/target_pb_service.d.ts +3 -0
  164. package/lib/proto/feature/target_pb_service.js +3 -0
  165. package/lib/proto/feature/target_pb_service.mjs +3 -0
  166. package/lib/proto/feature/variation_pb.d.ts +37 -0
  167. package/lib/proto/feature/variation_pb.js +233 -0
  168. package/lib/proto/feature/variation_pb.mjs +266 -0
  169. package/lib/proto/feature/variation_pb_service.d.ts +3 -0
  170. package/lib/proto/feature/variation_pb_service.js +3 -0
  171. package/lib/proto/feature/variation_pb_service.mjs +3 -0
  172. package/lib/proto/gateway/service_pb.d.ts +772 -0
  173. package/lib/proto/gateway/service_pb.js +5249 -0
  174. package/lib/proto/gateway/service_pb.mjs +6001 -0
  175. package/lib/proto/gateway/service_pb_service.d.ts +253 -0
  176. package/lib/proto/gateway/service_pb_service.js +436 -0
  177. package/lib/proto/gateway/service_pb_service.mjs +461 -0
  178. package/lib/proto/user/user_pb.d.ts +58 -0
  179. package/lib/proto/user/user_pb.js +410 -0
  180. package/lib/proto/user/user_pb.mjs +460 -0
  181. package/lib/proto/user/user_pb_service.d.ts +3 -0
  182. package/lib/proto/user/user_pb_service.js +3 -0
  183. package/lib/proto/user/user_pb_service.mjs +3 -0
  184. package/lib/protoc-gen-openapiv2/options/annotations_pb.d.ts +16 -0
  185. package/lib/protoc-gen-openapiv2/options/annotations_pb.js +100 -0
  186. package/lib/protoc-gen-openapiv2/options/annotations_pb.mjs +158 -0
  187. package/lib/protoc-gen-openapiv2/options/annotations_pb_service.d.ts +3 -0
  188. package/lib/protoc-gen-openapiv2/options/annotations_pb_service.js +3 -0
  189. package/lib/protoc-gen-openapiv2/options/annotations_pb_service.mjs +3 -0
  190. package/lib/protoc-gen-openapiv2/options/openapiv2_pb.d.ts +834 -0
  191. package/lib/protoc-gen-openapiv2/options/openapiv2_pb.js +5456 -0
  192. package/lib/protoc-gen-openapiv2/options/openapiv2_pb.mjs +6256 -0
  193. package/lib/protoc-gen-openapiv2/options/openapiv2_pb_service.d.ts +3 -0
  194. package/lib/protoc-gen-openapiv2/options/openapiv2_pb_service.js +3 -0
  195. package/lib/protoc-gen-openapiv2/options/openapiv2_pb_service.mjs +3 -0
  196. package/lib/ruleEvaluator.d.ts +13 -0
  197. package/lib/ruleEvaluator.js +80 -0
  198. package/lib/ruleEvaluator.mjs +41 -0
  199. package/lib/segmentEvaluator.d.ts +7 -0
  200. package/lib/segmentEvaluator.js +74 -0
  201. package/lib/segmentEvaluator.mjs +34 -0
  202. package/lib/strategyEvaluator.d.ts +10 -0
  203. package/lib/strategyEvaluator.js +135 -0
  204. package/lib/strategyEvaluator.mjs +83 -0
  205. package/lib/userEvaluation.d.ts +8 -0
  206. package/lib/userEvaluation.js +70 -0
  207. package/lib/userEvaluation.mjs +60 -0
  208. package/package.json +46 -0
  209. package/release-please-config.json +22 -0
package/lib/errors.mjs ADDED
@@ -0,0 +1,12 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.EVALUATOR_ERRORS = void 0;
4
+ const EVALUATOR_ERRORS = {
5
+ StrategyNotFound: new Error('evaluator: strategy not found'),
6
+ DefaultStrategyNotFound: new Error('evaluator: default strategy not found'),
7
+ FeatureNotFound: new Error('evaluator: feature not found'),
8
+ PrerequisiteVariationNotFound: new Error('evaluator: prerequisite variation not found'),
9
+ VariationNotFound: new Error('evaluator: variation not found'),
10
+ UnsupportedStrategy: new Error('evaluator: unsupported strategy'),
11
+ };
12
+ exports.EVALUATOR_ERRORS = EVALUATOR_ERRORS;
@@ -0,0 +1,25 @@
1
+ import { UserEvaluations } from './proto/feature/evaluation_pb';
2
+ import { Feature } from './proto/feature/feature_pb';
3
+ import { Reason } from './proto/feature/reason_pb';
4
+ import { SegmentUser } from './proto/feature/segment_pb';
5
+ import { Variation } from './proto/feature/variation_pb';
6
+ import { User } from './proto/user/user_pb';
7
+ export declare function EvaluationID(featureID: string, featureVersion: number, userID: string): string;
8
+ declare class Evaluator {
9
+ private ruleEvaluator;
10
+ private strategyEvaluator;
11
+ constructor();
12
+ evaluateFeatures(features: Feature[], user: User, mapSegmentUsers: Map<string, SegmentUser[]>, targetTag: string): Promise<UserEvaluations>;
13
+ getPrerequisiteDownwards(targetFeatures: Feature[], allFeatures: Feature[]): Feature[];
14
+ evaluateFeaturesByEvaluatedAt(features: Feature[], user: User, mapSegmentUsers: Map<string, SegmentUser[]>, prevUEID: string, evaluatedAt: number, userAttributesUpdated: boolean, targetTag: string): UserEvaluations;
15
+ private evaluate;
16
+ private tagExist;
17
+ private isArchivedBeforeLastThirtyDays;
18
+ listSegmentIDs(feature: Feature): string[];
19
+ assignUser(feature: Feature, user: User, segmentUsers: SegmentUser[], flagVariations: {
20
+ [key: string]: string;
21
+ }): [Reason, Variation];
22
+ getEvalFeatures(targetFeatures: Feature[], allFeatures: Feature[]): Feature[];
23
+ private findVariation;
24
+ }
25
+ export { Evaluator };
@@ -0,0 +1,503 @@
1
+ "use strict";
2
+
3
+ function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
4
+ function _callSuper(t, o, e) { return o = _getPrototypeOf(o), _possibleConstructorReturn(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], _getPrototypeOf(t).constructor) : o.apply(t, e)); }
5
+ function _possibleConstructorReturn(t, e) { if (e && ("object" == _typeof(e) || "function" == typeof e)) return e; if (void 0 !== e) throw new TypeError("Derived constructors may only return object or undefined"); return _assertThisInitialized(t); }
6
+ function _assertThisInitialized(e) { if (void 0 === e) throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); return e; }
7
+ function _inherits(t, e) { if ("function" != typeof e && null !== e) throw new TypeError("Super expression must either be null or a function"); t.prototype = Object.create(e && e.prototype, { constructor: { value: t, writable: !0, configurable: !0 } }), Object.defineProperty(t, "prototype", { writable: !1 }), e && _setPrototypeOf(t, e); }
8
+ function _wrapNativeSuper(t) { var r = "function" == typeof Map ? new Map() : void 0; return _wrapNativeSuper = function _wrapNativeSuper(t) { if (null === t || !_isNativeFunction(t)) return t; if ("function" != typeof t) throw new TypeError("Super expression must either be null or a function"); if (void 0 !== r) { if (r.has(t)) return r.get(t); r.set(t, Wrapper); } function Wrapper() { return _construct(t, arguments, _getPrototypeOf(this).constructor); } return Wrapper.prototype = Object.create(t.prototype, { constructor: { value: Wrapper, enumerable: !1, writable: !0, configurable: !0 } }), _setPrototypeOf(Wrapper, t); }, _wrapNativeSuper(t); }
9
+ function _construct(t, e, r) { if (_isNativeReflectConstruct()) return Reflect.construct.apply(null, arguments); var o = [null]; o.push.apply(o, e); var p = new (t.bind.apply(t, o))(); return r && _setPrototypeOf(p, r.prototype), p; }
10
+ function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); }
11
+ function _isNativeFunction(t) { try { return -1 !== Function.toString.call(t).indexOf("[native code]"); } catch (n) { return "function" == typeof t; } }
12
+ function _setPrototypeOf(t, e) { return _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function (t, e) { return t.__proto__ = e, t; }, _setPrototypeOf(t, e); }
13
+ function _getPrototypeOf(t) { return _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function (t) { return t.__proto__ || Object.getPrototypeOf(t); }, _getPrototypeOf(t); }
14
+ function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
15
+ function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
16
+ function _slicedToArray(r, e) { return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _nonIterableRest(); }
17
+ function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
18
+ function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t["return"] && (u = t["return"](), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
19
+ function _arrayWithHoles(r) { if (Array.isArray(r)) return r; }
20
+ function _createForOfIteratorHelper(r, e) { var t = "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (!t) { if (Array.isArray(r) || (t = _unsupportedIterableToArray(r)) || e && r && "number" == typeof r.length) { t && (r = t); var _n = 0, F = function F() {}; return { s: F, n: function n() { return _n >= r.length ? { done: !0 } : { done: !1, value: r[_n++] }; }, e: function e(r) { throw r; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var o, a = !0, u = !1; return { s: function s() { t = t.call(r); }, n: function n() { var r = t.next(); return a = r.done, r; }, e: function e(r) { u = !0, o = r; }, f: function f() { try { a || null == t["return"] || t["return"](); } finally { if (u) throw o; } } }; }
21
+ function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
22
+ function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
23
+ function _regeneratorRuntime() { "use strict"; /*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/facebook/regenerator/blob/main/LICENSE */ _regeneratorRuntime = function _regeneratorRuntime() { return e; }; var t, e = {}, r = Object.prototype, n = r.hasOwnProperty, o = Object.defineProperty || function (t, e, r) { t[e] = r.value; }, i = "function" == typeof Symbol ? Symbol : {}, a = i.iterator || "@@iterator", c = i.asyncIterator || "@@asyncIterator", u = i.toStringTag || "@@toStringTag"; function define(t, e, r) { return Object.defineProperty(t, e, { value: r, enumerable: !0, configurable: !0, writable: !0 }), t[e]; } try { define({}, ""); } catch (t) { define = function define(t, e, r) { return t[e] = r; }; } function wrap(t, e, r, n) { var i = e && e.prototype instanceof Generator ? e : Generator, a = Object.create(i.prototype), c = new Context(n || []); return o(a, "_invoke", { value: makeInvokeMethod(t, r, c) }), a; } function tryCatch(t, e, r) { try { return { type: "normal", arg: t.call(e, r) }; } catch (t) { return { type: "throw", arg: t }; } } e.wrap = wrap; var h = "suspendedStart", l = "suspendedYield", f = "executing", s = "completed", y = {}; function Generator() {} function GeneratorFunction() {} function GeneratorFunctionPrototype() {} var p = {}; define(p, a, function () { return this; }); var d = Object.getPrototypeOf, v = d && d(d(values([]))); v && v !== r && n.call(v, a) && (p = v); var g = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(p); function defineIteratorMethods(t) { ["next", "throw", "return"].forEach(function (e) { define(t, e, function (t) { return this._invoke(e, t); }); }); } function AsyncIterator(t, e) { function invoke(r, o, i, a) { var c = tryCatch(t[r], t, o); if ("throw" !== c.type) { var u = c.arg, h = u.value; return h && "object" == _typeof(h) && n.call(h, "__await") ? e.resolve(h.__await).then(function (t) { invoke("next", t, i, a); }, function (t) { invoke("throw", t, i, a); }) : e.resolve(h).then(function (t) { u.value = t, i(u); }, function (t) { return invoke("throw", t, i, a); }); } a(c.arg); } var r; o(this, "_invoke", { value: function value(t, n) { function callInvokeWithMethodAndArg() { return new e(function (e, r) { invoke(t, n, e, r); }); } return r = r ? r.then(callInvokeWithMethodAndArg, callInvokeWithMethodAndArg) : callInvokeWithMethodAndArg(); } }); } function makeInvokeMethod(e, r, n) { var o = h; return function (i, a) { if (o === f) throw Error("Generator is already running"); if (o === s) { if ("throw" === i) throw a; return { value: t, done: !0 }; } for (n.method = i, n.arg = a;;) { var c = n.delegate; if (c) { var u = maybeInvokeDelegate(c, n); if (u) { if (u === y) continue; return u; } } if ("next" === n.method) n.sent = n._sent = n.arg;else if ("throw" === n.method) { if (o === h) throw o = s, n.arg; n.dispatchException(n.arg); } else "return" === n.method && n.abrupt("return", n.arg); o = f; var p = tryCatch(e, r, n); if ("normal" === p.type) { if (o = n.done ? s : l, p.arg === y) continue; return { value: p.arg, done: n.done }; } "throw" === p.type && (o = s, n.method = "throw", n.arg = p.arg); } }; } function maybeInvokeDelegate(e, r) { var n = r.method, o = e.iterator[n]; if (o === t) return r.delegate = null, "throw" === n && e.iterator["return"] && (r.method = "return", r.arg = t, maybeInvokeDelegate(e, r), "throw" === r.method) || "return" !== n && (r.method = "throw", r.arg = new TypeError("The iterator does not provide a '" + n + "' method")), y; var i = tryCatch(o, e.iterator, r.arg); if ("throw" === i.type) return r.method = "throw", r.arg = i.arg, r.delegate = null, y; var a = i.arg; return a ? a.done ? (r[e.resultName] = a.value, r.next = e.nextLoc, "return" !== r.method && (r.method = "next", r.arg = t), r.delegate = null, y) : a : (r.method = "throw", r.arg = new TypeError("iterator result is not an object"), r.delegate = null, y); } function pushTryEntry(t) { var e = { tryLoc: t[0] }; 1 in t && (e.catchLoc = t[1]), 2 in t && (e.finallyLoc = t[2], e.afterLoc = t[3]), this.tryEntries.push(e); } function resetTryEntry(t) { var e = t.completion || {}; e.type = "normal", delete e.arg, t.completion = e; } function Context(t) { this.tryEntries = [{ tryLoc: "root" }], t.forEach(pushTryEntry, this), this.reset(!0); } function values(e) { if (e || "" === e) { var r = e[a]; if (r) return r.call(e); if ("function" == typeof e.next) return e; if (!isNaN(e.length)) { var o = -1, i = function next() { for (; ++o < e.length;) if (n.call(e, o)) return next.value = e[o], next.done = !1, next; return next.value = t, next.done = !0, next; }; return i.next = i; } } throw new TypeError(_typeof(e) + " is not iterable"); } return GeneratorFunction.prototype = GeneratorFunctionPrototype, o(g, "constructor", { value: GeneratorFunctionPrototype, configurable: !0 }), o(GeneratorFunctionPrototype, "constructor", { value: GeneratorFunction, configurable: !0 }), GeneratorFunction.displayName = define(GeneratorFunctionPrototype, u, "GeneratorFunction"), e.isGeneratorFunction = function (t) { var e = "function" == typeof t && t.constructor; return !!e && (e === GeneratorFunction || "GeneratorFunction" === (e.displayName || e.name)); }, e.mark = function (t) { return Object.setPrototypeOf ? Object.setPrototypeOf(t, GeneratorFunctionPrototype) : (t.__proto__ = GeneratorFunctionPrototype, define(t, u, "GeneratorFunction")), t.prototype = Object.create(g), t; }, e.awrap = function (t) { return { __await: t }; }, defineIteratorMethods(AsyncIterator.prototype), define(AsyncIterator.prototype, c, function () { return this; }), e.AsyncIterator = AsyncIterator, e.async = function (t, r, n, o, i) { void 0 === i && (i = Promise); var a = new AsyncIterator(wrap(t, r, n, o), i); return e.isGeneratorFunction(r) ? a : a.next().then(function (t) { return t.done ? t.value : a.next(); }); }, defineIteratorMethods(g), define(g, u, "Generator"), define(g, a, function () { return this; }), define(g, "toString", function () { return "[object Generator]"; }), e.keys = function (t) { var e = Object(t), r = []; for (var n in e) r.push(n); return r.reverse(), function next() { for (; r.length;) { var t = r.pop(); if (t in e) return next.value = t, next.done = !1, next; } return next.done = !0, next; }; }, e.values = values, Context.prototype = { constructor: Context, reset: function reset(e) { if (this.prev = 0, this.next = 0, this.sent = this._sent = t, this.done = !1, this.delegate = null, this.method = "next", this.arg = t, this.tryEntries.forEach(resetTryEntry), !e) for (var r in this) "t" === r.charAt(0) && n.call(this, r) && !isNaN(+r.slice(1)) && (this[r] = t); }, stop: function stop() { this.done = !0; var t = this.tryEntries[0].completion; if ("throw" === t.type) throw t.arg; return this.rval; }, dispatchException: function dispatchException(e) { if (this.done) throw e; var r = this; function handle(n, o) { return a.type = "throw", a.arg = e, r.next = n, o && (r.method = "next", r.arg = t), !!o; } for (var o = this.tryEntries.length - 1; o >= 0; --o) { var i = this.tryEntries[o], a = i.completion; if ("root" === i.tryLoc) return handle("end"); if (i.tryLoc <= this.prev) { var c = n.call(i, "catchLoc"), u = n.call(i, "finallyLoc"); if (c && u) { if (this.prev < i.catchLoc) return handle(i.catchLoc, !0); if (this.prev < i.finallyLoc) return handle(i.finallyLoc); } else if (c) { if (this.prev < i.catchLoc) return handle(i.catchLoc, !0); } else { if (!u) throw Error("try statement without catch or finally"); if (this.prev < i.finallyLoc) return handle(i.finallyLoc); } } } }, abrupt: function abrupt(t, e) { for (var r = this.tryEntries.length - 1; r >= 0; --r) { var o = this.tryEntries[r]; if (o.tryLoc <= this.prev && n.call(o, "finallyLoc") && this.prev < o.finallyLoc) { var i = o; break; } } i && ("break" === t || "continue" === t) && i.tryLoc <= e && e <= i.finallyLoc && (i = null); var a = i ? i.completion : {}; return a.type = t, a.arg = e, i ? (this.method = "next", this.next = i.finallyLoc, y) : this.complete(a); }, complete: function complete(t, e) { if ("throw" === t.type) throw t.arg; return "break" === t.type || "continue" === t.type ? this.next = t.arg : "return" === t.type ? (this.rval = this.arg = t.arg, this.method = "return", this.next = "end") : "normal" === t.type && e && (this.next = e), y; }, finish: function finish(t) { for (var e = this.tryEntries.length - 1; e >= 0; --e) { var r = this.tryEntries[e]; if (r.finallyLoc === t) return this.complete(r.completion, r.afterLoc), resetTryEntry(r), y; } }, "catch": function _catch(t) { for (var e = this.tryEntries.length - 1; e >= 0; --e) { var r = this.tryEntries[e]; if (r.tryLoc === t) { var n = r.completion; if ("throw" === n.type) { var o = n.arg; resetTryEntry(r); } return o; } } throw Error("illegal catch attempt"); }, delegateYield: function delegateYield(e, r, n) { return this.delegate = { iterator: values(e), resultName: r, nextLoc: n }, "next" === this.method && (this.arg = t), y; } }, e; }
24
+ function asyncGeneratorStep(n, t, e, r, o, a, c) { try { var i = n[a](c), u = i.value; } catch (n) { return void e(n); } i.done ? t(u) : Promise.resolve(u).then(r, o); }
25
+ function _asyncToGenerator(n) { return function () { var t = this, e = arguments; return new Promise(function (r, o) { var a = n.apply(t, e); function _next(n) { asyncGeneratorStep(a, r, o, _next, _throw, "next", n); } function _throw(n) { asyncGeneratorStep(a, r, o, _next, _throw, "throw", n); } _next(void 0); }); }; }
26
+ function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); }
27
+ function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } }
28
+ function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; }
29
+ function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
30
+ function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
31
+ function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
32
+ Object.defineProperty(exports, "__esModule", {
33
+ value: true
34
+ });
35
+ exports.Evaluator = void 0;
36
+ exports.EvaluationID = EvaluationID;
37
+ var errors_1 = require("./errors");
38
+ var clause_pb_1 = require("./proto/feature/clause_pb");
39
+ var evaluation_pb_1 = require("./proto/feature/evaluation_pb");
40
+ var reason_pb_1 = require("./proto/feature/reason_pb");
41
+ var variation_pb_1 = require("./proto/feature/variation_pb");
42
+ var ruleEvaluator_1 = require("./ruleEvaluator");
43
+ var strategyEvaluator_1 = require("./strategyEvaluator");
44
+ var userEvaluation_1 = require("./userEvaluation");
45
+ var modelFactory_1 = require("./modelFactory");
46
+ var SECONDS_TO_RE_EVALUATE_ALL = 30 * 24 * 60 * 60; // 30 days
47
+ var SECONDS_FOR_ADJUSTMENT = 10; // 10 seconds
48
+ function EvaluationID(featureID, featureVersion, userID) {
49
+ return "".concat(featureID, ":").concat(featureVersion, ":").concat(userID);
50
+ }
51
+ var Evaluator = /*#__PURE__*/function () {
52
+ function Evaluator() {
53
+ _classCallCheck(this, Evaluator);
54
+ _defineProperty(this, "ruleEvaluator", void 0);
55
+ _defineProperty(this, "strategyEvaluator", void 0);
56
+ this.ruleEvaluator = new ruleEvaluator_1.RuleEvaluator();
57
+ this.strategyEvaluator = new strategyEvaluator_1.StrategyEvaluator();
58
+ }
59
+ return _createClass(Evaluator, [{
60
+ key: "evaluateFeatures",
61
+ value: function () {
62
+ var _evaluateFeatures = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee(features, user, mapSegmentUsers, targetTag) {
63
+ return _regeneratorRuntime().wrap(function _callee$(_context) {
64
+ while (1) switch (_context.prev = _context.next) {
65
+ case 0:
66
+ return _context.abrupt("return", this.evaluate(features, user, mapSegmentUsers, false, targetTag));
67
+ case 1:
68
+ case "end":
69
+ return _context.stop();
70
+ }
71
+ }, _callee, this);
72
+ }));
73
+ function evaluateFeatures(_x, _x2, _x3, _x4) {
74
+ return _evaluateFeatures.apply(this, arguments);
75
+ }
76
+ return evaluateFeatures;
77
+ }() // GetPrerequisiteDownwards gets the features specified as prerequisite by the targetFeatures.
78
+ }, {
79
+ key: "getPrerequisiteDownwards",
80
+ value: function getPrerequisiteDownwards(targetFeatures, allFeatures) {
81
+ var allFeaturesMap = new Map();
82
+ var _iterator = _createForOfIteratorHelper(allFeatures),
83
+ _step;
84
+ try {
85
+ for (_iterator.s(); !(_step = _iterator.n()).done;) {
86
+ var feature = _step.value;
87
+ allFeaturesMap.set(feature.getId(), feature);
88
+ }
89
+ } catch (err) {
90
+ _iterator.e(err);
91
+ } finally {
92
+ _iterator.f();
93
+ }
94
+ var dependedOnTargets = getFeaturesDependedOnTargets(targetFeatures, allFeaturesMap);
95
+ return Object.values(dependedOnTargets);
96
+ }
97
+ }, {
98
+ key: "evaluateFeaturesByEvaluatedAt",
99
+ value: function evaluateFeaturesByEvaluatedAt(features, user, mapSegmentUsers, prevUEID, evaluatedAt, userAttributesUpdated, targetTag) {
100
+ if (prevUEID === '') {
101
+ return this.evaluate(features, user, mapSegmentUsers, true, targetTag);
102
+ }
103
+ var now = Math.floor(Date.now() / 1000);
104
+ if (evaluatedAt < now - SECONDS_TO_RE_EVALUATE_ALL) {
105
+ return this.evaluate(features, user, mapSegmentUsers, true, targetTag);
106
+ }
107
+ var adjustedEvalAt = evaluatedAt - SECONDS_FOR_ADJUSTMENT;
108
+ var updatedFeatures = features.filter(function (feature) {
109
+ return feature.getUpdatedAt() > adjustedEvalAt || userAttributesUpdated && feature.getRulesList().length > 0;
110
+ });
111
+ // If the UserEvaluationsID has changed, but both User Attributes and Feature Flags have not been updated,
112
+ // it is considered unusual and a force update should be performed.
113
+ if (updatedFeatures.length === 0) {
114
+ return this.evaluate(features, user, mapSegmentUsers, true, targetTag);
115
+ }
116
+ var evalTargets = this.getEvalFeatures(updatedFeatures, features);
117
+ return this.evaluate(evalTargets, user, mapSegmentUsers, false, targetTag);
118
+ }
119
+ }, {
120
+ key: "evaluate",
121
+ value: function evaluate(features, user, mapSegmentUsers, forceUpdate, targetTag) {
122
+ var flagVariations = {};
123
+ // fs need to be sorted in order from upstream to downstream.
124
+ var sortedFeatures = topologicalSort(features);
125
+ var evaluations = [];
126
+ var archivedIDs = [];
127
+ var _iterator2 = _createForOfIteratorHelper(sortedFeatures),
128
+ _step2;
129
+ try {
130
+ for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
131
+ var feature = _step2.value;
132
+ if (feature.getArchived()) {
133
+ // To keep response size small, the feature flags archived long time ago are excluded.
134
+ if (!this.isArchivedBeforeLastThirtyDays(feature)) {
135
+ archivedIDs.push(feature.getId());
136
+ }
137
+ continue;
138
+ }
139
+ var segmentUsers = this.listSegmentIDs(feature).flatMap(function (id) {
140
+ return mapSegmentUsers.get(id) || [];
141
+ });
142
+ var _this$assignUser = this.assignUser(feature, user, segmentUsers, flagVariations),
143
+ _this$assignUser2 = _slicedToArray(_this$assignUser, 2),
144
+ reason = _this$assignUser2[0],
145
+ variation = _this$assignUser2[1];
146
+ // VariationId is used to check if prerequisite flag's result is what user expects it to be.
147
+ flagVariations[feature.getId()] = variation.getId();
148
+ // When the tag is set in the request,
149
+ // it will return only the evaluations of flags that match the tag configured on the dashboard.
150
+ // When empty, it will return all the evaluations of the flags in the environment.
151
+ if (targetTag !== '' && !this.tagExist(feature.getTagsList(), targetTag)) {
152
+ continue;
153
+ }
154
+ var evaluationID = EvaluationID(feature.getId(), feature.getVersion(), user.getId());
155
+ var evaluation = new evaluation_pb_1.Evaluation();
156
+ evaluation.setId(evaluationID);
157
+ evaluation.setFeatureId(feature.getId());
158
+ evaluation.setFeatureVersion(feature.getVersion());
159
+ evaluation.setUserId(user.getId());
160
+ evaluation.setVariationId(variation.getId());
161
+ evaluation.setVariationName(variation.getName());
162
+ evaluation.setVariationValue(variation.getValue());
163
+ // Deprecated
164
+ // FIXME: Remove the Variation when is no longer being used.
165
+ // For security reasons, we should remove the variation description.
166
+ // We copy the variation object to avoid race conditions when removing
167
+ // the description directly from the `variation`
168
+ var copyVariation = new variation_pb_1.Variation();
169
+ copyVariation.setId(variation.getId());
170
+ copyVariation.setName(variation.getName());
171
+ copyVariation.setValue(variation.getValue());
172
+ evaluation.setVariation(copyVariation);
173
+ evaluation.setReason(reason);
174
+ evaluations.push(evaluation);
175
+ }
176
+ } catch (err) {
177
+ _iterator2.e(err);
178
+ } finally {
179
+ _iterator2.f();
180
+ }
181
+ var id = (0, userEvaluation_1.UserEvaluationsID)(user.getId(), arrayToRecord(user.getDataMap().toArray()), features);
182
+ var userEvaluations = (0, userEvaluation_1.NewUserEvaluations)(id, evaluations, archivedIDs, forceUpdate);
183
+ return userEvaluations;
184
+ }
185
+ }, {
186
+ key: "tagExist",
187
+ value: function tagExist(tags, target) {
188
+ return tags.includes(target);
189
+ }
190
+ /*
191
+ IsArchivedBeforeLastThirtyDays returns a bool value
192
+ indicating whether the feature flag was archived within the last thirty days.
193
+ */
194
+ }, {
195
+ key: "isArchivedBeforeLastThirtyDays",
196
+ value: function isArchivedBeforeLastThirtyDays(feature) {
197
+ if (!feature.getArchived()) {
198
+ return false;
199
+ }
200
+ var now = Math.floor(Date.now() / 1000);
201
+ return feature.getUpdatedAt() < now - SECONDS_TO_RE_EVALUATE_ALL;
202
+ }
203
+ }, {
204
+ key: "listSegmentIDs",
205
+ value: function listSegmentIDs(feature) {
206
+ var mapIDs = new Set();
207
+ var _iterator3 = _createForOfIteratorHelper(feature.getRulesList()),
208
+ _step3;
209
+ try {
210
+ for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) {
211
+ var rule = _step3.value;
212
+ var _iterator4 = _createForOfIteratorHelper(rule.getClausesList()),
213
+ _step4;
214
+ try {
215
+ for (_iterator4.s(); !(_step4 = _iterator4.n()).done;) {
216
+ var clause = _step4.value;
217
+ if (clause.getOperator() === clause_pb_1.Clause.Operator.SEGMENT) {
218
+ clause.getValuesList().forEach(function (value) {
219
+ return mapIDs.add(value);
220
+ });
221
+ }
222
+ }
223
+ } catch (err) {
224
+ _iterator4.e(err);
225
+ } finally {
226
+ _iterator4.f();
227
+ }
228
+ }
229
+ } catch (err) {
230
+ _iterator3.e(err);
231
+ } finally {
232
+ _iterator3.f();
233
+ }
234
+ return Array.from(mapIDs);
235
+ }
236
+ }, {
237
+ key: "assignUser",
238
+ value: function assignUser(feature, user, segmentUsers, flagVariations) {
239
+ var _iterator5 = _createForOfIteratorHelper(feature.getPrerequisitesList()),
240
+ _step5;
241
+ try {
242
+ for (_iterator5.s(); !(_step5 = _iterator5.n()).done;) {
243
+ var pf = _step5.value;
244
+ var variationId = flagVariations[pf.getFeatureId()];
245
+ if (!variationId) {
246
+ throw errors_1.EVALUATOR_ERRORS.PrerequisiteVariationNotFound;
247
+ }
248
+ if (pf.getVariationId() !== variationId) {
249
+ if (feature.getOffVariation() !== '') {
250
+ var _variation3 = this.findVariation(feature.getOffVariation(), feature.getVariationsList());
251
+ var _reason3 = (0, modelFactory_1.createReason)('', reason_pb_1.Reason.Type.PREREQUISITE);
252
+ return [_reason3, _variation3];
253
+ }
254
+ }
255
+ }
256
+ // It doesn't assign the user in case of the feature is disabled and OffVariation is not set
257
+ } catch (err) {
258
+ _iterator5.e(err);
259
+ } finally {
260
+ _iterator5.f();
261
+ }
262
+ if (!feature.getEnabled() && feature.getOffVariation() !== '') {
263
+ var _variation = this.findVariation(feature.getOffVariation(), feature.getVariationsList());
264
+ var _reason = (0, modelFactory_1.createReason)('', reason_pb_1.Reason.Type.OFF_VARIATION);
265
+ return [_reason, _variation];
266
+ }
267
+ // evaluate from top to bottom, return if one rule matches
268
+ // evaluate targeting rules
269
+ var _iterator6 = _createForOfIteratorHelper(feature.getTargetsList()),
270
+ _step6;
271
+ try {
272
+ for (_iterator6.s(); !(_step6 = _iterator6.n()).done;) {
273
+ var target = _step6.value;
274
+ if (target.getUsersList().includes(user.getId())) {
275
+ var _variation4 = this.findVariation(target.getVariation(), feature.getVariationsList());
276
+ var _reason4 = (0, modelFactory_1.createReason)('', reason_pb_1.Reason.Type.TARGET);
277
+ return [_reason4, _variation4];
278
+ }
279
+ }
280
+ // evaluate ruleset
281
+ } catch (err) {
282
+ _iterator6.e(err);
283
+ } finally {
284
+ _iterator6.f();
285
+ }
286
+ var rule = this.ruleEvaluator.evaluate(feature.getRulesList(), user, segmentUsers, flagVariations);
287
+ if (rule) {
288
+ var strategy = rule.getStrategy();
289
+ if (strategy == undefined) {
290
+ throw errors_1.EVALUATOR_ERRORS.StrategyNotFound;
291
+ }
292
+ var _variation2 = this.strategyEvaluator.evaluate(strategy, user.getId(), feature.getVariationsList(), feature.getId(), feature.getSamplingSeed());
293
+ var _reason2 = (0, modelFactory_1.createReason)(rule.getId(), reason_pb_1.Reason.Type.RULE);
294
+ return [_reason2, _variation2];
295
+ }
296
+ // use default strategy
297
+ var defaultStrategy = feature.getDefaultStrategy();
298
+ if (defaultStrategy == undefined) {
299
+ throw errors_1.EVALUATOR_ERRORS.DefaultStrategyNotFound;
300
+ }
301
+ var variation = this.strategyEvaluator.evaluate(defaultStrategy, user.getId(), feature.getVariationsList(), feature.getId(), feature.getSamplingSeed());
302
+ var reason = (0, modelFactory_1.createReason)('', reason_pb_1.Reason.Type.DEFAULT);
303
+ return [reason, variation];
304
+ }
305
+ }, {
306
+ key: "getEvalFeatures",
307
+ value: function getEvalFeatures(targetFeatures, allFeatures) {
308
+ var allFeaturesMap = new Map();
309
+ var _iterator7 = _createForOfIteratorHelper(allFeatures),
310
+ _step7;
311
+ try {
312
+ for (_iterator7.s(); !(_step7 = _iterator7.n()).done;) {
313
+ var feature = _step7.value;
314
+ allFeaturesMap.set(feature.getId(), feature);
315
+ }
316
+ } catch (err) {
317
+ _iterator7.e(err);
318
+ } finally {
319
+ _iterator7.f();
320
+ }
321
+ var evals1 = getFeaturesDependedOnTargets(targetFeatures, allFeaturesMap);
322
+ var evals2 = getFeaturesDependsOnTargets(targetFeatures, allFeaturesMap);
323
+ var mergedEvals = _objectSpread(_objectSpread({}, evals1), evals2);
324
+ return Object.values(mergedEvals);
325
+ }
326
+ }, {
327
+ key: "findVariation",
328
+ value: function findVariation(v, variations) {
329
+ var variation = variations.find(function (variation) {
330
+ return variation.getId() === v;
331
+ });
332
+ if (!variation) {
333
+ throw errors_1.EVALUATOR_ERRORS.VariationNotFound;
334
+ }
335
+ return variation;
336
+ }
337
+ }]);
338
+ }();
339
+ exports.Evaluator = Evaluator;
340
+ var Mark;
341
+ (function (Mark) {
342
+ Mark[Mark["Unvisited"] = 0] = "Unvisited";
343
+ Mark[Mark["Temporary"] = 1] = "Temporary";
344
+ Mark[Mark["Permanently"] = 2] = "Permanently";
345
+ })(Mark || (Mark = {}));
346
+ // FeatureIDsDependsOn returns the ids of the features that this feature depends on.
347
+ function featureIDsDependsOn(feature) {
348
+ var ids = [];
349
+ // Iterate over prerequisites and add their FeatureId
350
+ feature.getPrerequisitesList().forEach(function (p) {
351
+ ids.push(p.getFeatureId());
352
+ });
353
+ // Iterate over rules and collect ids from clauses where the operator is FEATURE_FLAG
354
+ feature.getRulesList().forEach(function (rule) {
355
+ rule.getClausesList().forEach(function (clause) {
356
+ if (clause.getOperator() === clause_pb_1.Clause.Operator.FEATURE_FLAG) {
357
+ ids.push(clause.getAttribute());
358
+ }
359
+ });
360
+ });
361
+ return ids;
362
+ }
363
+ // Error types
364
+ var ErrCycleExists = /*#__PURE__*/function (_Error) {
365
+ function ErrCycleExists() {
366
+ _classCallCheck(this, ErrCycleExists);
367
+ return _callSuper(this, ErrCycleExists, ['Cycle exists in the graph']);
368
+ }
369
+ _inherits(ErrCycleExists, _Error);
370
+ return _createClass(ErrCycleExists);
371
+ }(/*#__PURE__*/_wrapNativeSuper(Error));
372
+ var ErrFeatureNotFound = /*#__PURE__*/function (_Error2) {
373
+ function ErrFeatureNotFound() {
374
+ _classCallCheck(this, ErrFeatureNotFound);
375
+ return _callSuper(this, ErrFeatureNotFound, ['Feature not found']);
376
+ }
377
+ _inherits(ErrFeatureNotFound, _Error2);
378
+ return _createClass(ErrFeatureNotFound);
379
+ }(/*#__PURE__*/_wrapNativeSuper(Error)); // Topological sort function
380
+ function topologicalSort(features) {
381
+ var marks = {};
382
+ var mapFeatures = {};
383
+ features.forEach(function (f) {
384
+ marks[f.getId()] = Mark.Unvisited;
385
+ mapFeatures[f.getId()] = f;
386
+ });
387
+ var sortedFeatures = [];
388
+ var _sort = function sort(f) {
389
+ var fId = f.getId();
390
+ if (marks[fId] === Mark.Permanently) return;
391
+ if (marks[fId] === Mark.Temporary) {
392
+ throw new ErrCycleExists();
393
+ }
394
+ marks[fId] = Mark.Temporary;
395
+ var dependentFeatureIds = featureIDsDependsOn(f);
396
+ var _iterator8 = _createForOfIteratorHelper(dependentFeatureIds),
397
+ _step8;
398
+ try {
399
+ for (_iterator8.s(); !(_step8 = _iterator8.n()).done;) {
400
+ var fid = _step8.value;
401
+ var pf = mapFeatures[fid];
402
+ if (!pf) {
403
+ throw new ErrFeatureNotFound();
404
+ }
405
+ _sort(pf);
406
+ }
407
+ } catch (err) {
408
+ _iterator8.e(err);
409
+ } finally {
410
+ _iterator8.f();
411
+ }
412
+ marks[fId] = Mark.Permanently;
413
+ sortedFeatures.push(f);
414
+ };
415
+ // Process each feature
416
+ var _iterator9 = _createForOfIteratorHelper(features),
417
+ _step9;
418
+ try {
419
+ for (_iterator9.s(); !(_step9 = _iterator9.n()).done;) {
420
+ var f = _step9.value;
421
+ if (marks[f.getId()] === Mark.Unvisited) {
422
+ _sort(f);
423
+ }
424
+ }
425
+ } catch (err) {
426
+ _iterator9.e(err);
427
+ } finally {
428
+ _iterator9.f();
429
+ }
430
+ return sortedFeatures;
431
+ }
432
+ // getFeaturesDependedOnTargets returns the features that are depended on the target features.
433
+ // targetFeatures are included in the result.
434
+ function getFeaturesDependedOnTargets(targets, all) {
435
+ var evals = {};
436
+ var _dfs = function dfs(f) {
437
+ // If the feature is already in the evals map, skip
438
+ if (evals[f.getId()]) return;
439
+ // Add feature to evals
440
+ evals[f.getId()] = f;
441
+ // Get dependent features recursively
442
+ var featureDependencies = featureIDsDependsOn(f);
443
+ featureDependencies.forEach(function (fid) {
444
+ var target = all.get(fid);
445
+ if (target !== undefined) {
446
+ _dfs(target);
447
+ }
448
+ });
449
+ };
450
+ // Start DFS for each target feature
451
+ targets.forEach(function (f) {
452
+ return _dfs(f);
453
+ });
454
+ return evals;
455
+ }
456
+ // getFeaturesDependsOnTargets returns the features that depend on the target features.
457
+ // targetFeatures are included in the result.
458
+ function getFeaturesDependsOnTargets(targets, all) {
459
+ var evals = {};
460
+ // Mark target features in the evals map initially
461
+ targets.forEach(function (f) {
462
+ evals[f.getId()] = f;
463
+ });
464
+ // Depth-first search to determine if a feature depends on a target feature
465
+ var _dfs2 = function dfs(f) {
466
+ if (evals[f.getId()]) {
467
+ return true;
468
+ }
469
+ var featureDependencies = featureIDsDependsOn(f);
470
+ var _iterator10 = _createForOfIteratorHelper(featureDependencies),
471
+ _step10;
472
+ try {
473
+ for (_iterator10.s(); !(_step10 = _iterator10.n()).done;) {
474
+ var fid = _step10.value;
475
+ var dependentFeature = all.get(fid);
476
+ if (dependentFeature && _dfs2(dependentFeature)) {
477
+ // If the feature depends on one of the target features, add it to evals
478
+ evals[f.getId()] = f;
479
+ return true;
480
+ }
481
+ }
482
+ } catch (err) {
483
+ _iterator10.e(err);
484
+ } finally {
485
+ _iterator10.f();
486
+ }
487
+ return false;
488
+ };
489
+ // Apply DFS for all features, except target features
490
+ all.forEach(function (f) {
491
+ _dfs2(f);
492
+ });
493
+ return evals;
494
+ }
495
+ function arrayToRecord(arr) {
496
+ return arr.reduce(function (acc, _ref) {
497
+ var _ref2 = _slicedToArray(_ref, 2),
498
+ key = _ref2[0],
499
+ value = _ref2[1];
500
+ acc[key] = value;
501
+ return acc;
502
+ }, {});
503
+ }