@splitsoftware/openfeature-web-split-provider 1.0.0 → 1.2.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/CHANGES.txt +13 -2
- package/README.md +190 -16
- package/es/lib/context.js +55 -0
- package/es/lib/evaluation.js +21 -0
- package/es/lib/events.js +69 -0
- package/es/lib/js-split-provider.js +80 -142
- package/es/lib/parsers.js +38 -0
- package/es/lib/types.js +2 -0
- package/lib/lib/context.js +61 -0
- package/lib/lib/evaluation.js +25 -0
- package/lib/lib/events.js +74 -0
- package/lib/lib/js-split-provider.js +78 -140
- package/lib/lib/parsers.js +44 -0
- package/lib/lib/types.js +5 -0
- package/package.json +2 -2
- package/src/__tests__/bucketingKey.spec.js +145 -0
- package/src/__tests__/context.spec.js +146 -0
- package/src/__tests__/evaluation.spec.js +76 -0
- package/src/__tests__/events.spec.js +182 -0
- package/src/__tests__/integration/bucketing-key.test.js +81 -0
- package/src/__tests__/integration/e2e-events.test.js +1 -8
- package/src/__tests__/integration/openfeature-evaluation.test.js +162 -0
- package/src/__tests__/parsers.spec.js +115 -0
- package/src/__tests__/provider.spec.js +59 -3
- package/src/__tests__/types.spec.js +15 -0
- package/src/lib/context.ts +62 -0
- package/src/lib/evaluation.ts +33 -0
- package/src/lib/events.ts +105 -0
- package/src/lib/js-split-provider.ts +103 -180
- package/src/lib/parsers.ts +43 -0
- package/src/lib/types.ts +12 -0
- package/types/lib/context.d.ts +15 -0
- package/types/lib/evaluation.d.ts +7 -0
- package/types/lib/events.d.ts +20 -0
- package/types/lib/js-split-provider.d.ts +11 -7
- package/types/lib/parsers.d.ts +8 -0
- package/types/lib/types.d.ts +10 -0
|
@@ -1,50 +1,57 @@
|
|
|
1
|
-
import { __assign, __awaiter, __generator
|
|
2
|
-
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
1
|
+
import { __assign, __awaiter, __generator } from "tslib";
|
|
3
2
|
/* eslint-disable @typescript-eslint/no-unused-vars */
|
|
4
|
-
import { ParseError,
|
|
5
|
-
|
|
6
|
-
|
|
3
|
+
import { ParseError, ProviderEvents, OpenFeatureEventEmitter, } from "@openfeature/web-sdk";
|
|
4
|
+
import { bucketingKeyWarning, toSplitKey, transformContext } from "./context";
|
|
5
|
+
import { attachSplitReadyHandlers, attachSplitUpdateHandler } from "./events";
|
|
6
|
+
import { evaluateTreatment } from "./evaluation";
|
|
7
|
+
import { parseBooleanTreatment, parseValidJsonObject, parseValidNumber } from "./parsers";
|
|
7
8
|
var OpenFeatureSplitProvider = /** @class */ (function () {
|
|
8
9
|
function OpenFeatureSplitProvider(splitFactory) {
|
|
9
|
-
this.metadata = {
|
|
10
|
-
|
|
11
|
-
|
|
10
|
+
this.metadata = { name: "split" };
|
|
11
|
+
/** Readiness of the bound client (rejects if it timed out). */
|
|
12
|
+
this.ready = Promise.resolve();
|
|
12
13
|
this.events = new OpenFeatureEventEmitter();
|
|
13
|
-
// Asume 'user' as default traffic type'
|
|
14
14
|
this.trafficType = 'user';
|
|
15
15
|
this.factory = splitFactory;
|
|
16
16
|
this.client = splitFactory.client();
|
|
17
17
|
}
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
}
|
|
33
|
-
return Promise.resolve();
|
|
18
|
+
/**
|
|
19
|
+
* Warn through the Split SDK logger, so it follows the SDK log settings (`debug`, `factory.Logger`).
|
|
20
|
+
* `settings.log` is internal: the public typings only expose `factory.Logger`, which has no `warn`.
|
|
21
|
+
*/
|
|
22
|
+
OpenFeatureSplitProvider.prototype.warn = function (message) {
|
|
23
|
+
var _a, _b, _c;
|
|
24
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
25
|
+
(_c = (_b = (_a = this.factory.settings) === null || _a === void 0 ? void 0 : _a.log) === null || _b === void 0 ? void 0 : _b.warn) === null || _c === void 0 ? void 0 : _c.call(_b, "[".concat(this.metadata.name, "-openfeature] ").concat(message));
|
|
26
|
+
};
|
|
27
|
+
/** Warn once per context set; evaluations reuse the context and would repeat it. */
|
|
28
|
+
OpenFeatureSplitProvider.prototype.warnIfBucketingKeyIgnored = function (context) {
|
|
29
|
+
var warning = bucketingKeyWarning(context);
|
|
30
|
+
if (warning)
|
|
31
|
+
this.warn(warning);
|
|
34
32
|
};
|
|
35
|
-
OpenFeatureSplitProvider.prototype.initialize = function () {
|
|
33
|
+
OpenFeatureSplitProvider.prototype.initialize = function (context) {
|
|
36
34
|
return __awaiter(this, void 0, void 0, function () {
|
|
35
|
+
var key;
|
|
37
36
|
var _this = this;
|
|
38
37
|
return __generator(this, function (_a) {
|
|
39
38
|
switch (_a.label) {
|
|
40
|
-
case 0:
|
|
39
|
+
case 0:
|
|
40
|
+
if (context)
|
|
41
|
+
this.warnIfBucketingKeyIgnored(context);
|
|
42
|
+
key = context && toSplitKey(transformContext(context, this.trafficType));
|
|
43
|
+
if (key)
|
|
44
|
+
this.client = this.factory.client(key);
|
|
45
|
+
this.ready = new Promise(function (resolve, reject) {
|
|
41
46
|
try {
|
|
42
|
-
_this.
|
|
47
|
+
attachSplitReadyHandlers(_this.client, _this.events, { onSdkReady: resolve, onSdkTimedOut: reject }, _this.metadata.name);
|
|
48
|
+
attachSplitUpdateHandler(_this.client, _this.events, _this.metadata.name);
|
|
43
49
|
}
|
|
44
50
|
catch (_a) {
|
|
45
51
|
reject();
|
|
46
52
|
}
|
|
47
|
-
})
|
|
53
|
+
});
|
|
54
|
+
return [4 /*yield*/, this.ready];
|
|
48
55
|
case 1:
|
|
49
56
|
_a.sent();
|
|
50
57
|
return [2 /*return*/];
|
|
@@ -52,58 +59,64 @@ var OpenFeatureSplitProvider = /** @class */ (function () {
|
|
|
52
59
|
});
|
|
53
60
|
});
|
|
54
61
|
};
|
|
55
|
-
OpenFeatureSplitProvider.prototype.
|
|
56
|
-
var
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
62
|
+
OpenFeatureSplitProvider.prototype.onContextChange = function (oldContext, newContext) {
|
|
63
|
+
var _this = this;
|
|
64
|
+
this.warnIfBucketingKeyIgnored(newContext);
|
|
65
|
+
var newTrafficType = newContext.trafficType;
|
|
66
|
+
this.trafficType =
|
|
67
|
+
newTrafficType && newTrafficType !== this.trafficType
|
|
68
|
+
? newTrafficType
|
|
69
|
+
: this.trafficType;
|
|
70
|
+
var oldConsumer = transformContext(oldContext, this.trafficType);
|
|
71
|
+
var newConsumer = transformContext(newContext, this.trafficType);
|
|
72
|
+
var newKey = toSplitKey(newConsumer);
|
|
73
|
+
var keyChanged = newConsumer.targetingKey !== oldConsumer.targetingKey ||
|
|
74
|
+
newConsumer.bucketingKey !== oldConsumer.bucketingKey;
|
|
75
|
+
if (!newKey || !keyChanged) {
|
|
76
|
+
// Same client: OpenFeature marks the provider READY when this resolves, so wait if it is still loading
|
|
77
|
+
// (a timeout during that load must still surface as ERROR). If it already timed out, that was reported
|
|
78
|
+
// and this.ready stays rejected, so resolve instead of moving the provider to ERROR on a no-key change.
|
|
79
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
80
|
+
var _a = this.client.__getStatus(), isReady = _a.isReady, hasTimedout = _a.hasTimedout;
|
|
81
|
+
return isReady || hasTimedout ? Promise.resolve() : this.ready;
|
|
63
82
|
}
|
|
64
|
-
|
|
83
|
+
this.client = this.factory.client(newKey);
|
|
84
|
+
this.ready = new Promise(function (resolve, reject) {
|
|
85
|
+
var emitContextChange = function () {
|
|
86
|
+
_this.events.emit(ProviderEvents.ConfigurationChanged);
|
|
87
|
+
resolve();
|
|
88
|
+
};
|
|
89
|
+
attachSplitReadyHandlers(_this.client, _this.events, { onSdkReady: emitContextChange, onSdkTimedOut: reject }, _this.metadata.name);
|
|
90
|
+
attachSplitUpdateHandler(_this.client, _this.events, _this.metadata.name);
|
|
91
|
+
});
|
|
92
|
+
return this.ready;
|
|
93
|
+
};
|
|
94
|
+
OpenFeatureSplitProvider.prototype.resolveBooleanEvaluation = function (flagKey, _, context, _logger) {
|
|
95
|
+
var details = evaluateTreatment(this.client, flagKey, transformContext(context, this.trafficType));
|
|
96
|
+
var value = parseBooleanTreatment(details.value);
|
|
97
|
+
return __assign(__assign({}, details), { value: value });
|
|
65
98
|
};
|
|
66
99
|
OpenFeatureSplitProvider.prototype.resolveStringEvaluation = function (flagKey, _, context, _logger) {
|
|
67
|
-
|
|
68
|
-
return details;
|
|
100
|
+
return evaluateTreatment(this.client, flagKey, transformContext(context, this.trafficType));
|
|
69
101
|
};
|
|
70
102
|
OpenFeatureSplitProvider.prototype.resolveNumberEvaluation = function (flagKey, _, context, _logger) {
|
|
71
|
-
var details = this.
|
|
72
|
-
return __assign(__assign({}, details), { value:
|
|
103
|
+
var details = evaluateTreatment(this.client, flagKey, transformContext(context, this.trafficType));
|
|
104
|
+
return __assign(__assign({}, details), { value: parseValidNumber(details.value) });
|
|
73
105
|
};
|
|
74
106
|
OpenFeatureSplitProvider.prototype.resolveObjectEvaluation = function (flagKey, _, context, _logger) {
|
|
75
|
-
var details = this.
|
|
76
|
-
return __assign(__assign({}, details), { value:
|
|
77
|
-
};
|
|
78
|
-
OpenFeatureSplitProvider.prototype.evaluateTreatment = function (flagKey, consumer) {
|
|
79
|
-
if (flagKey == null || flagKey === '') {
|
|
80
|
-
throw new FlagNotFoundError('flagKey must be a non-empty string');
|
|
81
|
-
}
|
|
82
|
-
var treatment = this.client.getTreatmentWithConfig(flagKey, consumer.attributes);
|
|
83
|
-
var value = treatment.treatment, config = treatment.config;
|
|
84
|
-
if (value === CONTROL_TREATMENT) {
|
|
85
|
-
throw new FlagNotFoundError(CONTROL_VALUE_ERROR_MESSAGE);
|
|
86
|
-
}
|
|
87
|
-
var flagMetadata = { config: config ? config : '' };
|
|
88
|
-
var details = {
|
|
89
|
-
value: value,
|
|
90
|
-
variant: value,
|
|
91
|
-
flagMetadata: flagMetadata,
|
|
92
|
-
reason: StandardResolutionReasons.TARGETING_MATCH,
|
|
93
|
-
};
|
|
94
|
-
return details;
|
|
107
|
+
var details = evaluateTreatment(this.client, flagKey, transformContext(context, this.trafficType));
|
|
108
|
+
return __assign(__assign({}, details), { value: parseValidJsonObject(details.value) });
|
|
95
109
|
};
|
|
96
110
|
OpenFeatureSplitProvider.prototype.track = function (trackingEventName, context, details) {
|
|
97
|
-
|
|
98
|
-
if (trackingEventName == null || trackingEventName === '')
|
|
111
|
+
if (trackingEventName == null || trackingEventName === '') {
|
|
99
112
|
throw new ParseError('Missing eventName, required to track');
|
|
100
|
-
|
|
113
|
+
}
|
|
114
|
+
var trafficType = transformContext(context, this.trafficType).trafficType;
|
|
101
115
|
var value;
|
|
102
116
|
var properties = {};
|
|
103
117
|
if (details != null) {
|
|
104
|
-
if (details.value != null)
|
|
118
|
+
if (details.value != null)
|
|
105
119
|
value = details.value;
|
|
106
|
-
}
|
|
107
120
|
if (details.properties != null) {
|
|
108
121
|
properties = details.properties;
|
|
109
122
|
}
|
|
@@ -117,81 +130,6 @@ var OpenFeatureSplitProvider = /** @class */ (function () {
|
|
|
117
130
|
});
|
|
118
131
|
});
|
|
119
132
|
};
|
|
120
|
-
//Transform the context into an object useful for the Split API, an key string with arbitrary Split "Attributes".
|
|
121
|
-
OpenFeatureSplitProvider.prototype.transformContext = function (context) {
|
|
122
|
-
var targetingKey = context.targetingKey, ttVal = context.trafficType, attributes = __rest(context, ["targetingKey", "trafficType"]);
|
|
123
|
-
var trafficType = ttVal != null && typeof ttVal === 'string' && ttVal.trim() !== ''
|
|
124
|
-
? ttVal
|
|
125
|
-
: this.trafficType;
|
|
126
|
-
return {
|
|
127
|
-
targetingKey: targetingKey,
|
|
128
|
-
trafficType: trafficType,
|
|
129
|
-
// Stringify context objects include date.
|
|
130
|
-
attributes: JSON.parse(JSON.stringify(attributes)),
|
|
131
|
-
};
|
|
132
|
-
};
|
|
133
|
-
OpenFeatureSplitProvider.prototype.parseValidNumber = function (stringValue) {
|
|
134
|
-
if (stringValue === undefined) {
|
|
135
|
-
throw new ParseError("Invalid 'undefined' value.");
|
|
136
|
-
}
|
|
137
|
-
var result = Number.parseFloat(stringValue);
|
|
138
|
-
if (Number.isNaN(result)) {
|
|
139
|
-
throw new ParseError("Invalid numeric value ".concat(stringValue));
|
|
140
|
-
}
|
|
141
|
-
return result;
|
|
142
|
-
};
|
|
143
|
-
OpenFeatureSplitProvider.prototype.parseValidJsonObject = function (stringValue) {
|
|
144
|
-
if (stringValue === undefined) {
|
|
145
|
-
throw new ParseError("Invalid 'undefined' JSON value.");
|
|
146
|
-
}
|
|
147
|
-
// we may want to allow the parsing to be customized.
|
|
148
|
-
try {
|
|
149
|
-
var value = JSON.parse(stringValue);
|
|
150
|
-
if (typeof value !== "object") {
|
|
151
|
-
throw new ParseError("Flag value ".concat(stringValue, " had unexpected type ").concat(typeof value, ", expected \"object\""));
|
|
152
|
-
}
|
|
153
|
-
return value;
|
|
154
|
-
}
|
|
155
|
-
catch (err) {
|
|
156
|
-
throw new ParseError("Error parsing ".concat(stringValue, " as JSON, ").concat(err));
|
|
157
|
-
}
|
|
158
|
-
};
|
|
159
|
-
OpenFeatureSplitProvider.prototype.eventsHandler = function (onSdkReady, onSdkTimedOut) {
|
|
160
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
161
|
-
var onSdkReadyFromCache, clientStatus;
|
|
162
|
-
var _this = this;
|
|
163
|
-
return __generator(this, function (_a) {
|
|
164
|
-
onSdkReadyFromCache = function () {
|
|
165
|
-
_this.events.emit(ProviderEvents.Stale, {
|
|
166
|
-
message: "Split ready from cache",
|
|
167
|
-
});
|
|
168
|
-
};
|
|
169
|
-
clientStatus = this.client.__getStatus();
|
|
170
|
-
if (clientStatus.isReady) {
|
|
171
|
-
onSdkReady();
|
|
172
|
-
}
|
|
173
|
-
else {
|
|
174
|
-
if (clientStatus.isReadyFromCache) {
|
|
175
|
-
onSdkReadyFromCache();
|
|
176
|
-
}
|
|
177
|
-
else {
|
|
178
|
-
this.client.on(this.client.Event.SDK_READY_FROM_CACHE, onSdkReadyFromCache);
|
|
179
|
-
}
|
|
180
|
-
if (clientStatus.hasTimedout) {
|
|
181
|
-
onSdkTimedOut();
|
|
182
|
-
}
|
|
183
|
-
else {
|
|
184
|
-
this.client.on(this.client.Event.SDK_READY_TIMED_OUT, onSdkTimedOut);
|
|
185
|
-
}
|
|
186
|
-
this.client.on(this.client.Event.SDK_READY, onSdkReady);
|
|
187
|
-
}
|
|
188
|
-
this.client.on(this.client.Event.SDK_UPDATE, function () {
|
|
189
|
-
_this.events.emit(ProviderEvents.ConfigurationChanged);
|
|
190
|
-
});
|
|
191
|
-
return [2 /*return*/];
|
|
192
|
-
});
|
|
193
|
-
});
|
|
194
|
-
};
|
|
195
133
|
return OpenFeatureSplitProvider;
|
|
196
134
|
}());
|
|
197
135
|
export { OpenFeatureSplitProvider };
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { ParseError } from "@openfeature/web-sdk";
|
|
2
|
+
export function parseValidNumber(stringValue) {
|
|
3
|
+
if (stringValue === undefined) {
|
|
4
|
+
throw new ParseError("Invalid 'undefined' value.");
|
|
5
|
+
}
|
|
6
|
+
var result = Number.parseFloat(stringValue);
|
|
7
|
+
if (Number.isNaN(result)) {
|
|
8
|
+
throw new ParseError("Invalid numeric value ".concat(stringValue));
|
|
9
|
+
}
|
|
10
|
+
return result;
|
|
11
|
+
}
|
|
12
|
+
export function parseValidJsonObject(stringValue) {
|
|
13
|
+
if (stringValue === undefined) {
|
|
14
|
+
throw new ParseError("Invalid 'undefined' JSON value.");
|
|
15
|
+
}
|
|
16
|
+
try {
|
|
17
|
+
var value = JSON.parse(stringValue);
|
|
18
|
+
if (typeof value !== "object") {
|
|
19
|
+
throw new ParseError("Flag value ".concat(stringValue, " had unexpected type ").concat(typeof value, ", expected \"object\""));
|
|
20
|
+
}
|
|
21
|
+
return value;
|
|
22
|
+
}
|
|
23
|
+
catch (err) {
|
|
24
|
+
throw new ParseError("Error parsing ".concat(stringValue, " as JSON, ").concat(err));
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Map a string treatment to a boolean for boolean flag resolution.
|
|
29
|
+
* @throws ParseError if treatment is not a valid boolean representation
|
|
30
|
+
*/
|
|
31
|
+
export function parseBooleanTreatment(treatment) {
|
|
32
|
+
var lower = treatment.toLowerCase();
|
|
33
|
+
if (lower === 'on' || lower === 'true')
|
|
34
|
+
return true;
|
|
35
|
+
if (lower === 'off' || lower === 'false')
|
|
36
|
+
return false;
|
|
37
|
+
throw new ParseError("Invalid boolean value for ".concat(treatment));
|
|
38
|
+
}
|
package/es/lib/types.js
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.toSplitKey = exports.transformContext = exports.bucketingKeyWarning = void 0;
|
|
4
|
+
var tslib_1 = require("tslib");
|
|
5
|
+
/** Split SDK limit for key values. */
|
|
6
|
+
var KEY_MAX_LENGTH = 250;
|
|
7
|
+
/** Trimmed bucketing key, or the reason it cannot be used. The reason never includes the value. */
|
|
8
|
+
function parseBucketingKey(value) {
|
|
9
|
+
if (value === undefined || value === null)
|
|
10
|
+
return {};
|
|
11
|
+
if (typeof value !== 'string')
|
|
12
|
+
return { reason: 'it is not a string' };
|
|
13
|
+
var bucketingKey = value.trim();
|
|
14
|
+
if (!bucketingKey)
|
|
15
|
+
return { reason: 'it is blank' };
|
|
16
|
+
if (bucketingKey.length > KEY_MAX_LENGTH)
|
|
17
|
+
return { reason: "it is longer than ".concat(KEY_MAX_LENGTH, " characters") };
|
|
18
|
+
return { value: bucketingKey };
|
|
19
|
+
}
|
|
20
|
+
function readBucketingKey(value) {
|
|
21
|
+
return parseBucketingKey(value).value;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Warning for a bucketingKey in the context that will not be used, or undefined when there is nothing to report.
|
|
25
|
+
* Names the reason only, never the value.
|
|
26
|
+
*/
|
|
27
|
+
function bucketingKeyWarning(context) {
|
|
28
|
+
var _a = parseBucketingKey(context.bucketingKey), value = _a.value, reason = _a.reason;
|
|
29
|
+
if (reason) {
|
|
30
|
+
return "bucketingKey ignored because ".concat(reason).concat(context.targetingKey ? '; using the targeting key only' : '', ".");
|
|
31
|
+
}
|
|
32
|
+
if (value && !context.targetingKey)
|
|
33
|
+
return 'bucketingKey ignored because the context has no targetingKey.';
|
|
34
|
+
return undefined;
|
|
35
|
+
}
|
|
36
|
+
exports.bucketingKeyWarning = bucketingKeyWarning;
|
|
37
|
+
/**
|
|
38
|
+
* Transform OpenFeature evaluation context into a consumer object for the Split API.
|
|
39
|
+
* `bucketingKey` is reserved: it is never forwarded as an attribute.
|
|
40
|
+
*/
|
|
41
|
+
function transformContext(context, defaultTrafficType) {
|
|
42
|
+
var targetingKey = context.targetingKey, bucketingKey = context.bucketingKey, ttVal = context.trafficType, attributes = tslib_1.__rest(context, ["targetingKey", "bucketingKey", "trafficType"]);
|
|
43
|
+
var trafficType = ttVal != null && typeof ttVal === 'string' && ttVal.trim() !== ''
|
|
44
|
+
? ttVal
|
|
45
|
+
: defaultTrafficType;
|
|
46
|
+
return {
|
|
47
|
+
targetingKey: targetingKey,
|
|
48
|
+
bucketingKey: readBucketingKey(bucketingKey),
|
|
49
|
+
trafficType: trafficType,
|
|
50
|
+
attributes: JSON.parse(JSON.stringify(attributes)),
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
exports.transformContext = transformContext;
|
|
54
|
+
/** Split key for a consumer: `{ matchingKey, bucketingKey }` or the targeting key alone. */
|
|
55
|
+
function toSplitKey(_a) {
|
|
56
|
+
var targetingKey = _a.targetingKey, bucketingKey = _a.bucketingKey;
|
|
57
|
+
if (!targetingKey)
|
|
58
|
+
return undefined;
|
|
59
|
+
return bucketingKey ? { matchingKey: targetingKey, bucketingKey: bucketingKey } : targetingKey;
|
|
60
|
+
}
|
|
61
|
+
exports.toSplitKey = toSplitKey;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.evaluateTreatment = void 0;
|
|
4
|
+
var web_sdk_1 = require("@openfeature/web-sdk");
|
|
5
|
+
var types_1 = require("./types");
|
|
6
|
+
/**
|
|
7
|
+
* Evaluate a flag with the Split client and return string resolution details.
|
|
8
|
+
*/
|
|
9
|
+
function evaluateTreatment(client, flagKey, consumer) {
|
|
10
|
+
if (flagKey == null || flagKey === '') {
|
|
11
|
+
throw new web_sdk_1.FlagNotFoundError('flagKey must be a non-empty string');
|
|
12
|
+
}
|
|
13
|
+
var treatment = client.getTreatmentWithConfig(flagKey, consumer.attributes);
|
|
14
|
+
var value = treatment.treatment, config = treatment.config;
|
|
15
|
+
if (value === types_1.CONTROL_TREATMENT) {
|
|
16
|
+
throw new web_sdk_1.FlagNotFoundError(types_1.CONTROL_VALUE_ERROR_MESSAGE);
|
|
17
|
+
}
|
|
18
|
+
return {
|
|
19
|
+
value: value,
|
|
20
|
+
variant: value,
|
|
21
|
+
flagMetadata: { config: config ? config : '' },
|
|
22
|
+
reason: web_sdk_1.StandardResolutionReasons.TARGETING_MATCH,
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
exports.evaluateTreatment = evaluateTreatment;
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.attachSplitUpdateHandler = exports.attachSplitReadyHandlers = void 0;
|
|
4
|
+
var tslib_1 = require("tslib");
|
|
5
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
6
|
+
var web_sdk_1 = require("@openfeature/web-sdk");
|
|
7
|
+
/** Map Split SdkReadyMetadata to OpenFeature EventMetadata (string | boolean | number). */
|
|
8
|
+
function toReadyEventMetadata(sdkReadyMetadata) {
|
|
9
|
+
var meta = {
|
|
10
|
+
initialCacheLoad: sdkReadyMetadata.initialCacheLoad,
|
|
11
|
+
};
|
|
12
|
+
if (sdkReadyMetadata.lastUpdateTimestamp != null) {
|
|
13
|
+
meta.lastUpdateTimestamp = sdkReadyMetadata.lastUpdateTimestamp;
|
|
14
|
+
}
|
|
15
|
+
return meta;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Emit OpenFeature ProviderEvents.Ready with Split SDK ready metadata, then invoke the callback.
|
|
19
|
+
* When sdkReadyMetadata is provided (from SDK_READY event), use it; otherwise emit with empty metadata (e.g. when already ready).
|
|
20
|
+
*/
|
|
21
|
+
function emitReadyWithSplitMetadata(eventsEmitter, providerName, onSdkReady, sdkReadyMetadata) {
|
|
22
|
+
var readyDetails = {
|
|
23
|
+
providerName: providerName,
|
|
24
|
+
metadata: sdkReadyMetadata ? toReadyEventMetadata(sdkReadyMetadata) : {},
|
|
25
|
+
};
|
|
26
|
+
eventsEmitter.emit(web_sdk_1.ProviderEvents.Ready, readyDetails);
|
|
27
|
+
onSdkReady();
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Attach Split SDK event listeners and resolve/reject the ready promise based on client status.
|
|
31
|
+
* When the Split SDK becomes ready, emits ProviderEvents.Ready with Split ready metadata before resolving.
|
|
32
|
+
*/
|
|
33
|
+
function attachSplitReadyHandlers(client, eventsEmitter, callbacks, providerName) {
|
|
34
|
+
var onSdkReady = callbacks.onSdkReady, onSdkTimedOut = callbacks.onSdkTimedOut;
|
|
35
|
+
var onSdkReadyFromCache = function () {
|
|
36
|
+
eventsEmitter.emit(web_sdk_1.ProviderEvents.Stale, { message: 'Split ready from cache' });
|
|
37
|
+
};
|
|
38
|
+
var clientStatus = client.__getStatus();
|
|
39
|
+
if (clientStatus.isReady) {
|
|
40
|
+
emitReadyWithSplitMetadata(eventsEmitter, providerName, onSdkReady);
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
if (clientStatus.isReadyFromCache) {
|
|
44
|
+
onSdkReadyFromCache();
|
|
45
|
+
}
|
|
46
|
+
else {
|
|
47
|
+
client.on(client.Event.SDK_READY_FROM_CACHE, onSdkReadyFromCache);
|
|
48
|
+
}
|
|
49
|
+
if (clientStatus.hasTimedout) {
|
|
50
|
+
onSdkTimedOut();
|
|
51
|
+
}
|
|
52
|
+
else {
|
|
53
|
+
client.on(client.Event.SDK_READY_TIMED_OUT, onSdkTimedOut);
|
|
54
|
+
}
|
|
55
|
+
client.on(client.Event.SDK_READY, function (sdkReadyMetadata) {
|
|
56
|
+
emitReadyWithSplitMetadata(eventsEmitter, providerName, onSdkReady, sdkReadyMetadata);
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
exports.attachSplitReadyHandlers = attachSplitReadyHandlers;
|
|
60
|
+
/**
|
|
61
|
+
* Subscribe to Split SDK_UPDATE and emit OpenFeature ConfigurationChanged with details.
|
|
62
|
+
*/
|
|
63
|
+
function attachSplitUpdateHandler(client, eventsEmitter, providerName) {
|
|
64
|
+
client.on(client.Event.SDK_UPDATE, function (updateMetadata) {
|
|
65
|
+
var eventDetails = { providerName: providerName };
|
|
66
|
+
if (updateMetadata) {
|
|
67
|
+
eventDetails = tslib_1.__assign(tslib_1.__assign({}, eventDetails), { metadata: {
|
|
68
|
+
type: updateMetadata.type,
|
|
69
|
+
}, flagsChanged: updateMetadata.names || [] });
|
|
70
|
+
}
|
|
71
|
+
eventsEmitter.emit(web_sdk_1.ProviderEvents.ConfigurationChanged, eventDetails);
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
exports.attachSplitUpdateHandler = attachSplitUpdateHandler;
|