-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathindex.ts
More file actions
209 lines (188 loc) · 6.48 KB
/
index.ts
File metadata and controls
209 lines (188 loc) · 6.48 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
import { engineParser } from './Engine';
import { thenable } from '../utils/promise/thenable';
import { EXCEPTION, SPLIT_NOT_FOUND } from '../utils/labels';
import { CONTROL } from '../utils/constants';
import { ISplit, MaybeThenable } from '../dtos/types';
import { IStorageAsync, IStorageSync } from '../storages/types';
import { IEvaluationResult } from './types';
import SplitIO from '../../types/splitio';
import { ILogger } from '../logger/types';
import { returnSetsUnion, setToArray } from '../utils/lang/sets';
import { WARN_FLAGSET_WITHOUT_FLAGS } from '../logger/constants';
const treatmentException = {
treatment: CONTROL,
label: EXCEPTION,
config: null
};
function treatmentsException(splitNames: string[]) {
const evaluations: Record<string, IEvaluationResult> = {};
splitNames.forEach(splitName => {
evaluations[splitName] = treatmentException;
});
return evaluations;
}
export function evaluateFeature(
log: ILogger,
key: SplitIO.SplitKey,
splitName: string,
attributes: SplitIO.Attributes | undefined,
storage: IStorageSync | IStorageAsync,
options?: SplitIO.EvaluationOptions
): MaybeThenable<IEvaluationResult> {
let parsedSplit;
try {
parsedSplit = storage.splits.getSplit(splitName);
} catch (e) {
// Exception on sync `getSplit` storage. Not possible ATM with InMemory and InLocal storages.
return treatmentException;
}
if (thenable(parsedSplit)) {
return parsedSplit.then((split) => getEvaluation(
log,
key,
split,
attributes,
storage,
options,
)).catch(
// Exception on async `getSplit` storage. For example, when the storage is redis or
// pluggable and there is a connection issue and we can't retrieve the split to be evaluated
() => treatmentException
);
}
return getEvaluation(
log,
key,
parsedSplit,
attributes,
storage,
options,
);
}
export function evaluateFeatures(
log: ILogger,
key: SplitIO.SplitKey,
splitNames: string[],
attributes: SplitIO.Attributes | undefined,
storage: IStorageSync | IStorageAsync,
options?: SplitIO.EvaluationOptions,
): MaybeThenable<Record<string, IEvaluationResult>> {
let parsedSplits;
try {
parsedSplits = storage.splits.getSplits(splitNames);
} catch (e) {
// Exception on sync `getSplits` storage. Not possible ATM with InMemory and InLocal storages.
return treatmentsException(splitNames);
}
return thenable(parsedSplits) ?
parsedSplits.then(splits => getEvaluations(log, key, splitNames, splits, attributes, storage, options))
.catch(() => {
// Exception on async `getSplits` storage. For example, when the storage is redis or
// pluggable and there is a connection issue and we can't retrieve the split to be evaluated
return treatmentsException(splitNames);
}) :
getEvaluations(log, key, splitNames, parsedSplits, attributes, storage, options);
}
export function evaluateFeaturesByFlagSets(
log: ILogger,
key: SplitIO.SplitKey,
flagSets: string[],
attributes: SplitIO.Attributes | undefined,
storage: IStorageSync | IStorageAsync,
method: string,
options?: SplitIO.EvaluationOptions,
): MaybeThenable<Record<string, IEvaluationResult>> {
let storedFlagNames: MaybeThenable<Set<string>[]>;
function evaluate(featureFlagsByFlagSets: Set<string>[]) {
let featureFlags = new Set<string>();
for (let i = 0; i < flagSets.length; i++) {
const featureFlagByFlagSet = featureFlagsByFlagSets[i];
if (featureFlagByFlagSet.size) {
featureFlags = returnSetsUnion(featureFlags, featureFlagByFlagSet);
} else {
log.warn(WARN_FLAGSET_WITHOUT_FLAGS, [method, flagSets[i]]);
}
}
return featureFlags.size ?
evaluateFeatures(log, key, setToArray(featureFlags), attributes, storage, options) :
{};
}
// get features by flag sets
try {
storedFlagNames = storage.splits.getNamesByFlagSets(flagSets);
} catch (e) {
// return empty evaluations
return {};
}
// evaluate related features
return thenable(storedFlagNames) ?
storedFlagNames.then((storedFlagNames) => evaluate(storedFlagNames))
.catch(() => {
return {};
}) :
evaluate(storedFlagNames);
}
function getEvaluation(
log: ILogger,
key: SplitIO.SplitKey,
splitJSON: ISplit | null,
attributes: SplitIO.Attributes | undefined,
storage: IStorageSync | IStorageAsync,
options?: SplitIO.EvaluationOptions,
): MaybeThenable<IEvaluationResult> {
let evaluation: MaybeThenable<IEvaluationResult> = {
treatment: CONTROL,
label: SPLIT_NOT_FOUND,
config: null
};
if (splitJSON) {
const split = engineParser(log, splitJSON, storage);
evaluation = split.getTreatment(key, attributes, evaluateFeature);
// If the storage is async and the evaluated flag uses segments or dependencies, evaluation is thenable
if (thenable(evaluation)) {
return evaluation.then(result => {
result.changeNumber = splitJSON.changeNumber;
result.config = splitJSON.configurations && splitJSON.configurations[result.treatment] || null;
// @ts-expect-error impressionsDisabled is not exposed in the public typings yet.
result.impressionsDisabled = options?.impressionsDisabled || splitJSON.impressionsDisabled;
return result;
});
} else {
evaluation.changeNumber = splitJSON.changeNumber;
evaluation.config = splitJSON.configurations && splitJSON.configurations[evaluation.treatment] || null;
// @ts-expect-error impressionsDisabled is not exposed in the public typings yet.
evaluation.impressionsDisabled = options?.impressionsDisabled || splitJSON.impressionsDisabled;
}
}
return evaluation;
}
function getEvaluations(
log: ILogger,
key: SplitIO.SplitKey,
splitNames: string[],
splits: Record<string, ISplit | null>,
attributes: SplitIO.Attributes | undefined,
storage: IStorageSync | IStorageAsync,
options?: SplitIO.EvaluationOptions,
): MaybeThenable<Record<string, IEvaluationResult>> {
const result: Record<string, IEvaluationResult> = {};
const thenables: Promise<void>[] = [];
splitNames.forEach(splitName => {
const evaluation = getEvaluation(
log,
key,
splits[splitName],
attributes,
storage,
options
);
if (thenable(evaluation)) {
thenables.push(evaluation.then(res => {
result[splitName] = res;
}));
} else {
result[splitName] = evaluation;
}
});
return thenables.length > 0 ? Promise.all(thenables).then(() => result) : result;
}