launchdarkly-js-sdk-common 5.0.0-alpha.2 → 5.0.0-alpha.4
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 +1 -1
- package/src/EventProcessor.js +4 -21
- package/src/EventSender.js +1 -1
- package/src/Requestor.js +2 -2
- package/src/__tests__/Requestor-test.js +8 -8
- package/src/__tests__/context-test.js +109 -1
- package/src/__tests__/utils-test.js +1 -1
- package/src/configuration.js +9 -0
- package/src/context.js +36 -0
- package/src/index.js +2 -1
package/package.json
CHANGED
package/src/EventProcessor.js
CHANGED
|
@@ -4,6 +4,7 @@ const ContextFilter = require('./ContextFilter');
|
|
|
4
4
|
const errors = require('./errors');
|
|
5
5
|
const messages = require('./messages');
|
|
6
6
|
const utils = require('./utils');
|
|
7
|
+
const { getContextKeys } = require('./context');
|
|
7
8
|
|
|
8
9
|
function EventProcessor(
|
|
9
10
|
platform,
|
|
@@ -50,7 +51,7 @@ function EventProcessor(
|
|
|
50
51
|
// identify events always have an inline context
|
|
51
52
|
ret.context = contextFilter.filter(e.context);
|
|
52
53
|
} else {
|
|
53
|
-
ret.contextKeys =
|
|
54
|
+
ret.contextKeys = getContextKeysFromEvent(e);
|
|
54
55
|
delete ret['context'];
|
|
55
56
|
}
|
|
56
57
|
if (e.kind === 'feature') {
|
|
@@ -60,26 +61,8 @@ function EventProcessor(
|
|
|
60
61
|
return ret;
|
|
61
62
|
}
|
|
62
63
|
|
|
63
|
-
function
|
|
64
|
-
|
|
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;
|
|
64
|
+
function getContextKeysFromEvent(event) {
|
|
65
|
+
return getContextKeys(event.context, logger);
|
|
83
66
|
}
|
|
84
67
|
|
|
85
68
|
function addToOutbox(event) {
|
package/src/EventSender.js
CHANGED
|
@@ -31,7 +31,7 @@ function EventSender(platform, environmentId, options) {
|
|
|
31
31
|
const headers = isDiagnostic
|
|
32
32
|
? baseHeaders
|
|
33
33
|
: utils.extend({}, baseHeaders, {
|
|
34
|
-
'X-LaunchDarkly-Event-Schema': '
|
|
34
|
+
'X-LaunchDarkly-Event-Schema': '4',
|
|
35
35
|
'X-LaunchDarkly-Payload-ID': payloadId,
|
|
36
36
|
});
|
|
37
37
|
return platform
|
package/src/Requestor.js
CHANGED
|
@@ -88,11 +88,11 @@ function Requestor(platform, options, environment) {
|
|
|
88
88
|
let body;
|
|
89
89
|
|
|
90
90
|
if (useReport) {
|
|
91
|
-
endpoint = [baseUrl, '/sdk/evalx/', environment, '/
|
|
91
|
+
endpoint = [baseUrl, '/sdk/evalx/', environment, '/context'].join('');
|
|
92
92
|
body = JSON.stringify(context);
|
|
93
93
|
} else {
|
|
94
94
|
data = utils.base64URLEncode(JSON.stringify(context));
|
|
95
|
-
endpoint = [baseUrl, '/sdk/evalx/', environment, '/
|
|
95
|
+
endpoint = [baseUrl, '/sdk/evalx/', environment, '/contexts/', data].join('');
|
|
96
96
|
}
|
|
97
97
|
if (hash) {
|
|
98
98
|
query = 'h=' + hash;
|
|
@@ -69,7 +69,7 @@ describe('Requestor', () => {
|
|
|
69
69
|
|
|
70
70
|
expect(server.requests.length()).toEqual(1);
|
|
71
71
|
const req = await server.requests.take();
|
|
72
|
-
expect(req.path).toEqual(`/sdk/evalx/${env}/
|
|
72
|
+
expect(req.path).toEqual(`/sdk/evalx/${env}/contexts/${encodedUser}`);
|
|
73
73
|
});
|
|
74
74
|
});
|
|
75
75
|
|
|
@@ -81,7 +81,7 @@ describe('Requestor', () => {
|
|
|
81
81
|
|
|
82
82
|
expect(server.requests.length()).toEqual(1);
|
|
83
83
|
const req = await server.requests.take();
|
|
84
|
-
expect(req.path).toEqual(`/sdk/evalx/${env}/
|
|
84
|
+
expect(req.path).toEqual(`/sdk/evalx/${env}/contexts/${encodedUser}?h=hash1`);
|
|
85
85
|
});
|
|
86
86
|
});
|
|
87
87
|
|
|
@@ -93,7 +93,7 @@ describe('Requestor', () => {
|
|
|
93
93
|
|
|
94
94
|
expect(server.requests.length()).toEqual(1);
|
|
95
95
|
const req = await server.requests.take();
|
|
96
|
-
expect(req.path).toEqual(`/sdk/evalx/${env}/
|
|
96
|
+
expect(req.path).toEqual(`/sdk/evalx/${env}/contexts/${encodedUser}?withReasons=true`);
|
|
97
97
|
});
|
|
98
98
|
});
|
|
99
99
|
|
|
@@ -105,7 +105,7 @@ describe('Requestor', () => {
|
|
|
105
105
|
|
|
106
106
|
expect(server.requests.length()).toEqual(1);
|
|
107
107
|
const req = await server.requests.take();
|
|
108
|
-
expect(req.path).toEqual(`/sdk/evalx/${env}/
|
|
108
|
+
expect(req.path).toEqual(`/sdk/evalx/${env}/contexts/${encodedUser}?h=hash1&withReasons=true`);
|
|
109
109
|
});
|
|
110
110
|
});
|
|
111
111
|
|
|
@@ -117,7 +117,7 @@ describe('Requestor', () => {
|
|
|
117
117
|
|
|
118
118
|
expect(server.requests.length()).toEqual(1);
|
|
119
119
|
const req = await server.requests.take();
|
|
120
|
-
expect(req.path).toEqual(`/sdk/evalx/${env}/
|
|
120
|
+
expect(req.path).toEqual(`/sdk/evalx/${env}/context`);
|
|
121
121
|
});
|
|
122
122
|
});
|
|
123
123
|
|
|
@@ -129,7 +129,7 @@ describe('Requestor', () => {
|
|
|
129
129
|
|
|
130
130
|
expect(server.requests.length()).toEqual(1);
|
|
131
131
|
const req = await server.requests.take();
|
|
132
|
-
expect(req.path).toEqual(`/sdk/evalx/${env}/
|
|
132
|
+
expect(req.path).toEqual(`/sdk/evalx/${env}/context?h=hash1`);
|
|
133
133
|
});
|
|
134
134
|
});
|
|
135
135
|
|
|
@@ -141,7 +141,7 @@ describe('Requestor', () => {
|
|
|
141
141
|
|
|
142
142
|
expect(server.requests.length()).toEqual(1);
|
|
143
143
|
const req = await server.requests.take();
|
|
144
|
-
expect(req.path).toEqual(`/sdk/evalx/${env}/
|
|
144
|
+
expect(req.path).toEqual(`/sdk/evalx/${env}/context?withReasons=true`);
|
|
145
145
|
});
|
|
146
146
|
});
|
|
147
147
|
|
|
@@ -153,7 +153,7 @@ describe('Requestor', () => {
|
|
|
153
153
|
|
|
154
154
|
expect(server.requests.length()).toEqual(1);
|
|
155
155
|
const req = await server.requests.take();
|
|
156
|
-
expect(req.path).toEqual(`/sdk/evalx/${env}/
|
|
156
|
+
expect(req.path).toEqual(`/sdk/evalx/${env}/context?h=hash1&withReasons=true`);
|
|
157
157
|
});
|
|
158
158
|
});
|
|
159
159
|
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
const { checkContext, getContextKinds, getCanonicalKey } = require('../context');
|
|
1
|
+
const { checkContext, getContextKeys, getContextKinds, getCanonicalKey } = require('../context');
|
|
2
2
|
|
|
3
3
|
describe.each([{ key: 'test' }, { kind: 'user', key: 'test' }, { kind: 'multi', user: { key: 'test' } }])(
|
|
4
4
|
'given a context which contains a single kind',
|
|
@@ -91,3 +91,111 @@ describe('when determining canonical keys', () => {
|
|
|
91
91
|
expect(getCanonicalKey(null)).toBeUndefined();
|
|
92
92
|
});
|
|
93
93
|
});
|
|
94
|
+
|
|
95
|
+
describe('getContextKeys', () => {
|
|
96
|
+
it('returns undefined if argument is undefined', () => {
|
|
97
|
+
const context = undefined;
|
|
98
|
+
const keys = getContextKeys(context);
|
|
99
|
+
expect(keys).toBeUndefined();
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
it('works with legacy user without kind attribute', () => {
|
|
103
|
+
const user = {
|
|
104
|
+
key: 'legacy-user-key',
|
|
105
|
+
name: 'Test User',
|
|
106
|
+
custom: {
|
|
107
|
+
customAttribute1: true,
|
|
108
|
+
},
|
|
109
|
+
};
|
|
110
|
+
const keys = getContextKeys(user);
|
|
111
|
+
expect(keys).toEqual({ user: 'legacy-user-key' });
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
it('gets keys from multi context', () => {
|
|
115
|
+
const context = {
|
|
116
|
+
kind: 'multi',
|
|
117
|
+
user: {
|
|
118
|
+
key: 'test-user-key',
|
|
119
|
+
name: 'Test User',
|
|
120
|
+
isPremiumCustomer: true,
|
|
121
|
+
},
|
|
122
|
+
organization: {
|
|
123
|
+
key: 'test-organization-key',
|
|
124
|
+
name: 'Test Company',
|
|
125
|
+
industry: 'technology',
|
|
126
|
+
},
|
|
127
|
+
};
|
|
128
|
+
const keys = getContextKeys(context);
|
|
129
|
+
expect(keys).toEqual({ user: 'test-user-key', organization: 'test-organization-key' });
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
it('ignores undefined keys from multi context', () => {
|
|
133
|
+
const context = {
|
|
134
|
+
kind: 'multi',
|
|
135
|
+
user: {
|
|
136
|
+
key: 'test-user-key',
|
|
137
|
+
name: 'Test User',
|
|
138
|
+
isPremiumCustomer: true,
|
|
139
|
+
},
|
|
140
|
+
organization: {
|
|
141
|
+
name: 'Test Company',
|
|
142
|
+
industry: 'technology',
|
|
143
|
+
},
|
|
144
|
+
rogueAttribute: undefined,
|
|
145
|
+
};
|
|
146
|
+
const keys = getContextKeys(context);
|
|
147
|
+
expect(keys).toEqual({ user: 'test-user-key' });
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
it.only('ignores empty string and null keys from multi context', () => {
|
|
151
|
+
const context = {
|
|
152
|
+
kind: 'multi',
|
|
153
|
+
user: {
|
|
154
|
+
key: 'test-user-key',
|
|
155
|
+
name: 'Test User',
|
|
156
|
+
isPremiumCustomer: true,
|
|
157
|
+
},
|
|
158
|
+
organization: {
|
|
159
|
+
key: '',
|
|
160
|
+
name: 'Test Company',
|
|
161
|
+
industry: 'technology',
|
|
162
|
+
},
|
|
163
|
+
drone: {
|
|
164
|
+
key: null,
|
|
165
|
+
name: 'test-drone',
|
|
166
|
+
},
|
|
167
|
+
};
|
|
168
|
+
const keys = getContextKeys(context);
|
|
169
|
+
expect(keys).toEqual({ user: 'test-user-key' });
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
it('gets keys from single context', () => {
|
|
173
|
+
const context = {
|
|
174
|
+
kind: 'drone',
|
|
175
|
+
key: 'test-drone-key',
|
|
176
|
+
name: 'test-drone',
|
|
177
|
+
};
|
|
178
|
+
const keys = getContextKeys(context);
|
|
179
|
+
expect(keys).toEqual({ drone: 'test-drone-key' });
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
it('ignores kind when it is an empty string', () => {
|
|
183
|
+
const context = {
|
|
184
|
+
kind: '',
|
|
185
|
+
key: 'test-drone-key',
|
|
186
|
+
name: 'test-drone',
|
|
187
|
+
};
|
|
188
|
+
const keys = getContextKeys(context);
|
|
189
|
+
expect(keys).toEqual({});
|
|
190
|
+
});
|
|
191
|
+
|
|
192
|
+
it('ignores kind when it is null', () => {
|
|
193
|
+
const context = {
|
|
194
|
+
kind: null,
|
|
195
|
+
key: 'test-drone-key',
|
|
196
|
+
name: 'test-drone',
|
|
197
|
+
};
|
|
198
|
+
const keys = getContextKeys(context);
|
|
199
|
+
expect(keys).toEqual({});
|
|
200
|
+
});
|
|
201
|
+
});
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { appendUrlPath, base64URLEncode, getLDUserAgentString, wrapPromiseCallback
|
|
1
|
+
import { appendUrlPath, base64URLEncode, chunkEventsForUrl, getLDUserAgentString, wrapPromiseCallback } from '../utils';
|
|
2
2
|
|
|
3
3
|
import * as stubPlatform from './stubPlatform';
|
|
4
4
|
|
package/src/configuration.js
CHANGED
|
@@ -44,6 +44,10 @@ const baseOptionDefs = {
|
|
|
44
44
|
*/
|
|
45
45
|
const allowedTagCharacters = /^(\w|\.|-)+$/;
|
|
46
46
|
|
|
47
|
+
function canonicalizeUrl(url) {
|
|
48
|
+
return url?.replace(/\/+$/, '');
|
|
49
|
+
}
|
|
50
|
+
|
|
47
51
|
/**
|
|
48
52
|
* Verify that a value meets the requirements for a tag value.
|
|
49
53
|
* @param {string} tagValue
|
|
@@ -165,6 +169,11 @@ function validate(options, emitter, extraOptionDefs, logger) {
|
|
|
165
169
|
}
|
|
166
170
|
}
|
|
167
171
|
});
|
|
172
|
+
|
|
173
|
+
ret.baseUrl = canonicalizeUrl(ret.baseUrl);
|
|
174
|
+
ret.streamUrl = canonicalizeUrl(ret.streamUrl);
|
|
175
|
+
ret.eventsUrl = canonicalizeUrl(ret.eventsUrl);
|
|
176
|
+
|
|
168
177
|
return ret;
|
|
169
178
|
}
|
|
170
179
|
|
package/src/context.js
CHANGED
|
@@ -89,8 +89,44 @@ function getCanonicalKey(context) {
|
|
|
89
89
|
}
|
|
90
90
|
}
|
|
91
91
|
|
|
92
|
+
function getContextKeys(context, logger) {
|
|
93
|
+
if (!context) {
|
|
94
|
+
return undefined;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
const keys = {};
|
|
98
|
+
const { kind, key } = context;
|
|
99
|
+
|
|
100
|
+
switch (kind) {
|
|
101
|
+
case typeof kind === 'undefined':
|
|
102
|
+
keys.user = `${key}`;
|
|
103
|
+
break;
|
|
104
|
+
case 'multi':
|
|
105
|
+
Object.entries(context)
|
|
106
|
+
.filter(([key]) => key !== 'kind')
|
|
107
|
+
.forEach(([key, value]) => {
|
|
108
|
+
if (value?.key) {
|
|
109
|
+
keys[key] = value.key;
|
|
110
|
+
}
|
|
111
|
+
});
|
|
112
|
+
break;
|
|
113
|
+
case null:
|
|
114
|
+
logger?.warn(`null is not a valid context kind: ${context}`);
|
|
115
|
+
break;
|
|
116
|
+
case '':
|
|
117
|
+
logger?.warn(`'' is not a valid context kind: ${context}`);
|
|
118
|
+
break;
|
|
119
|
+
default:
|
|
120
|
+
keys[kind] = `${key}`;
|
|
121
|
+
break;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
return keys;
|
|
125
|
+
}
|
|
126
|
+
|
|
92
127
|
module.exports = {
|
|
93
128
|
checkContext,
|
|
129
|
+
getContextKeys,
|
|
94
130
|
getContextKinds,
|
|
95
131
|
getCanonicalKey,
|
|
96
132
|
};
|
package/src/index.js
CHANGED
|
@@ -14,7 +14,7 @@ const { commonBasicLogger } = require('./loggers');
|
|
|
14
14
|
const utils = require('./utils');
|
|
15
15
|
const errors = require('./errors');
|
|
16
16
|
const messages = require('./messages');
|
|
17
|
-
const { checkContext } = require('./context');
|
|
17
|
+
const { checkContext, getContextKeys } = require('./context');
|
|
18
18
|
const { InspectorTypes, InspectorManager } = require('./InspectorManager');
|
|
19
19
|
|
|
20
20
|
const changeEvent = 'change';
|
|
@@ -809,4 +809,5 @@ module.exports = {
|
|
|
809
809
|
errors,
|
|
810
810
|
messages,
|
|
811
811
|
utils,
|
|
812
|
+
getContextKeys,
|
|
812
813
|
};
|