Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 49 additions & 4 deletions src/__tests__/LDClient-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,53 @@ describe('LDClient', function() {
});
});

it('should handle localStorage getItem throwing an exception', function(done) {
sandbox.restore(window.localStorage.__proto__, 'getItem')
sandbox.stub(window.localStorage.__proto__, 'getItem').throws()

var warnSpy = sandbox.spy(console, 'warn');

var user = {key: 'user'};
var client = LDClient.initialize('UNKNOWN_ENVIRONMENT_ID', user, {
bootstrap: 'localstorage'
});

client.on('ready', function() {
expect(warnSpy.calledWith(messages.localStorageUnavailable())).to.be.true;
done();
});

requests[0].respond(
200,
{ 'Content-Type': 'application/json' },
'[{"key": "known", "kind": "custom"}]'
);

});

it('should handle localStorage setItem throwing an exception', function(done) {
sandbox.restore(window.localStorage.__proto__, 'setItem')
sandbox.stub(window.localStorage.__proto__, 'setItem').throws()

var user = {key: 'user'};
var client = LDClient.initialize('UNKNOWN_ENVIRONMENT_ID', user, {
bootstrap: 'localstorage'
});

var warnSpy = sandbox.spy(console, 'warn');

requests[0].respond(
200,
{ 'Content-Type': 'application/json' },
'[{"key": "known", "kind": "custom"}]'
);

client.on('ready', function() {
expect(warnSpy.calledWith(messages.localStorageUnavailable())).to.be.true;
done();
});
});

it('should not update cached settings if there was an error fetching flags', function(done) {
var user = {key: 'user'};
var json = '{"enable-foo": true}';
Expand Down Expand Up @@ -140,7 +187,7 @@ describe('LDClient', function() {
bootstrap: {} // so the client doesn't request settings
});

var warnSpy = sinon.spy(console, 'warn');
var warnSpy = sandbox.spy(console, 'warn');

requests[0].respond(
200,
Expand All @@ -151,7 +198,6 @@ describe('LDClient', function() {
client.on('ready', function() {
client.track('known');
expect(warnSpy.calledWith('Custom event key does not exist')).to.be.false;
warnSpy.restore();
done();
});
});
Expand Down Expand Up @@ -184,7 +230,7 @@ describe('LDClient', function() {
bootstrap: {} // so the client doesn't request settings
});

var warnSpy = sinon.spy(console, 'warn');
var warnSpy = sandbox.spy(console, 'warn');

requests[0].respond(
200,
Expand All @@ -195,7 +241,6 @@ describe('LDClient', function() {
client.on('ready', function() {
client.track('unknown');
expect(warnSpy.calledWith(messages.unknownCustomEventKey('unknown'))).to.be.true;
warnSpy.restore();
done();
});
});
Expand Down
14 changes: 8 additions & 6 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ var Requestor = require('./Requestor');
var Identity = require('./Identity');
var utils = require('./utils');
var messages = require('./messages');
var store = require('./store');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it matter than we require store.js here while the module is actually named Store.js (capital S)?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. That may cause problems in some environments. Fixed!


var flags = {};
var environment;
Expand Down Expand Up @@ -174,7 +175,7 @@ function updateSettings(settings) {
flags = settings;

if (useLocalStorage) {
localStorage.setItem(lsKey(environment, ident.getUser()), JSON.stringify(flags));
store.set(lsKey(environment, ident.getUser()), JSON.stringify(flags));
}

if (keys.length > 0) {
Expand Down Expand Up @@ -258,11 +259,12 @@ function initialize(env, user, options) {
}
else if (typeof(options.bootstrap) === 'string' && options.bootstrap.toUpperCase() === 'LOCALSTORAGE' && typeof(Storage) !== 'undefined') {
useLocalStorage = true;
// check if localstorage data is corrupted, if so clear it

// check if localStorage data is corrupted, if so clear it
try {
flags = JSON.parse(localStorage.getItem(localStorageKey));
flags = JSON.parse(store.get(localStorageKey));
} catch (error) {
localStorage.setItem(localStorageKey, null);
store.clear(localStorageKey);
}

if (flags === null) {
Expand All @@ -272,7 +274,7 @@ function initialize(env, user, options) {
emitter.emit(errorEvent)
}
flags = settings;
settings && localStorage.setItem(localStorageKey, JSON.stringify(flags));
settings && store.set(localStorageKey, JSON.stringify(flags));
emitter.emit(readyEvent);
});
} else {
Expand All @@ -285,7 +287,7 @@ function initialize(env, user, options) {
console.error('Error fetching flag settings: ', err);
emitter.emit(errorEvent)
}
settings && localStorage.setItem(localStorageKey, JSON.stringify(settings));
settings && store.set(localStorageKey, JSON.stringify(settings));
});
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ module.exports ={
invalidKey: function() {
return 'Event key must be a string';
},
localStorageUnavailable: function() {
return 'localStorage is unavailable';
},
unknownCustomEventKey: function(key) {
return 'Custom event "' + key + '" does not exist'
}
Expand Down
27 changes: 27 additions & 0 deletions src/store.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
var messages = require('./messages');

function get(key) {
try {
return window.localStorage.getItem(key);
} catch (ex) {
console.warn(messages.localStorageUnavailable());
}
}

function set(key, item) {
try {
window.localStorage.setItem(key, item);
} catch (ex) {
console.warn(messages.localStorageUnavailable());
}
}

function clear(key) {
set(key, null);
}

module.exports = {
get: get,
set: set,
clear: clear,
};