@microsoft/feature-management 1.0.0-preview.1 → 1.0.0

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 (46) hide show
  1. package/README.md +2 -0
  2. package/dist/commonjs/featureManager.js +71 -0
  3. package/dist/commonjs/featureManager.js.map +1 -0
  4. package/dist/commonjs/featureProvider.js +43 -0
  5. package/dist/commonjs/featureProvider.js.map +1 -0
  6. package/dist/commonjs/filter/TargetingFilter.js +138 -0
  7. package/dist/commonjs/filter/TargetingFilter.js.map +1 -0
  8. package/dist/commonjs/filter/TimeWindowFilter.js +22 -0
  9. package/{dist-esm → dist/commonjs}/filter/TimeWindowFilter.js.map +1 -1
  10. package/dist/commonjs/index.js +13 -0
  11. package/dist/commonjs/index.js.map +1 -0
  12. package/dist/commonjs/model.js +12 -0
  13. package/dist/commonjs/model.js.map +1 -0
  14. package/dist/commonjs/version.js +8 -0
  15. package/dist/commonjs/version.js.map +1 -0
  16. package/{dist-esm → dist/esm}/featureManager.js +18 -9
  17. package/dist/esm/featureManager.js.map +1 -0
  18. package/{dist-esm → dist/esm}/featureProvider.js +9 -6
  19. package/dist/esm/featureProvider.js.map +1 -0
  20. package/{dist-esm → dist/esm}/filter/TargetingFilter.js +47 -15
  21. package/dist/esm/filter/TargetingFilter.js.map +1 -0
  22. package/{dist-esm → dist/esm}/filter/TimeWindowFilter.js +4 -2
  23. package/dist/esm/filter/TimeWindowFilter.js.map +1 -0
  24. package/dist/esm/index.js +4 -0
  25. package/dist/esm/index.js.map +1 -0
  26. package/dist/esm/model.js +9 -0
  27. package/dist/esm/model.js.map +1 -0
  28. package/dist/esm/version.js +6 -0
  29. package/dist/esm/version.js.map +1 -0
  30. package/dist/umd/index.js +276 -0
  31. package/dist/umd/index.js.map +1 -0
  32. package/package.json +14 -19
  33. package/types/index.d.ts +121 -10
  34. package/dist/index.js +0 -234
  35. package/dist/index.js.map +0 -1
  36. package/dist-esm/featureManager.js.map +0 -1
  37. package/dist-esm/featureProvider.js.map +0 -1
  38. package/dist-esm/filter/FeatureFilter.js +0 -4
  39. package/dist-esm/filter/FeatureFilter.js.map +0 -1
  40. package/dist-esm/filter/TargetingFilter.js.map +0 -1
  41. package/dist-esm/gettable.js +0 -6
  42. package/dist-esm/gettable.js.map +0 -1
  43. package/dist-esm/index.js +0 -5
  44. package/dist-esm/index.js.map +0 -1
  45. package/dist-esm/model.js +0 -12
  46. package/dist-esm/model.js.map +0 -1
package/types/index.d.ts CHANGED
@@ -11,6 +11,9 @@ interface IGettable {
11
11
  get<T>(key: string): T | undefined;
12
12
  }
13
13
 
14
+ /**
15
+ * A feature flag is a named property that can be toggled to enable or disable some feature of an application.
16
+ */
14
17
  interface FeatureFlag {
15
18
  /**
16
19
  * An ID used to uniquely identify and reference the feature.
@@ -27,16 +30,27 @@ interface FeatureFlag {
27
30
  /**
28
31
  * A feature is OFF if enabled is false. If enabled is true, then the feature is ON if there are no conditions (null or empty) or if the conditions are satisfied.
29
32
  */
30
- enabled: boolean;
33
+ enabled?: boolean;
31
34
  /**
32
- * The declaration of conditions used to dynamically enable features.
35
+ * The declaration of conditions used to dynamically enable the feature.
33
36
  */
34
37
  conditions?: FeatureEnablementConditions;
38
+ /**
39
+ * The list of variants defined for this feature. A variant represents a configuration value of a feature flag that can be a string, a number, a boolean, or a JSON object.
40
+ */
41
+ variants?: Variant[];
42
+ /**
43
+ * Determines how variants should be allocated for the feature to various users.
44
+ */
45
+ allocation?: VariantAllocation;
46
+ /**
47
+ * The declaration of options used to configure telemetry for this feature.
48
+ */
49
+ telemetry?: TelemetryOptions;
35
50
  }
36
- declare enum RequirementType {
37
- Any = "Any",
38
- All = "All"
39
- }
51
+ /**
52
+ * The declaration of conditions used to dynamically enable the feature
53
+ */
40
54
  interface FeatureEnablementConditions {
41
55
  /**
42
56
  * Determines whether any or all registered client filters must be evaluated as true for the feature to be considered enabled.
@@ -47,15 +61,110 @@ interface FeatureEnablementConditions {
47
61
  */
48
62
  client_filters?: ClientFilter[];
49
63
  }
64
+ type RequirementType = "Any" | "All";
50
65
  interface ClientFilter {
51
66
  /**
52
- * The name used to refer to and require a client filter.
67
+ * The name used to refer to a client filter.
53
68
  */
54
69
  name: string;
55
70
  /**
56
- * Custom parameters for a given client filter. A client filter can require any set of parameters of any type.
71
+ * Parameters for a given client filter. A client filter can require any set of parameters of any type.
57
72
  */
58
- parameters?: unknown;
73
+ parameters?: Record<string, unknown>;
74
+ }
75
+ interface Variant {
76
+ /**
77
+ * The name used to refer to a feature variant.
78
+ */
79
+ name: string;
80
+ /**
81
+ * The configuration value for this feature variant.
82
+ */
83
+ configuration_value?: unknown;
84
+ /**
85
+ * The path to a configuration section used as the configuration value for this feature variant.
86
+ */
87
+ configuration_reference?: string;
88
+ /**
89
+ * Overrides the enabled state of the feature if the given variant is assigned. Does not override the state if value is None.
90
+ */
91
+ status_override?: "None" | "Enabled" | "Disabled";
92
+ }
93
+ /**
94
+ * Determines how variants should be allocated for the feature to various users.
95
+ */
96
+ interface VariantAllocation {
97
+ /**
98
+ * Specifies which variant should be used when the feature is considered disabled.
99
+ */
100
+ default_when_disabled?: string;
101
+ /**
102
+ * Specifies which variant should be used when the feature is considered enabled and no other allocation rules are applicable.
103
+ */
104
+ default_when_enabled?: string;
105
+ /**
106
+ * A list of objects, each containing a variant name and list of users for whom that variant should be used.
107
+ */
108
+ user?: UserAllocation[];
109
+ /**
110
+ * A list of objects, each containing a variant name and list of groups for which that variant should be used.
111
+ */
112
+ group?: GroupAllocation[];
113
+ /**
114
+ * A list of objects, each containing a variant name and percentage range for which that variant should be used.
115
+ */
116
+ percentile?: PercentileAllocation[];
117
+ /**
118
+ * The value percentile calculations are based on. The calculated percentile is consistent across features for a given user if the same nonempty seed is used.
119
+ */
120
+ seed?: string;
121
+ }
122
+ interface UserAllocation {
123
+ /**
124
+ * The name of the variant to use if the user allocation matches the current user.
125
+ */
126
+ variant: string;
127
+ /**
128
+ * Collection of users where if any match the current user, the variant specified in the user allocation is used.
129
+ */
130
+ users: string[];
131
+ }
132
+ interface GroupAllocation {
133
+ /**
134
+ * The name of the variant to use if the group allocation matches a group the current user is in.
135
+ */
136
+ variant: string;
137
+ /**
138
+ * Collection of groups where if the current user is in any of these groups, the variant specified in the group allocation is used.
139
+ */
140
+ groups: string[];
141
+ }
142
+ interface PercentileAllocation {
143
+ /**
144
+ * The name of the variant to use if the calculated percentile for the current user falls in the provided range.
145
+ */
146
+ variant: string;
147
+ /**
148
+ * The lower end of the percentage range for which this variant will be used.
149
+ */
150
+ from: number;
151
+ /**
152
+ * The upper end of the percentage range for which this variant will be used.
153
+ */
154
+ to: number;
155
+ }
156
+ /**
157
+ * The declaration of options used to configure telemetry for this feature.
158
+ */
159
+ interface TelemetryOptions {
160
+ /**
161
+ * Indicates if telemetry is enabled.
162
+ */
163
+ enabled?: boolean;
164
+ /**
165
+ * A container for metadata that should be bundled with flag telemetry.
166
+ */
167
+ metadata?: Record<string, string>;
59
168
  }
