forked from launchdarkly/node-server-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevaluate_flag.js
More file actions
284 lines (246 loc) · 7.16 KB
/
Copy pathevaluate_flag.js
File metadata and controls
284 lines (246 loc) · 7.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
var operators = require('./operators');
var util = require('util');
var sha1 = require('node-sha1');
var async = require('async');
var builtins = ['key', 'ip', 'country', 'email', 'firstName', 'lastName', 'avatar', 'name', 'anonymous'];
var noop = function(){};
function evaluate(flag, user, store, cb) {
cb = cb || noop;
if (!user || user.key === null || user.key === undefined) {
cb(null, null, null);
return;
}
if (!flag) {
cb(null, null, null);
return;
}
if (!flag.on) {
// Return the off variation if defined and valid
if (flag.offVariation != null) {
cb(null, get_variation(flag, flag.offVariation), null);
}
// Otherwise, return the default variation
else {
cb(null, null, null);
}
return;
}
eval_internal(flag, user, store, [], function(err, result, events) {
if (err) {
cb(err, result, events);
return;
}
if (result === null) {
// Return the off variation if defined and valid
if (flag.offVariation != null) {
cb(null, get_variation(flag, flag.offVariation), null);
}
// Otherwise, return the default variation
else {
cb(null, null, null);
}
} else {
cb(err, result, events);
}
});
return;
}
function eval_internal(flag, user, store, events, cb) {
// Evaluate prerequisites, if any
if (flag.prerequisites) {
async.mapSeries(flag.prerequisites,
function(prereq, callback) {
store.get(prereq.key, function(f) {
// If the flag does not exist in the store or is not on, the prerequisite
// is not satisfied
if (!f || !f.on) {
callback(new Error("Unsatisfied prerequisite"), null);
return;
}
eval_internal(f, user, store, events, function(err, value) {
// If there was an error, the value is null, the variation index is out of range,
// or the value does not match the indexed variation the prerequisite is not satisfied
var variation = get_variation(f, prereq.variation);
events.push(create_flag_event(f.key, user, value, null, f.version, flag.key));
if (err || value === null || variation === null || value != variation) {
callback(new Error("Unsatisfied prerequisite"), null)
} else {
// The prerequisite was satisfied
callback(null, null);
}
});
});
},
function(err, results) {
// If the error is that prerequisites weren't satisfied, we don't return an error,
// because we want to serve the 'offVariation'
if (err) {
cb(null, null, events);
return;
}
evalRules(flag, user, function(e, variation) {
cb(e, variation, events);
});
})
} else {
evalRules(flag, user, function(e, variation) {
cb(e, variation, events);
});
}
}
function evalRules(flag, user, cb) {
var i, j;
var target;
var variation;
var rule;
// Check target matches
for (i = 0; i < flag.targets.length; i++) {
target = flag.targets[i];
if (!target.values) {
continue;
}
for (j = 0; j < target.values.length; j++) {
if (user.key === target.values[j]) {
variation = get_variation(flag, target.variation);
cb(variation === null ? new Error("Undefined variation for flag " + flag.key) : null, variation);
return;
}
}
}
// Check rules
for (i = 0; i < flag.rules.length; i++) {
rule = flag.rules[i];
if (rule_match_user(rule, user)) {
variation = variation_for_user(rule, user, flag);
cb(variation === null ? new Error("Undefined variation for flag " + flag.key) : null, variation);
return;
}
}
// Check the fallthrough
variation = variation_for_user(flag.fallthrough, user, flag);
cb(variation === null ? new Error("Undefined variation for flag " + flag.key) : null, variation);
}
function rule_match_user(r, user) {
var i;
if (!r.clauses) {
return false;
}
// A rule matches if all its clauses match
for (i = 0; i < r.clauses.length; i++) {
if (!clause_match_user(r.clauses[i], user)) {
return false;
}
}
return true;
}
function clause_match_user(c, user) {
var uValue;
var matchFn;
var i;
uValue = user_value(user, c.attribute);
if (uValue === null || uValue === undefined) {
return false;
}
matchFn = operators.fn(c.op)
// The user's value is an array
if (Array === uValue.constructor) {
for (i = 0; i < uValue.length; i++) {
if (match_any(matchFn, uValue[i], c.values)) {
return maybe_negate(c, true);
}
}
return maybe_negate(c, false);
}
return maybe_negate(c, match_any(matchFn, uValue, c.values));
}
function maybe_negate(c, b) {
if (c.negate) {
return !b;
} else {
return b;
}
}
function match_any(matchFn, value, values) {
var i = 0;
for (i = 0; i < values.length; i++) {
if (matchFn(value, values[i])) {
return true;
}
}
return false;
}
// Given an index, return the variation value, or null if
// the index is invalid
function get_variation(flag, index) {
if (index >= flag.variations.length) {
return null;
} else {
return flag.variations[index];
}
}
// Given a variation or rollout 'r', select
// the variation for the given user
function variation_for_user(r, user, flag) {
var bucketBy;
var bucket;
var sum = 0;
var i;
var variation;
if (r.variation != null) {
// This represets a fixed variation; return it
return get_variation(flag, r.variation);
} else if (r.rollout != null) {
// This represents a percentage rollout. Assume
// we're rolling out by key
bucketBy = r.rollout.bucketBy != null ? r.rollout.bucketBy : "key";
bucket = bucket_user(user, flag.key, bucketBy, flag.salt);
for (i = 0; i < r.rollout.variations.length; i++) {
variate = r.rollout.variations[i];
sum += variate.weight / 100000.0;
if (bucket < sum) {
return get_variation(flag, variate.variation);
}
}
}
return null;
}
// Fetch an attribute value from a user object. Automatically
// navigates into the custom array when necessary
function user_value(user, attr) {
if (builtins.indexOf(attr) >= 0 && user.hasOwnProperty(attr)) {
return user[attr];
}
if (user.custom && user.custom.hasOwnProperty(attr)) {
return user.custom[attr];
}
return null;
}
// Compute a percentile for a user
function bucket_user(user, key, attr, salt) {
var uValue;
var idHash;
idHash = user_value(user, attr);
if (idHash === null) {
return 0;
}
if (user.secondary) {
idHash += "." + user.secondary;
}
hashKey = util.format("%s.%s.%s", key, salt, idHash);
hashVal = parseInt(sha1(hashKey).substring(0,15), 16);
result = hashVal / 0xFFFFFFFFFFFFFFF;
return result;
}
function create_flag_event(key, user, value, default_val, version, prereqOf) {
return {
"kind": "feature",
"key": key,
"user": user,
"value": value,
"default": default_val,
"creationDate": new Date().getTime(),
"version": version,
"prereqOf": prereqOf
};
}
module.exports = {evaluate: evaluate, create_flag_event: create_flag_event};