Skip to content

Commit f2b20b2

Browse files
committed
add warning for bootstrap not having metadata
1 parent e762629 commit f2b20b2

File tree

3 files changed

+27
-3
lines changed

3 files changed

+27
-3
lines changed

src/__tests__/LDClient-test.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,14 @@ describe('LDClient', () => {
175175
expect(client.variation('foo')).toEqual('bar');
176176
});
177177

178+
it('logs warning when bootstrap object uses old format', () => {
179+
const client = LDClient.initialize(envName, user, {
180+
bootstrap: { foo: 'bar' },
181+
});
182+
183+
expect(warnSpy).toHaveBeenCalledWith(messages.bootstrapOldFormat());
184+
});
185+
178186
it('sets flag values from bootstrap object with new format', () => {
179187
const client = LDClient.initialize(envName, user, {
180188
bootstrap: { foo: 'bar', $flagsState: { foo: { version: 1 } } },
@@ -183,6 +191,14 @@ describe('LDClient', () => {
183191
expect(client.variation('foo')).toEqual('bar');
184192
});
185193

194+
it('does not log warning when bootstrap object uses new format', () => {
195+
const client = LDClient.initialize(envName, user, {
196+
bootstrap: { foo: 'bar', $flagsState: { foo: { version: 1 } } },
197+
});
198+
199+
expect(warnSpy).not.toHaveBeenCalled();
200+
});
201+
186202
it('should contain package version', () => {
187203
// Arrange
188204
const version = LDClient.version;

src/index.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,15 @@ export function initialize(env, user, options = {}) {
3939
// If the bootstrap data came from an older server-side SDK, we'll have just a map of keys to values.
4040
// Newer SDKs that have an allFlagsState method will provide an extra "$flagsState" key that contains
4141
// the rest of the metadata we want. We do it this way for backward compatibility with older JS SDKs.
42+
const keys = Object.keys(data);
4243
const metadataKey = '$flagsState';
4344
const metadata = data[metadataKey];
45+
if (!metadata && keys.length) {
46+
console.warn(messages.bootstrapOldFormat());
47+
}
4448
const ret = {};
45-
for (const key in data) {
46-
if (key !== metadataKey && data.hasOwnProperty(key)) {
49+
keys.forEach(function(key) {
50+
if (key !== metadataKey) {
4751
let flag = { value: data[key] };
4852
if (metadata && metadata[key]) {
4953
flag = utils.extend(flag, metadata[key]);
@@ -52,7 +56,7 @@ export function initialize(env, user, options = {}) {
5256
}
5357
ret[key] = flag;
5458
}
55-
}
59+
});
5660
return ret;
5761
}
5862

src/messages.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ export const invalidUser = function() {
3737
return 'Invalid user specified.' + docLink;
3838
};
3939

40+
export const bootstrapOldFormat = function() {
41+
'LaunchDarkly client was initialized with bootstrap data that did not include flag metadata. Events may not be sent correctly.' + docLink;
42+
};
43+
4044
export const deprecated = function(oldName, newName) {
4145
return '[LaunchDarkly] "' + oldName + '" is deprecated, please use "' + newName + '"';
4246
};

0 commit comments

Comments
 (0)