launchdarkly-js-sdk-common 4.3.1 → 5.0.0-alpha.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/CHANGELOG.md CHANGED
@@ -2,6 +2,10 @@
2
2
 
3
3
  All notable changes to the `launchdarkly-js-sdk-common` package will be documented in this file. Changes that affect the dependent SDKs such as `launchdarkly-js-client-sdk` should also be logged in those projects, in the next release that uses the updated version of this package. This project adheres to [Semantic Versioning](http://semver.org).
4
4
 
5
+ ## [4.3.1] - 2022-10-17
6
+ ### Fixed:
7
+ - Fixed an issue that prevented the `flag-used` inspector from being called.
8
+
5
9
  ## [4.3.0] - 2022-10-17
6
10
  ### Added:
7
11
  - Added support for `Inspectors` that can be used for collecting information for monitoring, analytics, and debugging.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "launchdarkly-js-sdk-common",
3
- "version": "4.3.1",
3
+ "version": "5.0.0-alpha.1",
4
4
  "description": "LaunchDarkly SDK for JavaScript - common code",
5
5
  "author": "LaunchDarkly <team@launchdarkly.com>",
6
6
  "license": "Apache-2.0",
@@ -9,6 +9,7 @@
9
9
  "scripts": {
10
10
  "lint": "eslint --format 'node_modules/eslint-formatter-pretty' --ignore-path .eslintignore",
11
11
  "lint:all": "eslint --format 'node_modules/eslint-formatter-pretty' --ignore-path .eslintignore src",
12
+ "lint-fix:all": "eslint --fix --format 'node_modules/eslint-formatter-pretty' --ignore-path .eslintignore src",
12
13
  "format": "npm run format:md && npm run format:js",
13
14
  "format:md": "prettier --parser markdown --ignore-path .prettierignore --write '*.md'",
14
15
  "format:js": "prettier --ignore-path .prettierignore --write 'src/**/*.js'",
@@ -26,6 +27,7 @@
26
27
  "@babel/preset-env": "^7.6.3",
27
28
  "@babel/runtime": "7.6.3",
28
29
  "@rollup/plugin-replace": "^2.2.0",
30
+ "@types/jest": "^27.4.1",
29
31
  "babel-eslint": "^10.1.0",
30
32
  "babel-jest": "^25.1.0",
31
33
  "cross-env": "^5.1.4",
@@ -0,0 +1,95 @@
1
+ const { v1: uuidv1 } = require('uuid');
2
+ const { getContextKinds } = require('./context');
3
+
4
+ const errors = require('./errors');
5
+ const messages = require('./messages');
6
+ const utils = require('./utils');
7
+
8
+ const ldUserIdKey = 'ld:$anonUserId';
9
+
10
+ /**
11
+ * Create an object which can process a context and populate any required keys
12
+ * for anonymous objects.
13
+ *
14
+ * @param {Object} persistentStorage The persistent storage from which to store
15
+ * and access persisted anonymous context keys.
16
+ * @returns An AnonymousContextProcessor.
17
+ */
18
+ function AnonymousContextProcessor(persistentStorage) {
19
+ function getContextKeyIdString(kind) {
20
+ if (kind === undefined || kind === null || kind === 'user') {
21
+ return ldUserIdKey;
22
+ }
23
+ return `ld:$contextKey:${kind}`;
24
+ }
25
+
26
+ function getCachedContextKey(kind) {
27
+ return persistentStorage.get(getContextKeyIdString(kind));
28
+ }
29
+
30
+ function setCachedContextKey(id, kind) {
31
+ return persistentStorage.set(getContextKeyIdString(kind), id);
32
+ }
33
+
34
+ /**
35
+ * Process a single kind context, or a single context within a multi-kind context.
36
+ * @param {string} kind The kind of the context. Independent because the kind is not prevent
37
+ * within a context in a multi-kind context.
38
+ * @param {Object} context
39
+ * @returns {Promise} a promise that resolves to a processed contexts, or rejects
40
+ * a context which cannot be processed.
41
+ */
42
+ function processSingleKindContext(kind, context) {
43
+ // We are working on a copy of an original context, so we want to re-assign
44
+ // versus duplicating it again.
45
+
46
+ /* eslint-disable no-param-reassign */
47
+ if (context.key !== null && context.key !== undefined) {
48
+ context.key = context.key.toString();
49
+ return Promise.resolve(context);
50
+ }
51
+
52
+ if (context.anonymous) {
53
+ // If the key doesn't exist, then the persistent storage will resolve
54
+ // with undefined.
55
+ return getCachedContextKey(kind).then(cachedId => {
56
+ if (cachedId) {
57
+ context.key = cachedId;
58
+ return context;
59
+ } else {
60
+ const id = uuidv1();
61
+ context.key = id;
62
+ return setCachedContextKey(id, kind).then(() => context);
63
+ }
64
+ });
65
+ } else {
66
+ return Promise.reject(new errors.LDInvalidUserError(messages.invalidContext()));
67
+ }
68
+ /* eslint-enable no-param-reassign */
69
+ }
70
+
71
+ /**
72
+ * Process the context, returning a Promise that resolves to the processed context, or rejects if there is an error.
73
+ * @param {Object} context
74
+ * @returns {Promise} A promise which resolves to a processed context, or a rejection if the context cannot be
75
+ * processed. The context should still be checked for overall validity after being processed.
76
+ */
77
+ this.processContext = context => {
78
+ if (!context) {
79
+ return Promise.reject(new errors.LDInvalidUserError(messages.contextNotSpecified()));
80
+ }
81
+
82
+ const processedContext = utils.clone(context);
83
+
84
+ if (context.kind === 'multi') {
85
+ const kinds = getContextKinds(processedContext);
86
+
87
+ return Promise.all(kinds.map(kind => processSingleKindContext(kind, processedContext[kind]))).then(
88
+ () => processedContext
89
+ );
90
+ }
91
+ return processSingleKindContext(context.kind, processedContext);
92
+ };
93
+ }
94
+
95
+ module.exports = AnonymousContextProcessor;
@@ -0,0 +1,147 @@
1
+ const AttributeReference = require('./attributeReference');
2
+
3
+ function ContextFilter(config) {
4
+ const filter = {};
5
+
6
+ const allAttributesPrivate = config.allAttributesPrivate;
7
+ const privateAttributes = config.privateAttributes || [];
8
+
9
+ // These attributes cannot be removed via a private attribute.
10
+ const protectedAttributes = ['key', 'kind', '_meta', 'anonymous'];
11
+
12
+ const legacyTopLevelCopyAttributes = ['name', 'ip', 'firstName', 'lastName', 'email', 'avatar', 'country'];
13
+
14
+ /**
15
+ * For the given context and configuration get a list of attributes to filter.
16
+ * @param {Object} context
17
+ * @returns {string[]} A list of the attributes to filter.
18
+ */
19
+ const getAttributesToFilter = context =>
20
+ (allAttributesPrivate
21
+ ? Object.keys(context)
22
+ : [...privateAttributes, ...((context._meta && context._meta.privateAttributes) || [])]
23
+ ).filter(attr => !protectedAttributes.some(protectedAttr => AttributeReference.compare(attr, protectedAttr)));
24
+
25
+ /**
26
+ * @param {Object} context
27
+ * @returns {Object} A copy of the context with private attributes removed,
28
+ * and the redactedAttributes meta populated.
29
+ */
30
+ const filterSingleKind = context => {
31
+ if (typeof context !== 'object' || context === null || Array.isArray(context)) {
32
+ return undefined;
33
+ }
34
+
35
+ const { cloned, excluded } = AttributeReference.cloneExcluding(context, getAttributesToFilter(context));
36
+ cloned.key = String(cloned.key);
37
+ if (excluded.length) {
38
+ if (!cloned._meta) {
39
+ cloned._meta = {};
40
+ }
41
+ cloned._meta.redactedAttributes = excluded;
42
+ }
43
+ if (cloned._meta) {
44
+ if (cloned._meta.secondary === null) {
45
+ delete cloned._meta.secondary;
46
+ }
47
+ if (cloned._meta.secondary !== undefined) {
48
+ cloned._meta.secondary = String(cloned._meta.secondary);
49
+ }
50
+ delete cloned._meta['privateAttributes'];
51
+ if (Object.keys(cloned._meta).length === 0) {
52
+ delete cloned._meta;
53
+ }
54
+ }
55
+ // Make sure anonymous is boolean if present.
56
+ // Null counts as present, and would be falsy, which is the default.
57
+ if (cloned.anonymous !== undefined) {
58
+ cloned.anonymous = !!cloned.anonymous;
59
+ }
60
+
61
+ return cloned;
62
+ };
63
+
64
+ /**
65
+ * @param {Object} context
66
+ * @returns {Object} A copy of the context with the private attributes removed,
67
+ * and the redactedAttributes meta populated for each sub-context.
68
+ */
69
+ const filterMultiKind = context => {
70
+ const filtered = {
71
+ kind: context.kind,
72
+ };
73
+ const contextKeys = Object.keys(context);
74
+
75
+ for (const contextKey of contextKeys) {
76
+ if (contextKey !== 'kind') {
77
+ const filteredContext = filterSingleKind(context[contextKey]);
78
+ if (filteredContext) {
79
+ filtered[contextKey] = filteredContext;
80
+ }
81
+ }
82
+ }
83
+ return filtered;
84
+ };
85
+
86
+ /**
87
+ * Convert the LDUser object into an LDContext object.
88
+ * @param {Object} user The LDUser to produce an LDContext for.
89
+ * @returns {Object} A single kind context based on the provided user.
90
+ */
91
+ const legacyToSingleKind = user => {
92
+ const filtered = {
93
+ /* Destructure custom items into the top level.
94
+ Duplicate keys will be overridden by previously
95
+ top level items.
96
+ */
97
+ ...(user.custom || {}),
98
+
99
+ // Implicity a user kind.
100
+ kind: 'user',
101
+
102
+ key: user.key,
103
+ };
104
+
105
+ if (user.anonymous !== undefined) {
106
+ filtered.anonymous = !!user.anonymous;
107
+ }
108
+
109
+ // Copy top level keys and convert them to strings.
110
+ // Remove keys that may have been destructured from `custom`.
111
+ for (const key of legacyTopLevelCopyAttributes) {
112
+ delete filtered[key];
113
+ if (user[key] !== undefined && user[key] !== null) {
114
+ filtered[key] = String(user[key]);
115
+ }
116
+ }
117
+
118
+ if (user.privateAttributeNames !== undefined && user.privateAttributeNames !== null) {
119
+ filtered._meta = filtered._meta || {};
120
+ // If any private attributes started with '/' we need to convert them to references, otherwise the '/' will
121
+ // cause the literal to incorrectly be treated as a reference.
122
+ filtered._meta.privateAttributes = user.privateAttributeNames.map(
123
+ literal => (literal.startsWith('/') ? AttributeReference.literalToReference(literal) : literal)
124
+ );
125
+ }
126
+ if (user.secondary !== undefined && user.secondary !== null) {
127
+ filtered._meta = filtered._meta || {};
128
+ filtered._meta.secondary = String(user.secondary);
129
+ }
130
+
131
+ return filtered;
132
+ };
133
+
134
+ filter.filter = context => {
135
+ if (context.kind === undefined || context.kind === null) {
136
+ return filterSingleKind(legacyToSingleKind(context));
137
+ } else if (context.kind === 'multi') {
138
+ return filterMultiKind(context);
139
+ } else {
140
+ return filterSingleKind(context);
141
+ }
142
+ };
143
+
144
+ return filter;
145
+ }
146
+
147
+ module.exports = ContextFilter;
@@ -1,6 +1,6 @@
1
1
  const EventSender = require('./EventSender');
2
2
  const EventSummarizer = require('./EventSummarizer');
3
- const UserFilter = require('./UserFilter');
3
+ const ContextFilter = require('./ContextFilter');
4
4
  const errors = require('./errors');
5
5
  const messages = require('./messages');
6
6
  const utils = require('./utils');
@@ -17,8 +17,7 @@ function EventProcessor(
17
17
  const eventSender = sender || EventSender(platform, environmentId, options);
18
18
  const mainEventsUrl = utils.appendUrlPath(options.eventsUrl, '/events/bulk/' + environmentId);
19
19
  const summarizer = EventSummarizer();
20
- const userFilter = UserFilter(options);
21
- const inlineUsers = options.inlineUsersInEvents;
20
+ const contextFilter = ContextFilter(options);
22
21
  const samplingInterval = options.samplingInterval;
23
22
  const eventCapacity = options.eventCapacity;
24
23
  const flushInterval = options.flushInterval;
@@ -47,16 +46,12 @@ function EventProcessor(
47
46
  // Transform an event from its internal format to the format we use when sending a payload.
48
47
  function makeOutputEvent(e) {
49
48
  const ret = utils.extend({}, e);
50
- if (e.kind === 'alias') {
51
- // alias events do not require any transformation
52
- return ret;
53
- }
54
- if (inlineUsers || e.kind === 'identify') {
55
- // identify events always have an inline user
56
- ret.user = userFilter.filterUser(e.user);
49
+ if (e.kind === 'identify') {
50
+ // identify events always have an inline context
51
+ ret.context = contextFilter.filter(e.context);
57
52
  } else {
58
- ret.userKey = e.user.key;
59
- delete ret['user'];
53
+ ret.contextKeys = getContextKeys(e);
54
+ delete ret['context'];
60
55
  }
61
56
  if (e.kind === 'feature') {
62
57
  delete ret['trackEvents'];
@@ -65,6 +60,28 @@ function EventProcessor(
65
60
  return ret;
66
61
  }
67
62
 
63
+ function getContextKeys(event) {
64
+ const keys = {};
65
+ const context = event.context;
66
+ if (context !== undefined) {
67
+ if (context.kind === undefined) {
68
+ keys.user = String(context.key);
69
+ } else if (context.kind === 'multi') {
70
+ Object.entries(context)
71
+ .filter(([key]) => key !== 'kind')
72
+ .forEach(([key, value]) => {
73
+ if (value !== undefined && value.key !== undefined) {
74
+ keys[key] = value.key;
75
+ }
76
+ });
77
+ } else {
78
+ keys[context.kind] = String(context.key);
79
+ }
80
+ return keys;
81
+ }
82
+ return undefined;
83
+ }
84
+
68
85
  function addToOutbox(event) {
69
86
  if (queue.length < eventCapacity) {
70
87
  queue.push(event);
@@ -107,7 +124,7 @@ function EventProcessor(
107
124
  }
108
125
  if (addDebugEvent) {
109
126
  const debugEvent = utils.extend({}, event, { kind: 'debug' });
110
- debugEvent.user = userFilter.filterUser(debugEvent.user);
127
+ debugEvent.context = contextFilter.filter(debugEvent.context);
111
128
  delete debugEvent['trackEvents'];
112
129
  delete debugEvent['debugEventsUntilDate'];
113
130
  addToOutbox(debugEvent);
@@ -73,7 +73,7 @@ function EventSender(platform, environmentId, options) {
73
73
  // no need to break up events into chunks if we can send a POST
74
74
  chunks = [events];
75
75
  } else {
76
- chunks = utils.chunkUserEventsForUrl(MAX_URL_LENGTH - url.length, events);
76
+ chunks = utils.chunkEventsForUrl(MAX_URL_LENGTH - url.length, events);
77
77
  }
78
78
  const results = [];
79
79
  for (let i = 0; i < chunks.length; i++) {
package/src/Identity.js CHANGED
@@ -1,23 +1,22 @@
1
1
  const utils = require('./utils');
2
2
 
3
- function Identity(initialUser, onChange) {
3
+ function Identity(initialContext, onChange) {
4
4
  const ident = {};
5
- let user;
5
+ let context;
6
6
 
7
- ident.setUser = function(u) {
8
- const previousUser = user && utils.clone(user);
9
- user = utils.sanitizeUser(u);
10
- if (user && onChange) {
11
- onChange(utils.clone(user), previousUser);
7
+ ident.setContext = function(c) {
8
+ context = utils.sanitizeContext(c);
9
+ if (context && onChange) {
10
+ onChange(utils.clone(context));
12
11
  }
13
12
  };
14
13
 
15
- ident.getUser = function() {
16
- return user ? utils.clone(user) : null;
14
+ ident.getContext = function() {
15
+ return context ? utils.clone(context) : null;
17
16
  };
18
17
 
19
- if (initialUser) {
20
- ident.setUser(initialUser);
18
+ if (initialContext) {
19
+ ident.setContext(initialContext);
21
20
  }
22
21
 
23
22
  return ident;
@@ -5,9 +5,9 @@ function PersistentFlagStore(storage, environment, hash, ident) {
5
5
 
6
6
  function getFlagsKey() {
7
7
  let key = '';
8
- const user = ident.getUser();
9
- if (user) {
10
- key = hash || utils.btoa(JSON.stringify(user));
8
+ const context = ident.getContext();
9
+ if (context) {
10
+ key = hash || utils.btoa(JSON.stringify(context));
11
11
  }
12
12
  return 'ld:' + environment + ':' + key;
13
13
  }
package/src/Requestor.js CHANGED
@@ -79,9 +79,9 @@ function Requestor(platform, options, environment) {
79
79
  return fetchJSON(utils.appendUrlPath(baseUrl, path), null);
80
80
  };
81
81
 
82
- // Requests the current state of all flags for the given user from LaunchDarkly. Returns a Promise
82
+ // Requests the current state of all flags for the given context from LaunchDarkly. Returns a Promise
83
83
  // which will resolve with the parsed JSON response, or will be rejected if the request failed.
84
- requestor.fetchFlagSettings = function(user, hash) {
84
+ requestor.fetchFlagSettings = function(context, hash) {
85
85
  let data;
86
86
  let endpoint;
87
87
  let query = '';
@@ -89,9 +89,9 @@ function Requestor(platform, options, environment) {
89
89
 
90
90
  if (useReport) {
91
91
  endpoint = [baseUrl, '/sdk/evalx/', environment, '/user'].join('');
92
- body = JSON.stringify(user);
92
+ body = JSON.stringify(context);
93
93
  } else {
94
- data = utils.base64URLEncode(JSON.stringify(user));
94
+ data = utils.base64URLEncode(JSON.stringify(context));
95
95
  endpoint = [baseUrl, '/sdk/evalx/', environment, '/users/', data].join('');
96
96
  }
97
97
  if (hash) {
package/src/Stream.js CHANGED
@@ -1,6 +1,7 @@
1
1
  const messages = require('./messages');
2
2
  const { appendUrlPath, base64URLEncode, objectHasOwnProperty } = require('./utils');
3
3
  const { getLDHeaders, transformHeaders } = require('./headers');
4
+ const { isHttpErrorRecoverable } = require('./errors');
4
5
 
5
6
  // The underlying event source implementation is abstracted via the platform object, which should
6
7
  // have these three properties:
@@ -16,6 +17,8 @@ const { getLDHeaders, transformHeaders } = require('./headers');
16
17
  // interval between heartbeats from the LaunchDarkly streaming server. If this amount of time elapses
17
18
  // with no new data, the connection will be cycled.
18
19
  const streamReadTimeoutMillis = 5 * 60 * 1000; // 5 minutes
20
+ const maxRetryDelay = 30 * 1000; // Maximum retry delay 30 seconds.
21
+ const jitterRatio = 0.5; // Delay should be 50%-100% of calculated time.
19
22
 
20
23
  function Stream(platform, config, environment, diagnosticsAccumulator) {
21
24
  const baseUrl = config.streamUrl;
@@ -24,18 +27,34 @@ function Stream(platform, config, environment, diagnosticsAccumulator) {
24
27
  const evalUrlPrefix = appendUrlPath(baseUrl, '/eval/' + environment);
25
28
  const useReport = config.useReport;
26
29
  const withReasons = config.evaluationReasons;
27
- const streamReconnectDelay = config.streamReconnectDelay;
30
+ const baseReconnectDelay = config.streamReconnectDelay;
28
31
  const headers = getLDHeaders(platform, config);
29
32
  let firstConnectionErrorLogged = false;
30
33
  let es = null;
31
34
  let reconnectTimeoutReference = null;
32
35
  let connectionAttemptStartTime;
33
- let user = null;
36
+ let context = null;
34
37
  let hash = null;
35
38
  let handlers = null;
39
+ let retryCount = 0;
36
40
 
37
- stream.connect = function(newUser, newHash, newHandlers) {
38
- user = newUser;
41
+ function backoff() {
42
+ const delay = baseReconnectDelay * Math.pow(2, retryCount);
43
+ return delay > maxRetryDelay ? maxRetryDelay : delay;
44
+ }
45
+
46
+ function jitter(computedDelayMillis) {
47
+ return computedDelayMillis - Math.trunc(Math.random() * jitterRatio * computedDelayMillis);
48
+ }
49
+
50
+ function getNextRetryDelay() {
51
+ const delay = jitter(backoff());
52
+ retryCount += 1;
53
+ return delay;
54
+ }
55
+
56
+ stream.connect = function(newContext, newHash, newHandlers) {
57
+ context = newContext;
39
58
  hash = newHash;
40
59
  handlers = {};
41
60
  for (const key in newHandlers || {}) {
@@ -63,13 +82,31 @@ function Stream(platform, config, environment, diagnosticsAccumulator) {
63
82
  };
64
83
 
65
84
  function handleError(err) {
85
+ // The event source may not produce a status. But the LaunchDarkly
86
+ // polyfill can. If we can get the status, then we should stop retrying
87
+ // on certain error codes.
88
+ if (err.status && typeof err.status === 'number' && !isHttpErrorRecoverable(err.status)) {
89
+ // If we encounter an unrecoverable condition, then we do not want to
90
+ // retry anymore.
91
+ closeConnection();
92
+ logger.error(messages.unrecoverableStreamError(err));
93
+ // Ensure any pending retry attempts are not done.
94
+ if (reconnectTimeoutReference) {
95
+ clearTimeout(reconnectTimeoutReference);
96
+ reconnectTimeoutReference = null;
97
+ }
98
+ return;
99
+ }
100
+
101
+ const delay = getNextRetryDelay();
102
+
66
103
  if (!firstConnectionErrorLogged) {
67
- logger.warn(messages.streamError(err, streamReconnectDelay));
104
+ logger.warn(messages.streamError(err, delay));
68
105
  firstConnectionErrorLogged = true;
69
106
  }
70
107
  logConnectionResult(false);
71
108
  closeConnection();
72
- tryConnect(streamReconnectDelay);
109
+ tryConnect(delay);
73
110
  }
74
111
 
75
112
  function tryConnect(delay) {
@@ -96,14 +133,14 @@ function Stream(platform, config, environment, diagnosticsAccumulator) {
96
133
  url = evalUrlPrefix;
97
134
  options.method = 'REPORT';
98
135
  options.headers['Content-Type'] = 'application/json';
99
- options.body = JSON.stringify(user);
136
+ options.body = JSON.stringify(context);
100
137
  } else {
101
138
  // if we can't do REPORT, fall back to the old ping-based stream
102
139
  url = appendUrlPath(baseUrl, '/ping/' + environment);
103
140
  query = '';
104
141
  }
105
142
  } else {
106
- url = evalUrlPrefix + '/' + base64URLEncode(JSON.stringify(user));
143
+ url = evalUrlPrefix + '/' + base64URLEncode(JSON.stringify(context));
107
144
  }
108
145
  options.headers = transformHeaders(options.headers, config);
109
146
  if (withReasons) {
@@ -123,6 +160,11 @@ function Stream(platform, config, environment, diagnosticsAccumulator) {
123
160
  }
124
161
 
125
162
  es.onerror = handleError;
163
+
164
+ es.onopen = () => {
165
+ // If the connection is a success, then reset the retryCount.
166
+ retryCount = 0;
167
+ };
126
168
  }
127
169
  }
128
170