launchdarkly-node-client-sdk 2.2.2 → 3.0.1
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/.ldrelease/config.yml +5 -0
- package/CHANGELOG.md +37 -0
- package/contract-tests/index.js +2 -1
- package/contract-tests/sdkClientEntity.js +10 -13
- package/docs/Makefile +0 -5
- package/docs/doc.md +7 -0
- package/docs/typedoc.js +6 -4
- package/package.json +5 -4
- package/src/__tests__/LDClient-events-test.js +4 -4
- package/src/__tests__/LDClient-streaming-test.js +6 -6
- package/src/__tests__/LDClient-test.js +4 -4
- package/src/__tests__/LDClient-tls-test.js +8 -8
- package/src/index.js +2 -2
- package/test-types.ts +57 -24
- package/typings.d.ts +6 -14
package/.ldrelease/config.yml
CHANGED
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,43 @@
|
|
|
2
2
|
|
|
3
3
|
All notable changes to the LaunchDarkly Client-Side SDK for Node.js will be documented in this file.
|
|
4
4
|
|
|
5
|
+
## [3.0.0] - 2023-01-12
|
|
6
|
+
The latest version of this SDK supports LaunchDarkly's new custom contexts feature. Contexts are an evolution of a previously-existing concept, "users." Contexts let you create targeting rules for feature flags based on a variety of different information, including attributes pertaining to users, organizations, devices, and more. You can even combine contexts to create "multi-contexts."
|
|
7
|
+
|
|
8
|
+
This feature is only available to members of LaunchDarkly's Early Access Program (EAP). If you're in the EAP, you can use contexts by updating your SDK to the latest version and, if applicable, updating your Relay Proxy. Outdated SDK versions do not support contexts, and will cause unpredictable flag evaluation behavior.
|
|
9
|
+
|
|
10
|
+
If you are not in the EAP, only use single contexts of kind "user", or continue to use the user type if available. If you try to create contexts, the context will be sent to LaunchDarkly, but any data not related to the user object will be ignored.
|
|
11
|
+
|
|
12
|
+
For detailed information about this version, please refer to the list below. For information on how to upgrade from the previous version, please read the [migration guide](https://docs.launchdarkly.com/sdk/client-side/node-js/migration-2-to-3).
|
|
13
|
+
|
|
14
|
+
### Added:
|
|
15
|
+
- The types `LDContext`, `LDSingleKindContext`, and `LDMultiKindContext` define the new "context" model.
|
|
16
|
+
- All SDK methods that took an `LDUser` parameter now take an `LDContext`. `LDUser` is now a subset of `LDContext`, so existing code based on users will still work.
|
|
17
|
+
|
|
18
|
+
### Changed _(breaking changes from 3.x)_:
|
|
19
|
+
- There is no longer such a thing as a `secondary` meta-attribute that affects percentage rollouts. If you set an attribute with that name in `LDContext`, it will simply be a custom attribute like any other.
|
|
20
|
+
- Evaluations now treat the `anonymous` attribute as a simple boolean, with no distinction between a false state and an undefined state.
|
|
21
|
+
- `LDClient.getUser` has been replaced with `LDClient.getContext`.
|
|
22
|
+
- `privateAttributeNames` has been replaced with `privateAttributes` in `LDOptions`. Private attributes now allow using attribute references.
|
|
23
|
+
- Update the version of typedoc used for documentation generation, and update the documentation generation process.
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
### Changed (behavioral changes):
|
|
27
|
+
- Analytics event data now uses a new JSON schema due to differences between the context model and the old user model.
|
|
28
|
+
|
|
29
|
+
### Removed:
|
|
30
|
+
- Removed all types, fields, and methods that were deprecated as of the most recent 3.x release.
|
|
31
|
+
- Removed the `secondary` meta-attribute in `LDUser`.
|
|
32
|
+
- The `alias` method no longer exists because alias events are not needed in the new context model.
|
|
33
|
+
- The `autoAliasingOptOut` and `inlineUsersInEvents` options no longer exist because they are not relevant in the new context model.
|
|
34
|
+
|
|
35
|
+
### Deprecated:
|
|
36
|
+
- The `LDUser` object has been deprecated. Support for `LDUser` is maintained to simplify the upgrade process, but it is recommended to use `LDContext` in the shape of either `LDSingleKindContext` or `LDMultiKindContext`.
|
|
37
|
+
|
|
38
|
+
## [2.2.2] - 2022-12-19
|
|
39
|
+
### Fixed:
|
|
40
|
+
- Upgraded to `js-sdk-common` version `4.3.3` which fixed this [issue](https://github.com/launchdarkly/js-sdk-common/issues/84): Warning: Accessing non-existent property 'messages' of module exports inside circular dependency
|
|
41
|
+
|
|
5
42
|
## [2.2.1] - 2022-10-21
|
|
6
43
|
### Changed:
|
|
7
44
|
- Upgraded to `js-sdk-common` version `4.3.2` which includes implementations of `jitter` and `backoff` for streaming connections. When a connection fails the retry will start at the `streamReconnectDelay` and will double on each unsuccessful consecutive connection attempt (`backoff`) to a max of 30 seconds. The delay will be adjusted from 50%-100% of the calculated delay to prevent many clients from attempting to reconnect at the same time (`jitter`).
|
package/contract-tests/index.js
CHANGED
|
@@ -16,7 +16,8 @@ function makeSdkConfig(options, tag) {
|
|
|
16
16
|
autoAliasingOptOut: options.clientSide.autoAliasingOptOut,
|
|
17
17
|
evaluationReasons: options.clientSide.evaluationReasons,
|
|
18
18
|
logger: sdkLogger(tag),
|
|
19
|
-
useReport: options.clientSide.useReport
|
|
19
|
+
useReport: options.clientSide.useReport,
|
|
20
|
+
sendEventsOnlyForVariation: true,
|
|
20
21
|
};
|
|
21
22
|
|
|
22
23
|
if (options.serviceEndpoints) {
|
|
@@ -47,8 +48,7 @@ function makeSdkConfig(options, tag) {
|
|
|
47
48
|
cf.eventCapacity = options.events.capacity;
|
|
48
49
|
cf.diagnosticOptOut = !options.events.enableDiagnostics;
|
|
49
50
|
cf.flushInterval = maybeTime(options.events.flushIntervalMs);
|
|
50
|
-
cf.
|
|
51
|
-
cf.privateAttributeNames = options.events.globalPrivateAttributes;
|
|
51
|
+
cf.privateAttributes = options.events.globalPrivateAttributes;
|
|
52
52
|
} else {
|
|
53
53
|
cf.sendEvents = false;
|
|
54
54
|
}
|
|
@@ -63,8 +63,8 @@ function makeSdkConfig(options, tag) {
|
|
|
63
63
|
return cf;
|
|
64
64
|
}
|
|
65
65
|
|
|
66
|
-
function
|
|
67
|
-
return {key: 'key-not-specified'};
|
|
66
|
+
function makeDefaultInitialContext() {
|
|
67
|
+
return {kind: 'user', key: 'key-not-specified'};
|
|
68
68
|
}
|
|
69
69
|
|
|
70
70
|
async function newSdkClientEntity(options) {
|
|
@@ -78,11 +78,12 @@ async function newSdkClientEntity(options) {
|
|
|
78
78
|
? options.configuration.startWaitTimeMs
|
|
79
79
|
: 5000;
|
|
80
80
|
const sdkConfig = makeSdkConfig(options.configuration, options.tag);
|
|
81
|
-
const
|
|
82
|
-
||
|
|
81
|
+
const initialContext = options.configuration.clientSide?.initialUser ||
|
|
82
|
+
options.configuration.clientSide?.initialContext ||
|
|
83
|
+
makeDefaultInitialContext();
|
|
83
84
|
const client = ld.initialize(
|
|
84
85
|
options.configuration.credential || 'unknown-env-id',
|
|
85
|
-
|
|
86
|
+
initialContext,
|
|
86
87
|
sdkConfig
|
|
87
88
|
);
|
|
88
89
|
let failed = false;
|
|
@@ -119,7 +120,7 @@ async function newSdkClientEntity(options) {
|
|
|
119
120
|
return { state: client.allFlags() };
|
|
120
121
|
|
|
121
122
|
case 'identifyEvent':
|
|
122
|
-
await client.identify(params.identifyEvent.user);
|
|
123
|
+
await client.identify(params.identifyEvent.user || params.identifyEvent.context);
|
|
123
124
|
return undefined;
|
|
124
125
|
|
|
125
126
|
case 'customEvent': {
|
|
@@ -128,10 +129,6 @@ async function newSdkClientEntity(options) {
|
|
|
128
129
|
return undefined;
|
|
129
130
|
}
|
|
130
131
|
|
|
131
|
-
case 'aliasEvent':
|
|
132
|
-
client.alias(params.aliasEvent.user, params.aliasEvent.previousUser);
|
|
133
|
-
return undefined;
|
|
134
|
-
|
|
135
132
|
case 'flushEvents':
|
|
136
133
|
client.flush();
|
|
137
134
|
return undefined;
|
package/docs/Makefile
CHANGED
|
@@ -15,13 +15,8 @@
|
|
|
15
15
|
|
|
16
16
|
html: prepare
|
|
17
17
|
../node_modules/.bin/typedoc --options typedoc.js
|
|
18
|
-
mv before-typedoc-package.json ../package.json
|
|
19
|
-
mv before-typedoc-package-lock.json ../package-lock.json
|
|
20
18
|
|
|
21
19
|
prepare:
|
|
22
|
-
cp ../package.json before-typedoc-package.json
|
|
23
|
-
cp ../package-lock.json before-typedoc-package-lock.json
|
|
24
|
-
cd .. && npm install "typedoc@<0.20.0"
|
|
25
20
|
rm -rf build
|
|
26
21
|
mkdir build
|
|
27
22
|
@# Extract the whole module declaration from launchdarkly-js-sdk-common, then remove the first and last lines
|
package/docs/doc.md
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
This is the API reference for the LaunchDarkly Client-Side Node SDK.
|
|
2
|
+
|
|
3
|
+
In typical usage, you will call [[initialize]] in the main process at startup time to obtain an
|
|
4
|
+
an instance of [[LDClient]].
|
|
5
|
+
|
|
6
|
+
For more information, see the [SDK reference guide](https://docs.launchdarkly.com/sdk/client-side/node-js).
|
|
7
|
+
|
package/docs/typedoc.js
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
// Note that the format of this file is barely documented on the TypeDoc site. In general,
|
|
2
|
+
// the properties are equivalent to the command-line options described here:
|
|
3
|
+
// https://typedoc.org/api/
|
|
1
4
|
|
|
2
5
|
let version = process.env.VERSION;
|
|
3
6
|
if (!version) {
|
|
@@ -8,8 +11,7 @@ if (!version) {
|
|
|
8
11
|
module.exports = {
|
|
9
12
|
out: './build/html',
|
|
10
13
|
name: 'LaunchDarkly Client-Side Node SDK (' + version + ')',
|
|
11
|
-
readme: '
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
entryPoint: '"launchdarkly-node-client-sdk"' // note extra quotes - workaround for a TypeDoc bug
|
|
14
|
+
readme: 'doc.md',
|
|
15
|
+
entryPointStrategy: 'resolve', // Allows us to specify the specific entrypoints.
|
|
16
|
+
entryPoints: ["./build/typings.d.ts"] // This is the updated version created by the makefile.
|
|
15
17
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "launchdarkly-node-client-sdk",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "3.0.1",
|
|
4
4
|
"description": "LaunchDarkly Client-Side SDK for Node.js",
|
|
5
5
|
"author": "LaunchDarkly <team@launchdarkly.com>",
|
|
6
6
|
"license": "Apache-2.0",
|
|
@@ -17,7 +17,7 @@
|
|
|
17
17
|
"test": "jest --ci",
|
|
18
18
|
"check-typescript": "node_modules/typescript/bin/tsc",
|
|
19
19
|
"contract-test-service": "npm --prefix contract-tests install && npm --prefix contract-tests start",
|
|
20
|
-
"contract-test-harness": "curl -s https://raw.githubusercontent.com/launchdarkly/sdk-test-harness/main/downloader/run.sh \\ | VERSION=
|
|
20
|
+
"contract-test-harness": "curl -s https://raw.githubusercontent.com/launchdarkly/sdk-test-harness/main/downloader/run.sh \\ | VERSION=v2 PARAMS=\"-url http://localhost:8000 -debug -stop-service-at-end $TEST_HARNESS_PARAMS\" sh",
|
|
21
21
|
"contract-tests": "npm run contract-test-service & npm run contract-test-harness"
|
|
22
22
|
},
|
|
23
23
|
"engines": {
|
|
@@ -40,7 +40,8 @@
|
|
|
40
40
|
"jest-junit": "^14.0.1",
|
|
41
41
|
"launchdarkly-js-test-helpers": "1.2.1",
|
|
42
42
|
"prettier": "2.7.1",
|
|
43
|
-
"typescript": "^4.8.4"
|
|
43
|
+
"typescript": "^4.8.4",
|
|
44
|
+
"typedoc": "^0.23.24"
|
|
44
45
|
},
|
|
45
46
|
"jest": {
|
|
46
47
|
"rootDir": "src",
|
|
@@ -64,7 +65,7 @@
|
|
|
64
65
|
},
|
|
65
66
|
"dependencies": {
|
|
66
67
|
"launchdarkly-eventsource": "1.4.3",
|
|
67
|
-
"launchdarkly-js-sdk-common": "
|
|
68
|
+
"launchdarkly-js-sdk-common": "5.0.2",
|
|
68
69
|
"node-localstorage": "^1.3.1"
|
|
69
70
|
},
|
|
70
71
|
"repository": {
|
|
@@ -4,7 +4,7 @@ const { TestHttpHandlers, TestHttpServers, sleepAsync, withCloseable } = require
|
|
|
4
4
|
|
|
5
5
|
describe('LDClient', () => {
|
|
6
6
|
const envName = 'UNKNOWN_ENVIRONMENT_ID';
|
|
7
|
-
const
|
|
7
|
+
const context = { key: 'user' };
|
|
8
8
|
|
|
9
9
|
// Event generation in general is tested in a non-platform-specific way in launchdarkly-js-sdk-common.
|
|
10
10
|
// The following tests just demonstrate that the common client calls our platform object when it
|
|
@@ -15,7 +15,7 @@ describe('LDClient', () => {
|
|
|
15
15
|
it('sends an event for track()', async () => {
|
|
16
16
|
await withCloseable(TestHttpServers.start, async server => {
|
|
17
17
|
const config = { bootstrap: {}, eventsUrl: server.url, diagnosticOptOut: true };
|
|
18
|
-
const client = LDClient.initialize(envName,
|
|
18
|
+
const client = LDClient.initialize(envName, context, config);
|
|
19
19
|
await withCloseable(client, async () => {
|
|
20
20
|
const data = { thing: 'stuff' };
|
|
21
21
|
await client.waitForInitialization();
|
|
@@ -30,7 +30,7 @@ describe('LDClient', () => {
|
|
|
30
30
|
const trackEvent = events[1];
|
|
31
31
|
expect(trackEvent.kind).toEqual('custom');
|
|
32
32
|
expect(trackEvent.key).toEqual('eventkey');
|
|
33
|
-
expect(trackEvent.
|
|
33
|
+
expect(trackEvent.contextKeys).toEqual({ user: 'user' });
|
|
34
34
|
expect(trackEvent.data).toEqual(data);
|
|
35
35
|
expect(trackEvent.url).toEqual(null);
|
|
36
36
|
});
|
|
@@ -65,7 +65,7 @@ describe('LDClient', () => {
|
|
|
65
65
|
await withCloseable(TestHttpServers.start, async server => {
|
|
66
66
|
server.byDefault(TestHttpHandlers.respond(202));
|
|
67
67
|
const config = { bootstrap: {}, eventsUrl: server.url };
|
|
68
|
-
const client = LDClient.initialize(envName,
|
|
68
|
+
const client = LDClient.initialize(envName, context, config);
|
|
69
69
|
await withCloseable(client, async () => {
|
|
70
70
|
const data = await expectDiagnosticEventAndDiscardRegularEvent(server);
|
|
71
71
|
expect(data.kind).toEqual('diagnostic-init');
|
|
@@ -15,9 +15,9 @@ const {
|
|
|
15
15
|
|
|
16
16
|
describe('LDClient streaming', () => {
|
|
17
17
|
const envName = 'UNKNOWN_ENVIRONMENT_ID';
|
|
18
|
-
const
|
|
19
|
-
const
|
|
20
|
-
const expectedGetUrl = '/eval/' + envName + '/' +
|
|
18
|
+
const context = { key: 'user' };
|
|
19
|
+
const encodedContext = 'eyJrZXkiOiJ1c2VyIn0';
|
|
20
|
+
const expectedGetUrl = '/eval/' + envName + '/' + encodedContext;
|
|
21
21
|
const expectedReportUrl = '/eval/' + envName;
|
|
22
22
|
|
|
23
23
|
it('makes GET request and receives an event', async () => {
|
|
@@ -34,7 +34,7 @@ describe('LDClient streaming', () => {
|
|
|
34
34
|
streamUrl: server.url,
|
|
35
35
|
sendEvents: false,
|
|
36
36
|
};
|
|
37
|
-
await withCloseable(LDClient.initialize(envName,
|
|
37
|
+
await withCloseable(LDClient.initialize(envName, context, config), async client => {
|
|
38
38
|
const changeEvents = eventSink(client, 'change:flag');
|
|
39
39
|
await client.waitForInitialization();
|
|
40
40
|
|
|
@@ -62,7 +62,7 @@ describe('LDClient streaming', () => {
|
|
|
62
62
|
sendEvents: false,
|
|
63
63
|
useReport: true,
|
|
64
64
|
};
|
|
65
|
-
await withCloseable(LDClient.initialize(envName,
|
|
65
|
+
await withCloseable(LDClient.initialize(envName, context, config), async client => {
|
|
66
66
|
const changeEvents = eventSink(client, 'change:flag');
|
|
67
67
|
await client.waitForInitialization();
|
|
68
68
|
|
|
@@ -71,7 +71,7 @@ describe('LDClient streaming', () => {
|
|
|
71
71
|
|
|
72
72
|
expect(server.requestCount()).toEqual(1);
|
|
73
73
|
const req = await server.nextRequest();
|
|
74
|
-
expect(req.body).toEqual(JSON.stringify(
|
|
74
|
+
expect(req.body).toEqual(JSON.stringify(context));
|
|
75
75
|
});
|
|
76
76
|
});
|
|
77
77
|
});
|
|
@@ -5,7 +5,7 @@ const { TestHttpHandlers, TestHttpServers, withCloseable } = require('launchdark
|
|
|
5
5
|
|
|
6
6
|
describe('LDClient', () => {
|
|
7
7
|
const envName = 'UNKNOWN_ENVIRONMENT_ID';
|
|
8
|
-
const
|
|
8
|
+
const context = { key: 'user' };
|
|
9
9
|
|
|
10
10
|
it('should exist', () => {
|
|
11
11
|
expect(LDClient).toBeDefined();
|
|
@@ -20,7 +20,7 @@ describe('LDClient', () => {
|
|
|
20
20
|
await withCloseable(TestHttpServers.start, async server => {
|
|
21
21
|
server.byDefault(TestHttpHandlers.respondJson({}));
|
|
22
22
|
const config = { baseUrl: server.url, sendEvents: false };
|
|
23
|
-
await withCloseable(LDClient.initialize(envName,
|
|
23
|
+
await withCloseable(LDClient.initialize(envName, context, config), async client => {
|
|
24
24
|
await client.waitForInitialization();
|
|
25
25
|
});
|
|
26
26
|
});
|
|
@@ -30,7 +30,7 @@ describe('LDClient', () => {
|
|
|
30
30
|
await withCloseable(TestHttpServers.start, async server => {
|
|
31
31
|
server.byDefault(TestHttpHandlers.respondJson({}));
|
|
32
32
|
const config = { baseUrl: server.url, sendEvents: false };
|
|
33
|
-
await withCloseable(LDClient.initialize(envName,
|
|
33
|
+
await withCloseable(LDClient.initialize(envName, context, config), async client => {
|
|
34
34
|
await client.waitForInitialization();
|
|
35
35
|
|
|
36
36
|
expect(server.requestCount()).toEqual(1);
|
|
@@ -50,7 +50,7 @@ describe('LDClient', () => {
|
|
|
50
50
|
error: jest.fn(),
|
|
51
51
|
};
|
|
52
52
|
const config = { bootstrap: {}, sendEvents: false, logger: logger };
|
|
53
|
-
await withCloseable(LDClient.initialize(envName,
|
|
53
|
+
await withCloseable(LDClient.initialize(envName, context, config), async client => {
|
|
54
54
|
await client.waitForInitialization();
|
|
55
55
|
|
|
56
56
|
client.track('whatever');
|
|
@@ -17,10 +17,10 @@ const stubLogger = {
|
|
|
17
17
|
|
|
18
18
|
describe('LDClient TLS configuration', () => {
|
|
19
19
|
const envName = 'UNKNOWN_ENVIRONMENT_ID';
|
|
20
|
-
const
|
|
21
|
-
const
|
|
22
|
-
const expectedPollingUrl = '/sdk/evalx/' + envName + '/
|
|
23
|
-
const expectedStreamingUrl = '/eval/' + envName + '/' +
|
|
20
|
+
const context = { key: 'user' };
|
|
21
|
+
const encodedContext = 'eyJrZXkiOiJ1c2VyIn0';
|
|
22
|
+
const expectedPollingUrl = '/sdk/evalx/' + envName + '/contexts/' + encodedContext;
|
|
23
|
+
const expectedStreamingUrl = '/eval/' + envName + '/' + encodedContext;
|
|
24
24
|
|
|
25
25
|
it('can connect via HTTPS to a server with a self-signed certificate, if CA is specified', async () => {
|
|
26
26
|
await withCloseable(TestHttpServers.startSecure, async server => {
|
|
@@ -31,7 +31,7 @@ describe('LDClient TLS configuration', () => {
|
|
|
31
31
|
tlsParams: { ca: server.certificate },
|
|
32
32
|
diagnosticOptOut: true,
|
|
33
33
|
};
|
|
34
|
-
await withCloseable(LDClient.initialize(envName,
|
|
34
|
+
await withCloseable(LDClient.initialize(envName, context, config), async client => {
|
|
35
35
|
await client.waitForInitialization();
|
|
36
36
|
});
|
|
37
37
|
});
|
|
@@ -46,7 +46,7 @@ describe('LDClient TLS configuration', () => {
|
|
|
46
46
|
logger: stubLogger,
|
|
47
47
|
diagnosticOptOut: true,
|
|
48
48
|
};
|
|
49
|
-
await withCloseable(LDClient.initialize(envName,
|
|
49
|
+
await withCloseable(LDClient.initialize(envName, context, config), async client => {
|
|
50
50
|
await expect(client.waitForInitialization()).rejects.toThrow(/network error.*self[- ]signed/);
|
|
51
51
|
});
|
|
52
52
|
});
|
|
@@ -68,7 +68,7 @@ describe('LDClient TLS configuration', () => {
|
|
|
68
68
|
tlsParams: { ca: server.certificate },
|
|
69
69
|
diagnosticOptOut: true,
|
|
70
70
|
};
|
|
71
|
-
await withCloseable(LDClient.initialize(envName,
|
|
71
|
+
await withCloseable(LDClient.initialize(envName, context, config), async client => {
|
|
72
72
|
const changeEvents = eventSink(client, 'change:flag');
|
|
73
73
|
|
|
74
74
|
await client.waitForInitialization();
|
|
@@ -91,7 +91,7 @@ describe('LDClient TLS configuration', () => {
|
|
|
91
91
|
tlsParams: { ca: server.certificate },
|
|
92
92
|
diagnosticOptOut: true,
|
|
93
93
|
};
|
|
94
|
-
await withCloseable(LDClient.initialize(envName,
|
|
94
|
+
await withCloseable(LDClient.initialize(envName, context, config), async client => {
|
|
95
95
|
await client.waitForInitialization();
|
|
96
96
|
await client.flush();
|
|
97
97
|
|
package/src/index.js
CHANGED
|
@@ -4,7 +4,7 @@ const nodePlatform = require('./nodePlatform');
|
|
|
4
4
|
const packageJson = require('../package.json');
|
|
5
5
|
|
|
6
6
|
// This creates a client-side SDK instance to be used in Node.
|
|
7
|
-
function initialize(env,
|
|
7
|
+
function initialize(env, context, options = {}) {
|
|
8
8
|
// Pass our platform object to the common code to create the Node version of the client
|
|
9
9
|
const platform = nodePlatform(options);
|
|
10
10
|
const extraOptionDefs = {
|
|
@@ -14,7 +14,7 @@ function initialize(env, user, options = {}) {
|
|
|
14
14
|
if (!options.logger) {
|
|
15
15
|
extraOptionDefs.logger = { default: basicLogger() };
|
|
16
16
|
}
|
|
17
|
-
const clientVars = common.initialize(env,
|
|
17
|
+
const clientVars = common.initialize(env, context, options, platform, extraOptionDefs);
|
|
18
18
|
|
|
19
19
|
clientVars.start();
|
|
20
20
|
|
package/test-types.ts
CHANGED
|
@@ -4,11 +4,11 @@
|
|
|
4
4
|
|
|
5
5
|
import * as ld from 'launchdarkly-node-client-sdk';
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
const ver: string = ld.version;
|
|
8
8
|
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
9
|
+
const emptyOptions: ld.LDOptions = {};
|
|
10
|
+
const logger: ld.LDLogger = ld.basicLogger({ level: 'info' });
|
|
11
|
+
const allOptions: ld.LDOptions = {
|
|
12
12
|
bootstrap: { },
|
|
13
13
|
baseUrl: '',
|
|
14
14
|
eventsUrl: '',
|
|
@@ -19,18 +19,16 @@ var allOptions: ld.LDOptions = {
|
|
|
19
19
|
evaluationReasons: true,
|
|
20
20
|
sendEvents: true,
|
|
21
21
|
allAttributesPrivate: true,
|
|
22
|
-
|
|
23
|
-
inlineUsersInEvents: true,
|
|
24
|
-
allowFrequentDuplicateEvents: true,
|
|
22
|
+
privateAttributes: [ 'x' ],
|
|
25
23
|
sendEventsOnlyForVariation: true,
|
|
26
24
|
flushInterval: 1,
|
|
27
25
|
streamReconnectDelay: 1,
|
|
28
|
-
logger: logger
|
|
29
|
-
autoAliasingOptOut: true
|
|
26
|
+
logger: logger
|
|
30
27
|
};
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
28
|
+
const userWithKeyOnly: ld.LDUser = { key: 'user' };
|
|
29
|
+
const contextWithKey: ld.LDContext = {kind: 'user', key: 'user-key'};
|
|
30
|
+
const anonymousUser: ld.LDUser = { key: 'anon-user', anonymous: true };
|
|
31
|
+
const user: ld.LDUser = {
|
|
34
32
|
key: 'user',
|
|
35
33
|
name: 'name',
|
|
36
34
|
firstName: 'first',
|
|
@@ -50,7 +48,31 @@ var user: ld.LDUser = {
|
|
|
50
48
|
},
|
|
51
49
|
privateAttributeNames: [ 'name', 'email' ]
|
|
52
50
|
};
|
|
53
|
-
|
|
51
|
+
|
|
52
|
+
const singleContext: ld.LDContext = {
|
|
53
|
+
kind: 'user',
|
|
54
|
+
key: 'user',
|
|
55
|
+
name: 'name',
|
|
56
|
+
firstName: 'first',
|
|
57
|
+
lastName: 'last',
|
|
58
|
+
email: 'test@example.com',
|
|
59
|
+
avatar: 'http://avatar.url',
|
|
60
|
+
ip: '1.1.1.1',
|
|
61
|
+
country: 'us',
|
|
62
|
+
anonymous: true,
|
|
63
|
+
_meta: {
|
|
64
|
+
privateAttributes: ['country']
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
const multiContext: ld.LDContext = {
|
|
69
|
+
kind: 'multi',
|
|
70
|
+
user: { key: 'user-key' },
|
|
71
|
+
org: { key: 'org-key' }
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
const client: ld.LDClient = ld.initialize('env', user, allOptions);
|
|
54
76
|
|
|
55
77
|
client.waitUntilReady().then(() => {});
|
|
56
78
|
client.waitForInitialization().then(() => {});
|
|
@@ -59,21 +81,32 @@ client.identify(user).then(() => {});
|
|
|
59
81
|
client.identify(user, undefined, () => {});
|
|
60
82
|
client.identify(user, 'hash').then(() => {});
|
|
61
83
|
|
|
62
|
-
client.
|
|
84
|
+
client.identify(contextWithKey).then(() => {});
|
|
85
|
+
client.identify(contextWithKey, undefined, () => {});
|
|
86
|
+
client.identify(contextWithKey, 'hash').then(() => {});
|
|
87
|
+
|
|
88
|
+
client.identify(singleContext).then(() => {});
|
|
89
|
+
client.identify(singleContext, undefined, () => {});
|
|
90
|
+
client.identify(singleContext, 'hash').then(() => {});
|
|
91
|
+
|
|
92
|
+
client.identify(multiContext).then(() => {});
|
|
93
|
+
client.identify(multiContext, undefined, () => {});
|
|
94
|
+
client.identify(multiContext, 'hash').then(() => {});
|
|
95
|
+
|
|
63
96
|
|
|
64
|
-
|
|
97
|
+
const context: ld.LDContext = client.getContext();
|
|
65
98
|
|
|
66
99
|
client.flush(() => {});
|
|
67
100
|
client.flush().then(() => {});
|
|
68
101
|
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
102
|
+
const boolFlagValue: ld.LDFlagValue = client.variation('key', false);
|
|
103
|
+
const numberFlagValue: ld.LDFlagValue = client.variation('key', 2);
|
|
104
|
+
const stringFlagValue: ld.LDFlagValue = client.variation('key', 'default');
|
|
72
105
|
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
106
|
+
const detail: ld.LDEvaluationDetail = client.variationDetail('key', 'default');
|
|
107
|
+
const detailValue: ld.LDFlagValue = detail.value;
|
|
108
|
+
const detailIndex: number | undefined = detail.variationIndex;
|
|
109
|
+
const detailReason: ld.LDEvaluationReason | undefined = detail.reason;
|
|
77
110
|
|
|
78
111
|
client.setStreaming(true);
|
|
79
112
|
client.setStreaming();
|
|
@@ -86,8 +119,8 @@ client.track('event');
|
|
|
86
119
|
client.track('event', { someData: 'x' });
|
|
87
120
|
client.track('event', null, 3.5);
|
|
88
121
|
|
|
89
|
-
|
|
90
|
-
|
|
122
|
+
const flagSet: ld.LDFlagSet = client.allFlags();
|
|
123
|
+
const flagSetValue: ld.LDFlagValue = flagSet['key'];
|
|
91
124
|
|
|
92
125
|
client.close(() => {});
|
|
93
126
|
client.close().then(() => {});
|
package/typings.d.ts
CHANGED
|
@@ -1,11 +1,3 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* This is the API reference for the LaunchDarkly Client-Side Node SDK.
|
|
3
|
-
*
|
|
4
|
-
* In typical usage, you will call [[initialize]] in the main process at startup time to obtain an
|
|
5
|
-
* an instance of [[LDClient]].
|
|
6
|
-
*
|
|
7
|
-
* For more information, see the [SDK reference guide](https://docs.launchdarkly.com/sdk/client-side/node-js).
|
|
8
|
-
*/
|
|
9
1
|
declare module 'launchdarkly-node-client-sdk' {
|
|
10
2
|
|
|
11
3
|
//// DOCBUILD-START-REPLACE (see docs/Makefile)
|
|
@@ -20,7 +12,7 @@ declare module 'launchdarkly-node-client-sdk' {
|
|
|
20
12
|
LDClientBase,
|
|
21
13
|
LDLogger,
|
|
22
14
|
LDOptionsBase,
|
|
23
|
-
|
|
15
|
+
LDContext
|
|
24
16
|
} from 'launchdarkly-js-sdk-common';
|
|
25
17
|
//// DOCBUILD-END-REPLACE
|
|
26
18
|
|
|
@@ -39,22 +31,22 @@ declare module 'launchdarkly-node-client-sdk' {
|
|
|
39
31
|
*
|
|
40
32
|
* @param envKey
|
|
41
33
|
* The LaunchDarkly environment ID.
|
|
42
|
-
* @param
|
|
43
|
-
* The initial
|
|
44
|
-
* The
|
|
34
|
+
* @param context
|
|
35
|
+
* The initial context properties. These can be changed later with [[LDClient.identify]].
|
|
36
|
+
* The context must have a `key` property, except that if you omit `context.key` and set `context.anonymous` to
|
|
45
37
|
* true, the SDK will create a randomized unique key (which will be cached in local storage for the
|
|
46
38
|
* current OS user account, so the next initialization will reuse the same key).
|
|
47
39
|
* @param options
|
|
48
40
|
* Optional configuration settings.
|
|
49
41
|
*/
|
|
50
|
-
export function initialize(envKey: string,
|
|
42
|
+
export function initialize(envKey: string, context: LDContext, options?: LDOptions): LDClient;
|
|
51
43
|
|
|
52
44
|
/**
|
|
53
45
|
* Initialization options for the LaunchDarkly client.
|
|
54
46
|
*/
|
|
55
47
|
export interface LDOptions extends LDOptionsBase {
|
|
56
48
|
/**
|
|
57
|
-
* Determines where flag values and anonymous
|
|
49
|
+
* Determines where flag values and anonymous context keys are cached in the filesystem. By
|
|
58
50
|
* default, this is the current directory.
|
|
59
51
|
*/
|
|
60
52
|
localStoragePath?: string;
|