@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,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Classifies Parameters retrieval failures using structured fields only.
|
|
3
|
+
*
|
|
4
|
+
* Agent HTTP 5xx and 429 responses identify an unavailable store; other statuses
|
|
5
|
+
* identify configuration failures. Missing parameters always propagate, even
|
|
6
|
+
* when their cause resembles an outage. Transform errors and unrelated errors
|
|
7
|
+
* also propagate because they are not retrieval errors.
|
|
8
|
+
*
|
|
9
|
+
* For a plain GetParameterError, a TypeError cause identifies a failed fetch.
|
|
10
|
+
* Timeout, abort, throttling, internal server, and service unavailable names,
|
|
11
|
+
* Node network codes, and SDK HTTP 5xx or 429 metadata identify an outage.
|
|
12
|
+
* All other causes, including access denial, missing resources, bad requests,
|
|
13
|
+
* other HTTP statuses, and absent causes, propagate unchanged.
|
|
14
|
+
*
|
|
15
|
+
* @param error - Supply the Parameters error to classify
|
|
16
|
+
* @internal
|
|
17
|
+
*/
|
|
18
|
+
declare const isAvailabilityFailure: (error: unknown) => boolean;
|
|
19
|
+
export { isAvailabilityFailure };
|
|
20
|
+
//# sourceMappingURL=classify.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"classify.d.ts","sourceRoot":"","sources":["../../../src/parameters-stores/classify.ts"],"names":[],"mappings":"AAWA;;;;;;;;;;;;;;;;GAgBG;AACH,QAAA,MAAM,qBAAqB,GAAI,OAAO,OAAO,KAAG,OA4B/C,CAAC;AAEF,OAAO,EAAE,qBAAqB,EAAE,CAAC"}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { isRecord } from '@aws-lambda-powertools/commons';
|
|
2
|
+
import { AppConfigAgentError, GetParameterError, ParameterNotFoundError, } from '@aws-lambda-powertools/parameters/errors';
|
|
3
|
+
import { AvailabilityErrorCodes, AvailabilityErrorNames, } from '../constants.js';
|
|
4
|
+
/**
|
|
5
|
+
* Classifies Parameters retrieval failures using structured fields only.
|
|
6
|
+
*
|
|
7
|
+
* Agent HTTP 5xx and 429 responses identify an unavailable store; other statuses
|
|
8
|
+
* identify configuration failures. Missing parameters always propagate, even
|
|
9
|
+
* when their cause resembles an outage. Transform errors and unrelated errors
|
|
10
|
+
* also propagate because they are not retrieval errors.
|
|
11
|
+
*
|
|
12
|
+
* For a plain GetParameterError, a TypeError cause identifies a failed fetch.
|
|
13
|
+
* Timeout, abort, throttling, internal server, and service unavailable names,
|
|
14
|
+
* Node network codes, and SDK HTTP 5xx or 429 metadata identify an outage.
|
|
15
|
+
* All other causes, including access denial, missing resources, bad requests,
|
|
16
|
+
* other HTTP statuses, and absent causes, propagate unchanged.
|
|
17
|
+
*
|
|
18
|
+
* @param error - Supply the Parameters error to classify
|
|
19
|
+
* @internal
|
|
20
|
+
*/
|
|
21
|
+
const isAvailabilityFailure = (error) => {
|
|
22
|
+
if (error instanceof AppConfigAgentError) {
|
|
23
|
+
return error.statusCode >= 500 || error.statusCode === 429;
|
|
24
|
+
}
|
|
25
|
+
if (!(error instanceof GetParameterError) ||
|
|
26
|
+
error instanceof ParameterNotFoundError) {
|
|
27
|
+
return false;
|
|
28
|
+
}
|
|
29
|
+
const cause = error.cause;
|
|
30
|
+
if (cause instanceof TypeError)
|
|
31
|
+
return true;
|
|
32
|
+
// isRecord excludes native Errors, so accept those through their own type guard.
|
|
33
|
+
if (!(cause instanceof Error) && !isRecord(cause))
|
|
34
|
+
return false;
|
|
35
|
+
if (AvailabilityErrorNames.some((name) => cause.name === name))
|
|
36
|
+
return true;
|
|
37
|
+
if ('code' in cause &&
|
|
38
|
+
AvailabilityErrorCodes.some((code) => cause.code === code))
|
|
39
|
+
return true;
|
|
40
|
+
if ('$metadata' in cause && isRecord(cause.$metadata)) {
|
|
41
|
+
const statusCode = cause.$metadata.httpStatusCode;
|
|
42
|
+
return (typeof statusCode === 'number' &&
|
|
43
|
+
(statusCode >= 500 || statusCode === 429));
|
|
44
|
+
}
|
|
45
|
+
return false;
|
|
46
|
+
};
|
|
47
|
+
export { isAvailabilityFailure };
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { JSONValue } from '@aws-lambda-powertools/commons/types';
|
|
2
|
+
import type { EnvelopeOptions } from '../types/Envelope.js';
|
|
3
|
+
/**
|
|
4
|
+
* Creates a Flag Document source with shared availability and envelope handling.
|
|
5
|
+
* @param {Object} options - Configure retrieval and document selection
|
|
6
|
+
* @param {string} options.storeName - Name the Store in availability errors
|
|
7
|
+
* @param {Function} options.retrieve - Retrieve JSON with missing configurations rejected
|
|
8
|
+
* @param {string} options.envelope - Select an embedded Flag Document
|
|
9
|
+
* @param {Function} options.envelopeSearchFn - Apply the envelope expression
|
|
10
|
+
* @internal
|
|
11
|
+
*/
|
|
12
|
+
declare const createDocumentSource: ({ storeName, retrieve, ...envelopeOptions }: {
|
|
13
|
+
storeName: string;
|
|
14
|
+
retrieve: () => Promise<JSONValue>;
|
|
15
|
+
} & EnvelopeOptions) => (() => Promise<JSONValue>);
|
|
16
|
+
export { createDocumentSource };
|
|
17
|
+
//# sourceMappingURL=createDocumentSource.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"createDocumentSource.d.ts","sourceRoot":"","sources":["../../../src/parameters-stores/createDocumentSource.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,sCAAsC,CAAC;AAEtE,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAC;AAI5D;;;;;;;;GAQG;AACH,QAAA,MAAM,oBAAoB,GACvB,6CAIE;IACD,SAAS,EAAE,MAAM,CAAC;IAClB,QAAQ,EAAE,MAAM,OAAO,CAAC,SAAS,CAAC,CAAC;CACpC,GAAG,eAAe,KAAG,CAAC,MAAM,OAAO,CAAC,SAAS,CAAC,CAc9C,CAAC;AAEJ,OAAO,EAAE,oBAAoB,EAAE,CAAC"}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { StoreUnavailableError } from '../errors.js';
|
|
2
|
+
import { isAvailabilityFailure } from './classify.js';
|
|
3
|
+
import { applyEnvelope } from './envelope.js';
|
|
4
|
+
/**
|
|
5
|
+
* Creates a Flag Document source with shared availability and envelope handling.
|
|
6
|
+
* @param {Object} options - Configure retrieval and document selection
|
|
7
|
+
* @param {string} options.storeName - Name the Store in availability errors
|
|
8
|
+
* @param {Function} options.retrieve - Retrieve JSON with missing configurations rejected
|
|
9
|
+
* @param {string} options.envelope - Select an embedded Flag Document
|
|
10
|
+
* @param {Function} options.envelopeSearchFn - Apply the envelope expression
|
|
11
|
+
* @internal
|
|
12
|
+
*/
|
|
13
|
+
const createDocumentSource = ({ storeName, retrieve, ...envelopeOptions }) => async () => {
|
|
14
|
+
let document;
|
|
15
|
+
try {
|
|
16
|
+
document = await retrieve();
|
|
17
|
+
}
|
|
18
|
+
catch (error) {
|
|
19
|
+
if (isAvailabilityFailure(error)) {
|
|
20
|
+
throw new StoreUnavailableError(`${storeName} is unavailable`, {
|
|
21
|
+
cause: error,
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
throw error;
|
|
25
|
+
}
|
|
26
|
+
return applyEnvelope(document, envelopeOptions);
|
|
27
|
+
};
|
|
28
|
+
export { createDocumentSource };
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { JSONValue } from '@aws-lambda-powertools/commons/types';
|
|
2
|
+
import type { EnvelopeOptions, EnvelopeSearchFn } from '../types/Envelope.js';
|
|
3
|
+
/**
|
|
4
|
+
* Rejects incomplete envelope options before a Store constructs its transport.
|
|
5
|
+
* @param {Object} options - Supply the document selection options
|
|
6
|
+
* @param {string} options.envelope - Select an embedded Flag Document
|
|
7
|
+
* @param {Function} options.envelopeSearchFn - Apply the envelope expression
|
|
8
|
+
* @internal
|
|
9
|
+
*/
|
|
10
|
+
declare const validateEnvelopeOptions: (options: {
|
|
11
|
+
envelope?: string;
|
|
12
|
+
envelopeSearchFn?: EnvelopeSearchFn;
|
|
13
|
+
}) => EnvelopeOptions;
|
|
14
|
+
/**
|
|
15
|
+
* Selects a Flag Document without falling back after an envelope miss.
|
|
16
|
+
* @param document - Supply the JSON configuration
|
|
17
|
+
* @param options - Select the Flag Document with an expression and search function
|
|
18
|
+
* @internal
|
|
19
|
+
*/
|
|
20
|
+
declare const applyEnvelope: (document: JSONValue, options: EnvelopeOptions) => JSONValue;
|
|
21
|
+
export { applyEnvelope, validateEnvelopeOptions };
|
|
22
|
+
//# sourceMappingURL=envelope.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"envelope.d.ts","sourceRoot":"","sources":["../../../src/parameters-stores/envelope.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,sCAAsC,CAAC;AAEtE,OAAO,KAAK,EAAE,eAAe,EAAE,gBAAgB,EAAE,MAAM,sBAAsB,CAAC;AAE9E;;;;;;GAMG;AACH,QAAA,MAAM,uBAAuB,GAAI,SAAS;IACxC,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,gBAAgB,CAAC,EAAE,gBAAgB,CAAC;CACrC,KAAG,eASH,CAAC;AAEF;;;;;GAKG;AACH,QAAA,MAAM,aAAa,GACjB,UAAU,SAAS,EACnB,SAAS,eAAe,KACvB,SAWF,CAAC;AAEF,OAAO,EAAE,aAAa,EAAE,uBAAuB,EAAE,CAAC"}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { FeatureFlagsError, SchemaValidationError } from '../errors.js';
|
|
2
|
+
/**
|
|
3
|
+
* Rejects incomplete envelope options before a Store constructs its transport.
|
|
4
|
+
* @param {Object} options - Supply the document selection options
|
|
5
|
+
* @param {string} options.envelope - Select an embedded Flag Document
|
|
6
|
+
* @param {Function} options.envelopeSearchFn - Apply the envelope expression
|
|
7
|
+
* @internal
|
|
8
|
+
*/
|
|
9
|
+
const validateEnvelopeOptions = (options) => {
|
|
10
|
+
const { envelope, envelopeSearchFn } = options;
|
|
11
|
+
if (envelope === undefined && envelopeSearchFn === undefined)
|
|
12
|
+
return {};
|
|
13
|
+
if (envelope === undefined || envelopeSearchFn === undefined) {
|
|
14
|
+
throw new FeatureFlagsError("Options 'envelope' and 'envelopeSearchFn' must be provided together");
|
|
15
|
+
}
|
|
16
|
+
return { envelope, envelopeSearchFn };
|
|
17
|
+
};
|
|
18
|
+
/**
|
|
19
|
+
* Selects a Flag Document without falling back after an envelope miss.
|
|
20
|
+
* @param document - Supply the JSON configuration
|
|
21
|
+
* @param options - Select the Flag Document with an expression and search function
|
|
22
|
+
* @internal
|
|
23
|
+
*/
|
|
24
|
+
const applyEnvelope = (document, options) => {
|
|
25
|
+
const { envelope, envelopeSearchFn } = options;
|
|
26
|
+
if (envelope === undefined)
|
|
27
|
+
return document;
|
|
28
|
+
const result = envelopeSearchFn(envelope, document);
|
|
29
|
+
if (result === undefined || result === null) {
|
|
30
|
+
throw new SchemaValidationError(`Envelope '${envelope}' did not select a Flag Document`);
|
|
31
|
+
}
|
|
32
|
+
// Search functions return unknown; the rule engine validates the selected document at the JSON boundary.
|
|
33
|
+
return result;
|
|
34
|
+
};
|
|
35
|
+
export { applyEnvelope, validateEnvelopeOptions };
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { Comparator, RuleAction, TimeAction } from '../types/RuleEngine.js';
|
|
2
|
+
/** Dispatches each supported Action to its strict comparator. @internal */
|
|
3
|
+
declare const actions: Record<Exclude<RuleAction, TimeAction>, Comparator>;
|
|
4
|
+
export { actions };
|
|
5
|
+
//# sourceMappingURL=actions.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"actions.d.ts","sourceRoot":"","sources":["../../../src/ruleEngine/actions.ts"],"names":[],"mappings":"AASA,OAAO,KAAK,EACV,UAAU,EACV,UAAU,EACV,UAAU,EACX,MAAM,wBAAwB,CAAC;AAkJhC,2EAA2E;AAC3E,QAAA,MAAM,OAAO,EAAE,MAAM,CAAC,OAAO,CAAC,UAAU,EAAE,UAAU,CAAC,EAAE,UAAU,CAqChE,CAAC;AAEF,OAAO,EAAE,OAAO,EAAE,CAAC"}
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
import { compareStringsByCodePoint, isNull, isNumber, isRecord, isStrictEqual, isString, } from '@aws-lambda-powertools/commons';
|
|
2
|
+
import { RuleActions } from '../constants.js';
|
|
3
|
+
import { isModuloRange } from './guards.js';
|
|
4
|
+
/**
|
|
5
|
+
* Compares numbers directly and strings by Unicode code point without coercion.
|
|
6
|
+
* @param context - Supply the context operand
|
|
7
|
+
* @param value - Supply the document operand
|
|
8
|
+
* @param comparator - Compare numeric operands or a string order against zero
|
|
9
|
+
* @internal
|
|
10
|
+
*/
|
|
11
|
+
const compareOrdered = (context, value, comparator) => {
|
|
12
|
+
if (isNumber(context) && isNumber(value)) {
|
|
13
|
+
return comparator(context, value);
|
|
14
|
+
}
|
|
15
|
+
if (isString(context) && isString(value)) {
|
|
16
|
+
return comparator(compareStringsByCodePoint(context, value), 0);
|
|
17
|
+
}
|
|
18
|
+
throw new TypeError('Incompatible operands');
|
|
19
|
+
};
|
|
20
|
+
/**
|
|
21
|
+
* Compares two strings without coercion.
|
|
22
|
+
* @param context - Supply the context operand
|
|
23
|
+
* @param value - Supply the document operand
|
|
24
|
+
* @param comparator - Apply the string comparison
|
|
25
|
+
* @internal
|
|
26
|
+
*/
|
|
27
|
+
const compareStrings = (context, value, comparator) => {
|
|
28
|
+
if (isString(context) && isString(value))
|
|
29
|
+
return comparator(context, value);
|
|
30
|
+
throw new TypeError('Incompatible operands');
|
|
31
|
+
};
|
|
32
|
+
/**
|
|
33
|
+
* Checks whether a string starts with a prefix by Unicode code point.
|
|
34
|
+
* @param container - Supply the string to check
|
|
35
|
+
* @param prefix - Supply the prefix to match
|
|
36
|
+
* @internal
|
|
37
|
+
*/
|
|
38
|
+
const startsWithByCodePoint = (container, prefix) => {
|
|
39
|
+
if (prefix === '')
|
|
40
|
+
return true;
|
|
41
|
+
// Compare code points so a lone surrogate cannot match half of a surrogate pair.
|
|
42
|
+
const containerCodePoints = Array.from(container);
|
|
43
|
+
const prefixCodePoints = Array.from(prefix);
|
|
44
|
+
return prefixCodePoints.every((codePoint, index) => codePoint === containerCodePoints[index]);
|
|
45
|
+
};
|
|
46
|
+
/**
|
|
47
|
+
* Checks whether a string ends with a suffix by Unicode code point.
|
|
48
|
+
* @param container - Supply the string to check
|
|
49
|
+
* @param suffix - Supply the suffix to match
|
|
50
|
+
* @internal
|
|
51
|
+
*/
|
|
52
|
+
const endsWithByCodePoint = (container, suffix) => {
|
|
53
|
+
if (suffix === '')
|
|
54
|
+
return true;
|
|
55
|
+
// Compare code points so a lone surrogate cannot match half of a surrogate pair.
|
|
56
|
+
const containerCodePoints = Array.from(container);
|
|
57
|
+
const suffixCodePoints = Array.from(suffix);
|
|
58
|
+
const start = containerCodePoints.length - suffixCodePoints.length;
|
|
59
|
+
return suffixCodePoints.every((codePoint, index) => codePoint === containerCodePoints[start + index]);
|
|
60
|
+
};
|
|
61
|
+
/**
|
|
62
|
+
* Checks whether an array contains a value using strict JSON equality.
|
|
63
|
+
* @param container - Supply the array to search
|
|
64
|
+
* @param needle - Supply the value to find
|
|
65
|
+
* @internal
|
|
66
|
+
*/
|
|
67
|
+
const containsArray = (container, needle) => container.some((entry) => isStrictEqual(entry, needle));
|
|
68
|
+
/**
|
|
69
|
+
* Checks whether a string contains a substring by Unicode code point.
|
|
70
|
+
* @param container - Supply the string to search
|
|
71
|
+
* @param needle - Supply the substring to find
|
|
72
|
+
* @internal
|
|
73
|
+
*/
|
|
74
|
+
const containsSubstring = (container, needle) => {
|
|
75
|
+
if (needle === '')
|
|
76
|
+
return true;
|
|
77
|
+
// Iterate code points so a lone surrogate cannot match half of a surrogate pair.
|
|
78
|
+
const containerCodePoints = Array.from(container);
|
|
79
|
+
const needleCodePoints = Array.from(needle);
|
|
80
|
+
for (const [index] of containerCodePoints.entries()) {
|
|
81
|
+
if (needleCodePoints.every((codePoint, offset) => codePoint === containerCodePoints[index + offset])) {
|
|
82
|
+
return true;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
return false;
|
|
86
|
+
};
|
|
87
|
+
/**
|
|
88
|
+
* Checks Python-style membership in arrays, own object keys, or strings.
|
|
89
|
+
* @param container - Supply the array, object, or string to search
|
|
90
|
+
* @param needle - Supply the value, key, or substring to find
|
|
91
|
+
* @internal
|
|
92
|
+
*/
|
|
93
|
+
const contains = (container, needle) => {
|
|
94
|
+
if (Array.isArray(container))
|
|
95
|
+
return containsArray(container, needle);
|
|
96
|
+
if (isRecord(container)) {
|
|
97
|
+
if (isString(needle))
|
|
98
|
+
return Object.hasOwn(container, needle);
|
|
99
|
+
if (isNumber(needle) || typeof needle === 'boolean' || isNull(needle))
|
|
100
|
+
return false;
|
|
101
|
+
throw new TypeError('Incompatible operands');
|
|
102
|
+
}
|
|
103
|
+
if (isString(container)) {
|
|
104
|
+
if (!isString(needle))
|
|
105
|
+
throw new TypeError('Incompatible operands');
|
|
106
|
+
return containsSubstring(container, needle);
|
|
107
|
+
}
|
|
108
|
+
throw new TypeError('Incompatible operands');
|
|
109
|
+
};
|
|
110
|
+
/**
|
|
111
|
+
* Compares two arrays using a supplied membership quantifier.
|
|
112
|
+
* @param context - Supply the context array
|
|
113
|
+
* @param value - Supply the document array
|
|
114
|
+
* @param comparator - Apply the membership quantifier
|
|
115
|
+
* @internal
|
|
116
|
+
*/
|
|
117
|
+
const compareArrays = (context, value, comparator) => {
|
|
118
|
+
if (!Array.isArray(context) || !Array.isArray(value))
|
|
119
|
+
throw new TypeError('Incompatible operands');
|
|
120
|
+
return comparator((entry) => containsArray(value, entry), context);
|
|
121
|
+
};
|
|
122
|
+
/** Dispatches each supported Action to its strict comparator. @internal */
|
|
123
|
+
const actions = {
|
|
124
|
+
[RuleActions.EQUALS]: isStrictEqual,
|
|
125
|
+
[RuleActions.NOT_EQUALS]: (context, value) => !isStrictEqual(context, value),
|
|
126
|
+
[RuleActions.KEY_GREATER_THAN_VALUE]: (context, value) => compareOrdered(context, value, (left, right) => left > right),
|
|
127
|
+
[RuleActions.KEY_GREATER_THAN_OR_EQUAL_VALUE]: (context, value) => compareOrdered(context, value, (left, right) => left >= right),
|
|
128
|
+
[RuleActions.KEY_LESS_THAN_VALUE]: (context, value) => compareOrdered(context, value, (left, right) => left < right),
|
|
129
|
+
[RuleActions.KEY_LESS_THAN_OR_EQUAL_VALUE]: (context, value) => compareOrdered(context, value, (left, right) => left <= right),
|
|
130
|
+
[RuleActions.STARTSWITH]: (context, value) => compareStrings(context, value, startsWithByCodePoint),
|
|
131
|
+
[RuleActions.ENDSWITH]: (context, value) => compareStrings(context, value, endsWithByCodePoint),
|
|
132
|
+
[RuleActions.IN]: (context, value) => contains(value, context),
|
|
133
|
+
[RuleActions.NOT_IN]: (context, value) => !contains(value, context),
|
|
134
|
+
[RuleActions.KEY_IN_VALUE]: (context, value) => contains(value, context),
|
|
135
|
+
[RuleActions.KEY_NOT_IN_VALUE]: (context, value) => !contains(value, context),
|
|
136
|
+
[RuleActions.VALUE_IN_KEY]: (context, value) => contains(context, value),
|
|
137
|
+
[RuleActions.VALUE_NOT_IN_KEY]: (context, value) => !contains(context, value),
|
|
138
|
+
[RuleActions.ALL_IN_VALUE]: (context, value) => compareArrays(context, value, (matches, entries) => entries.every(matches)),
|
|
139
|
+
[RuleActions.ANY_IN_VALUE]: (context, value) => compareArrays(context, value, (matches, entries) => entries.some(matches)),
|
|
140
|
+
[RuleActions.NONE_IN_VALUE]: (context, value) => compareArrays(context, value, (matches, entries) => !entries.some(matches)),
|
|
141
|
+
[RuleActions.MODULO_RANGE]: (context, value) => {
|
|
142
|
+
if (!isNumber(context) || !isModuloRange(value))
|
|
143
|
+
throw new TypeError('Incompatible operands');
|
|
144
|
+
const { BASE: base, START: start, END: end } = value;
|
|
145
|
+
const remainder = context % base;
|
|
146
|
+
// Python gives negative remainders the divisor's sign. Avoid adding the base
|
|
147
|
+
// to positive remainders, which can lose precision for fractional contexts.
|
|
148
|
+
const modulo = remainder < 0 ? remainder + base : remainder;
|
|
149
|
+
return start <= modulo && modulo <= end;
|
|
150
|
+
},
|
|
151
|
+
};
|
|
152
|
+
export { actions };
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Parses timezone-naive datetime.fromisoformat formats into sortable wall-clock fields.
|
|
3
|
+
* Accepts calendar and ISO week dates, basic and extended times, date-only bounds,
|
|
4
|
+
* one-character separators, and dot/comma fractions. Like Python, fractions denote
|
|
5
|
+
* seconds even after hours or minutes and truncate to six digits. Never parses a
|
|
6
|
+
* bound through the host timezone or resolves a DST gap/overlap to an instant.
|
|
7
|
+
* @param value - Supply the document bound
|
|
8
|
+
* @internal
|
|
9
|
+
*/
|
|
10
|
+
declare const parseNaiveDatetime: (value: unknown) => string | null;
|
|
11
|
+
export { parseNaiveDatetime };
|
|
12
|
+
//# sourceMappingURL=datetime.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"datetime.d.ts","sourceRoot":"","sources":["../../../src/ruleEngine/datetime.ts"],"names":[],"mappings":"AAyEA;;;;;;;;GAQG;AACH,QAAA,MAAM,kBAAkB,GAAI,OAAO,OAAO,KAAG,MAAM,GAAG,IA8CrD,CAAC;AAEF,OAAO,EAAE,kBAAkB,EAAE,CAAC"}
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Checks Gregorian calendar fields without depending on the host timezone.
|
|
3
|
+
* @param year - Supply the year
|
|
4
|
+
* @param month - Supply the month
|
|
5
|
+
* @param day - Supply the day
|
|
6
|
+
* @internal
|
|
7
|
+
*/
|
|
8
|
+
const isCalendarDate = (year, month, day) => {
|
|
9
|
+
const leapYear = year % 4 === 0 && (year % 100 !== 0 || year % 400 === 0);
|
|
10
|
+
const monthLengths = [
|
|
11
|
+
31,
|
|
12
|
+
leapYear ? 29 : 28,
|
|
13
|
+
31,
|
|
14
|
+
30,
|
|
15
|
+
31,
|
|
16
|
+
30,
|
|
17
|
+
31,
|
|
18
|
+
31,
|
|
19
|
+
30,
|
|
20
|
+
31,
|
|
21
|
+
30,
|
|
22
|
+
31,
|
|
23
|
+
];
|
|
24
|
+
return (year >= 1 &&
|
|
25
|
+
year <= 9999 &&
|
|
26
|
+
month >= 1 &&
|
|
27
|
+
month <= 12 &&
|
|
28
|
+
day >= 1 &&
|
|
29
|
+
day <= monthLengths[month - 1]);
|
|
30
|
+
};
|
|
31
|
+
/**
|
|
32
|
+
* Converts a Python-compatible ISO week date to a calendar date using UTC only.
|
|
33
|
+
* @param year - Supply the ISO week year
|
|
34
|
+
* @param week - Supply the ISO week number
|
|
35
|
+
* @param day - Supply the ISO weekday, Monday being one
|
|
36
|
+
* @internal
|
|
37
|
+
*/
|
|
38
|
+
const weekDate = (year, week, day) => {
|
|
39
|
+
const date = new Date(0);
|
|
40
|
+
date.setUTCFullYear(year, 0, 1);
|
|
41
|
+
const januaryFirst = date.getUTCDay();
|
|
42
|
+
const leapYear = year % 4 === 0 && (year % 100 !== 0 || year % 400 === 0);
|
|
43
|
+
const weeks = januaryFirst === 4 || (januaryFirst === 3 && leapYear) ? 53 : 52;
|
|
44
|
+
if (year < 1 || week < 1 || week > weeks)
|
|
45
|
+
return null;
|
|
46
|
+
date.setUTCDate(4);
|
|
47
|
+
const mondayOffset = (date.getUTCDay() + 6) % 7;
|
|
48
|
+
date.setUTCDate(4 - mondayOffset + (week - 1) * 7 + day - 1);
|
|
49
|
+
if (date.getUTCFullYear() > 9999)
|
|
50
|
+
return null;
|
|
51
|
+
return date.toISOString().slice(0, 10);
|
|
52
|
+
};
|
|
53
|
+
/**
|
|
54
|
+
* Finds Python's date/time separator, including ambiguous basic ISO week dates.
|
|
55
|
+
* @param value - Supply the datetime string
|
|
56
|
+
* @internal
|
|
57
|
+
*/
|
|
58
|
+
const datePrefixLength = (value) => {
|
|
59
|
+
if (value[4] === '-') {
|
|
60
|
+
if (value[5] !== 'W')
|
|
61
|
+
return 10;
|
|
62
|
+
// Python prefers '-' as a separator in YYYY-Www-## over a numeric separator.
|
|
63
|
+
return value[8] === '-' && !/^\d$/.test(value[10] ?? '') ? 10 : 8;
|
|
64
|
+
}
|
|
65
|
+
if (value[4] !== 'W')
|
|
66
|
+
return 8;
|
|
67
|
+
let index = 7;
|
|
68
|
+
while (/^\d$/.test(value[index] ?? ''))
|
|
69
|
+
index++;
|
|
70
|
+
if (index < 9)
|
|
71
|
+
return index;
|
|
72
|
+
return index % 2 === 0 ? 7 : 8;
|
|
73
|
+
};
|
|
74
|
+
/**
|
|
75
|
+
* Parses timezone-naive datetime.fromisoformat formats into sortable wall-clock fields.
|
|
76
|
+
* Accepts calendar and ISO week dates, basic and extended times, date-only bounds,
|
|
77
|
+
* one-character separators, and dot/comma fractions. Like Python, fractions denote
|
|
78
|
+
* seconds even after hours or minutes and truncate to six digits. Never parses a
|
|
79
|
+
* bound through the host timezone or resolves a DST gap/overlap to an instant.
|
|
80
|
+
* @param value - Supply the document bound
|
|
81
|
+
* @internal
|
|
82
|
+
*/
|
|
83
|
+
const parseNaiveDatetime = (value) => {
|
|
84
|
+
if (typeof value !== 'string')
|
|
85
|
+
return null;
|
|
86
|
+
const dateLength = datePrefixLength(value);
|
|
87
|
+
const datePrefix = value.slice(0, dateLength);
|
|
88
|
+
const calendar = /^(\d{4})(-?)(\d{2})\2(\d{2})$/.exec(datePrefix);
|
|
89
|
+
const week = /^(\d{4})(-?)W(\d{2})(?:\2([1-7]))?$/.exec(datePrefix);
|
|
90
|
+
let date;
|
|
91
|
+
if (calendar) {
|
|
92
|
+
const year = Number(calendar[1]);
|
|
93
|
+
const month = Number(calendar[3]);
|
|
94
|
+
const day = Number(calendar[4]);
|
|
95
|
+
if (!isCalendarDate(year, month, day))
|
|
96
|
+
return null;
|
|
97
|
+
date = `${calendar[1]}-${calendar[3]}-${calendar[4]}`;
|
|
98
|
+
}
|
|
99
|
+
else if (week) {
|
|
100
|
+
const parsed = weekDate(Number(week[1]), Number(week[3]), Number(week[4] ?? 1));
|
|
101
|
+
if (parsed === null)
|
|
102
|
+
return null;
|
|
103
|
+
date = parsed;
|
|
104
|
+
}
|
|
105
|
+
else {
|
|
106
|
+
return null;
|
|
107
|
+
}
|
|
108
|
+
if (value.length === dateLength)
|
|
109
|
+
return `${date}T00:00:00.000000`;
|
|
110
|
+
// Python permits any single Unicode character between date and time.
|
|
111
|
+
// A parsed date prefix with remaining input guarantees a separator code point.
|
|
112
|
+
const separator = String.fromCodePoint(value.codePointAt(dateLength));
|
|
113
|
+
// Python's validator replaces Z with +00:00 before parsing, so Z is never a naive separator.
|
|
114
|
+
if (separator === 'Z')
|
|
115
|
+
return null;
|
|
116
|
+
const time = value.slice(dateLength + separator.length);
|
|
117
|
+
// Python also accepts a basic HHMMSS time followed by two or more fraction digits.
|
|
118
|
+
const match = /^(\d{2})(?:(:?)(\d{2})(?:\2(\d{2}))?)?(?:[.,](\d+))?$/.exec(time) ??
|
|
119
|
+
/^(\d{2})()(\d{2})(\d{2})(\d{2,})$/.exec(time);
|
|
120
|
+
// JavaScript's $ also matches before a trailing newline; Python consumes all input.
|
|
121
|
+
if (!match || match[0] !== time)
|
|
122
|
+
return null;
|
|
123
|
+
const hour = match[1];
|
|
124
|
+
const minute = match[3] ?? '00';
|
|
125
|
+
const second = match[4] ?? '00';
|
|
126
|
+
if (Number(hour) > 23 || Number(minute) > 59 || Number(second) > 59)
|
|
127
|
+
return null;
|
|
128
|
+
const fraction = (match[5] ?? '').slice(0, 6).padEnd(6, '0');
|
|
129
|
+
return `${date}T${hour}:${minute}:${second}.${fraction}`;
|
|
130
|
+
};
|
|
131
|
+
export { parseNaiveDatetime };
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { StoreResult } from '../types/index.js';
|
|
2
|
+
import type { EvaluateFlagOptions, Flag } from '../types/RuleEngine.js';
|
|
3
|
+
/**
|
|
4
|
+
* Evaluates one flag using its first fully matching Rule or its Flag Default.
|
|
5
|
+
* @param name - Identify the flag
|
|
6
|
+
* @param flag - Supply the validated flag definition
|
|
7
|
+
* @param {Object} options - Configure evaluation facts, logging, and the clock
|
|
8
|
+
* @param {EvaluationContext} options.context - Supply caller facts for evaluation
|
|
9
|
+
* @param {GenericLogger} options.logger - Log the selected Rule or Flag Default without values
|
|
10
|
+
* @param {Date} options.now - Supply the Store call's shared clock instant
|
|
11
|
+
* @internal
|
|
12
|
+
*/
|
|
13
|
+
declare const evaluate: (name: string, flag: Flag, { context, logger, now }: EvaluateFlagOptions) => StoreResult;
|
|
14
|
+
export { evaluate };
|
|
15
|
+
//# sourceMappingURL=evaluate.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"evaluate.d.ts","sourceRoot":"","sources":["../../../src/ruleEngine/evaluate.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AACrD,OAAO,KAAK,EAEV,mBAAmB,EACnB,IAAI,EAEL,MAAM,wBAAwB,CAAC;AAKhC;;;;;;;;;GASG;AACH,QAAA,MAAM,QAAQ,GACZ,MAAM,MAAM,EACZ,MAAM,IAAI,EACV,0BAA0B,mBAAmB,KAC5C,WAoBF,CAAC;AA4BF,OAAO,EAAE,QAAQ,EAAE,CAAC"}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { isTruthy } from '@aws-lambda-powertools/commons';
|
|
2
|
+
import { StoreNames } from '../constants.js';
|
|
3
|
+
import { ConditionEvaluationError } from '../errors.js';
|
|
4
|
+
import { actions } from './actions.js';
|
|
5
|
+
import { cloneJSONValue } from './json.js';
|
|
6
|
+
import { isTimeAction, timeActions } from './time.js';
|
|
7
|
+
/**
|
|
8
|
+
* Evaluates one flag using its first fully matching Rule or its Flag Default.
|
|
9
|
+
* @param name - Identify the flag
|
|
10
|
+
* @param flag - Supply the validated flag definition
|
|
11
|
+
* @param {Object} options - Configure evaluation facts, logging, and the clock
|
|
12
|
+
* @param {EvaluationContext} options.context - Supply caller facts for evaluation
|
|
13
|
+
* @param {GenericLogger} options.logger - Log the selected Rule or Flag Default without values
|
|
14
|
+
* @param {Date} options.now - Supply the Store call's shared clock instant
|
|
15
|
+
* @internal
|
|
16
|
+
*/
|
|
17
|
+
const evaluate = (name, flag, { context, logger, now }) => {
|
|
18
|
+
for (const [rule, definition] of Object.entries(flag.rules)) {
|
|
19
|
+
if (definition.conditions.every((condition) => matches(condition, { name, rule, context, now }))) {
|
|
20
|
+
const value = cloneJSONValue(definition.when_match);
|
|
21
|
+
logger?.debug({
|
|
22
|
+
store: StoreNames.RULE_ENGINE,
|
|
23
|
+
name,
|
|
24
|
+
outcome: 'rule',
|
|
25
|
+
rule,
|
|
26
|
+
});
|
|
27
|
+
return { found: true, value, enabled: isTruthy(value) };
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
const value = cloneJSONValue(flag.default);
|
|
31
|
+
logger?.debug({ store: StoreNames.RULE_ENGINE, name, outcome: 'default' });
|
|
32
|
+
return { found: true, value, enabled: isTruthy(value) };
|
|
33
|
+
};
|
|
34
|
+
/**
|
|
35
|
+
* Applies the shared clock instant to schedule Conditions and own context keys to ordinary Conditions.
|
|
36
|
+
* @param condition - Supply the validated Condition
|
|
37
|
+
* @param {Object} options - Supply identifiers, caller facts, and the clock
|
|
38
|
+
* @param {string} options.name - Identify the flag
|
|
39
|
+
* @param {string} options.rule - Identify the Rule
|
|
40
|
+
* @param {EvaluationContext} options.context - Supply caller facts for evaluation
|
|
41
|
+
* @param {Date} options.now - Supply the Store call's shared clock instant
|
|
42
|
+
* @internal
|
|
43
|
+
*/
|
|
44
|
+
const matches = (condition, { name, rule, context, now }) => {
|
|
45
|
+
const { key, action, value } = condition;
|
|
46
|
+
try {
|
|
47
|
+
if (isTimeAction(action))
|
|
48
|
+
return timeActions[action](value, now);
|
|
49
|
+
if (!Object.hasOwn(context, key))
|
|
50
|
+
return false;
|
|
51
|
+
return actions[action](context[key], value);
|
|
52
|
+
}
|
|
53
|
+
catch {
|
|
54
|
+
throw new ConditionEvaluationError(`Flag '${name}', rule '${rule}', key '${key}', action '${action}': incompatible operands`);
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
export { evaluate };
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import type { DatetimeRange, DaysOfWeek, ModuloRange, TimeRange } from '../types/RuleEngine.js';
|
|
2
|
+
/**
|
|
3
|
+
* Identifies malformed modulo objects, integer fields, and inclusive bounds.
|
|
4
|
+
* @param value - Supply the document operand
|
|
5
|
+
* @internal
|
|
6
|
+
*/
|
|
7
|
+
declare const getModuloRangeError: (value: unknown) => string | null;
|
|
8
|
+
/**
|
|
9
|
+
* Identifies malformed anchored HH:MM bounds or optional IANA zones.
|
|
10
|
+
* @param value - Supply the document operand
|
|
11
|
+
* @internal
|
|
12
|
+
*/
|
|
13
|
+
declare const getTimeRangeError: (value: unknown) => string | null;
|
|
14
|
+
/**
|
|
15
|
+
* Identifies malformed naive ISO bounds or optional IANA zones.
|
|
16
|
+
* @param value - Supply the document operand
|
|
17
|
+
* @internal
|
|
18
|
+
*/
|
|
19
|
+
declare const getDatetimeRangeError: (value: unknown) => string | null;
|
|
20
|
+
/**
|
|
21
|
+
* Identifies malformed Python day lists or optional IANA zones.
|
|
22
|
+
* @param value - Supply the document operand
|
|
23
|
+
* @internal
|
|
24
|
+
*/
|
|
25
|
+
declare const getDaysOfWeekError: (value: unknown) => string | null;
|
|
26
|
+
/**
|
|
27
|
+
* Narrows complete modulo shapes using the same rules as document validation.
|
|
28
|
+
* @param value - Supply the operand to check
|
|
29
|
+
* @internal
|
|
30
|
+
*/
|
|
31
|
+
declare const isModuloRange: (value: unknown) => value is ModuloRange;
|
|
32
|
+
/**
|
|
33
|
+
* Narrows complete time-range shapes, including anchored bounds and valid zones.
|
|
34
|
+
* @param value - Supply the operand to check
|
|
35
|
+
* @internal
|
|
36
|
+
*/
|
|
37
|
+
declare const isTimeRange: (value: unknown) => value is TimeRange;
|
|
38
|
+
/**
|
|
39
|
+
* Narrows complete datetime-range shapes, including naive ISO bounds and valid zones.
|
|
40
|
+
* @param value - Supply the operand to check
|
|
41
|
+
* @internal
|
|
42
|
+
*/
|
|
43
|
+
declare const isDatetimeRange: (value: unknown) => value is DatetimeRange;
|
|
44
|
+
/**
|
|
45
|
+
* Narrows complete day-list shapes, including Python names and valid zones.
|
|
46
|
+
* @param value - Supply the operand to check
|
|
47
|
+
* @internal
|
|
48
|
+
*/
|
|
49
|
+
declare const isDaysOfWeek: (value: unknown) => value is DaysOfWeek;
|
|
50
|
+
export { getDatetimeRangeError, getDaysOfWeekError, getModuloRangeError, getTimeRangeError, isDatetimeRange, isDaysOfWeek, isModuloRange, isTimeRange, };
|
|
51
|
+
//# sourceMappingURL=guards.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"guards.d.ts","sourceRoot":"","sources":["../../../src/ruleEngine/guards.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EACV,aAAa,EACb,UAAU,EACV,WAAW,EACX,SAAS,EACV,MAAM,wBAAwB,CAAC;AAqBhC;;;;GAIG;AACH,QAAA,MAAM,mBAAmB,GAAI,OAAO,OAAO,KAAG,MAAM,GAAG,IAiBtD,CAAC;AAqBF;;;;GAIG;AACH,QAAA,MAAM,iBAAiB,GAAI,OAAO,OAAO,KAAG,MAAM,GAAG,IAQlD,CAAC;AAEJ;;;;GAIG;AACH,QAAA,MAAM,qBAAqB,GAAI,OAAO,OAAO,KAAG,MAAM,GAAG,IAKtD,CAAC;AAEJ;;;;GAIG;AACH,QAAA,MAAM,kBAAkB,GAAI,OAAO,OAAO,KAAG,MAAM,GAAG,IAUrD,CAAC;AAEF;;;;GAIG;AACH,QAAA,MAAM,aAAa,GAAI,OAAO,OAAO,KAAG,KAAK,IAAI,WACZ,CAAC;AAEtC;;;;GAIG;AACH,QAAA,MAAM,WAAW,GAAI,OAAO,OAAO,KAAG,KAAK,IAAI,SACZ,CAAC;AAEpC;;;;GAIG;AACH,QAAA,MAAM,eAAe,GAAI,OAAO,OAAO,KAAG,KAAK,IAAI,aACZ,CAAC;AAExC;;;;GAIG;AACH,QAAA,MAAM,YAAY,GAAI,OAAO,OAAO,KAAG,KAAK,IAAI,UACZ,CAAC;AAErC,OAAO,EACL,qBAAqB,EACrB,kBAAkB,EAClB,mBAAmB,EACnB,iBAAiB,EACjB,eAAe,EACf,YAAY,EACZ,aAAa,EACb,WAAW,GACZ,CAAC"}
|