Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
22
599 changes: 356 additions & 243 deletions package-lock.json

Large diffs are not rendered by default.

8 changes: 7 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@splitsoftware/openfeature-web-split-provider",
"version": "1.1.0",
"version": "1.0.0",
"description": "Split OpenFeature Web Provider",
"files": [
"README.md",
Expand All @@ -17,6 +17,12 @@
"bugs": "https://github.com/splitio/split-openfeature-provider-web-js/issues",
"license": "Apache-2.0",
"author": "Josh Sirota <josh.sirota@split.io>",
"contributors": [
"Nicolas Zelaya <nicolas.zelaya@harness.io> (https://github.com/NicoZelaya)",
"Emiliano Sanchez <emiliano.sanchez@harness.io> (https://github.com/EmilianoSanchez)",
"Emmanuel Zamora <emmanuel.zamora@harness.io> (https://github.com/ZamoraEmmanuel)",
"SDK Team <sdks@harness.io>"
],
"main": "lib/index.js",
"module": "es/index.js",
"browser": "lib/index.js",
Expand Down
9 changes: 4 additions & 5 deletions src/__tests__/integration/debug.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ describe('OpenFeature Split Provider - Debug Tests', () => {

beforeAll(async () => {
// Create with more debug options
splitClient = SplitFactory({
const splitFactory = SplitFactory({
core: {
authorizationKey: 'localhost',
},
Expand All @@ -20,7 +20,8 @@ describe('OpenFeature Split Provider - Debug Tests', () => {
'obj_feature': '{"key": "value"}'
},
debug: true
}).client();
})
splitClient =splitFactory.client();

// Add direct Split client test to verify it works as expected
console.log('Direct Split client test:');
Expand All @@ -30,9 +31,7 @@ describe('OpenFeature Split Provider - Debug Tests', () => {
console.log('- obj_feature:', splitClient.getTreatment('obj_feature'));

// Create provider
const provider = new OpenFeatureSplitProvider({
splitClient
});
const provider = new OpenFeatureSplitProvider(splitFactory);

// Register provider
OpenFeature.setProvider(provider);
Expand Down
9 changes: 4 additions & 5 deletions src/__tests__/integration/e2e.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ describe('OpenFeature Split Provider - E2E Integration Tests', () => {
// Set up before all tests
beforeAll(async () => {
// Initialize the Split client in localhost mode
splitClient = SplitFactory({
const splitFactory = SplitFactory({
core: {
authorizationKey: 'localhost'
},
Expand All @@ -32,7 +32,8 @@ describe('OpenFeature Split Provider - E2E Integration Tests', () => {
treatment: '{"key": "value"}'
}
}
}).client();
})
splitClient = splitFactory.client();

// Wait for the client to be ready
await new Promise((resolve) => {
Expand All @@ -55,9 +56,7 @@ describe('OpenFeature Split Provider - E2E Integration Tests', () => {
});

// Create the Split provider with the real Split client
const provider = new OpenFeatureSplitProvider({
splitClient
});
const provider = new OpenFeatureSplitProvider(splitFactory);

// Register the provider with OpenFeature
OpenFeature.setProvider(provider);
Expand Down
4 changes: 1 addition & 3 deletions src/__tests__/integration/mock.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,7 @@ describe('OpenFeature Split Provider - Mock Integration Tests', () => {
};

// Create the provider with our mock Split client
provider = new OpenFeatureSplitProvider({
splitClient: mockSplitClient
});
provider = new OpenFeatureSplitProvider({ client: () => mockSplitClient});

// Register with OpenFeature
OpenFeature.setProviderAndWait(provider);
Expand Down
12 changes: 6 additions & 6 deletions src/__tests__/integration/working.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { OpenFeatureSplitProvider } from '../../lib/js-split-provider';
describe('OpenFeature Split Provider - Working Integration Test', () => {
let client;
let provider;
let options;
let splitClient;

// Properly define the split treatments for localhost mode
const localFeatures = {
Expand All @@ -21,15 +21,15 @@ describe('OpenFeature Split Provider - Working Integration Test', () => {

beforeEach(async () => {
// Create client
const splitClient = SplitFactory({
const splitFactory = SplitFactory({
core: {
authorizationKey: 'localhost'
},
features: localFeatures
}).client();
})
splitClient = splitFactory.client();

options = {splitClient}
provider = new OpenFeatureSplitProvider(options);
provider = new OpenFeatureSplitProvider(splitFactory);

OpenFeature.setProvider(provider);
OpenFeature.setContext({targetingKey: 'user1'})
Expand All @@ -38,7 +38,7 @@ describe('OpenFeature Split Provider - Working Integration Test', () => {
});

afterEach(async () => {
await options.splitClient.destroy();
await splitClient.destroy();
});

test('boolean treatment evaluations', async () => {
Expand Down
2 changes: 1 addition & 1 deletion src/__tests__/provider-unit.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ describe('OpenFeatureSplitProvider Unit Tests', () => {

// Create the provider with our mock client
provider = new OpenFeatureSplitProvider({
splitClient: mockSplitClient
client: () => mockSplitClient
});
});

Expand Down
19 changes: 15 additions & 4 deletions src/__tests__/webSuites/client.spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable @typescript-eslint/no-unused-vars */
import { OpenFeature } from '@openfeature/web-sdk';
import { OpenFeature, OpenFeatureEventEmitter, ProviderEvents } from '@openfeature/web-sdk';
import { SplitFactory } from '@splitsoftware/splitio-browserjs';
import { OpenFeatureSplitProvider } from '../..';

Expand All @@ -26,6 +26,10 @@ jest.mock('@splitsoftware/splitio-browserjs', () => {
// Add ready method for web SDK
ready: jest.fn(() => true),
__getStatus: () => ({isReady: true}),
Event: {
'SDK_UPDATE': 'SDK_UPDATE'
},
on: () => {},
// Add event registration compatible with web SDK
addListener: jest.fn(({ event, handler }) => {
if (event === 'SDK_READY') {
Expand Down Expand Up @@ -155,6 +159,12 @@ jest.mock('@openfeature/web-sdk', () => {
};

return {
OpenFeatureEventEmitter: () => ({
emit: () => {}
}),
ProviderEvents: {
Ready: 'ready'
},
OpenFeature: {
setProvider: jest.fn(),
getClient: jest.fn(() => mockClient)
Expand Down Expand Up @@ -304,14 +314,15 @@ export default async function() {
};

// Configure Split client for web environment
let splitClient = SplitFactory({
let splitFactory = SplitFactory({
core: {
authorizationKey: 'localhost'
},
features: mockFeatures,
}).client();
});
let splitClient = splitFactory.client();

let provider = new OpenFeatureSplitProvider({splitClient});
let provider = new OpenFeatureSplitProvider(splitFactory);
OpenFeature.setProvider(provider);

let client = OpenFeature.getClient('test');
Expand Down
43 changes: 18 additions & 25 deletions src/lib/js-split-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,10 @@ import {
Logger,
ProviderEvents,
OpenFeature,
OpenFeatureEventEmitter,
} from "@openfeature/web-sdk";
import type SplitIO from "@splitsoftware/splitio/types/splitio";

export interface SplitProviderOptions {
splitClient: SplitIO.IBrowserClient;
}

type Consumer = {
key: string | undefined;
attributes: SplitIO.Attributes;
Expand All @@ -30,32 +27,28 @@ export class OpenFeatureSplitProvider implements Provider {
metadata = {
name: "split",
};
private initialized: Promise<void>;
private client: SplitIO.IBrowserClient;
public readonly events = new OpenFeatureEventEmitter();

constructor(options: SplitProviderOptions) {
this.client = options.splitClient;

// Create initialization promise
this.initialized = new Promise<void>((resolve) => {
constructor(splitFactory: SplitIO.IBrowserSDK) {
this.client = splitFactory.client();
this.client.on(this.client.Event.SDK_UPDATE, () => {
this.events.emit(ProviderEvents.ConfigurationChanged)
});

const onSdkReady = () => {
console.log(`${this.metadata.name} provider initialized`);
resolve();
};
const onSdkReady = () => {
console.log(`${this.metadata.name} provider initialized`);
this.events.emit(ProviderEvents.Ready)
};

// If client is ready, resolve immediately
if (this.isClientReady()) {
onSdkReady();
} else {
this.client.on(this.client.Event.SDK_READY, onSdkReady);
}
}).catch((e) => {
// In case of any issues, resolve the promise to prevent hanging
console.warn(`${this.metadata.name} provider initialization error: ${e}`);
});
// If client is ready, resolve immediately
if (this.isClientReady()) {
onSdkReady();
} else {
this.client.on(this.client.Event.SDK_READY, onSdkReady);
}
}

// Safe method to check if client is ready
private isClientReady(): boolean {
return (this.client as any).__getStatus().isReady;
Expand Down