@depup/launchdarkly-node-server-sdk 7.0.4-depup.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.
- package/.babelrc +16 -0
- package/.circleci/config.yml +89 -0
- package/.eslintignore +5 -0
- package/.eslintrc.yaml +114 -0
- package/.github/ISSUE_TEMPLATE/bug_report.md +37 -0
- package/.github/ISSUE_TEMPLATE/config.yml +5 -0
- package/.github/ISSUE_TEMPLATE/feature_request.md +20 -0
- package/.github/pull_request_template.md +21 -0
- package/.github/workflows/stale.yml +8 -0
- package/.hound.yml +33 -0
- package/.ldrelease/config.yml +28 -0
- package/.prettierrc +6 -0
- package/CHANGELOG.md +603 -0
- package/CODEOWNERS +2 -0
- package/CONTRIBUTING.md +55 -0
- package/LICENSE.txt +13 -0
- package/README.md +36 -0
- package/SECURITY.md +5 -0
- package/attribute_reference.js +217 -0
- package/big_segments.js +117 -0
- package/caching_store_wrapper.js +240 -0
- package/changes.json +30 -0
- package/configuration.js +235 -0
- package/context.js +98 -0
- package/context_filter.js +137 -0
- package/contract-tests/README.md +7 -0
- package/contract-tests/index.js +109 -0
- package/contract-tests/log.js +23 -0
- package/contract-tests/package.json +15 -0
- package/contract-tests/sdkClientEntity.js +110 -0
- package/contract-tests/testharness-suppressions.txt +2 -0
- package/diagnostic_events.js +151 -0
- package/docs/typedoc.js +10 -0
- package/errors.js +26 -0
- package/evaluator.js +822 -0
- package/event_factory.js +121 -0
- package/event_processor.js +320 -0
- package/event_summarizer.js +101 -0
- package/feature_store.js +120 -0
- package/feature_store_event_wrapper.js +258 -0
- package/file_data_source.js +192 -0
- package/flags_state.js +46 -0
- package/index.d.ts +2426 -0
- package/index.js +452 -0
- package/integrations.js +7 -0
- package/interfaces.js +2 -0
- package/loggers.js +125 -0
- package/messages.js +31 -0
- package/operators.js +106 -0
- package/package.json +105 -0
- package/polling.js +70 -0
- package/requestor.js +62 -0
- package/scripts/better-audit.sh +76 -0
- package/sharedtest/big_segment_store_tests.js +86 -0
- package/sharedtest/feature_store_tests.js +177 -0
- package/sharedtest/persistent_feature_store_tests.js +183 -0
- package/sharedtest/store_tests.js +7 -0
- package/streaming.js +179 -0
- package/test/LDClient-big-segments-test.js +92 -0
- package/test/LDClient-end-to-end-test.js +218 -0
- package/test/LDClient-evaluation-all-flags-test.js +226 -0
- package/test/LDClient-evaluation-test.js +204 -0
- package/test/LDClient-events-test.js +502 -0
- package/test/LDClient-listeners-test.js +180 -0
- package/test/LDClient-test.js +96 -0
- package/test/LDClient-tls-test.js +110 -0
- package/test/attribute_reference-test.js +494 -0
- package/test/big_segments-test.js +182 -0
- package/test/caching_store_wrapper-test.js +434 -0
- package/test/configuration-test.js +249 -0
- package/test/context-test.js +93 -0
- package/test/context_filter-test.js +424 -0
- package/test/diagnostic_events-test.js +152 -0
- package/test/evaluator-big-segments-test.js +301 -0
- package/test/evaluator-bucketing-test.js +333 -0
- package/test/evaluator-clause-test.js +277 -0
- package/test/evaluator-flag-test.js +452 -0
- package/test/evaluator-pre-conditions-test.js +105 -0
- package/test/evaluator-rule-test.js +131 -0
- package/test/evaluator-segment-match-test.js +310 -0
- package/test/evaluator_helpers.js +106 -0
- package/test/event_processor-test.js +680 -0
- package/test/event_summarizer-test.js +146 -0
- package/test/feature_store-test.js +42 -0
- package/test/feature_store_event_wrapper-test.js +182 -0
- package/test/feature_store_test_base.js +60 -0
- package/test/file_data_source-test.js +255 -0
- package/test/loggers-test.js +126 -0
- package/test/operators-test.js +102 -0
- package/test/polling-test.js +158 -0
- package/test/requestor-test.js +60 -0
- package/test/store_tests_big_segments-test.js +61 -0
- package/test/streaming-test.js +323 -0
- package/test/stubs.js +107 -0
- package/test/test_data-test.js +341 -0
- package/test/update_queue-test.js +61 -0
- package/test-types.ts +210 -0
- package/test_data.js +323 -0
- package/tsconfig.json +14 -0
- package/update_queue.js +28 -0
- package/utils/__tests__/httpUtils-test.js +39 -0
- package/utils/__tests__/wrapPromiseCallback-test.js +33 -0
- package/utils/asyncUtils.js +32 -0
- package/utils/httpUtils.js +105 -0
- package/utils/stringifyAttrs.js +14 -0
- package/utils/wrapPromiseCallback.js +36 -0
- package/versioned_data_kind.js +34 -0
package/evaluator.js
ADDED
|
@@ -0,0 +1,822 @@
|
|
|
1
|
+
const crypto = require('crypto');
|
|
2
|
+
|
|
3
|
+
const operators = require('./operators');
|
|
4
|
+
const util = require('util');
|
|
5
|
+
const { safeAsyncEachSeries } = require('./utils/asyncUtils');
|
|
6
|
+
const AttributeReference = require('./attribute_reference');
|
|
7
|
+
const { checkContext } = require('./context');
|
|
8
|
+
|
|
9
|
+
const builtins = ['key', 'ip', 'country', 'email', 'firstName', 'lastName', 'avatar', 'name', 'anonymous'];
|
|
10
|
+
|
|
11
|
+
const bigSegementsStatusPriority = {
|
|
12
|
+
HEALTHY: 1,
|
|
13
|
+
STALE: 2,
|
|
14
|
+
STORE_ERROR: 3,
|
|
15
|
+
NOT_CONFIGURED: 4,
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
function stringifyContextAttrs(context) {
|
|
19
|
+
// Only legacy contexts may have non-string keys.
|
|
20
|
+
if (context.kind === undefined && typeof context.key !== 'string') {
|
|
21
|
+
return {
|
|
22
|
+
...context,
|
|
23
|
+
key: String(context.key),
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
return context;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const noop = () => {};
|
|
30
|
+
|
|
31
|
+
// This internal object encapsulates SDK state that's used for every flag evaluation. Each
|
|
32
|
+
// LDClient maintains a single instance of it.
|
|
33
|
+
//
|
|
34
|
+
// The "queries" object provides read-only async data access on demand. Its methods are:
|
|
35
|
+
// getFlag(key: string, callback: (flag) => void): void
|
|
36
|
+
// getSegment(key: string, callback: (segment) => void): void
|
|
37
|
+
// getBigSegmentsMembership(userKey: string, callback: ([ BigSegmentStoreMembership, status ]) => void): void
|
|
38
|
+
function Evaluator(queries) {
|
|
39
|
+
const ret = {};
|
|
40
|
+
|
|
41
|
+
ret.evaluate = (flag, context, eventFactory, maybeCallback) => {
|
|
42
|
+
evaluate(flag, context, queries, eventFactory, maybeCallback);
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
return ret;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// Callback receives (err, detail, events) where detail has the properties "value", "variationIndex", and "reason";
|
|
49
|
+
// detail will never be null even if there's an error; events is either an array or undefined.
|
|
50
|
+
function evaluate(flag, context, queries, eventFactory, maybeCallback) {
|
|
51
|
+
const cb = maybeCallback || noop;
|
|
52
|
+
if (!checkContext(context, true)) {
|
|
53
|
+
cb(null, errorResult('USER_NOT_SPECIFIED'), []);
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
if (!flag) {
|
|
58
|
+
cb(null, errorResult('FLAG_NOT_FOUND'), []);
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const sanitizedContext = stringifyContextAttrs(context);
|
|
63
|
+
|
|
64
|
+
const stateOut = {};
|
|
65
|
+
evalInternal(flag, sanitizedContext, queries, stateOut, eventFactory, [flag.key], (err, detail) => {
|
|
66
|
+
const result = detail;
|
|
67
|
+
if (stateOut.bigSegmentsStatus) {
|
|
68
|
+
result.reason.bigSegmentsStatus = stateOut.bigSegmentsStatus;
|
|
69
|
+
}
|
|
70
|
+
cb(err, result, stateOut.events);
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function evalInternal(flag, context, queries, stateOut, eventFactory, visitedFlags, cb) {
|
|
75
|
+
// If flag is off, return the off variation
|
|
76
|
+
if (!flag.on) {
|
|
77
|
+
getOffResult(flag, { kind: 'OFF' }, cb);
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
checkPrerequisites(flag, context, queries, stateOut, eventFactory, visitedFlags, (err, failureReason) => {
|
|
82
|
+
if (stateOut.error) {
|
|
83
|
+
cb(...stateOut.error);
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
if (err || failureReason) {
|
|
87
|
+
getOffResult(flag, failureReason, cb);
|
|
88
|
+
} else {
|
|
89
|
+
evalRules(flag, context, queries, stateOut, cb);
|
|
90
|
+
}
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// Callback receives (err, reason) where reason is null if successful, or a "prerequisite failed" reason
|
|
95
|
+
function checkPrerequisites(flag, context, queries, stateOut, eventFactory, visitedFlags, cb) {
|
|
96
|
+
if (flag.prerequisites && flag.prerequisites.length) {
|
|
97
|
+
safeAsyncEachSeries(
|
|
98
|
+
flag.prerequisites,
|
|
99
|
+
(prereq, callback) => {
|
|
100
|
+
if (visitedFlags.indexOf(prereq.key) !== -1) {
|
|
101
|
+
/* eslint-disable no-param-reassign */
|
|
102
|
+
stateOut.error = [
|
|
103
|
+
new Error(
|
|
104
|
+
`Prerequisite of ${flag.key} causing a circular reference.` +
|
|
105
|
+
' This is probably a temporary condition due to an incomplete update.'
|
|
106
|
+
),
|
|
107
|
+
errorResult('MALFORMED_FLAG'),
|
|
108
|
+
];
|
|
109
|
+
/* eslint-enable no-param-reassign */
|
|
110
|
+
callback(null);
|
|
111
|
+
return;
|
|
112
|
+
}
|
|
113
|
+
const updatedVisitedFlags = [...visitedFlags, prereq.key];
|
|
114
|
+
queries.getFlag(prereq.key, prereqFlag => {
|
|
115
|
+
// If the flag does not exist in the store or is not on, the prerequisite
|
|
116
|
+
// is not satisfied
|
|
117
|
+
if (!prereqFlag) {
|
|
118
|
+
callback({
|
|
119
|
+
key: prereq.key,
|
|
120
|
+
err: new Error('Could not retrieve prerequisite feature flag "' + prereq.key + '"'),
|
|
121
|
+
});
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
124
|
+
evalInternal(prereqFlag, context, queries, stateOut, eventFactory, updatedVisitedFlags, (err, detail) => {
|
|
125
|
+
// If there was an error, the value is null, the variation index is out of range,
|
|
126
|
+
// or the value does not match the indexed variation the prerequisite is not satisfied
|
|
127
|
+
stateOut.events = stateOut.events || []; // eslint-disable-line no-param-reassign
|
|
128
|
+
stateOut.events.push(eventFactory.newEvalEvent(prereqFlag, context, detail, null, flag));
|
|
129
|
+
if (err) {
|
|
130
|
+
callback({ key: prereq.key, err: err });
|
|
131
|
+
} else if (!prereqFlag.on || detail.variationIndex !== prereq.variation) {
|
|
132
|
+
// Note that if the prerequisite flag is off, we don't consider it a match no matter what its
|
|
133
|
+
// off variation was. But we still evaluate it and generate an event.
|
|
134
|
+
callback({ key: prereq.key });
|
|
135
|
+
} else {
|
|
136
|
+
// The prerequisite was satisfied
|
|
137
|
+
callback(null);
|
|
138
|
+
}
|
|
139
|
+
});
|
|
140
|
+
});
|
|
141
|
+
},
|
|
142
|
+
errInfo => {
|
|
143
|
+
if (errInfo) {
|
|
144
|
+
cb(errInfo.err, {
|
|
145
|
+
kind: 'PREREQUISITE_FAILED',
|
|
146
|
+
prerequisiteKey: errInfo.key,
|
|
147
|
+
});
|
|
148
|
+
} else {
|
|
149
|
+
cb(null, null);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
);
|
|
153
|
+
} else {
|
|
154
|
+
cb(null, null);
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
function evalRules(flag, context, queries, stateOut, cb) {
|
|
159
|
+
if (evalTargets(flag, context, cb)) {
|
|
160
|
+
return;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
safeAsyncEachSeries(
|
|
164
|
+
flag.rules,
|
|
165
|
+
(rule, callback) => {
|
|
166
|
+
ruleMatchContext(
|
|
167
|
+
rule,
|
|
168
|
+
context,
|
|
169
|
+
queries,
|
|
170
|
+
stateOut,
|
|
171
|
+
matched => {
|
|
172
|
+
// We raise an "error" on the first rule that *does* match, to stop evaluating more rules
|
|
173
|
+
callback(matched ? rule : null);
|
|
174
|
+
},
|
|
175
|
+
[]
|
|
176
|
+
);
|
|
177
|
+
},
|
|
178
|
+
// The following function executes once all of the rules have been checked
|
|
179
|
+
err => {
|
|
180
|
+
// If there was an error processing the rules, then it will
|
|
181
|
+
// have been populated into stateOut.error.
|
|
182
|
+
if (stateOut.error) {
|
|
183
|
+
return cb(...stateOut.error);
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
// we use the "error" value to indicate that a rule was successfully matched (since we only care
|
|
187
|
+
// about the first match, and eachSeries terminates on the first "error")
|
|
188
|
+
if (err) {
|
|
189
|
+
const rule = err;
|
|
190
|
+
const reason = { kind: 'RULE_MATCH', ruleId: rule.id };
|
|
191
|
+
for (let i = 0; i < flag.rules.length; i++) {
|
|
192
|
+
if (flag.rules[i].id === rule.id) {
|
|
193
|
+
reason.ruleIndex = i;
|
|
194
|
+
break;
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
getResultForVariationOrRollout(rule, context, flag, reason, cb);
|
|
198
|
+
} else {
|
|
199
|
+
// no rule matched; check the fallthrough
|
|
200
|
+
getResultForVariationOrRollout(flag.fallthrough, context, flag, { kind: 'FALLTHROUGH' }, cb);
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
);
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
function evalTarget(flag, target, context, cb) {
|
|
207
|
+
if (!target.values) {
|
|
208
|
+
return false;
|
|
209
|
+
}
|
|
210
|
+
const matchContext = getContextForKind(context, target.contextKind);
|
|
211
|
+
if (!matchContext) {
|
|
212
|
+
return false;
|
|
213
|
+
}
|
|
214
|
+
const matchKey = matchContext.key;
|
|
215
|
+
return target.values.some(key => {
|
|
216
|
+
if (key === matchKey) {
|
|
217
|
+
getVariation(flag, target.variation, { kind: 'TARGET_MATCH' }, cb);
|
|
218
|
+
return true;
|
|
219
|
+
}
|
|
220
|
+
return false;
|
|
221
|
+
});
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
function evalTargets(flag, context, cb) {
|
|
225
|
+
if (!flag.contextTargets || !flag.contextTargets.length) {
|
|
226
|
+
return (
|
|
227
|
+
flag.targets &&
|
|
228
|
+
flag.targets.some(target =>
|
|
229
|
+
// We can call evalTarget with this just like we could with a target from contextTargets: it does not
|
|
230
|
+
// have a contextKind property, but our default behavior is to treat that as a contextKind of "user".
|
|
231
|
+
evalTarget(flag, target, context, cb)
|
|
232
|
+
)
|
|
233
|
+
);
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
return flag.contextTargets.some(target => {
|
|
237
|
+
if (!target.contextKind || target.contextKind === 'user') {
|
|
238
|
+
const userTarget = (flag.targets || []).find(ut => ut.variation === target.variation);
|
|
239
|
+
return userTarget && evalTarget(flag, userTarget, context, cb);
|
|
240
|
+
} else {
|
|
241
|
+
return evalTarget(flag, target, context, cb);
|
|
242
|
+
}
|
|
243
|
+
});
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
function ruleMatchContext(r, context, queries, stateOut, cb, segmentsVisited) {
|
|
247
|
+
if (!r.clauses) {
|
|
248
|
+
cb(false);
|
|
249
|
+
return;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
// A rule matches if all its clauses match.
|
|
253
|
+
safeAsyncEachSeries(
|
|
254
|
+
r.clauses,
|
|
255
|
+
(clause, callback) => {
|
|
256
|
+
clauseMatchContext(
|
|
257
|
+
clause,
|
|
258
|
+
context,
|
|
259
|
+
queries,
|
|
260
|
+
stateOut,
|
|
261
|
+
matched => {
|
|
262
|
+
// on the first clause that does *not* match, we raise an "error" to stop the loop
|
|
263
|
+
callback(matched ? null : clause);
|
|
264
|
+
},
|
|
265
|
+
segmentsVisited
|
|
266
|
+
);
|
|
267
|
+
},
|
|
268
|
+
err => {
|
|
269
|
+
cb(!err);
|
|
270
|
+
}
|
|
271
|
+
);
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
function clauseMatchContext(c, context, queries, stateOut, matchedCb, segmentsVisited) {
|
|
275
|
+
if (c.op === 'segmentMatch') {
|
|
276
|
+
safeAsyncEachSeries(
|
|
277
|
+
c.values,
|
|
278
|
+
(value, seriesCallback) => {
|
|
279
|
+
queries.getSegment(value, segment => {
|
|
280
|
+
if (segment) {
|
|
281
|
+
if (segmentsVisited.indexOf(segment.key) >= 0) {
|
|
282
|
+
/* eslint-disable no-param-reassign */
|
|
283
|
+
stateOut.error = [
|
|
284
|
+
new Error(
|
|
285
|
+
`Segment rule referencing segment ${segment.key} caused a circular reference. ` +
|
|
286
|
+
'This is probably a temporary condition due to an incomplete update'
|
|
287
|
+
),
|
|
288
|
+
errorResult('MALFORMED_FLAG'),
|
|
289
|
+
];
|
|
290
|
+
/* eslint-enable no-param-reassign */
|
|
291
|
+
|
|
292
|
+
//The return needs to be non-null in order to skip the rest of the series.
|
|
293
|
+
return seriesCallback(true);
|
|
294
|
+
}
|
|
295
|
+
const newVisited = [...segmentsVisited, segment.key];
|
|
296
|
+
segmentMatchContext(
|
|
297
|
+
segment,
|
|
298
|
+
context,
|
|
299
|
+
queries,
|
|
300
|
+
stateOut,
|
|
301
|
+
result =>
|
|
302
|
+
// On the first segment that matches, we call seriesCallback with an
|
|
303
|
+
// arbitrary non-null value, which safeAsyncEachSeries interprets as an
|
|
304
|
+
// "error", causing it to skip the rest of the series.
|
|
305
|
+
seriesCallback(result ? segment : null),
|
|
306
|
+
newVisited
|
|
307
|
+
);
|
|
308
|
+
} else {
|
|
309
|
+
return seriesCallback(null);
|
|
310
|
+
}
|
|
311
|
+
});
|
|
312
|
+
},
|
|
313
|
+
// The following function executes once all of the clauses have been checked
|
|
314
|
+
err => {
|
|
315
|
+
// an "error" indicates that a segment *did* match
|
|
316
|
+
matchedCb(maybeNegate(c, !!err));
|
|
317
|
+
}
|
|
318
|
+
);
|
|
319
|
+
} else {
|
|
320
|
+
matchedCb(clauseMatchContextNoSegments(c, context, stateOut));
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
function getContextValueForClause(c, context) {
|
|
325
|
+
const kind = c.contextKind || 'user';
|
|
326
|
+
const isKindRule = AttributeReference.isKind(c.attribute);
|
|
327
|
+
|
|
328
|
+
if (isKindRule && context.kind !== 'multi') {
|
|
329
|
+
return [true, context.kind || 'user'];
|
|
330
|
+
} else if (isKindRule) {
|
|
331
|
+
return [true, Object.keys(context).filter(key => key !== 'kind')];
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
return contextValue(context, kind, c.attribute, !c.contextKind);
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
function clauseMatchContextNoSegments(c, context, stateOut) {
|
|
338
|
+
const matchFn = operators.fn(c.op);
|
|
339
|
+
const [validReference, cValue] = getContextValueForClause(c, context);
|
|
340
|
+
|
|
341
|
+
if (!validReference) {
|
|
342
|
+
stateOut.error = [new Error('Invalid attribute reference in clause'), errorResult('MALFORMED_FLAG')]; // eslint-disable-line no-param-reassign
|
|
343
|
+
return false;
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
if (cValue === null || cValue === undefined) {
|
|
347
|
+
return false;
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
// The contexts's value is an array
|
|
351
|
+
if (Array.isArray(cValue)) {
|
|
352
|
+
for (let i = 0; i < cValue.length; i++) {
|
|
353
|
+
if (matchAny(matchFn, cValue[i], c.values)) {
|
|
354
|
+
return maybeNegate(c, true);
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
return maybeNegate(c, false);
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
return maybeNegate(c, matchAny(matchFn, cValue, c.values));
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
/**
|
|
364
|
+
* Get a priority for the given big segment status.
|
|
365
|
+
* @param {string} status
|
|
366
|
+
* @returns Integer representing the priority.
|
|
367
|
+
*/
|
|
368
|
+
function getBigSegmentsStatusPriority(status) {
|
|
369
|
+
return bigSegementsStatusPriority[status] || 0;
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
/**
|
|
373
|
+
* Given two big segment statuses return the one with the higher priority.
|
|
374
|
+
* @param {string} old
|
|
375
|
+
* @param {string} latest
|
|
376
|
+
* @returns The status with the higher priority.
|
|
377
|
+
*/
|
|
378
|
+
function computeUpdatedBigSegmentsStatus(old, latest) {
|
|
379
|
+
if (old !== undefined && getBigSegmentsStatusPriority(old) > getBigSegmentsStatusPriority(latest)) {
|
|
380
|
+
return old;
|
|
381
|
+
}
|
|
382
|
+
return latest;
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
function segmentMatchContext(segment, context, queries, stateOut, cb, segmentsVisited) {
|
|
386
|
+
if (!segment.unbounded) {
|
|
387
|
+
return simpleSegmentMatchContext(segment, context, true, queries, stateOut, cb, segmentsVisited);
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
if (!segment.generation) {
|
|
391
|
+
// Big Segment queries can only be done if the generation is known. If it's unset,
|
|
392
|
+
// that probably means the data store was populated by an older SDK that doesn't know
|
|
393
|
+
// about the generation property and therefore dropped it from the JSON data. We'll treat
|
|
394
|
+
// that as a "not configured" condition.
|
|
395
|
+
stateOut.bigSegmentsStatus = computeUpdatedBigSegmentsStatus(stateOut.bigSegmentsStatus, 'NOT_CONFIGURED'); // eslint-disable-line no-param-reassign
|
|
396
|
+
return cb(false);
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
const bigSegmentKind = segment.unboundedContextKind || 'user';
|
|
400
|
+
const bigSegmentContext = getContextForKind(context, bigSegmentKind);
|
|
401
|
+
|
|
402
|
+
if (!bigSegmentContext) {
|
|
403
|
+
return cb(false);
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
if (stateOut.bigSegmentsMembership && stateOut.bigSegmentsMembership[bigSegmentContext.key]) {
|
|
407
|
+
// We've already done the query at some point during the flag evaluation and stored
|
|
408
|
+
// the result (if any) in stateOut.bigSegmentsMembership, so we don't need to do it
|
|
409
|
+
// again. Even if multiple Big Segments are being referenced, the membership includes
|
|
410
|
+
// *all* of the user's segment memberships.
|
|
411
|
+
|
|
412
|
+
return bigSegmentMatchContext(
|
|
413
|
+
stateOut.bigSegmentsMembership[bigSegmentContext.key],
|
|
414
|
+
segment,
|
|
415
|
+
bigSegmentContext,
|
|
416
|
+
queries,
|
|
417
|
+
stateOut,
|
|
418
|
+
cb
|
|
419
|
+
);
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
queries.getBigSegmentsMembership(bigSegmentContext.key, result => {
|
|
423
|
+
/* eslint-disable no-param-reassign */
|
|
424
|
+
stateOut.bigSegmentsMembership = stateOut.bigSegmentsMembership || {};
|
|
425
|
+
if (result) {
|
|
426
|
+
stateOut.bigSegmentsMembership[bigSegmentContext.key] = result[0];
|
|
427
|
+
stateOut.bigSegmentsStatus = computeUpdatedBigSegmentsStatus(stateOut.bigSegmentsStatus, result[1]);
|
|
428
|
+
} else {
|
|
429
|
+
stateOut.bigSegmentsStatus = computeUpdatedBigSegmentsStatus(stateOut.bigSegmentsStatus, 'NOT_CONFIGURED');
|
|
430
|
+
}
|
|
431
|
+
/* eslint-enable no-param-reassign */
|
|
432
|
+
return bigSegmentMatchContext(
|
|
433
|
+
stateOut.bigSegmentsMembership[bigSegmentContext.key],
|
|
434
|
+
segment,
|
|
435
|
+
bigSegmentContext,
|
|
436
|
+
queries,
|
|
437
|
+
stateOut,
|
|
438
|
+
cb
|
|
439
|
+
);
|
|
440
|
+
});
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
function bigSegmentMatchContext(membership, segment, context, queries, stateOut, cb) {
|
|
444
|
+
const segmentRef = makeBigSegmentRef(segment);
|
|
445
|
+
const included = membership && membership[segmentRef];
|
|
446
|
+
if (included !== undefined) {
|
|
447
|
+
return cb(included);
|
|
448
|
+
}
|
|
449
|
+
return simpleSegmentMatchContext(segment, context, false, queries, stateOut, cb);
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
function getContextForKind(context, inKind) {
|
|
453
|
+
const kind = inKind || 'user';
|
|
454
|
+
if (context.kind === 'multi') {
|
|
455
|
+
return context[kind];
|
|
456
|
+
} else if (context.kind === kind || (context.kind === undefined && kind === 'user')) {
|
|
457
|
+
return context;
|
|
458
|
+
}
|
|
459
|
+
return undefined;
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
/**
|
|
463
|
+
* Search the given contextTargets and userTargets. If a match is made, then
|
|
464
|
+
* return `[true, true]`. If a match is not made then return `[false, _]`.
|
|
465
|
+
* If there was an error which prevents matching, then return `[true, false]`.
|
|
466
|
+
* @param {{contextKind: string, values: string[]}[]} contextTargets
|
|
467
|
+
* @param {string[]} userTargets
|
|
468
|
+
* @param {Object} context
|
|
469
|
+
* @returns {[boolean, boolean]} Pair of booleans where the first indicates
|
|
470
|
+
* if the return value should be used, and the second indicates if there
|
|
471
|
+
* was a match.
|
|
472
|
+
*/
|
|
473
|
+
function segmentSearch(contextTargets, userTargets, context) {
|
|
474
|
+
const contextKind = context.kind || 'user';
|
|
475
|
+
for (const { contextKind: kind, values } of contextTargets) {
|
|
476
|
+
const contextForKind = getContextForKind(context, kind);
|
|
477
|
+
if (contextForKind) {
|
|
478
|
+
if (values.indexOf(contextForKind.key) >= 0) {
|
|
479
|
+
return [true, true];
|
|
480
|
+
}
|
|
481
|
+
}
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
const userContext = contextKind === 'user' ? context : context['user'];
|
|
485
|
+
if (userContext) {
|
|
486
|
+
if (userTargets.indexOf(userContext.key) >= 0) {
|
|
487
|
+
return [true, true];
|
|
488
|
+
}
|
|
489
|
+
}
|
|
490
|
+
return [false, false];
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
function simpleSegmentMatchContext(segment, context, useIncludesAndExcludes, queries, stateOut, cb, segmentsVisited) {
|
|
494
|
+
if (useIncludesAndExcludes) {
|
|
495
|
+
const includedRes = segmentSearch(segment.includedContexts || [], segment.included || [], context);
|
|
496
|
+
if (includedRes[0]) {
|
|
497
|
+
return cb(includedRes[1]);
|
|
498
|
+
}
|
|
499
|
+
const excludedRes = segmentSearch(segment.excludedContexts || [], segment.excluded || [], context);
|
|
500
|
+
if (excludedRes[0]) {
|
|
501
|
+
// The match was an exclusion, so it should be negated.
|
|
502
|
+
return cb(!excludedRes[1]);
|
|
503
|
+
}
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
safeAsyncEachSeries(
|
|
507
|
+
segment.rules || [],
|
|
508
|
+
(rule, callback) => {
|
|
509
|
+
segmentRuleMatchContext(
|
|
510
|
+
rule,
|
|
511
|
+
context,
|
|
512
|
+
segment.key,
|
|
513
|
+
segment.salt,
|
|
514
|
+
queries,
|
|
515
|
+
stateOut,
|
|
516
|
+
res => {
|
|
517
|
+
// on the first rule that does match, we raise an "error" to stop the loop
|
|
518
|
+
callback(res ? res : null);
|
|
519
|
+
},
|
|
520
|
+
segmentsVisited
|
|
521
|
+
);
|
|
522
|
+
},
|
|
523
|
+
err => {
|
|
524
|
+
cb(err);
|
|
525
|
+
}
|
|
526
|
+
);
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
function segmentRuleMatchContext(rule, context, segmentKey, salt, queries, stateOut, cb, segmentsVisited) {
|
|
530
|
+
safeAsyncEachSeries(
|
|
531
|
+
rule.clauses,
|
|
532
|
+
(clause, callback) => {
|
|
533
|
+
clauseMatchContext(
|
|
534
|
+
clause,
|
|
535
|
+
context,
|
|
536
|
+
queries,
|
|
537
|
+
stateOut,
|
|
538
|
+
matched => {
|
|
539
|
+
// on the first clause that does *not* match, we raise an "error" to stop the loop
|
|
540
|
+
callback(matched ? null : clause);
|
|
541
|
+
},
|
|
542
|
+
segmentsVisited
|
|
543
|
+
);
|
|
544
|
+
},
|
|
545
|
+
err => {
|
|
546
|
+
if (err) {
|
|
547
|
+
return cb(false);
|
|
548
|
+
}
|
|
549
|
+
// If the weight is absent, this rule matches
|
|
550
|
+
if (rule.weight === undefined || rule.weight === null) {
|
|
551
|
+
return cb(true);
|
|
552
|
+
}
|
|
553
|
+
|
|
554
|
+
// All of the clauses are met. See if the user buckets in
|
|
555
|
+
const { invalid, refAttr } = validateReference(!rule.contextKind, rule.bucketBy || 'key');
|
|
556
|
+
if (invalid) {
|
|
557
|
+
stateOut.error = [new Error('Invalid attribute reference in rule.'), errorResult('MALFORMED_FLAG')]; // eslint-disable-line no-param-reassign
|
|
558
|
+
return cb(false);
|
|
559
|
+
}
|
|
560
|
+
const [bucket] = bucketContext(context, segmentKey, refAttr, salt, rule.contextKind);
|
|
561
|
+
const weight = rule.weight / 100000.0;
|
|
562
|
+
return cb(bucket < weight);
|
|
563
|
+
}
|
|
564
|
+
);
|
|
565
|
+
}
|
|
566
|
+
|
|
567
|
+
function maybeNegate(c, b) {
|
|
568
|
+
if (c.negate) {
|
|
569
|
+
return !b;
|
|
570
|
+
} else {
|
|
571
|
+
return b;
|
|
572
|
+
}
|
|
573
|
+
}
|
|
574
|
+
|
|
575
|
+
function matchAny(matchFn, value, values) {
|
|
576
|
+
for (let i = 0; i < values.length; i++) {
|
|
577
|
+
if (matchFn(value, values[i])) {
|
|
578
|
+
return true;
|
|
579
|
+
}
|
|
580
|
+
}
|
|
581
|
+
|
|
582
|
+
return false;
|
|
583
|
+
}
|
|
584
|
+
|
|
585
|
+
function getVariation(flag, index, reason, cb) {
|
|
586
|
+
if (index === null || index === undefined || index < 0 || index >= flag.variations.length) {
|
|
587
|
+
cb(new Error('Invalid variation index in flag'), errorResult('MALFORMED_FLAG'));
|
|
588
|
+
} else {
|
|
589
|
+
cb(null, { value: flag.variations[index], variationIndex: index, reason: reason });
|
|
590
|
+
}
|
|
591
|
+
}
|
|
592
|
+
|
|
593
|
+
function getOffResult(flag, reason, cb) {
|
|
594
|
+
if (flag.offVariation === null || flag.offVariation === undefined) {
|
|
595
|
+
cb(null, { value: null, variationIndex: null, reason: reason });
|
|
596
|
+
} else {
|
|
597
|
+
getVariation(flag, flag.offVariation, reason, cb);
|
|
598
|
+
}
|
|
599
|
+
}
|
|
600
|
+
|
|
601
|
+
function getResultForVariationOrRollout(r, context, flag, reason, cb) {
|
|
602
|
+
if (!r) {
|
|
603
|
+
cb(new Error('Fallthrough variation undefined'), errorResult('MALFORMED_FLAG'));
|
|
604
|
+
} else {
|
|
605
|
+
const [index, inExperiment, errorData] = variationForUser(r, context, flag);
|
|
606
|
+
if (errorData !== undefined) {
|
|
607
|
+
cb(...errorData);
|
|
608
|
+
} else if (index === null || index === undefined) {
|
|
609
|
+
cb(new Error('Variation/rollout object with no variation or rollout'), errorResult('MALFORMED_FLAG'));
|
|
610
|
+
} else {
|
|
611
|
+
const transformedReason = reason;
|
|
612
|
+
if (inExperiment) {
|
|
613
|
+
transformedReason.inExperiment = true;
|
|
614
|
+
}
|
|
615
|
+
getVariation(flag, index, transformedReason, cb);
|
|
616
|
+
}
|
|
617
|
+
}
|
|
618
|
+
}
|
|
619
|
+
|
|
620
|
+
function errorResult(errorKind) {
|
|
621
|
+
return { value: null, variationIndex: null, reason: { kind: 'ERROR', errorKind: errorKind } };
|
|
622
|
+
}
|
|
623
|
+
|
|
624
|
+
// Given a variation or rollout 'r', select the variation for the given context.
|
|
625
|
+
// Returns an array of the form [variationIndex, inExperiment, Error].
|
|
626
|
+
function variationForUser(r, context, flag) {
|
|
627
|
+
if (r.variation !== null && r.variation !== undefined) {
|
|
628
|
+
// This represets a fixed variation; return it
|
|
629
|
+
return [r.variation, false, undefined];
|
|
630
|
+
}
|
|
631
|
+
const rollout = r.rollout;
|
|
632
|
+
if (rollout) {
|
|
633
|
+
const isExperiment = rollout.kind === 'experiment';
|
|
634
|
+
const variations = rollout.variations;
|
|
635
|
+
if (variations && variations.length > 0) {
|
|
636
|
+
// This represents a percentage rollout. Assume
|
|
637
|
+
// we're rolling out by key
|
|
638
|
+
const bucketBy = isExperiment ? 'key' : rollout.bucketBy || 'key';
|
|
639
|
+
const { invalid, refAttr } = validateReference(!rollout.contextKind, bucketBy);
|
|
640
|
+
if (invalid) {
|
|
641
|
+
return [
|
|
642
|
+
undefined,
|
|
643
|
+
undefined,
|
|
644
|
+
[new Error('Invalid attribute reference for bucketBy in rollout'), errorResult('MALFORMED_FLAG')],
|
|
645
|
+
];
|
|
646
|
+
}
|
|
647
|
+
const [bucket, hadContext] = bucketContext(
|
|
648
|
+
context,
|
|
649
|
+
flag.key,
|
|
650
|
+
refAttr,
|
|
651
|
+
flag.salt,
|
|
652
|
+
rollout.seed,
|
|
653
|
+
rollout.contextKind
|
|
654
|
+
);
|
|
655
|
+
let sum = 0;
|
|
656
|
+
for (let i = 0; i < variations.length; i++) {
|
|
657
|
+
const variate = variations[i];
|
|
658
|
+
sum += variate.weight / 100000.0;
|
|
659
|
+
if (bucket < sum) {
|
|
660
|
+
return [variate.variation, isExperiment && hadContext && !variate.untracked, undefined];
|
|
661
|
+
}
|
|
662
|
+
}
|
|
663
|
+
|
|
664
|
+
// The context's bucket value was greater than or equal to the end of the last bucket. This could happen due
|
|
665
|
+
// to a rounding error, or due to the fact that we are scaling to 100000 rather than 99999, or the flag
|
|
666
|
+
// data could contain buckets that don't actually add up to 100000. Rather than returning an error in
|
|
667
|
+
// this case (or changing the scaling, which would potentially change the results for *all* users), we
|
|
668
|
+
// will simply put the context in the last bucket.
|
|
669
|
+
const lastVariate = variations[variations.length - 1];
|
|
670
|
+
return [lastVariate.variation, isExperiment && !lastVariate.untracked, undefined];
|
|
671
|
+
}
|
|
672
|
+
}
|
|
673
|
+
|
|
674
|
+
return [null, false];
|
|
675
|
+
}
|
|
676
|
+
|
|
677
|
+
// Fetch an attribute value from a context object. Automatically
|
|
678
|
+
// navigates into the custom array when necessary
|
|
679
|
+
function legacyUserValue(context, attr) {
|
|
680
|
+
if (builtins.some(builtIn => AttributeReference.compare(attr, builtIn))) {
|
|
681
|
+
return contextValueByReference(context, attr);
|
|
682
|
+
}
|
|
683
|
+
if (context.custom) {
|
|
684
|
+
return contextValueByReference(context.custom, attr);
|
|
685
|
+
}
|
|
686
|
+
return null;
|
|
687
|
+
}
|
|
688
|
+
|
|
689
|
+
/**
|
|
690
|
+
* Get a value from the context by the specified reference.
|
|
691
|
+
* @param {Object} context
|
|
692
|
+
* @param {string} reference
|
|
693
|
+
* @returns The value from the context, or undefined. If the value
|
|
694
|
+
* would have been a non-array object, then undefined will be returned.
|
|
695
|
+
* Objects are not valid for clauses.
|
|
696
|
+
*/
|
|
697
|
+
function contextValueByReference(context, reference) {
|
|
698
|
+
const value = AttributeReference.get(context, reference);
|
|
699
|
+
// Rules cannot use objects as a value.
|
|
700
|
+
if (typeof value === 'object' && !Array.isArray(value)) {
|
|
701
|
+
return undefined;
|
|
702
|
+
}
|
|
703
|
+
return value;
|
|
704
|
+
}
|
|
705
|
+
|
|
706
|
+
function legacyAttributeToReference(attr) {
|
|
707
|
+
return attr && attr.startsWith('/') ? AttributeReference.literalToReference(attr) : attr;
|
|
708
|
+
}
|
|
709
|
+
|
|
710
|
+
/**
|
|
711
|
+
* Get a value from the specified context that matches the kind and
|
|
712
|
+
* attribute reference.
|
|
713
|
+
* @param {Object} context The context to get a value from.
|
|
714
|
+
* @param {string} kind The kind that the value must be from.
|
|
715
|
+
* @param {string} attr An attribute reference to the value.
|
|
716
|
+
* @param {boolean} isLegacy Boolean flag indicating if the attribute is from
|
|
717
|
+
* a type which didn't specify a contextKind.
|
|
718
|
+
* @returns {[boolean, any]} A tuple where the first value indicates if the reference was valid and the second is the value
|
|
719
|
+
* of the attribute. The value will be undefined if the attribute does not exist, or if the type
|
|
720
|
+
* of the attribute is not suitable for evaluation (an object).
|
|
721
|
+
*/
|
|
722
|
+
function contextValue(context, kind, attr, isLegacy) {
|
|
723
|
+
//In the old format an attribute name could have started with a '/' but not
|
|
724
|
+
//been a reference. In this case these attributes need converted.
|
|
725
|
+
const { invalid, refAttr } = validateReference(isLegacy, attr);
|
|
726
|
+
if (invalid) {
|
|
727
|
+
return [!invalid, undefined];
|
|
728
|
+
}
|
|
729
|
+
|
|
730
|
+
// If anonymous is not defined, then it is considered false.
|
|
731
|
+
if (attr === 'anonymous') {
|
|
732
|
+
const forKind = getContextForKind(context, kind);
|
|
733
|
+
return [true, contextValueByReference(forKind, refAttr) || false];
|
|
734
|
+
}
|
|
735
|
+
|
|
736
|
+
if (!context.kind) {
|
|
737
|
+
if (kind === 'user') {
|
|
738
|
+
return [true, legacyUserValue(context, refAttr)];
|
|
739
|
+
}
|
|
740
|
+
return [true, undefined];
|
|
741
|
+
} else if (context.kind === 'multi') {
|
|
742
|
+
return [true, contextValueByReference(context[kind], refAttr)];
|
|
743
|
+
} else if (context.kind === kind) {
|
|
744
|
+
return [true, contextValueByReference(context, refAttr)];
|
|
745
|
+
}
|
|
746
|
+
return [true, undefined];
|
|
747
|
+
}
|
|
748
|
+
|
|
749
|
+
/**
|
|
750
|
+
* Validate an attribute and return an escaped version if needed.
|
|
751
|
+
* @param {boolean} isLegacy
|
|
752
|
+
* @param {string} attr
|
|
753
|
+
* @returns A pair where the first value indicates if the reference was valid,
|
|
754
|
+
* and the second value is the reference, possibly converted from a literal.
|
|
755
|
+
*/
|
|
756
|
+
function validateReference(isLegacy, attr) {
|
|
757
|
+
const refAttr = isLegacy ? legacyAttributeToReference(attr) : attr;
|
|
758
|
+
|
|
759
|
+
const invalid = attr === '' || (refAttr.startsWith('/') && !AttributeReference.isValidReference(refAttr));
|
|
760
|
+
return { invalid, refAttr };
|
|
761
|
+
}
|
|
762
|
+
|
|
763
|
+
/**
|
|
764
|
+
* Compute a bucket value for use in a rollout or experiment. If an error condition prevents us from
|
|
765
|
+
* computing a valid bucket value, we return zero, which will cause the evaluation to use the first
|
|
766
|
+
* bucket.
|
|
767
|
+
*
|
|
768
|
+
* @returns {[number, boolean]} A tuple where the first value is the bucket, and the second value
|
|
769
|
+
* indicates if there was a context for the value specified by `kindForRollout`. If there was not
|
|
770
|
+
* a context for the specified kind, then the `inExperiment` attribute should be `false`.
|
|
771
|
+
*/
|
|
772
|
+
function bucketContext(context, key, attr, salt, seed, kindForRollout) {
|
|
773
|
+
const kindOrDefault = kindForRollout || 'user';
|
|
774
|
+
//Key pre-validated. So we can disregard the validation here.
|
|
775
|
+
const [, value] = contextValue(context, kindOrDefault, attr, kindForRollout === undefined);
|
|
776
|
+
|
|
777
|
+
const idHash = bucketableStringValue(value);
|
|
778
|
+
|
|
779
|
+
if (idHash === null) {
|
|
780
|
+
// If we got a value, then we know there was a context, but if we didn't get a value, then
|
|
781
|
+
// it could either be there wasn't an attribute, the attribute was undefined/null, or there
|
|
782
|
+
// was not a context. So here check for the context.
|
|
783
|
+
const contextForKind = getContextForKind(context, kindForRollout);
|
|
784
|
+
|
|
785
|
+
return [0, !!contextForKind];
|
|
786
|
+
}
|
|
787
|
+
|
|
788
|
+
const prefix = seed ? util.format('%d.', seed) : util.format('%s.%s.', key, salt);
|
|
789
|
+
const hashKey = prefix + idHash;
|
|
790
|
+
const hashVal = parseInt(sha1Hex(hashKey).substring(0, 15), 16);
|
|
791
|
+
|
|
792
|
+
return [hashVal / 0xfffffffffffffff, true];
|
|
793
|
+
}
|
|
794
|
+
|
|
795
|
+
function bucketableStringValue(value) {
|
|
796
|
+
if (typeof value === 'string') {
|
|
797
|
+
return value;
|
|
798
|
+
}
|
|
799
|
+
if (Number.isInteger(value)) {
|
|
800
|
+
return '' + value;
|
|
801
|
+
}
|
|
802
|
+
return null;
|
|
803
|
+
}
|
|
804
|
+
|
|
805
|
+
function sha1Hex(input) {
|
|
806
|
+
const hash = crypto.createHash('sha1');
|
|
807
|
+
hash.update(input);
|
|
808
|
+
return hash.digest('hex');
|
|
809
|
+
}
|
|
810
|
+
|
|
811
|
+
function makeBigSegmentRef(segment) {
|
|
812
|
+
// The format of Big Segment references is independent of what store implementation is being
|
|
813
|
+
// used; the store implementation receives only this string and does not know the details of
|
|
814
|
+
// the data model. The Relay Proxy will use the same format when writing to the store.
|
|
815
|
+
return segment.key + '.g' + segment.generation;
|
|
816
|
+
}
|
|
817
|
+
|
|
818
|
+
module.exports = {
|
|
819
|
+
Evaluator,
|
|
820
|
+
bucketContext,
|
|
821
|
+
makeBigSegmentRef,
|
|
822
|
+
};
|