launchdarkly-js-sdk-common 5.0.0-alpha.6 → 5.0.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/.ldrelease/config.yml +2 -1
- package/package.json +1 -1
- package/src/EventProcessor.js +2 -1
- package/src/InspectorManager.js +7 -7
- package/src/__tests__/InspectorManager-test.js +8 -8
- package/src/__tests__/LDClient-inspectors-test.js +9 -9
- package/src/__tests__/PersistentFlagStore-test.js +4 -4
- package/src/__tests__/stubPlatform.js +2 -2
- package/src/__tests__/testUtils.js +1 -1
- package/src/__tests__/utils-test.js +2 -2
- package/src/context.js +1 -1
- package/src/diagnosticEvents.js +1 -1
- package/src/index.js +2 -2
- package/typings.d.ts +1 -2
package/.ldrelease/config.yml
CHANGED
package/package.json
CHANGED
package/src/EventProcessor.js
CHANGED
|
@@ -136,7 +136,8 @@ function EventProcessor(
|
|
|
136
136
|
}
|
|
137
137
|
queue = [];
|
|
138
138
|
logger.debug(messages.debugPostingEvents(eventsToSend.length));
|
|
139
|
-
return eventSender.sendEvents(eventsToSend, mainEventsUrl).then(
|
|
139
|
+
return eventSender.sendEvents(eventsToSend, mainEventsUrl).then(responses => {
|
|
140
|
+
const responseInfo = responses && responses[0];
|
|
140
141
|
if (responseInfo) {
|
|
141
142
|
if (responseInfo.serverTime) {
|
|
142
143
|
lastKnownPastTime = responseInfo.serverTime;
|
package/src/InspectorManager.js
CHANGED
|
@@ -57,12 +57,12 @@ function InspectorManager(inspectors, logger) {
|
|
|
57
57
|
*
|
|
58
58
|
* @param {string} flagKey The key for the flag.
|
|
59
59
|
* @param {Object} detail The LDEvaluationDetail for the flag.
|
|
60
|
-
* @param {Object}
|
|
60
|
+
* @param {Object} context The LDContext for the flag.
|
|
61
61
|
*/
|
|
62
|
-
manager.onFlagUsed = (flagKey, detail,
|
|
62
|
+
manager.onFlagUsed = (flagKey, detail, context) => {
|
|
63
63
|
if (inspectorsByType[InspectorTypes.flagUsed].length) {
|
|
64
64
|
onNextTick(() => {
|
|
65
|
-
inspectorsByType[InspectorTypes.flagUsed].forEach(inspector => inspector.method(flagKey, detail,
|
|
65
|
+
inspectorsByType[InspectorTypes.flagUsed].forEach(inspector => inspector.method(flagKey, detail, context));
|
|
66
66
|
});
|
|
67
67
|
}
|
|
68
68
|
};
|
|
@@ -99,16 +99,16 @@ function InspectorManager(inspectors, logger) {
|
|
|
99
99
|
};
|
|
100
100
|
|
|
101
101
|
/**
|
|
102
|
-
* Notify the registered inspectors that the
|
|
102
|
+
* Notify the registered inspectors that the context identity has changed.
|
|
103
103
|
*
|
|
104
104
|
* The notification itself will be dispatched asynchronously.
|
|
105
105
|
*
|
|
106
|
-
* @param {Object}
|
|
106
|
+
* @param {Object} context The `LDContext` which is now identified.
|
|
107
107
|
*/
|
|
108
|
-
manager.onIdentityChanged =
|
|
108
|
+
manager.onIdentityChanged = context => {
|
|
109
109
|
if (inspectorsByType[InspectorTypes.clientIdentityChanged].length) {
|
|
110
110
|
onNextTick(() => {
|
|
111
|
-
inspectorsByType[InspectorTypes.clientIdentityChanged].forEach(inspector => inspector.method(
|
|
111
|
+
inspectorsByType[InspectorTypes.clientIdentityChanged].forEach(inspector => inspector.method(context));
|
|
112
112
|
});
|
|
113
113
|
}
|
|
114
114
|
};
|
|
@@ -39,16 +39,16 @@ describe('given an inspector with callbacks of every type', () => {
|
|
|
39
39
|
{
|
|
40
40
|
type: 'flag-used',
|
|
41
41
|
name: 'my-flag-used-inspector',
|
|
42
|
-
method: (flagKey, flagDetail,
|
|
43
|
-
eventQueue.add({ type: 'flag-used', flagKey, flagDetail,
|
|
42
|
+
method: (flagKey, flagDetail, context) => {
|
|
43
|
+
eventQueue.add({ type: 'flag-used', flagKey, flagDetail, context });
|
|
44
44
|
},
|
|
45
45
|
},
|
|
46
46
|
// 'flag-used registered twice.
|
|
47
47
|
{
|
|
48
48
|
type: 'flag-used',
|
|
49
49
|
name: 'my-other-flag-used-inspector',
|
|
50
|
-
method: (flagKey, flagDetail,
|
|
51
|
-
eventQueue.add({ type: 'flag-used', flagKey, flagDetail,
|
|
50
|
+
method: (flagKey, flagDetail, context) => {
|
|
51
|
+
eventQueue.add({ type: 'flag-used', flagKey, flagDetail, context });
|
|
52
52
|
},
|
|
53
53
|
},
|
|
54
54
|
{
|
|
@@ -75,10 +75,10 @@ describe('given an inspector with callbacks of every type', () => {
|
|
|
75
75
|
{
|
|
76
76
|
type: 'client-identity-changed',
|
|
77
77
|
name: 'my-identity-inspector',
|
|
78
|
-
method:
|
|
78
|
+
method: context => {
|
|
79
79
|
eventQueue.add({
|
|
80
80
|
type: 'client-identity-changed',
|
|
81
|
-
|
|
81
|
+
context,
|
|
82
82
|
});
|
|
83
83
|
},
|
|
84
84
|
},
|
|
@@ -137,7 +137,7 @@ describe('given an inspector with callbacks of every type', () => {
|
|
|
137
137
|
kind: 'OFF',
|
|
138
138
|
},
|
|
139
139
|
},
|
|
140
|
-
|
|
140
|
+
context: { key: 'test-key' },
|
|
141
141
|
};
|
|
142
142
|
const event1 = await eventQueue.take();
|
|
143
143
|
expect(event1).toMatchObject(expectedEvent);
|
|
@@ -180,7 +180,7 @@ describe('given an inspector with callbacks of every type', () => {
|
|
|
180
180
|
const event = await eventQueue.take();
|
|
181
181
|
expect(event).toMatchObject({
|
|
182
182
|
type: 'client-identity-changed',
|
|
183
|
-
|
|
183
|
+
context: { key: 'the-key' },
|
|
184
184
|
});
|
|
185
185
|
});
|
|
186
186
|
});
|
|
@@ -3,7 +3,7 @@ const { respondJson } = require('./mockHttp');
|
|
|
3
3
|
const stubPlatform = require('./stubPlatform');
|
|
4
4
|
|
|
5
5
|
const envName = 'UNKNOWN_ENVIRONMENT_ID';
|
|
6
|
-
const
|
|
6
|
+
const context = { key: 'context-key' };
|
|
7
7
|
|
|
8
8
|
describe('given a streaming client with registered inspectors', () => {
|
|
9
9
|
const eventQueue = new AsyncQueue();
|
|
@@ -11,15 +11,15 @@ describe('given a streaming client with registered inspectors', () => {
|
|
|
11
11
|
const inspectors = [
|
|
12
12
|
{
|
|
13
13
|
type: 'flag-used',
|
|
14
|
-
method: (flagKey, flagDetail,
|
|
15
|
-
eventQueue.add({ type: 'flag-used', flagKey, flagDetail,
|
|
14
|
+
method: (flagKey, flagDetail, context) => {
|
|
15
|
+
eventQueue.add({ type: 'flag-used', flagKey, flagDetail, context });
|
|
16
16
|
},
|
|
17
17
|
},
|
|
18
18
|
// 'flag-used registered twice.
|
|
19
19
|
{
|
|
20
20
|
type: 'flag-used',
|
|
21
|
-
method: (flagKey, flagDetail,
|
|
22
|
-
eventQueue.add({ type: 'flag-used', flagKey, flagDetail,
|
|
21
|
+
method: (flagKey, flagDetail, context) => {
|
|
22
|
+
eventQueue.add({ type: 'flag-used', flagKey, flagDetail, context });
|
|
23
23
|
},
|
|
24
24
|
},
|
|
25
25
|
{
|
|
@@ -43,10 +43,10 @@ describe('given a streaming client with registered inspectors', () => {
|
|
|
43
43
|
},
|
|
44
44
|
{
|
|
45
45
|
type: 'client-identity-changed',
|
|
46
|
-
method:
|
|
46
|
+
method: context => {
|
|
47
47
|
eventQueue.add({
|
|
48
48
|
type: 'client-identity-changed',
|
|
49
|
-
|
|
49
|
+
context,
|
|
50
50
|
});
|
|
51
51
|
},
|
|
52
52
|
},
|
|
@@ -60,7 +60,7 @@ describe('given a streaming client with registered inspectors', () => {
|
|
|
60
60
|
const server = platform.testing.http.newServer();
|
|
61
61
|
server.byDefault(respondJson({}));
|
|
62
62
|
const config = { streaming: true, baseUrl: server.url, inspectors, sendEvents: false };
|
|
63
|
-
client = platform.testing.makeClient(envName,
|
|
63
|
+
client = platform.testing.makeClient(envName, context, config);
|
|
64
64
|
await client.waitUntilReady();
|
|
65
65
|
});
|
|
66
66
|
|
|
@@ -81,7 +81,7 @@ describe('given a streaming client with registered inspectors', () => {
|
|
|
81
81
|
const ident = await eventQueue.take();
|
|
82
82
|
expect(ident).toMatchObject({
|
|
83
83
|
type: 'client-identity-changed',
|
|
84
|
-
|
|
84
|
+
context,
|
|
85
85
|
});
|
|
86
86
|
const flagsEvent = await eventQueue.take();
|
|
87
87
|
expect(flagsEvent).toMatchObject({
|
|
@@ -7,10 +7,10 @@ import PersistentStorage from '../PersistentStorage';
|
|
|
7
7
|
import * as utils from '../utils';
|
|
8
8
|
|
|
9
9
|
describe('PersistentFlagStore', () => {
|
|
10
|
-
const
|
|
11
|
-
const ident = Identity(
|
|
10
|
+
const context = { key: 'context-key', kind: 'user' };
|
|
11
|
+
const ident = Identity(context);
|
|
12
12
|
const env = 'ENVIRONMENT';
|
|
13
|
-
const lsKey = 'ld:' + env + ':' + utils.btoa(JSON.stringify(
|
|
13
|
+
const lsKey = 'ld:' + env + ':' + utils.btoa(JSON.stringify(context));
|
|
14
14
|
|
|
15
15
|
it('stores flags', async () => {
|
|
16
16
|
const platform = stubPlatform.defaults();
|
|
@@ -73,7 +73,7 @@ describe('PersistentFlagStore', () => {
|
|
|
73
73
|
expect(platform.testing.getLocalStorageImmediately(lsKey)).toBe(undefined);
|
|
74
74
|
});
|
|
75
75
|
|
|
76
|
-
it('uses hash, if present, instead of
|
|
76
|
+
it('uses hash, if present, instead of context properties', async () => {
|
|
77
77
|
const platform = stubPlatform.defaults();
|
|
78
78
|
const storage = PersistentStorage(platform.localStorage, platform.testing.logger);
|
|
79
79
|
const hash = '12345';
|
|
@@ -83,14 +83,14 @@ export function defaults() {
|
|
|
83
83
|
|
|
84
84
|
eventSourcesCreated,
|
|
85
85
|
|
|
86
|
-
makeClient: (env,
|
|
86
|
+
makeClient: (env, context, options = {}) => {
|
|
87
87
|
const config = { logger: p.testing.logger, ...options };
|
|
88
88
|
// We want to simulate what the platform-specific SDKs will do in their own initialization functions.
|
|
89
89
|
// They will call the common package's LDClient.initialize() and receive the clientVars object which
|
|
90
90
|
// contains both the underlying client (in its "client" property) and some internal methods that the
|
|
91
91
|
// platform-specific SDKs can use to do internal stuff. One of those is start(), which they will
|
|
92
92
|
// call after doing any other initialization things they may need to do.
|
|
93
|
-
const clientVars = LDClient.initialize(env,
|
|
93
|
+
const clientVars = LDClient.initialize(env, context, config, p);
|
|
94
94
|
clientVars.start();
|
|
95
95
|
return clientVars.client;
|
|
96
96
|
},
|
|
@@ -62,7 +62,7 @@ export function MockEventSender() {
|
|
|
62
62
|
calls,
|
|
63
63
|
sendEvents: (events, url) => {
|
|
64
64
|
calls.add({ events, url });
|
|
65
|
-
return Promise.resolve({ serverTime, status });
|
|
65
|
+
return Promise.resolve([{ serverTime, status }]);
|
|
66
66
|
},
|
|
67
67
|
setServerTime: time => {
|
|
68
68
|
serverTime = time;
|
|
@@ -66,8 +66,8 @@ describe('utils', () => {
|
|
|
66
66
|
|
|
67
67
|
describe('chunkEventsForUrl', () => {
|
|
68
68
|
it('should properly chunk the list of events', () => {
|
|
69
|
-
const
|
|
70
|
-
const event = { kind: 'identify', key:
|
|
69
|
+
const context = { key: 'foo', kind: 'user' };
|
|
70
|
+
const event = { kind: 'identify', key: context.key };
|
|
71
71
|
const eventLength = base64URLEncode(JSON.stringify(event)).length;
|
|
72
72
|
const events = [event, event, event, event, event];
|
|
73
73
|
const chunks = chunkEventsForUrl(eventLength * 2, events);
|
package/src/context.js
CHANGED
package/src/diagnosticEvents.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
const { v1: uuidv1 } = require('uuid');
|
|
2
2
|
// Note that in the diagnostic events spec, these IDs are to be generated with UUID v4. However,
|
|
3
|
-
// in JS we were already using v1 for unique
|
|
3
|
+
// in JS we were already using v1 for unique context keys, so to avoid bringing in two packages we
|
|
4
4
|
// will use v1 here as well.
|
|
5
5
|
|
|
6
6
|
const { baseOptionDefs } = require('./configuration');
|
package/src/index.js
CHANGED
|
@@ -189,8 +189,8 @@ function initialize(env, context, specifiedOptions, platform, extraOptionDefs) {
|
|
|
189
189
|
}
|
|
190
190
|
}
|
|
191
191
|
|
|
192
|
-
function onIdentifyChange(
|
|
193
|
-
sendIdentifyEvent(
|
|
192
|
+
function onIdentifyChange(context) {
|
|
193
|
+
sendIdentifyEvent(context);
|
|
194
194
|
notifyInspectionIdentityChanged();
|
|
195
195
|
}
|
|
196
196
|
|
package/typings.d.ts
CHANGED
|
@@ -327,8 +327,7 @@ declare module 'launchdarkly-js-sdk-common' {
|
|
|
327
327
|
*
|
|
328
328
|
* This is a metadata property, rather than an attribute that can be addressed in evaluations:
|
|
329
329
|
* that is, a rule clause that references the attribute name "privateAttributes", will not use
|
|
330
|
-
* this value, but
|
|
331
|
-
* method such as SetString.
|
|
330
|
+
* this value, but would use a "privateAttributes" attribute set on the context.
|
|
332
331
|
*/
|
|
333
332
|
privateAttributes?: string[];
|
|
334
333
|
}
|