flagsmith-nodejs 3.3.3 → 4.0.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/.github/workflows/publish.yml +2 -2
- package/.github/workflows/pull_request.yaml +7 -14
- package/build/{flagsmith-engine → cjs/flagsmith-engine}/environments/models.d.ts +3 -3
- package/build/{flagsmith-engine → cjs/flagsmith-engine}/environments/models.js +20 -13
- package/build/{flagsmith-engine → cjs/flagsmith-engine}/environments/util.d.ts +1 -1
- package/build/cjs/flagsmith-engine/environments/util.js +23 -0
- package/build/cjs/flagsmith-engine/features/models.js +118 -0
- package/build/{flagsmith-engine → cjs/flagsmith-engine}/features/util.d.ts +1 -1
- package/build/cjs/flagsmith-engine/features/util.js +27 -0
- package/build/{flagsmith-engine → cjs/flagsmith-engine}/identities/models.d.ts +2 -2
- package/build/cjs/flagsmith-engine/identities/models.js +48 -0
- package/build/{flagsmith-engine → cjs/flagsmith-engine}/identities/traits/models.js +5 -4
- package/build/{flagsmith-engine → cjs/flagsmith-engine}/identities/util.d.ts +2 -2
- package/build/cjs/flagsmith-engine/identities/util.js +22 -0
- package/build/cjs/flagsmith-engine/index.d.ts +14 -0
- package/build/cjs/flagsmith-engine/index.js +75 -0
- package/build/cjs/flagsmith-engine/organisations/models.js +21 -0
- package/build/{flagsmith-engine → cjs/flagsmith-engine}/organisations/util.d.ts +1 -1
- package/build/cjs/flagsmith-engine/organisations/util.js +8 -0
- package/build/{flagsmith-engine → cjs/flagsmith-engine}/projects/models.d.ts +2 -2
- package/build/{flagsmith-engine → cjs/flagsmith-engine}/projects/models.js +8 -5
- package/build/{flagsmith-engine → cjs/flagsmith-engine}/projects/util.d.ts +1 -1
- package/build/cjs/flagsmith-engine/projects/util.js +15 -0
- package/build/{flagsmith-engine → cjs/flagsmith-engine}/segments/evaluators.d.ts +4 -4
- package/build/cjs/flagsmith-engine/segments/evaluators.js +37 -0
- package/build/{flagsmith-engine → cjs/flagsmith-engine}/segments/models.d.ts +1 -1
- package/build/cjs/flagsmith-engine/segments/models.js +111 -0
- package/build/{flagsmith-engine → cjs/flagsmith-engine}/segments/util.d.ts +1 -1
- package/build/{flagsmith-engine → cjs/flagsmith-engine}/segments/util.js +9 -11
- package/build/{flagsmith-engine → cjs/flagsmith-engine}/utils/collections.d.ts +1 -1
- package/build/cjs/flagsmith-engine/utils/collections.js +6 -0
- package/build/cjs/flagsmith-engine/utils/errors.js +6 -0
- package/build/cjs/flagsmith-engine/utils/hashing/index.js +29 -0
- package/build/{flagsmith-engine → cjs/flagsmith-engine}/utils/index.js +5 -5
- package/build/{index.d.ts → cjs/index.d.ts} +3 -3
- package/build/cjs/index.js +18 -0
- package/build/cjs/package.json +1 -0
- package/build/{sdk → cjs/sdk}/analytics.d.ts +3 -0
- package/build/cjs/sdk/analytics.js +73 -0
- package/build/cjs/sdk/errors.js +9 -0
- package/build/{sdk → cjs/sdk}/index.d.ts +19 -18
- package/build/cjs/sdk/index.js +400 -0
- package/build/{sdk → cjs/sdk}/models.d.ts +2 -2
- package/build/cjs/sdk/models.js +101 -0
- package/build/{sdk → cjs/sdk}/offline_handlers.d.ts +1 -1
- package/build/cjs/sdk/offline_handlers.js +23 -0
- package/build/{sdk → cjs/sdk}/polling_manager.d.ts +1 -1
- package/build/cjs/sdk/polling_manager.js +29 -0
- package/build/{sdk → cjs/sdk}/types.d.ts +15 -7
- package/build/cjs/sdk/utils.d.ts +36 -0
- package/build/cjs/sdk/utils.js +63 -0
- package/build/esm/flagsmith-engine/environments/models.d.ts +22 -0
- package/build/esm/flagsmith-engine/environments/models.js +32 -0
- package/build/esm/flagsmith-engine/environments/util.d.ts +3 -0
- package/build/esm/flagsmith-engine/environments/util.js +18 -0
- package/build/esm/flagsmith-engine/features/constants.d.ts +4 -0
- package/build/esm/flagsmith-engine/features/constants.js +4 -0
- package/build/esm/flagsmith-engine/features/models.d.ts +37 -0
- package/build/esm/flagsmith-engine/features/models.js +110 -0
- package/build/esm/flagsmith-engine/features/util.d.ts +4 -0
- package/build/esm/flagsmith-engine/features/util.js +21 -0
- package/build/esm/flagsmith-engine/identities/models.d.ts +15 -0
- package/build/esm/flagsmith-engine/identities/models.js +44 -0
- package/build/esm/flagsmith-engine/identities/traits/models.d.ts +5 -0
- package/build/esm/flagsmith-engine/identities/traits/models.js +8 -0
- package/build/esm/flagsmith-engine/identities/util.d.ts +4 -0
- package/build/esm/flagsmith-engine/identities/util.js +17 -0
- package/build/esm/flagsmith-engine/index.d.ts +14 -0
- package/build/esm/flagsmith-engine/index.js +62 -0
- package/build/esm/flagsmith-engine/organisations/models.d.ts +9 -0
- package/build/esm/flagsmith-engine/organisations/models.js +17 -0
- package/build/esm/flagsmith-engine/organisations/util.d.ts +2 -0
- package/build/esm/flagsmith-engine/organisations/util.js +4 -0
- package/build/esm/flagsmith-engine/projects/models.d.ts +10 -0
- package/build/esm/flagsmith-engine/projects/models.js +13 -0
- package/build/esm/flagsmith-engine/projects/util.d.ts +2 -0
- package/build/esm/flagsmith-engine/projects/util.js +11 -0
- package/build/esm/flagsmith-engine/segments/constants.d.ts +34 -0
- package/build/esm/flagsmith-engine/segments/constants.js +36 -0
- package/build/esm/flagsmith-engine/segments/evaluators.d.ts +7 -0
- package/build/esm/flagsmith-engine/segments/evaluators.js +31 -0
- package/build/esm/flagsmith-engine/segments/models.d.ts +37 -0
- package/build/esm/flagsmith-engine/segments/models.js +102 -0
- package/build/esm/flagsmith-engine/segments/util.d.ts +6 -0
- package/build/esm/flagsmith-engine/segments/util.js +23 -0
- package/build/esm/flagsmith-engine/utils/collections.d.ts +3 -0
- package/build/esm/flagsmith-engine/utils/collections.js +2 -0
- package/build/esm/flagsmith-engine/utils/errors.d.ts +2 -0
- package/build/esm/flagsmith-engine/utils/errors.js +2 -0
- package/build/esm/flagsmith-engine/utils/hashing/index.d.ts +9 -0
- package/build/esm/flagsmith-engine/utils/hashing/index.js +25 -0
- package/build/esm/flagsmith-engine/utils/index.d.ts +1 -0
- package/build/esm/flagsmith-engine/utils/index.js +13 -0
- package/build/esm/index.d.ts +3 -0
- package/build/esm/index.js +2 -0
- package/build/esm/sdk/analytics.d.ts +35 -0
- package/build/esm/sdk/analytics.js +69 -0
- package/build/esm/sdk/errors.d.ts +4 -0
- package/build/esm/sdk/errors.js +4 -0
- package/build/esm/sdk/index.d.ts +131 -0
- package/build/esm/sdk/index.js +390 -0
- package/build/esm/sdk/models.d.ts +55 -0
- package/build/esm/sdk/models.js +94 -0
- package/build/esm/sdk/offline_handlers.d.ts +9 -0
- package/build/esm/sdk/offline_handlers.js +18 -0
- package/build/esm/sdk/polling_manager.d.ts +9 -0
- package/build/esm/sdk/polling_manager.js +25 -0
- package/build/esm/sdk/types.d.ts +38 -0
- package/build/esm/sdk/types.js +1 -0
- package/build/esm/sdk/utils.d.ts +36 -0
- package/build/esm/sdk/utils.js +56 -0
- package/flagsmith-engine/environments/models.ts +3 -3
- package/flagsmith-engine/environments/util.ts +4 -4
- package/flagsmith-engine/features/models.ts +2 -2
- package/flagsmith-engine/features/util.ts +1 -1
- package/flagsmith-engine/identities/models.ts +4 -5
- package/flagsmith-engine/identities/traits/models.ts +0 -1
- package/flagsmith-engine/identities/util.ts +4 -4
- package/flagsmith-engine/index.ts +13 -13
- package/flagsmith-engine/organisations/util.ts +1 -1
- package/flagsmith-engine/projects/models.ts +2 -2
- package/flagsmith-engine/projects/util.ts +4 -4
- package/flagsmith-engine/segments/evaluators.ts +6 -6
- package/flagsmith-engine/segments/models.ts +5 -5
- package/flagsmith-engine/segments/util.ts +3 -3
- package/flagsmith-engine/utils/collections.ts +1 -1
- package/flagsmith-engine/utils/hashing/index.ts +5 -29
- package/flagsmith-engine/utils/index.ts +1 -1
- package/index.ts +4 -8
- package/package.json +21 -16
- package/sdk/analytics.ts +7 -5
- package/sdk/index.ts +55 -46
- package/sdk/models.ts +2 -3
- package/sdk/offline_handlers.ts +2 -2
- package/sdk/polling_manager.ts +2 -3
- package/sdk/types.ts +35 -24
- package/sdk/utils.ts +49 -37
- package/tests/engine/e2e/engine.test.ts +8 -11
- package/tests/engine/unit/engine.test.ts +5 -5
- package/tests/engine/unit/segments/segment_evaluators.test.ts +9 -9
- package/tests/engine/unit/utils/utils.test.ts +2 -2
- package/tests/sdk/analytics.test.ts +8 -13
- package/tests/sdk/data/identity-with-transient-traits.json +41 -0
- package/tests/sdk/data/transient-identity.json +29 -0
- package/tests/sdk/flagsmith-cache.test.ts +16 -32
- package/tests/sdk/flagsmith-environment-flags.test.ts +21 -36
- package/tests/sdk/flagsmith-identity-flags.test.ts +83 -32
- package/tests/sdk/flagsmith.test.ts +67 -99
- package/tests/sdk/offline-handlers.test.ts +5 -6
- package/tests/sdk/polling.test.ts +6 -8
- package/tests/sdk/utils.ts +19 -15
- package/tsconfig.cjs.json +7 -0
- package/tsconfig.esm.json +7 -0
- package/tsconfig.json +8 -4
- package/vitest.config.ts +17 -0
- package/build/flagsmith-engine/environments/util.js +0 -27
- package/build/flagsmith-engine/features/models.js +0 -132
- package/build/flagsmith-engine/features/util.js +0 -27
- package/build/flagsmith-engine/identities/models.js +0 -113
- package/build/flagsmith-engine/identities/util.js +0 -46
- package/build/flagsmith-engine/index.d.ts +0 -14
- package/build/flagsmith-engine/index.js +0 -127
- package/build/flagsmith-engine/organisations/models.js +0 -21
- package/build/flagsmith-engine/organisations/util.js +0 -8
- package/build/flagsmith-engine/projects/util.js +0 -15
- package/build/flagsmith-engine/segments/evaluators.js +0 -45
- package/build/flagsmith-engine/segments/models.js +0 -147
- package/build/flagsmith-engine/utils/collections.js +0 -26
- package/build/flagsmith-engine/utils/errors.js +0 -26
- package/build/flagsmith-engine/utils/hashing/index.js +0 -60
- package/build/index.js +0 -23
- package/build/sdk/analytics.js +0 -120
- package/build/sdk/errors.js +0 -34
- package/build/sdk/index.js +0 -594
- package/build/sdk/models.js +0 -149
- package/build/sdk/offline_handlers.js +0 -66
- package/build/sdk/polling_manager.js +0 -72
- package/build/sdk/utils.d.ts +0 -12
- package/build/sdk/utils.js +0 -107
- package/jest.config.js +0 -5
- package/tests/index.js +0 -0
- /package/build/{flagsmith-engine → cjs/flagsmith-engine}/features/constants.d.ts +0 -0
- /package/build/{flagsmith-engine → cjs/flagsmith-engine}/features/constants.js +0 -0
- /package/build/{flagsmith-engine → cjs/flagsmith-engine}/features/models.d.ts +0 -0
- /package/build/{flagsmith-engine → cjs/flagsmith-engine}/identities/traits/models.d.ts +0 -0
- /package/build/{flagsmith-engine → cjs/flagsmith-engine}/organisations/models.d.ts +0 -0
- /package/build/{flagsmith-engine → cjs/flagsmith-engine}/segments/constants.d.ts +0 -0
- /package/build/{flagsmith-engine → cjs/flagsmith-engine}/segments/constants.js +0 -0
- /package/build/{flagsmith-engine → cjs/flagsmith-engine}/utils/errors.d.ts +0 -0
- /package/build/{flagsmith-engine → cjs/flagsmith-engine}/utils/hashing/index.d.ts +0 -0
- /package/build/{flagsmith-engine → cjs/flagsmith-engine}/utils/index.d.ts +0 -0
- /package/build/{sdk → cjs/sdk}/errors.d.ts +0 -0
- /package/build/{sdk → cjs/sdk}/types.js +0 -0
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.SegmentModel = exports.SegmentRuleModel = exports.SegmentConditionModel = exports.getMatchingFunctions = exports.semverMatchingFunction = exports.matchingFunctions = exports.any = exports.all = void 0;
|
|
4
|
+
const semver = require("semver");
|
|
5
|
+
const index_js_1 = require("../utils/index.js");
|
|
6
|
+
const constants_js_1 = require("./constants.js");
|
|
7
|
+
const util_js_1 = require("./util.js");
|
|
8
|
+
const all = (iterable) => iterable.filter(e => !!e).length === iterable.length;
|
|
9
|
+
exports.all = all;
|
|
10
|
+
const any = (iterable) => iterable.filter(e => !!e).length > 0;
|
|
11
|
+
exports.any = any;
|
|
12
|
+
exports.matchingFunctions = {
|
|
13
|
+
[constants_js_1.CONDITION_OPERATORS.EQUAL]: (thisValue, otherValue) => thisValue == otherValue,
|
|
14
|
+
[constants_js_1.CONDITION_OPERATORS.GREATER_THAN]: (thisValue, otherValue) => otherValue > thisValue,
|
|
15
|
+
[constants_js_1.CONDITION_OPERATORS.GREATER_THAN_INCLUSIVE]: (thisValue, otherValue) => otherValue >= thisValue,
|
|
16
|
+
[constants_js_1.CONDITION_OPERATORS.LESS_THAN]: (thisValue, otherValue) => thisValue > otherValue,
|
|
17
|
+
[constants_js_1.CONDITION_OPERATORS.LESS_THAN_INCLUSIVE]: (thisValue, otherValue) => thisValue >= otherValue,
|
|
18
|
+
[constants_js_1.CONDITION_OPERATORS.NOT_EQUAL]: (thisValue, otherValue) => thisValue != otherValue,
|
|
19
|
+
[constants_js_1.CONDITION_OPERATORS.CONTAINS]: (thisValue, otherValue) => !!otherValue && otherValue.includes(thisValue),
|
|
20
|
+
};
|
|
21
|
+
exports.semverMatchingFunction = {
|
|
22
|
+
...exports.matchingFunctions,
|
|
23
|
+
[constants_js_1.CONDITION_OPERATORS.EQUAL]: (thisValue, otherValue) => semver.eq(thisValue, otherValue),
|
|
24
|
+
[constants_js_1.CONDITION_OPERATORS.GREATER_THAN]: (thisValue, otherValue) => semver.gt(otherValue, thisValue),
|
|
25
|
+
[constants_js_1.CONDITION_OPERATORS.GREATER_THAN_INCLUSIVE]: (thisValue, otherValue) => semver.gte(otherValue, thisValue),
|
|
26
|
+
[constants_js_1.CONDITION_OPERATORS.LESS_THAN]: (thisValue, otherValue) => semver.gt(thisValue, otherValue),
|
|
27
|
+
[constants_js_1.CONDITION_OPERATORS.LESS_THAN_INCLUSIVE]: (thisValue, otherValue) => semver.gte(thisValue, otherValue),
|
|
28
|
+
};
|
|
29
|
+
const getMatchingFunctions = (semver) => (semver ? exports.semverMatchingFunction : exports.matchingFunctions);
|
|
30
|
+
exports.getMatchingFunctions = getMatchingFunctions;
|
|
31
|
+
class SegmentConditionModel {
|
|
32
|
+
EXCEPTION_OPERATOR_METHODS = {
|
|
33
|
+
[constants_js_1.NOT_CONTAINS]: 'evaluateNotContains',
|
|
34
|
+
[constants_js_1.REGEX]: 'evaluateRegex',
|
|
35
|
+
[constants_js_1.MODULO]: 'evaluateModulo',
|
|
36
|
+
[constants_js_1.IN]: 'evaluateIn'
|
|
37
|
+
};
|
|
38
|
+
operator;
|
|
39
|
+
value;
|
|
40
|
+
property_;
|
|
41
|
+
constructor(operator, value, property) {
|
|
42
|
+
this.operator = operator;
|
|
43
|
+
this.value = value;
|
|
44
|
+
this.property_ = property;
|
|
45
|
+
}
|
|
46
|
+
matchesTraitValue(traitValue) {
|
|
47
|
+
const evaluators = {
|
|
48
|
+
evaluateNotContains: (traitValue) => {
|
|
49
|
+
return typeof traitValue == "string" &&
|
|
50
|
+
!!this.value &&
|
|
51
|
+
!traitValue.includes(this.value?.toString());
|
|
52
|
+
},
|
|
53
|
+
evaluateRegex: (traitValue) => {
|
|
54
|
+
return !!this.value && !!traitValue?.toString().match(new RegExp(this.value));
|
|
55
|
+
},
|
|
56
|
+
evaluateModulo: (traitValue) => {
|
|
57
|
+
if (isNaN(parseFloat(traitValue)) || !this.value) {
|
|
58
|
+
return false;
|
|
59
|
+
}
|
|
60
|
+
const parts = (this.value).split("|");
|
|
61
|
+
const [divisor, reminder] = [parseFloat(parts[0]), parseFloat(parts[1])];
|
|
62
|
+
return traitValue % divisor === reminder;
|
|
63
|
+
},
|
|
64
|
+
evaluateIn: (traitValue) => {
|
|
65
|
+
return this.value?.split(',').includes(traitValue.toString());
|
|
66
|
+
},
|
|
67
|
+
};
|
|
68
|
+
// TODO: move this logic to the evaluator module
|
|
69
|
+
if (this.EXCEPTION_OPERATOR_METHODS[this.operator]) {
|
|
70
|
+
const evaluatorFunction = evaluators[this.EXCEPTION_OPERATOR_METHODS[this.operator]];
|
|
71
|
+
return evaluatorFunction(traitValue);
|
|
72
|
+
}
|
|
73
|
+
const defaultFunction = (x, y) => false;
|
|
74
|
+
const matchingFunctionSet = (0, exports.getMatchingFunctions)((0, util_js_1.isSemver)(this.value));
|
|
75
|
+
const matchingFunction = matchingFunctionSet[this.operator] || defaultFunction;
|
|
76
|
+
const traitType = (0, util_js_1.isSemver)(this.value) ? 'semver' : typeof traitValue;
|
|
77
|
+
const castToTypeOfTraitValue = (0, index_js_1.getCastingFunction)(traitType);
|
|
78
|
+
return matchingFunction(castToTypeOfTraitValue(this.value), traitValue);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
exports.SegmentConditionModel = SegmentConditionModel;
|
|
82
|
+
class SegmentRuleModel {
|
|
83
|
+
type;
|
|
84
|
+
rules = [];
|
|
85
|
+
conditions = [];
|
|
86
|
+
constructor(type) {
|
|
87
|
+
this.type = type;
|
|
88
|
+
}
|
|
89
|
+
static none(iterable) {
|
|
90
|
+
return iterable.filter(e => !!e).length === 0;
|
|
91
|
+
}
|
|
92
|
+
matchingFunction() {
|
|
93
|
+
return {
|
|
94
|
+
[constants_js_1.ANY_RULE]: exports.any,
|
|
95
|
+
[constants_js_1.ALL_RULE]: exports.all,
|
|
96
|
+
[constants_js_1.NONE_RULE]: SegmentRuleModel.none
|
|
97
|
+
}[this.type];
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
exports.SegmentRuleModel = SegmentRuleModel;
|
|
101
|
+
class SegmentModel {
|
|
102
|
+
id;
|
|
103
|
+
name;
|
|
104
|
+
rules = [];
|
|
105
|
+
featureStates = [];
|
|
106
|
+
constructor(id, name) {
|
|
107
|
+
this.id = id;
|
|
108
|
+
this.name = name;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
exports.SegmentModel = SegmentModel;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { SegmentConditionModel, SegmentModel, SegmentRuleModel } from './models';
|
|
1
|
+
import { SegmentConditionModel, SegmentModel, SegmentRuleModel } from './models.js';
|
|
2
2
|
export declare function buildSegmentConditionModel(segmentConditionJSON: any): SegmentConditionModel;
|
|
3
3
|
export declare function buildSegmentRuleModel(ruleModelJSON: any): SegmentRuleModel;
|
|
4
4
|
export declare function buildSegmentModel(segmentModelJSON: any): SegmentModel;
|
|
@@ -1,25 +1,23 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.removeSemverSuffix = exports.isSemver = exports.buildSegmentModel = exports.buildSegmentRuleModel = exports.buildSegmentConditionModel = void 0;
|
|
4
|
-
|
|
5
|
-
|
|
4
|
+
const util_js_1 = require("../features/util.js");
|
|
5
|
+
const models_js_1 = require("./models.js");
|
|
6
6
|
function buildSegmentConditionModel(segmentConditionJSON) {
|
|
7
|
-
return new
|
|
7
|
+
return new models_js_1.SegmentConditionModel(segmentConditionJSON.operator, segmentConditionJSON.value, segmentConditionJSON.property_);
|
|
8
8
|
}
|
|
9
9
|
exports.buildSegmentConditionModel = buildSegmentConditionModel;
|
|
10
10
|
function buildSegmentRuleModel(ruleModelJSON) {
|
|
11
|
-
|
|
12
|
-
ruleModel.rules = ruleModelJSON.rules.map(
|
|
13
|
-
ruleModel.conditions = ruleModelJSON.conditions.map(
|
|
11
|
+
const ruleModel = new models_js_1.SegmentRuleModel(ruleModelJSON.type);
|
|
12
|
+
ruleModel.rules = ruleModelJSON.rules.map((r) => buildSegmentRuleModel(r));
|
|
13
|
+
ruleModel.conditions = ruleModelJSON.conditions.map((c) => buildSegmentConditionModel(c));
|
|
14
14
|
return ruleModel;
|
|
15
15
|
}
|
|
16
16
|
exports.buildSegmentRuleModel = buildSegmentRuleModel;
|
|
17
17
|
function buildSegmentModel(segmentModelJSON) {
|
|
18
|
-
|
|
19
|
-
model.featureStates = segmentModelJSON['feature_states'].map(
|
|
20
|
-
|
|
21
|
-
});
|
|
22
|
-
model.rules = segmentModelJSON['rules'].map(function (r) { return buildSegmentRuleModel(r); });
|
|
18
|
+
const model = new models_js_1.SegmentModel(segmentModelJSON.id, segmentModelJSON.name);
|
|
19
|
+
model.featureStates = segmentModelJSON['feature_states'].map((fs) => (0, util_js_1.buildFeatureStateModel)(fs));
|
|
20
|
+
model.rules = segmentModelJSON['rules'].map((r) => buildSegmentRuleModel(r));
|
|
23
21
|
return model;
|
|
24
22
|
}
|
|
25
23
|
exports.buildSegmentModel = buildSegmentModel;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getHashedPercentateForObjIds = void 0;
|
|
4
|
+
const node_crypto_1 = require("node:crypto");
|
|
5
|
+
const md5 = (data) => (0, node_crypto_1.createHash)('md5').update(data).digest('hex');
|
|
6
|
+
const makeRepeated = (arr, repeats) => Array.from({ length: repeats }, () => arr).flat();
|
|
7
|
+
// https://stackoverflow.com/questions/12532871/how-to-convert-a-very-large-hex-number-to-decimal-in-javascript
|
|
8
|
+
/**
|
|
9
|
+
* Given a list of object ids, get a floating point number between 0 and 1 based on
|
|
10
|
+
* the hash of those ids. This should give the same value every time for any list of ids.
|
|
11
|
+
*
|
|
12
|
+
* @param {Array<any>} objectIds list of object ids to calculate the has for
|
|
13
|
+
* @param {} iterations=1 num times to include each id in the generated string to hash
|
|
14
|
+
* @returns number number between 0 (inclusive) and 100 (exclusive)
|
|
15
|
+
*/
|
|
16
|
+
function getHashedPercentateForObjIds(objectIds, iterations = 1) {
|
|
17
|
+
let toHash = makeRepeated(objectIds, iterations).join(',');
|
|
18
|
+
const hashedValue = md5(toHash);
|
|
19
|
+
const hashedInt = BigInt('0x' + hashedValue);
|
|
20
|
+
const value = (Number((hashedInt % 9999n)) / 9998.0) * 100;
|
|
21
|
+
// we ignore this for it's nearly impossible use case to catch
|
|
22
|
+
/* istanbul ignore next */
|
|
23
|
+
if (value === 100) {
|
|
24
|
+
/* istanbul ignore next */
|
|
25
|
+
return getHashedPercentateForObjIds(objectIds, iterations + 1);
|
|
26
|
+
}
|
|
27
|
+
return value;
|
|
28
|
+
}
|
|
29
|
+
exports.getHashedPercentateForObjIds = getHashedPercentateForObjIds;
|
|
@@ -1,17 +1,17 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.getCastingFunction = void 0;
|
|
4
|
-
|
|
4
|
+
const util_js_1 = require("../segments/util.js");
|
|
5
5
|
function getCastingFunction(traitType) {
|
|
6
6
|
switch (traitType) {
|
|
7
7
|
case 'boolean':
|
|
8
|
-
return
|
|
8
|
+
return (x) => !['False', 'false'].includes(x);
|
|
9
9
|
case 'number':
|
|
10
|
-
return
|
|
10
|
+
return (x) => parseFloat(x);
|
|
11
11
|
case 'semver':
|
|
12
|
-
return
|
|
12
|
+
return (x) => (0, util_js_1.removeSemverSuffix)(x);
|
|
13
13
|
default:
|
|
14
|
-
return
|
|
14
|
+
return (x) => String(x);
|
|
15
15
|
}
|
|
16
16
|
}
|
|
17
17
|
exports.getCastingFunction = getCastingFunction;
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export { AnalyticsProcessor, FlagsmithAPIError, FlagsmithClientError, EnvironmentDataPollingManager, FlagsmithCache, DefaultFlag, Flags,
|
|
2
|
-
export { FlagsmithConfig } from './sdk/types';
|
|
3
|
-
export { EnvironmentModel, FeatureStateModel, IdentityModel, TraitModel, SegmentModel, OrganisationModel } from './flagsmith-engine';
|
|
1
|
+
export { AnalyticsProcessor, FlagsmithAPIError, FlagsmithClientError, EnvironmentDataPollingManager, FlagsmithCache, DefaultFlag, Flags, Flagsmith, } from './sdk/index.js';
|
|
2
|
+
export { FlagsmithConfig } from './sdk/types.js';
|
|
3
|
+
export { EnvironmentModel, FeatureStateModel, IdentityModel, TraitModel, SegmentModel, OrganisationModel } from './flagsmith-engine/index.js';
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.OrganisationModel = exports.SegmentModel = exports.TraitModel = exports.IdentityModel = exports.FeatureStateModel = exports.EnvironmentModel = exports.Flagsmith = exports.Flags = exports.DefaultFlag = exports.EnvironmentDataPollingManager = exports.FlagsmithClientError = exports.FlagsmithAPIError = exports.AnalyticsProcessor = void 0;
|
|
4
|
+
var index_js_1 = require("./sdk/index.js");
|
|
5
|
+
Object.defineProperty(exports, "AnalyticsProcessor", { enumerable: true, get: function () { return index_js_1.AnalyticsProcessor; } });
|
|
6
|
+
Object.defineProperty(exports, "FlagsmithAPIError", { enumerable: true, get: function () { return index_js_1.FlagsmithAPIError; } });
|
|
7
|
+
Object.defineProperty(exports, "FlagsmithClientError", { enumerable: true, get: function () { return index_js_1.FlagsmithClientError; } });
|
|
8
|
+
Object.defineProperty(exports, "EnvironmentDataPollingManager", { enumerable: true, get: function () { return index_js_1.EnvironmentDataPollingManager; } });
|
|
9
|
+
Object.defineProperty(exports, "DefaultFlag", { enumerable: true, get: function () { return index_js_1.DefaultFlag; } });
|
|
10
|
+
Object.defineProperty(exports, "Flags", { enumerable: true, get: function () { return index_js_1.Flags; } });
|
|
11
|
+
Object.defineProperty(exports, "Flagsmith", { enumerable: true, get: function () { return index_js_1.Flagsmith; } });
|
|
12
|
+
var index_js_2 = require("./flagsmith-engine/index.js");
|
|
13
|
+
Object.defineProperty(exports, "EnvironmentModel", { enumerable: true, get: function () { return index_js_2.EnvironmentModel; } });
|
|
14
|
+
Object.defineProperty(exports, "FeatureStateModel", { enumerable: true, get: function () { return index_js_2.FeatureStateModel; } });
|
|
15
|
+
Object.defineProperty(exports, "IdentityModel", { enumerable: true, get: function () { return index_js_2.IdentityModel; } });
|
|
16
|
+
Object.defineProperty(exports, "TraitModel", { enumerable: true, get: function () { return index_js_2.TraitModel; } });
|
|
17
|
+
Object.defineProperty(exports, "SegmentModel", { enumerable: true, get: function () { return index_js_2.SegmentModel; } });
|
|
18
|
+
Object.defineProperty(exports, "OrganisationModel", { enumerable: true, get: function () { return index_js_2.OrganisationModel; } });
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"type": "commonjs"}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { Logger } from 'pino';
|
|
2
|
+
import { Fetch } from "./types.js";
|
|
2
3
|
export declare class AnalyticsProcessor {
|
|
3
4
|
private analyticsEndpoint;
|
|
4
5
|
private environmentKey;
|
|
@@ -9,6 +10,7 @@ export declare class AnalyticsProcessor {
|
|
|
9
10
|
private requestTimeoutMs;
|
|
10
11
|
private logger;
|
|
11
12
|
private currentFlush;
|
|
13
|
+
private customFetch;
|
|
12
14
|
/**
|
|
13
15
|
* AnalyticsProcessor is used to track how often individual Flags are evaluated within
|
|
14
16
|
* the Flagsmith SDK. Docs: https://docs.flagsmith.com/advanced-use/flag-analytics.
|
|
@@ -23,6 +25,7 @@ export declare class AnalyticsProcessor {
|
|
|
23
25
|
baseApiUrl: string;
|
|
24
26
|
requestTimeoutMs?: number;
|
|
25
27
|
logger?: Logger;
|
|
28
|
+
fetch?: Fetch;
|
|
26
29
|
});
|
|
27
30
|
/**
|
|
28
31
|
* Sends all the collected data to the api asynchronously and resets the timer
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.AnalyticsProcessor = void 0;
|
|
4
|
+
const pino_1 = require("pino");
|
|
5
|
+
const ANALYTICS_ENDPOINT = 'analytics/flags/';
|
|
6
|
+
// Used to control how often we send data(in seconds)
|
|
7
|
+
const ANALYTICS_TIMER = 10;
|
|
8
|
+
class AnalyticsProcessor {
|
|
9
|
+
analyticsEndpoint;
|
|
10
|
+
environmentKey;
|
|
11
|
+
lastFlushed;
|
|
12
|
+
analyticsData;
|
|
13
|
+
requestTimeoutMs = 3000;
|
|
14
|
+
logger;
|
|
15
|
+
currentFlush;
|
|
16
|
+
customFetch;
|
|
17
|
+
/**
|
|
18
|
+
* AnalyticsProcessor is used to track how often individual Flags are evaluated within
|
|
19
|
+
* the Flagsmith SDK. Docs: https://docs.flagsmith.com/advanced-use/flag-analytics.
|
|
20
|
+
*
|
|
21
|
+
* @param data.environmentKey environment key obtained from the Flagsmith UI
|
|
22
|
+
* @param data.baseApiUrl base api url to override when using self hosted version
|
|
23
|
+
* @param data.requestTimeoutMs used to tell requests to stop waiting for a response after a
|
|
24
|
+
given number of milliseconds
|
|
25
|
+
*/
|
|
26
|
+
constructor(data) {
|
|
27
|
+
this.analyticsEndpoint = data.baseApiUrl + ANALYTICS_ENDPOINT;
|
|
28
|
+
this.environmentKey = data.environmentKey;
|
|
29
|
+
this.lastFlushed = Date.now();
|
|
30
|
+
this.analyticsData = {};
|
|
31
|
+
this.requestTimeoutMs = data.requestTimeoutMs || this.requestTimeoutMs;
|
|
32
|
+
this.logger = data.logger || (0, pino_1.pino)();
|
|
33
|
+
this.customFetch = data.fetch ?? fetch;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Sends all the collected data to the api asynchronously and resets the timer
|
|
37
|
+
*/
|
|
38
|
+
async flush() {
|
|
39
|
+
if (this.currentFlush || !Object.keys(this.analyticsData).length) {
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
try {
|
|
43
|
+
this.currentFlush = this.customFetch(this.analyticsEndpoint, {
|
|
44
|
+
method: 'POST',
|
|
45
|
+
body: JSON.stringify(this.analyticsData),
|
|
46
|
+
signal: AbortSignal.timeout(this.requestTimeoutMs),
|
|
47
|
+
headers: {
|
|
48
|
+
'Content-Type': 'application/json',
|
|
49
|
+
'X-Environment-Key': this.environmentKey
|
|
50
|
+
}
|
|
51
|
+
});
|
|
52
|
+
await this.currentFlush;
|
|
53
|
+
}
|
|
54
|
+
catch (error) {
|
|
55
|
+
// We don't want failing to write analytics to cause any exceptions in the main
|
|
56
|
+
// thread so we just swallow them here.
|
|
57
|
+
this.logger.warn('Failed to post analytics to Flagsmith API. Not clearing data, will retry.');
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
finally {
|
|
61
|
+
this.currentFlush = undefined;
|
|
62
|
+
}
|
|
63
|
+
this.analyticsData = {};
|
|
64
|
+
this.lastFlushed = Date.now();
|
|
65
|
+
}
|
|
66
|
+
trackFeature(featureName) {
|
|
67
|
+
this.analyticsData[featureName] = (this.analyticsData[featureName] || 0) + 1;
|
|
68
|
+
if (Date.now() - this.lastFlushed > ANALYTICS_TIMER * 1000) {
|
|
69
|
+
this.flush();
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
exports.AnalyticsProcessor = AnalyticsProcessor;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.FlagsmithAPIError = exports.FlagsmithClientError = void 0;
|
|
4
|
+
class FlagsmithClientError extends Error {
|
|
5
|
+
}
|
|
6
|
+
exports.FlagsmithClientError = FlagsmithClientError;
|
|
7
|
+
class FlagsmithAPIError extends Error {
|
|
8
|
+
}
|
|
9
|
+
exports.FlagsmithAPIError = FlagsmithAPIError;
|
|
@@ -1,23 +1,23 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { EnvironmentModel } from '../flagsmith-engine/
|
|
3
|
-
import { IdentityModel } from '../flagsmith-engine/
|
|
4
|
-
import { BaseOfflineHandler } from './offline_handlers';
|
|
5
|
-
import { DefaultFlag, Flags } from './models';
|
|
6
|
-
import { EnvironmentDataPollingManager } from './polling_manager';
|
|
7
|
-
import { SegmentModel } from '../flagsmith-engine/
|
|
8
|
-
import { FlagsmithConfig } from './types';
|
|
9
|
-
export { AnalyticsProcessor } from './analytics';
|
|
10
|
-
export { FlagsmithAPIError, FlagsmithClientError } from './errors';
|
|
11
|
-
export { DefaultFlag, Flags } from './models';
|
|
12
|
-
export { EnvironmentDataPollingManager } from './polling_manager';
|
|
13
|
-
export { FlagsmithCache, FlagsmithConfig } from './types';
|
|
1
|
+
import { Dispatcher } from 'undici-types';
|
|
2
|
+
import { EnvironmentModel } from '../flagsmith-engine/index.js';
|
|
3
|
+
import { IdentityModel } from '../flagsmith-engine/index.js';
|
|
4
|
+
import { BaseOfflineHandler } from './offline_handlers.js';
|
|
5
|
+
import { DefaultFlag, Flags } from './models.js';
|
|
6
|
+
import { EnvironmentDataPollingManager } from './polling_manager.js';
|
|
7
|
+
import { SegmentModel } from '../flagsmith-engine/index.js';
|
|
8
|
+
import { FlagsmithConfig, FlagsmithTraitValue, ITraitConfig } from './types.js';
|
|
9
|
+
export { AnalyticsProcessor } from './analytics.js';
|
|
10
|
+
export { FlagsmithAPIError, FlagsmithClientError } from './errors.js';
|
|
11
|
+
export { DefaultFlag, Flags } from './models.js';
|
|
12
|
+
export { EnvironmentDataPollingManager } from './polling_manager.js';
|
|
13
|
+
export { FlagsmithCache, FlagsmithConfig } from './types.js';
|
|
14
14
|
export declare class Flagsmith {
|
|
15
15
|
environmentKey?: string;
|
|
16
16
|
apiUrl?: string;
|
|
17
17
|
customHeaders?: {
|
|
18
18
|
[key: string]: any;
|
|
19
19
|
};
|
|
20
|
-
agent
|
|
20
|
+
agent?: Dispatcher;
|
|
21
21
|
requestTimeoutMs?: number;
|
|
22
22
|
enableLocalEvaluation?: boolean;
|
|
23
23
|
environmentRefreshIntervalSeconds: number;
|
|
@@ -36,6 +36,7 @@ export declare class Flagsmith {
|
|
|
36
36
|
private onEnvironmentChange?;
|
|
37
37
|
private analyticsProcessor?;
|
|
38
38
|
private logger;
|
|
39
|
+
private customFetch;
|
|
39
40
|
/**
|
|
40
41
|
* A Flagsmith client.
|
|
41
42
|
*
|
|
@@ -87,13 +88,13 @@ export declare class Flagsmith {
|
|
|
87
88
|
*
|
|
88
89
|
* @param {string} identifier a unique identifier for the identity in the current
|
|
89
90
|
environment, e.g. email address, username, uuid
|
|
90
|
-
* @param {{[key:string]:any}} traits? a dictionary of traits to add / update on the identity in
|
|
91
|
-
Flagsmith, e.g. {"num_orders": 10}
|
|
91
|
+
* @param {{[key:string]:any | ITraitConfig}} traits? a dictionary of traits to add / update on the identity in
|
|
92
|
+
Flagsmith, e.g. {"num_orders": 10} or {age: {value: 30, transient: true}}
|
|
92
93
|
* @returns Flags object holding all the flags for the given identity.
|
|
93
94
|
*/
|
|
94
95
|
getIdentityFlags(identifier: string, traits?: {
|
|
95
|
-
[key: string]:
|
|
96
|
-
}): Promise<Flags>;
|
|
96
|
+
[key: string]: FlagsmithTraitValue | ITraitConfig;
|
|
97
|
+
}, transient?: boolean): Promise<Flags>;
|
|
97
98
|
/**
|
|
98
99
|
* Get the segments for the current environment for a given identity. Will also
|
|
99
100
|
upsert all traits to the Flagsmith API for future evaluations. Providing a
|