posthog-node 6.2.1 → 6.2.2
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/dist/client.d.ts +1 -1
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +14 -3
- package/dist/client.mjs +14 -3
- package/dist/extensions/feature-flags/feature-flags-v2.d.ts +254 -0
- package/dist/extensions/feature-flags/feature-flags-v2.d.ts.map +1 -0
- package/dist/extensions/feature-flags/feature-flags-v2.js +1408 -0
- package/dist/extensions/feature-flags/feature-flags-v2.mjs +1331 -0
- package/dist/extensions/feature-flags/feature-flags.d.ts +15 -4
- package/dist/extensions/feature-flags/feature-flags.d.ts.map +1 -1
- package/dist/extensions/feature-flags/feature-flags.js +55 -7
- package/dist/extensions/feature-flags/feature-flags.mjs +55 -7
- package/dist/feature-flag-evaluations.d.ts +8 -6
- package/dist/feature-flag-evaluations.d.ts.map +1 -1
- package/dist/flag-events.d.ts +2 -2
- package/dist/flag-events.d.ts.map +1 -1
- package/dist/flag-events.js +2 -1
- package/dist/flag-events.mjs +2 -1
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/dist/version.mjs +1 -1
- package/package.json +2 -2
- package/src/client.ts +18 -6
- package/src/extensions/feature-flags/feature-flags-v2.ts +2086 -0
- package/src/extensions/feature-flags/feature-flags.ts +110 -20
- package/src/feature-flag-evaluations.ts +8 -6
- package/src/flag-events.ts +3 -2
- package/src/version.ts +1 -1
|
@@ -0,0 +1,1331 @@
|
|
|
1
|
+
import { InconclusiveMatchError, hashSHA1, matchFeatureFlagProperty } from "@posthog/core";
|
|
2
|
+
import { parsedFlagRecordOf, readFlagRecords } from "../../flags-response.mjs";
|
|
3
|
+
const CONFIG_VERSION = 2;
|
|
4
|
+
const ASSIGNMENT_ALGORITHM = 'sha1_60_v1';
|
|
5
|
+
const HASH_SCALE = 0xfffffffffffffff;
|
|
6
|
+
const MAX_IDENTIFIER_LENGTH = 200;
|
|
7
|
+
const MAX_RULES = 100;
|
|
8
|
+
const MAX_PREDICATES = 100;
|
|
9
|
+
const MIN_VARIANTS = 2;
|
|
10
|
+
const MAX_VARIANTS = 20;
|
|
11
|
+
const MAX_SEED_LENGTH = 400;
|
|
12
|
+
const MAX_OBJECT_DEPTH = 20;
|
|
13
|
+
const MAX_JSON_DEPTH = 127;
|
|
14
|
+
const MAX_U64 = BigInt('18446744073709551615');
|
|
15
|
+
const CONFIG_FIELDS = new Set([
|
|
16
|
+
'version',
|
|
17
|
+
'return_type',
|
|
18
|
+
'default_value',
|
|
19
|
+
'rules',
|
|
20
|
+
'aggregation_group_type_index'
|
|
21
|
+
]);
|
|
22
|
+
const RULE_FIELDS = [
|
|
23
|
+
'id',
|
|
24
|
+
'rule_type',
|
|
25
|
+
'targeting',
|
|
26
|
+
"description",
|
|
27
|
+
'metadata'
|
|
28
|
+
];
|
|
29
|
+
const ROLLOUT_FIELDS = [
|
|
30
|
+
'rollout_percentage',
|
|
31
|
+
'on_rollout_miss',
|
|
32
|
+
'assignment_algorithm',
|
|
33
|
+
'seed',
|
|
34
|
+
'assign_by'
|
|
35
|
+
];
|
|
36
|
+
const FIELDS_BY_RULE_TYPE = {
|
|
37
|
+
targeted_release: new Set([
|
|
38
|
+
...RULE_FIELDS,
|
|
39
|
+
'value'
|
|
40
|
+
]),
|
|
41
|
+
percentage_rollout: new Set([
|
|
42
|
+
...RULE_FIELDS,
|
|
43
|
+
...ROLLOUT_FIELDS,
|
|
44
|
+
'value'
|
|
45
|
+
]),
|
|
46
|
+
experiment: new Set([
|
|
47
|
+
...RULE_FIELDS,
|
|
48
|
+
...ROLLOUT_FIELDS,
|
|
49
|
+
'paused',
|
|
50
|
+
'variants',
|
|
51
|
+
'holdout'
|
|
52
|
+
])
|
|
53
|
+
};
|
|
54
|
+
const VARIANT_FIELDS = new Set([
|
|
55
|
+
'key',
|
|
56
|
+
'weight',
|
|
57
|
+
'value'
|
|
58
|
+
]);
|
|
59
|
+
const HOLDOUT_FIELDS = new Set([
|
|
60
|
+
'seed',
|
|
61
|
+
'exclusion_percentage'
|
|
62
|
+
]);
|
|
63
|
+
const TARGETING_FIELDS = new Set([
|
|
64
|
+
'properties'
|
|
65
|
+
]);
|
|
66
|
+
const PROPERTY_FIELDS = new Set([
|
|
67
|
+
'key',
|
|
68
|
+
'type',
|
|
69
|
+
'value',
|
|
70
|
+
'operator',
|
|
71
|
+
'negation',
|
|
72
|
+
'group_type_index',
|
|
73
|
+
'cohort_name',
|
|
74
|
+
'group_key_names',
|
|
75
|
+
'label'
|
|
76
|
+
]);
|
|
77
|
+
const UNSUPPORTED_PROPERTY_TYPES = new Set([
|
|
78
|
+
'cohort',
|
|
79
|
+
'group',
|
|
80
|
+
'flag'
|
|
81
|
+
]);
|
|
82
|
+
const EXACT_OPERATORS = new Set([
|
|
83
|
+
'exact',
|
|
84
|
+
'is_not'
|
|
85
|
+
]);
|
|
86
|
+
const PRESENCE_OPERATORS = new Set([
|
|
87
|
+
'is_set',
|
|
88
|
+
'is_not_set'
|
|
89
|
+
]);
|
|
90
|
+
const TEXT_OPERATORS = new Set([
|
|
91
|
+
'icontains',
|
|
92
|
+
'not_icontains',
|
|
93
|
+
'starts_with',
|
|
94
|
+
'not_starts_with',
|
|
95
|
+
'ends_with',
|
|
96
|
+
'not_ends_with'
|
|
97
|
+
]);
|
|
98
|
+
const MULTI_TEXT_OPERATORS = new Set([
|
|
99
|
+
'icontains_multi',
|
|
100
|
+
'not_icontains_multi'
|
|
101
|
+
]);
|
|
102
|
+
const REGEX_OPERATORS = new Set([
|
|
103
|
+
'regex',
|
|
104
|
+
'not_regex'
|
|
105
|
+
]);
|
|
106
|
+
const NUMBER_OPERATORS = new Set([
|
|
107
|
+
'gt',
|
|
108
|
+
'gte',
|
|
109
|
+
'lt',
|
|
110
|
+
'lte'
|
|
111
|
+
]);
|
|
112
|
+
const SEMVER_COMPARISON_OPERATORS = new Set([
|
|
113
|
+
'semver_eq',
|
|
114
|
+
'semver_neq',
|
|
115
|
+
'semver_gt',
|
|
116
|
+
'semver_gte',
|
|
117
|
+
'semver_lt',
|
|
118
|
+
'semver_lte'
|
|
119
|
+
]);
|
|
120
|
+
const SEMVER_RANGE_OPERATORS = new Set([
|
|
121
|
+
'semver_tilde',
|
|
122
|
+
'semver_caret',
|
|
123
|
+
'semver_wildcard'
|
|
124
|
+
]);
|
|
125
|
+
const DATE_OPERATORS = new Set([
|
|
126
|
+
'is_date_exact',
|
|
127
|
+
'is_date_after',
|
|
128
|
+
'is_date_before'
|
|
129
|
+
]);
|
|
130
|
+
const OPERATORS = new Set([
|
|
131
|
+
...EXACT_OPERATORS,
|
|
132
|
+
...PRESENCE_OPERATORS,
|
|
133
|
+
...TEXT_OPERATORS,
|
|
134
|
+
...MULTI_TEXT_OPERATORS,
|
|
135
|
+
...REGEX_OPERATORS,
|
|
136
|
+
...NUMBER_OPERATORS,
|
|
137
|
+
...SEMVER_COMPARISON_OPERATORS,
|
|
138
|
+
...SEMVER_RANGE_OPERATORS,
|
|
139
|
+
...DATE_OPERATORS
|
|
140
|
+
]);
|
|
141
|
+
const OPERATORS_MATCHING_ABSENCE = new Set([
|
|
142
|
+
'is_not',
|
|
143
|
+
'not_icontains',
|
|
144
|
+
'not_starts_with',
|
|
145
|
+
'not_ends_with',
|
|
146
|
+
'not_icontains_multi',
|
|
147
|
+
'not_regex'
|
|
148
|
+
]);
|
|
149
|
+
class Halt extends Error {
|
|
150
|
+
constructor(result){
|
|
151
|
+
super(result.detail), this.result = result;
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
function unusableContext(detail) {
|
|
155
|
+
return new Halt({
|
|
156
|
+
outcome: 'inconclusive',
|
|
157
|
+
reason: 'missing_context',
|
|
158
|
+
detail
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
function invalidProperty(detail) {
|
|
162
|
+
return new Halt({
|
|
163
|
+
outcome: 'error',
|
|
164
|
+
kind: 'invalid_property',
|
|
165
|
+
detail
|
|
166
|
+
});
|
|
167
|
+
}
|
|
168
|
+
class Rejected extends Error {
|
|
169
|
+
constructor(kind, field){
|
|
170
|
+
super(field), this.kind = kind, this.field = field;
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
const malformed = (field)=>new Rejected('malformed', field);
|
|
174
|
+
const unsupported = (field)=>new Rejected('unsupported', field);
|
|
175
|
+
function parseConfigV2(filters) {
|
|
176
|
+
try {
|
|
177
|
+
return readConfig(filters);
|
|
178
|
+
} catch (error) {
|
|
179
|
+
if (error instanceof Rejected) return {
|
|
180
|
+
outcome: 'unsupported',
|
|
181
|
+
kind: error.kind,
|
|
182
|
+
field: error.field
|
|
183
|
+
};
|
|
184
|
+
throw error;
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
function isConfigV2Unsupported(config) {
|
|
188
|
+
return 'outcome' in config;
|
|
189
|
+
}
|
|
190
|
+
const hasOwn = (target, field)=>Object.prototype.hasOwnProperty.call(target, field);
|
|
191
|
+
function readConfig(filters) {
|
|
192
|
+
const document = object(filters, 'filters');
|
|
193
|
+
const version = optional(document, 'version');
|
|
194
|
+
if (!isNumber(version) || version !== CONFIG_VERSION) throw unsupported('version');
|
|
195
|
+
checkJson(document);
|
|
196
|
+
checkClosed(document, CONFIG_FIELDS, 'filters');
|
|
197
|
+
const returnType = string(document, 'return_type');
|
|
198
|
+
if (!hasOwn(VALUE_CHECKS, returnType)) throw malformed('return_type');
|
|
199
|
+
if (hasOwn(document, 'aggregation_group_type_index')) {
|
|
200
|
+
const group = document.aggregation_group_type_index;
|
|
201
|
+
if (!isNumber(group) || !Number.isInteger(group)) throw malformed('aggregation_group_type_index');
|
|
202
|
+
throw unsupported('aggregation_group_type_index');
|
|
203
|
+
}
|
|
204
|
+
const defaultValue = required(document, 'default_value');
|
|
205
|
+
if (null !== defaultValue && !VALUE_CHECKS[returnType](defaultValue)) throw malformed('default_value');
|
|
206
|
+
const rules = array(document, 'rules', MAX_RULES).map((rule)=>readRule(rule, returnType));
|
|
207
|
+
if (new Set(rules.map((rule)=>rule.id)).size !== rules.length) throw malformed('rule.id');
|
|
208
|
+
const comparesDates = rules.some((rule)=>rule.targeting.some((predicate)=>DATE_OPERATORS.has(predicate.operator)));
|
|
209
|
+
return {
|
|
210
|
+
defaultValue: defaultValue,
|
|
211
|
+
rules,
|
|
212
|
+
comparesDates
|
|
213
|
+
};
|
|
214
|
+
}
|
|
215
|
+
function checkJson(document) {
|
|
216
|
+
const pending = [
|
|
217
|
+
[
|
|
218
|
+
document,
|
|
219
|
+
1
|
|
220
|
+
]
|
|
221
|
+
];
|
|
222
|
+
while(pending.length > 0){
|
|
223
|
+
const [value, depth] = pending.pop();
|
|
224
|
+
if (Array.isArray(value) || isJsonObject(value)) {
|
|
225
|
+
if (depth > MAX_JSON_DEPTH) throw malformed('filters');
|
|
226
|
+
if (Array.isArray(value)) for(let index = 0; index < value.length; index++)pending.push([
|
|
227
|
+
value[index],
|
|
228
|
+
depth + 1
|
|
229
|
+
]);
|
|
230
|
+
else for (const key of Object.keys(value)){
|
|
231
|
+
if (!isText(key)) throw malformed('filters');
|
|
232
|
+
pending.push([
|
|
233
|
+
value[key],
|
|
234
|
+
depth + 1
|
|
235
|
+
]);
|
|
236
|
+
}
|
|
237
|
+
} else if (!isJsonScalar(value)) throw malformed('filters');
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
function isJsonScalar(value) {
|
|
241
|
+
if (null === value || 'boolean' == typeof value) return true;
|
|
242
|
+
if ('string' == typeof value) return isText(value);
|
|
243
|
+
return 'number' == typeof value && Number.isFinite(value);
|
|
244
|
+
}
|
|
245
|
+
function isText(value) {
|
|
246
|
+
for(let index = 0; index < value.length; index++){
|
|
247
|
+
const unit = value.charCodeAt(index);
|
|
248
|
+
if (unit >= 0xd800 && unit <= 0xdbff) {
|
|
249
|
+
const next = value.charCodeAt(index + 1);
|
|
250
|
+
if (!(next >= 0xdc00 && next <= 0xdfff)) return false;
|
|
251
|
+
index++;
|
|
252
|
+
} else if (unit >= 0xdc00 && unit <= 0xdfff) return false;
|
|
253
|
+
}
|
|
254
|
+
return true;
|
|
255
|
+
}
|
|
256
|
+
function isJsonObject(value) {
|
|
257
|
+
if (null === value || 'object' != typeof value || Array.isArray(value)) return false;
|
|
258
|
+
const prototype = Object.getPrototypeOf(value);
|
|
259
|
+
return prototype === Object.prototype || null === prototype;
|
|
260
|
+
}
|
|
261
|
+
const isNumber = (value)=>'number' == typeof value && !Number.isNaN(value);
|
|
262
|
+
function object(value, field) {
|
|
263
|
+
if (!isJsonObject(value)) throw malformed(field);
|
|
264
|
+
return value;
|
|
265
|
+
}
|
|
266
|
+
function required(mapping, field) {
|
|
267
|
+
if (!hasOwn(mapping, field)) throw malformed(field);
|
|
268
|
+
return mapping[field];
|
|
269
|
+
}
|
|
270
|
+
function optional(mapping, field) {
|
|
271
|
+
return hasOwn(mapping, field) ? mapping[field] : void 0;
|
|
272
|
+
}
|
|
273
|
+
function string(mapping, field) {
|
|
274
|
+
const value = required(mapping, field);
|
|
275
|
+
if ('string' != typeof value) throw malformed(field);
|
|
276
|
+
return value;
|
|
277
|
+
}
|
|
278
|
+
function array(mapping, field, limit) {
|
|
279
|
+
const value = required(mapping, field);
|
|
280
|
+
if (!Array.isArray(value) || value.length > limit) throw malformed(field);
|
|
281
|
+
return value;
|
|
282
|
+
}
|
|
283
|
+
function checkClosed(mapping, fields, field) {
|
|
284
|
+
if (Object.keys(mapping).some((key)=>!fields.has(key))) throw malformed(field);
|
|
285
|
+
}
|
|
286
|
+
const isSafeNumber = (value)=>isNumber(value) && Math.abs(value) <= Number.MAX_SAFE_INTEGER;
|
|
287
|
+
const isObjectValue = (value)=>isJsonObject(value) && isNestedValue(value, 1);
|
|
288
|
+
function isNestedValue(value, depth) {
|
|
289
|
+
const items = Array.isArray(value) ? value : isJsonObject(value) ? Object.values(value) : void 0;
|
|
290
|
+
if (void 0 !== items) return depth <= MAX_OBJECT_DEPTH && items.every((item)=>isNestedValue(item, depth + 1));
|
|
291
|
+
return !isNumber(value) || isSafeNumber(value);
|
|
292
|
+
}
|
|
293
|
+
const VALUE_CHECKS = {
|
|
294
|
+
boolean: (value)=>'boolean' == typeof value,
|
|
295
|
+
string: (value)=>'string' == typeof value && '' !== value,
|
|
296
|
+
number: isSafeNumber,
|
|
297
|
+
object: isObjectValue
|
|
298
|
+
};
|
|
299
|
+
const RULE_ID_PATTERN = /^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$/;
|
|
300
|
+
const VARIANT_KEY_PATTERN = /^[A-Za-z0-9_-]+$/;
|
|
301
|
+
function readRule(value, returnType) {
|
|
302
|
+
const rule = object(value, 'rule');
|
|
303
|
+
const ruleType = string(rule, 'rule_type');
|
|
304
|
+
if (!hasOwn(FIELDS_BY_RULE_TYPE, ruleType)) throw malformed('rule_type');
|
|
305
|
+
checkClosed(rule, FIELDS_BY_RULE_TYPE[ruleType], 'rule');
|
|
306
|
+
let id = string(rule, 'id');
|
|
307
|
+
if (!RULE_ID_PATTERN.test(id)) throw malformed('rule.id');
|
|
308
|
+
id = id.toLowerCase();
|
|
309
|
+
if (hasOwn(rule, "description") && 'string' != typeof rule.description) throw malformed("description");
|
|
310
|
+
if (hasOwn(rule, 'metadata') && !isJsonObject(rule.metadata)) throw malformed('metadata');
|
|
311
|
+
const targeting = readTargeting(required(rule, 'targeting'));
|
|
312
|
+
if ('targeted_release' === ruleType) return {
|
|
313
|
+
ruleType,
|
|
314
|
+
id,
|
|
315
|
+
targeting,
|
|
316
|
+
value: typedValue(rule, returnType)
|
|
317
|
+
};
|
|
318
|
+
if ('percentage_rollout' === ruleType) {
|
|
319
|
+
const rollout = readRollout(rule);
|
|
320
|
+
return {
|
|
321
|
+
ruleType,
|
|
322
|
+
id,
|
|
323
|
+
targeting,
|
|
324
|
+
value: typedValue(rule, returnType),
|
|
325
|
+
rollout
|
|
326
|
+
};
|
|
327
|
+
}
|
|
328
|
+
const paused = required(rule, 'paused');
|
|
329
|
+
if ('boolean' != typeof paused) throw malformed('paused');
|
|
330
|
+
const rollout = readRollout(rule);
|
|
331
|
+
const variants = readVariants(rule, returnType);
|
|
332
|
+
const holdout = hasOwn(rule, 'holdout') ? readHoldout(rule.holdout) : void 0;
|
|
333
|
+
return {
|
|
334
|
+
ruleType: 'experiment',
|
|
335
|
+
id,
|
|
336
|
+
targeting,
|
|
337
|
+
paused,
|
|
338
|
+
rollout,
|
|
339
|
+
variants,
|
|
340
|
+
holdout
|
|
341
|
+
};
|
|
342
|
+
}
|
|
343
|
+
function typedValue(mapping, returnType) {
|
|
344
|
+
const value = required(mapping, 'value');
|
|
345
|
+
if (!VALUE_CHECKS[returnType](value)) throw malformed('value');
|
|
346
|
+
return value;
|
|
347
|
+
}
|
|
348
|
+
function readRollout(rule) {
|
|
349
|
+
if (string(rule, 'assignment_algorithm') !== ASSIGNMENT_ALGORITHM) throw malformed('assignment_algorithm');
|
|
350
|
+
if (hasOwn(rule, 'assign_by') && 'person' !== rule.assign_by) throw malformed('assign_by');
|
|
351
|
+
const onRolloutMiss = string(rule, 'on_rollout_miss');
|
|
352
|
+
if ('continue' !== onRolloutMiss && 'return_default' !== onRolloutMiss) throw malformed('on_rollout_miss');
|
|
353
|
+
return {
|
|
354
|
+
percentage: feature_flags_v2_percentage(rule, 'rollout_percentage'),
|
|
355
|
+
returnDefaultOnMiss: 'return_default' === onRolloutMiss,
|
|
356
|
+
seed: feature_flags_v2_seed(rule)
|
|
357
|
+
};
|
|
358
|
+
}
|
|
359
|
+
function feature_flags_v2_percentage(mapping, field) {
|
|
360
|
+
const value = required(mapping, field);
|
|
361
|
+
if (!isNumber(value) || !(value >= 0 && value <= 100) || Number(value.toFixed(2)) !== value) throw malformed(field);
|
|
362
|
+
return value;
|
|
363
|
+
}
|
|
364
|
+
function feature_flags_v2_seed(mapping) {
|
|
365
|
+
const value = string(mapping, 'seed');
|
|
366
|
+
const length = codePointLength(value);
|
|
367
|
+
if (length < 1 || length > MAX_SEED_LENGTH) throw malformed('seed');
|
|
368
|
+
return value;
|
|
369
|
+
}
|
|
370
|
+
function readVariants(rule, returnType) {
|
|
371
|
+
const items = array(rule, 'variants', MAX_VARIANTS);
|
|
372
|
+
if (items.length < MIN_VARIANTS) throw malformed('variants');
|
|
373
|
+
const variants = items.map((item)=>readVariant(item, returnType));
|
|
374
|
+
if (new Set(variants.map((variant)=>variant.key)).size !== variants.length) throw malformed('variant.key');
|
|
375
|
+
if (10000 !== variants.reduce((sum, variant)=>sum + Math.floor(100 * variant.weight + 0.5), 0)) throw malformed('variants');
|
|
376
|
+
return variants;
|
|
377
|
+
}
|
|
378
|
+
function readVariant(value, returnType) {
|
|
379
|
+
const variant = object(value, 'variant');
|
|
380
|
+
checkClosed(variant, VARIANT_FIELDS, 'variant');
|
|
381
|
+
const key = string(variant, 'key');
|
|
382
|
+
if (!VARIANT_KEY_PATTERN.test(key)) throw malformed('variant.key');
|
|
383
|
+
return {
|
|
384
|
+
key,
|
|
385
|
+
weight: feature_flags_v2_percentage(variant, 'weight'),
|
|
386
|
+
value: typedValue(variant, returnType)
|
|
387
|
+
};
|
|
388
|
+
}
|
|
389
|
+
function readHoldout(value) {
|
|
390
|
+
const holdout = object(value, 'holdout');
|
|
391
|
+
checkClosed(holdout, HOLDOUT_FIELDS, 'holdout');
|
|
392
|
+
return {
|
|
393
|
+
seed: feature_flags_v2_seed(holdout),
|
|
394
|
+
exclusionPercentage: feature_flags_v2_percentage(holdout, 'exclusion_percentage')
|
|
395
|
+
};
|
|
396
|
+
}
|
|
397
|
+
function readTargeting(value) {
|
|
398
|
+
const targeting = object(value, 'targeting');
|
|
399
|
+
checkClosed(targeting, TARGETING_FIELDS, 'targeting');
|
|
400
|
+
return array(targeting, 'properties', MAX_PREDICATES).map(readPredicate);
|
|
401
|
+
}
|
|
402
|
+
function readPredicate(value) {
|
|
403
|
+
const prop = object(value, 'property');
|
|
404
|
+
checkClosed(prop, PROPERTY_FIELDS, 'property');
|
|
405
|
+
const propertyType = string(prop, 'type');
|
|
406
|
+
if (UNSUPPORTED_PROPERTY_TYPES.has(propertyType)) throw unsupported('property.type');
|
|
407
|
+
if ('person' !== propertyType) throw malformed('property.type');
|
|
408
|
+
const key = string(prop, 'key');
|
|
409
|
+
if ('' === key) throw malformed('property.key');
|
|
410
|
+
let operator = optional(prop, 'operator') ?? null;
|
|
411
|
+
if (null === operator) operator = 'exact';
|
|
412
|
+
else if ('string' != typeof operator || !OPERATORS.has(operator)) throw malformed('property.operator');
|
|
413
|
+
const negation = optional(prop, 'negation') ?? null;
|
|
414
|
+
if (null !== negation && 'boolean' != typeof negation) throw malformed('property.negation');
|
|
415
|
+
checkUnreadFields(prop);
|
|
416
|
+
const filterValue = optional(prop, 'value') ?? null;
|
|
417
|
+
return {
|
|
418
|
+
key,
|
|
419
|
+
operator: operator,
|
|
420
|
+
value: filterValue,
|
|
421
|
+
negation: true === negation,
|
|
422
|
+
operand: readOperand(operator, filterValue)
|
|
423
|
+
};
|
|
424
|
+
}
|
|
425
|
+
function checkUnreadFields(prop) {
|
|
426
|
+
if ((optional(prop, 'group_type_index') ?? null) !== null) throw malformed('property.group_type_index');
|
|
427
|
+
for (const field of [
|
|
428
|
+
'cohort_name',
|
|
429
|
+
'label'
|
|
430
|
+
]){
|
|
431
|
+
const value = optional(prop, field) ?? null;
|
|
432
|
+
if (null !== value && 'string' != typeof value) throw malformed('property.presentation');
|
|
433
|
+
}
|
|
434
|
+
const names = optional(prop, 'group_key_names') ?? null;
|
|
435
|
+
if (null !== names && !(isJsonObject(names) && Object.values(names).every((name)=>'string' == typeof name))) throw malformed('property.group_key_names');
|
|
436
|
+
}
|
|
437
|
+
function readOperand(operator, value) {
|
|
438
|
+
if (NUMBER_OPERATORS.has(operator)) {
|
|
439
|
+
if ('string' == typeof value || isNumber(value)) return toNumber(value);
|
|
440
|
+
} else if (SEMVER_COMPARISON_OPERATORS.has(operator) || SEMVER_RANGE_OPERATORS.has(operator)) {
|
|
441
|
+
if ('string' == typeof value) return readSemverFilter(operator, value);
|
|
442
|
+
} else if (DATE_OPERATORS.has(operator)) {
|
|
443
|
+
if ('string' == typeof value) return readDateFilter(value);
|
|
444
|
+
} else if (MULTI_TEXT_OPERATORS.has(operator)) {
|
|
445
|
+
if (Array.isArray(value) && value.every((item)=>'string' == typeof item)) return null;
|
|
446
|
+
} else if (REGEX_OPERATORS.has(operator) || TEXT_OPERATORS.has(operator)) {
|
|
447
|
+
if ('string' == typeof value) return null;
|
|
448
|
+
} else if (!EXACT_OPERATORS.has(operator)) return null;
|
|
449
|
+
else if (isStringOrBoolean(value) || Array.isArray(value) && value.length > 0 && value.every(isStringOrBoolean)) return null;
|
|
450
|
+
throw malformed('property.value');
|
|
451
|
+
}
|
|
452
|
+
const isStringOrBoolean = (value)=>'string' == typeof value || 'boolean' == typeof value;
|
|
453
|
+
function evaluateConfigV2(config, context, options = {}) {
|
|
454
|
+
return evaluateConfigV2WithHash(config, context, feature_flags_v2_drawHash, options);
|
|
455
|
+
}
|
|
456
|
+
async function evaluateConfigV2WithHash(config, context, draw, { active = true } = {}) {
|
|
457
|
+
if (!active) return {
|
|
458
|
+
outcome: 'evaluated',
|
|
459
|
+
value: null,
|
|
460
|
+
reason: 'flag_disabled'
|
|
461
|
+
};
|
|
462
|
+
if (isConfigV2Unsupported(config)) return config;
|
|
463
|
+
if (void 0 === context.now && config.comparesDates) context = {
|
|
464
|
+
...context,
|
|
465
|
+
now: new Date()
|
|
466
|
+
};
|
|
467
|
+
const draws = new Draws(context.identifier, draw);
|
|
468
|
+
try {
|
|
469
|
+
for(let index = 0; index < config.rules.length; index++){
|
|
470
|
+
const rule = config.rules[index];
|
|
471
|
+
if (!rule.targeting.every((predicate)=>matches(predicate, context))) continue;
|
|
472
|
+
const result = await applyRule(rule, index, config.defaultValue, draws);
|
|
473
|
+
if (void 0 !== result) return result;
|
|
474
|
+
}
|
|
475
|
+
} catch (error) {
|
|
476
|
+
if (error instanceof Halt) return error.result;
|
|
477
|
+
if (error instanceof InconclusiveMatchError) return {
|
|
478
|
+
outcome: 'inconclusive',
|
|
479
|
+
reason: error.reason,
|
|
480
|
+
detail: error.message
|
|
481
|
+
};
|
|
482
|
+
throw error;
|
|
483
|
+
}
|
|
484
|
+
return {
|
|
485
|
+
outcome: 'evaluated',
|
|
486
|
+
value: config.defaultValue,
|
|
487
|
+
reason: 'no_rule_match'
|
|
488
|
+
};
|
|
489
|
+
}
|
|
490
|
+
async function applyRule(rule, index, defaultValue, draws) {
|
|
491
|
+
const result = (reason, value, variantKey)=>({
|
|
492
|
+
outcome: 'evaluated',
|
|
493
|
+
value,
|
|
494
|
+
reason,
|
|
495
|
+
ruleId: rule.id,
|
|
496
|
+
ruleType: rule.ruleType,
|
|
497
|
+
ruleIndex: index,
|
|
498
|
+
...void 0 !== variantKey ? {
|
|
499
|
+
variantKey
|
|
500
|
+
} : {}
|
|
501
|
+
});
|
|
502
|
+
if ('targeted_release' === rule.ruleType) return result('targeting_match', rule.value);
|
|
503
|
+
if ('experiment' === rule.ruleType) {
|
|
504
|
+
if (rule.paused) return result('experiment_paused', defaultValue);
|
|
505
|
+
const holdout = rule.holdout;
|
|
506
|
+
if (void 0 !== holdout && await draws.includes('holdout', holdout.seed, holdout.exclusionPercentage)) return result('holdout', defaultValue);
|
|
507
|
+
}
|
|
508
|
+
const rollout = rule.rollout;
|
|
509
|
+
if (await draws.includes('rollout', rollout.seed, rollout.percentage)) {
|
|
510
|
+
if ('percentage_rollout' === rule.ruleType) return result('targeting_match', rule.value);
|
|
511
|
+
const variant = selectVariant(await draws.draw('variant', rollout.seed), rule.variants);
|
|
512
|
+
return result('experiment_split', variant.value, variant.key);
|
|
513
|
+
}
|
|
514
|
+
if (rollout.returnDefaultOnMiss) return result('rollout_miss', defaultValue);
|
|
515
|
+
}
|
|
516
|
+
async function hash01(prefix, identifier, salt) {
|
|
517
|
+
const digest = await hashSHA1(prefix + identifier + salt);
|
|
518
|
+
return parseInt(digest.slice(0, 15), 16) / HASH_SCALE;
|
|
519
|
+
}
|
|
520
|
+
function feature_flags_v2_drawHash(use, seed, subject) {
|
|
521
|
+
if ('holdout' === use) return hash01(seed, subject, '');
|
|
522
|
+
return hash01(`${seed}.`, subject, 'variant' === use ? 'variant' : '');
|
|
523
|
+
}
|
|
524
|
+
async function inRollout(percentage, draw) {
|
|
525
|
+
return 100 === percentage || await draw() <= percentage / 100;
|
|
526
|
+
}
|
|
527
|
+
function selectVariant(hash, variants) {
|
|
528
|
+
let boundary = 0;
|
|
529
|
+
for (const variant of variants){
|
|
530
|
+
boundary += variant.weight / 100;
|
|
531
|
+
if (hash < boundary) return variant;
|
|
532
|
+
}
|
|
533
|
+
return variants[variants.length - 1];
|
|
534
|
+
}
|
|
535
|
+
function truncateIdentifier(identifier) {
|
|
536
|
+
return codePointLength(identifier) <= MAX_IDENTIFIER_LENGTH ? identifier : Array.from(identifier).slice(0, MAX_IDENTIFIER_LENGTH).join('');
|
|
537
|
+
}
|
|
538
|
+
function codePointLength(value) {
|
|
539
|
+
let length = 0;
|
|
540
|
+
for (const _ of value)length++;
|
|
541
|
+
return length;
|
|
542
|
+
}
|
|
543
|
+
class Draws {
|
|
544
|
+
constructor(identifier, drawHash){
|
|
545
|
+
this.identifier = identifier;
|
|
546
|
+
this.drawHash = drawHash;
|
|
547
|
+
this.subject = truncateIdentifier(identifier);
|
|
548
|
+
}
|
|
549
|
+
async draw(use, seed) {
|
|
550
|
+
const key = `${use}\0${seed}`;
|
|
551
|
+
let hash = this.hashes?.get(key);
|
|
552
|
+
if (void 0 === hash) {
|
|
553
|
+
if (!isText(this.identifier)) throw unusableContext('The identifier is not valid Unicode, so it cannot be hashed');
|
|
554
|
+
hash = await this.drawHash(use, seed, this.subject);
|
|
555
|
+
this.hashes ??= new Map();
|
|
556
|
+
this.hashes.set(key, hash);
|
|
557
|
+
}
|
|
558
|
+
return hash;
|
|
559
|
+
}
|
|
560
|
+
async includes(use, seed, percentage) {
|
|
561
|
+
return '' !== this.subject && await inRollout(percentage, ()=>this.draw(use, seed));
|
|
562
|
+
}
|
|
563
|
+
}
|
|
564
|
+
function matches(predicate, context) {
|
|
565
|
+
const properties = context.properties;
|
|
566
|
+
if (null == properties) throw unusableContext('No person properties were passed');
|
|
567
|
+
const present = hasOwn(properties, predicate.key) && isKeptByJson(properties[predicate.key]);
|
|
568
|
+
if (!present && !context.propertiesComplete) throw unusableContext(`The person property '${predicate.key}' was not passed`);
|
|
569
|
+
if (REGEX_OPERATORS.has(predicate.operator) && !isValidPattern(predicate.value)) throw new Halt({
|
|
570
|
+
outcome: 'error',
|
|
571
|
+
kind: 'invalid_regex',
|
|
572
|
+
detail: `The pattern for '${predicate.key}' is not valid`
|
|
573
|
+
});
|
|
574
|
+
const matched = matchOperator(predicate, present, present ? properties[predicate.key] : void 0, context);
|
|
575
|
+
return matched !== predicate.negation;
|
|
576
|
+
}
|
|
577
|
+
function isKeptByJson(value) {
|
|
578
|
+
return void 0 !== value && 'function' != typeof value && 'symbol' != typeof value;
|
|
579
|
+
}
|
|
580
|
+
function matchOperator(predicate, present, value, context) {
|
|
581
|
+
const operator = predicate.operator;
|
|
582
|
+
if (PRESENCE_OPERATORS.has(operator)) return 'is_set' === operator ? present : !present;
|
|
583
|
+
if (!present) return OPERATORS_MATCHING_ABSENCE.has(operator);
|
|
584
|
+
if (EXACT_OPERATORS.has(operator)) {
|
|
585
|
+
const matched = matchExact(predicate.value, value, context.explicitExactMatching);
|
|
586
|
+
return 'exact' === operator ? matched : !matched;
|
|
587
|
+
}
|
|
588
|
+
if (TEXT_OPERATORS.has(operator)) return matchText(operator, predicate.value, value);
|
|
589
|
+
if (MULTI_TEXT_OPERATORS.has(operator)) return matchAnySubstring(predicate, value);
|
|
590
|
+
if (REGEX_OPERATORS.has(operator)) {
|
|
591
|
+
const found = matchPattern(predicate.value, jsonText(value));
|
|
592
|
+
return 'regex' === operator ? found : !found;
|
|
593
|
+
}
|
|
594
|
+
if (NUMBER_OPERATORS.has(operator)) return matchNumber(predicate, value);
|
|
595
|
+
if (DATE_OPERATORS.has(operator)) return matchDate(predicate, value, context);
|
|
596
|
+
return matchSemver(predicate, value);
|
|
597
|
+
}
|
|
598
|
+
function matchExact(filterValue, value, explicitExactMatching) {
|
|
599
|
+
const valueText = jsonText(value).toLowerCase();
|
|
600
|
+
if (!explicitExactMatching && isTruthyOrFalsy(filterValue)) return isTruthy(filterValue) === isTruthy(value);
|
|
601
|
+
if (Array.isArray(filterValue)) return filterValue.map((candidate)=>String(candidate).toLowerCase()).includes(valueText);
|
|
602
|
+
return String(filterValue).toLowerCase() === valueText;
|
|
603
|
+
}
|
|
604
|
+
function isTruthyOrFalsy(value) {
|
|
605
|
+
if ('boolean' == typeof value) return true;
|
|
606
|
+
if ('string' == typeof value) {
|
|
607
|
+
const lower = value.toLowerCase();
|
|
608
|
+
return 'true' === lower || 'false' === lower;
|
|
609
|
+
}
|
|
610
|
+
return Array.isArray(value) && value.every(isTruthyOrFalsy);
|
|
611
|
+
}
|
|
612
|
+
function isTruthy(value) {
|
|
613
|
+
if ('boolean' == typeof value) return value;
|
|
614
|
+
if ('string' == typeof value) return 'true' === value.toLowerCase();
|
|
615
|
+
return Array.isArray(value) && value.every(isTruthy);
|
|
616
|
+
}
|
|
617
|
+
function matchText(operator, filterValue, value) {
|
|
618
|
+
const valueText = asciiLower(jsonText(value));
|
|
619
|
+
const filterText = asciiLower(filterValue);
|
|
620
|
+
let matched;
|
|
621
|
+
matched = 'icontains' === operator || 'not_icontains' === operator ? valueText.includes(filterText) : 'starts_with' === operator || 'not_starts_with' === operator ? valueText.startsWith(filterText) : valueText.endsWith(filterText);
|
|
622
|
+
return operator.startsWith('not_') ? !matched : matched;
|
|
623
|
+
}
|
|
624
|
+
function matchAnySubstring(predicate, value) {
|
|
625
|
+
const valueText = asciiLower(jsonText(value));
|
|
626
|
+
let found = false;
|
|
627
|
+
for (const item of predicate.value)if (valueText.includes(asciiLower(item))) {
|
|
628
|
+
found = true;
|
|
629
|
+
break;
|
|
630
|
+
}
|
|
631
|
+
return 'icontains_multi' === predicate.operator ? found : !found;
|
|
632
|
+
}
|
|
633
|
+
function asciiLower(value) {
|
|
634
|
+
return value.replace(/[A-Z]/g, (character)=>character.toLowerCase());
|
|
635
|
+
}
|
|
636
|
+
function isValidPattern(pattern) {
|
|
637
|
+
if ('' === pattern) return true;
|
|
638
|
+
matchPattern(pattern, '');
|
|
639
|
+
try {
|
|
640
|
+
new RegExp(pattern);
|
|
641
|
+
return true;
|
|
642
|
+
} catch (error) {
|
|
643
|
+
if (error instanceof SyntaxError) return false;
|
|
644
|
+
throw new InconclusiveMatchError('Regex compilation requires server evaluation', 'unsupported_definition');
|
|
645
|
+
}
|
|
646
|
+
}
|
|
647
|
+
function matchPattern(pattern, text) {
|
|
648
|
+
return matchFeatureFlagProperty({
|
|
649
|
+
key: 'value',
|
|
650
|
+
value: pattern,
|
|
651
|
+
operator: 'regex'
|
|
652
|
+
}, {
|
|
653
|
+
value: text
|
|
654
|
+
});
|
|
655
|
+
}
|
|
656
|
+
function matchNumber(predicate, value) {
|
|
657
|
+
const number = toNumber(value);
|
|
658
|
+
if (null === number) throw invalidProperty(`The person property '${predicate.key}' is not a number`);
|
|
659
|
+
const operand = predicate.operand;
|
|
660
|
+
if (null === operand) throw invalidProperty(`The filter value for '${predicate.key}' is not a number`);
|
|
661
|
+
switch(predicate.operator){
|
|
662
|
+
case 'gt':
|
|
663
|
+
return number > operand;
|
|
664
|
+
case 'gte':
|
|
665
|
+
return number >= operand;
|
|
666
|
+
case 'lt':
|
|
667
|
+
return number < operand;
|
|
668
|
+
default:
|
|
669
|
+
return number <= operand;
|
|
670
|
+
}
|
|
671
|
+
}
|
|
672
|
+
const NUMBER_PATTERN = /^[+-]?(?:inf|infinity|nan|(?:[0-9]+(?:\.[0-9]*)?|\.[0-9]+)(?:e[+-]?[0-9]+)?)$/i;
|
|
673
|
+
function parseNumberText(text) {
|
|
674
|
+
const unsigned = text.replace(/^[+-]/, '').toLowerCase();
|
|
675
|
+
if ('nan' === unsigned) return 0 / 0;
|
|
676
|
+
if ('inf' === unsigned || 'infinity' === unsigned) return text.startsWith('-') ? -1 / 0 : 1 / 0;
|
|
677
|
+
return Number(text);
|
|
678
|
+
}
|
|
679
|
+
function toNumber(value) {
|
|
680
|
+
if ('string' == typeof value) return NUMBER_PATTERN.test(value) ? parseNumberText(value) : null;
|
|
681
|
+
if ('number' != typeof value) {
|
|
682
|
+
if (null === value || 'boolean' == typeof value || Array.isArray(value) || isJsonObject(value)) return null;
|
|
683
|
+
throw unusableContext(`A property value of type ${describeType(value)} is not a JSON value`);
|
|
684
|
+
}
|
|
685
|
+
if (!Number.isFinite(value)) throw unusableContext('A property value is not a finite number');
|
|
686
|
+
return value;
|
|
687
|
+
}
|
|
688
|
+
function describeType(value) {
|
|
689
|
+
if ('object' != typeof value || null === value) return typeof value;
|
|
690
|
+
return Object.getPrototypeOf(value)?.constructor?.name ?? 'object';
|
|
691
|
+
}
|
|
692
|
+
function jsonText(value) {
|
|
693
|
+
if ('string' == typeof value) {
|
|
694
|
+
if (!isText(value)) throw new InconclusiveMatchError('A string with a lone surrogate is not JSON text', 'missing_context');
|
|
695
|
+
return value;
|
|
696
|
+
}
|
|
697
|
+
return jsonValueText(value, 1);
|
|
698
|
+
}
|
|
699
|
+
function jsonValueText(value, depth) {
|
|
700
|
+
if ((Array.isArray(value) || isJsonObject(value)) && depth > MAX_JSON_DEPTH) throw new InconclusiveMatchError('A value is nested too deeply to compare', 'missing_context');
|
|
701
|
+
if (null === value) return 'null';
|
|
702
|
+
if ('boolean' == typeof value) return value ? 'true' : 'false';
|
|
703
|
+
if ('string' == typeof value) {
|
|
704
|
+
if (!isText(value)) throw new InconclusiveMatchError('A string with a lone surrogate is not JSON text', 'missing_context');
|
|
705
|
+
return JSON.stringify(value);
|
|
706
|
+
}
|
|
707
|
+
if ('number' == typeof value) return numberText(value);
|
|
708
|
+
if (Array.isArray(value)) {
|
|
709
|
+
const items = [];
|
|
710
|
+
for(let index = 0; index < value.length; index++)items.push(jsonValueText(value[index], depth + 1));
|
|
711
|
+
return `[${items.join(',')}]`;
|
|
712
|
+
}
|
|
713
|
+
if (isJsonObject(value)) {
|
|
714
|
+
const keys = Object.keys(value).sort(compareCodePoints);
|
|
715
|
+
return `{${keys.map((key)=>`${jsonValueText(key, depth + 1)}:${jsonValueText(value[key], depth + 1)}`).join(',')}}`;
|
|
716
|
+
}
|
|
717
|
+
throw new InconclusiveMatchError(`A property value of type ${describeType(value)} is not a JSON value`, 'missing_context');
|
|
718
|
+
}
|
|
719
|
+
function numberText(value) {
|
|
720
|
+
if (!Number.isFinite(value)) throw new InconclusiveMatchError('A number that is not finite is not JSON text', 'missing_context');
|
|
721
|
+
if (Number.isInteger(value)) {
|
|
722
|
+
if (Math.abs(value) > Number.MAX_SAFE_INTEGER) throw new InconclusiveMatchError(`The JSON text of ${value} may be an integer or a decimal, which the flags service compares differently`, 'missing_context');
|
|
723
|
+
return String(value);
|
|
724
|
+
}
|
|
725
|
+
return floatText(value);
|
|
726
|
+
}
|
|
727
|
+
function floatText(value) {
|
|
728
|
+
const [mantissa, exponentText] = Math.abs(value).toExponential().split('e');
|
|
729
|
+
const digits = mantissa.replace('.', '');
|
|
730
|
+
const point = Number(exponentText) + 1;
|
|
731
|
+
let text;
|
|
732
|
+
if (point > 0 && point <= 16) text = digits.length <= point ? `${digits}${'0'.repeat(point - digits.length)}.0` : `${digits.slice(0, point)}.${digits.slice(point)}`;
|
|
733
|
+
else if (point > -5 && point <= 0) text = `0.${'0'.repeat(-point)}${digits}`;
|
|
734
|
+
else {
|
|
735
|
+
const fraction = digits.length > 1 ? `.${digits.slice(1)}` : '';
|
|
736
|
+
const exponent = point - 1;
|
|
737
|
+
text = `${digits[0]}${fraction}e${exponent >= 0 ? '+' : ''}${exponent}`;
|
|
738
|
+
}
|
|
739
|
+
return value < 0 ? `-${text}` : text;
|
|
740
|
+
}
|
|
741
|
+
function compareCodePoints(left, right) {
|
|
742
|
+
const leftPoints = Array.from(left);
|
|
743
|
+
const rightPoints = Array.from(right);
|
|
744
|
+
for(let index = 0; index < Math.min(leftPoints.length, rightPoints.length); index++){
|
|
745
|
+
const difference = leftPoints[index].codePointAt(0) - rightPoints[index].codePointAt(0);
|
|
746
|
+
if (0 !== difference) return difference;
|
|
747
|
+
}
|
|
748
|
+
return leftPoints.length - rightPoints.length;
|
|
749
|
+
}
|
|
750
|
+
const WHITESPACE = new Set([
|
|
751
|
+
0x09,
|
|
752
|
+
0x0a,
|
|
753
|
+
0x0b,
|
|
754
|
+
0x0c,
|
|
755
|
+
0x0d,
|
|
756
|
+
0x20,
|
|
757
|
+
0x85,
|
|
758
|
+
0xa0,
|
|
759
|
+
0x1680,
|
|
760
|
+
0x2000,
|
|
761
|
+
0x2001,
|
|
762
|
+
0x2002,
|
|
763
|
+
0x2003,
|
|
764
|
+
0x2004,
|
|
765
|
+
0x2005,
|
|
766
|
+
0x2006,
|
|
767
|
+
0x2007,
|
|
768
|
+
0x2008,
|
|
769
|
+
0x2009,
|
|
770
|
+
0x200a,
|
|
771
|
+
0x2028,
|
|
772
|
+
0x2029,
|
|
773
|
+
0x202f,
|
|
774
|
+
0x205f,
|
|
775
|
+
0x3000
|
|
776
|
+
].map((code)=>String.fromCharCode(code)));
|
|
777
|
+
const INTEGER = '(?:0|[1-9][0-9]*)';
|
|
778
|
+
const PRE_RELEASE_PART = `(?:${INTEGER}|[0-9]*[A-Za-z-][0-9A-Za-z-]*)`;
|
|
779
|
+
const PRE_RELEASE = `${PRE_RELEASE_PART}(?:\\.${PRE_RELEASE_PART})*`;
|
|
780
|
+
const BUILD = '[0-9A-Za-z-]+(?:\\.[0-9A-Za-z-]+)*';
|
|
781
|
+
const VERSION_PATTERN = new RegExp(`^(${INTEGER})\\.(${INTEGER})\\.(${INTEGER})(?:-(${PRE_RELEASE}))?(?:\\+(${BUILD}))?$`);
|
|
782
|
+
const REQUIREMENT_PATTERN = new RegExp(`^(?<major>${INTEGER})(?:(?<anyMinor>\\.[xX*](?:\\.[xX*])?)|\\.(?<minor>${INTEGER})(?:(?<anyPatch>\\.[xX*])|\\.(?<patch>${INTEGER})(?:-(?<pre>${PRE_RELEASE}))?(?:\\+${BUILD})?)?)?$`);
|
|
783
|
+
const UNSIGNED_INTEGER_PATTERN = /^\+?([0-9]+)$/;
|
|
784
|
+
function compareKeys(left, right) {
|
|
785
|
+
for(let index = 0; index < Math.min(left.length, right.length); index++){
|
|
786
|
+
const a = left[index];
|
|
787
|
+
const b = right[index];
|
|
788
|
+
const difference = Array.isArray(a) && Array.isArray(b) ? compareKeys(a, b) : a < b ? -1 : a > b ? 1 : 0;
|
|
789
|
+
if (0 !== difference) return difference;
|
|
790
|
+
}
|
|
791
|
+
return left.length - right.length;
|
|
792
|
+
}
|
|
793
|
+
function isU64(digits) {
|
|
794
|
+
const significant = digits.replace(/^0+/, '');
|
|
795
|
+
return significant.length <= 20 && BigInt(significant || '0') <= MAX_U64;
|
|
796
|
+
}
|
|
797
|
+
function stripWhitespace(text) {
|
|
798
|
+
let start = 0;
|
|
799
|
+
let end = text.length;
|
|
800
|
+
while(start < end && WHITESPACE.has(text[start]))start++;
|
|
801
|
+
while(end > start && WHITESPACE.has(text[end - 1]))end--;
|
|
802
|
+
return text.slice(start, end);
|
|
803
|
+
}
|
|
804
|
+
function parseVersion(text) {
|
|
805
|
+
const match = VERSION_PATTERN.exec(canonicalVersion(trimVersion(text)));
|
|
806
|
+
if (null === match || ![
|
|
807
|
+
match[1],
|
|
808
|
+
match[2],
|
|
809
|
+
match[3]
|
|
810
|
+
].every(isU64)) return null;
|
|
811
|
+
return {
|
|
812
|
+
major: BigInt(match[1]),
|
|
813
|
+
minor: BigInt(match[2]),
|
|
814
|
+
patch: BigInt(match[3]),
|
|
815
|
+
pre: match[4] ?? '',
|
|
816
|
+
build: match[5] ?? ''
|
|
817
|
+
};
|
|
818
|
+
}
|
|
819
|
+
function trimVersion(text) {
|
|
820
|
+
return stripWhitespace(text.startsWith('v') ? text.slice(1) : text);
|
|
821
|
+
}
|
|
822
|
+
function canonicalVersion(version) {
|
|
823
|
+
const suffix = version.search(/[-+]/);
|
|
824
|
+
const core = -1 === suffix ? version : version.slice(0, suffix);
|
|
825
|
+
const parts = core.split('.').map(stripLeadingZeros);
|
|
826
|
+
while(parts.length < 3)parts.push('0');
|
|
827
|
+
return parts.join('.') + version.slice(core.length);
|
|
828
|
+
}
|
|
829
|
+
function stripLeadingZeros(part) {
|
|
830
|
+
if (!/^[0-9]+$/.test(part)) return part;
|
|
831
|
+
return part.replace(/^0+/, '') || '0';
|
|
832
|
+
}
|
|
833
|
+
function preReleaseKey(pre) {
|
|
834
|
+
if ('' === pre) return [
|
|
835
|
+
1
|
|
836
|
+
];
|
|
837
|
+
return [
|
|
838
|
+
0,
|
|
839
|
+
pre.split('.').map((part)=>/^[0-9]+$/.test(part) ? [
|
|
840
|
+
0,
|
|
841
|
+
part.length,
|
|
842
|
+
part
|
|
843
|
+
] : [
|
|
844
|
+
1,
|
|
845
|
+
part
|
|
846
|
+
])
|
|
847
|
+
];
|
|
848
|
+
}
|
|
849
|
+
function buildKey(build) {
|
|
850
|
+
return build.split('.').map((part)=>{
|
|
851
|
+
if ('' === part || /^[0-9]+$/.test(part)) {
|
|
852
|
+
const digits = part.replace(/^0+/, '');
|
|
853
|
+
return [
|
|
854
|
+
0,
|
|
855
|
+
digits.length,
|
|
856
|
+
digits,
|
|
857
|
+
part.length
|
|
858
|
+
];
|
|
859
|
+
}
|
|
860
|
+
return [
|
|
861
|
+
1,
|
|
862
|
+
part
|
|
863
|
+
];
|
|
864
|
+
});
|
|
865
|
+
}
|
|
866
|
+
function versionKey(version) {
|
|
867
|
+
return [
|
|
868
|
+
version.major,
|
|
869
|
+
version.minor,
|
|
870
|
+
version.patch,
|
|
871
|
+
preReleaseKey(version.pre),
|
|
872
|
+
buildKey(version.build)
|
|
873
|
+
];
|
|
874
|
+
}
|
|
875
|
+
function readSemverFilter(operator, text) {
|
|
876
|
+
const admitted = 'semver_wildcard' === operator ? trimWildcards(text) : text;
|
|
877
|
+
const parsed = parseVersion(admitted);
|
|
878
|
+
const numbers = admitted.split('-')[0].split('.').slice(0, 3);
|
|
879
|
+
if (null === parsed || !numbers.every(isPlainNumber)) throw malformed('property.value');
|
|
880
|
+
if (SEMVER_COMPARISON_OPERATORS.has(operator)) return {
|
|
881
|
+
kind: 'version',
|
|
882
|
+
key: versionKey(parsed)
|
|
883
|
+
};
|
|
884
|
+
const version = trimVersion(text);
|
|
885
|
+
if ('semver_wildcard' === operator) {
|
|
886
|
+
const parts = version.split('.').map(stripLeadingZeros);
|
|
887
|
+
return parseRequirement(parts.join('.').replace(/\*/g, 'x'), null);
|
|
888
|
+
}
|
|
889
|
+
return parseRequirement(canonicalVersion(version), 'semver_tilde' === operator ? 'tilde' : 'caret');
|
|
890
|
+
}
|
|
891
|
+
function trimWildcards(text) {
|
|
892
|
+
let end = text.length;
|
|
893
|
+
while(end > 0 && ('.' === text[end - 1] || '*' === text[end - 1]))end--;
|
|
894
|
+
return text.slice(0, end);
|
|
895
|
+
}
|
|
896
|
+
function isPlainNumber(text) {
|
|
897
|
+
const number = UNSIGNED_INTEGER_PATTERN.exec(stripWhitespace(text));
|
|
898
|
+
return null !== number && isU64(number[1]);
|
|
899
|
+
}
|
|
900
|
+
function parseRequirement(text, kind) {
|
|
901
|
+
const groups = REQUIREMENT_PATTERN.exec(text)?.groups;
|
|
902
|
+
if (void 0 === groups) return null;
|
|
903
|
+
const { major, minor, patch } = groups;
|
|
904
|
+
if (![
|
|
905
|
+
major,
|
|
906
|
+
minor,
|
|
907
|
+
patch
|
|
908
|
+
].every((number)=>!number || isU64(number))) return null;
|
|
909
|
+
const resolvedKind = kind ?? (groups.anyMinor || groups.anyPatch ? 'wildcard' : 'caret');
|
|
910
|
+
return {
|
|
911
|
+
kind: resolvedKind,
|
|
912
|
+
major: BigInt(major),
|
|
913
|
+
minor: minor ? BigInt(minor) : null,
|
|
914
|
+
patch: patch ? BigInt(patch) : null,
|
|
915
|
+
pre: groups.pre ?? ''
|
|
916
|
+
};
|
|
917
|
+
}
|
|
918
|
+
function matchSemver(predicate, value) {
|
|
919
|
+
const version = parseVersion(jsonText(value));
|
|
920
|
+
if (null === version) return false;
|
|
921
|
+
const operand = predicate.operand;
|
|
922
|
+
if (null !== operand && 'version' === operand.kind) {
|
|
923
|
+
const order = compareKeys(versionKey(version), operand.key);
|
|
924
|
+
switch(predicate.operator){
|
|
925
|
+
case 'semver_eq':
|
|
926
|
+
return 0 === order;
|
|
927
|
+
case 'semver_neq':
|
|
928
|
+
return 0 !== order;
|
|
929
|
+
case 'semver_gt':
|
|
930
|
+
return order > 0;
|
|
931
|
+
case 'semver_gte':
|
|
932
|
+
return order >= 0;
|
|
933
|
+
case 'semver_lt':
|
|
934
|
+
return order < 0;
|
|
935
|
+
default:
|
|
936
|
+
return order <= 0;
|
|
937
|
+
}
|
|
938
|
+
}
|
|
939
|
+
if (null === operand) throw invalidProperty(`The filter value for '${predicate.key}' is not a version range`);
|
|
940
|
+
return satisfies(version, operand);
|
|
941
|
+
}
|
|
942
|
+
function satisfies(version, requirement) {
|
|
943
|
+
if (!inRange(version, requirement)) return false;
|
|
944
|
+
return '' === version.pre || '' !== requirement.pre && version.major === requirement.major && version.minor === requirement.minor && version.patch === requirement.patch;
|
|
945
|
+
}
|
|
946
|
+
function inRange(version, requirement) {
|
|
947
|
+
const { kind, major, minor, patch, pre } = requirement;
|
|
948
|
+
if (version.major !== major) return false;
|
|
949
|
+
if ('wildcard' === kind) return (null === minor || minor === version.minor) && (null === patch || patch === version.patch) && version.pre === pre;
|
|
950
|
+
const reached = compareKeys(preReleaseKey(version.pre), preReleaseKey(pre)) >= 0;
|
|
951
|
+
if ('tilde' === kind) {
|
|
952
|
+
if (null !== minor && version.minor !== minor) return false;
|
|
953
|
+
if (null !== patch && version.patch !== patch) return version.patch > patch;
|
|
954
|
+
return reached;
|
|
955
|
+
}
|
|
956
|
+
if (null === minor) return true;
|
|
957
|
+
if (null === patch) return major > BigInt(0) ? version.minor >= minor : version.minor === minor;
|
|
958
|
+
if (major > BigInt(0)) {
|
|
959
|
+
if (version.minor !== minor) return version.minor > minor;
|
|
960
|
+
if (version.patch !== patch) return version.patch > patch;
|
|
961
|
+
} else if (minor > BigInt(0)) {
|
|
962
|
+
if (version.minor !== minor) return false;
|
|
963
|
+
if (version.patch !== patch) return version.patch > patch;
|
|
964
|
+
} else if (version.minor !== minor || version.patch !== patch) return false;
|
|
965
|
+
return reached;
|
|
966
|
+
}
|
|
967
|
+
const MIN_EPOCH_SECONDS = -62135596800;
|
|
968
|
+
const MAX_EPOCH_SECONDS = 253402300799;
|
|
969
|
+
const SECONDS_PER_DAY = 86400;
|
|
970
|
+
const RELATIVE_DATE_PATTERN = /^-?([0-9]+)([hdwmy])$/;
|
|
971
|
+
const HOURS_PER_INTERVAL = {
|
|
972
|
+
h: 1,
|
|
973
|
+
d: 24,
|
|
974
|
+
w: 168
|
|
975
|
+
};
|
|
976
|
+
const DATE_PATTERN = /^(?<year>[0-9]{4})-(?<month>[0-9]{2})-(?<day>[0-9]{2})(?:(?<separator>[T ])(?<hour>[0-9]{2}):(?<minute>[0-9]{2})(?::(?<second>[0-9]{2})(?:\.(?<fraction>[0-9]{1,9}))?)?(?<offset>Z|[+-][0-9]{2}:[0-9]{2})?)?$/;
|
|
977
|
+
function parseDate(text) {
|
|
978
|
+
const relative = RELATIVE_DATE_PATTERN.exec(text);
|
|
979
|
+
if (null !== relative) {
|
|
980
|
+
const digits = relative[1].replace(/^0+/, '');
|
|
981
|
+
if (digits.length > 4) return null;
|
|
982
|
+
return {
|
|
983
|
+
kind: 'relative',
|
|
984
|
+
number: Number(digits || '0'),
|
|
985
|
+
interval: relative[2]
|
|
986
|
+
};
|
|
987
|
+
}
|
|
988
|
+
const groups = DATE_PATTERN.exec(text)?.groups;
|
|
989
|
+
if (void 0 === groups) return null;
|
|
990
|
+
const offset = parseOffset(groups.offset);
|
|
991
|
+
if (void 0 !== groups.offset && (null === offset || 'T' !== groups.separator || void 0 === groups.second)) return null;
|
|
992
|
+
const wallClock = {
|
|
993
|
+
year: Number(groups.year),
|
|
994
|
+
month: Number(groups.month),
|
|
995
|
+
day: Number(groups.day),
|
|
996
|
+
hour: Number(groups.hour ?? 0),
|
|
997
|
+
minute: Number(groups.minute ?? 0),
|
|
998
|
+
second: Number(groups.second ?? 0)
|
|
999
|
+
};
|
|
1000
|
+
if (!isValidWallClock(wallClock)) return null;
|
|
1001
|
+
const fraction = groups.fraction ?? '';
|
|
1002
|
+
return {
|
|
1003
|
+
kind: 'absolute',
|
|
1004
|
+
wallClock,
|
|
1005
|
+
nanoseconds: Number(fraction.padEnd(9, '0')),
|
|
1006
|
+
offset
|
|
1007
|
+
};
|
|
1008
|
+
}
|
|
1009
|
+
function parseOffset(text) {
|
|
1010
|
+
if (void 0 === text) return null;
|
|
1011
|
+
if ('Z' === text) return 0;
|
|
1012
|
+
const hours = Number(text.slice(1, 3));
|
|
1013
|
+
const minutes = Number(text.slice(4, 6));
|
|
1014
|
+
if (hours > 23 || minutes > 59) return null;
|
|
1015
|
+
const seconds = 3600 * hours + 60 * minutes;
|
|
1016
|
+
return '-' === text[0] ? -seconds : seconds;
|
|
1017
|
+
}
|
|
1018
|
+
function isValidWallClock({ year, month, day, hour, minute, second }) {
|
|
1019
|
+
return year >= 1 && year <= 9999 && month >= 1 && month <= 12 && day >= 1 && day <= daysInMonth(year, month) && hour <= 23 && minute <= 59 && second <= 59;
|
|
1020
|
+
}
|
|
1021
|
+
function daysInMonth(year, month) {
|
|
1022
|
+
if (2 === month) {
|
|
1023
|
+
const leap = year % 4 === 0 && year % 100 !== 0 || year % 400 === 0;
|
|
1024
|
+
return leap ? 29 : 28;
|
|
1025
|
+
}
|
|
1026
|
+
return [
|
|
1027
|
+
4,
|
|
1028
|
+
6,
|
|
1029
|
+
9,
|
|
1030
|
+
11
|
|
1031
|
+
].includes(month) ? 30 : 31;
|
|
1032
|
+
}
|
|
1033
|
+
function readDateFilter(text) {
|
|
1034
|
+
const date = parseDate(text);
|
|
1035
|
+
if (null === date || 'absolute' === date.kind && null === date.offset && text.includes('T')) throw unsupported('property.value');
|
|
1036
|
+
return date;
|
|
1037
|
+
}
|
|
1038
|
+
function matchDate(predicate, value, context) {
|
|
1039
|
+
const subject = propertyInstant(value, context);
|
|
1040
|
+
if (null === subject) return false;
|
|
1041
|
+
const target = resolveDate(predicate.operand, context);
|
|
1042
|
+
if (null === target) return false;
|
|
1043
|
+
const order = subject[0] !== target[0] ? subject[0] - target[0] : subject[1] - target[1];
|
|
1044
|
+
switch(predicate.operator){
|
|
1045
|
+
case 'is_date_exact':
|
|
1046
|
+
return 0 === order;
|
|
1047
|
+
case 'is_date_after':
|
|
1048
|
+
return order > 0;
|
|
1049
|
+
default:
|
|
1050
|
+
return order < 0;
|
|
1051
|
+
}
|
|
1052
|
+
}
|
|
1053
|
+
function propertyInstant(value, context) {
|
|
1054
|
+
if (null === value || 'boolean' == typeof value || Array.isArray(value) || isJsonObject(value)) return null;
|
|
1055
|
+
if ('number' == typeof value) return epochInstant(value);
|
|
1056
|
+
if ('string' != typeof value) throw unusableContext(`A property value of type ${describeType(value)} is not a JSON value`);
|
|
1057
|
+
if (NUMBER_PATTERN.test(value)) return epochInstant(parseNumberText(value));
|
|
1058
|
+
const date = parseDate(value);
|
|
1059
|
+
if (null !== date) return resolveDate(date, context);
|
|
1060
|
+
if (!/[0-9]/.test(value)) return null;
|
|
1061
|
+
throw unusableContext('The date property is in a form that needs the flags service to read');
|
|
1062
|
+
}
|
|
1063
|
+
function epochInstant(value) {
|
|
1064
|
+
if (!Number.isFinite(value)) throw unusableContext('A date property is not a finite number');
|
|
1065
|
+
const seconds = Math.floor(value);
|
|
1066
|
+
const nanoseconds = Math.max(0, roundHalfUp(value % 1 * 1e9));
|
|
1067
|
+
if (seconds < MIN_EPOCH_SECONDS || seconds > MAX_EPOCH_SECONDS || nanoseconds >= 1000000000) throw unusableContext('A date property is outside the supported range');
|
|
1068
|
+
return [
|
|
1069
|
+
seconds,
|
|
1070
|
+
nanoseconds
|
|
1071
|
+
];
|
|
1072
|
+
}
|
|
1073
|
+
function roundHalfUp(value) {
|
|
1074
|
+
const whole = Math.floor(value);
|
|
1075
|
+
return value - whole >= 0.5 ? whole + 1 : whole;
|
|
1076
|
+
}
|
|
1077
|
+
function resolveDate(date, context) {
|
|
1078
|
+
if ('relative' === date.kind) {
|
|
1079
|
+
const [wallClock, milliseconds] = relativeWallClock(date, context);
|
|
1080
|
+
return localInstant(wallClock, 1000000 * milliseconds, context);
|
|
1081
|
+
}
|
|
1082
|
+
if (null === date.offset) return localInstant(date.wallClock, date.nanoseconds, context);
|
|
1083
|
+
return [
|
|
1084
|
+
wallClockSeconds(date.wallClock) - date.offset,
|
|
1085
|
+
date.nanoseconds
|
|
1086
|
+
];
|
|
1087
|
+
}
|
|
1088
|
+
function timeZoneOf(context) {
|
|
1089
|
+
if (void 0 === context.timeZone) throw unusableContext("The project's timezone is not known");
|
|
1090
|
+
return context.timeZone;
|
|
1091
|
+
}
|
|
1092
|
+
function relativeWallClock(date, context) {
|
|
1093
|
+
const now = context.now ?? new Date();
|
|
1094
|
+
const nowMilliseconds = now.getTime();
|
|
1095
|
+
if (!Number.isFinite(nowMilliseconds)) throw unusableContext('The current time is not a valid date');
|
|
1096
|
+
const milliseconds = (nowMilliseconds % 1000 + 1000) % 1000;
|
|
1097
|
+
const nowSeconds = (nowMilliseconds - milliseconds) / 1000;
|
|
1098
|
+
const timeZone = timeZoneOf(context);
|
|
1099
|
+
const local = fromWallClockSeconds(nowSeconds + offsetAt(nowSeconds, timeZone));
|
|
1100
|
+
let moved;
|
|
1101
|
+
if ('m' === date.interval) moved = subtractMonths(local, date.number);
|
|
1102
|
+
else if ('y' === date.interval) moved = subtractYears(local, date.number);
|
|
1103
|
+
else {
|
|
1104
|
+
const hours = date.number * HOURS_PER_INTERVAL[date.interval];
|
|
1105
|
+
moved = fromWallClockSeconds(wallClockSeconds(local) - 3600 * hours);
|
|
1106
|
+
if (moved.year < 1 || moved.year > 9999) moved = null;
|
|
1107
|
+
}
|
|
1108
|
+
if (null === moved) throw unusableContext('A relative date is outside the supported range');
|
|
1109
|
+
return [
|
|
1110
|
+
moved,
|
|
1111
|
+
milliseconds
|
|
1112
|
+
];
|
|
1113
|
+
}
|
|
1114
|
+
function subtractMonths(wallClock, months) {
|
|
1115
|
+
const currentIndex = 12 * wallClock.year + wallClock.month - 1;
|
|
1116
|
+
const monthIndex = currentIndex - months;
|
|
1117
|
+
const year = Math.floor(monthIndex / 12);
|
|
1118
|
+
const month = monthIndex % 12 + 1;
|
|
1119
|
+
if (year < 1 || year > 9999) return null;
|
|
1120
|
+
let day = wallClock.day;
|
|
1121
|
+
for(let index = currentIndex - 1; index >= monthIndex; index--){
|
|
1122
|
+
if (day <= 28) break;
|
|
1123
|
+
day = Math.min(day, daysInMonth(Math.floor(index / 12), index % 12 + 1));
|
|
1124
|
+
}
|
|
1125
|
+
return {
|
|
1126
|
+
...wallClock,
|
|
1127
|
+
year,
|
|
1128
|
+
month,
|
|
1129
|
+
day
|
|
1130
|
+
};
|
|
1131
|
+
}
|
|
1132
|
+
function subtractYears(wallClock, years) {
|
|
1133
|
+
const year = wallClock.year - years;
|
|
1134
|
+
if (year < 1 || year > 9999) return null;
|
|
1135
|
+
const day = years > 0 && 2 === wallClock.month && 29 === wallClock.day ? 28 : wallClock.day;
|
|
1136
|
+
return {
|
|
1137
|
+
...wallClock,
|
|
1138
|
+
year,
|
|
1139
|
+
day
|
|
1140
|
+
};
|
|
1141
|
+
}
|
|
1142
|
+
function localInstant(wallClock, nanoseconds, context) {
|
|
1143
|
+
const timeZone = timeZoneOf(context);
|
|
1144
|
+
const local = wallClockSeconds(wallClock);
|
|
1145
|
+
const candidates = [
|
|
1146
|
+
offsetAt(local - SECONDS_PER_DAY, timeZone),
|
|
1147
|
+
offsetAt(local + SECONDS_PER_DAY, timeZone)
|
|
1148
|
+
].map((offset)=>local - offset).filter((instant)=>instant + offsetAt(instant, timeZone) === local);
|
|
1149
|
+
if (0 === candidates.length) return null;
|
|
1150
|
+
return [
|
|
1151
|
+
Math.min(...candidates),
|
|
1152
|
+
nanoseconds
|
|
1153
|
+
];
|
|
1154
|
+
}
|
|
1155
|
+
function wallClockSeconds({ year, month, day, hour, minute, second }) {
|
|
1156
|
+
return daysFromCivil(year, month, day) * SECONDS_PER_DAY + 3600 * hour + 60 * minute + second;
|
|
1157
|
+
}
|
|
1158
|
+
function fromWallClockSeconds(seconds) {
|
|
1159
|
+
const days = Math.floor(seconds / SECONDS_PER_DAY);
|
|
1160
|
+
const rest = seconds - days * SECONDS_PER_DAY;
|
|
1161
|
+
const [year, month, day] = civilFromDays(days);
|
|
1162
|
+
return {
|
|
1163
|
+
year,
|
|
1164
|
+
month,
|
|
1165
|
+
day,
|
|
1166
|
+
hour: Math.floor(rest / 3600),
|
|
1167
|
+
minute: Math.floor(rest % 3600 / 60),
|
|
1168
|
+
second: rest % 60
|
|
1169
|
+
};
|
|
1170
|
+
}
|
|
1171
|
+
function daysFromCivil(year, month, day) {
|
|
1172
|
+
const y = month <= 2 ? year - 1 : year;
|
|
1173
|
+
const era = Math.floor(y / 400);
|
|
1174
|
+
const yearOfEra = y - 400 * era;
|
|
1175
|
+
const dayOfYear = Math.floor((153 * (month + (month > 2 ? -3 : 9)) + 2) / 5) + day - 1;
|
|
1176
|
+
const dayOfEra = 365 * yearOfEra + Math.floor(yearOfEra / 4) - Math.floor(yearOfEra / 100) + dayOfYear;
|
|
1177
|
+
return 146097 * era + dayOfEra - 719468;
|
|
1178
|
+
}
|
|
1179
|
+
function civilFromDays(days) {
|
|
1180
|
+
const z = days + 719468;
|
|
1181
|
+
const era = Math.floor(z / 146097);
|
|
1182
|
+
const dayOfEra = z - 146097 * era;
|
|
1183
|
+
const yearOfEra = Math.floor((dayOfEra - Math.floor(dayOfEra / 1460) + Math.floor(dayOfEra / 36524) - Math.floor(dayOfEra / 146096)) / 365);
|
|
1184
|
+
const dayOfYear = dayOfEra - (365 * yearOfEra + Math.floor(yearOfEra / 4) - Math.floor(yearOfEra / 100));
|
|
1185
|
+
const monthIndex = Math.floor((5 * dayOfYear + 2) / 153);
|
|
1186
|
+
const day = dayOfYear - Math.floor((153 * monthIndex + 2) / 5) + 1;
|
|
1187
|
+
const month = monthIndex < 10 ? monthIndex + 3 : monthIndex - 9;
|
|
1188
|
+
return [
|
|
1189
|
+
yearOfEra + 400 * era + (month <= 2 ? 1 : 0),
|
|
1190
|
+
month,
|
|
1191
|
+
day
|
|
1192
|
+
];
|
|
1193
|
+
}
|
|
1194
|
+
const timeZoneFormats = new Map();
|
|
1195
|
+
function offsetAt(seconds, timeZone) {
|
|
1196
|
+
if ('UTC' === timeZone) return 0;
|
|
1197
|
+
let format = timeZoneFormats.get(timeZone);
|
|
1198
|
+
if (void 0 === format) {
|
|
1199
|
+
try {
|
|
1200
|
+
format = new Intl.DateTimeFormat('en-US', {
|
|
1201
|
+
timeZone,
|
|
1202
|
+
calendar: 'gregory',
|
|
1203
|
+
era: 'short',
|
|
1204
|
+
year: 'numeric',
|
|
1205
|
+
month: 'numeric',
|
|
1206
|
+
day: 'numeric',
|
|
1207
|
+
hour: 'numeric',
|
|
1208
|
+
minute: 'numeric',
|
|
1209
|
+
second: 'numeric',
|
|
1210
|
+
hourCycle: 'h23'
|
|
1211
|
+
});
|
|
1212
|
+
} catch {
|
|
1213
|
+
throw unusableContext(`The timezone '${timeZone}' is not known`);
|
|
1214
|
+
}
|
|
1215
|
+
timeZoneFormats.set(timeZone, format);
|
|
1216
|
+
}
|
|
1217
|
+
const parts = {};
|
|
1218
|
+
for (const part of format.formatToParts(new Date(1000 * seconds)))parts[part.type] = part.value;
|
|
1219
|
+
const year = 'BC' === parts.era || 'B' === parts.era ? 1 - Number(parts.year) : Number(parts.year);
|
|
1220
|
+
const local = wallClockSeconds({
|
|
1221
|
+
year,
|
|
1222
|
+
month: Number(parts.month),
|
|
1223
|
+
day: Number(parts.day),
|
|
1224
|
+
hour: Number(parts.hour),
|
|
1225
|
+
minute: Number(parts.minute),
|
|
1226
|
+
second: Number(parts.second)
|
|
1227
|
+
});
|
|
1228
|
+
return local - seconds;
|
|
1229
|
+
}
|
|
1230
|
+
const LOCAL_REASON_DESCRIPTION = 'Evaluated locally';
|
|
1231
|
+
function isConfigVersion1(flag) {
|
|
1232
|
+
const filters = isJsonObject(flag) ? flag.filters : void 0;
|
|
1233
|
+
if (!isJsonObject(filters) || void 0 === filters.version) return true;
|
|
1234
|
+
return 1 === filters.version;
|
|
1235
|
+
}
|
|
1236
|
+
function parseConfigV2Definitions(flags) {
|
|
1237
|
+
const parsed = new Map();
|
|
1238
|
+
for (const flag of flags)if (!isConfigVersion1(flag)) parsed.set(flag.key, {
|
|
1239
|
+
definition: flag,
|
|
1240
|
+
config: parseConfigV2(flag.filters)
|
|
1241
|
+
});
|
|
1242
|
+
return parsed;
|
|
1243
|
+
}
|
|
1244
|
+
function configFor(flag, parsed) {
|
|
1245
|
+
const entry = parsed.get(flag.key);
|
|
1246
|
+
return void 0 !== entry && entry.definition === flag ? entry.config : parseConfigV2(flag.filters);
|
|
1247
|
+
}
|
|
1248
|
+
function configVersionOf(flag) {
|
|
1249
|
+
const filters = flag.filters;
|
|
1250
|
+
return isJsonObject(filters) ? filters.version : void 0;
|
|
1251
|
+
}
|
|
1252
|
+
function configV2UnresolvedReason(flag, parsed) {
|
|
1253
|
+
if (configVersionOf(flag) !== CONFIG_VERSION) return 'unsupported_definition';
|
|
1254
|
+
return flag.active && isConfigV2Unsupported(configFor(flag, parsed)) ? 'unsupported_definition' : void 0;
|
|
1255
|
+
}
|
|
1256
|
+
function configV2HasPayloads(flag) {
|
|
1257
|
+
const filters = flag.filters;
|
|
1258
|
+
return isJsonObject(filters) && ('number' === filters.return_type || 'object' === filters.return_type);
|
|
1259
|
+
}
|
|
1260
|
+
async function evaluateConfigV2Definition(flag, parsed, context) {
|
|
1261
|
+
const version = configVersionOf(flag);
|
|
1262
|
+
if (version !== CONFIG_VERSION) throw new InconclusiveMatchError(`Config version ${JSON.stringify(version)} is not supported`, 'unsupported_definition');
|
|
1263
|
+
let outcome = await evaluateConfigV2(configFor(flag, parsed), {
|
|
1264
|
+
identifier: String(context.distinctId),
|
|
1265
|
+
properties: context.properties,
|
|
1266
|
+
propertiesComplete: false,
|
|
1267
|
+
explicitExactMatching: 2 === context.propertyMatchingVersion,
|
|
1268
|
+
timeZone: 'UTC'
|
|
1269
|
+
}, {
|
|
1270
|
+
active: Boolean(flag.active)
|
|
1271
|
+
});
|
|
1272
|
+
if ('evaluated' === outcome.outcome) {
|
|
1273
|
+
const result = localResult(flag, outcome);
|
|
1274
|
+
if (!result.record.failed) return result;
|
|
1275
|
+
outcome = {
|
|
1276
|
+
outcome: 'unsupported',
|
|
1277
|
+
kind: 'malformed',
|
|
1278
|
+
field: result.record.unreadableField ?? 'record'
|
|
1279
|
+
};
|
|
1280
|
+
}
|
|
1281
|
+
throw new InconclusiveMatchError(describe(outcome), reasonOf(outcome));
|
|
1282
|
+
}
|
|
1283
|
+
function localResult(flag, outcome) {
|
|
1284
|
+
const definition = flag;
|
|
1285
|
+
const metadata = {
|
|
1286
|
+
config_version: CONFIG_VERSION,
|
|
1287
|
+
description: null
|
|
1288
|
+
};
|
|
1289
|
+
if (Number.isInteger(definition.id)) metadata.id = definition.id;
|
|
1290
|
+
if (Number.isInteger(definition.version)) metadata.version = definition.version;
|
|
1291
|
+
if ('boolean' == typeof definition.has_experiment) metadata.has_experiment = definition.has_experiment;
|
|
1292
|
+
if (void 0 !== outcome.ruleType) {
|
|
1293
|
+
metadata.rule_type = outcome.ruleType;
|
|
1294
|
+
metadata.rule_id = outcome.ruleId;
|
|
1295
|
+
}
|
|
1296
|
+
if (void 0 !== outcome.variantKey) metadata.variant_key = outcome.variantKey;
|
|
1297
|
+
const raw = {
|
|
1298
|
+
key: flag.key,
|
|
1299
|
+
value: null === outcome.value ? null : JSON.parse(JSON.stringify(outcome.value)),
|
|
1300
|
+
reason: {
|
|
1301
|
+
code: outcome.reason,
|
|
1302
|
+
condition_index: outcome.ruleIndex ?? null,
|
|
1303
|
+
description: LOCAL_REASON_DESCRIPTION
|
|
1304
|
+
},
|
|
1305
|
+
metadata
|
|
1306
|
+
};
|
|
1307
|
+
const { body } = readFlagRecords({
|
|
1308
|
+
flags: {
|
|
1309
|
+
[flag.key]: raw
|
|
1310
|
+
}
|
|
1311
|
+
});
|
|
1312
|
+
const detail = body.flags[flag.key];
|
|
1313
|
+
const record = parsedFlagRecordOf(detail);
|
|
1314
|
+
const rendered = detail.metadata?.payload;
|
|
1315
|
+
return {
|
|
1316
|
+
detail,
|
|
1317
|
+
record,
|
|
1318
|
+
value: detail.variant ?? detail.enabled,
|
|
1319
|
+
payload: null == rendered ? null : JSON.parse(rendered)
|
|
1320
|
+
};
|
|
1321
|
+
}
|
|
1322
|
+
function reasonOf(outcome) {
|
|
1323
|
+
if ('inconclusive' === outcome.outcome) return outcome.reason;
|
|
1324
|
+
if ('error' === outcome.outcome && 'invalid_property' === outcome.kind) return 'missing_context';
|
|
1325
|
+
return 'unsupported_definition';
|
|
1326
|
+
}
|
|
1327
|
+
function describe(outcome) {
|
|
1328
|
+
if ('unsupported' === outcome.outcome) return `The config version 2 document is ${outcome.kind} at '${outcome.field}'`;
|
|
1329
|
+
return outcome.detail;
|
|
1330
|
+
}
|
|
1331
|
+
export { configV2HasPayloads, configV2UnresolvedReason, evaluateConfigV2, evaluateConfigV2Definition, evaluateConfigV2WithHash, hash01, inRollout, isConfigV2Unsupported, isConfigVersion1, jsonText, parseConfigV2, parseConfigV2Definitions, selectVariant, truncateIdentifier };
|