diff --git a/js/plugins/EncryptedInsightStorage.js b/js/plugins/EncryptedInsightStorage.js index d0f381fa0..d5b6f0b9c 100644 --- a/js/plugins/EncryptedInsightStorage.js +++ b/js/plugins/EncryptedInsightStorage.js @@ -11,6 +11,9 @@ EncryptedInsightStorage.prototype.getItem = function(name, callback) { var key = cryptoUtil.kdfbinary(this.password + this.email); InsightStorage.prototype.getItem.apply(this, [name, function(err, body) { + if (err) { + return callback(err); + } var decryptedJson = cryptoUtil.decrypt(key, body); if (!decryptedJson) { return callback('Internal Error'); diff --git a/js/plugins/GoogleDrive.js b/js/plugins/GoogleDrive.js index c98149584..599314961 100644 --- a/js/plugins/GoogleDrive.js +++ b/js/plugins/GoogleDrive.js @@ -95,6 +95,16 @@ GoogleDrive.prototype._httpGet = function(theUrl) { return xmlHttp.responseText; } +GoogleDrive.prototype.createItem = function(name, value, callback) { + this.getItem(name, function(err, retrieved) { + if (err || !retrieved) { + return this.setItem(name, value, callback); + } else { + return callback('EEXISTS'); + } + }); +}; + GoogleDrive.prototype.getItem = function(k, cb) { //console.log('[googleDrive.js.95:getItem:]', k); //TODO var self = this; diff --git a/js/plugins/InsightStorage.js b/js/plugins/InsightStorage.js index ec47a9c5f..9ecbddd15 100644 --- a/js/plugins/InsightStorage.js +++ b/js/plugins/InsightStorage.js @@ -16,6 +16,17 @@ InsightStorage.prototype.setCredentials = function(email, password, opts) { this.password = password; }; +InsightStorage.prototype.createItem = function(name, value, callback) { + var self = this; + this.getItem(name, function(err, retrieved) { + if (err || !retrieved) { + return self.setItem(name, value, callback); + } else { + return callback('EEXISTS'); + } + }); +}; + InsightStorage.prototype.getItem = function(name, callback) { var key = cryptoUtil.kdf(this.password + this.email); var secret = cryptoUtil.kdf(key, this.password); diff --git a/js/plugins/LocalStorage.js b/js/plugins/LocalStorage.js index 61df027ae..6715d8cf1 100644 --- a/js/plugins/LocalStorage.js +++ b/js/plugins/LocalStorage.js @@ -16,6 +16,16 @@ LocalStorage.prototype.getItem = function(k,cb) { return cb(null, localStorage.getItem(k)); }; +/** + * Same as setItem, but fails if an item already exists + */ +LocalStorage.prototype.createItem = function(name, value, callback) { + if (localStorage.getItem(name)) { + return callback('EEXISTS'); + } + return this.setItem(name, value, callback); +}; + LocalStorage.prototype.setItem = function(k,v,cb) { localStorage.setItem(k,v); return cb();