@aws-lambda-powertools/feature-flags 0.0.0-stage → 2.37.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/README.md +71 -2
- package/lib/cjs/FeatureFlags.d.ts +98 -0
- package/lib/cjs/FeatureFlags.d.ts.map +1 -0
- package/lib/cjs/FeatureFlags.js +260 -0
- package/lib/cjs/appconfig/appConfigStore.d.ts +38 -0
- package/lib/cjs/appconfig/appConfigStore.d.ts.map +1 -0
- package/lib/cjs/appconfig/appConfigStore.js +65 -0
- package/lib/cjs/appconfig/index.d.ts +4 -0
- package/lib/cjs/appconfig/index.d.ts.map +1 -0
- package/lib/cjs/appconfig/index.js +5 -0
- package/lib/cjs/appconfig-agent/appConfigAgentStore.d.ts +37 -0
- package/lib/cjs/appconfig-agent/appConfigAgentStore.d.ts.map +1 -0
- package/lib/cjs/appconfig-agent/appConfigAgentStore.js +59 -0
- package/lib/cjs/appconfig-agent/appConfigVariantStore.d.ts +46 -0
- package/lib/cjs/appconfig-agent/appConfigVariantStore.d.ts.map +1 -0
- package/lib/cjs/appconfig-agent/appConfigVariantStore.js +176 -0
- package/lib/cjs/appconfig-agent/index.d.ts +6 -0
- package/lib/cjs/appconfig-agent/index.d.ts.map +1 -0
- package/lib/cjs/appconfig-agent/index.js +8 -0
- package/lib/cjs/constants.d.ts +56 -0
- package/lib/cjs/constants.d.ts.map +1 -0
- package/lib/cjs/constants.js +86 -0
- package/lib/cjs/context.d.ts +21 -0
- package/lib/cjs/context.d.ts.map +1 -0
- package/lib/cjs/context.js +42 -0
- package/lib/cjs/envelope/index.d.ts +20 -0
- package/lib/cjs/envelope/index.d.ts.map +1 -0
- package/lib/cjs/envelope/index.js +29 -0
- package/lib/cjs/errors.d.ts +74 -0
- package/lib/cjs/errors.d.ts.map +1 -0
- package/lib/cjs/errors.js +116 -0
- package/lib/cjs/index.d.ts +6 -0
- package/lib/cjs/index.d.ts.map +1 -0
- package/lib/cjs/index.js +15 -0
- package/lib/cjs/package.json +1 -0
- package/lib/cjs/parameters-stores/classify.d.ts +20 -0
- package/lib/cjs/parameters-stores/classify.d.ts.map +1 -0
- package/lib/cjs/parameters-stores/classify.js +50 -0
- package/lib/cjs/parameters-stores/createDocumentSource.d.ts +17 -0
- package/lib/cjs/parameters-stores/createDocumentSource.d.ts.map +1 -0
- package/lib/cjs/parameters-stores/createDocumentSource.js +31 -0
- package/lib/cjs/parameters-stores/envelope.d.ts +22 -0
- package/lib/cjs/parameters-stores/envelope.d.ts.map +1 -0
- package/lib/cjs/parameters-stores/envelope.js +39 -0
- package/lib/cjs/ruleEngine/actions.d.ts +5 -0
- package/lib/cjs/ruleEngine/actions.d.ts.map +1 -0
- package/lib/cjs/ruleEngine/actions.js +155 -0
- package/lib/cjs/ruleEngine/datetime.d.ts +12 -0
- package/lib/cjs/ruleEngine/datetime.d.ts.map +1 -0
- package/lib/cjs/ruleEngine/datetime.js +134 -0
- package/lib/cjs/ruleEngine/evaluate.d.ts +15 -0
- package/lib/cjs/ruleEngine/evaluate.d.ts.map +1 -0
- package/lib/cjs/ruleEngine/evaluate.js +60 -0
- package/lib/cjs/ruleEngine/guards.d.ts +51 -0
- package/lib/cjs/ruleEngine/guards.d.ts.map +1 -0
- package/lib/cjs/ruleEngine/guards.js +123 -0
- package/lib/cjs/ruleEngine/index.d.ts +35 -0
- package/lib/cjs/ruleEngine/index.d.ts.map +1 -0
- package/lib/cjs/ruleEngine/index.js +70 -0
- package/lib/cjs/ruleEngine/json.d.ts +16 -0
- package/lib/cjs/ruleEngine/json.d.ts.map +1 -0
- package/lib/cjs/ruleEngine/json.js +38 -0
- package/lib/cjs/ruleEngine/time.d.ts +14 -0
- package/lib/cjs/ruleEngine/time.d.ts.map +1 -0
- package/lib/cjs/ruleEngine/time.js +102 -0
- package/lib/cjs/ruleEngine/validate.d.ts +11 -0
- package/lib/cjs/ruleEngine/validate.d.ts.map +1 -0
- package/lib/cjs/ruleEngine/validate.js +134 -0
- package/lib/cjs/ruleEngine/validateScheduleAndModulo.d.ts +13 -0
- package/lib/cjs/ruleEngine/validateScheduleAndModulo.d.ts.map +1 -0
- package/lib/cjs/ruleEngine/validateScheduleAndModulo.js +38 -0
- package/lib/cjs/ruleEngine/validationCache.d.ts +12 -0
- package/lib/cjs/ruleEngine/validationCache.d.ts.map +1 -0
- package/lib/cjs/ruleEngine/validationCache.js +29 -0
- package/lib/cjs/types/AppConfigAgentStore.d.ts +20 -0
- package/lib/cjs/types/AppConfigAgentStore.d.ts.map +1 -0
- package/lib/cjs/types/AppConfigAgentStore.js +2 -0
- package/lib/cjs/types/AppConfigStore.d.ts +25 -0
- package/lib/cjs/types/AppConfigStore.d.ts.map +1 -0
- package/lib/cjs/types/AppConfigStore.js +2 -0
- package/lib/cjs/types/AppConfigVariantStore.d.ts +16 -0
- package/lib/cjs/types/AppConfigVariantStore.d.ts.map +1 -0
- package/lib/cjs/types/AppConfigVariantStore.js +2 -0
- package/lib/cjs/types/Envelope.d.ts +16 -0
- package/lib/cjs/types/Envelope.d.ts.map +1 -0
- package/lib/cjs/types/Envelope.js +2 -0
- package/lib/cjs/types/RuleEngine.d.ts +83 -0
- package/lib/cjs/types/RuleEngine.d.ts.map +1 -0
- package/lib/cjs/types/RuleEngine.js +2 -0
- package/lib/cjs/types/VariantEvaluation.d.ts +20 -0
- package/lib/cjs/types/VariantEvaluation.d.ts.map +1 -0
- package/lib/cjs/types/VariantEvaluation.js +2 -0
- package/lib/cjs/types/index.d.ts +81 -0
- package/lib/cjs/types/index.d.ts.map +1 -0
- package/lib/cjs/types/index.js +2 -0
- package/lib/cjs/variant.d.ts +9 -0
- package/lib/cjs/variant.d.ts.map +1 -0
- package/lib/cjs/variant.js +26 -0
- package/lib/esm/FeatureFlags.d.ts +98 -0
- package/lib/esm/FeatureFlags.d.ts.map +1 -0
- package/lib/esm/FeatureFlags.js +257 -0
- package/lib/esm/appconfig/appConfigStore.d.ts +38 -0
- package/lib/esm/appconfig/appConfigStore.d.ts.map +1 -0
- package/lib/esm/appconfig/appConfigStore.js +62 -0
- package/lib/esm/appconfig/index.d.ts +4 -0
- package/lib/esm/appconfig/index.d.ts.map +1 -0
- package/lib/esm/appconfig/index.js +1 -0
- package/lib/esm/appconfig-agent/appConfigAgentStore.d.ts +37 -0
- package/lib/esm/appconfig-agent/appConfigAgentStore.d.ts.map +1 -0
- package/lib/esm/appconfig-agent/appConfigAgentStore.js +56 -0
- package/lib/esm/appconfig-agent/appConfigVariantStore.d.ts +46 -0
- package/lib/esm/appconfig-agent/appConfigVariantStore.d.ts.map +1 -0
- package/lib/esm/appconfig-agent/appConfigVariantStore.js +172 -0
- package/lib/esm/appconfig-agent/index.d.ts +6 -0
- package/lib/esm/appconfig-agent/index.d.ts.map +1 -0
- package/lib/esm/appconfig-agent/index.js +2 -0
- package/lib/esm/constants.d.ts +56 -0
- package/lib/esm/constants.d.ts.map +1 -0
- package/lib/esm/constants.js +77 -0
- package/lib/esm/context.d.ts +21 -0
- package/lib/esm/context.d.ts.map +1 -0
- package/lib/esm/context.js +37 -0
- package/lib/esm/envelope/index.d.ts +20 -0
- package/lib/esm/envelope/index.d.ts.map +1 -0
- package/lib/esm/envelope/index.js +26 -0
- package/lib/esm/errors.d.ts +74 -0
- package/lib/esm/errors.d.ts.map +1 -0
- package/lib/esm/errors.js +107 -0
- package/lib/esm/index.d.ts +6 -0
- package/lib/esm/index.d.ts.map +1 -0
- package/lib/esm/index.js +3 -0
- package/lib/esm/package.json +1 -0
- package/lib/esm/parameters-stores/classify.d.ts +20 -0
- package/lib/esm/parameters-stores/classify.d.ts.map +1 -0
- package/lib/esm/parameters-stores/classify.js +47 -0
- package/lib/esm/parameters-stores/createDocumentSource.d.ts +17 -0
- package/lib/esm/parameters-stores/createDocumentSource.d.ts.map +1 -0
- package/lib/esm/parameters-stores/createDocumentSource.js +28 -0
- package/lib/esm/parameters-stores/envelope.d.ts +22 -0
- package/lib/esm/parameters-stores/envelope.d.ts.map +1 -0
- package/lib/esm/parameters-stores/envelope.js +35 -0
- package/lib/esm/ruleEngine/actions.d.ts +5 -0
- package/lib/esm/ruleEngine/actions.d.ts.map +1 -0
- package/lib/esm/ruleEngine/actions.js +152 -0
- package/lib/esm/ruleEngine/datetime.d.ts +12 -0
- package/lib/esm/ruleEngine/datetime.d.ts.map +1 -0
- package/lib/esm/ruleEngine/datetime.js +131 -0
- package/lib/esm/ruleEngine/evaluate.d.ts +15 -0
- package/lib/esm/ruleEngine/evaluate.d.ts.map +1 -0
- package/lib/esm/ruleEngine/evaluate.js +57 -0
- package/lib/esm/ruleEngine/guards.d.ts +51 -0
- package/lib/esm/ruleEngine/guards.d.ts.map +1 -0
- package/lib/esm/ruleEngine/guards.js +113 -0
- package/lib/esm/ruleEngine/index.d.ts +35 -0
- package/lib/esm/ruleEngine/index.d.ts.map +1 -0
- package/lib/esm/ruleEngine/index.js +67 -0
- package/lib/esm/ruleEngine/json.d.ts +16 -0
- package/lib/esm/ruleEngine/json.d.ts.map +1 -0
- package/lib/esm/ruleEngine/json.js +34 -0
- package/lib/esm/ruleEngine/time.d.ts +14 -0
- package/lib/esm/ruleEngine/time.d.ts.map +1 -0
- package/lib/esm/ruleEngine/time.js +97 -0
- package/lib/esm/ruleEngine/validate.d.ts +11 -0
- package/lib/esm/ruleEngine/validate.d.ts.map +1 -0
- package/lib/esm/ruleEngine/validate.js +131 -0
- package/lib/esm/ruleEngine/validateScheduleAndModulo.d.ts +13 -0
- package/lib/esm/ruleEngine/validateScheduleAndModulo.d.ts.map +1 -0
- package/lib/esm/ruleEngine/validateScheduleAndModulo.js +35 -0
- package/lib/esm/ruleEngine/validationCache.d.ts +12 -0
- package/lib/esm/ruleEngine/validationCache.d.ts.map +1 -0
- package/lib/esm/ruleEngine/validationCache.js +26 -0
- package/lib/esm/types/AppConfigAgentStore.d.ts +20 -0
- package/lib/esm/types/AppConfigAgentStore.d.ts.map +1 -0
- package/lib/esm/types/AppConfigAgentStore.js +1 -0
- package/lib/esm/types/AppConfigStore.d.ts +25 -0
- package/lib/esm/types/AppConfigStore.d.ts.map +1 -0
- package/lib/esm/types/AppConfigStore.js +1 -0
- package/lib/esm/types/AppConfigVariantStore.d.ts +16 -0
- package/lib/esm/types/AppConfigVariantStore.d.ts.map +1 -0
- package/lib/esm/types/AppConfigVariantStore.js +1 -0
- package/lib/esm/types/Envelope.d.ts +16 -0
- package/lib/esm/types/Envelope.d.ts.map +1 -0
- package/lib/esm/types/Envelope.js +1 -0
- package/lib/esm/types/RuleEngine.d.ts +83 -0
- package/lib/esm/types/RuleEngine.d.ts.map +1 -0
- package/lib/esm/types/RuleEngine.js +1 -0
- package/lib/esm/types/VariantEvaluation.d.ts +20 -0
- package/lib/esm/types/VariantEvaluation.d.ts.map +1 -0
- package/lib/esm/types/VariantEvaluation.js +1 -0
- package/lib/esm/types/index.d.ts +81 -0
- package/lib/esm/types/index.d.ts.map +1 -0
- package/lib/esm/types/index.js +1 -0
- package/lib/esm/variant.d.ts +9 -0
- package/lib/esm/variant.d.ts.map +1 -0
- package/lib/esm/variant.js +21 -0
- package/package.json +137 -3
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import { isRecord, isString } from '@aws-lambda-powertools/commons';
|
|
2
|
+
import { TimeDays } from '../constants.js';
|
|
3
|
+
import { parseNaiveDatetime } from './datetime.js';
|
|
4
|
+
/**
|
|
5
|
+
* Identifies invalid optional IANA zones without throwing or exposing values.
|
|
6
|
+
* @param timezone - Supply the optional document timezone
|
|
7
|
+
* @internal
|
|
8
|
+
*/
|
|
9
|
+
const getTimezoneError = (timezone) => {
|
|
10
|
+
if (timezone === undefined)
|
|
11
|
+
return null;
|
|
12
|
+
if (!isString(timezone))
|
|
13
|
+
return 'TIMEZONE must be a string';
|
|
14
|
+
try {
|
|
15
|
+
// Intl accepts fixed-offset identifiers, but schedules require named IANA zones.
|
|
16
|
+
if (/^[+-]/.test(timezone))
|
|
17
|
+
return 'TIMEZONE must be a valid IANA timezone';
|
|
18
|
+
new Intl.DateTimeFormat('en-US', { timeZone: timezone });
|
|
19
|
+
return null;
|
|
20
|
+
}
|
|
21
|
+
catch {
|
|
22
|
+
return 'TIMEZONE must be a valid IANA timezone';
|
|
23
|
+
}
|
|
24
|
+
};
|
|
25
|
+
/**
|
|
26
|
+
* Identifies malformed modulo objects, integer fields, and inclusive bounds.
|
|
27
|
+
* @param value - Supply the document operand
|
|
28
|
+
* @internal
|
|
29
|
+
*/
|
|
30
|
+
const getModuloRangeError = (value) => {
|
|
31
|
+
if (!isRecord(value))
|
|
32
|
+
return 'value must be an object with BASE, START, and END';
|
|
33
|
+
const { BASE: base, START: start, END: end } = value;
|
|
34
|
+
if (typeof base !== 'number' ||
|
|
35
|
+
!Number.isInteger(base) ||
|
|
36
|
+
typeof start !== 'number' ||
|
|
37
|
+
!Number.isInteger(start) ||
|
|
38
|
+
typeof end !== 'number' ||
|
|
39
|
+
!Number.isInteger(end))
|
|
40
|
+
return 'BASE, START, and END must be integers';
|
|
41
|
+
// Integer END < BASE avoids rounding BASE - 1 while enforcing the same bound.
|
|
42
|
+
if (!(0 <= start && start <= end && end < base))
|
|
43
|
+
return 'must satisfy 0 <= START <= END <= BASE - 1';
|
|
44
|
+
return null;
|
|
45
|
+
};
|
|
46
|
+
/**
|
|
47
|
+
* Identifies malformed schedule objects, bounds, and optional timezones.
|
|
48
|
+
* @param value - Supply the document operand
|
|
49
|
+
* @param validateBound - Check one bound's format
|
|
50
|
+
* @param boundError - Identify a malformed bound without its value
|
|
51
|
+
* @internal
|
|
52
|
+
*/
|
|
53
|
+
const getRangeError = (value, validateBound, boundError) => {
|
|
54
|
+
if (!isRecord(value))
|
|
55
|
+
return 'value must be an object';
|
|
56
|
+
for (const bound of [value.START, value.END]) {
|
|
57
|
+
if (!validateBound(bound))
|
|
58
|
+
return boundError;
|
|
59
|
+
}
|
|
60
|
+
return getTimezoneError(value.TIMEZONE);
|
|
61
|
+
};
|
|
62
|
+
/**
|
|
63
|
+
* Identifies malformed anchored HH:MM bounds or optional IANA zones.
|
|
64
|
+
* @param value - Supply the document operand
|
|
65
|
+
* @internal
|
|
66
|
+
*/
|
|
67
|
+
const getTimeRangeError = (value) => getRangeError(value, (bound) => isString(bound) &&
|
|
68
|
+
bound.length === 5 &&
|
|
69
|
+
/^(?:2[0-3]|[01]\d):[0-5]\d$/.test(bound), 'START and END must use HH:MM');
|
|
70
|
+
/**
|
|
71
|
+
* Identifies malformed naive ISO bounds or optional IANA zones.
|
|
72
|
+
* @param value - Supply the document operand
|
|
73
|
+
* @internal
|
|
74
|
+
*/
|
|
75
|
+
const getDatetimeRangeError = (value) => getRangeError(value, (bound) => parseNaiveDatetime(bound) !== null, 'START and END must use timezone-naive ISO8601');
|
|
76
|
+
/**
|
|
77
|
+
* Identifies malformed Python day lists or optional IANA zones.
|
|
78
|
+
* @param value - Supply the document operand
|
|
79
|
+
* @internal
|
|
80
|
+
*/
|
|
81
|
+
const getDaysOfWeekError = (value) => {
|
|
82
|
+
if (!isRecord(value))
|
|
83
|
+
return 'value must be an object';
|
|
84
|
+
if (!Array.isArray(value.DAYS) ||
|
|
85
|
+
value.DAYS.some((day) => !isString(day) || !TimeDays.some((validDay) => validDay === day)))
|
|
86
|
+
return 'DAYS must be an array of Python day names';
|
|
87
|
+
return getTimezoneError(value.TIMEZONE);
|
|
88
|
+
};
|
|
89
|
+
/**
|
|
90
|
+
* Narrows complete modulo shapes using the same rules as document validation.
|
|
91
|
+
* @param value - Supply the operand to check
|
|
92
|
+
* @internal
|
|
93
|
+
*/
|
|
94
|
+
const isModuloRange = (value) => getModuloRangeError(value) === null;
|
|
95
|
+
/**
|
|
96
|
+
* Narrows complete time-range shapes, including anchored bounds and valid zones.
|
|
97
|
+
* @param value - Supply the operand to check
|
|
98
|
+
* @internal
|
|
99
|
+
*/
|
|
100
|
+
const isTimeRange = (value) => getTimeRangeError(value) === null;
|
|
101
|
+
/**
|
|
102
|
+
* Narrows complete datetime-range shapes, including naive ISO bounds and valid zones.
|
|
103
|
+
* @param value - Supply the operand to check
|
|
104
|
+
* @internal
|
|
105
|
+
*/
|
|
106
|
+
const isDatetimeRange = (value) => getDatetimeRangeError(value) === null;
|
|
107
|
+
/**
|
|
108
|
+
* Narrows complete day-list shapes, including Python names and valid zones.
|
|
109
|
+
* @param value - Supply the operand to check
|
|
110
|
+
* @internal
|
|
111
|
+
*/
|
|
112
|
+
const isDaysOfWeek = (value) => getDaysOfWeekError(value) === null;
|
|
113
|
+
export { getDatetimeRangeError, getDaysOfWeekError, getModuloRangeError, getTimeRangeError, isDatetimeRange, isDaysOfWeek, isModuloRange, isTimeRange, };
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import type { Store } from '../types/index.js';
|
|
2
|
+
import type { RuleEngineStoreOptions } from '../types/RuleEngine.js';
|
|
3
|
+
/**
|
|
4
|
+
* Creates a local {@link Store | `Store`} that evaluates Python-format Flag Documents.
|
|
5
|
+
* Supports Powertools for Python 3.36.0 Actions and missing-key semantics.
|
|
6
|
+
* Uses strict boolean/number equality, throws for incompatible operands, rejects numeric Rule names that JavaScript reorders, and rejects falsy non-object rules.
|
|
7
|
+
*
|
|
8
|
+
* Membership Actions check strict array membership, own object keys, or Unicode code-point substrings.
|
|
9
|
+
* Retrieves the source on every call and propagates source errors unchanged.
|
|
10
|
+
* Keeps only the latest validated, compiled snapshot, keyed by JSON content without sorting keys.
|
|
11
|
+
* Validates the entire document when content changes; invalid replacements throw without replacing the snapshot.
|
|
12
|
+
* Empty-document and empty-rules warnings fire on validation, once per new document content rather than every call.
|
|
13
|
+
* Each call evaluates its captured, owned snapshot and returns copies of structured values.
|
|
14
|
+
* Schedule Actions ignore caller context and read the clock in their IANA timezone, defaulting to UTC.
|
|
15
|
+
* Reads the clock once after retrieving and validating each document, sharing the instant across all flags and Conditions in that call.
|
|
16
|
+
* MODULO_RANGE uses Python's positive-divisor remainder, including negative and fractional contexts.
|
|
17
|
+
*
|
|
18
|
+
* @param {Object} options - Configure retrieval and logging
|
|
19
|
+
* @param {string} options.name - Name the Store, defaulting to `rule-engine`
|
|
20
|
+
* @param {Function} options.source - Retrieve a Flag Document for each Store call
|
|
21
|
+
* @param {GenericLogger} options.logger - Log warnings and outcomes without values
|
|
22
|
+
* @param {Function} options.now - Supply the schedule clock, defaulting to () => new Date()
|
|
23
|
+
* @example
|
|
24
|
+
* ```typescript
|
|
25
|
+
* const store = ruleEngineStore({
|
|
26
|
+
* source: async () => ({ checkout_v2: { default: true } }),
|
|
27
|
+
* });
|
|
28
|
+
* const flags = new FeatureFlags({ store });
|
|
29
|
+
* const enabled = await flags.evaluate({ name: 'checkout_v2', default: false });
|
|
30
|
+
* ```
|
|
31
|
+
*/
|
|
32
|
+
declare const ruleEngineStore: ({ source, logger, now, name, }: RuleEngineStoreOptions) => Store;
|
|
33
|
+
export type { RuleEngineStoreOptions } from '../types/RuleEngine.js';
|
|
34
|
+
export { ruleEngineStore };
|
|
35
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/ruleEngine/index.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,KAAK,EAAe,MAAM,mBAAmB,CAAC;AAC5D,OAAO,KAAK,EAAE,sBAAsB,EAAE,MAAM,wBAAwB,CAAC;AAKrE;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AACH,QAAA,MAAM,eAAe,GAAI,gCAKtB,sBAAsB,KAAG,KAiC3B,CAAC;AAEF,YAAY,EAAE,sBAAsB,EAAE,MAAM,wBAAwB,CAAC;AACrE,OAAO,EAAE,eAAe,EAAE,CAAC"}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { StoreNames } from '../constants.js';
|
|
2
|
+
import { evaluate } from './evaluate.js';
|
|
3
|
+
import { currentDate } from './time.js';
|
|
4
|
+
import { createValidationCache } from './validationCache.js';
|
|
5
|
+
/**
|
|
6
|
+
* Creates a local {@link Store | `Store`} that evaluates Python-format Flag Documents.
|
|
7
|
+
* Supports Powertools for Python 3.36.0 Actions and missing-key semantics.
|
|
8
|
+
* Uses strict boolean/number equality, throws for incompatible operands, rejects numeric Rule names that JavaScript reorders, and rejects falsy non-object rules.
|
|
9
|
+
*
|
|
10
|
+
* Membership Actions check strict array membership, own object keys, or Unicode code-point substrings.
|
|
11
|
+
* Retrieves the source on every call and propagates source errors unchanged.
|
|
12
|
+
* Keeps only the latest validated, compiled snapshot, keyed by JSON content without sorting keys.
|
|
13
|
+
* Validates the entire document when content changes; invalid replacements throw without replacing the snapshot.
|
|
14
|
+
* Empty-document and empty-rules warnings fire on validation, once per new document content rather than every call.
|
|
15
|
+
* Each call evaluates its captured, owned snapshot and returns copies of structured values.
|
|
16
|
+
* Schedule Actions ignore caller context and read the clock in their IANA timezone, defaulting to UTC.
|
|
17
|
+
* Reads the clock once after retrieving and validating each document, sharing the instant across all flags and Conditions in that call.
|
|
18
|
+
* MODULO_RANGE uses Python's positive-divisor remainder, including negative and fractional contexts.
|
|
19
|
+
*
|
|
20
|
+
* @param {Object} options - Configure retrieval and logging
|
|
21
|
+
* @param {string} options.name - Name the Store, defaulting to `rule-engine`
|
|
22
|
+
* @param {Function} options.source - Retrieve a Flag Document for each Store call
|
|
23
|
+
* @param {GenericLogger} options.logger - Log warnings and outcomes without values
|
|
24
|
+
* @param {Function} options.now - Supply the schedule clock, defaulting to () => new Date()
|
|
25
|
+
* @example
|
|
26
|
+
* ```typescript
|
|
27
|
+
* const store = ruleEngineStore({
|
|
28
|
+
* source: async () => ({ checkout_v2: { default: true } }),
|
|
29
|
+
* });
|
|
30
|
+
* const flags = new FeatureFlags({ store });
|
|
31
|
+
* const enabled = await flags.evaluate({ name: 'checkout_v2', default: false });
|
|
32
|
+
* ```
|
|
33
|
+
*/
|
|
34
|
+
const ruleEngineStore = ({ source, logger, now = currentDate, name = StoreNames.RULE_ENGINE, }) => {
|
|
35
|
+
const validateDocument = createValidationCache(logger);
|
|
36
|
+
return {
|
|
37
|
+
name,
|
|
38
|
+
getMany: async (names, context) => {
|
|
39
|
+
const document = validateDocument(await source());
|
|
40
|
+
const instant = now();
|
|
41
|
+
const results = [];
|
|
42
|
+
for (const name of new Set(names)) {
|
|
43
|
+
results.push([
|
|
44
|
+
name,
|
|
45
|
+
Object.hasOwn(document, name)
|
|
46
|
+
? evaluate(name, document[name], { context, logger, now: instant })
|
|
47
|
+
: { found: false },
|
|
48
|
+
]);
|
|
49
|
+
}
|
|
50
|
+
// Object.fromEntries keeps __proto__ and other inherited names as own keys.
|
|
51
|
+
return Object.fromEntries(results);
|
|
52
|
+
},
|
|
53
|
+
getAll: async (context) => {
|
|
54
|
+
const document = validateDocument(await source());
|
|
55
|
+
const instant = now();
|
|
56
|
+
const results = [];
|
|
57
|
+
for (const [name, flag] of Object.entries(document)) {
|
|
58
|
+
results.push([
|
|
59
|
+
name,
|
|
60
|
+
evaluate(name, flag, { context, logger, now: instant }),
|
|
61
|
+
]);
|
|
62
|
+
}
|
|
63
|
+
return Object.fromEntries(results);
|
|
64
|
+
},
|
|
65
|
+
};
|
|
66
|
+
};
|
|
67
|
+
export { ruleEngineStore };
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { JSONValue } from '@aws-lambda-powertools/commons/types';
|
|
2
|
+
/**
|
|
3
|
+
* Checks JSON values at the source boundary, including callers that bypass types.
|
|
4
|
+
* @param value - Supply the value to check
|
|
5
|
+
* @param ancestors - Track containers to reject cycles without rejecting shared values
|
|
6
|
+
* @internal
|
|
7
|
+
*/
|
|
8
|
+
declare const isJSONValue: (value: unknown, ancestors?: Set<unknown>) => value is JSONValue;
|
|
9
|
+
/**
|
|
10
|
+
* Copies structured results so callers cannot mutate a cached snapshot.
|
|
11
|
+
* @param value - Supply the validated result value
|
|
12
|
+
* @internal
|
|
13
|
+
*/
|
|
14
|
+
declare const cloneJSONValue: (value: JSONValue) => JSONValue;
|
|
15
|
+
export { cloneJSONValue, isJSONValue };
|
|
16
|
+
//# sourceMappingURL=json.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"json.d.ts","sourceRoot":"","sources":["../../../src/ruleEngine/json.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,sCAAsC,CAAC;AAEtE;;;;;GAKG;AACH,QAAA,MAAM,WAAW,GACf,OAAO,OAAO,EACd,wBAA8B,KAC7B,KAAK,IAAI,SAeX,CAAC;AAEF;;;;GAIG;AACH,QAAA,MAAM,cAAc,GAAI,OAAO,SAAS,KAAG,SACmC,CAAC;AAE/E,OAAO,EAAE,cAAc,EAAE,WAAW,EAAE,CAAC"}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { isNull, isNumber, isRecord, isString, } from '@aws-lambda-powertools/commons';
|
|
2
|
+
/**
|
|
3
|
+
* Checks JSON values at the source boundary, including callers that bypass types.
|
|
4
|
+
* @param value - Supply the value to check
|
|
5
|
+
* @param ancestors - Track containers to reject cycles without rejecting shared values
|
|
6
|
+
* @internal
|
|
7
|
+
*/
|
|
8
|
+
const isJSONValue = (value, ancestors = new Set()) => {
|
|
9
|
+
if (isNull(value) || isString(value) || typeof value === 'boolean')
|
|
10
|
+
return true;
|
|
11
|
+
if (isNumber(value))
|
|
12
|
+
return Number.isFinite(value);
|
|
13
|
+
if (!Array.isArray(value) && !isRecord(value))
|
|
14
|
+
return false;
|
|
15
|
+
// Custom serializers can hide invalid fields or replace content before the cache key is computed.
|
|
16
|
+
if (typeof Reflect.get(value, 'toJSON') === 'function')
|
|
17
|
+
return false;
|
|
18
|
+
if (ancestors.has(value))
|
|
19
|
+
return false;
|
|
20
|
+
ancestors.add(value);
|
|
21
|
+
const entries = Array.isArray(value)
|
|
22
|
+
? Array.from(value)
|
|
23
|
+
: Object.values(value);
|
|
24
|
+
const valid = entries.every((entry) => isJSONValue(entry, ancestors));
|
|
25
|
+
ancestors.delete(value);
|
|
26
|
+
return valid;
|
|
27
|
+
};
|
|
28
|
+
/**
|
|
29
|
+
* Copies structured results so callers cannot mutate a cached snapshot.
|
|
30
|
+
* @param value - Supply the validated result value
|
|
31
|
+
* @internal
|
|
32
|
+
*/
|
|
33
|
+
const cloneJSONValue = (value) => typeof value === 'object' && value !== null ? structuredClone(value) : value;
|
|
34
|
+
export { cloneJSONValue, isJSONValue };
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { JSONValue } from '@aws-lambda-powertools/commons/types';
|
|
2
|
+
import type { TimeAction } from '../types/RuleEngine.js';
|
|
3
|
+
/**
|
|
4
|
+
* Identifies Actions that ignore caller context and read the clock.
|
|
5
|
+
* @param action - Supply the validated Action
|
|
6
|
+
* @internal
|
|
7
|
+
*/
|
|
8
|
+
declare const isTimeAction: (action: string) => action is TimeAction;
|
|
9
|
+
/** Supplies the default clock without reading host-local calendar fields. @internal */
|
|
10
|
+
declare const currentDate: () => Date;
|
|
11
|
+
/** Dispatches schedule Actions after validation, without using context values. @internal */
|
|
12
|
+
declare const timeActions: Record<TimeAction, (value: JSONValue, now: Date) => boolean>;
|
|
13
|
+
export { currentDate, isTimeAction, timeActions };
|
|
14
|
+
//# sourceMappingURL=time.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"time.d.ts","sourceRoot":"","sources":["../../../src/ruleEngine/time.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,sCAAsC,CAAC;AAEtE,OAAO,KAAK,EAEV,UAAU,EAEX,MAAM,wBAAwB,CAAC;AAIhC;;;;GAIG;AACH,QAAA,MAAM,YAAY,GAAI,QAAQ,MAAM,KAAG,MAAM,IAAI,UACV,CAAC;AAExC,uFAAuF;AACvF,QAAA,MAAM,WAAW,QAAO,IAAkB,CAAC;AA0E3C,4FAA4F;AAC5F,QAAA,MAAM,WAAW,EAAE,MAAM,CACvB,UAAU,EACV,CAAC,KAAK,EAAE,SAAS,EAAE,GAAG,EAAE,IAAI,KAAK,OAAO,CAgBzC,CAAC;AAEF,OAAO,EAAE,WAAW,EAAE,YAAY,EAAE,WAAW,EAAE,CAAC"}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import { RuleActions, TimeActionKeys } from '../constants.js';
|
|
2
|
+
import { parseNaiveDatetime } from './datetime.js';
|
|
3
|
+
import { isDatetimeRange, isDaysOfWeek, isTimeRange } from './guards.js';
|
|
4
|
+
/**
|
|
5
|
+
* Identifies Actions that ignore caller context and read the clock.
|
|
6
|
+
* @param action - Supply the validated Action
|
|
7
|
+
* @internal
|
|
8
|
+
*/
|
|
9
|
+
const isTimeAction = (action) => Object.hasOwn(TimeActionKeys, action);
|
|
10
|
+
/** Supplies the default clock without reading host-local calendar fields. @internal */
|
|
11
|
+
const currentDate = () => new Date();
|
|
12
|
+
/**
|
|
13
|
+
* Reads wall-clock fields from an instant in the document's timezone, defaulting to UTC.
|
|
14
|
+
* @param now - Supply the clock instant
|
|
15
|
+
* @param timeZone - Supply the validated IANA zone
|
|
16
|
+
* @internal
|
|
17
|
+
*/
|
|
18
|
+
const wallClock = (now, timeZone = 'UTC') => {
|
|
19
|
+
const formatter = new Intl.DateTimeFormat('en-US', {
|
|
20
|
+
timeZone,
|
|
21
|
+
calendar: 'gregory',
|
|
22
|
+
numberingSystem: 'latn',
|
|
23
|
+
hourCycle: 'h23',
|
|
24
|
+
year: 'numeric',
|
|
25
|
+
month: '2-digit',
|
|
26
|
+
day: '2-digit',
|
|
27
|
+
hour: '2-digit',
|
|
28
|
+
minute: '2-digit',
|
|
29
|
+
second: '2-digit',
|
|
30
|
+
fractionalSecondDigits: 3,
|
|
31
|
+
weekday: 'long',
|
|
32
|
+
});
|
|
33
|
+
const fields = {};
|
|
34
|
+
for (const { type, value } of formatter.formatToParts(now))
|
|
35
|
+
fields[type] = value;
|
|
36
|
+
return fields;
|
|
37
|
+
};
|
|
38
|
+
/**
|
|
39
|
+
* Converts HH:MM to full minutes since midnight.
|
|
40
|
+
* @param value - Supply a validated time bound
|
|
41
|
+
* @internal
|
|
42
|
+
*/
|
|
43
|
+
const minutesSinceMidnight = (value) => Number(value.slice(0, 2)) * 60 + Number(value.slice(3));
|
|
44
|
+
/**
|
|
45
|
+
* Matches inclusive time bounds, including ranges crossing midnight within one hour.
|
|
46
|
+
* Python replaces only hours/minutes on now, retaining its seconds/microseconds.
|
|
47
|
+
* Comparing minutes therefore includes the entire START and END minutes.
|
|
48
|
+
* @param value - Supply validated schedule bounds
|
|
49
|
+
* @param now - Supply the clock instant
|
|
50
|
+
* @internal
|
|
51
|
+
*/
|
|
52
|
+
const compareTimeRange = (value, now) => {
|
|
53
|
+
const fields = wallClock(now, value.TIMEZONE);
|
|
54
|
+
const current = Number(fields.hour) * 60 + Number(fields.minute);
|
|
55
|
+
const start = minutesSinceMidnight(value.START);
|
|
56
|
+
const end = minutesSinceMidnight(value.END);
|
|
57
|
+
return end < start
|
|
58
|
+
? start <= current || current <= end
|
|
59
|
+
: start <= current && current <= end;
|
|
60
|
+
};
|
|
61
|
+
/**
|
|
62
|
+
* Matches inclusive naive datetime bounds against zoned wall-clock fields.
|
|
63
|
+
* Python attaches the same dateutil tzinfo to START, END, and now. Its datetime
|
|
64
|
+
* comparisons then ignore offsets and fold, comparing wall-clock fields. This
|
|
65
|
+
* deliberately matches both occurrences of a fall-back time and leaves spring
|
|
66
|
+
* gaps unnormalized. Reversed intervals validate but never match.
|
|
67
|
+
* @param value - Supply validated naive datetime bounds and timezone
|
|
68
|
+
* @param now - Supply the clock instant
|
|
69
|
+
* @internal
|
|
70
|
+
*/
|
|
71
|
+
const compareDatetimeRange = (value, now) => {
|
|
72
|
+
const fields = wallClock(now, value.TIMEZONE);
|
|
73
|
+
const current = `${fields.year.padStart(4, '0')}-${fields.month}-${fields.day}T${fields.hour}:${fields.minute}:${fields.second}.${fields.fractionalSecond.padEnd(6, '0')}`;
|
|
74
|
+
// Validation guarantees both bounds parse to canonical wall-clock strings.
|
|
75
|
+
const start = parseNaiveDatetime(value.START);
|
|
76
|
+
const end = parseNaiveDatetime(value.END);
|
|
77
|
+
return start <= current && current <= end;
|
|
78
|
+
};
|
|
79
|
+
/** Dispatches schedule Actions after validation, without using context values. @internal */
|
|
80
|
+
const timeActions = {
|
|
81
|
+
[RuleActions.SCHEDULE_BETWEEN_TIME_RANGE]: (value, now) => {
|
|
82
|
+
if (!isTimeRange(value))
|
|
83
|
+
throw new TypeError('Incompatible operands');
|
|
84
|
+
return compareTimeRange(value, now);
|
|
85
|
+
},
|
|
86
|
+
[RuleActions.SCHEDULE_BETWEEN_DATETIME_RANGE]: (value, now) => {
|
|
87
|
+
if (!isDatetimeRange(value))
|
|
88
|
+
throw new TypeError('Incompatible operands');
|
|
89
|
+
return compareDatetimeRange(value, now);
|
|
90
|
+
},
|
|
91
|
+
[RuleActions.SCHEDULE_BETWEEN_DAYS_OF_WEEK]: (value, now) => {
|
|
92
|
+
if (!isDaysOfWeek(value))
|
|
93
|
+
throw new TypeError('Incompatible operands');
|
|
94
|
+
return value.DAYS.includes(wallClock(now, value.TIMEZONE).weekday.toUpperCase());
|
|
95
|
+
},
|
|
96
|
+
};
|
|
97
|
+
export { currentDate, isTimeAction, timeActions };
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { GenericLogger } from '@aws-lambda-powertools/commons/types';
|
|
2
|
+
import type { FlagDocument } from '../types/RuleEngine.js';
|
|
3
|
+
/**
|
|
4
|
+
* Validates an entire Python-format Flag Document and returns typed definitions.
|
|
5
|
+
* @param document - Supply the retrieved document
|
|
6
|
+
* @param logger - Log accepted empty documents and empty Rules without values
|
|
7
|
+
* @internal
|
|
8
|
+
*/
|
|
9
|
+
declare const validate: (document: unknown, logger?: GenericLogger) => FlagDocument;
|
|
10
|
+
export { validate };
|
|
11
|
+
//# sourceMappingURL=validate.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"validate.d.ts","sourceRoot":"","sources":["../../../src/ruleEngine/validate.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EACV,aAAa,EAEd,MAAM,sCAAsC,CAAC;AAG9C,OAAO,KAAK,EAGV,YAAY,EAGb,MAAM,wBAAwB,CAAC;AAIhC;;;;;GAKG;AACH,QAAA,MAAM,QAAQ,GAAI,UAAU,OAAO,EAAE,SAAS,aAAa,KAAG,YA+B7D,CAAC;AAuIF,OAAO,EAAE,QAAQ,EAAE,CAAC"}
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
import { isRecord, isString } from '@aws-lambda-powertools/commons';
|
|
2
|
+
import { FlagDocumentFields, RuleActions, StoreNames } from '../constants.js';
|
|
3
|
+
import { SchemaValidationError } from '../errors.js';
|
|
4
|
+
import { isJSONValue } from './json.js';
|
|
5
|
+
import { validateScheduleAndModulo } from './validateScheduleAndModulo.js';
|
|
6
|
+
/**
|
|
7
|
+
* Validates an entire Python-format Flag Document and returns typed definitions.
|
|
8
|
+
* @param document - Supply the retrieved document
|
|
9
|
+
* @param logger - Log accepted empty documents and empty Rules without values
|
|
10
|
+
* @internal
|
|
11
|
+
*/
|
|
12
|
+
const validate = (document, logger) => {
|
|
13
|
+
if (!isRecord(document))
|
|
14
|
+
throw new SchemaValidationError('Flag Document must be an object');
|
|
15
|
+
const entries = [];
|
|
16
|
+
for (const [name, flag] of Object.entries(document)) {
|
|
17
|
+
const location = `Flag '${name}'`;
|
|
18
|
+
if (!isRecord(flag) || !Object.hasOwn(flag, FlagDocumentFields.DEFAULT)) {
|
|
19
|
+
throw new SchemaValidationError(`${location} must be an object with a default`);
|
|
20
|
+
}
|
|
21
|
+
const booleanType = Object.hasOwn(flag, FlagDocumentFields.BOOLEAN_TYPE)
|
|
22
|
+
? flag[FlagDocumentFields.BOOLEAN_TYPE]
|
|
23
|
+
: true;
|
|
24
|
+
if (typeof booleanType !== 'boolean')
|
|
25
|
+
throw new SchemaValidationError(`${location} boolean_type must be a boolean`);
|
|
26
|
+
const defaultValue = validateResult(flag[FlagDocumentFields.DEFAULT], booleanType, `${location} default`);
|
|
27
|
+
const rules = Object.hasOwn(flag, FlagDocumentFields.RULES)
|
|
28
|
+
? validateRules(flag[FlagDocumentFields.RULES], name, booleanType, logger)
|
|
29
|
+
: {};
|
|
30
|
+
entries.push([name, { default: defaultValue, rules }]);
|
|
31
|
+
}
|
|
32
|
+
if (entries.length === 0)
|
|
33
|
+
logger?.warn({ store: StoreNames.RULE_ENGINE, reason: 'empty-document' });
|
|
34
|
+
return Object.fromEntries(entries);
|
|
35
|
+
};
|
|
36
|
+
/**
|
|
37
|
+
* Validates named Rules without changing their insertion order.
|
|
38
|
+
* @param rules - Supply the Rules object
|
|
39
|
+
* @param name - Identify the flag
|
|
40
|
+
* @param booleanType - Require boolean results for boolean flags
|
|
41
|
+
* @param logger - Log accepted empty Rules without values
|
|
42
|
+
* @internal
|
|
43
|
+
*/
|
|
44
|
+
const validateRules = (rules, name, booleanType, logger) => {
|
|
45
|
+
if (!isRecord(rules))
|
|
46
|
+
throw new SchemaValidationError(`Flag '${name}' rules must be an object`);
|
|
47
|
+
const entries = [];
|
|
48
|
+
for (const [ruleName, rule] of Object.entries(rules)) {
|
|
49
|
+
const location = `Flag '${name}', rule '${ruleName}'`;
|
|
50
|
+
if (ruleName.length === 0 ||
|
|
51
|
+
(/^(0|[1-9]\d*)$/.test(ruleName) && Number(ruleName) < 2 ** 32 - 1)) {
|
|
52
|
+
throw new SchemaValidationError(`${location} must use a non-empty name that preserves insertion order`);
|
|
53
|
+
}
|
|
54
|
+
if (!isRecord(rule) ||
|
|
55
|
+
!Object.hasOwn(rule, FlagDocumentFields.WHEN_MATCH)) {
|
|
56
|
+
throw new SchemaValidationError(`${location} must be an object with when_match`);
|
|
57
|
+
}
|
|
58
|
+
const whenMatch = validateResult(rule[FlagDocumentFields.WHEN_MATCH], booleanType, `${location} when_match`);
|
|
59
|
+
const conditions = rule[FlagDocumentFields.CONDITIONS];
|
|
60
|
+
if (!Array.isArray(conditions) || conditions.length === 0) {
|
|
61
|
+
throw new SchemaValidationError(`${location} conditions must be a non-empty array`);
|
|
62
|
+
}
|
|
63
|
+
const validatedConditions = [];
|
|
64
|
+
for (const [index, condition] of conditions.entries())
|
|
65
|
+
validatedConditions.push(validateCondition(condition, `${location}, condition ${index}`));
|
|
66
|
+
entries.push([
|
|
67
|
+
ruleName,
|
|
68
|
+
{ when_match: whenMatch, conditions: validatedConditions },
|
|
69
|
+
]);
|
|
70
|
+
}
|
|
71
|
+
if (entries.length === 0)
|
|
72
|
+
logger?.warn({
|
|
73
|
+
store: StoreNames.RULE_ENGINE,
|
|
74
|
+
reason: 'empty-rules',
|
|
75
|
+
name,
|
|
76
|
+
});
|
|
77
|
+
return Object.fromEntries(entries);
|
|
78
|
+
};
|
|
79
|
+
/**
|
|
80
|
+
* Validates a Condition and identifies failures without including operand values.
|
|
81
|
+
* @param condition - Supply the Condition object
|
|
82
|
+
* @param location - Identify its flag, Rule, and zero-based Condition index
|
|
83
|
+
* @internal
|
|
84
|
+
*/
|
|
85
|
+
const validateCondition = (condition, location) => {
|
|
86
|
+
if (!isRecord(condition))
|
|
87
|
+
throw new SchemaValidationError(`${location} must be an object`);
|
|
88
|
+
const action = condition[FlagDocumentFields.ACTION];
|
|
89
|
+
const key = condition[FlagDocumentFields.KEY];
|
|
90
|
+
const identifier = `${location}, key '${isString(key) ? key : '<invalid>'}', action '${isString(action) ? action : '<invalid>'}'`;
|
|
91
|
+
if (!isString(action))
|
|
92
|
+
throw new SchemaValidationError(`${identifier} action must be a string`);
|
|
93
|
+
if (!isRuleAction(action))
|
|
94
|
+
throw new SchemaValidationError(`${identifier} action is unknown`);
|
|
95
|
+
if (!isString(key) || key.length === 0)
|
|
96
|
+
throw new SchemaValidationError(`${identifier} key must be a non-empty string`);
|
|
97
|
+
const value = condition[FlagDocumentFields.VALUE];
|
|
98
|
+
if (!Object.hasOwn(condition, FlagDocumentFields.VALUE) ||
|
|
99
|
+
!isJSONValue(value)) {
|
|
100
|
+
throw new SchemaValidationError(`${identifier} value must be present and contain JSON`);
|
|
101
|
+
}
|
|
102
|
+
if ((action === RuleActions.ALL_IN_VALUE ||
|
|
103
|
+
action === RuleActions.ANY_IN_VALUE ||
|
|
104
|
+
action === RuleActions.NONE_IN_VALUE) &&
|
|
105
|
+
!Array.isArray(value)) {
|
|
106
|
+
throw new SchemaValidationError(`${identifier} value must be an array`);
|
|
107
|
+
}
|
|
108
|
+
validateScheduleAndModulo(action, key, value, identifier);
|
|
109
|
+
return { action, key, value };
|
|
110
|
+
};
|
|
111
|
+
/**
|
|
112
|
+
* Checks whether an Action belongs to the supported vocabulary.
|
|
113
|
+
* @param action - Supply the Action name
|
|
114
|
+
* @internal
|
|
115
|
+
*/
|
|
116
|
+
const isRuleAction = (action) => Object.hasOwn(RuleActions, action);
|
|
117
|
+
/**
|
|
118
|
+
* Validates a Flag Default or Rule result.
|
|
119
|
+
* @param value - Supply the result value
|
|
120
|
+
* @param booleanType - Require a boolean for boolean flags
|
|
121
|
+
* @param location - Identify the flag, Rule, and field without values
|
|
122
|
+
* @internal
|
|
123
|
+
*/
|
|
124
|
+
const validateResult = (value, booleanType, location) => {
|
|
125
|
+
if (booleanType && typeof value !== 'boolean')
|
|
126
|
+
throw new SchemaValidationError(`${location} must be a boolean`);
|
|
127
|
+
if (!isJSONValue(value))
|
|
128
|
+
throw new SchemaValidationError(`${location} must contain JSON`);
|
|
129
|
+
return value;
|
|
130
|
+
};
|
|
131
|
+
export { validate };
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { JSONValue } from '@aws-lambda-powertools/commons/types';
|
|
2
|
+
import type { RuleAction } from '../types/RuleEngine.js';
|
|
3
|
+
/**
|
|
4
|
+
* Validates schedule keys, time/day bounds, zones, and modulo integer bounds.
|
|
5
|
+
* @param action - Supply the validated Action name
|
|
6
|
+
* @param key - Supply the context or clock key
|
|
7
|
+
* @param value - Supply the JSON document operand
|
|
8
|
+
* @param location - Identify the Condition without operand values
|
|
9
|
+
* @internal
|
|
10
|
+
*/
|
|
11
|
+
declare const validateScheduleAndModulo: (action: RuleAction, key: string, value: JSONValue, location: string) => void;
|
|
12
|
+
export { validateScheduleAndModulo };
|
|
13
|
+
//# sourceMappingURL=validateScheduleAndModulo.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"validateScheduleAndModulo.d.ts","sourceRoot":"","sources":["../../../src/ruleEngine/validateScheduleAndModulo.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,sCAAsC,CAAC;AAGtE,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,wBAAwB,CAAC;AAazD;;;;;;;GAOG;AACH,QAAA,MAAM,yBAAyB,GAC7B,QAAQ,UAAU,EAClB,KAAK,MAAM,EACX,OAAO,SAAS,EAChB,UAAU,MAAM,KACf,IA4BF,CAAC;AAEF,OAAO,EAAE,yBAAyB,EAAE,CAAC"}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { RuleActions, TimeActionKeys } from '../constants.js';
|
|
2
|
+
import { SchemaValidationError } from '../errors.js';
|
|
3
|
+
import { getDatetimeRangeError, getDaysOfWeekError, getModuloRangeError, getTimeRangeError, isDatetimeRange, isDaysOfWeek, isModuloRange, isTimeRange, } from './guards.js';
|
|
4
|
+
import { isTimeAction } from './time.js';
|
|
5
|
+
/**
|
|
6
|
+
* Validates schedule keys, time/day bounds, zones, and modulo integer bounds.
|
|
7
|
+
* @param action - Supply the validated Action name
|
|
8
|
+
* @param key - Supply the context or clock key
|
|
9
|
+
* @param value - Supply the JSON document operand
|
|
10
|
+
* @param location - Identify the Condition without operand values
|
|
11
|
+
* @internal
|
|
12
|
+
*/
|
|
13
|
+
const validateScheduleAndModulo = (action, key, value, location) => {
|
|
14
|
+
if (action === RuleActions.MODULO_RANGE) {
|
|
15
|
+
if (!isModuloRange(value))
|
|
16
|
+
throw new SchemaValidationError(`${location} ${getModuloRangeError(value)}`);
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
19
|
+
if (!isTimeAction(action))
|
|
20
|
+
return;
|
|
21
|
+
if (key !== TimeActionKeys[action])
|
|
22
|
+
throw new SchemaValidationError(`${location} key must be ${TimeActionKeys[action]}`);
|
|
23
|
+
if (action === RuleActions.SCHEDULE_BETWEEN_DAYS_OF_WEEK) {
|
|
24
|
+
if (!isDaysOfWeek(value))
|
|
25
|
+
throw new SchemaValidationError(`${location} ${getDaysOfWeekError(value)}`);
|
|
26
|
+
}
|
|
27
|
+
else if (action === RuleActions.SCHEDULE_BETWEEN_TIME_RANGE) {
|
|
28
|
+
if (!isTimeRange(value))
|
|
29
|
+
throw new SchemaValidationError(`${location} ${getTimeRangeError(value)}`);
|
|
30
|
+
}
|
|
31
|
+
else if (!isDatetimeRange(value)) {
|
|
32
|
+
throw new SchemaValidationError(`${location} ${getDatetimeRangeError(value)}`);
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
export { validateScheduleAndModulo };
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { GenericLogger } from '@aws-lambda-powertools/commons/types';
|
|
2
|
+
import type { DocumentValidator } from '../types/RuleEngine.js';
|
|
3
|
+
/**
|
|
4
|
+
* Returns a validator that reuses the last validation while document content is unchanged.
|
|
5
|
+
* The returned function keeps state between calls: the last validated document and its content key.
|
|
6
|
+
* It keeps exactly one entry, and invalid documents throw without replacing it.
|
|
7
|
+
* @param logger - Log validation warnings without values
|
|
8
|
+
* @internal
|
|
9
|
+
*/
|
|
10
|
+
declare const createValidationCache: (logger?: GenericLogger) => DocumentValidator;
|
|
11
|
+
export { createValidationCache };
|
|
12
|
+
//# sourceMappingURL=validationCache.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"validationCache.d.ts","sourceRoot":"","sources":["../../../src/ruleEngine/validationCache.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,sCAAsC,CAAC;AAE1E,OAAO,KAAK,EACV,iBAAiB,EAElB,MAAM,wBAAwB,CAAC;AAIhC;;;;;;GAMG;AACH,QAAA,MAAM,qBAAqB,GAAI,SAAS,aAAa,KAAG,iBAcvD,CAAC;AAEF,OAAO,EAAE,qBAAqB,EAAE,CAAC"}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { SchemaValidationError } from '../errors.js';
|
|
2
|
+
import { isJSONValue } from './json.js';
|
|
3
|
+
import { validate } from './validate.js';
|
|
4
|
+
/**
|
|
5
|
+
* Returns a validator that reuses the last validation while document content is unchanged.
|
|
6
|
+
* The returned function keeps state between calls: the last validated document and its content key.
|
|
7
|
+
* It keeps exactly one entry, and invalid documents throw without replacing it.
|
|
8
|
+
* @param logger - Log validation warnings without values
|
|
9
|
+
* @internal
|
|
10
|
+
*/
|
|
11
|
+
const createValidationCache = (logger) => {
|
|
12
|
+
let lastValidated;
|
|
13
|
+
return (document) => {
|
|
14
|
+
// Check before stringifying: undefined, nonfinite numbers, and non-JSON objects can otherwise collide with valid JSON.
|
|
15
|
+
if (!isJSONValue(document))
|
|
16
|
+
throw new SchemaValidationError('Flag Document must contain JSON');
|
|
17
|
+
const key = JSON.stringify(document);
|
|
18
|
+
if (lastValidated?.key === key)
|
|
19
|
+
return lastValidated.document;
|
|
20
|
+
// Validate an owned copy so neither source mutations nor concurrent retrievals change this call's rules.
|
|
21
|
+
const compiled = validate(structuredClone(document), logger);
|
|
22
|
+
lastValidated = { key, document: compiled };
|
|
23
|
+
return compiled;
|
|
24
|
+
};
|
|
25
|
+
};
|
|
26
|
+
export { createValidationCache };
|