Skip to content

Commit a0956f5

Browse files
committed
chore: init shared openfeature server provider
1 parent 247ae7c commit a0956f5

20 files changed

Lines changed: 954 additions & 0 deletions

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"packages/shared/sdk-server",
1010
"packages/shared/sdk-server-edge",
1111
"packages/shared/akamai-edgeworker-sdk",
12+
"packages/shared/openfeature-server-common",
1213
"packages/sdk/server-node",
1314
"packages/sdk/server-node/contract-tests",
1415
"packages/sdk/cloudflare",
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
Copyright 2025 Catamorphic, Co.
2+
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License.
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# LaunchDarkly OpenFeature Common Server Provider
2+
3+
<!--
4+
[![NPM][openfeature-server-common-npm-badge]][openfeature-server-common-npm-link]
5+
[![Actions Status][openfeature-server-common-ci-badge]][openfeature-server-common-ci]
6+
-->
7+
8+
> [!CAUTION]
9+
> This SDK is in pre-release and not subject to backwards compatibility
10+
> guarantees. The API may change based on feedback.
11+
>
12+
> Pin to a specific minor version and review the [changelog](CHANGELOG.md) before upgrading.
13+
14+
This package contains the shared OpenFeature provider implementation for LaunchDarkly server-side JavaScript SDKs. It provides a base provider class and translation utilities that convert between OpenFeature and LaunchDarkly concepts.
15+
16+
This package is not intended to be used directly.
17+
18+
## Contributing
19+
20+
See [Contributing](../CONTRIBUTING.md).
21+
22+
## Verifying SDK build provenance with the SLSA framework
23+
24+
LaunchDarkly uses the [SLSA framework](https://slsa.dev/spec/v1.0/about) (Supply-chain Levels for Software Artifacts) to help developers make their supply chain more secure by ensuring the authenticity and build integrity of our published SDK packages. To learn more, see the [provenance guide](PROVENANCE.md).
25+
26+
## About LaunchDarkly
27+
28+
- LaunchDarkly is a continuous delivery platform that provides feature flags as a service and allows developers to iterate quickly and safely. We allow you to easily flag your features and manage them from the LaunchDarkly dashboard. With LaunchDarkly, you can:
29+
- Roll out a new feature to a subset of your users (like a group of users who opt-in to a beta tester group), gathering feedback and bug reports from real-world use cases.
30+
- Gradually roll out a feature to an increasing percentage of users, and track the effect that the feature has on key metrics (for instance, how likely is a user to complete a purchase if they have feature A versus feature B?).
31+
- Turn off a feature that you realize is causing performance problems in production, without needing to re-deploy, or even restart the application with a changed configuration file.
32+
- Grant access to certain features based on user attributes, like payment plan (eg: users on the 'gold' plan get access to more features than users in the 'silver' plan).
33+
- Disable parts of your application to facilitate maintenance, without taking everything offline.
34+
- LaunchDarkly provides feature flag SDKs for a wide variety of languages and technologies. Check out [our documentation](https://docs.launchdarkly.com/sdk) for a complete list.
35+
- Explore LaunchDarkly
36+
- [launchdarkly.com](https://www.launchdarkly.com/ 'LaunchDarkly Main Website') for more information
37+
- [docs.launchdarkly.com](https://docs.launchdarkly.com/ 'LaunchDarkly Documentation') for our documentation and SDK reference guides
38+
- [apidocs.launchdarkly.com](https://apidocs.launchdarkly.com/ 'LaunchDarkly API Documentation') for our API documentation
39+
- [blog.launchdarkly.com](https://blog.launchdarkly.com/ 'LaunchDarkly Blog Documentation') for the latest product updates
40+
41+
<!--
42+
[openfeature-server-common-npm-badge]: https://img.shields.io/npm/v/@launchdarkly/openfeature-js-server-common.svg?style=flat-square
43+
[openfeature-server-common-npm-link]: https://www.npmjs.com/package/@launchdarkly/openfeature-js-server-common
44+
[openfeature-server-common-ci-badge]: https://github.com/launchdarkly/js-core/actions/workflows/openfeature-node-server.yml/badge.svg
45+
[openfeature-server-common-ci]: https://github.com/launchdarkly/js-core/actions/workflows/openfeature-node-server.yml
46+
-->
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import type { LDLogger } from '@launchdarkly/js-sdk-common';
2+
3+
export default class TestLogger implements LDLogger {
4+
public logs: string[] = [];
5+
6+
error(...args: any[]): void {
7+
this.logs.push(args.join(' '));
8+
}
9+
10+
warn(...args: any[]): void {
11+
this.logs.push(args.join(' '));
12+
}
13+
14+
info(...args: any[]): void {
15+
this.logs.push(args.join(' '));
16+
}
17+
18+
debug(...args: any[]): void {
19+
this.logs.push(args.join(' '));
20+
}
21+
22+
reset() {
23+
this.logs = [];
24+
}
25+
}
Lines changed: 218 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,218 @@
1+
import { translateContext } from '../src/translateContext';
2+
import TestLogger from './TestLogger';
3+
4+
it('Uses the targetingKey as the user key', () => {
5+
const logger = new TestLogger();
6+
expect(translateContext(logger, { targetingKey: 'the-key' })).toEqual({
7+
key: 'the-key',
8+
kind: 'user',
9+
});
10+
expect(logger.logs.length).toEqual(0);
11+
});
12+
13+
it('gives targetingKey precedence over key', () => {
14+
const logger = new TestLogger();
15+
expect(translateContext(logger, { targetingKey: 'target-key', key: 'key-key' })).toEqual({
16+
key: 'target-key',
17+
kind: 'user',
18+
});
19+
// Should log a warning about both being defined.
20+
expect(logger.logs.length).toEqual(1);
21+
});
22+
23+
describe.each([
24+
['name', 'value2'],
25+
['firstName', 'value3'],
26+
['lastName', 'value4'],
27+
['email', 'value5'],
28+
['avatar', 'value6'],
29+
['ip', 'value7'],
30+
['country', 'value8'],
31+
['anonymous', true],
32+
])('given correct built-in attributes', (key, value) => {
33+
const logger = new TestLogger();
34+
it('translates the key correctly', () => {
35+
expect(translateContext(logger, { targetingKey: 'the-key', [key]: value })).toEqual({
36+
key: 'the-key',
37+
[key]: value,
38+
kind: 'user',
39+
});
40+
expect(logger.logs.length).toEqual(0);
41+
});
42+
});
43+
44+
it.each(['key', 'targetingKey'])('handles key or targetingKey', (key) => {
45+
const logger = new TestLogger();
46+
expect(translateContext(logger, { [key]: 'the-key' })).toEqual({
47+
key: 'the-key',
48+
kind: 'user',
49+
});
50+
expect(logger.logs.length).toEqual(0);
51+
});
52+
53+
describe.each([
54+
['name', 17],
55+
['anonymous', 'value'],
56+
])('given incorrect built-in attributes', (key, value) => {
57+
it('the bad key is omitted', () => {
58+
const logger = new TestLogger();
59+
expect(translateContext(logger, { targetingKey: 'the-key', [key]: value })).toEqual({
60+
key: 'the-key',
61+
kind: 'user',
62+
});
63+
expect(logger.logs[0]).toMatch(new RegExp(`The attribute '${key}' must be of type.*`));
64+
});
65+
});
66+
67+
it('accepts custom attributes', () => {
68+
const logger = new TestLogger();
69+
expect(translateContext(logger, { targetingKey: 'the-key', someAttr: 'someValue' })).toEqual({
70+
key: 'the-key',
71+
kind: 'user',
72+
someAttr: 'someValue',
73+
});
74+
expect(logger.logs.length).toEqual(0);
75+
});
76+
77+
it('accepts string/boolean/number arrays', () => {
78+
const logger = new TestLogger();
79+
expect(
80+
translateContext(logger, {
81+
targetingKey: 'the-key',
82+
strings: ['a', 'b', 'c'],
83+
numbers: [1, 2, 3],
84+
booleans: [true, false],
85+
}),
86+
).toEqual({
87+
key: 'the-key',
88+
kind: 'user',
89+
strings: ['a', 'b', 'c'],
90+
numbers: [1, 2, 3],
91+
booleans: [true, false],
92+
});
93+
expect(logger.logs.length).toEqual(0);
94+
});
95+
96+
it('converts date to ISO strings', () => {
97+
const date = new Date();
98+
const logger = new TestLogger();
99+
expect(translateContext(logger, { targetingKey: 'the-key', date })).toEqual({
100+
key: 'the-key',
101+
kind: 'user',
102+
date: date.toISOString(),
103+
});
104+
expect(logger.logs.length).toEqual(0);
105+
});
106+
107+
it('can convert a single kind context', () => {
108+
const evaluationContext = {
109+
kind: 'organization',
110+
targetingKey: 'my-org-key',
111+
};
112+
113+
const expectedContext = {
114+
kind: 'organization',
115+
key: 'my-org-key',
116+
};
117+
118+
const logger = new TestLogger();
119+
expect(translateContext(logger, evaluationContext)).toEqual(expectedContext);
120+
expect(logger.logs.length).toEqual(0);
121+
});
122+
123+
it('can convert a multi-context', () => {
124+
const evaluationContext = {
125+
kind: 'multi',
126+
organization: {
127+
targetingKey: 'my-org-key',
128+
myCustomAttribute: 'myAttributeValue',
129+
},
130+
user: {
131+
targetingKey: 'my-user-key',
132+
},
133+
};
134+
135+
const expectedContext = {
136+
kind: 'multi',
137+
organization: {
138+
key: 'my-org-key',
139+
myCustomAttribute: 'myAttributeValue',
140+
},
141+
user: {
142+
key: 'my-user-key',
143+
},
144+
};
145+
146+
const logger = new TestLogger();
147+
expect(translateContext(logger, evaluationContext)).toEqual(expectedContext);
148+
expect(logger.logs.length).toEqual(0);
149+
});
150+
151+
it('can handle privateAttributes in a single context', () => {
152+
const evaluationContext = {
153+
kind: 'organization',
154+
name: 'the-org-name',
155+
targetingKey: 'my-org-key',
156+
myCustomAttribute: 'myCustomValue',
157+
privateAttributes: ['myCustomAttribute'],
158+
};
159+
160+
const expectedContext = {
161+
kind: 'organization',
162+
name: 'the-org-name',
163+
key: 'my-org-key',
164+
myCustomAttribute: 'myCustomValue',
165+
_meta: {
166+
privateAttributes: ['myCustomAttribute'],
167+
},
168+
};
169+
170+
const logger = new TestLogger();
171+
expect(translateContext(logger, evaluationContext)).toEqual(expectedContext);
172+
expect(logger.logs.length).toEqual(0);
173+
});
174+
175+
it('detects a cycle and logs an error', () => {
176+
const a: any = {
177+
b: { c: {} },
178+
};
179+
180+
a.b.c = a;
181+
const evaluationContext = {
182+
key: 'a-key',
183+
kind: 'singularity',
184+
a,
185+
};
186+
187+
const expectedContext = {
188+
key: 'a-key',
189+
kind: 'singularity',
190+
a: { b: {} },
191+
};
192+
193+
const logger = new TestLogger();
194+
expect(translateContext(logger, evaluationContext)).toEqual(expectedContext);
195+
expect(logger.logs.length).toEqual(1);
196+
});
197+
198+
it('allows references in different branches', () => {
199+
const a = { test: 'test' };
200+
201+
const evaluationContext = {
202+
key: 'a-key',
203+
kind: 'singularity',
204+
b: { a },
205+
c: { a },
206+
};
207+
208+
const expectedContext = {
209+
key: 'a-key',
210+
kind: 'singularity',
211+
b: { a: { test: 'test' } },
212+
c: { a: { test: 'test' } },
213+
};
214+
215+
const logger = new TestLogger();
216+
expect(translateContext(logger, evaluationContext)).toEqual(expectedContext);
217+
expect(logger.logs.length).toEqual(0);
218+
});
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import { translateResult } from '../src/translateResult';
2+
3+
it.each([true, 'potato', 42, { yes: 'no' }])('puts the value into the result.', (value) => {
4+
expect(
5+
translateResult<typeof value>({
6+
value,
7+
reason: {
8+
kind: 'OFF',
9+
},
10+
}).value,
11+
).toEqual(value);
12+
});
13+
14+
it('converts the variationIndex into a string variant', () => {
15+
expect(
16+
translateResult<boolean>({
17+
value: true,
18+
variationIndex: 9,
19+
reason: {
20+
kind: 'OFF',
21+
},
22+
}).variant,
23+
).toEqual('9');
24+
});
25+
26+
it.each(['OFF', 'FALLTHROUGH', 'TARGET_MATCH', 'PREREQUISITE_FAILED', 'ERROR'])(
27+
'populates the resolution reason',
28+
(reason) => {
29+
expect(
30+
translateResult<boolean>({
31+
value: true,
32+
variationIndex: 9,
33+
reason: {
34+
kind: reason,
35+
},
36+
}).reason,
37+
).toEqual(reason);
38+
},
39+
);
40+
41+
it('does not populate the errorCode when there is not an error', () => {
42+
const translated = translateResult<boolean>({
43+
value: true,
44+
variationIndex: 9,
45+
reason: {
46+
kind: 'OFF',
47+
},
48+
});
49+
expect(translated.errorCode).toBeUndefined();
50+
});
51+
52+
it('does populate the errorCode when there is an error', () => {
53+
const translated = translateResult<boolean>({
54+
value: true,
55+
variationIndex: 9,
56+
reason: {
57+
kind: 'ERROR',
58+
errorKind: 'BAD_APPLE',
59+
},
60+
});
61+
expect(translated.errorCode).toEqual('GENERAL');
62+
});

0 commit comments

Comments
 (0)