Skip to content

Commit dac6e89

Browse files
LaunchDarklyReleaseBoteli-darklyzmdavisLaunchDarklyCIbwoskow-ld
authored
prepare 3.6.0 release (#66)
* initial move of code from js-client-sdk-private * changelog note * rm obsolete comment * add npm audit helper * update babel, jest, rollup * fix rollup config * fix ES build, dependency cleanup * add Releaser metadata * Update babel config to work in `test` without `useBuiltIns` * copyedits * fix misnamed directory * use spread operator instead of Object.assign * add issue templates * add babel-eslint * add event capacity config property * re-add deprecation warning on samplingInterval * better config validation * remove rollup-plugins-node-resolve * use newer Rollup node-resolve plugin * rm rollup-plugin-includepaths (unused) * npm audit fix (handlebars dependency from jest) * comment * copyedit * use new test helpers + misc test cleanup * clean up stream testing logic * fix hash parameter * linter * clearer way to model the config option defaults/types * test improvements * change internal param name * comment * fix default logger logic * simpler way to enforce minimum values * implement diagnostic events in common JS package (#11) * add support for function type in config options * add support for function type in config options (#13) * add wrapper metadata options and fix custom header logic * lint * lint * remove image-loading logic from common code, replace it with an abstraction * add validation for options.streaming * typo * rm unused params * typo in comment * misc fixes to merged code from external PR * add event payload ID header * npm audit fix * change exact dependencies to best-compatible * standardize linting * disallow "window" and "document" * improve diag event tests + debug logging * misc cleanup * fix updating secure mode hash with identify() * don't omit streamInits.failed when it's false * clean up init state logic, prevent unhandled rejections * lint * less strict matching of json content-type header * remove unsafe usage of hasOwnProperty * console logger must tolerate console object not always existing * fix double initialization of diagnostics manager * fix TypeScript declaration for track() and add more TS compilation tests (#27) * remove startsWith usage (#28) * prevent poll caused by a stream ping from overwriting later poll for another user (#29) * upgrade jest dependency and transitive yargs-parser dependency (#30) * Add null to LDEvaluationDetail.reason type (#31) * Revert "Add null to LDEvaluationDetail.reason type (#31)" This reverts commit bcb1573. * Revert "Add null to LDEvaluationDetail.reason type (#31)" This reverts commit bcb1573. * nullable evaluation reason (#32) * adding alias event functionality (#33) * set stream read timeout * Add prepare script (#34) * add a missing typescript verification (#36) * Removed the guides link * Correct doc link (#36) * Fix typo in LDClient.on jsdoc (#37) * add inlineUsersInEvents option in TypeScript (#37) * Filter private attributes on debug event users. Send variation for debug events. * update uuid package version (#39) * use Releaser v2 config + newer CI image * First half, add the type, create the new options, add the new util method, and add tests * Second half, call the tranform util method before calling any HTTP requests * Update the transform to work on a copy of headers instead of mutating it * add comments about removing custom event warning logic in the future * revert updating of UUID dependency (#43) * Revert "update uuid package version (#39)" This reverts commit 3b2ff6c. * update package-lock.json * better error handling for local storage operations (#44) * better error handling for local storage operations * lint * fix obsolete comments * add basic logger similar to server-side Node SDK (#45) * fix exports and add validation of custom logger (#46) * remove typedoc.js file that interferes with Releaser's docs build * update typescript version * add maintenance branch * backport sc-142333 fix * make URL path concatenation work right whether base URL has a trailing slash or not (#61) * make URL path concatenation work right whether base URL has a trailing slash or not * lint * Implement application tags for 3.x. (#62) Co-authored-by: Eli Bishop <eli@launchdarkly.com> Co-authored-by: Zach Davis <zach@launchdarkly.com> Co-authored-by: LaunchDarklyCI <dev@launchdarkly.com> Co-authored-by: Ben Woskow <bwoskow@launchdarkly.com> Co-authored-by: Ben Woskow <48036130+bwoskow-ld@users.noreply.github.com> Co-authored-by: Michael Siadak <mike.siadak@gmail.com> Co-authored-by: Jeff Wen <sinchangwen@gmail.com> Co-authored-by: Andrey Krasnov <34657799+Doesntmeananything@users.noreply.github.com> Co-authored-by: Gavin Whelan <gwhelan@launchdarkly.com> Co-authored-by: LaunchDarklyReleaseBot <launchdarklyreleasebot@launchdarkly.com> Co-authored-by: Louis Chan <lchan@launchdarkly.com> Co-authored-by: Louis Chan <91093020+louis-launchdarkly@users.noreply.github.com> Co-authored-by: Ryan Lamb <4955475+kinyoklion@users.noreply.github.com>
1 parent b6d90c7 commit dac6e89

16 files changed

+314
-12896
lines changed

package-lock.json

-12,791
This file was deleted.

src/EventProcessor.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export default function EventProcessor(
1515
) {
1616
const processor = {};
1717
const eventSender = sender || EventSender(platform, environmentId, options);
18-
const mainEventsUrl = options.eventsUrl + '/events/bulk/' + environmentId;
18+
const mainEventsUrl = utils.appendUrlPath(options.eventsUrl, '/events/bulk/' + environmentId);
1919
const summarizer = EventSummarizer();
2020
const userFilter = UserFilter(options);
2121
const inlineUsers = options.inlineUsersInEvents;

src/EventSender.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import * as errors from './errors';
22
import * as utils from './utils';
33
import uuidv1 from 'uuid/v1';
4+
import { getLDHeaders, transformHeaders } from './headers';
45

56
const MAX_URL_LENGTH = 2000;
67

78
export default function EventSender(platform, environmentId, options) {
89
const imageUrlPath = '/a/' + environmentId + '.gif';
9-
const baseHeaders = utils.extend({ 'Content-Type': 'application/json' }, utils.getLDHeaders(platform, options));
10+
const baseHeaders = utils.extend({ 'Content-Type': 'application/json' }, getLDHeaders(platform, options));
1011
const httpFallbackPing = platform.httpFallbackPing; // this will be set for us if we're in the browser SDK
1112
const sender = {};
1213

@@ -34,7 +35,7 @@ export default function EventSender(platform, environmentId, options) {
3435
'X-LaunchDarkly-Payload-ID': payloadId,
3536
});
3637
return platform
37-
.httpRequest('POST', url, utils.transformHeaders(headers, options), jsonBody)
38+
.httpRequest('POST', url, transformHeaders(headers, options), jsonBody)
3839
.promise.then(result => {
3940
if (!result) {
4041
// This was a response from a fire-and-forget request, so we won't have a status.

src/Requestor.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import * as utils from './utils';
22
import * as errors from './errors';
33
import * as messages from './messages';
44
import promiseCoalescer from './promiseCoalescer';
5+
import { transformHeaders, getLDHeaders } from './headers';
56

67
const jsonContentType = 'application/json';
78

@@ -31,7 +32,7 @@ export default function Requestor(platform, options, environment) {
3132
}
3233

3334
const method = body ? 'REPORT' : 'GET';
34-
const headers = utils.getLDHeaders(platform, options);
35+
const headers = getLDHeaders(platform, options);
3536
if (body) {
3637
headers['Content-Type'] = jsonContentType;
3738
}
@@ -45,7 +46,7 @@ export default function Requestor(platform, options, environment) {
4546
activeRequests[endpoint] = coalescer;
4647
}
4748

48-
const req = platform.httpRequest(method, endpoint, utils.transformHeaders(headers, options), body);
49+
const req = platform.httpRequest(method, endpoint, transformHeaders(headers, options), body);
4950
const p = req.promise.then(
5051
result => {
5152
if (result.status === 200) {
@@ -75,7 +76,7 @@ export default function Requestor(platform, options, environment) {
7576
// Performs a GET request to an arbitrary path under baseUrl. Returns a Promise which will resolve
7677
// with the parsed JSON response, or will be rejected if the request failed.
7778
requestor.fetchJSON = function(path) {
78-
return fetchJSON(baseUrl + path, null);
79+
return fetchJSON(utils.appendUrlPath(baseUrl, path), null);
7980
};
8081

8182
// Requests the current state of all flags for the given user from LaunchDarkly. Returns a Promise

src/Stream.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import * as messages from './messages';
2-
import { base64URLEncode, getLDHeaders, transformHeaders, objectHasOwnProperty } from './utils';
2+
import { appendUrlPath, base64URLEncode, objectHasOwnProperty } from './utils';
3+
import { getLDHeaders, transformHeaders } from './headers';
34

45
// The underlying event source implementation is abstracted via the platform object, which should
56
// have these three properties:
@@ -20,7 +21,7 @@ export default function Stream(platform, config, environment, diagnosticsAccumul
2021
const baseUrl = config.streamUrl;
2122
const logger = config.logger;
2223
const stream = {};
23-
const evalUrlPrefix = baseUrl + '/eval/' + environment;
24+
const evalUrlPrefix = appendUrlPath(baseUrl, '/eval/' + environment);
2425
const useReport = config.useReport;
2526
const withReasons = config.evaluationReasons;
2627
const streamReconnectDelay = config.streamReconnectDelay;
@@ -98,7 +99,7 @@ export default function Stream(platform, config, environment, diagnosticsAccumul
9899
options.body = JSON.stringify(user);
99100
} else {
100101
// if we can't do REPORT, fall back to the old ping-based stream
101-
url = baseUrl + '/ping/' + environment;
102+
url = appendUrlPath(baseUrl, '/ping/' + environment);
102103
query = '';
103104
}
104105
} else {

src/__tests__/Stream-test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { DiagnosticsAccumulator } from '../diagnosticEvents';
22
import * as messages from '../messages';
33
import Stream from '../Stream';
4-
import { getLDHeaders } from '../utils';
4+
import { getLDHeaders } from '../headers';
55

66
import { sleepAsync } from 'launchdarkly-js-test-helpers';
77
import EventSource from './EventSource-mock';

src/__tests__/configuration-test.js

+39
Original file line numberDiff line numberDiff line change
@@ -216,4 +216,43 @@ describe('configuration', () => {
216216
expect(config.extraFunctionOption).toBe(fn);
217217
await listener.expectError(messages.wrongOptionType('extraNumericOptionWithoutDefault', 'number', 'string'));
218218
});
219+
220+
it('handles a valid application id', async () => {
221+
const listener = errorListener();
222+
const configIn = { application: { id: 'test-application' } };
223+
expect(configuration.validate(configIn, listener.emitter, null, listener.logger).application.id).toEqual(
224+
'test-application'
225+
);
226+
});
227+
228+
it('logs a warning with an invalid application id', async () => {
229+
const listener = errorListener();
230+
const configIn = { application: { id: 'test #$#$#' } };
231+
expect(configuration.validate(configIn, listener.emitter, null, listener.logger).application.id).toBeUndefined();
232+
await listener.expectWarningOnly(messages.invalidTagValue('application.id'));
233+
});
234+
235+
it('handles a valid application version', async () => {
236+
const listener = errorListener();
237+
const configIn = { application: { version: 'test-version' } };
238+
expect(configuration.validate(configIn, listener.emitter, null, listener.logger).application.version).toEqual(
239+
'test-version'
240+
);
241+
});
242+
243+
it('logs a warning with an invalid application version', async () => {
244+
const listener = errorListener();
245+
const configIn = { application: { version: 'test #$#$#' } };
246+
expect(
247+
configuration.validate(configIn, listener.emitter, null, listener.logger).application.version
248+
).toBeUndefined();
249+
await listener.expectWarningOnly(messages.invalidTagValue('application.version'));
250+
});
251+
252+
it('includes application id and version in tags when present', async () => {
253+
expect(configuration.getTags({ application: { id: 'test-id', version: 'test-version' } })).toEqual({
254+
'application-id': ['test-id'],
255+
'application-version': ['test-version'],
256+
});
257+
});
219258
});

src/__tests__/headers-test.js

+110
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
import { getLDHeaders, transformHeaders } from '../headers';
2+
import { getLDUserAgentString } from '../utils';
3+
import * as stubPlatform from './stubPlatform';
4+
5+
describe('getLDHeaders', () => {
6+
it('sends no headers unless sendLDHeaders is true', () => {
7+
const platform = stubPlatform.defaults();
8+
const headers = getLDHeaders(platform, {});
9+
expect(headers).toEqual({});
10+
});
11+
12+
it('adds user-agent header', () => {
13+
const platform = stubPlatform.defaults();
14+
const headers = getLDHeaders(platform, { sendLDHeaders: true });
15+
expect(headers).toMatchObject({ 'X-LaunchDarkly-User-Agent': getLDUserAgentString(platform) });
16+
});
17+
18+
it('adds wrapper info if specified, without version', () => {
19+
const platform = stubPlatform.defaults();
20+
const headers = getLDHeaders(platform, { sendLDHeaders: true, wrapperName: 'FakeSDK' });
21+
expect(headers).toMatchObject({
22+
'X-LaunchDarkly-User-Agent': getLDUserAgentString(platform),
23+
'X-LaunchDarkly-Wrapper': 'FakeSDK',
24+
});
25+
});
26+
27+
it('adds wrapper info if specified, with version', () => {
28+
const platform = stubPlatform.defaults();
29+
const headers = getLDHeaders(platform, { sendLDHeaders: true, wrapperName: 'FakeSDK', wrapperVersion: '9.9' });
30+
expect(headers).toMatchObject({
31+
'X-LaunchDarkly-User-Agent': getLDUserAgentString(platform),
32+
'X-LaunchDarkly-Wrapper': 'FakeSDK/9.9',
33+
});
34+
});
35+
36+
it('sets the X-LaunchDarkly-Tags header with valid id and version.', () => {
37+
const platform = stubPlatform.defaults();
38+
const headers = getLDHeaders(platform, {
39+
sendLDHeaders: true,
40+
application: {
41+
id: 'test-application',
42+
version: 'test-version',
43+
},
44+
});
45+
expect(headers).toMatchObject({
46+
'X-LaunchDarkly-User-Agent': getLDUserAgentString(platform),
47+
'x-launchdarkly-tags': 'application-id/test-application application-version/test-version',
48+
});
49+
});
50+
51+
it('sets the X-LaunchDarkly-Tags header with just application id', () => {
52+
const platform = stubPlatform.defaults();
53+
const headers = getLDHeaders(platform, {
54+
sendLDHeaders: true,
55+
application: {
56+
id: 'test-application',
57+
},
58+
});
59+
expect(headers).toMatchObject({
60+
'X-LaunchDarkly-User-Agent': getLDUserAgentString(platform),
61+
'x-launchdarkly-tags': 'application-id/test-application',
62+
});
63+
});
64+
65+
it('sets the X-LaunchDarkly-Tags header with just application version.', () => {
66+
const platform = stubPlatform.defaults();
67+
const headers = getLDHeaders(platform, {
68+
sendLDHeaders: true,
69+
application: {
70+
version: 'test-version',
71+
},
72+
});
73+
expect(headers).toMatchObject({
74+
'X-LaunchDarkly-User-Agent': getLDUserAgentString(platform),
75+
'x-launchdarkly-tags': 'application-version/test-version',
76+
});
77+
});
78+
});
79+
80+
describe('transformHeaders', () => {
81+
it('does not modify the headers if the option is not available', () => {
82+
const inputHeaders = { a: '1', b: '2' };
83+
const headers = transformHeaders(inputHeaders, {});
84+
expect(headers).toEqual(inputHeaders);
85+
});
86+
87+
it('modifies the headers if the option has a transform', () => {
88+
const inputHeaders = { c: '3', d: '4' };
89+
const outputHeaders = { c: '9', d: '4', e: '5' };
90+
const headerTransform = input => {
91+
const output = { ...input };
92+
output['c'] = '9';
93+
output['e'] = '5';
94+
return output;
95+
};
96+
const headers = transformHeaders(inputHeaders, { requestHeaderTransform: headerTransform });
97+
expect(headers).toEqual(outputHeaders);
98+
});
99+
100+
it('cannot mutate the input header object', () => {
101+
const inputHeaders = { f: '6' };
102+
const expectedInputHeaders = { f: '6' };
103+
const headerMutate = input => {
104+
input['f'] = '7'; // eslint-disable-line no-param-reassign
105+
return input;
106+
};
107+
transformHeaders(inputHeaders, { requestHeaderTransform: headerMutate });
108+
expect(inputHeaders).toEqual(expectedInputHeaders);
109+
});
110+
});

src/__tests__/utils-test.js

+8-66
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import {
2+
appendUrlPath,
23
base64URLEncode,
3-
getLDHeaders,
4-
transformHeaders,
54
getLDUserAgentString,
65
wrapPromiseCallback,
76
chunkUserEventsForUrl,
@@ -10,6 +9,13 @@ import {
109
import * as stubPlatform from './stubPlatform';
1110

1211
describe('utils', () => {
12+
it('appendUrlPath', () => {
13+
expect(appendUrlPath('http://base', '/path')).toEqual('http://base/path');
14+
expect(appendUrlPath('http://base', 'path')).toEqual('http://base/path');
15+
expect(appendUrlPath('http://base/', '/path')).toEqual('http://base/path');
16+
expect(appendUrlPath('http://base/', '/path')).toEqual('http://base/path');
17+
});
18+
1319
describe('wrapPromiseCallback', () => {
1420
it('should resolve to the value', done => {
1521
const promise = wrapPromiseCallback(Promise.resolve('woohoo'));
@@ -48,70 +54,6 @@ describe('utils', () => {
4854
});
4955
});
5056

51-
describe('getLDHeaders', () => {
52-
it('sends no headers unless sendLDHeaders is true', () => {
53-
const platform = stubPlatform.defaults();
54-
const headers = getLDHeaders(platform, {});
55-
expect(headers).toEqual({});
56-
});
57-
58-
it('adds custom user-agent header', () => {
59-
const platform = stubPlatform.defaults();
60-
const headers = getLDHeaders(platform, { sendLDHeaders: true });
61-
expect(headers).toMatchObject({ 'X-LaunchDarkly-User-Agent': getLDUserAgentString(platform) });
62-
});
63-
64-
it('adds wrapper info if specified, without version', () => {
65-
const platform = stubPlatform.defaults();
66-
const headers = getLDHeaders(platform, { sendLDHeaders: true, wrapperName: 'FakeSDK' });
67-
expect(headers).toMatchObject({
68-
'X-LaunchDarkly-User-Agent': getLDUserAgentString(platform),
69-
'X-LaunchDarkly-Wrapper': 'FakeSDK',
70-
});
71-
});
72-
73-
it('adds wrapper info if specified, with version', () => {
74-
const platform = stubPlatform.defaults();
75-
const headers = getLDHeaders(platform, { sendLDHeaders: true, wrapperName: 'FakeSDK', wrapperVersion: '9.9' });
76-
expect(headers).toMatchObject({
77-
'X-LaunchDarkly-User-Agent': getLDUserAgentString(platform),
78-
'X-LaunchDarkly-Wrapper': 'FakeSDK/9.9',
79-
});
80-
});
81-
});
82-
83-
describe('transformHeaders', () => {
84-
it('does not modify the headers if the option is not available', () => {
85-
const inputHeaders = { a: '1', b: '2' };
86-
const headers = transformHeaders(inputHeaders, {});
87-
expect(headers).toEqual(inputHeaders);
88-
});
89-
90-
it('modifies the headers if the option has a transform', () => {
91-
const inputHeaders = { c: '3', d: '4' };
92-
const outputHeaders = { c: '9', d: '4', e: '5' };
93-
const headerTransform = input => {
94-
const output = { ...input };
95-
output['c'] = '9';
96-
output['e'] = '5';
97-
return output;
98-
};
99-
const headers = transformHeaders(inputHeaders, { requestHeaderTransform: headerTransform });
100-
expect(headers).toEqual(outputHeaders);
101-
});
102-
103-
it('cannot mutate the input header object', () => {
104-
const inputHeaders = { f: '6' };
105-
const expectedInputHeaders = { f: '6' };
106-
const headerMutate = input => {
107-
input['f'] = '7'; // eslint-disable-line no-param-reassign
108-
return input;
109-
};
110-
transformHeaders(inputHeaders, { requestHeaderTransform: headerMutate });
111-
expect(inputHeaders).toEqual(expectedInputHeaders);
112-
});
113-
});
114-
11557
describe('getLDUserAgentString', () => {
11658
it('uses platform user-agent and package version by default', () => {
11759
const platform = stubPlatform.defaults();

0 commit comments

Comments
 (0)