@splitsoftware/openfeature-js-split-provider 1.2.0 → 1.4.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 +14 -0
- package/README.md +37 -3
- package/es/lib/client-resolver.js +17 -0
- package/es/lib/context.js +28 -0
- package/es/lib/js-split-provider.js +66 -103
- package/es/lib/parsers.js +26 -0
- package/es/lib/readiness.js +64 -0
- package/es/lib/types.js +4 -0
- package/es/package.json +1 -0
- package/lib/index.js +1 -1
- package/lib/lib/client-resolver.js +21 -0
- package/lib/lib/context.js +32 -0
- package/lib/lib/js-split-provider.js +68 -105
- package/lib/lib/parsers.js +31 -0
- package/lib/lib/readiness.js +70 -0
- package/lib/lib/types.js +7 -0
- package/package.json +12 -7
- package/src/__tests__/nodeSuites/client.spec.js +17 -2
- package/src/__tests__/nodeSuites/client_redis.spec.js +9 -10
- package/src/__tests__/nodeSuites/provider.spec.js +191 -0
- package/src/index.ts +1 -1
- package/src/lib/__tests__/client-resolver.spec.ts +64 -0
- package/src/lib/__tests__/context.spec.ts +114 -0
- package/src/lib/__tests__/parsers.spec.ts +63 -0
- package/src/lib/__tests__/readiness.spec.ts +159 -0
- package/src/lib/client-resolver.ts +22 -0
- package/src/lib/context.ts +35 -0
- package/src/lib/js-split-provider.ts +120 -144
- package/src/lib/parsers.ts +32 -0
- package/src/lib/readiness.ts +103 -0
- package/src/lib/types.ts +41 -0
- package/types/index.d.ts +1 -1
- package/types/lib/client-resolver.d.ts +7 -0
- package/types/lib/context.d.ts +8 -0
- package/types/lib/js-split-provider.d.ts +16 -18
- package/types/lib/parsers.d.ts +3 -0
- package/types/lib/readiness.d.ts +16 -0
- package/types/lib/types.d.ts +32 -0
|
@@ -2,38 +2,46 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.OpenFeatureSplitProvider = void 0;
|
|
4
4
|
const server_sdk_1 = require("@openfeature/server-sdk");
|
|
5
|
-
const
|
|
6
|
-
const
|
|
7
|
-
const
|
|
5
|
+
const client_resolver_js_1 = require("./client-resolver.js");
|
|
6
|
+
const context_js_1 = require("./context.js");
|
|
7
|
+
const parsers_js_1 = require("./parsers.js");
|
|
8
|
+
const readiness_js_1 = require("./readiness.js");
|
|
9
|
+
const types_js_1 = require("./types.js");
|
|
8
10
|
class OpenFeatureSplitProvider {
|
|
9
|
-
getSplitClient(options) {
|
|
10
|
-
if (typeof (options) === 'string') {
|
|
11
|
-
const splitFactory = (0, splitio_1.SplitFactory)({ core: { authorizationKey: options } });
|
|
12
|
-
return splitFactory.client();
|
|
13
|
-
}
|
|
14
|
-
let splitClient;
|
|
15
|
-
try {
|
|
16
|
-
splitClient = options.client();
|
|
17
|
-
}
|
|
18
|
-
catch {
|
|
19
|
-
splitClient = options.splitClient;
|
|
20
|
-
}
|
|
21
|
-
return splitClient;
|
|
22
|
-
}
|
|
23
11
|
constructor(options) {
|
|
24
12
|
this.metadata = {
|
|
25
|
-
name:
|
|
13
|
+
name: types_js_1.PROVIDER_NAME,
|
|
26
14
|
};
|
|
15
|
+
this.runsOn = 'server';
|
|
16
|
+
// Invalid-bucketingKey reasons already warned about; repeats are logged at debug.
|
|
17
|
+
this.warnedBucketingKeyIssues = new Set();
|
|
27
18
|
this.events = new server_sdk_1.OpenFeatureEventEmitter();
|
|
28
|
-
|
|
29
|
-
this.
|
|
30
|
-
this.client
|
|
31
|
-
this.client.on(this.client.Event.SDK_UPDATE, () => {
|
|
32
|
-
|
|
19
|
+
this.trafficType = types_js_1.DEFAULT_TRAFFIC_TYPE;
|
|
20
|
+
this.client = (0, client_resolver_js_1.getSplitClient)(options);
|
|
21
|
+
(0, readiness_js_1.attachReadyEventHandlers)(this.client, this.events, this.metadata.name);
|
|
22
|
+
this.client.on(this.client.Event.SDK_UPDATE, (updateMetadata) => {
|
|
23
|
+
const eventDetails = {
|
|
24
|
+
providerName: this.metadata.name,
|
|
25
|
+
...(updateMetadata
|
|
26
|
+
? {
|
|
27
|
+
metadata: { type: updateMetadata.type },
|
|
28
|
+
flagsChanged: updateMetadata.names || [],
|
|
29
|
+
}
|
|
30
|
+
: {}),
|
|
31
|
+
};
|
|
32
|
+
this.events.emit(server_sdk_1.ProviderEvents.ConfigurationChanged, eventDetails);
|
|
33
33
|
});
|
|
34
34
|
}
|
|
35
|
-
|
|
36
|
-
|
|
35
|
+
/**
|
|
36
|
+
* Called by the SDK after the provider is set. Waits for the Split client to be ready.
|
|
37
|
+
* When this promise resolves, the SDK emits ProviderEvents.Ready.
|
|
38
|
+
*/
|
|
39
|
+
async initialize(_context) {
|
|
40
|
+
void _context;
|
|
41
|
+
await (0, readiness_js_1.waitUntilReady)(this.client, this.events, this.metadata.name);
|
|
42
|
+
}
|
|
43
|
+
async resolveBooleanEvaluation(flagKey, _, context, logger) {
|
|
44
|
+
const details = await this.evaluateTreatment(flagKey, (0, context_js_1.transformContext)(context, this.trafficType), logger);
|
|
37
45
|
const treatment = details.value.toLowerCase();
|
|
38
46
|
if (treatment === 'on' || treatment === 'true') {
|
|
39
47
|
return { ...details, value: true };
|
|
@@ -43,49 +51,58 @@ class OpenFeatureSplitProvider {
|
|
|
43
51
|
}
|
|
44
52
|
throw new server_sdk_1.ParseError(`Invalid boolean value for ${treatment}`);
|
|
45
53
|
}
|
|
46
|
-
async resolveStringEvaluation(flagKey, _, context) {
|
|
47
|
-
|
|
48
|
-
return details;
|
|
54
|
+
async resolveStringEvaluation(flagKey, _, context, logger) {
|
|
55
|
+
return this.evaluateTreatment(flagKey, (0, context_js_1.transformContext)(context, this.trafficType), logger);
|
|
49
56
|
}
|
|
50
|
-
async resolveNumberEvaluation(flagKey, _, context) {
|
|
51
|
-
const details = await this.evaluateTreatment(flagKey,
|
|
52
|
-
return { ...details, value:
|
|
57
|
+
async resolveNumberEvaluation(flagKey, _, context, logger) {
|
|
58
|
+
const details = await this.evaluateTreatment(flagKey, (0, context_js_1.transformContext)(context, this.trafficType), logger);
|
|
59
|
+
return { ...details, value: (0, parsers_js_1.parseValidNumber)(details.value) };
|
|
53
60
|
}
|
|
54
|
-
async resolveObjectEvaluation(flagKey, _, context) {
|
|
55
|
-
const details = await this.evaluateTreatment(flagKey,
|
|
56
|
-
return { ...details, value:
|
|
61
|
+
async resolveObjectEvaluation(flagKey, _, context, logger) {
|
|
62
|
+
const details = await this.evaluateTreatment(flagKey, (0, context_js_1.transformContext)(context, this.trafficType), logger);
|
|
63
|
+
return { ...details, value: (0, parsers_js_1.parseValidJsonObject)(details.value) };
|
|
57
64
|
}
|
|
58
|
-
async evaluateTreatment(flagKey, consumer) {
|
|
65
|
+
async evaluateTreatment(flagKey, consumer, logger) {
|
|
59
66
|
if (!consumer.targetingKey) {
|
|
60
67
|
throw new server_sdk_1.TargetingKeyMissingError('The Split provider requires a targeting key.');
|
|
61
68
|
}
|
|
62
69
|
if (flagKey == null || flagKey === '') {
|
|
63
70
|
throw new server_sdk_1.FlagNotFoundError('flagKey must be a non-empty string');
|
|
64
71
|
}
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
72
|
+
if (consumer.bucketingKeyIssue) {
|
|
73
|
+
const message = `Split provider: ignoring bucketingKey (${consumer.bucketingKeyIssue}); evaluating with the targeting key only.`;
|
|
74
|
+
if (this.warnedBucketingKeyIssues.has(consumer.bucketingKeyIssue)) {
|
|
75
|
+
logger?.debug(message);
|
|
76
|
+
}
|
|
77
|
+
else {
|
|
78
|
+
this.warnedBucketingKeyIssues.add(consumer.bucketingKeyIssue);
|
|
79
|
+
logger?.warn(message);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
const splitKey = consumer.bucketingKey
|
|
83
|
+
? { matchingKey: consumer.targetingKey, bucketingKey: consumer.bucketingKey }
|
|
84
|
+
: consumer.targetingKey;
|
|
85
|
+
await (0, readiness_js_1.waitUntilReady)(this.client, this.events, this.metadata.name);
|
|
86
|
+
const { treatment: value, config } = await this.client.getTreatmentWithConfig(splitKey, flagKey, consumer.attributes);
|
|
87
|
+
if (value === types_js_1.CONTROL_TREATMENT) {
|
|
88
|
+
throw new server_sdk_1.FlagNotFoundError(types_js_1.CONTROL_VALUE_ERROR_MESSAGE);
|
|
71
89
|
}
|
|
72
|
-
const flagMetadata = { config: config
|
|
73
|
-
|
|
74
|
-
value
|
|
90
|
+
const flagMetadata = Object.freeze({ config: config ?? '' });
|
|
91
|
+
return {
|
|
92
|
+
value,
|
|
75
93
|
variant: value,
|
|
76
|
-
flagMetadata
|
|
94
|
+
flagMetadata,
|
|
77
95
|
reason: server_sdk_1.StandardResolutionReasons.TARGETING_MATCH,
|
|
78
96
|
};
|
|
79
|
-
return details;
|
|
80
97
|
}
|
|
81
98
|
async track(trackingEventName, context, details) {
|
|
82
|
-
|
|
83
|
-
if (trackingEventName == null || trackingEventName === '')
|
|
99
|
+
if (trackingEventName == null || trackingEventName === '') {
|
|
84
100
|
throw new server_sdk_1.ParseError('Missing eventName, required to track');
|
|
85
|
-
|
|
86
|
-
const { targetingKey, trafficType } =
|
|
87
|
-
if (targetingKey == null || targetingKey === '')
|
|
101
|
+
}
|
|
102
|
+
const { targetingKey, trafficType } = (0, context_js_1.transformContext)(context, this.trafficType);
|
|
103
|
+
if (targetingKey == null || targetingKey === '') {
|
|
88
104
|
throw new server_sdk_1.TargetingKeyMissingError('Missing targetingKey, required to track');
|
|
105
|
+
}
|
|
89
106
|
let value;
|
|
90
107
|
let properties = {};
|
|
91
108
|
if (details != null) {
|
|
@@ -101,59 +118,5 @@ class OpenFeatureSplitProvider {
|
|
|
101
118
|
async onClose() {
|
|
102
119
|
return this.client.destroy();
|
|
103
120
|
}
|
|
104
|
-
//Transform the context into an object useful for the Split API, an key string with arbitrary Split 'Attributes'.
|
|
105
|
-
transformContext(context) {
|
|
106
|
-
const { targetingKey, trafficType: ttVal, ...attributes } = context;
|
|
107
|
-
const trafficType = ttVal != null && typeof ttVal === 'string' && ttVal.trim() !== ''
|
|
108
|
-
? ttVal
|
|
109
|
-
: this.trafficType;
|
|
110
|
-
return {
|
|
111
|
-
targetingKey,
|
|
112
|
-
trafficType,
|
|
113
|
-
// Stringify context objects include date.
|
|
114
|
-
attributes: JSON.parse(JSON.stringify(attributes)),
|
|
115
|
-
};
|
|
116
|
-
}
|
|
117
|
-
parseValidNumber(stringValue) {
|
|
118
|
-
if (stringValue === undefined) {
|
|
119
|
-
throw new server_sdk_1.ParseError(`Invalid 'undefined' value.`);
|
|
120
|
-
}
|
|
121
|
-
const result = Number.parseFloat(stringValue);
|
|
122
|
-
if (Number.isNaN(result)) {
|
|
123
|
-
throw new server_sdk_1.ParseError(`Invalid numeric value ${stringValue}`);
|
|
124
|
-
}
|
|
125
|
-
return result;
|
|
126
|
-
}
|
|
127
|
-
parseValidJsonObject(stringValue) {
|
|
128
|
-
if (stringValue === undefined) {
|
|
129
|
-
throw new server_sdk_1.ParseError(`Invalid 'undefined' JSON value.`);
|
|
130
|
-
}
|
|
131
|
-
// we may want to allow the parsing to be customized.
|
|
132
|
-
try {
|
|
133
|
-
const value = JSON.parse(stringValue);
|
|
134
|
-
if (typeof value !== 'object') {
|
|
135
|
-
throw new server_sdk_1.ParseError(`Flag value ${stringValue} had unexpected type ${typeof value}, expected "object"`);
|
|
136
|
-
}
|
|
137
|
-
return value;
|
|
138
|
-
}
|
|
139
|
-
catch (err) {
|
|
140
|
-
throw new server_sdk_1.ParseError(`Error parsing ${stringValue} as JSON, ${err}`);
|
|
141
|
-
}
|
|
142
|
-
}
|
|
143
|
-
async readinessHandler(onSdkReady, onSdkTimedOut) {
|
|
144
|
-
const clientStatus = this.client.getStatus();
|
|
145
|
-
if (clientStatus.isReady) {
|
|
146
|
-
onSdkReady();
|
|
147
|
-
}
|
|
148
|
-
else {
|
|
149
|
-
if (clientStatus.hasTimedout) {
|
|
150
|
-
onSdkTimedOut();
|
|
151
|
-
}
|
|
152
|
-
else {
|
|
153
|
-
this.client.on(this.client.Event.SDK_READY_TIMED_OUT, onSdkTimedOut);
|
|
154
|
-
}
|
|
155
|
-
this.client.on(this.client.Event.SDK_READY, onSdkReady);
|
|
156
|
-
}
|
|
157
|
-
}
|
|
158
121
|
}
|
|
159
122
|
exports.OpenFeatureSplitProvider = OpenFeatureSplitProvider;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.parseValidJsonObject = exports.parseValidNumber = void 0;
|
|
4
|
+
const server_sdk_1 = require("@openfeature/server-sdk");
|
|
5
|
+
function parseValidNumber(stringValue) {
|
|
6
|
+
if (stringValue === undefined) {
|
|
7
|
+
throw new server_sdk_1.ParseError(`Invalid 'undefined' value.`);
|
|
8
|
+
}
|
|
9
|
+
const result = Number.parseFloat(stringValue);
|
|
10
|
+
if (Number.isNaN(result)) {
|
|
11
|
+
throw new server_sdk_1.ParseError(`Invalid numeric value ${stringValue}`);
|
|
12
|
+
}
|
|
13
|
+
return result;
|
|
14
|
+
}
|
|
15
|
+
exports.parseValidNumber = parseValidNumber;
|
|
16
|
+
function parseValidJsonObject(stringValue) {
|
|
17
|
+
if (stringValue === undefined) {
|
|
18
|
+
throw new server_sdk_1.ParseError(`Invalid 'undefined' JSON value.`);
|
|
19
|
+
}
|
|
20
|
+
try {
|
|
21
|
+
const value = JSON.parse(stringValue);
|
|
22
|
+
if (typeof value !== 'object') {
|
|
23
|
+
throw new server_sdk_1.ParseError(`Flag value ${stringValue} had unexpected type ${typeof value}, expected "object"`);
|
|
24
|
+
}
|
|
25
|
+
return value;
|
|
26
|
+
}
|
|
27
|
+
catch (err) {
|
|
28
|
+
throw new server_sdk_1.ParseError(`Error parsing ${stringValue} as JSON, ${err}`);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
exports.parseValidJsonObject = parseValidJsonObject;
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.emitReadyEvent = exports.waitUntilReady = exports.attachReadyEventHandlers = void 0;
|
|
4
|
+
const server_sdk_1 = require("@openfeature/server-sdk");
|
|
5
|
+
const types_js_1 = require("./types.js");
|
|
6
|
+
/**
|
|
7
|
+
* Builds OpenFeature Ready event details including Split SDK ready metadata.
|
|
8
|
+
* Handles Split SDK not passing metadata (e.g. in consumer/Redis mode).
|
|
9
|
+
*/
|
|
10
|
+
function buildReadyEventDetails(providerName, splitMetadata, readyFromCache) {
|
|
11
|
+
const metadata = {
|
|
12
|
+
readyFromCache,
|
|
13
|
+
initialCacheLoad: splitMetadata?.initialCacheLoad ?? false,
|
|
14
|
+
};
|
|
15
|
+
if (splitMetadata?.lastUpdateTimestamp != null) {
|
|
16
|
+
metadata.lastUpdateTimestamp = splitMetadata.lastUpdateTimestamp;
|
|
17
|
+
}
|
|
18
|
+
return {
|
|
19
|
+
providerName: providerName || types_js_1.PROVIDER_NAME,
|
|
20
|
+
metadata,
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Registers Split SDK_READY and SDK_READY_FROM_CACHE listeners and forwards them
|
|
25
|
+
* as OpenFeature ProviderEvents.Ready with event metadata (initialCacheLoad, lastUpdateTimestamp, readyFromCache).
|
|
26
|
+
* If the client is already ready when attaching (e.g. localhost or reused client), emits Ready once
|
|
27
|
+
* with best-effort metadata so handlers always receive at least one Ready when the client is ready.
|
|
28
|
+
*/
|
|
29
|
+
function attachReadyEventHandlers(client, events, providerName = types_js_1.PROVIDER_NAME) {
|
|
30
|
+
client.on(client.Event.SDK_READY_FROM_CACHE, (splitMetadata) => {
|
|
31
|
+
events.emit(server_sdk_1.ProviderEvents.Ready, buildReadyEventDetails(providerName, splitMetadata, true));
|
|
32
|
+
});
|
|
33
|
+
client.on(client.Event.SDK_READY, (splitMetadata) => {
|
|
34
|
+
events.emit(server_sdk_1.ProviderEvents.Ready, buildReadyEventDetails(providerName, splitMetadata, false));
|
|
35
|
+
});
|
|
36
|
+
const status = client.getStatus();
|
|
37
|
+
if (status.isReady) {
|
|
38
|
+
events.emit(server_sdk_1.ProviderEvents.Ready, buildReadyEventDetails(providerName, undefined, false));
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
exports.attachReadyEventHandlers = attachReadyEventHandlers;
|
|
42
|
+
/**
|
|
43
|
+
* Returns a promise that resolves when the Split client is ready (SDK_READY),
|
|
44
|
+
* or rejects if the client has timed out (SDK_READY_TIMED_OUT).
|
|
45
|
+
* Used to gate evaluations until the SDK has synchronized with the backend.
|
|
46
|
+
*/
|
|
47
|
+
function waitUntilReady(client, events, providerName = types_js_1.PROVIDER_NAME) {
|
|
48
|
+
return new Promise((resolve, reject) => {
|
|
49
|
+
const status = client.getStatus();
|
|
50
|
+
if (status.isReady) {
|
|
51
|
+
emitReadyEvent(client, events, providerName);
|
|
52
|
+
resolve();
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
if (status.hasTimedout) {
|
|
56
|
+
reject();
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
client.on(client.Event.SDK_READY_TIMED_OUT, reject);
|
|
60
|
+
client.on(client.Event.SDK_READY, () => {
|
|
61
|
+
emitReadyEvent(client, events, providerName);
|
|
62
|
+
resolve();
|
|
63
|
+
});
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
exports.waitUntilReady = waitUntilReady;
|
|
67
|
+
function emitReadyEvent(client, events, providerName = types_js_1.PROVIDER_NAME) {
|
|
68
|
+
events.emit(server_sdk_1.ProviderEvents.Ready, buildReadyEventDetails(providerName, undefined, false));
|
|
69
|
+
}
|
|
70
|
+
exports.emitReadyEvent = emitReadyEvent;
|
package/lib/lib/types.js
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.PROVIDER_NAME = exports.DEFAULT_TRAFFIC_TYPE = exports.CONTROL_VALUE_ERROR_MESSAGE = exports.CONTROL_TREATMENT = void 0;
|
|
4
|
+
exports.CONTROL_TREATMENT = 'control';
|
|
5
|
+
exports.CONTROL_VALUE_ERROR_MESSAGE = 'Received the "control" value from Split.';
|
|
6
|
+
exports.DEFAULT_TRAFFIC_TYPE = 'user';
|
|
7
|
+
exports.PROVIDER_NAME = 'split';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@splitsoftware/openfeature-js-split-provider",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.4.0",
|
|
4
4
|
"description": "Split OpenFeature Provider",
|
|
5
5
|
"files": [
|
|
6
6
|
"README.md",
|
|
@@ -12,9 +12,9 @@
|
|
|
12
12
|
"es",
|
|
13
13
|
"src"
|
|
14
14
|
],
|
|
15
|
-
"repository": "splitio/openfeature-
|
|
16
|
-
"homepage": "https://github.com/splitio/openfeature-
|
|
17
|
-
"bugs": "https://github.com/splitio/openfeature-
|
|
15
|
+
"repository": "splitio/split-openfeature-provider-js",
|
|
16
|
+
"homepage": "https://github.com/splitio/split-openfeature-provider-js#readme",
|
|
17
|
+
"bugs": "https://github.com/splitio/split-openfeature-provider-js/issues",
|
|
18
18
|
"license": "Apache-2.0",
|
|
19
19
|
"author": "Josh Sirota <josh.sirota@split.io>",
|
|
20
20
|
"contributors": [
|
|
@@ -30,18 +30,22 @@
|
|
|
30
30
|
},
|
|
31
31
|
"exports": {
|
|
32
32
|
".": {
|
|
33
|
+
"types": "./types/index.d.ts",
|
|
33
34
|
"require": "./lib/index.js",
|
|
34
35
|
"import": "./es/index.js"
|
|
35
36
|
}
|
|
36
37
|
},
|
|
38
|
+
"dependencies": {
|
|
39
|
+
"@splitsoftware/splitio": "^11.10.0",
|
|
40
|
+
"tslib": "^2.3.1"
|
|
41
|
+
},
|
|
37
42
|
"peerDependencies": {
|
|
38
|
-
"@openfeature/server-sdk": "^1.20.0"
|
|
39
|
-
"@splitsoftware/splitio": "^11.8.0"
|
|
43
|
+
"@openfeature/server-sdk": "^1.20.0"
|
|
40
44
|
},
|
|
41
45
|
"devDependencies": {
|
|
42
46
|
"@eslint/js": "^9.35.0",
|
|
43
47
|
"@openfeature/server-sdk": "^1.20.0",
|
|
44
|
-
"@splitsoftware/splitio": "^11.
|
|
48
|
+
"@splitsoftware/splitio": "^11.10.0",
|
|
45
49
|
"@types/jest": "^30.0.0",
|
|
46
50
|
"@types/node": "^24.3.1",
|
|
47
51
|
"copyfiles": "^2.4.1",
|
|
@@ -65,6 +69,7 @@
|
|
|
65
69
|
"build-cjs": "rimraf lib && tsc -outDir lib -m CommonJS",
|
|
66
70
|
"postbuild-cjs": "cross-env NODE_ENV=cjs node scripts/copy.packages.json.js && ./scripts/build_cjs_replace_imports.sh",
|
|
67
71
|
"build": "rimraf lib es && npm run build-cjs && npm run build-esm",
|
|
72
|
+
"postbuild": "node scripts/check_module_imports.js",
|
|
68
73
|
"check": "npm run check:lint",
|
|
69
74
|
"check:lint": "eslint src",
|
|
70
75
|
"test": "cross-env NODE_ENV=test jest",
|
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
/* eslint-disable jest/no-conditional-expect */
|
|
2
|
+
import { OpenFeature, ProviderEvents } from '@openfeature/server-sdk';
|
|
2
3
|
import { OpenFeatureSplitProvider } from '../../lib/js-split-provider';
|
|
3
4
|
import { getLocalHostSplitClient, getSplitFactory } from '../testUtils';
|
|
4
5
|
|
|
5
|
-
import { OpenFeature } from '@openfeature/server-sdk';
|
|
6
|
-
|
|
7
6
|
const cases = [
|
|
8
7
|
[
|
|
9
8
|
'openfeature client tests mode: splitClient',
|
|
@@ -119,6 +118,22 @@ describe.each(cases)('%s', (label, getOptions) => {
|
|
|
119
118
|
expect(client.metadata.name).toBe('test');
|
|
120
119
|
});
|
|
121
120
|
|
|
121
|
+
test('Ready event includes Split metadata (readyFromCache, initialCacheLoad)', async () => {
|
|
122
|
+
const readyDetails = [];
|
|
123
|
+
const testProvider = new OpenFeatureSplitProvider(options);
|
|
124
|
+
testProvider.events.addHandler(ProviderEvents.Ready, (details) => readyDetails.push(details));
|
|
125
|
+
await OpenFeature.setProviderAndWait(testProvider);
|
|
126
|
+
expect(readyDetails.length).toBeGreaterThanOrEqual(1);
|
|
127
|
+
const withMetadata = readyDetails.find((d) => d && d.metadata);
|
|
128
|
+
expect(withMetadata).toBeDefined();
|
|
129
|
+
expect(withMetadata.providerName).toBe('split');
|
|
130
|
+
expect(typeof withMetadata.metadata.readyFromCache).toBe('boolean');
|
|
131
|
+
expect(typeof withMetadata.metadata.initialCacheLoad).toBe('boolean');
|
|
132
|
+
if (withMetadata.metadata.lastUpdateTimestamp != null) {
|
|
133
|
+
expect(typeof withMetadata.metadata.lastUpdateTimestamp).toBe('number');
|
|
134
|
+
}
|
|
135
|
+
});
|
|
136
|
+
|
|
122
137
|
test('evaluate Boolean without details test', async () => {
|
|
123
138
|
let details = await client.getBooleanDetails('some_other_feature', true);
|
|
124
139
|
expect(details.flagKey).toBe('some_other_feature');
|
|
@@ -32,25 +32,24 @@ const startRedis = () => {
|
|
|
32
32
|
return promise;
|
|
33
33
|
};
|
|
34
34
|
|
|
35
|
-
let redisServer
|
|
36
|
-
let splitClient
|
|
35
|
+
let redisServer;
|
|
36
|
+
let splitClient;
|
|
37
|
+
let client;
|
|
37
38
|
|
|
38
39
|
beforeAll(async () => {
|
|
39
40
|
redisServer = await startRedis();
|
|
41
|
+
splitClient = getRedisSplitClient(redisPort);
|
|
42
|
+
const provider = new OpenFeatureSplitProvider({ splitClient });
|
|
43
|
+
await OpenFeature.setProviderAndWait(provider);
|
|
44
|
+
client = OpenFeature.getClient();
|
|
40
45
|
}, 30000);
|
|
41
46
|
|
|
42
47
|
afterAll(async () => {
|
|
43
|
-
await redisServer.close();
|
|
44
|
-
await splitClient.destroy();
|
|
48
|
+
if (redisServer) await redisServer.close();
|
|
49
|
+
if (splitClient && typeof splitClient.destroy === 'function') await splitClient.destroy();
|
|
45
50
|
});
|
|
46
51
|
|
|
47
52
|
describe('Regular usage - DEBUG strategy', () => {
|
|
48
|
-
splitClient = getRedisSplitClient(redisPort);
|
|
49
|
-
const provider = new OpenFeatureSplitProvider({ splitClient });
|
|
50
|
-
|
|
51
|
-
OpenFeature.setProviderAndWait(provider);
|
|
52
|
-
const client = OpenFeature.getClient();
|
|
53
|
-
|
|
54
53
|
test('Evaluate always on flag', async () => {
|
|
55
54
|
await client.getBooleanValue('always-on', false, {targetingKey: 'emma-ss'}).then(result => {
|
|
56
55
|
expect(result).toBe(true);
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
/* eslint-disable jest/no-conditional-expect */
|
|
2
|
+
import { ProviderEvents } from '@openfeature/server-sdk';
|
|
2
3
|
import { getLocalHostSplitClient, getSplitFactory } from '../testUtils';
|
|
3
4
|
import { OpenFeatureSplitProvider } from '../../lib/js-split-provider';
|
|
4
5
|
|
|
@@ -239,3 +240,193 @@ describe.each(cases)('%s', (label, getOptions) => {
|
|
|
239
240
|
expect(trackSpy).toHaveBeenCalledWith('u1', 'user', 'purchase', 9.99, { plan: 'pro', beta: true });
|
|
240
241
|
});
|
|
241
242
|
});
|
|
243
|
+
|
|
244
|
+
describe('provider events metadata', () => {
|
|
245
|
+
const SDK_UPDATE = 'state::update';
|
|
246
|
+
|
|
247
|
+
function createMockSplitClient() {
|
|
248
|
+
const listeners = {};
|
|
249
|
+
const mock = {
|
|
250
|
+
Event: { SDK_UPDATE },
|
|
251
|
+
getStatus: jest.fn().mockReturnValue({ isReady: true, hasTimedout: false }),
|
|
252
|
+
getTreatmentWithConfig: jest.fn().mockResolvedValue({ treatment: 'on', config: '' }),
|
|
253
|
+
on: jest.fn((event, cb) => {
|
|
254
|
+
listeners[event] = listeners[event] || [];
|
|
255
|
+
listeners[event].push(cb);
|
|
256
|
+
return mock;
|
|
257
|
+
}),
|
|
258
|
+
track: jest.fn(),
|
|
259
|
+
destroy: jest.fn(),
|
|
260
|
+
_emit(event, payload) {
|
|
261
|
+
(listeners[event] || []).forEach((cb) => cb(payload));
|
|
262
|
+
},
|
|
263
|
+
};
|
|
264
|
+
return mock;
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
test('ConfigurationChanged event includes metadata (type) and flagsChanged when FLAGS_UPDATE', async () => {
|
|
268
|
+
const mockClient = createMockSplitClient();
|
|
269
|
+
const provider = new OpenFeatureSplitProvider({ splitClient: mockClient });
|
|
270
|
+
const configChangedDetails = [];
|
|
271
|
+
provider.events.addHandler(ProviderEvents.ConfigurationChanged, (details) => configChangedDetails.push(details));
|
|
272
|
+
|
|
273
|
+
mockClient._emit(SDK_UPDATE, { type: 'FLAGS_UPDATE', names: ['flag1', 'flag2'] });
|
|
274
|
+
|
|
275
|
+
expect(configChangedDetails.length).toBe(1);
|
|
276
|
+
expect(configChangedDetails[0].providerName).toBe('split');
|
|
277
|
+
expect(configChangedDetails[0].metadata).toEqual({ type: 'FLAGS_UPDATE' });
|
|
278
|
+
expect(configChangedDetails[0].flagsChanged).toEqual(['flag1', 'flag2']);
|
|
279
|
+
|
|
280
|
+
await provider.onClose();
|
|
281
|
+
});
|
|
282
|
+
|
|
283
|
+
test('ConfigurationChanged event includes metadata without flagsChanged when SEGMENTS_UPDATE', async () => {
|
|
284
|
+
const mockClient = createMockSplitClient();
|
|
285
|
+
const provider = new OpenFeatureSplitProvider({ splitClient: mockClient });
|
|
286
|
+
const configChangedDetails = [];
|
|
287
|
+
provider.events.addHandler(ProviderEvents.ConfigurationChanged, (details) => configChangedDetails.push(details));
|
|
288
|
+
|
|
289
|
+
mockClient._emit(SDK_UPDATE, { type: 'SEGMENTS_UPDATE' });
|
|
290
|
+
|
|
291
|
+
expect(configChangedDetails.length).toBe(1);
|
|
292
|
+
expect(configChangedDetails[0].providerName).toBe('split');
|
|
293
|
+
expect(configChangedDetails[0].metadata).toEqual({ type: 'SEGMENTS_UPDATE' });
|
|
294
|
+
expect(configChangedDetails[0].flagsChanged).toEqual([]);
|
|
295
|
+
|
|
296
|
+
await provider.onClose();
|
|
297
|
+
});
|
|
298
|
+
|
|
299
|
+
test('ConfigurationChanged event includes only providerName when SDK_UPDATE payload is undefined', async () => {
|
|
300
|
+
const mockClient = createMockSplitClient();
|
|
301
|
+
const provider = new OpenFeatureSplitProvider({ splitClient: mockClient });
|
|
302
|
+
const configChangedDetails = [];
|
|
303
|
+
provider.events.addHandler(ProviderEvents.ConfigurationChanged, (details) => configChangedDetails.push(details));
|
|
304
|
+
|
|
305
|
+
mockClient._emit(SDK_UPDATE, undefined);
|
|
306
|
+
|
|
307
|
+
expect(configChangedDetails.length).toBe(1);
|
|
308
|
+
expect(configChangedDetails[0].providerName).toBe('split');
|
|
309
|
+
expect(configChangedDetails[0].metadata).toBeUndefined();
|
|
310
|
+
expect(configChangedDetails[0].flagsChanged).toBeUndefined();
|
|
311
|
+
|
|
312
|
+
await provider.onClose();
|
|
313
|
+
});
|
|
314
|
+
});
|
|
315
|
+
|
|
316
|
+
describe('bucketingKey', () => {
|
|
317
|
+
function createMockClient(treatment = 'on') {
|
|
318
|
+
return {
|
|
319
|
+
Event: { SDK_UPDATE: 'state::update' },
|
|
320
|
+
getStatus: jest.fn().mockReturnValue({ isReady: true, hasTimedout: false }),
|
|
321
|
+
getTreatmentWithConfig: jest.fn().mockResolvedValue({ treatment, config: '' }),
|
|
322
|
+
on: jest.fn(),
|
|
323
|
+
track: jest.fn(),
|
|
324
|
+
destroy: jest.fn(),
|
|
325
|
+
};
|
|
326
|
+
}
|
|
327
|
+
const createLogger = () => ({ debug: jest.fn(), info: jest.fn(), warn: jest.fn(), error: jest.fn() });
|
|
328
|
+
|
|
329
|
+
let client;
|
|
330
|
+
let provider;
|
|
331
|
+
let logger;
|
|
332
|
+
beforeEach(() => {
|
|
333
|
+
client = createMockClient();
|
|
334
|
+
provider = new OpenFeatureSplitProvider({ splitClient: client });
|
|
335
|
+
logger = createLogger();
|
|
336
|
+
});
|
|
337
|
+
afterEach(() => provider.onClose());
|
|
338
|
+
|
|
339
|
+
test('valid bucketingKey → evaluates with { matchingKey, bucketingKey }', async () => {
|
|
340
|
+
const result = await provider.resolveBooleanEvaluation('f', false, { targetingKey: 'u1', bucketingKey: 'b1', plan: 'pro' }, logger);
|
|
341
|
+
expect(result.value).toBe(true);
|
|
342
|
+
expect(client.getTreatmentWithConfig).toHaveBeenCalledWith({ matchingKey: 'u1', bucketingKey: 'b1' }, 'f', { plan: 'pro' });
|
|
343
|
+
expect(logger.warn).not.toHaveBeenCalled();
|
|
344
|
+
});
|
|
345
|
+
|
|
346
|
+
test('padded bucketingKey is trimmed', async () => {
|
|
347
|
+
await provider.resolveStringEvaluation('f', 'x', { targetingKey: 'u1', bucketingKey: ' b1 ' }, logger);
|
|
348
|
+
expect(client.getTreatmentWithConfig).toHaveBeenCalledWith({ matchingKey: 'u1', bucketingKey: 'b1' }, 'f', {});
|
|
349
|
+
});
|
|
350
|
+
|
|
351
|
+
test.each([undefined, null])('bucketingKey %p → plain targeting key, no warning', async (value) => {
|
|
352
|
+
await provider.resolveStringEvaluation('f', 'x', { targetingKey: 'u1', bucketingKey: value }, logger);
|
|
353
|
+
expect(client.getTreatmentWithConfig).toHaveBeenCalledWith('u1', 'f', {});
|
|
354
|
+
expect(logger.warn).not.toHaveBeenCalled();
|
|
355
|
+
});
|
|
356
|
+
|
|
357
|
+
test.each([
|
|
358
|
+
['', 'blank'],
|
|
359
|
+
[' ', 'blank'],
|
|
360
|
+
[1, 'not a string'],
|
|
361
|
+
[true, 'not a string'],
|
|
362
|
+
[{ x: 1 }, 'not a string'],
|
|
363
|
+
])('invalid bucketingKey %p → targeting key only + one warning (%s)', async (value, reason) => {
|
|
364
|
+
const result = await provider.resolveStringEvaluation('f', 'x', { targetingKey: 'u1', bucketingKey: value }, logger);
|
|
365
|
+
expect(result.value).toBe('on');
|
|
366
|
+
expect(client.getTreatmentWithConfig).toHaveBeenCalledWith('u1', 'f', {});
|
|
367
|
+
expect(logger.warn).toHaveBeenCalledTimes(1);
|
|
368
|
+
const message = logger.warn.mock.calls[0][0];
|
|
369
|
+
expect(message).toContain('bucketingKey');
|
|
370
|
+
expect(message).toContain(reason);
|
|
371
|
+
if (typeof value === 'string' && value !== '') expect(message).not.toContain(`(${value})`);
|
|
372
|
+
});
|
|
373
|
+
|
|
374
|
+
test('warning never contains the value', async () => {
|
|
375
|
+
await provider.resolveStringEvaluation('f', 'x', { targetingKey: 'u1', bucketingKey: 12345 }, logger);
|
|
376
|
+
expect(logger.warn.mock.calls[0][0]).not.toContain('12345');
|
|
377
|
+
});
|
|
378
|
+
|
|
379
|
+
test('repeated invalid bucketingKey warns once per reason, then logs at debug', async () => {
|
|
380
|
+
const ctx = { targetingKey: 'u1', bucketingKey: '' };
|
|
381
|
+
await provider.resolveStringEvaluation('f', 'x', ctx, logger);
|
|
382
|
+
await provider.resolveStringEvaluation('g', 'x', ctx, logger);
|
|
383
|
+
await provider.resolveStringEvaluation('h', 'x', ctx, logger);
|
|
384
|
+
expect(logger.warn).toHaveBeenCalledTimes(1);
|
|
385
|
+
expect(logger.debug).toHaveBeenCalledTimes(2);
|
|
386
|
+
expect(logger.debug.mock.calls[0][0]).toContain('blank');
|
|
387
|
+
|
|
388
|
+
await provider.resolveStringEvaluation('f', 'x', { targetingKey: 'u1', bucketingKey: 1 }, logger);
|
|
389
|
+
expect(logger.warn).toHaveBeenCalledTimes(2);
|
|
390
|
+
expect(logger.warn.mock.calls[1][0]).toContain('not a string');
|
|
391
|
+
});
|
|
392
|
+
|
|
393
|
+
test('invalid bucketingKey without a logger does not throw', async () => {
|
|
394
|
+
await expect(provider.resolveStringEvaluation('f', 'x', { targetingKey: 'u1', bucketingKey: '' })).resolves.toMatchObject({ value: 'on' });
|
|
395
|
+
});
|
|
396
|
+
|
|
397
|
+
test('over-length bucketingKey is passed through; control → FlagNotFoundError', async () => {
|
|
398
|
+
client.getTreatmentWithConfig.mockResolvedValue({ treatment: 'control', config: null });
|
|
399
|
+
const long = 'b'.repeat(251);
|
|
400
|
+
await expect(provider.resolveBooleanEvaluation('f', false, { targetingKey: 'u1', bucketingKey: long }, logger))
|
|
401
|
+
.rejects.toMatchObject({ code: 'FLAG_NOT_FOUND' });
|
|
402
|
+
expect(client.getTreatmentWithConfig).toHaveBeenCalledWith({ matchingKey: 'u1', bucketingKey: long }, 'f', {});
|
|
403
|
+
});
|
|
404
|
+
|
|
405
|
+
test.each([
|
|
406
|
+
[{ bucketingKey: 'b1' }],
|
|
407
|
+
[{ targetingKey: '', bucketingKey: 'b1' }],
|
|
408
|
+
[{ targetingKey: '' }],
|
|
409
|
+
])('missing or empty targetingKey %p → TargetingKeyMissingError', async (context) => {
|
|
410
|
+
await expect(provider.resolveBooleanEvaluation('f', false, context, logger))
|
|
411
|
+
.rejects.toMatchObject({ code: 'TARGETING_KEY_MISSING' });
|
|
412
|
+
expect(client.getTreatmentWithConfig).not.toHaveBeenCalled();
|
|
413
|
+
});
|
|
414
|
+
|
|
415
|
+
test('track sends the targeting key only', async () => {
|
|
416
|
+
await provider.track('evt', { targetingKey: 'u1', trafficType: 'user', bucketingKey: 'b1_secret' }, { value: 1, properties: { a: 'x' } });
|
|
417
|
+
expect(client.track).toHaveBeenCalledWith('u1', 'user', 'evt', 1, { a: 'x' });
|
|
418
|
+
expect(JSON.stringify(client.track.mock.calls)).not.toContain('b1_secret');
|
|
419
|
+
});
|
|
420
|
+
});
|
|
421
|
+
|
|
422
|
+
describe('bucketingKey with the localhost SDK', () => {
|
|
423
|
+
test('over-length bucketingKey → real SDK returns control → FLAG_NOT_FOUND', async () => {
|
|
424
|
+
const provider = new OpenFeatureSplitProvider(getSplitFactory());
|
|
425
|
+
await provider.initialize();
|
|
426
|
+
await expect(provider.resolveBooleanEvaluation('my_feature', false, { targetingKey: 'key', bucketingKey: 'b'.repeat(251) }))
|
|
427
|
+
.rejects.toMatchObject({ code: 'FLAG_NOT_FOUND' });
|
|
428
|
+
await expect(provider.resolveBooleanEvaluation('my_feature', false, { targetingKey: 'key', bucketingKey: 'anything' }))
|
|
429
|
+
.resolves.toMatchObject({ value: true });
|
|
430
|
+
await provider.onClose();
|
|
431
|
+
});
|
|
432
|
+
});
|
package/src/index.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export * from './lib/js-split-provider';
|
|
1
|
+
export * from './lib/js-split-provider.js';
|