Skip to content

Commit c12969e

Browse files
authored
Implement hello app. (#1)
1 parent d1a8b39 commit c12969e

File tree

6 files changed

+139
-0
lines changed

6 files changed

+139
-0
lines changed

.circleci/config.yml

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
version: 2.1
2+
3+
workflows:
4+
test:
5+
jobs:
6+
- do-test:
7+
context: hello-world-demos
8+
9+
test-daily:
10+
triggers:
11+
- schedule:
12+
cron: "0 6 * * *"
13+
filters:
14+
branches:
15+
only: main
16+
jobs:
17+
- do-test:
18+
context: hello-world-demos
19+
20+
jobs:
21+
do-test:
22+
docker:
23+
- image: cimg/node:14.19
24+
environment:
25+
SOURCE_FILE_NAME: index.js
26+
steps:
27+
- checkout
28+
- run:
29+
name: insert SDK key and flag key into demo code
30+
command: |
31+
sed -i.bak "s/sdkKey *= *''/sdkKey = '${LD_HELLO_WORLD_SDK_KEY}'/" ${SOURCE_FILE_NAME}
32+
sed -i.bak "s/featureFlagKey *= *'.*'/featureFlagKey = '${LD_HELLO_WORLD_FLAG_KEY_WITH_TRUE_VALUE}'/" ${SOURCE_FILE_NAME}
33+
- run:
34+
name: run demo
35+
command: |
36+
npm i
37+
npm start | tee output.txt
38+
grep "is true for this user" output.txt || (echo "Flag did not evaluate to expected true value" && exit 1)

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package-lock.json
2+
node_modules/
3+
index.js.bak

LICENSE.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
Copyright 2022 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.

README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# LaunchDarkly sample OpenFeature Server-Side Node.js application
2+
3+
We've built a simple console that demonstrates how LaunchDarkly's OpenFeature provider works.
4+
5+
The LaunchDarkly OpenFeature provider for the Server-Side SDK for Node.js is designed primarily for use in multi-user systems such as web servers and applications. It follows the server-side LaunchDarkly model for multi-user contexts. It is not intended for use in desktop, browser, and embedded systems applications.
6+
7+
## Supported Node versions
8+
9+
This sample is compatible with Node.js versions 14 and above.
10+
11+
## Build instructions
12+
13+
1. Install dependencies using `npm install`.
14+
15+
2. Edit `index.js` and set the value of `sdkKey` to your LaunchDarkly SDK key. If there is an existing boolean feature flag in your LaunchDarkly project that you want to evaluate, set `featureFlagKey` to the flag key.
16+
17+
```
18+
const sdkKey = '1234567890abcdef';
19+
20+
const featureFlagKey = 'my-flag';
21+
```
22+
23+
3. On the command line, run `npm start`
24+
25+
You should receive the message ”Feature flag ‘<flag key>’ is <true/false> for this user”.

index.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { OpenFeature } from '@openfeature/js-sdk';
2+
import { init } from 'launchdarkly-node-server-sdk';
3+
import { LaunchDarklyProvider } from '@launchdarkly/openfeature-node-server';
4+
5+
// Set sdkKey to your LaunchDarkly SDK key.
6+
const sdkKey = '';
7+
8+
// Set featureFlagKey to the feature flag key you want to evaluate
9+
const featureFlagKey = 'my-boolean-flag';
10+
11+
// Set up the user properties. This user should appear on your LaunchDarkly users dashboard
12+
// soon after you run the demo.
13+
// Remember when using OpenFeature to use `targetingKey` instead of `key`.
14+
const context = {
15+
targetingKey: 'example-user-key'
16+
};
17+
18+
const ldClient = init(sdkKey);
19+
await ldClient.waitForInitialization();
20+
21+
OpenFeature.setProvider(new LaunchDarklyProvider(ldClient));
22+
const client = OpenFeature.getClient();
23+
24+
const flagValue = await client.getBooleanValue(featureFlagKey, false, context);
25+
26+
console.log(`Feature flag '${featureFlagKey}' is ${flagValue} for this user`);
27+
28+
// Here we ensure that the SDK shuts down cleanly and has a chance to deliver analytics
29+
// events to LaunchDarkly before the program exits. If analytics events are not delivered,
30+
// the user properties and flag usage statistics will not appear on your dashboard. In a
31+
// normal long-running application, the SDK would continue running and events would be
32+
// delivered automatically in the background.
33+
await ldClient.flush();
34+
ldClient.close();

package.json

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"name": "hello-openfeature-node-server",
3+
"version": "1.0.0",
4+
"description": "Minimal Example using LaunchDarkly with OpenFeature",
5+
"main": "index.js",
6+
"keywords": [
7+
"launchdarkly",
8+
"launch",
9+
"darkly",
10+
"node",
11+
"sdk",
12+
"openfeature",
13+
"provider"
14+
],
15+
"scripts": {
16+
"start": "node index.js"
17+
},
18+
"type": "module",
19+
"author": "LaunchDarkly <team@launchdarkly.com>",
20+
"license": "Apache-2.0",
21+
"dependencies": {
22+
"@launchdarkly/openfeature-node-server": ">= 0.1.6",
23+
"@openfeature/js-sdk": "^1.x",
24+
"launchdarkly-node-server-sdk": ">= 6.4.3"
25+
}
26+
}

0 commit comments

Comments
 (0)