launchdarkly-js-sdk-common 4.3.2 → 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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "launchdarkly-js-sdk-common",
3
- "version": "4.3.2",
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
@@ -33,7 +33,7 @@ function Stream(platform, config, environment, diagnosticsAccumulator) {
33
33
  let es = null;
34
34
  let reconnectTimeoutReference = null;
35
35
  let connectionAttemptStartTime;
36
- let user = null;
36
+ let context = null;
37
37
  let hash = null;
38
38
  let handlers = null;
39
39
  let retryCount = 0;
@@ -53,8 +53,8 @@ function Stream(platform, config, environment, diagnosticsAccumulator) {
53
53
  return delay;
54
54
  }
55
55
 
56
- stream.connect = function(newUser, newHash, newHandlers) {
57
- user = newUser;
56
+ stream.connect = function(newContext, newHash, newHandlers) {
57
+ context = newContext;
58
58
  hash = newHash;
59
59
  handlers = {};
60
60
  for (const key in newHandlers || {}) {
@@ -133,14 +133,14 @@ function Stream(platform, config, environment, diagnosticsAccumulator) {
133
133
  url = evalUrlPrefix;
134
134
  options.method = 'REPORT';
135
135
  options.headers['Content-Type'] = 'application/json';
136
- options.body = JSON.stringify(user);
136
+ options.body = JSON.stringify(context);
137
137
  } else {
138
138
  // if we can't do REPORT, fall back to the old ping-based stream
139
139
  url = appendUrlPath(baseUrl, '/ping/' + environment);
140
140
  query = '';
141
141
  }
142
142
  } else {
143
- url = evalUrlPrefix + '/' + base64URLEncode(JSON.stringify(user));
143
+ url = evalUrlPrefix + '/' + base64URLEncode(JSON.stringify(context));
144
144
  }
145
145
  options.headers = transformHeaders(options.headers, config);
146
146
  if (withReasons) {