forked from launchdarkly/node-server-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevaluate_flag.js
356 lines (313 loc) · 9.62 KB
/
evaluate_flag.js
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
var operators = require('./operators');
var dataKind = require('./versioned_data_kind');
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, featureStore, cb) {
cb = cb || noop;
if (!user || user.key === null || user.key === undefined) {
cb(null, null, null, null);
return;
}
if (!flag) {
cb(null, null, null, null);
return;
}
if (!flag.on) {
// Return the off variation if defined and valid
cb(null, flag.offVariation, getVariation(flag, flag.offVariation), null);
return;
}
evalInternal(flag, user, featureStore, [], function(err, variation, value, events) {
if (err) {
cb(err, variation, value, events);
return;
}
if (variation === null) {
// Return the off variation if defined and valid
cb(null, flag.offVariation, getVariation(flag, flag.offVariation), events);
} else {
cb(err, variation, value, events);
}
});
return;
}
function evalInternal(flag, user, featureStore, events, cb) {
// Evaluate prerequisites, if any
if (flag.prerequisites) {
async.mapSeries(flag.prerequisites,
function(prereq, callback) {
featureStore.get(dataKind.features, 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;
}
evalInternal(f, user, featureStore, events, function(err, variation, 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
events.push(createFlagEvent(f.key, f, user, variation, value, null, flag.key));
if (err || value === null || variation != prereq.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, null, events);
return;
}
evalRules(flag, user, featureStore, function(e, variation, value) {
cb(e, variation, value, events);
});
})
} else {
evalRules(flag, user, featureStore, function(e, variation, value) {
cb(e, variation, value, events);
});
}
}
function evalRules(flag, user, featureStore, 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]) {
value = getVariation(flag, target.variation);
cb(value === null ? new Error("Undefined variation for flag " + flag.key) : null,
target.variation, value);
return;
}
}
}
async.mapSeries(flag.rules,
function(rule, callback) {
ruleMatchUser(rule, user, featureStore, function(matched) {
setImmediate(callback, matched ? rule : null, null);
});
},
function(err, results) {
// we use the "error" value to indicate that a rule was successfully matched (since we only care
// about the first match, and mapSeries terminates on the first "error")
if (err) {
var rule = err;
variation = variationForUser(rule, user, flag);
} else {
// no rule matched; check the fallthrough
variation = variationForUser(flag.fallthrough, user, flag);
}
cb(variation === null ? new Error("Undefined variation for flag " + flag.key) : null,
variation, getVariation(flag, variation));
}
);
}
function ruleMatchUser(r, user, featureStore, cb) {
var i;
if (!r.clauses) {
return false;
}
// A rule matches if all its clauses match
async.mapSeries(r.clauses,
function(clause, callback) {
clauseMatchUser(clause, user, featureStore, function(matched) {
// on the first clause that does *not* match, we raise an "error" to stop the loop
setImmediate(callback, matched ? null : clause, null);
});
},
function(err, results) {
cb(!err);
}
);
}
function clauseMatchUser(c, user, featureStore, cb) {
if (c.op == 'segmentMatch') {
async.mapSeries(c.values,
function(value, callback) {
featureStore.get(dataKind.segments, value, function(segment) {
if (segment && segmentMatchUser(segment, user)) {
// on the first segment that matches, we raise an "error" to stop the loop
callback(segment, null);
} else {
callback(null, null);
}
});
},
function(err, results) {
// an "error" indicates that a segment *did* match
cb(maybeNegate(c, !!err));
}
);
} else {
cb(clauseMatchUserNoSegments(c, user));
}
}
function clauseMatchUserNoSegments(c, user) {
var uValue;
var matchFn;
var i;
uValue = userValue(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 (matchAny(matchFn, uValue[i], c.values)) {
return maybeNegate(c, true);
}
}
return maybeNegate(c, false);
}
return maybeNegate(c, matchAny(matchFn, uValue, c.values));
}
function segmentMatchUser(segment, user) {
if (user.key) {
if ((segment.included || []).indexOf(user.key) >= 0) {
return true;
}
if ((segment.excluded || []).indexOf(user.key) >= 0) {
return false;
}
for (var i = 0; i < (segment.rules || []).length; i++) {
if (segmentRuleMatchUser(segment.rules[i], user, segment.key, segment.salt)) {
return true;
}
}
}
return false;
}
function segmentRuleMatchUser(rule, user, segmentKey, salt) {
for (var i = 0; i < (rule.clauses || []).length; i++) {
if (!clauseMatchUserNoSegments(rule.clauses[i], user)) {
return false;
}
}
// If the weight is absent, this rule matches
if (rule.weight === undefined || rule.weight === null) {
return true;
}
// All of the clauses are met. See if the user buckets in
var bucket = bucketUser(user, segmentKey, rule.bucketBy || "key", salt);
var weight = rule.weight / 100000.0;
return bucket < weight;
}
function maybeNegate(c, b) {
if (c.negate) {
return !b;
} else {
return b;
}
}
function matchAny(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 getVariation(flag, index) {
if (index === null || index === undefined || 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 variationForUser(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 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 = bucketUser(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 variate.variation;
}
}
}
return null;
}
// Fetch an attribute value from a user object. Automatically
// navigates into the custom array when necessary
function userValue(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 bucketUser(user, key, attr, salt) {
var uValue;
var idHash;
idHash = bucketableStringValue(userValue(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 bucketableStringValue(value) {
if (typeof(value) === 'string') {
return value;
}
if (Number.isInteger(value)) {
return '' + value;
}
return null;
}
function createFlagEvent(key, flag, user, variation, value, defaultVal, prereqOf) {
return {
"kind": "feature",
"key": key,
"user": user,
"variation": variation,
"value": value,
"default": defaultVal,
"creationDate": new Date().getTime(),
"version": flag ? flag.version : null,
"prereqOf": prereqOf,
"trackEvents": flag ? flag.trackEvents : null,
"debugEventsUntilDate": flag ? flag.debugEventsUntilDate : null
};
}
module.exports = {evaluate: evaluate, bucketUser: bucketUser, createFlagEvent: createFlagEvent};