launchdarkly-js-sdk-common 5.0.0-alpha.4 → 5.0.0-alpha.6

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "launchdarkly-js-sdk-common",
3
- "version": "5.0.0-alpha.4",
3
+ "version": "5.0.0-alpha.6",
4
4
  "description": "LaunchDarkly SDK for JavaScript - common code",
5
5
  "author": "LaunchDarkly <team@launchdarkly.com>",
6
6
  "license": "Apache-2.0",
@@ -1,11 +1,24 @@
1
+ const { getContextKinds } = require('./context');
2
+
3
+ function getKinds(event) {
4
+ if (event.context) {
5
+ return getContextKinds(event.context);
6
+ }
7
+ if (event.contextKeys) {
8
+ return Object.keys(event.contextKeys);
9
+ }
10
+ return [];
11
+ }
12
+
1
13
  function EventSummarizer() {
2
14
  const es = {};
3
15
 
4
16
  let startDate = 0,
5
17
  endDate = 0,
6
- counters = {};
18
+ counters = {},
19
+ contextKinds = {};
7
20
 
8
- es.summarizeEvent = function(event) {
21
+ es.summarizeEvent = event => {
9
22
  if (event.kind === 'feature') {
10
23
  const counterKey =
11
24
  event.key +
@@ -14,14 +27,21 @@ function EventSummarizer() {
14
27
  ':' +
15
28
  (event.version !== null && event.version !== undefined ? event.version : '');
16
29
  const counterVal = counters[counterKey];
30
+ let kinds = contextKinds[event.key];
31
+ if (!kinds) {
32
+ kinds = new Set();
33
+ contextKinds[event.key] = kinds;
34
+ }
35
+ getKinds(event).forEach(kind => kinds.add(kind));
36
+
17
37
  if (counterVal) {
18
38
  counterVal.count = counterVal.count + 1;
19
39
  } else {
20
40
  counters[counterKey] = {
21
41
  count: 1,
22
42
  key: event.key,
23
- variation: event.variation,
24
43
  version: event.version,
44
+ variation: event.variation,
25
45
  value: event.value,
26
46
  default: event.default,
27
47
  };
@@ -35,16 +55,16 @@ function EventSummarizer() {
35
55
  }
36
56
  };
37
57
 
38
- es.getSummary = function() {
58
+ es.getSummary = () => {
39
59
  const flagsOut = {};
40
60
  let empty = true;
41
- for (const i in counters) {
42
- const c = counters[i];
61
+ for (const c of Object.values(counters)) {
43
62
  let flag = flagsOut[c.key];
44
63
  if (!flag) {
45
64
  flag = {
46
65
  default: c.default,
47
66
  counters: [],
67
+ contextKinds: [...contextKinds[c.key]],
48
68
  };
49
69
  flagsOut[c.key] = flag;
50
70
  }
@@ -55,7 +75,7 @@ function EventSummarizer() {
55
75
  if (c.variation !== undefined && c.variation !== null) {
56
76
  counterOut.variation = c.variation;
57
77
  }
58
- if (c.version) {
78
+ if (c.version !== undefined && c.version !== null) {
59
79
  counterOut.version = c.version;
60
80
  } else {
61
81
  counterOut.unknown = true;
@@ -72,10 +92,11 @@ function EventSummarizer() {
72
92
  };
73
93
  };
74
94
 
75
- es.clearSummary = function() {
95
+ es.clearSummary = () => {
76
96
  startDate = 0;
77
97
  endDate = 0;
78
98
  counters = {};
99
+ contextKinds = {};
79
100
  };
80
101
 
81
102
  return es;
@@ -344,10 +344,12 @@ describe.each([
344
344
  expect(se.endDate).toEqual(2000);
345
345
  expect(se.features).toEqual({
346
346
  flagkey1: {
347
+ contextKinds: ['user'],
347
348
  default: 'default1',
348
349
  counters: [{ version: 11, variation: 1, value: 'value1', count: 1 }],
349
350
  },
350
351
  flagkey2: {
352
+ contextKinds: ['user'],
351
353
  default: 'default2',
352
354
  counters: [{ version: 22, variation: 1, value: 'value2', count: 1 }],
353
355
  },
@@ -39,7 +39,7 @@ describe('EventSummarizer', () => {
39
39
  creationDate: 1000,
40
40
  key: key,
41
41
  version: version,
42
- user: user,
42
+ context: user,
43
43
  variation: variation,
44
44
  value: value,
45
45
  default: defaultVal,
@@ -63,6 +63,7 @@ describe('EventSummarizer', () => {
63
63
  data.features.key1.counters.sort((a, b) => a.value - b.value);
64
64
  const expectedFeatures = {
65
65
  key1: {
66
+ contextKinds: ['user'],
66
67
  default: 111,
67
68
  counters: [
68
69
  { value: 100, variation: 1, version: 11, count: 2 },
@@ -70,10 +71,12 @@ describe('EventSummarizer', () => {
70
71
  ],
71
72
  },
72
73
  key2: {
74
+ contextKinds: ['user'],
73
75
  default: 222,
74
76
  counters: [{ value: 999, variation: 1, version: 22, count: 1 }],
75
77
  },
76
78
  badkey: {
79
+ contextKinds: ['user'],
77
80
  default: 333,
78
81
  counters: [{ value: 333, unknown: true, count: 1 }],
79
82
  },
@@ -94,10 +97,58 @@ describe('EventSummarizer', () => {
94
97
  data.features.key1.counters.sort((a, b) => a.value - b.value);
95
98
  const expectedFeatures = {
96
99
  key1: {
100
+ contextKinds: ['user'],
97
101
  default: 111,
98
102
  counters: [{ variation: 0, value: 100, version: 11, count: 1 }, { value: 111, version: 11, count: 2 }],
99
103
  },
100
104
  };
101
105
  expect(data.features).toEqual(expectedFeatures);
102
106
  });
107
+
108
+ it('includes keys from all kinds', () => {
109
+ const es = EventSummarizer();
110
+ const event1 = {
111
+ kind: 'feature',
112
+ creationDate: 1000,
113
+ key: 'key1',
114
+ version: 11,
115
+ context: { key: 'test' },
116
+ variation: 1,
117
+ value: 100,
118
+ default: 111,
119
+ };
120
+ const event2 = {
121
+ kind: 'feature',
122
+ creationDate: 1000,
123
+ key: 'key1',
124
+ version: 11,
125
+ context: { kind: 'org', key: 'test' },
126
+ variation: 1,
127
+ value: 100,
128
+ default: 111,
129
+ };
130
+ const event3 = {
131
+ kind: 'feature',
132
+ creationDate: 1000,
133
+ key: 'key1',
134
+ version: 11,
135
+ context: { kind: 'multi', bacon: { key: 'crispy' }, eggs: { key: 'scrambled' } },
136
+ variation: 1,
137
+ value: 100,
138
+ default: 111,
139
+ };
140
+ es.summarizeEvent(event1);
141
+ es.summarizeEvent(event2);
142
+ es.summarizeEvent(event3);
143
+ const data = es.getSummary();
144
+
145
+ const expectedFeatures = {
146
+ key1: {
147
+ default: 111,
148
+ counters: [{ variation: 1, value: 100, version: 11, count: 3 }],
149
+ contextKinds: ['user', 'org', 'bacon', 'eggs'],
150
+ },
151
+ };
152
+ expect(data.features).toEqual(expectedFeatures);
153
+ });
103
154
  });
package/src/context.js CHANGED
@@ -3,6 +3,8 @@
3
3
  * @param {string} kind
4
4
  * @returns true if the kind is valid.
5
5
  */
6
+ const { commonBasicLogger } = require('./loggers');
7
+
6
8
  function validKind(kind) {
7
9
  return typeof kind === 'string' && kind !== 'kind' && kind.match(/^(\w|\.|-)+$/);
8
10
  }
@@ -89,7 +91,7 @@ function getCanonicalKey(context) {
89
91
  }
90
92
  }
91
93
 
92
- function getContextKeys(context, logger) {
94
+ function getContextKeys(context, logger = commonBasicLogger()) {
93
95
  if (!context) {
94
96
  return undefined;
95
97
  }
@@ -111,10 +113,10 @@ function getContextKeys(context, logger) {
111
113
  });
112
114
  break;
113
115
  case null:
114
- logger?.warn(`null is not a valid context kind: ${context}`);
116
+ logger.warn(`null is not a valid context kind: ${context}`);
115
117
  break;
116
118
  case '':
117
- logger?.warn(`'' is not a valid context kind: ${context}`);
119
+ logger.warn(`'' is not a valid context kind: ${context}`);
118
120
  break;
119
121
  default:
120
122
  keys[kind] = `${key}`;
package/test-types.ts CHANGED
@@ -91,3 +91,5 @@ var flagSetValue: ld.LDFlagValue = flagSet['key'];
91
91
 
92
92
  client.close(() => {});
93
93
  client.close().then(() => {});
94
+
95
+ var contextKeys = ld.getContextKeys(user);
package/typings.d.ts CHANGED
@@ -984,6 +984,11 @@ declare module 'launchdarkly-js-sdk-common' {
984
984
  */
985
985
  export type LDLogLevel = 'debug' | 'info' | 'warn' | 'error' | 'none';
986
986
 
987
+ export function getContextKeys(
988
+ context: LDContext,
989
+ logger?: LDLogger,
990
+ ): {[attribute: string]: string} | undefined;
991
+
987
992
  /**
988
993
  * Callback interface for collecting information about the SDK at runtime.
989
994
  *