60
169
 
61
170
  interface IFeatureFlagProvider {
@@ -98,4 +207,6 @@ interface FeatureManagerOptions {
98
207
  customFilters?: IFeatureFilter[];
99
208
  }
100
209
 
101
- export { ConfigurationMapFeatureFlagProvider, ConfigurationObjectFeatureFlagProvider, FeatureManager, type IFeatureFlagProvider };
210
+ declare const VERSION = "1.0.0";
211
+
212
+ export { ConfigurationMapFeatureFlagProvider, ConfigurationObjectFeatureFlagProvider, FeatureManager, type IFeatureFilter, type IFeatureFlagProvider, VERSION };
package/dist/index.js DELETED
@@ -1,234 +0,0 @@
1
- 'use strict';
2
-
3
- var crypto = require('crypto');
4
-
5
- // Copyright (c) Microsoft Corporation.
6
- // Licensed under the MIT license.
7
- class TimeWindowFilter {
8
- name = "Microsoft.TimeWindow";
9
- evaluate(context) {
10
- const { featureName, parameters } = context;
11
- const startTime = parameters.Start !== undefined ? new Date(parameters.Start) : undefined;
12
- const endTime = parameters.End !== undefined ? new Date(parameters.End) : undefined;
13
- if (startTime === undefined && endTime === undefined) {
14
- // If neither start nor end time is specified, then the filter is not applicable.
15
- console.warn(`The ${this.name} feature filter is not valid for feature ${featureName}. It must specify either 'Start', 'End', or both.`);
16
- return false;
17
- }
18
- const now = new Date();
19
- return (startTime === undefined || startTime <= now) && (endTime === undefined || now < endTime);
20
- }
21
- }
22
-
23
- // Copyright (c) Microsoft Corporation.
24
- // Licensed under the MIT license.
25
- var RequirementType;
26
- (function (RequirementType) {
27
- RequirementType["Any"] = "Any";
28
- RequirementType["All"] = "All";
29
- })(RequirementType || (RequirementType = {}));
30
- // Feature Management Section fed into feature manager.
31
- // Converted from https://github.com/Azure/AppConfiguration/blob/main/docs/FeatureManagement/FeatureManagement.v1.0.0.schema.json
32
- const FEATURE_MANAGEMENT_KEY = "feature_management";
33
- const FEATURE_FLAGS_KEY = "feature_flags";
34
-
35
- // Copyright (c) Microsoft Corporation.
36
- // Licensed under the MIT license.
37
- class TargetingFilter {
38
- name = "Microsoft.Targeting";
39
- evaluate(context, appContext) {
40
- const { featureName, parameters } = context;
41
- TargetingFilter.#validateParameters(parameters);
42
- if (appContext === undefined) {
43
- throw new Error("The app context is required for targeting filter.");
44
- }
45
- if (parameters.Audience.Exclusion !== undefined) {
46
- // check if the user is in the exclusion list
47
- if (appContext?.userId !== undefined &&
48
- parameters.Audience.Exclusion.Users !== undefined &&
49
- parameters.Audience.Exclusion.Users.includes(appContext.userId)) {
50
- return false;
51
- }
52
- // check if the user is in a group within exclusion list
53
- if (appContext?.groups !== undefined &&
54
- parameters.Audience.Exclusion.Groups !== undefined) {
55
- for (const excludedGroup of parameters.Audience.Exclusion.Groups) {
56
- if (appContext.groups.includes(excludedGroup)) {
57
- return false;
58
- }
59
- }
60
- }
61
- }
62
- // check if the user is being targeted directly
63
- if (appContext?.userId !== undefined &&
64
- parameters.Audience.Users !== undefined &&
65
- parameters.Audience.Users.includes(appContext.userId)) {
66
- return true;
67
- }
68
- // check if the user is in a group that is being targeted
69
- if (appContext?.groups !== undefined &&
70
- parameters.Audience.Groups !== undefined) {
71
- for (const group of parameters.Audience.Groups) {
72
- if (appContext.groups.includes(group.Name)) {
73
- const audienceContextId = constructAudienceContextId(featureName, appContext.userId, group.Name);
74
- const rolloutPercentage = group.RolloutPercentage;
75
- if (TargetingFilter.#isTargeted(audienceContextId, rolloutPercentage)) {
76
- return true;
77
- }
78
- }
79
- }
80
- }
81
- // check if the user is being targeted by a default rollout percentage
82
- const defaultContextId = constructAudienceContextId(featureName, appContext?.userId);
83
- return TargetingFilter.#isTargeted(defaultContextId, parameters.Audience.DefaultRolloutPercentage);
84
- }
85
- static #isTargeted(audienceContextId, rolloutPercentage) {
86
- if (rolloutPercentage === 100) {
87
- return true;
88
- }
89
- // Cryptographic hashing algorithms ensure adequate entropy across hash values.
90
- const contextMarker = stringToUint32(audienceContextId);
91
- const contextPercentage = (contextMarker / 0xFFFFFFFF) * 100;
92
- return contextPercentage < rolloutPercentage;
93
- }
94
- static #validateParameters(parameters) {
95
- if (parameters.Audience.DefaultRolloutPercentage < 0 || parameters.Audience.DefaultRolloutPercentage > 100) {
96
- throw new Error("Audience.DefaultRolloutPercentage must be a number between 0 and 100.");
97
- }
98
- // validate RolloutPercentage for each group
99
- if (parameters.Audience.Groups !== undefined) {
100
- for (const group of parameters.Audience.Groups) {
101
- if (group.RolloutPercentage < 0 || group.RolloutPercentage > 100) {
102
- throw new Error(`RolloutPercentage of group ${group.Name} must be a number between 0 and 100.`);
103
- }
104
- }
105
- }
106
- }
107
- }
108
- /**
109
- * Constructs the context id for the audience.
110
- * The context id is used to determine if the user is part of the audience for a feature.
111
- * If groupName is provided, the context id is constructed as follows:
112
- * userId + "\n" + featureName + "\n" + groupName
113
- * Otherwise, the context id is constructed as follows:
114
- * userId + "\n" + featureName
115
- *
116
- * @param featureName name of the feature
117
- * @param userId userId from app context
118
- * @param groupName group name from app context
119
- * @returns a string that represents the context id for the audience
120
- */
121
- function constructAudienceContextId(featureName, userId, groupName) {
122
- let contextId = `${userId ?? ""}\n${featureName}`;
123
- if (groupName !== undefined) {
124
- contextId += `\n${groupName}`;
125
- }
126
- return contextId;
127
- }
128
- function stringToUint32(str) {
129
- // Create a SHA-256 hash of the string
130
- const hash = crypto.createHash("sha256").update(str).digest();
131
- // Get the first 4 bytes of the hash
132
- const first4Bytes = hash.subarray(0, 4);
133
- // Convert the 4 bytes to a uint32 with little-endian encoding
134
- const uint32 = first4Bytes.readUInt32LE(0);
135
- return uint32;
136
- }
137
-
138
- // Copyright (c) Microsoft Corporation.
139
- // Licensed under the MIT license.
140
- class FeatureManager {
141
- #provider;
142
- #featureFilters = new Map();
143
- constructor(provider, options) {
144
- this.#provider = provider;
145
- const builtinFilters = [new TimeWindowFilter(), new TargetingFilter()];
146
- // If a custom filter shares a name with an existing filter, the custom filter overrides the existing one.
147
- for (const filter of [...builtinFilters, ...(options?.customFilters ?? [])]) {
148
- this.#featureFilters.set(filter.name, filter);
149
- }
150
- }
151
- async listFeatureNames() {
152
- const features = await this.#provider.getFeatureFlags();
153
- const featureNameSet = new Set(features.map((feature) => feature.id));
154
- return Array.from(featureNameSet);
155
- }
156
- // If multiple feature flags are found, the first one takes precedence.
157
- async isEnabled(featureName, context) {
158
- const featureFlag = await this.#provider.getFeatureFlag(featureName);
159
- if (featureFlag === undefined) {
160
- // If the feature is not found, then it is disabled.
161
- return false;
162
- }
163
- if (featureFlag.enabled === false) {
164
- // If the feature is explicitly disabled, then it is disabled.
165
- return false;
166
- }
167
- const clientFilters = featureFlag.conditions?.client_filters;
168
- if (clientFilters === undefined || clientFilters.length <= 0) {
169
- // If there are no client filters, then the feature is enabled.
170
- return true;
171
- }
172
- const requirementType = featureFlag.conditions?.requirement_type ?? RequirementType.Any; // default to any.
173
- /**
174
- * While iterating through the client filters, we short-circuit the evaluation based on the requirement type.
175
- * - When requirement type is "All", the feature is enabled if all client filters are matched. If any client filter is not matched, the feature is disabled, otherwise it is enabled. `shortCircuitEvaluationResult` is false.
176
- * - When requirement type is "Any", the feature is enabled if any client filter is matched. If any client filter is matched, the feature is enabled, otherwise it is disabled. `shortCircuitEvaluationResult` is true.
177
- */
178
- const shortCircuitEvaluationResult = requirementType === RequirementType.Any;
179
- for (const clientFilter of clientFilters) {
180
- const matchedFeatureFilter = this.#featureFilters.get(clientFilter.name);
181
- const contextWithFeatureName = { featureName, parameters: clientFilter.parameters };
182
- if (matchedFeatureFilter === undefined) {
183
- console.warn(`Feature filter ${clientFilter.name} is not found.`);
184
- return false;
185
- }
186
- if (await matchedFeatureFilter.evaluate(contextWithFeatureName, context) === shortCircuitEvaluationResult) {
187
- return shortCircuitEvaluationResult;
188
- }
189
- }
190
- // If we get here, then we have not found a client filter that matches the requirement type.
191
- return !shortCircuitEvaluationResult;
192
- }
193
- }
194
-
195
- // Copyright (c) Microsoft Corporation.
196
- // Licensed under the MIT license.
197
- /**
198
- * A feature flag provider that uses a map-like configuration to provide feature flags.
199
- */
200
- class ConfigurationMapFeatureFlagProvider {
201
- #configuration;
202
- constructor(configuration) {
203
- this.#configuration = configuration;
204
- }
205
- async getFeatureFlag(featureName) {
206
- const featureConfig = this.#configuration.get(FEATURE_MANAGEMENT_KEY);
207
- return featureConfig?.[FEATURE_FLAGS_KEY]?.find((feature) => feature.id === featureName);
208
- }
209
- async getFeatureFlags() {
210
- const featureConfig = this.#configuration.get(FEATURE_MANAGEMENT_KEY);
211
- return featureConfig?.[FEATURE_FLAGS_KEY] ?? [];
212
- }
213
- }
214
- /**
215
- * A feature flag provider that uses an object-like configuration to provide feature flags.
216
- */
217
- class ConfigurationObjectFeatureFlagProvider {
218
- #configuration;
219
- constructor(configuration) {
220
- this.#configuration = configuration;
221
- }
222
- async getFeatureFlag(featureName) {
223
- const featureFlags = this.#configuration[FEATURE_MANAGEMENT_KEY]?.[FEATURE_FLAGS_KEY];
224
- return featureFlags?.find((feature) => feature.id === featureName);
225
- }
226
- async getFeatureFlags() {
227
- return this.#configuration[FEATURE_MANAGEMENT_KEY]?.[FEATURE_FLAGS_KEY] ?? [];
228
- }
229
- }
230
-
231
- exports.ConfigurationMapFeatureFlagProvider = ConfigurationMapFeatureFlagProvider;
232
- exports.ConfigurationObjectFeatureFlagProvider = ConfigurationObjectFeatureFlagProvider;
233
- exports.FeatureManager = FeatureManager;
234
- //# sourceMappingURL=index.js.map
package/dist/index.js.map DELETED
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.js","sources":["../src/filter/TimeWindowFilter.ts","../src/model.ts","../src/filter/TargetingFilter.ts","../src/featureManager.ts","../src/featureProvider.ts"],"sourcesContent":["// Copyright (c) Microsoft Corporation.\r\n// Licensed under the MIT license.\r\n\r\nimport { IFeatureFilter } from \"./FeatureFilter\";\r\n\r\n// [Start, End)\r\ntype TimeWindowParameters = {\r\n Start?: string;\r\n End?: string;\r\n}\r\n\r\ntype TimeWindowFilterEvaluationContext = {\r\n featureName: string;\r\n parameters: TimeWindowParameters;\r\n}\r\n\r\nexport class TimeWindowFilter implements IFeatureFilter {\r\n name: string = \"Microsoft.TimeWindow\";\r\n\r\n evaluate(context: TimeWindowFilterEvaluationContext): boolean {\r\n const {featureName, parameters} = context;\r\n const startTime = parameters.Start !== undefined ? new Date(parameters.Start) : undefined;\r\n const endTime = parameters.End !== undefined ? new Date(parameters.End) : undefined;\r\n\r\n if (startTime === undefined && endTime === undefined) {\r\n // If neither start nor end time is specified, then the filter is not applicable.\r\n console.warn(`The ${this.name} feature filter is not valid for feature ${featureName}. It must specify either 'Start', 'End', or both.`);\r\n return false;\r\n }\r\n const now = new Date();\r\n return (startTime === undefined || startTime <= now) && (endTime === undefined || now < endTime);\r\n }\r\n}\r\n","// Copyright (c) Microsoft Corporation.\r\n// Licensed under the MIT license.\r\n\r\n// Converted from https://github.com/Azure/AppConfiguration/blob/main/docs/FeatureManagement/FeatureFlag.v1.1.0.schema.json\r\n\r\nexport interface FeatureFlag {\r\n /**\r\n * An ID used to uniquely identify and reference the feature.\r\n */\r\n id: string\r\n\r\n /**\r\n * A description of the feature.\r\n */\r\n description?: string\r\n\r\n /**\r\n * A display name for the feature to use for display rather than the ID.\r\n */\r\n display_name?: string\r\n\r\n /**\r\n * A feature is OFF if enabled is false. If enabled is true, then the feature is ON if there are no conditions (null or empty) or if the conditions are satisfied.\r\n */\r\n enabled: boolean\r\n\r\n /**\r\n * The declaration of conditions used to dynamically enable features.\r\n */\r\n conditions?: FeatureEnablementConditions\r\n}\r\n\r\nexport enum RequirementType {\r\n Any = \"Any\",\r\n All = \"All\"\r\n}\r\n\r\nexport interface FeatureEnablementConditions {\r\n /**\r\n * Determines whether any or all registered client filters must be evaluated as true for the feature to be considered enabled.\r\n */\r\n requirement_type?: RequirementType\r\n\r\n /**\r\n * Filters that must run on the client and be evaluated as true for the feature to be considered enabled.\r\n */\r\n client_filters?: ClientFilter[]\r\n}\r\n\r\nexport interface ClientFilter {\r\n /**\r\n * The name used to refer to and require a client filter.\r\n */\r\n name: string\r\n /**\r\n * Custom parameters for a given client filter. A client filter can require any set of parameters of any type.\r\n */\r\n parameters?: unknown\r\n}\r\n\r\n// Feature Management Section fed into feature manager.\r\n// Converted from https://github.com/Azure/AppConfiguration/blob/main/docs/FeatureManagement/FeatureManagement.v1.0.0.schema.json\r\n\r\nexport const FEATURE_MANAGEMENT_KEY = \"feature_management\"\r\nexport const FEATURE_FLAGS_KEY = \"feature_flags\"\r\n\r\nexport interface FeatureManagementConfiguration {\r\n feature_management: FeatureManagement\r\n}\r\n\r\n/**\r\n * Declares feature management configuration.\r\n */\r\nexport interface FeatureManagement {\r\n feature_flags: FeatureFlag[];\r\n}\r\n","// Copyright (c) Microsoft Corporation.\r\n// Licensed under the MIT license.\r\n\r\nimport { IFeatureFilter } from \"./FeatureFilter\";\r\nimport { createHash } from \"crypto\";\r\n\r\ntype TargetingFilterParameters = {\r\n Audience: {\r\n DefaultRolloutPercentage: number;\r\n Users?: string[];\r\n Groups?: {\r\n Name: string;\r\n RolloutPercentage: number;\r\n }[];\r\n Exclusion?: {\r\n Users?: string[];\r\n Groups?: string[];\r\n };\r\n }\r\n}\r\n\r\ntype TargetingFilterEvaluationContext = {\r\n featureName: string;\r\n parameters: TargetingFilterParameters;\r\n}\r\n\r\ntype TargetingFilterAppContext = {\r\n userId?: string;\r\n groups?: string[];\r\n}\r\n\r\nexport class TargetingFilter implements IFeatureFilter {\r\n name: string = \"Microsoft.Targeting\";\r\n\r\n evaluate(context: TargetingFilterEvaluationContext, appContext?: TargetingFilterAppContext): boolean {\r\n const { featureName, parameters } = context;\r\n TargetingFilter.#validateParameters(parameters);\r\n\r\n if (appContext === undefined) {\r\n throw new Error(\"The app context is required for targeting filter.\");\r\n }\r\n\r\n if (parameters.Audience.Exclusion !== undefined) {\r\n // check if the user is in the exclusion list\r\n if (appContext?.userId !== undefined &&\r\n parameters.Audience.Exclusion.Users !== undefined &&\r\n parameters.Audience.Exclusion.Users.includes(appContext.userId)) {\r\n return false;\r\n }\r\n // check if the user is in a group within exclusion list\r\n if (appContext?.groups !== undefined &&\r\n parameters.Audience.Exclusion.Groups !== undefined) {\r\n for (const excludedGroup of parameters.Audience.Exclusion.Groups) {\r\n if (appContext.groups.includes(excludedGroup)) {\r\n return false;\r\n }\r\n }\r\n }\r\n }\r\n\r\n // check if the user is being targeted directly\r\n if (appContext?.userId !== undefined &&\r\n parameters.Audience.Users !== undefined &&\r\n parameters.Audience.Users.includes(appContext.userId)) {\r\n return true;\r\n }\r\n\r\n // check if the user is in a group that is being targeted\r\n if (appContext?.groups !== undefined &&\r\n parameters.Audience.Groups !== undefined) {\r\n for (const group of parameters.Audience.Groups) {\r\n if (appContext.groups.includes(group.Name)) {\r\n const audienceContextId = constructAudienceContextId(featureName, appContext.userId, group.Name);\r\n const rolloutPercentage = group.RolloutPercentage;\r\n if (TargetingFilter.#isTargeted(audienceContextId, rolloutPercentage)) {\r\n return true;\r\n }\r\n }\r\n }\r\n }\r\n\r\n // check if the user is being targeted by a default rollout percentage\r\n const defaultContextId = constructAudienceContextId(featureName, appContext?.userId);\r\n return TargetingFilter.#isTargeted(defaultContextId, parameters.Audience.DefaultRolloutPercentage);\r\n }\r\n\r\n static #isTargeted(audienceContextId: string, rolloutPercentage: number): boolean {\r\n if (rolloutPercentage === 100) {\r\n return true;\r\n }\r\n // Cryptographic hashing algorithms ensure adequate entropy across hash values.\r\n const contextMarker = stringToUint32(audienceContextId);\r\n const contextPercentage = (contextMarker / 0xFFFFFFFF) * 100;\r\n return contextPercentage < rolloutPercentage;\r\n }\r\n\r\n static #validateParameters(parameters: TargetingFilterParameters): void {\r\n if (parameters.Audience.DefaultRolloutPercentage < 0 || parameters.Audience.DefaultRolloutPercentage > 100) {\r\n throw new Error(\"Audience.DefaultRolloutPercentage must be a number between 0 and 100.\");\r\n }\r\n // validate RolloutPercentage for each group\r\n if (parameters.Audience.Groups !== undefined) {\r\n for (const group of parameters.Audience.Groups) {\r\n if (group.RolloutPercentage < 0 || group.RolloutPercentage > 100) {\r\n throw new Error(`RolloutPercentage of group ${group.Name} must be a number between 0 and 100.`);\r\n }\r\n }\r\n }\r\n }\r\n}\r\n\r\n/**\r\n * Constructs the context id for the audience.\r\n * The context id is used to determine if the user is part of the audience for a feature.\r\n * If groupName is provided, the context id is constructed as follows:\r\n * userId + \"\\n\" + featureName + \"\\n\" + groupName\r\n * Otherwise, the context id is constructed as follows:\r\n * userId + \"\\n\" + featureName\r\n *\r\n * @param featureName name of the feature\r\n * @param userId userId from app context\r\n * @param groupName group name from app context\r\n * @returns a string that represents the context id for the audience\r\n */\r\nfunction constructAudienceContextId(featureName: string, userId: string | undefined, groupName?: string) {\r\n let contextId = `${userId ?? \"\"}\\n${featureName}`;\r\n if (groupName !== undefined) {\r\n contextId += `\\n${groupName}`;\r\n }\r\n return contextId\r\n}\r\n\r\nfunction stringToUint32(str: string): number {\r\n // Create a SHA-256 hash of the string\r\n const hash = createHash(\"sha256\").update(str).digest();\r\n\r\n // Get the first 4 bytes of the hash\r\n const first4Bytes = hash.subarray(0, 4);\r\n\r\n // Convert the 4 bytes to a uint32 with little-endian encoding\r\n const uint32 = first4Bytes.readUInt32LE(0);\r\n return uint32;\r\n}\r\n","// Copyright (c) Microsoft Corporation.\r\n// Licensed under the MIT license.\r\n\r\nimport { TimeWindowFilter } from \"./filter/TimeWindowFilter\";\r\nimport { IFeatureFilter } from \"./filter/FeatureFilter\";\r\nimport { RequirementType } from \"./model\";\r\nimport { IFeatureFlagProvider } from \"./featureProvider\";\r\nimport { TargetingFilter } from \"./filter/TargetingFilter\";\r\n\r\nexport class FeatureManager {\r\n #provider: IFeatureFlagProvider;\r\n #featureFilters: Map<string, IFeatureFilter> = new Map();\r\n\r\n constructor(provider: IFeatureFlagProvider, options?: FeatureManagerOptions) {\r\n this.#provider = provider;\r\n\r\n const builtinFilters = [new TimeWindowFilter(), new TargetingFilter()];\r\n\r\n // If a custom filter shares a name with an existing filter, the custom filter overrides the existing one.\r\n for (const filter of [...builtinFilters, ...(options?.customFilters ?? [])]) {\r\n this.#featureFilters.set(filter.name, filter);\r\n }\r\n }\r\n\r\n async listFeatureNames(): Promise<string[]> {\r\n const features = await this.#provider.getFeatureFlags();\r\n const featureNameSet = new Set(features.map((feature) => feature.id));\r\n return Array.from(featureNameSet);\r\n }\r\n\r\n // If multiple feature flags are found, the first one takes precedence.\r\n async isEnabled(featureName: string, context?: unknown): Promise<boolean> {\r\n const featureFlag = await this.#provider.getFeatureFlag(featureName);\r\n if (featureFlag === undefined) {\r\n // If the feature is not found, then it is disabled.\r\n return false;\r\n }\r\n\r\n if (featureFlag.enabled === false) {\r\n // If the feature is explicitly disabled, then it is disabled.\r\n return false;\r\n }\r\n\r\n const clientFilters = featureFlag.conditions?.client_filters;\r\n if (clientFilters === undefined || clientFilters.length <= 0) {\r\n // If there are no client filters, then the feature is enabled.\r\n return true;\r\n }\r\n\r\n const requirementType = featureFlag.conditions?.requirement_type ?? RequirementType.Any; // default to any.\r\n\r\n /**\r\n * While iterating through the client filters, we short-circuit the evaluation based on the requirement type.\r\n * - When requirement type is \"All\", the feature is enabled if all client filters are matched. If any client filter is not matched, the feature is disabled, otherwise it is enabled. `shortCircuitEvaluationResult` is false.\r\n * - When requirement type is \"Any\", the feature is enabled if any client filter is matched. If any client filter is matched, the feature is enabled, otherwise it is disabled. `shortCircuitEvaluationResult` is true.\r\n */\r\n const shortCircuitEvaluationResult: boolean = requirementType === RequirementType.Any;\r\n\r\n for (const clientFilter of clientFilters) {\r\n const matchedFeatureFilter = this.#featureFilters.get(clientFilter.name);\r\n const contextWithFeatureName = { featureName, parameters: clientFilter.parameters };\r\n if (matchedFeatureFilter === undefined) {\r\n console.warn(`Feature filter ${clientFilter.name} is not found.`);\r\n return false;\r\n }\r\n if (await matchedFeatureFilter.evaluate(contextWithFeatureName, context) === shortCircuitEvaluationResult) {\r\n return shortCircuitEvaluationResult;\r\n }\r\n }\r\n\r\n // If we get here, then we have not found a client filter that matches the requirement type.\r\n return !shortCircuitEvaluationResult;\r\n }\r\n\r\n}\r\n\r\ninterface FeatureManagerOptions {\r\n customFilters?: IFeatureFilter[];\r\n}\r\n","// Copyright (c) Microsoft Corporation.\r\n// Licensed under the MIT license.\r\n\r\nimport { IGettable } from \"./gettable\";\r\nimport { FeatureFlag, FeatureManagementConfiguration, FEATURE_MANAGEMENT_KEY, FEATURE_FLAGS_KEY } from \"./model\";\r\n\r\nexport interface IFeatureFlagProvider {\r\n /**\r\n * Get all feature flags.\r\n */\r\n getFeatureFlags(): Promise<FeatureFlag[]>;\r\n\r\n /**\r\n * Get a feature flag by name.\r\n * @param featureName The name of the feature flag.\r\n */\r\n getFeatureFlag(featureName: string): Promise<FeatureFlag | undefined>;\r\n}\r\n\r\n/**\r\n * A feature flag provider that uses a map-like configuration to provide feature flags.\r\n */\r\nexport class ConfigurationMapFeatureFlagProvider implements IFeatureFlagProvider {\r\n #configuration: IGettable;\r\n\r\n constructor(configuration: IGettable) {\r\n this.#configuration = configuration;\r\n }\r\n async getFeatureFlag(featureName: string): Promise<FeatureFlag | undefined> {\r\n const featureConfig = this.#configuration.get<FeatureManagementConfiguration>(FEATURE_MANAGEMENT_KEY);\r\n return featureConfig?.[FEATURE_FLAGS_KEY]?.find((feature) => feature.id === featureName);\r\n }\r\n\r\n async getFeatureFlags(): Promise<FeatureFlag[]> {\r\n const featureConfig = this.#configuration.get<FeatureManagementConfiguration>(FEATURE_MANAGEMENT_KEY);\r\n return featureConfig?.[FEATURE_FLAGS_KEY] ?? [];\r\n }\r\n}\r\n\r\n/**\r\n * A feature flag provider that uses an object-like configuration to provide feature flags.\r\n */\r\nexport class ConfigurationObjectFeatureFlagProvider implements IFeatureFlagProvider {\r\n #configuration: Record<string, unknown>;\r\n\r\n constructor(configuration: Record<string, unknown>) {\r\n this.#configuration = configuration;\r\n }\r\n\r\n async getFeatureFlag(featureName: string): Promise<FeatureFlag | undefined> {\r\n const featureFlags = this.#configuration[FEATURE_MANAGEMENT_KEY]?.[FEATURE_FLAGS_KEY];\r\n return featureFlags?.find((feature: FeatureFlag) => feature.id === featureName);\r\n }\r\n\r\n async getFeatureFlags(): Promise<FeatureFlag[]> {\r\n return this.#configuration[FEATURE_MANAGEMENT_KEY]?.[FEATURE_FLAGS_KEY] ?? [];\r\n }\r\n}\r\n"],"names":["createHash"],"mappings":";;;;AAAA;AACA;MAea,gBAAgB,CAAA;IACzB,IAAI,GAAW,sBAAsB,CAAC;AAEtC,IAAA,QAAQ,CAAC,OAA0C,EAAA;AAC/C,QAAA,MAAM,EAAC,WAAW,EAAE,UAAU,EAAC,GAAG,OAAO,CAAC;QAC1C,MAAM,SAAS,GAAG,UAAU,CAAC,KAAK,KAAK,SAAS,GAAG,IAAI,IAAI,CAAC,UAAU,CAAC,KAAK,CAAC,GAAG,SAAS,CAAC;QAC1F,MAAM,OAAO,GAAG,UAAU,CAAC,GAAG,KAAK,SAAS,GAAG,IAAI,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,GAAG,SAAS,CAAC;QAEpF,IAAI,SAAS,KAAK,SAAS,IAAI,OAAO,KAAK,SAAS,EAAE;;YAElD,OAAO,CAAC,IAAI,CAAC,CAAO,IAAA,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TimeWindowFilter } from \"./filter/TimeWindowFilter\";\r\nimport { IFeatureFilter } from \"./filter/FeatureFilter\";\r\nimport { RequirementType } from \"./model\";\r\nimport { IFeatureFlagProvider } from \"./featureProvider\";\r\nimport { TargetingFilter } from \"./filter/TargetingFilter\";\r\n\r\nexport class FeatureManager {\r\n #provider: IFeatureFlagProvider;\r\n #featureFilters: Map<string, IFeatureFilter> = new Map();\r\n\r\n constructor(provider: IFeatureFlagProvider, options?: FeatureManagerOptions) {\r\n this.#provider = provider;\r\n\r\n const builtinFilters = [new TimeWindowFilter(), new TargetingFilter()];\r\n\r\n // If a custom filter shares a name with an existing filter, the custom filter overrides the existing one.\r\n for (const filter of [...builtinFilters, ...(options?.customFilters ?? [])]) {\r\n this.#featureFilters.set(filter.name, filter);\r\n }\r\n }\r\n\r\n async listFeatureNames(): Promise<string[]> {\r\n const features = await this.#provider.getFeatureFlags();\r\n const featureNameSet = new Set(features.map((feature) => feature.id));\r\n return Array.from(featureNameSet);\r\n }\r\n\r\n // If multiple feature flags are found, the first one takes precedence.\r\n async isEnabled(featureName: string, context?: unknown): Promise<boolean> {\r\n const featureFlag = await this.#provider.getFeatureFlag(featureName);\r\n if (featureFlag === undefined) {\r\n // If the feature is not found, then it is disabled.\r\n return false;\r\n }\r\n\r\n if (featureFlag.enabled === false) {\r\n // If the feature is explicitly disabled, then it is disabled.\r\n return false;\r\n }\r\n\r\n const clientFilters = featureFlag.conditions?.client_filters;\r\n if (clientFilters === undefined || clientFilters.length <= 0) {\r\n // If there are no client filters, then the feature is enabled.\r\n return true;\r\n }\r\n\r\n const requirementType = featureFlag.conditions?.requirement_type ?? RequirementType.Any; // default to any.\r\n\r\n /**\r\n * While iterating through the client filters, we short-circuit the evaluation based on the requirement type.\r\n * - When requirement type is \"All\", the feature is enabled if all client filters are matched. If any client filter is not matched, the feature is disabled, otherwise it is enabled. `shortCircuitEvaluationResult` is false.\r\n * - When requirement type is \"Any\", the feature is enabled if any client filter is matched. If any client filter is matched, the feature is enabled, otherwise it is disabled. `shortCircuitEvaluationResult` is true.\r\n */\r\n const shortCircuitEvaluationResult: boolean = requirementType === RequirementType.Any;\r\n\r\n for (const clientFilter of clientFilters) {\r\n const matchedFeatureFilter = this.#featureFilters.get(clientFilter.name);\r\n const contextWithFeatureName = { featureName, parameters: clientFilter.parameters };\r\n if (matchedFeatureFilter === undefined) {\r\n console.warn(`Feature filter ${clientFilter.name} is not found.`);\r\n return false;\r\n }\r\n if (await matchedFeatureFilter.evaluate(contextWithFeatureName, context) === shortCircuitEvaluationResult) {\r\n return shortCircuitEvaluationResult;\r\n }\r\n }\r\n\r\n // If we get here, then we have not found a client filter that matches the requirement type.\r\n return !shortCircuitEvaluationResult;\r\n }\r\n\r\n}\r\n\r\ninterface FeatureManagerOptions {\r\n customFilters?: IFeatureFilter[];\r\n}\r\n"]}
@@ -1 +0,0 @@
1
- {"version":3,"file":"featureProvider.js","sourceRoot":"","sources":["../src/featureProvider.ts"],"names":[],"mappings":"AAAA,uCAAuC;AACvC,kCAAkC;AAGlC,OAAO,EAA+C,sBAAsB,EAAE,iBAAiB,EAAE,MAAM,SAAS,CAAC;AAejH;;GAEG;AACH,MAAM,OAAO,mCAAmC;IAC5C,cAAc,CAAY;IAE1B,YAAY,aAAwB;QAChC,IAAI,CAAC,cAAc,GAAG,aAAa,CAAC;IACxC,CAAC;IACD,KAAK,CAAC,cAAc,CAAC,WAAmB;QACpC,MAAM,aAAa,GAAG,IAAI,CAAC,cAAc,CAAC,GAAG,CAAiC,sBAAsB,CAAC,CAAC;QACtG,OAAO,aAAa,EAAE,CAAC,iBAAiB,CAAC,EAAE,IAAI,CAAC,CAAC,OAAO,EAAE,EAAE,CAAC,OAAO,CAAC,EAAE,KAAK,WAAW,CAAC,CAAC;IAC7F,CAAC;IAED,KAAK,CAAC,eAAe;QACjB,MAAM,aAAa,GAAG,IAAI,CAAC,cAAc,CAAC,GAAG,CAAiC,sBAAsB,CAAC,CAAC;QACtG,OAAO,aAAa,EAAE,CAAC,iBAAiB,CAAC,IAAI,EAAE,CAAC;IACpD,CAAC;CACJ;AAED;;GAEG;AACH,MAAM,OAAO,sCAAsC;IAC/C,cAAc,CAA0B;IAExC,YAAY,aAAsC;QAC9C,IAAI,CAAC,cAAc,GAAG,aAAa,CAAC;IACxC,CAAC;IAED,KAAK,CAAC,cAAc,CAAC,WAAmB;QACpC,MAAM,YAAY,GAAG,IAAI,CAAC,cAAc,CAAC,sBAAsB,CAAC,EAAE,CAAC,iBAAiB,CAAC,CAAC;QACtF,OAAO,YAAY,EAAE,IAAI,CAAC,CAAC,OAAoB,EAAE,EAAE,CAAC,OAAO,CAAC,EAAE,KAAK,WAAW,CAAC,CAAC;IACpF,CAAC;IAED,KAAK,CAAC,eAAe;QACjB,OAAO,IAAI,CAAC,cAAc,CAAC,sBAAsB,CAAC,EAAE,CAAC,iBAAiB,CAAC,IAAI,EAAE,CAAC;IAClF,CAAC;CACJ","sourcesContent":["// Copyright (c) Microsoft Corporation.\r\n// Licensed under the MIT license.\r\n\r\nimport { IGettable } from \"./gettable\";\r\nimport { FeatureFlag, FeatureManagementConfiguration, FEATURE_MANAGEMENT_KEY, FEATURE_FLAGS_KEY } from \"./model\";\r\n\r\nexport interface IFeatureFlagProvider {\r\n /**\r\n * Get all feature flags.\r\n */\r\n getFeatureFlags(): Promise<FeatureFlag[]>;\r\n\r\n /**\r\n * Get a feature flag by name.\r\n * @param featureName The name of the feature flag.\r\n */\r\n getFeatureFlag(featureName: string): Promise<FeatureFlag | undefined>;\r\n}\r\n\r\n/**\r\n * A feature flag provider that uses a map-like configuration to provide feature flags.\r\n */\r\nexport class ConfigurationMapFeatureFlagProvider implements IFeatureFlagProvider {\r\n #configuration: IGettable;\r\n\r\n constructor(configuration: IGettable) {\r\n this.#configuration = configuration;\r\n }\r\n async getFeatureFlag(featureName: string): Promise<FeatureFlag | undefined> {\r\n const featureConfig = this.#configuration.get<FeatureManagementConfiguration>(FEATURE_MANAGEMENT_KEY);\r\n return featureConfig?.[FEATURE_FLAGS_KEY]?.find((feature) => feature.id === featureName);\r\n }\r\n\r\n async getFeatureFlags(): Promise<FeatureFlag[]> {\r\n const featureConfig = this.#configuration.get<FeatureManagementConfiguration>(FEATURE_MANAGEMENT_KEY);\r\n return featureConfig?.[FEATURE_FLAGS_KEY] ?? [];\r\n }\r\n}\r\n\r\n/**\r\n * A feature flag provider that uses an object-like configuration to provide feature flags.\r\n */\r\nexport class ConfigurationObjectFeatureFlagProvider implements IFeatureFlagProvider {\r\n #configuration: Record<string, unknown>;\r\n\r\n constructor(configuration: Record<string, unknown>) {\r\n this.#configuration = configuration;\r\n }\r\n\r\n async getFeatureFlag(featureName: string): Promise<FeatureFlag | undefined> {\r\n const featureFlags = this.#configuration[FEATURE_MANAGEMENT_KEY]?.[FEATURE_FLAGS_KEY];\r\n return featureFlags?.find((feature: FeatureFlag) => feature.id === featureName);\r\n }\r\n\r\n async getFeatureFlags(): Promise<FeatureFlag[]> {\r\n return this.#configuration[FEATURE_MANAGEMENT_KEY]?.[FEATURE_FLAGS_KEY] ?? [];\r\n }\r\n}\r\n"]}
@@ -1,4 +0,0 @@
1
- // Copyright (c) Microsoft Corporation.
2
- // Licensed under the MIT license.
3
- export {};
4
- //# sourceMappingURL=FeatureFilter.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"FeatureFilter.js","sourceRoot":"","sources":["../../src/filter/FeatureFilter.ts"],"names":[],"mappings":"AAAA,uCAAuC;AACvC,kCAAkC","sourcesContent":["// Copyright (c) Microsoft Corporation.\r\n// Licensed under the MIT license.\r\n\r\nexport interface IFeatureFilter {\r\n name: string; // e.g. Microsoft.TimeWindow\r\n evaluate(context: IFeatureFilterEvaluationContext, appContext?: unknown): boolean | Promise<boolean>;\r\n}\r\n\r\nexport interface IFeatureFilterEvaluationContext {\r\n featureName: string;\r\n parameters?: unknown;\r\n}\r\n"]}
@@ -1 +0,0 @@
1
- {"version":3,"file":"TargetingFilter.js","sourceRoot":"","sources":["../../src/filter/Targ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AC,CAAC;IAC3C,OAAO,MAAM,CAAC;AAClB,CAAC","sourcesContent":["// Copyright (c) Microsoft Corporation.\r\n// Licensed under the MIT license.\r\n\r\nimport { IFeatureFilter } from \"./FeatureFilter\";\r\nimport { createHash } from \"crypto\";\r\n\r\ntype TargetingFilterParameters = {\r\n Audience: {\r\n DefaultRolloutPercentage: number;\r\n Users?: string[];\r\n Groups?: {\r\n Name: string;\r\n RolloutPercentage: number;\r\n }[];\r\n Exclusion?: {\r\n Users?: string[];\r\n Groups?: string[];\r\n };\r\n }\r\n}\r\n\r\ntype TargetingFilterEvaluationContext = {\r\n featureName: string;\r\n parameters: TargetingFilterParameters;\r\n}\r\n\r\ntype TargetingFilterAppContext = {\r\n userId?: string;\r\n groups?: string[];\r\n}\r\n\r\nexport class TargetingFilter implements IFeatureFilter {\r\n name: string = \"Microsoft.Targeting\";\r\n\r\n evaluate(context: TargetingFilterEvaluationContext, appContext?: TargetingFilterAppContext): boolean {\r\n const { featureName, parameters } = context;\r\n TargetingFilter.#validateParameters(parameters);\r\n\r\n if (appContext === undefined) {\r\n throw new Error(\"The app context is required for targeting filter.\");\r\n }\r\n\r\n if (parameters.Audience.Exclusion !== undefined) {\r\n // check if the user is in the exclusion list\r\n if (appContext?.userId !== undefined &&\r\n parameters.Audience.Exclusion.Users !== undefined &&\r\n parameters.Audience.Exclusion.Users.includes(appContext.userId)) {\r\n return false;\r\n }\r\n // check if the user is in a group within exclusion list\r\n if (appContext?.groups !== undefined &&\r\n parameters.Audience.Exclusion.Groups !== undefined) {\r\n for (const excludedGroup of parameters.Audience.Exclusion.Groups) {\r\n if (appContext.groups.includes(excludedGroup)) {\r\n return false;\r\n }\r\n }\r\n }\r\n }\r\n\r\n // check if the user is being targeted directly\r\n if (appContext?.userId !== undefined &&\r\n parameters.Audience.Users !== undefined &&\r\n parameters.Audience.Users.includes(appContext.userId)) {\r\n return true;\r\n }\r\n\r\n // check if the user is in a group that is being targeted\r\n if (appContext?.groups !== undefined &&\r\n parameters.Audience.Groups !== undefined) {\r\n for (const group of parameters.Audience.Groups) {\r\n if (appContext.groups.includes(group.Name)) {\r\n const audienceContextId = constructAudienceContextId(featureName, appContext.userId, group.Name);\r\n const rolloutPercentage = group.RolloutPercentage;\r\n if (TargetingFilter.#isTargeted(audienceContextId, rolloutPercentage)) {\r\n return true;\r\n }\r\n }\r\n }\r\n }\r\n\r\n // check if the user is being targeted by a default rollout percentage\r\n const defaultContextId = constructAudienceContextId(featureName, appContext?.userId);\r\n return TargetingFilter.#isTargeted(defaultContextId, parameters.Audience.DefaultRolloutPercentage);\r\n }\r\n\r\n static #isTargeted(audienceContextId: string, rolloutPercentage: number): boolean {\r\n if (rolloutPercentage === 100) {\r\n return true;\r\n }\r\n // Cryptographic hashing algorithms ensure adequate entropy across hash values.\r\n const contextMarker = stringToUint32(audienceContextId);\r\n const contextPercentage = (contextMarker / 0xFFFFFFFF) * 100;\r\n return contextPercentage < rolloutPercentage;\r\n }\r\n\r\n static #validateParameters(parameters: TargetingFilterParameters): void {\r\n if (parameters.Audience.DefaultRolloutPercentage < 0 || parameters.Audience.DefaultRolloutPercentage > 100) {\r\n throw new Error(\"Audience.DefaultRolloutPercentage must be a number between 0 and 100.\");\r\n }\r\n // validate RolloutPercentage for each group\r\n if (parameters.Audience.Groups !== undefined) {\r\n for (const group of parameters.Audience.Groups) {\r\n if (group.RolloutPercentage < 0 || group.RolloutPercentage > 100) {\r\n throw new Error(`RolloutPercentage of group ${group.Name} must be a number between 0 and 100.`);\r\n }\r\n }\r\n }\r\n }\r\n}\r\n\r\n/**\r\n * Constructs the context id for the audience.\r\n * The context id is used to determine if the user is part of the audience for a feature.\r\n * If groupName is provided, the context id is constructed as follows:\r\n * userId + \"\\n\" + featureName + \"\\n\" + groupName\r\n * Otherwise, the context id is constructed as follows:\r\n * userId + \"\\n\" + featureName\r\n *\r\n * @param featureName name of the feature\r\n * @param userId userId from app context\r\n * @param groupName group name from app context\r\n * @returns a string that represents the context id for the audience\r\n */\r\nfunction constructAudienceContextId(featureName: string, userId: string | undefined, groupName?: string) {\r\n let contextId = `${userId ?? \"\"}\\n${featureName}`;\r\n if (groupName !== undefined) {\r\n contextId += `\\n${groupName}`;\r\n }\r\n return contextId\r\n}\r\n\r\nfunction stringToUint32(str: string): number {\r\n // Create a SHA-256 hash of the string\r\n const hash = createHash(\"sha256\").update(str).digest();\r\n\r\n // Get the first 4 bytes of the hash\r\n const first4Bytes = hash.subarray(0, 4);\r\n\r\n // Convert the 4 bytes to a uint32 with little-endian encoding\r\n const uint32 = first4Bytes.readUInt32LE(0);\r\n return uint32;\r\n}\r\n"]}
@@ -1,6 +0,0 @@
1
- // Copyright (c) Microsoft Corporation.
2
- // Licensed under the MIT license.
3
- export function isGettable(object) {
4
- return typeof object === "object" && object !== null && typeof object.get === "function";
5
- }
6
- //# sourceMappingURL=gettable.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"gettable.js","sourceRoot":"","sources":["../src/gettable.ts"],"names":[],"mappings":"AAAA,uCAAuC;AACvC,kCAAkC;AAMlC,MAAM,UAAU,UAAU,CAAC,MAAe;IACtC,OAAO,OAAO,MAAM,KAAK,QAAQ,IAAI,MAAM,KAAK,IAAI,IAAI,OAAQ,MAAoB,CAAC,GAAG,KAAK,UAAU,CAAC;AAC5G,CAAC","sourcesContent":["// Copyright (c) Microsoft Corporation.\r\n// Licensed under the MIT license.\r\n\r\nexport interface IGettable {\r\n get<T>(key: string): T | undefined;\r\n}\r\n\r\nexport function isGettable(object: unknown): object is IGettable {\r\n return typeof object === \"object\" && object !== null && typeof (object as IGettable).get === \"function\";\r\n}\n"]}
package/dist-esm/index.js DELETED
@@ -1,5 +0,0 @@
1
- // Copyright (c) Microsoft Corporation.
2
- // Licensed under the MIT license.
3
- export { FeatureManager } from "./featureManager";
4
- export { ConfigurationMapFeatureFlagProvider, ConfigurationObjectFeatureFlagProvider } from "./featureProvider";
5
- //# sourceMappingURL=index.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,uCAAuC;AACvC,kCAAkC;AAElC,OAAO,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AAClD,OAAO,EAAE,mCAAmC,EAAE,sCAAsC,EAAwB,MAAM,mBAAmB,CAAC","sourcesContent":["// Copyright (c) Microsoft Corporation.\r\n// Licensed under the MIT license.\r\n\r\nexport { FeatureManager } from \"./featureManager\";\r\nexport { ConfigurationMapFeatureFlagProvider, ConfigurationObjectFeatureFlagProvider, IFeatureFlagProvider } from \"./featureProvider\";\n"]}
package/dist-esm/model.js DELETED
@@ -1,12 +0,0 @@
1
- // Copyright (c) Microsoft Corporation.
2
- // Licensed under the MIT license.
3
- export var RequirementType;
4
- (function (RequirementType) {
5
- RequirementType["Any"] = "Any";
6
- RequirementType["All"] = "All";
7
- })(RequirementType || (RequirementType = {}));
8
- // Feature Management Section fed into feature manager.
9
- // Converted from https://github.com/Azure/AppConfiguration/blob/main/docs/FeatureManagement/FeatureManagement.v1.0.0.schema.json
10
- export const FEATURE_MANAGEMENT_KEY = "feature_management";
11
- export const FEATURE_FLAGS_KEY = "feature_flags";
12
- //# sourceMappingURL=model.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"model.js","sourceRoot":"","sources":["../src/model.ts"],"names":[],"mappings":"AAAA,uCAAuC;AACvC,kCAAkC;AA+BlC,MAAM,CAAN,IAAY,eAGX;AAHD,WAAY,eAAe;IACzB,8BAAW,CAAA;IACX,8BAAW,CAAA;AACb,CAAC,EAHW,eAAe,KAAf,eAAe,QAG1B;AAyBD,uDAAuD;AACvD,iIAAiI;AAEjI,MAAM,CAAC,MAAM,sBAAsB,GAAG,oBAAoB,CAAA;AAC1D,MAAM,CAAC,MAAM,iBAAiB,GAAG,eAAe,CAAA","sourcesContent":["// Copyright (c) Microsoft Corporation.\r\n// Licensed under the MIT license.\r\n\r\n// Converted from https://github.com/Azure/AppConfiguration/blob/main/docs/FeatureManagement/FeatureFlag.v1.1.0.schema.json\r\n\r\nexport interface FeatureFlag {\r\n /**\r\n * An ID used to uniquely identify and reference the feature.\r\n */\r\n id: string\r\n\r\n /**\r\n * A description of the feature.\r\n */\r\n description?: string\r\n\r\n /**\r\n * A display name for the feature to use for display rather than the ID.\r\n */\r\n display_name?: string\r\n\r\n /**\r\n * A feature is OFF if enabled is false. If enabled is true, then the feature is ON if there are no conditions (null or empty) or if the conditions are satisfied.\r\n */\r\n enabled: boolean\r\n\r\n /**\r\n * The declaration of conditions used to dynamically enable features.\r\n */\r\n conditions?: FeatureEnablementConditions\r\n}\r\n\r\nexport enum RequirementType {\r\n Any = \"Any\",\r\n All = \"All\"\r\n}\r\n\r\nexport interface FeatureEnablementConditions {\r\n /**\r\n * Determines whether any or all registered client filters must be evaluated as true for the feature to be considered enabled.\r\n */\r\n requirement_type?: RequirementType\r\n\r\n /**\r\n * Filters that must run on the client and be evaluated as true for the feature to be considered enabled.\r\n */\r\n client_filters?: ClientFilter[]\r\n}\r\n\r\nexport interface ClientFilter {\r\n /**\r\n * The name used to refer to and require a client filter.\r\n */\r\n name: string\r\n /**\r\n * Custom parameters for a given client filter. A client filter can require any set of parameters of any type.\r\n */\r\n parameters?: unknown\r\n}\r\n\r\n// Feature Management Section fed into feature manager.\r\n// Converted from https://github.com/Azure/AppConfiguration/blob/main/docs/FeatureManagement/FeatureManagement.v1.0.0.schema.json\r\n\r\nexport const FEATURE_MANAGEMENT_KEY = \"feature_management\"\r\nexport const FEATURE_FLAGS_KEY = \"feature_flags\"\r\n\r\nexport interface FeatureManagementConfiguration {\r\n feature_management: FeatureManagement\r\n}\r\n\r\n/**\r\n * Declares feature management configuration.\r\n */\r\nexport interface FeatureManagement {\r\n feature_flags: FeatureFlag[];\r\n}\r\n"]}