@splitsoftware/openfeature-js-split-provider 1.3.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 CHANGED
@@ -1,3 +1,13 @@
1
+ 1.4.0 (November 26, 2026)
2
+ - Added support for the `bucketingKey` evaluation context attribute: when it is a non-blank string, the provider evaluates with Split key { matchingKey: targetingKey, bucketingKey }.
3
+ - BREAKING-ish behavior change: `bucketingKey` is no longer forwarded to Split as a targeting attribute. Targeting rules on an attribute literally named `bucketingKey` stop matching.
4
+ - A `bucketingKey` longer than 250 characters now makes the evaluation return the default value (Split `control`, FLAG_NOT_FOUND) instead of being ignored.
5
+ - `bucketingKey` is trimmed; blank or non-string values are ignored with a warning.
6
+ - Fixed native ESM import failing with ERR_MODULE_NOT_FOUND on Node.js (relative imports now include `.js` extensions and the `es` build is marked as `"type": "module"`) (#26).
7
+ - Fixed `repository`, `homepage` and `bugs` URLs in package.json.
8
+ - Added a `types` condition to package.json `exports` so TypeScript resolves the type definitions with `moduleResolution` `node16`, `nodenext` and `bundler`.
9
+ - Added `tslib` as an explicit dependency (it was required by the build output but only installed transitively).
10
+
1
11
  1.3.0 (March 2, 2026)
2
12
  - Updated ConfigurationChanged event to forward SDK_UPDATE metadata from Split (flagsChanged, metadata with type and names)
3
13
  - Requires @splitsoftware/splitio ^11.10.0 for SDK_UPDATE metadata support
package/README.md CHANGED
@@ -91,6 +91,18 @@ OpenFeatureAPI.getInstance().setCtx(context)
91
91
  ```
92
92
  If the context was set at the client or api level, it is not required to provide it during flag evaluation.
93
93
 
94
+ ### Bucketing key
95
+ Set `bucketingKey` in the evaluation context to have Split bucket on a different key than the targeting key (Split's matching key + bucketing key). It is optional and sits next to `targetingKey` and `trafficType`:
96
+ ```js
97
+ const context = { targetingKey: 'user-123', bucketingKey: 'account-42' };
98
+ const value = await client.getBooleanValue('boolFlag', false, context);
99
+ ```
100
+ - `bucketingKey` is reserved: it is **not** forwarded to Split as an attribute.
101
+ - Leading/trailing whitespace is trimmed. A blank or non-string value is ignored (a warning is logged through the OpenFeature logger) and the targeting key alone is used.
102
+ - An empty targeting key (`''`) is treated as missing.
103
+ - A value longer than 250 characters is rejected by the Split SDK: the evaluation returns the default value (`FLAG_NOT_FOUND`, Split `control`).
104
+ - A targeting key is still required. `track` sends the targeting key only.
105
+
94
106
  ## Evaluate with details
95
107
  Use the get*Details(...) APIs to get the value and rich context (variant, reason, error code, metadata). This provider includes the Split treatment config as a raw JSON string under flagMetadata["config"]
96
108
 
package/es/lib/context.js CHANGED
@@ -1,15 +1,27 @@
1
- import { DEFAULT_TRAFFIC_TYPE } from './types';
1
+ import { DEFAULT_TRAFFIC_TYPE } from './types.js';
2
+ function readBucketingKey(value) {
3
+ if (value == null)
4
+ return {};
5
+ if (typeof value !== 'string')
6
+ return { bucketingKeyIssue: 'not a string' };
7
+ const bucketingKey = value.trim();
8
+ if (bucketingKey === '')
9
+ return { bucketingKeyIssue: 'blank' };
10
+ return { bucketingKey };
11
+ }
2
12
  /**
3
13
  * Transforms OpenFeature evaluation context into the consumer shape used by the Split API:
4
- * targeting key, traffic type (with default), and remaining attributes.
14
+ * targeting key, bucketing key (validated, trimmed), traffic type (with default),
15
+ * and remaining attributes. `bucketingKey` is never forwarded as an attribute.
5
16
  */
6
17
  export function transformContext(context, defaultTrafficType = DEFAULT_TRAFFIC_TYPE) {
7
- const { targetingKey, trafficType: ttVal, ...attributes } = context;
18
+ const { targetingKey, bucketingKey, trafficType: ttVal, ...attributes } = context;
8
19
  const trafficType = ttVal != null && typeof ttVal === 'string' && ttVal.trim() !== ''
9
20
  ? ttVal
10
21
  : defaultTrafficType;
11
22
  return {
12
23
  targetingKey,
24
+ ...readBucketingKey(bucketingKey),
13
25
  trafficType,
14
26
  attributes: JSON.parse(JSON.stringify(attributes)),
15
27
  };
@@ -1,15 +1,17 @@
1
1
  import { FlagNotFoundError, OpenFeatureEventEmitter, ParseError, ProviderEvents, StandardResolutionReasons, TargetingKeyMissingError } from '@openfeature/server-sdk';
2
- import { getSplitClient } from './client-resolver';
3
- import { transformContext } from './context';
4
- import { parseValidJsonObject, parseValidNumber } from './parsers';
5
- import { attachReadyEventHandlers, waitUntilReady } from './readiness';
6
- import { CONTROL_TREATMENT, CONTROL_VALUE_ERROR_MESSAGE, DEFAULT_TRAFFIC_TYPE, PROVIDER_NAME } from './types';
2
+ import { getSplitClient } from './client-resolver.js';
3
+ import { transformContext } from './context.js';
4
+ import { parseValidJsonObject, parseValidNumber } from './parsers.js';
5
+ import { attachReadyEventHandlers, waitUntilReady } from './readiness.js';
6
+ import { CONTROL_TREATMENT, CONTROL_VALUE_ERROR_MESSAGE, DEFAULT_TRAFFIC_TYPE, PROVIDER_NAME } from './types.js';
7
7
  export class OpenFeatureSplitProvider {
8
8
  constructor(options) {
9
9
  this.metadata = {
10
10
  name: PROVIDER_NAME,
11
11
  };
12
12
  this.runsOn = 'server';
13
+ // Invalid-bucketingKey reasons already warned about; repeats are logged at debug.
14
+ this.warnedBucketingKeyIssues = new Set();
13
15
  this.events = new OpenFeatureEventEmitter();
14
16
  this.trafficType = DEFAULT_TRAFFIC_TYPE;
15
17
  this.client = getSplitClient(options);
@@ -36,8 +38,7 @@ export class OpenFeatureSplitProvider {
36
38
  await waitUntilReady(this.client, this.events, this.metadata.name);
37
39
  }
38
40
  async resolveBooleanEvaluation(flagKey, _, context, logger) {
39
- void logger;
40
- const details = await this.evaluateTreatment(flagKey, transformContext(context, this.trafficType));
41
+ const details = await this.evaluateTreatment(flagKey, transformContext(context, this.trafficType), logger);
41
42
  const treatment = details.value.toLowerCase();
42
43
  if (treatment === 'on' || treatment === 'true') {
43
44
  return { ...details, value: true };
@@ -48,28 +49,38 @@ export class OpenFeatureSplitProvider {
48
49
  throw new ParseError(`Invalid boolean value for ${treatment}`);
49
50
  }
50
51
  async resolveStringEvaluation(flagKey, _, context, logger) {
51
- void logger;
52
- return this.evaluateTreatment(flagKey, transformContext(context, this.trafficType));
52
+ return this.evaluateTreatment(flagKey, transformContext(context, this.trafficType), logger);
53
53
  }
54
54
  async resolveNumberEvaluation(flagKey, _, context, logger) {
55
- void logger;
56
- const details = await this.evaluateTreatment(flagKey, transformContext(context, this.trafficType));
55
+ const details = await this.evaluateTreatment(flagKey, transformContext(context, this.trafficType), logger);
57
56
  return { ...details, value: parseValidNumber(details.value) };
58
57
  }
59
58
  async resolveObjectEvaluation(flagKey, _, context, logger) {
60
- void logger;
61
- const details = await this.evaluateTreatment(flagKey, transformContext(context, this.trafficType));
59
+ const details = await this.evaluateTreatment(flagKey, transformContext(context, this.trafficType), logger);
62
60
  return { ...details, value: parseValidJsonObject(details.value) };
63
61
  }
64
- async evaluateTreatment(flagKey, consumer) {
62
+ async evaluateTreatment(flagKey, consumer, logger) {
65
63
  if (!consumer.targetingKey) {
66
64
  throw new TargetingKeyMissingError('The Split provider requires a targeting key.');
67
65
  }
68
66
  if (flagKey == null || flagKey === '') {
69
67
  throw new FlagNotFoundError('flagKey must be a non-empty string');
70
68
  }
69
+ if (consumer.bucketingKeyIssue) {
70
+ const message = `Split provider: ignoring bucketingKey (${consumer.bucketingKeyIssue}); evaluating with the targeting key only.`;
71
+ if (this.warnedBucketingKeyIssues.has(consumer.bucketingKeyIssue)) {
72
+ logger?.debug(message);
73
+ }
74
+ else {
75
+ this.warnedBucketingKeyIssues.add(consumer.bucketingKeyIssue);
76
+ logger?.warn(message);
77
+ }
78
+ }
79
+ const splitKey = consumer.bucketingKey
80
+ ? { matchingKey: consumer.targetingKey, bucketingKey: consumer.bucketingKey }
81
+ : consumer.targetingKey;
71
82
  await waitUntilReady(this.client, this.events, this.metadata.name);
72
- const { treatment: value, config } = await this.client.getTreatmentWithConfig(consumer.targetingKey, flagKey, consumer.attributes);
83
+ const { treatment: value, config } = await this.client.getTreatmentWithConfig(splitKey, flagKey, consumer.attributes);
73
84
  if (value === CONTROL_TREATMENT) {
74
85
  throw new FlagNotFoundError(CONTROL_VALUE_ERROR_MESSAGE);
75
86
  }
@@ -1,5 +1,5 @@
1
1
  import { ProviderEvents } from '@openfeature/server-sdk';
2
- import { PROVIDER_NAME } from './types';
2
+ import { PROVIDER_NAME } from './types.js';
3
3
  /**
4
4
  * Builds OpenFeature Ready event details including Split SDK ready metadata.
5
5
  * Handles Split SDK not passing metadata (e.g. in consumer/Redis mode).
@@ -0,0 +1 @@
1
+ { "type": "module" }
package/lib/index.js CHANGED
@@ -1,4 +1,4 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  const tslib_1 = require("tslib");
4
- tslib_1.__exportStar(require("./lib/js-split-provider"), exports);
4
+ tslib_1.__exportStar(require("./lib/js-split-provider.js"), exports);
@@ -1,18 +1,30 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.transformContext = void 0;
4
- const types_1 = require("./types");
4
+ const types_js_1 = require("./types.js");
5
+ function readBucketingKey(value) {
6
+ if (value == null)
7
+ return {};
8
+ if (typeof value !== 'string')
9
+ return { bucketingKeyIssue: 'not a string' };
10
+ const bucketingKey = value.trim();
11
+ if (bucketingKey === '')
12
+ return { bucketingKeyIssue: 'blank' };
13
+ return { bucketingKey };
14
+ }
5
15
  /**
6
16
  * Transforms OpenFeature evaluation context into the consumer shape used by the Split API:
7
- * targeting key, traffic type (with default), and remaining attributes.
17
+ * targeting key, bucketing key (validated, trimmed), traffic type (with default),
18
+ * and remaining attributes. `bucketingKey` is never forwarded as an attribute.
8
19
  */
9
- function transformContext(context, defaultTrafficType = types_1.DEFAULT_TRAFFIC_TYPE) {
10
- const { targetingKey, trafficType: ttVal, ...attributes } = context;
20
+ function transformContext(context, defaultTrafficType = types_js_1.DEFAULT_TRAFFIC_TYPE) {
21
+ const { targetingKey, bucketingKey, trafficType: ttVal, ...attributes } = context;
11
22
  const trafficType = ttVal != null && typeof ttVal === 'string' && ttVal.trim() !== ''
12
23
  ? ttVal
13
24
  : defaultTrafficType;
14
25
  return {
15
26
  targetingKey,
27
+ ...readBucketingKey(bucketingKey),
16
28
  trafficType,
17
29
  attributes: JSON.parse(JSON.stringify(attributes)),
18
30
  };
@@ -2,21 +2,23 @@
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 client_resolver_1 = require("./client-resolver");
6
- const context_1 = require("./context");
7
- const parsers_1 = require("./parsers");
8
- const readiness_1 = require("./readiness");
9
- const types_1 = require("./types");
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");
10
10
  class OpenFeatureSplitProvider {
11
11
  constructor(options) {
12
12
  this.metadata = {
13
- name: types_1.PROVIDER_NAME,
13
+ name: types_js_1.PROVIDER_NAME,
14
14
  };
15
15
  this.runsOn = 'server';
16
+ // Invalid-bucketingKey reasons already warned about; repeats are logged at debug.
17
+ this.warnedBucketingKeyIssues = new Set();
16
18
  this.events = new server_sdk_1.OpenFeatureEventEmitter();
17
- this.trafficType = types_1.DEFAULT_TRAFFIC_TYPE;
18
- this.client = (0, client_resolver_1.getSplitClient)(options);
19
- (0, readiness_1.attachReadyEventHandlers)(this.client, this.events, this.metadata.name);
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);
20
22
  this.client.on(this.client.Event.SDK_UPDATE, (updateMetadata) => {
21
23
  const eventDetails = {
22
24
  providerName: this.metadata.name,
@@ -36,11 +38,10 @@ class OpenFeatureSplitProvider {
36
38
  */
37
39
  async initialize(_context) {
38
40
  void _context;
39
- await (0, readiness_1.waitUntilReady)(this.client, this.events, this.metadata.name);
41
+ await (0, readiness_js_1.waitUntilReady)(this.client, this.events, this.metadata.name);
40
42
  }
41
43
  async resolveBooleanEvaluation(flagKey, _, context, logger) {
42
- void logger;
43
- const details = await this.evaluateTreatment(flagKey, (0, context_1.transformContext)(context, this.trafficType));
44
+ const details = await this.evaluateTreatment(flagKey, (0, context_js_1.transformContext)(context, this.trafficType), logger);
44
45
  const treatment = details.value.toLowerCase();
45
46
  if (treatment === 'on' || treatment === 'true') {
46
47
  return { ...details, value: true };
@@ -51,30 +52,40 @@ class OpenFeatureSplitProvider {
51
52
  throw new server_sdk_1.ParseError(`Invalid boolean value for ${treatment}`);
52
53
  }
53
54
  async resolveStringEvaluation(flagKey, _, context, logger) {
54
- void logger;
55
- return this.evaluateTreatment(flagKey, (0, context_1.transformContext)(context, this.trafficType));
55
+ return this.evaluateTreatment(flagKey, (0, context_js_1.transformContext)(context, this.trafficType), logger);
56
56
  }
57
57
  async resolveNumberEvaluation(flagKey, _, context, logger) {
58
- void logger;
59
- const details = await this.evaluateTreatment(flagKey, (0, context_1.transformContext)(context, this.trafficType));
60
- return { ...details, value: (0, parsers_1.parseValidNumber)(details.value) };
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) };
61
60
  }
62
61
  async resolveObjectEvaluation(flagKey, _, context, logger) {
63
- void logger;
64
- const details = await this.evaluateTreatment(flagKey, (0, context_1.transformContext)(context, this.trafficType));
65
- return { ...details, value: (0, parsers_1.parseValidJsonObject)(details.value) };
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) };
66
64
  }
67
- async evaluateTreatment(flagKey, consumer) {
65
+ async evaluateTreatment(flagKey, consumer, logger) {
68
66
  if (!consumer.targetingKey) {
69
67
  throw new server_sdk_1.TargetingKeyMissingError('The Split provider requires a targeting key.');
70
68
  }
71
69
  if (flagKey == null || flagKey === '') {
72
70
  throw new server_sdk_1.FlagNotFoundError('flagKey must be a non-empty string');
73
71
  }
74
- await (0, readiness_1.waitUntilReady)(this.client, this.events, this.metadata.name);
75
- const { treatment: value, config } = await this.client.getTreatmentWithConfig(consumer.targetingKey, flagKey, consumer.attributes);
76
- if (value === types_1.CONTROL_TREATMENT) {
77
- throw new server_sdk_1.FlagNotFoundError(types_1.CONTROL_VALUE_ERROR_MESSAGE);
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);
78
89
  }
79
90
  const flagMetadata = Object.freeze({ config: config ?? '' });
80
91
  return {
@@ -88,7 +99,7 @@ class OpenFeatureSplitProvider {
88
99
  if (trackingEventName == null || trackingEventName === '') {
89
100
  throw new server_sdk_1.ParseError('Missing eventName, required to track');
90
101
  }
91
- const { targetingKey, trafficType } = (0, context_1.transformContext)(context, this.trafficType);
102
+ const { targetingKey, trafficType } = (0, context_js_1.transformContext)(context, this.trafficType);
92
103
  if (targetingKey == null || targetingKey === '') {
93
104
  throw new server_sdk_1.TargetingKeyMissingError('Missing targetingKey, required to track');
94
105
  }
@@ -2,7 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.emitReadyEvent = exports.waitUntilReady = exports.attachReadyEventHandlers = void 0;
4
4
  const server_sdk_1 = require("@openfeature/server-sdk");
5
- const types_1 = require("./types");
5
+ const types_js_1 = require("./types.js");
6
6
  /**
7
7
  * Builds OpenFeature Ready event details including Split SDK ready metadata.
8
8
  * Handles Split SDK not passing metadata (e.g. in consumer/Redis mode).
@@ -16,7 +16,7 @@ function buildReadyEventDetails(providerName, splitMetadata, readyFromCache) {
16
16
  metadata.lastUpdateTimestamp = splitMetadata.lastUpdateTimestamp;
17
17
  }
18
18
  return {
19
- providerName: providerName || types_1.PROVIDER_NAME,
19
+ providerName: providerName || types_js_1.PROVIDER_NAME,
20
20
  metadata,
21
21
  };
22
22
  }
@@ -26,7 +26,7 @@ function buildReadyEventDetails(providerName, splitMetadata, readyFromCache) {
26
26
  * If the client is already ready when attaching (e.g. localhost or reused client), emits Ready once
27
27
  * with best-effort metadata so handlers always receive at least one Ready when the client is ready.
28
28
  */
29
- function attachReadyEventHandlers(client, events, providerName = types_1.PROVIDER_NAME) {
29
+ function attachReadyEventHandlers(client, events, providerName = types_js_1.PROVIDER_NAME) {
30
30
  client.on(client.Event.SDK_READY_FROM_CACHE, (splitMetadata) => {
31
31
  events.emit(server_sdk_1.ProviderEvents.Ready, buildReadyEventDetails(providerName, splitMetadata, true));
32
32
  });
@@ -44,7 +44,7 @@ exports.attachReadyEventHandlers = attachReadyEventHandlers;
44
44
  * or rejects if the client has timed out (SDK_READY_TIMED_OUT).
45
45
  * Used to gate evaluations until the SDK has synchronized with the backend.
46
46
  */
47
- function waitUntilReady(client, events, providerName = types_1.PROVIDER_NAME) {
47
+ function waitUntilReady(client, events, providerName = types_js_1.PROVIDER_NAME) {
48
48
  return new Promise((resolve, reject) => {
49
49
  const status = client.getStatus();
50
50
  if (status.isReady) {
@@ -64,7 +64,7 @@ function waitUntilReady(client, events, providerName = types_1.PROVIDER_NAME) {
64
64
  });
65
65
  }
66
66
  exports.waitUntilReady = waitUntilReady;
67
- function emitReadyEvent(client, events, providerName = types_1.PROVIDER_NAME) {
67
+ function emitReadyEvent(client, events, providerName = types_js_1.PROVIDER_NAME) {
68
68
  events.emit(server_sdk_1.ProviderEvents.Ready, buildReadyEventDetails(providerName, undefined, false));
69
69
  }
70
70
  exports.emitReadyEvent = emitReadyEvent;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@splitsoftware/openfeature-js-split-provider",
3
- "version": "1.3.0",
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-split-provider-js",
16
- "homepage": "https://github.com/splitio/openfeature-split-provider-js#readme",
17
- "bugs": "https://github.com/splitio/openfeature-split-provider-js/issues",
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,12 +30,14 @@
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
  },
37
38
  "dependencies": {
38
- "@splitsoftware/splitio": "^11.10.0"
39
+ "@splitsoftware/splitio": "^11.10.0",
40
+ "tslib": "^2.3.1"
39
41
  },
40
42
  "peerDependencies": {
41
43
  "@openfeature/server-sdk": "^1.20.0"
@@ -67,6 +69,7 @@
67
69
  "build-cjs": "rimraf lib && tsc -outDir lib -m CommonJS",
68
70
  "postbuild-cjs": "cross-env NODE_ENV=cjs node scripts/copy.packages.json.js && ./scripts/build_cjs_replace_imports.sh",
69
71
  "build": "rimraf lib es && npm run build-cjs && npm run build-esm",
72
+ "postbuild": "node scripts/check_module_imports.js",
70
73
  "check": "npm run check:lint",
71
74
  "check:lint": "eslint src",
72
75
  "test": "cross-env NODE_ENV=test jest",
@@ -312,3 +312,121 @@ describe('provider events metadata', () => {
312
312
  await provider.onClose();
313
313
  });
314
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';
@@ -70,3 +70,45 @@ describe('context', () => {
70
70
  });
71
71
  });
72
72
  });
73
+
74
+ describe('transformContext bucketingKey', () => {
75
+ it('reads a valid bucketingKey and does not forward it as an attribute', () => {
76
+ const result = transformContext({ targetingKey: 'u1', bucketingKey: 'b1', plan: 'pro' });
77
+ expect(result.bucketingKey).toBe('b1');
78
+ expect(result.bucketingKeyIssue).toBeUndefined();
79
+ expect(result.attributes).toEqual({ plan: 'pro' });
80
+ });
81
+
82
+ it('trims surrounding whitespace', () => {
83
+ const result = transformContext({ targetingKey: 'u1', bucketingKey: ' b1 ' });
84
+ expect(result.bucketingKey).toBe('b1');
85
+ expect(result.bucketingKeyIssue).toBeUndefined();
86
+ });
87
+
88
+ it.each([undefined, null])('treats %p as absent, with no issue', (value) => {
89
+ const result = transformContext({ targetingKey: 'u1', bucketingKey: value } as never);
90
+ expect(result).not.toHaveProperty('bucketingKey');
91
+ expect(result.bucketingKeyIssue).toBeUndefined();
92
+ expect(result.attributes).toEqual({});
93
+ });
94
+
95
+ it.each([
96
+ ['', 'blank'],
97
+ [' ', 'blank'],
98
+ [1, 'not a string'],
99
+ [true, 'not a string'],
100
+ [{ x: 1 }, 'not a string'],
101
+ ])('ignores %p with issue %p and strips it from attributes', (value, issue) => {
102
+ const result = transformContext({ targetingKey: 'u1', bucketingKey: value } as never);
103
+ expect(result.bucketingKey).toBeUndefined();
104
+ expect(result.bucketingKeyIssue).toBe(issue);
105
+ expect(result.attributes).toEqual({});
106
+ });
107
+
108
+ it('keeps BucketingKey and bucketing_key as plain attributes', () => {
109
+ const result = transformContext({ targetingKey: 'u1', BucketingKey: 'x', bucketing_key: 'y' });
110
+ expect(result.bucketingKey).toBeUndefined();
111
+ expect(result.bucketingKeyIssue).toBeUndefined();
112
+ expect(result.attributes).toEqual({ BucketingKey: 'x', bucketing_key: 'y' });
113
+ });
114
+ });
@@ -1,6 +1,6 @@
1
1
  import { SplitFactory } from '@splitsoftware/splitio';
2
2
  import type SplitIO from '@splitsoftware/splitio/types/splitio';
3
- import type { SplitProviderConstructorOptions, SplitProviderOptions } from './types';
3
+ import type { SplitProviderConstructorOptions, SplitProviderOptions } from './types.js';
4
4
 
5
5
  /**
6
6
  * Resolves the Split client from the various supported constructor option shapes.
@@ -1,22 +1,34 @@
1
1
  import type { EvaluationContext } from '@openfeature/server-sdk';
2
- import type { Consumer } from './types';
3
- import { DEFAULT_TRAFFIC_TYPE } from './types';
2
+ import type { BucketingKeyIssue, Consumer } from './types.js';
3
+ import { DEFAULT_TRAFFIC_TYPE } from './types.js';
4
+
5
+ function readBucketingKey(
6
+ value: unknown
7
+ ): { bucketingKey?: string; bucketingKeyIssue?: BucketingKeyIssue } {
8
+ if (value == null) return {};
9
+ if (typeof value !== 'string') return { bucketingKeyIssue: 'not a string' };
10
+ const bucketingKey = value.trim();
11
+ if (bucketingKey === '') return { bucketingKeyIssue: 'blank' };
12
+ return { bucketingKey };
13
+ }
4
14
 
5
15
  /**
6
16
  * Transforms OpenFeature evaluation context into the consumer shape used by the Split API:
7
- * targeting key, traffic type (with default), and remaining attributes.
17
+ * targeting key, bucketing key (validated, trimmed), traffic type (with default),
18
+ * and remaining attributes. `bucketingKey` is never forwarded as an attribute.
8
19
  */
9
20
  export function transformContext(
10
21
  context: EvaluationContext,
11
22
  defaultTrafficType: string = DEFAULT_TRAFFIC_TYPE
12
23
  ): Consumer {
13
- const { targetingKey, trafficType: ttVal, ...attributes } = context;
24
+ const { targetingKey, bucketingKey, trafficType: ttVal, ...attributes } = context;
14
25
  const trafficType =
15
26
  ttVal != null && typeof ttVal === 'string' && ttVal.trim() !== ''
16
27
  ? ttVal
17
28
  : defaultTrafficType;
18
29
  return {
19
30
  targetingKey,
31
+ ...readBucketingKey(bucketingKey),
20
32
  trafficType,
21
33
  attributes: JSON.parse(JSON.stringify(attributes)),
22
34
  };
@@ -14,10 +14,10 @@ import {
14
14
  TrackingEventDetails
15
15
  } from '@openfeature/server-sdk';
16
16
  import type SplitIO from '@splitsoftware/splitio/types/splitio';
17
- import { getSplitClient } from './client-resolver';
18
- import { transformContext } from './context';
19
- import { parseValidJsonObject, parseValidNumber } from './parsers';
20
- import { attachReadyEventHandlers, waitUntilReady } from './readiness';
17
+ import { getSplitClient } from './client-resolver.js';
18
+ import { transformContext } from './context.js';
19
+ import { parseValidJsonObject, parseValidNumber } from './parsers.js';
20
+ import { attachReadyEventHandlers, waitUntilReady } from './readiness.js';
21
21
  import {
22
22
  CONTROL_TREATMENT,
23
23
  CONTROL_VALUE_ERROR_MESSAGE,
@@ -25,7 +25,7 @@ import {
25
25
  PROVIDER_NAME,
26
26
  SplitProviderConstructorOptions,
27
27
  type Consumer
28
- } from './types';
28
+ } from './types.js';
29
29
 
30
30
  export class OpenFeatureSplitProvider implements Provider {
31
31
  readonly metadata = {
@@ -36,6 +36,8 @@ export class OpenFeatureSplitProvider implements Provider {
36
36
 
37
37
  private client: SplitIO.IClient | SplitIO.IAsyncClient;
38
38
  private trafficType: string;
39
+ // Invalid-bucketingKey reasons already warned about; repeats are logged at debug.
40
+ private readonly warnedBucketingKeyIssues = new Set<string>();
39
41
  public readonly events = new OpenFeatureEventEmitter();
40
42
 
41
43
  constructor(
@@ -78,10 +80,10 @@ export class OpenFeatureSplitProvider implements Provider {
78
80
  context: EvaluationContext,
79
81
  logger: Logger
80
82
  ): Promise<ResolutionDetails<boolean>> {
81
- void logger;
82
83
  const details = await this.evaluateTreatment(
83
84
  flagKey,
84
- transformContext(context, this.trafficType)
85
+ transformContext(context, this.trafficType),
86
+ logger
85
87
  );
86
88
  const treatment = details.value.toLowerCase();
87
89
 
@@ -101,10 +103,10 @@ export class OpenFeatureSplitProvider implements Provider {
101
103
  context: EvaluationContext,
102
104
  logger: Logger
103
105
  ): Promise<ResolutionDetails<string>> {
104
- void logger;
105
106
  return this.evaluateTreatment(
106
107
  flagKey,
107
- transformContext(context, this.trafficType)
108
+ transformContext(context, this.trafficType),
109
+ logger
108
110
  );
109
111
  }
110
112
 
@@ -114,10 +116,10 @@ export class OpenFeatureSplitProvider implements Provider {
114
116
  context: EvaluationContext,
115
117
  logger: Logger
116
118
  ): Promise<ResolutionDetails<number>> {
117
- void logger;
118
119
  const details = await this.evaluateTreatment(
119
120
  flagKey,
120
- transformContext(context, this.trafficType)
121
+ transformContext(context, this.trafficType),
122
+ logger
121
123
  );
122
124
  return { ...details, value: parseValidNumber(details.value) };
123
125
  }
@@ -128,17 +130,18 @@ export class OpenFeatureSplitProvider implements Provider {
128
130
  context: EvaluationContext,
129
131
  logger: Logger
130
132
  ): Promise<ResolutionDetails<U>> {
131
- void logger;
132
133
  const details = await this.evaluateTreatment(
133
134
  flagKey,
134
- transformContext(context, this.trafficType)
135
+ transformContext(context, this.trafficType),
136
+ logger
135
137
  );
136
138
  return { ...details, value: parseValidJsonObject<U>(details.value) };
137
139
  }
138
140
 
139
141
  private async evaluateTreatment(
140
142
  flagKey: string,
141
- consumer: Consumer
143
+ consumer: Consumer,
144
+ logger?: Logger
142
145
  ): Promise<ResolutionDetails<string>> {
143
146
  if (!consumer.targetingKey) {
144
147
  throw new TargetingKeyMissingError(
@@ -148,11 +151,23 @@ export class OpenFeatureSplitProvider implements Provider {
148
151
  if (flagKey == null || flagKey === '') {
149
152
  throw new FlagNotFoundError('flagKey must be a non-empty string');
150
153
  }
154
+ if (consumer.bucketingKeyIssue) {
155
+ const message = `Split provider: ignoring bucketingKey (${consumer.bucketingKeyIssue}); evaluating with the targeting key only.`;
156
+ if (this.warnedBucketingKeyIssues.has(consumer.bucketingKeyIssue)) {
157
+ logger?.debug(message);
158
+ } else {
159
+ this.warnedBucketingKeyIssues.add(consumer.bucketingKeyIssue);
160
+ logger?.warn(message);
161
+ }
162
+ }
163
+ const splitKey: SplitIO.SplitKey = consumer.bucketingKey
164
+ ? { matchingKey: consumer.targetingKey, bucketingKey: consumer.bucketingKey }
165
+ : consumer.targetingKey;
151
166
 
152
167
  await waitUntilReady(this.client, this.events, this.metadata.name);
153
168
  const { treatment: value, config }: SplitIO.TreatmentWithConfig =
154
169
  await this.client.getTreatmentWithConfig(
155
- consumer.targetingKey,
170
+ splitKey,
156
171
  flagKey,
157
172
  consumer.attributes
158
173
  );
@@ -1,7 +1,7 @@
1
1
  import type { OpenFeatureEventEmitter } from '@openfeature/server-sdk';
2
2
  import { ProviderEvents } from '@openfeature/server-sdk';
3
3
  import type SplitIO from '@splitsoftware/splitio/types/splitio';
4
- import { PROVIDER_NAME } from './types';
4
+ import { PROVIDER_NAME } from './types.js';
5
5
 
6
6
  /**
7
7
  * Builds OpenFeature Ready event details including Split SDK ready metadata.
package/src/lib/types.ts CHANGED
@@ -7,12 +7,21 @@ export type SplitProviderOptions = {
7
7
  splitClient: SplitIO.IClient | SplitIO.IAsyncClient;
8
8
  };
9
9
 
10
+ /**
11
+ * Why a `bucketingKey` present in the evaluation context was ignored.
12
+ */
13
+ export type BucketingKeyIssue = 'blank' | 'not a string';
14
+
10
15
  /**
11
16
  * Consumer representation used for Split API calls:
12
- * targeting key, traffic type, and attributes.
17
+ * targeting key, optional bucketing key, traffic type, and attributes.
13
18
  */
14
19
  export type Consumer = {
15
20
  targetingKey: string | undefined;
21
+ /** Valid (non-blank string) bucketing key, trimmed. */
22
+ bucketingKey?: string;
23
+ /** Set when a `bucketingKey` was present but ignored. */
24
+ bucketingKeyIssue?: BucketingKeyIssue;
16
25
  trafficType: string;
17
26
  attributes: SplitIO.Attributes;
18
27
  };
package/types/index.d.ts CHANGED
@@ -1 +1 @@
1
- export * from './lib/js-split-provider';
1
+ export * from './lib/js-split-provider.js';
@@ -1,5 +1,5 @@
1
1
  import type SplitIO from '@splitsoftware/splitio/types/splitio';
2
- import type { SplitProviderConstructorOptions } from './types';
2
+ import type { SplitProviderConstructorOptions } from './types.js';
3
3
  /**
4
4
  * Resolves the Split client from the various supported constructor option shapes.
5
5
  * Supports: API key (string), Split SDK/AsyncSDK (factory), or pre-built splitClient.
@@ -1,7 +1,8 @@
1
1
  import type { EvaluationContext } from '@openfeature/server-sdk';
2
- import type { Consumer } from './types';
2
+ import type { Consumer } from './types.js';
3
3
  /**
4
4
  * Transforms OpenFeature evaluation context into the consumer shape used by the Split API:
5
- * targeting key, traffic type (with default), and remaining attributes.
5
+ * targeting key, bucketing key (validated, trimmed), traffic type (with default),
6
+ * and remaining attributes. `bucketingKey` is never forwarded as an attribute.
6
7
  */
7
8
  export declare function transformContext(context: EvaluationContext, defaultTrafficType?: string): Consumer;
@@ -1,5 +1,5 @@
1
1
  import { EvaluationContext, JsonValue, Logger, OpenFeatureEventEmitter, Provider, ResolutionDetails, TrackingEventDetails } from '@openfeature/server-sdk';
2
- import { SplitProviderConstructorOptions } from './types';
2
+ import { SplitProviderConstructorOptions } from './types.js';
3
3
  export declare class OpenFeatureSplitProvider implements Provider {
4
4
  readonly metadata: {
5
5
  readonly name: "split";
@@ -7,6 +7,7 @@ export declare class OpenFeatureSplitProvider implements Provider {
7
7
  readonly runsOn: "server";
8
8
  private client;
9
9
  private trafficType;
10
+ private readonly warnedBucketingKeyIssues;
10
11
  readonly events: OpenFeatureEventEmitter;
11
12
  constructor(options: SplitProviderConstructorOptions);
12
13
  /**
@@ -5,12 +5,20 @@ import type SplitIO from '@splitsoftware/splitio/types/splitio';
5
5
  export type SplitProviderOptions = {
6
6
  splitClient: SplitIO.IClient | SplitIO.IAsyncClient;
7
7
  };
8
+ /**
9
+ * Why a `bucketingKey` present in the evaluation context was ignored.
10
+ */
11
+ export type BucketingKeyIssue = 'blank' | 'not a string';
8
12
  /**
9
13
  * Consumer representation used for Split API calls:
10
- * targeting key, traffic type, and attributes.
14
+ * targeting key, optional bucketing key, traffic type, and attributes.
11
15
  */
12
16
  export type Consumer = {
13
17
  targetingKey: string | undefined;
18
+ /** Valid (non-blank string) bucketing key, trimmed. */
19
+ bucketingKey?: string;
20
+ /** Set when a `bucketingKey` was present but ignored. */
21
+ bucketingKeyIssue?: BucketingKeyIssue;
14
22
  trafficType: string;
15
23
  attributes: SplitIO.Attributes;
16
24
  };